1 /****************************************************************************
3 * $Id: linphonec.c,v 1.57 2007/11/14 13:40:27 smorlat Exp $
5 * Copyright (C) 2006 Sandro Santilli <strk@keybit.net>
6 * Copyright (C) 2002 Florian Winterstein <flox@gmx.net>
7 * Copyright (C) 2000 Simon MORLAT <simon.morlat@free.fr>
9 ****************************************************************************
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; either version 2
14 * of the License, or (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25 ****************************************************************************/
29 #include <sys/types.h>
33 #include "private.h" /*coreapi/private.h, needed for LINPHONE_VERSION */
39 #include <linphonecore.h>
41 #include "linphonec.h"
50 #include <sys/socket.h>
56 #if defined(_WIN32_WCE)
58 #if !defined(PATH_MAX)
63 #define strdup _strdup
71 #define _(String) gettext(String)
74 #define _(something) (something)
78 #define PACKAGE_DIR ""
81 #ifdef HAVE_X11_XLIB_H
85 /***************************************************************************
89 ***************************************************************************/
92 LinphoneAuthInfo *elem[MAX_PENDING_AUTH];
96 /***************************************************************************
98 * Forward declarations
100 ***************************************************************************/
102 char *lpc_strip_blanks(char *input);
104 static int handle_configfile_migration(void);
105 #if !defined(_WIN32_WCE)
106 static int copy_file(const char *from, const char *to);
107 #endif /*_WIN32_WCE*/
108 static int linphonec_parse_cmdline(int argc, char **argv);
109 static int linphonec_init(int argc, char **argv);
110 static int linphonec_main_loop (LinphoneCore * opm);
111 static int linphonec_idle_call (void);
113 static int linphonec_initialize_readline(void);
114 static int linphonec_finish_readline();
115 static char **linephonec_readline_completion(const char *text,
119 /* These are callback for linphone core */
120 static void linphonec_prompt_for_auth(LinphoneCore *lc, const char *realm,
121 const char *username);
122 static void linphonec_display_refer (LinphoneCore * lc, const char *refer_to);
123 static void linphonec_display_something (LinphoneCore * lc, const char *something);
124 static void linphonec_display_url (LinphoneCore * lc, const char *something, const char *url);
125 static void linphonec_display_warning (LinphoneCore * lc, const char *something);
126 static void linphonec_notify_received(LinphoneCore *lc, LinphoneCall *call, const char *from,const char *event);
128 static void linphonec_notify_presence_received(LinphoneCore *lc,LinphoneFriend *fid);
129 static void linphonec_new_unknown_subscriber(LinphoneCore *lc,
130 LinphoneFriend *lf, const char *url);
132 static void linphonec_text_received(LinphoneCore *lc, LinphoneChatRoom *cr,
133 const LinphoneAddress *from, const char *msg);
134 static void linphonec_display_status (LinphoneCore * lc, const char *something);
135 static void linphonec_dtmf_received(LinphoneCore *lc, LinphoneCall *call, int dtmf);
136 static void print_prompt(LinphoneCore *opm);
137 void linphonec_out(const char *fmt,...);
138 /***************************************************************************
142 ***************************************************************************/
144 LinphoneCore *linphonec;
147 static char *histfile_name=NULL;
148 static char last_in_history[256];
150 //auto answer (-a) option
151 static bool_t auto_answer=FALSE;
152 static bool_t answer_call=FALSE;
153 static bool_t vcap_enabled=FALSE;
154 static bool_t display_enabled=FALSE;
155 static bool_t preview_enabled=FALSE;
156 static bool_t show_general_state=FALSE;
157 static bool_t unix_socket=FALSE;
158 static bool_t linphonec_running=TRUE;
159 LPC_AUTH_STACK auth_stack;
160 static int trace_level = 0;
161 static char *logfile_name = NULL;
162 static char configfile_name[PATH_MAX];
163 static const char *factory_configfile_name=NULL;
164 static char *sip_addr_to_call = NULL; /* for autocall */
165 static int window_id = 0; /* 0=standalone window, or window id for embedding video */
166 #if !defined(_WIN32_WCE)
167 static ortp_pipe_t client_sock=ORTP_PIPE_INVALID;
168 #endif /*_WIN32_WCE*/
169 char prompt[PROMPT_MAX_LEN];
170 #if !defined(_WIN32_WCE)
171 static ortp_thread_t pipe_reader_th;
172 static bool_t pipe_reader_run=FALSE;
173 #endif /*_WIN32_WCE*/
174 #if !defined(_WIN32_WCE)
175 static ortp_pipe_t server_sock;
176 #endif /*_WIN32_WCE*/
178 bool_t linphonec_camera_enabled=TRUE;
182 void linphonec_call_identify(LinphoneCall* call){
183 static long callid=1;
184 linphone_call_set_user_pointer (call,(void*)callid);
188 LinphoneCall *linphonec_get_call(long id){
189 const MSList *elem=linphone_core_get_calls(linphonec);
190 for (;elem!=NULL;elem=elem->next){
191 LinphoneCall *call=(LinphoneCall*)elem->data;
192 if (linphone_call_get_user_pointer (call)==(void*)id){
196 linphonec_out("Sorry, no call with id %i exists at this time.\n",id);
200 /***************************************************************************
202 * Linphone core callbacks
204 ***************************************************************************/
207 * Linphone core callback
210 linphonec_display_refer (LinphoneCore * lc, const char *refer_to)
212 linphonec_out("Receiving out of call refer to %s\n", refer_to);
216 * Linphone core callback
219 linphonec_display_something (LinphoneCore * lc, const char *something)
221 fprintf (stdout, "%s\n%s", something,prompt);
226 * Linphone core callback
229 linphonec_display_status (LinphoneCore * lc, const char *something)
231 fprintf (stdout, "%s\n%s", something,prompt);
236 * Linphone core callback
239 linphonec_display_warning (LinphoneCore * lc, const char *something)
241 fprintf (stdout, "Warning: %s\n%s", something,prompt);
246 * Linphone core callback
249 linphonec_display_url (LinphoneCore * lc, const char *something, const char *url)
251 fprintf (stdout, "%s : %s\n", something, url);
255 * Linphone core callback
258 linphonec_prompt_for_auth(LinphoneCore *lc, const char *realm, const char *username)
260 /* no prompt possible when using pipes or tcp mode*/
262 linphone_core_abort_authentication(lc,NULL);
264 LinphoneAuthInfo *pending_auth;
266 if ( auth_stack.nitems+1 > MAX_PENDING_AUTH )
269 "Can't accept another authentication request.\n"
270 "Consider incrementing MAX_PENDING_AUTH macro.\n");
274 pending_auth=linphone_auth_info_new(username,NULL,NULL,NULL,realm);
275 auth_stack.elem[auth_stack.nitems++]=pending_auth;
280 * Linphone core callback
283 linphonec_notify_received(LinphoneCore *lc, LinphoneCall *call, const char *from,const char *event)
285 if(!strcmp(event,"refer"))
287 linphonec_out("The distand endpoint %s of call %li has been transfered, you can safely close the call.\n",
288 from,(long)linphone_call_get_user_pointer (call));
294 * Linphone core callback
297 linphonec_notify_presence_received(LinphoneCore *lc,LinphoneFriend *fid)
299 char *tmp=linphone_address_as_string(linphone_friend_get_address(fid));
300 printf("Friend %s is %s\n", tmp, linphone_online_status_to_string(linphone_friend_get_status(fid)));
302 // todo: update Friend list state (unimplemented)
306 * Linphone core callback
309 linphonec_new_unknown_subscriber(LinphoneCore *lc, LinphoneFriend *lf,
312 printf("Friend %s requested subscription "
313 "(accept/deny is not implemented yet)\n", url);
314 // This means that this person wishes to be notified
315 // of your presence information (online, busy, away...).
319 static void linphonec_call_updated(LinphoneCall *call){
320 const LinphoneCallParams *cp=linphone_call_get_current_params(call);
321 if (!linphone_call_camera_enabled (call) && linphone_call_params_video_enabled (cp)){
322 linphonec_out("Far end requests to share video.\nType 'camera on' if you agree.\n");
326 static void linphonec_call_state_changed(LinphoneCore *lc, LinphoneCall *call, LinphoneCallState st, const char *msg){
327 char *from=linphone_call_get_remote_address_as_string(call);
328 long id=(long)linphone_call_get_user_pointer (call);
330 case LinphoneCallEnd:
331 linphonec_out("Call %i with %s ended (%s).\n", id, from, linphone_reason_to_string(linphone_call_get_reason(call)));
333 case LinphoneCallResuming:
334 linphonec_out("Resuming call %i with %s.\n", id, from);
336 case LinphoneCallStreamsRunning:
337 linphonec_out("Media streams established with %s for call %i.\n", from,id);
339 case LinphoneCallPausing:
340 linphonec_out("Pausing call %i with %s.\n", id, from);
342 case LinphoneCallPaused:
343 linphonec_out("Call %i with %s is now paused.\n", id, from);
345 case LinphoneCallPausedByRemote:
346 linphonec_out("Call %i has been paused by %s.\n",id,from);
348 case LinphoneCallIncomingReceived:
349 linphonec_call_identify(call);
350 linphone_call_enable_camera (call,linphonec_camera_enabled);
351 id=(long)linphone_call_get_user_pointer (call);
352 linphonec_set_caller(from);
356 linphonec_out("Receiving new incoming call from %s, assigned id %i\n", from,id);
358 case LinphoneCallOutgoingInit:
359 linphonec_call_identify(call);
360 id=(long)linphone_call_get_user_pointer (call);
361 linphonec_out("Establishing call id to %s, assigned id %i\n", from,id);
363 case LinphoneCallUpdatedByRemote:
364 linphonec_call_updated(call);
366 case LinphoneCallOutgoingProgress:
367 linphonec_out("Call %i to %s in progress.\n", id, from);
369 case LinphoneCallOutgoingRinging:
370 linphonec_out("Call %i to %s ringing.\n", id, from);
372 case LinphoneCallConnected:
373 linphonec_out("Call %i with %s connected.\n", id, from);
375 case LinphoneCallOutgoingEarlyMedia:
376 linphonec_out("Call %i with %s early media.\n", id, from);
378 case LinphoneCallError:
379 linphonec_out("Call %i with %s error.\n", id, from);
388 * Linphone core callback
391 linphonec_text_received(LinphoneCore *lc, LinphoneChatRoom *cr,
392 const LinphoneAddress *from, const char *msg)
394 linphonec_out("Message received from %s: %s\n", linphone_address_as_string(from), msg);
395 // TODO: provide mechanism for answering.. ('say' command?)
399 static void linphonec_dtmf_received(LinphoneCore *lc, LinphoneCall *call, int dtmf){
400 char *from=linphone_call_get_remote_address_as_string(call);
401 fprintf(stdout,"Receiving tone %c from %s\n",dtmf,from);
406 static char received_prompt[PROMPT_MAX_LEN];
407 static ms_mutex_t prompt_mutex;
408 static bool_t have_prompt=FALSE;
410 static void *prompt_reader_thread(void *arg){
412 char tmp[PROMPT_MAX_LEN];
413 while ((ret=fgets(tmp,sizeof(tmp),stdin))!=NULL){
414 ms_mutex_lock(&prompt_mutex);
415 strcpy(received_prompt,ret);
417 ms_mutex_unlock(&prompt_mutex);
422 static void start_prompt_reader(void){
424 ms_mutex_init(&prompt_mutex,NULL);
425 ortp_thread_create(&th,NULL,prompt_reader_thread,NULL);
427 #if !defined(_WIN32_WCE)
428 static ortp_pipe_t create_server_socket(void){
431 snprintf(path,sizeof(path)-1,"linphonec-%i",getuid());
435 DWORD size=sizeof(username)-1;
436 GetUserName(username,&size);
437 snprintf(path,sizeof(path)-1,"linphonec-%s",username);
440 return ortp_server_pipe_create(path);
444 static void *pipe_thread(void*p){
446 server_sock=create_server_socket();
447 if (server_sock==ORTP_PIPE_INVALID) return NULL;
448 while(pipe_reader_run){
449 while(client_sock!=ORTP_PIPE_INVALID){ /*sleep until the last command is finished*/
456 client_sock=ortp_server_pipe_accept_client(server_sock);
457 if (client_sock!=ORTP_PIPE_INVALID){
459 /*now read from the client */
460 if ((len=ortp_pipe_read(client_sock,(uint8_t*)tmp,sizeof(tmp)-1))>0){
461 ortp_mutex_lock(&prompt_mutex);
463 strcpy(received_prompt,tmp);
464 printf("Receiving command '%s'\n",received_prompt);fflush(stdout);
466 ortp_mutex_unlock(&prompt_mutex);
468 printf("read nothing\n");fflush(stdout);
469 ortp_server_pipe_close_client(client_sock);
470 client_sock=ORTP_PIPE_INVALID;
474 if (pipe_reader_run) fprintf(stderr,"accept() failed: %s\n",strerror(errno));
477 ms_message("Exiting pipe_reader_thread.");
482 static void start_pipe_reader(void){
483 ms_mutex_init(&prompt_mutex,NULL);
484 pipe_reader_run=TRUE;
485 ortp_thread_create(&pipe_reader_th,NULL,pipe_thread,NULL);
488 static void stop_pipe_reader(void){
489 pipe_reader_run=FALSE;
490 linphonec_command_finished();
491 ortp_server_pipe_close(server_sock);
492 ortp_thread_join(pipe_reader_th,NULL);
494 #endif /*_WIN32_WCE*/
497 #define BOOL_HAVE_READLINE 1
499 #define BOOL_HAVE_READLINE 0
502 char *linphonec_readline(char *prompt){
503 if (unix_socket || !BOOL_HAVE_READLINE ){
504 static bool_t prompt_reader_started=FALSE;
505 static bool_t pipe_reader_started=FALSE;
506 if (!prompt_reader_started){
507 start_prompt_reader();
508 prompt_reader_started=TRUE;
510 if (unix_socket && !pipe_reader_started){
511 #if !defined(_WIN32_WCE)
513 pipe_reader_started=TRUE;
514 #endif /*_WIN32_WCE*/
516 fprintf(stdout,"%s",prompt);
519 ms_mutex_lock(&prompt_mutex);
521 char *ret=strdup(received_prompt);
523 ms_mutex_unlock(&prompt_mutex);
526 ms_mutex_unlock(&prompt_mutex);
527 linphonec_idle_call();
530 /* Following is to get the video window going as it
531 should. Maybe should we only have this on when the option -V
535 if (PeekMessage(&msg, NULL, 0, 0,1)) {
536 TranslateMessage(&msg);
537 DispatchMessage(&msg);
545 return readline(prompt);
550 void linphonec_out(const char *fmt,...){
553 va_start (args, fmt);
554 res=ortp_strdup_vprintf(fmt,args);
558 #if !defined(_WIN32_WCE)
559 if (client_sock!=ORTP_PIPE_INVALID){
560 if (ortp_pipe_write(client_sock,(uint8_t*)res,strlen(res))==-1){
561 fprintf(stderr,"Fail to send output via pipe: %s",strerror(errno));
564 #endif /*_WIN32_WCE*/
568 void linphonec_command_finished(void){
569 #if !defined(_WIN32_WCE)
570 if (client_sock!=ORTP_PIPE_INVALID){
571 ortp_server_pipe_close_client(client_sock);
572 client_sock=ORTP_PIPE_INVALID;
574 #endif /*_WIN32_WCE*/
577 void linphonec_set_autoanswer(bool_t enabled){
581 bool_t linphonec_get_autoanswer(){
585 LinphoneCoreVTable linphonec_vtable={0};
587 /***************************************************************************/
593 * - char *histfile_name
596 #if defined (_WIN32_WCE)
598 char **convert_args_to_ascii(int argc, _TCHAR **wargv){
600 char **result=malloc(argc*sizeof(char*));
603 wcstombs(argtmp,wargv[i],sizeof(argtmp));
604 result[i]=strdup(argtmp);
609 int _tmain(int argc, _TCHAR* wargv[]) {
610 char **argv=convert_args_to_ascii(argc,wargv);
615 main (int argc, char *argv[]) {
617 linphonec_vtable.call_state_changed=linphonec_call_state_changed;
618 linphonec_vtable.notify_presence_recv = linphonec_notify_presence_received;
619 linphonec_vtable.new_subscription_request = linphonec_new_unknown_subscriber;
620 linphonec_vtable.auth_info_requested = linphonec_prompt_for_auth;
621 linphonec_vtable.display_status = linphonec_display_status;
622 linphonec_vtable.display_message=linphonec_display_something;
623 linphonec_vtable.display_warning=linphonec_display_warning;
624 linphonec_vtable.display_url=linphonec_display_url;
625 linphonec_vtable.text_received=linphonec_text_received;
626 linphonec_vtable.dtmf_received=linphonec_dtmf_received;
627 linphonec_vtable.refer_received=linphonec_display_refer;
628 linphonec_vtable.notify_recv=linphonec_notify_received;
630 if (! linphonec_init(argc, argv) ) exit(EXIT_FAILURE);
632 linphonec_main_loop (linphonec);
634 linphonec_finish(EXIT_SUCCESS);
636 exit(EXIT_SUCCESS); /* should never reach here */
640 * Initialize linphonec
643 linphonec_init(int argc, char **argv)
646 //g_mem_set_vtable(&dbgtable);
649 * Set initial values for global variables
655 snprintf(configfile_name, PATH_MAX, "%s/.linphonerc",
657 #elif defined(_WIN32_WCE)
658 strncpy(configfile_name,PACKAGE_DIR "\\linphonerc",PATH_MAX);
659 mylogfile=fopen(PACKAGE_DIR "\\" "linphonec.log","w");
660 printf("Logs are redirected in" PACKAGE_DIR "\\linphonec.log");
662 snprintf(configfile_name, PATH_MAX, "%s/Linphone/linphonerc",
665 /* Handle configuration filename changes */
666 switch (handle_configfile_migration())
668 case -1: /* error during file copies */
670 "Error in configuration file migration\n");
673 case 0: /* nothing done */
674 case 1: /* migrated */
680 if (NULL == bindtextdomain (GETTEXT_PACKAGE, PACKAGE_LOCALE_DIR))
681 perror ("bindtextdomain failed");
683 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
685 textdomain (GETTEXT_PACKAGE);
687 printf ("NLS disabled.\n");
690 linphonec_parse_cmdline(argc, argv);
694 if (logfile_name != NULL)
695 mylogfile = fopen (logfile_name, "w+");
697 if (mylogfile == NULL)
701 "INFO: no logfile, logging to stdout\n");
703 linphone_core_enable_logs(mylogfile);
707 linphone_core_disable_logs();
710 * Initialize auth stack
715 * Initialize linphone core
717 linphonec=linphone_core_new (&linphonec_vtable, configfile_name, factory_configfile_name, NULL);
718 linphone_core_enable_video(linphonec,vcap_enabled,display_enabled);
719 if (display_enabled && window_id != 0)
721 printf ("Setting window_id: 0x%x\n", window_id);
722 linphone_core_set_native_video_window_id(linphonec,window_id);
725 linphone_core_enable_video_preview(linphonec,preview_enabled);
726 if (!(vcap_enabled || display_enabled)) printf("Warning: video is disabled in linphonec, use -V or -C or -D to enable.\n");
729 * Initialize readline
731 linphonec_initialize_readline();
733 #if !defined(_WIN32_WCE)
735 * Initialize signal handlers
737 signal(SIGTERM, linphonec_finish);
738 signal(SIGINT, linphonec_finish);
739 #endif /*_WIN32_WCE*/
744 void linphonec_main_loop_exit(void){
745 linphonec_running=FALSE;
749 * Close linphonec, cleanly terminating
753 linphonec_finish(int exit_status)
755 // Do not allow concurrent destroying to prevent glibc errors
756 static bool_t terminating=FALSE;
757 if (terminating) return;
759 linphonec_out("Terminating...\n");
761 /* Terminate any pending call */
762 linphone_core_terminate_all_calls(linphonec);
764 linphonec_finish_readline();
766 #if !defined(_WIN32_WCE)
769 #endif /*_WIN32_WCE*/
771 linphone_core_destroy (linphonec);
773 if (mylogfile != NULL && mylogfile != stdout)
783 * This is called from idle_call() whenever
784 * pending_auth != NULL.
786 * It prompts user for a password.
787 * Hitting ^D (EOF) would make this function
789 * Any other input would try to set linphone core
790 * auth_password for the pending_auth, add the auth_info
794 linphonec_prompt_for_auth_final(LinphoneCore *lc)
796 static int reentrancy=0;
798 char auth_prompt[256];
800 rl_hook_func_t *old_event_hook;
803 if (reentrancy!=0) return 0;
807 LinphoneAuthInfo *pending_auth=auth_stack.elem[auth_stack.nitems-1];
809 snprintf(auth_prompt, 256, "Password for %s on %s: ",
810 pending_auth->username, pending_auth->realm);
815 * Disable event hook to avoid entering an
816 * infinite loop. This would prevent idle_call
817 * from being called during authentication reads.
818 * Note that it might be undesiderable...
820 old_event_hook=rl_event_hook;
826 input=linphonec_readline(auth_prompt);
829 * If EOF (^D) is sent you probably don't want
830 * to provide an auth password... should give up
831 * the operation, but there's no mechanism to
832 * send this info back to caller currently...
836 printf("Cancel requested, but not implemented.\n");
841 iptr=lpc_strip_blanks(input);
844 * Only blanks, continue asking
852 /* Something typed, let's try */
857 * No check is done here to ensure password is correct.
858 * I guess password will be asked again later.
860 linphone_auth_info_set_passwd(pending_auth, input);
861 linphone_core_add_auth_info(lc, pending_auth);
862 linphone_auth_info_destroy(pending_auth);
863 auth_stack.elem[auth_stack.nitems-1]=0;
864 --(auth_stack.nitems);
867 * Reset line_buffer, to avoid the password
868 * to be used again from outer readline
870 rl_line_buffer[0]='\0';
871 rl_event_hook=old_event_hook;
877 print_usage (int exit_status)
879 fprintf (stdout, "\n\
880 usage: linphonec [-c file] [-s sipaddr] [-a] [-V] [-d level ] [-l logfile]\n\
883 -b file specify path of readonly factory configuration file.\n\
884 -c file specify path of configuration file.\n\
885 -d level be verbose. 0 is no output. 6 is all output\n\
886 -l logfile specify the log file for your SIP phone\n\
887 -s sipaddress specify the sip call to do at startup\n\
888 -a enable auto answering for incoming calls\n\
889 -V enable video features globally (disabled by default)\n\
890 -C enable video capture only (disabled by default)\n\
891 -D enable video display only (disabled by default)\n\
892 -S show general state messages (disabled by default)\n\
893 --wid windowid force embedding of video window into provided windowid (disabled by default)\n\
894 -v or --version display version and exits.\n");
901 #ifdef HAVE_X11_XLIB_H
902 static void x11_apply_video_params(VideoParams *params, Window window){
904 unsigned int flags=0;
905 static Display *display = NULL;
906 const char *dname=getenv("DISPLAY");
908 if (display==NULL && dname!=NULL){
909 display=XOpenDisplay(dname);
913 ms_error("Could not open display %s",dname);
916 memset(&wc,0,sizeof(wc));
925 flags|=CWWidth|CWHeight;
927 /*printf("XConfigureWindow x=%i,y=%i,w=%i,h=%i\n",
928 wc.x, wc.y ,wc.width, wc.height);*/
929 XConfigureWindow(display,window,flags,&wc);
931 XMapWindow(display,window);
933 XUnmapWindow(display,window);
934 XSync(display,FALSE);
939 static void lpc_apply_video_params(){
940 static unsigned long old_wid=0,old_pwid=0;
941 unsigned long wid=linphone_core_get_native_video_window_id (linphonec);
942 unsigned long pwid=linphone_core_get_native_preview_window_id (linphonec);
944 if (wid!=0 && (lpc_video_params.refresh || old_wid!=wid)){
945 lpc_video_params.refresh=FALSE;
946 #ifdef HAVE_X11_XLIB_H
947 if (lpc_video_params.wid==0){ // do not manage window if embedded
948 x11_apply_video_params(&lpc_video_params,wid);
953 if (pwid!=0 && (lpc_preview_params.refresh || old_pwid!=pwid)){
954 lpc_preview_params.refresh=FALSE;
955 #ifdef HAVE_X11_XLIB_H
956 /*printf("wid=%lu pwid=%lu\n",wid,pwid);*/
957 if (lpc_preview_params.wid==0){ // do not manage window if embedded
958 printf("Refreshing\n");
959 x11_apply_video_params(&lpc_preview_params,pwid);
971 * Called every second from main read loop.
973 * Will use the following globals:
975 * - LinphoneCore linphonec
976 * - LPC_AUTH_STACK auth_stack;
980 linphonec_idle_call ()
982 LinphoneCore *opm=linphonec;
984 /* Uncomment the following to verify being called */
987 linphone_core_iterate(opm);
989 fprintf (stdout, "-------auto answering to call-------\n" );
990 linphone_core_accept_call(opm,NULL);
993 /* auto call handling */
994 if (sip_addr_to_call != NULL )
997 snprintf (buf, sizeof(buf),"call %s", sip_addr_to_call);
998 sip_addr_to_call=NULL;
999 linphonec_parse_command_line(linphonec, buf);
1002 if ( auth_stack.nitems )
1005 * Inhibit command completion
1006 * during password prompts
1008 #ifdef HAVE_READLINE
1009 rl_inhibit_completion=1;
1011 linphonec_prompt_for_auth_final(opm);
1012 #ifdef HAVE_READLINE
1013 rl_inhibit_completion=0;
1017 #ifdef VIDEO_ENABLED
1018 lpc_apply_video_params();
1024 #ifdef HAVE_READLINE
1028 * - char *histfile_name (also sets this)
1029 * - char *last_in_history (allocates it)
1032 linphonec_initialize_readline()
1034 /*rl_bind_key('\t', rl_insert);*/
1036 /* Allow conditional parsing of ~/.inputrc */
1037 rl_readline_name = "linphonec";
1039 /* Call idle_call() every second */
1040 rl_set_keyboard_input_timeout(LPC_READLINE_TIMEOUT);
1041 rl_event_hook=linphonec_idle_call;
1043 /* Set history file and read it */
1044 histfile_name = ms_strdup_printf ("%s/.linphonec_history",
1046 read_history(histfile_name);
1048 /* Initialized last_in_history cache*/
1049 last_in_history[0] = '\0';
1051 /* Register a completion function */
1052 rl_attempted_completion_function = linephonec_readline_completion;
1054 /* printf("Readline initialized.\n"); */
1062 * - char *histfile_name (writes history to file and frees it)
1063 * - char *last_in_history (frees it)
1067 linphonec_finish_readline()
1070 stifle_history(HISTSIZE);
1071 write_history(histfile_name);
1072 free(histfile_name);
1079 static void print_prompt(LinphoneCore *opm){
1080 #ifdef IDENTITY_AS_PROMPT
1081 snprintf(prompt, PROMPT_MAX_LEN, "%s> ",
1082 linphone_core_get_primary_contact(opm));
1084 snprintf(prompt, PROMPT_MAX_LEN, "linphonec> ");
1089 linphonec_main_loop (LinphoneCore * opm)
1096 while (linphonec_running && (input=linphonec_readline(prompt)))
1098 char *iptr; /* input and input pointer */
1102 iptr=lpc_strip_blanks(input);
1104 input_len = strlen(iptr);
1107 * Do nothing but release memory
1108 * if only blanks are read
1116 #ifdef HAVE_READLINE
1118 * Only add to history if not already
1119 * last item in it, and only if the command
1120 * doesn't start with a space (to allow for
1123 if ( iptr == input && strcmp(last_in_history, iptr) )
1125 strncpy(last_in_history,iptr,sizeof(last_in_history));
1126 last_in_history[sizeof(last_in_history)-1]='\0';
1131 linphonec_parse_command_line(linphonec, iptr);
1132 linphonec_command_finished();
1140 * Parse command line switches
1145 * - char *logfile_name
1146 * - char *configfile_name
1150 linphonec_parse_cmdline(int argc, char **argv)
1154 while (arg_num < argc)
1156 int old_arg_num = arg_num;
1157 if (strncmp ("-d", argv[arg_num], 2) == 0)
1161 trace_level = atoi (argv[arg_num]);
1165 else if (strncmp ("-l", argv[arg_num], 2) == 0)
1169 logfile_name = argv[arg_num];
1171 else if (strncmp ("-c", argv[arg_num], 2) == 0)
1173 if ( ++arg_num >= argc ) print_usage(EXIT_FAILURE);
1174 #if !defined(_WIN32_WCE)
1175 if (access(argv[arg_num],F_OK)!=0 )
1178 "Cannot open config file %s.\n",
1182 #endif /*_WIN32_WCE*/
1183 snprintf(configfile_name, PATH_MAX, "%s", argv[arg_num]);
1185 else if (strncmp ("-b", argv[arg_num], 2) == 0)
1187 if ( ++arg_num >= argc ) print_usage(EXIT_FAILURE);
1188 #if !defined(_WIN32_WCE)
1189 if (access(argv[arg_num],F_OK)!=0 )
1192 "Cannot open config file %s.\n",
1196 #endif /*_WIN32_WCE*/
1197 factory_configfile_name = argv[arg_num];
1199 else if (strncmp ("-s", argv[arg_num], 2) == 0)
1203 sip_addr_to_call = argv[arg_num];
1205 else if (strncmp ("-a", argv[arg_num], 2) == 0)
1209 else if (strncmp ("-C", argv[arg_num], 2) == 0)
1211 vcap_enabled = TRUE;
1213 else if (strncmp ("-D", argv[arg_num], 2) == 0)
1215 display_enabled = TRUE;
1217 else if (strncmp ("-V", argv[arg_num], 2) == 0)
1219 display_enabled = TRUE;
1220 vcap_enabled = TRUE;
1221 preview_enabled=TRUE;
1223 else if ((strncmp ("-v", argv[arg_num], 2) == 0)
1226 ("--version", argv[arg_num],
1227 strlen ("--version")) == 0))
1229 #if !defined(_WIN32_WCE)
1230 printf ("version: " LINPHONE_VERSION "\n");
1232 exit (EXIT_SUCCESS);
1234 else if (strncmp ("-S", argv[arg_num], 2) == 0)
1236 show_general_state = TRUE;
1238 else if (strncmp ("--pipe", argv[arg_num], 6) == 0)
1242 else if (strncmp ("--wid", argv[arg_num], 5) == 0)
1245 if (arg_num < argc) {
1247 window_id = strtol( argv[arg_num], &tmp, 0 );
1250 else if (old_arg_num == arg_num)
1252 fprintf (stderr, "ERROR: bad arguments\n");
1253 print_usage (EXIT_FAILURE);
1262 * Up to version 1.2.1 linphone used ~/.linphonec for
1263 * CLI and ~/.gnome2/linphone for GUI as configuration file.
1264 * In newer version both interfaces will use ~/.linphonerc.
1266 * This function helps transparently migrating from one
1267 * to the other layout using the following heuristic:
1269 * IF new_config EXISTS => do nothing
1270 * ELSE IF old_cli_config EXISTS => copy to new_config
1271 * ELSE IF old_gui_config EXISTS => copy to new_config
1274 * 0 if it did nothing
1275 * 1 if it migrated successfully
1279 handle_configfile_migration()
1281 #if !defined(_WIN32_WCE)
1285 #if !defined(_WIN32_WCE)
1286 const char *home = getenv("HOME");
1288 const char *home = ".";
1289 #endif /*_WIN32_WCE*/
1290 new_cfg = ms_strdup_printf("%s/.linphonerc", home);
1293 * If the *NEW* configuration already exists
1296 if (access(new_cfg,F_OK)==0)
1302 old_cfg_cli = ms_strdup_printf("%s/.linphonec", home);
1305 * If the *OLD* CLI configurations exist copy it to
1306 * the new file and make it a symlink.
1308 if (access(old_cfg_cli, F_OK)==0)
1310 if ( ! copy_file(old_cfg_cli, new_cfg) )
1316 printf("%s copied to %s\n", old_cfg_cli, new_cfg);
1323 old_cfg_gui = ms_strdup_printf("%s/.gnome2/linphone", home);
1326 * If the *OLD* GUI configurations exist copy it to
1327 * the new file and make it a symlink.
1329 if (access(old_cfg_gui, F_OK)==0)
1331 if ( ! copy_file(old_cfg_gui, new_cfg) )
1338 printf("%s copied to %s\n", old_cfg_gui, new_cfg);
1346 #endif /*_WIN32_WCE*/
1349 #if !defined(_WIN32_WCE)
1351 * Copy file "from" to file "to".
1352 * Destination file is truncated if existing.
1353 * Return 1 on success, 0 on error (printing an error).
1356 copy_file(const char *from, const char *to)
1363 /* Open "from" file for reading */
1364 in=fopen(from, "r");
1367 snprintf(message, 255, "Can't open %s for reading: %s\n",
1368 from, strerror(errno));
1369 fprintf(stderr, "%s", message);
1373 /* Open "to" file for writing (will truncate existing files) */
1377 snprintf(message, 255, "Can't open %s for writing: %s\n",
1378 to, strerror(errno));
1379 fprintf(stderr, "%s", message);
1383 /* Copy data from "in" to "out" */
1384 while ( (n=fread(buf, 1, sizeof buf, in)) > 0 )
1386 if ( ! fwrite(buf, 1, n, out) )
1397 #endif /*_WIN32_WCE*/
1399 #ifdef HAVE_READLINE
1401 linephonec_readline_completion(const char *text, int start, int end)
1403 char **matches = NULL;
1406 * Prevent readline from falling
1407 * back to filename-completion
1409 rl_attempted_completion_over=1;
1412 * If this is the start of line we complete with commands
1416 return rl_completion_matches(text, linphonec_command_generator);
1420 * Otherwise, we should peek at command name
1421 * or context to implement a smart completion.
1422 * For example: "call .." could return
1423 * friends' sip-uri as matches
1432 * Strip blanks from a string.
1433 * Return a pointer into the provided string.
1434 * Modifies input adding a NULL at first
1435 * of trailing blanks.
1438 lpc_strip_blanks(char *input)
1442 /* Find first non-blank */
1443 while(*input && isspace(*input)) ++input;
1445 /* Find last non-blank */
1446 iptr=input+strlen(input);
1448 while(isspace(*--iptr));
1455 /****************************************************************************
1457 * $Log: linphonec.c,v $
1458 * Revision 1.57 2007/11/14 13:40:27 smorlat
1459 * fix --disable-video build.
1461 * Revision 1.56 2007/09/26 14:07:27 fixkowalski
1462 * - ANSI/C++ compilation issues with non-GCC compilers
1463 * - Faster epm-based packaging
1464 * - Ability to build & run on FC6's eXosip/osip
1466 * Revision 1.55 2007/09/24 16:01:58 smorlat
1469 * Revision 1.54 2007/08/22 14:06:11 smorlat
1470 * authentication bugs fixed.
1472 * Revision 1.53 2007/02/13 21:31:01 smorlat
1473 * added patch for general state.
1474 * new doxygen for oRTP
1477 * Revision 1.52 2007/01/10 14:11:24 smorlat
1478 * add --video to linphonec.
1480 * Revision 1.51 2006/08/21 12:49:59 smorlat
1481 * merged several little patches.
1483 * Revision 1.50 2006/07/26 08:17:28 smorlat
1486 * Revision 1.49 2006/07/17 18:45:00 smorlat
1487 * support for several event queues in ortp.
1488 * glib dependency removed from coreapi/ and console/
1490 * Revision 1.48 2006/04/09 12:45:32 smorlat
1491 * linphonec improvements.
1493 * Revision 1.47 2006/04/04 08:04:34 smorlat
1494 * switched to mediastreamer2, most bugs fixed.
1496 * Revision 1.46 2006/03/16 17:17:40 smorlat
1499 * Revision 1.45 2006/03/12 21:48:31 smorlat
1500 * gcc-2.95 compile error fixed.
1501 * mediastreamer2 in progress
1503 * Revision 1.44 2006/03/04 11:17:10 smorlat
1504 * mediastreamer2 in progress.
1506 * Revision 1.43 2006/02/13 09:50:50 strk
1507 * fixed unused variable warning.
1509 * Revision 1.42 2006/02/02 15:39:18 strk
1510 * - Added 'friend list' and 'friend call' commands
1511 * - Allowed for multiple DTFM send in a single line
1512 * - Added status-specific callback (bare version)
1514 * Revision 1.41 2006/02/02 13:30:05 strk
1515 * - Padded vtable with missing callbacks
1516 * (fixing a segfault on friends subscription)
1517 * - Handled friends notify (bare version)
1518 * - Handled text messages receive (bare version)
1519 * - Printed message on subscription request (bare version)
1521 * Revision 1.40 2006/01/26 09:48:05 strk
1522 * Added limits.h include
1524 * Revision 1.39 2006/01/26 02:11:01 strk
1525 * Removed unused variables, fixed copyright date
1527 * Revision 1.38 2006/01/25 18:33:02 strk
1528 * Removed the -t swich, terminate_on_close made the default behaviour
1530 * Revision 1.37 2006/01/20 14:12:34 strk
1531 * Added linphonec_init() and linphonec_finish() functions.
1532 * Handled SIGINT and SIGTERM to invoke linphonec_finish().
1533 * Handling of auto-termination (-t) moved to linphonec_finish().
1534 * Reworked main (input read) loop to not rely on 'terminate'
1535 * and 'run' variable (dropped). configfile_name allocated on stack
1536 * using PATH_MAX limit. Changed print_usage signature to allow
1537 * for an exit_status specification.
1539 * Revision 1.36 2006/01/18 09:25:32 strk
1540 * Command completion inhibited in proxy addition and auth request prompts.
1541 * Avoided use of readline's internal filename completion.
1543 * Revision 1.35 2006/01/14 13:29:32 strk
1544 * Reworked commands interface to use a table structure,
1545 * used by command line parser and help function.
1546 * Implemented first level of completion (commands).
1547 * Added notification of invalid "answer" and "terminate"
1548 * commands (no incoming call, no active call).
1549 * Forbidden "call" intialization when a call is already active.
1550 * Cleaned up all commands, adding more feedback and error checks.
1552 * Revision 1.34 2006/01/13 13:00:29 strk
1553 * Added linphonec.h. Code layout change (added comments, forward decl,
1554 * globals on top, copyright notices and Logs). Handled out-of-memory
1555 * condition on history management. Removed assumption on sizeof(char).
1556 * Fixed bug in authentication prompt (introduced by readline).
1557 * Added support for multiple authentication requests (up to MAX_PENDING_AUTH).
1560 ****************************************************************************/