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
83 #include <SDL/SDL_syswm.h>
86 /***************************************************************************
90 ***************************************************************************/
93 LinphoneAuthInfo *elem[MAX_PENDING_AUTH];
97 /***************************************************************************
99 * Forward declarations
101 ***************************************************************************/
103 char *lpc_strip_blanks(char *input);
105 static int handle_configfile_migration(void);
106 #if !defined(_WIN32_WCE)
107 static int copy_file(const char *from, const char *to);
108 #endif /*_WIN32_WCE*/
109 static int linphonec_parse_cmdline(int argc, char **argv);
110 static int linphonec_init(int argc, char **argv);
111 static int linphonec_main_loop (LinphoneCore * opm, char * sipAddr);
112 static int linphonec_idle_call (void);
114 static int linphonec_initialize_readline(void);
115 static int linphonec_finish_readline();
116 static char **linephonec_readline_completion(const char *text,
120 /* These are callback for linphone core */
121 static void linphonec_prompt_for_auth(LinphoneCore *lc, const char *realm,
122 const char *username);
123 static void linphonec_display_refer (LinphoneCore * lc, const char *refer_to);
124 static void linphonec_display_something (LinphoneCore * lc, const char *something);
125 static void linphonec_display_url (LinphoneCore * lc, const char *something, const char *url);
126 static void linphonec_display_warning (LinphoneCore * lc, const char *something);
127 static void linphonec_notify_received(LinphoneCore *lc, LinphoneCall *call, const char *from,const char *event);
129 static void linphonec_notify_presence_received(LinphoneCore *lc,LinphoneFriend *fid);
130 static void linphonec_new_unknown_subscriber(LinphoneCore *lc,
131 LinphoneFriend *lf, const char *url);
133 static void linphonec_text_received(LinphoneCore *lc, LinphoneChatRoom *cr,
134 const LinphoneAddress *from, const char *msg);
135 static void linphonec_display_status (LinphoneCore * lc, const char *something);
136 static void linphonec_dtmf_received(LinphoneCore *lc, LinphoneCall *call, int dtmf);
137 static void print_prompt(LinphoneCore *opm);
138 void linphonec_out(const char *fmt,...);
139 /***************************************************************************
143 ***************************************************************************/
145 LinphoneCore *linphonec;
148 static char *histfile_name=NULL;
149 static char last_in_history[256];
151 //auto answer (-a) option
152 static bool_t auto_answer=FALSE;
153 static bool_t answer_call=FALSE;
154 static bool_t vcap_enabled=FALSE;
155 static bool_t display_enabled=FALSE;
156 static bool_t preview_enabled=FALSE;
157 static bool_t show_general_state=FALSE;
158 static bool_t unix_socket=FALSE;
159 static bool_t linphonec_running=TRUE;
160 LPC_AUTH_STACK auth_stack;
161 static int trace_level = 0;
162 static char *logfile_name = NULL;
163 static char configfile_name[PATH_MAX];
164 static const char *factory_configfile_name=NULL;
165 static char *sipAddr = NULL; /* for autocall */
166 static int window_id = 0; /* 0=standalone window, or window id for embedding video */
167 #if !defined(_WIN32_WCE)
168 static ortp_pipe_t client_sock=ORTP_PIPE_INVALID;
169 #endif /*_WIN32_WCE*/
170 char prompt[PROMPT_MAX_LEN];
171 #if !defined(_WIN32_WCE)
172 static ortp_thread_t pipe_reader_th;
173 static bool_t pipe_reader_run=FALSE;
174 #endif /*_WIN32_WCE*/
175 #if !defined(_WIN32_WCE)
176 static ortp_pipe_t server_sock;
177 #endif /*_WIN32_WCE*/
179 bool_t linphonec_camera_enabled=TRUE;
183 void linphonec_call_identify(LinphoneCall* call){
184 static long callid=1;
185 linphone_call_set_user_pointer (call,(void*)callid);
189 LinphoneCall *linphonec_get_call(long id){
190 const MSList *elem=linphone_core_get_calls(linphonec);
191 for (;elem!=NULL;elem=elem->next){
192 LinphoneCall *call=(LinphoneCall*)elem->data;
193 if (linphone_call_get_user_pointer (call)==(void*)id){
197 linphonec_out("Sorry, no call with id %i exists at this time.\n",id);
201 /***************************************************************************
203 * Linphone core callbacks
205 ***************************************************************************/
208 * Linphone core callback
211 linphonec_display_refer (LinphoneCore * lc, const char *refer_to)
213 linphonec_out("Receiving out of call refer to %s\n", refer_to);
217 * Linphone core callback
220 linphonec_display_something (LinphoneCore * lc, const char *something)
222 fprintf (stdout, "%s\n%s", something,prompt);
227 * Linphone core callback
230 linphonec_display_status (LinphoneCore * lc, const char *something)
232 fprintf (stdout, "%s\n%s", something,prompt);
237 * Linphone core callback
240 linphonec_display_warning (LinphoneCore * lc, const char *something)
242 fprintf (stdout, "Warning: %s\n%s", something,prompt);
247 * Linphone core callback
250 linphonec_display_url (LinphoneCore * lc, const char *something, const char *url)
252 fprintf (stdout, "%s : %s\n", something, url);
256 * Linphone core callback
259 linphonec_prompt_for_auth(LinphoneCore *lc, const char *realm, const char *username)
261 /* no prompt possible when using pipes or tcp mode*/
263 linphone_core_abort_authentication(lc,NULL);
265 LinphoneAuthInfo *pending_auth;
267 if ( auth_stack.nitems+1 > MAX_PENDING_AUTH )
270 "Can't accept another authentication request.\n"
271 "Consider incrementing MAX_PENDING_AUTH macro.\n");
275 pending_auth=linphone_auth_info_new(username,NULL,NULL,NULL,realm);
276 auth_stack.elem[auth_stack.nitems++]=pending_auth;
281 * Linphone core callback
284 linphonec_notify_received(LinphoneCore *lc, LinphoneCall *call, const char *from,const char *event)
286 if(!strcmp(event,"refer"))
288 linphonec_out("The distand endpoint %s of call %li has been transfered, you can safely close the call.\n",
289 from,(long)linphone_call_get_user_pointer (call));
295 * Linphone core callback
298 linphonec_notify_presence_received(LinphoneCore *lc,LinphoneFriend *fid)
300 char *tmp=linphone_address_as_string(linphone_friend_get_address(fid));
301 printf("Friend %s is %s\n", tmp, linphone_online_status_to_string(linphone_friend_get_status(fid)));
303 // todo: update Friend list state (unimplemented)
307 * Linphone core callback
310 linphonec_new_unknown_subscriber(LinphoneCore *lc, LinphoneFriend *lf,
313 printf("Friend %s requested subscription "
314 "(accept/deny is not implemented yet)\n", url);
315 // This means that this person wishes to be notified
316 // of your presence information (online, busy, away...).
320 static void linphonec_call_updated(LinphoneCall *call){
321 const LinphoneCallParams *cp=linphone_call_get_current_params(call);
322 if (!linphone_call_camera_enabled (call) && linphone_call_params_video_enabled (cp)){
323 linphonec_out("Far end requests to share video.\nType 'camera on' if you agree.\n");
327 static void linphonec_call_state_changed(LinphoneCore *lc, LinphoneCall *call, LinphoneCallState st, const char *msg){
328 char *from=linphone_call_get_remote_address_as_string(call);
329 long id=(long)linphone_call_get_user_pointer (call);
331 case LinphoneCallEnd:
332 linphonec_out("Call %i with %s ended.\n", id, from);
334 case LinphoneCallResuming:
335 linphonec_out("Resuming call %i with %s.\n", id, from);
337 case LinphoneCallStreamsRunning:
338 linphonec_out("Media streams established with %s for call %i.\n", from,id);
340 case LinphoneCallPausing:
341 linphonec_out("Pausing call %i with %s.\n", id, from);
343 case LinphoneCallPaused:
344 linphonec_out("Call %i with %s is now paused.\n", id, from);
346 case LinphoneCallPausedByRemote:
347 linphonec_out("Call %i has been paused by %s.\n",id,from);
349 case LinphoneCallIncomingReceived:
350 linphonec_call_identify(call);
351 linphone_call_enable_camera (call,linphonec_camera_enabled);
352 id=(long)linphone_call_get_user_pointer (call);
353 linphonec_set_caller(from);
357 linphonec_out("Receiving new incoming call from %s, assigned id %i\n", from,id);
359 case LinphoneCallOutgoingInit:
360 linphonec_call_identify(call);
361 id=(long)linphone_call_get_user_pointer (call);
362 from=linphone_call_get_remote_address_as_string(call);
363 linphonec_out("Establishing call id to %s, assigned id %i\n", from,id);
365 case LinphoneCallUpdatedByRemote:
366 linphonec_call_updated(call);
368 case LinphoneCallOutgoingProgress:
369 linphonec_out("Call %i to %s in progress.\n", id, from);
371 case LinphoneCallOutgoingRinging:
372 linphonec_out("Call %i to %s ringing.\n", id, from);
374 case LinphoneCallConnected:
375 linphonec_out("Call %i with %s connected.\n", id, from);
377 case LinphoneCallOutgoingEarlyMedia:
378 linphonec_out("Call %i with %s early media.\n", id, from);
380 case LinphoneCallError:
381 linphonec_out("Call %i with %s error.\n", id, from);
390 * Linphone core callback
393 linphonec_text_received(LinphoneCore *lc, LinphoneChatRoom *cr,
394 const LinphoneAddress *from, const char *msg)
396 linphonec_out("Message received from %s: %s\n", linphone_address_as_string(from), msg);
397 // TODO: provide mechanism for answering.. ('say' command?)
401 static void linphonec_dtmf_received(LinphoneCore *lc, LinphoneCall *call, int dtmf){
402 char *from=linphone_call_get_remote_address_as_string(call);
403 fprintf(stdout,"Receiving tone %c from %s\n",dtmf,from);
408 static char received_prompt[PROMPT_MAX_LEN];
409 static ms_mutex_t prompt_mutex;
410 static bool_t have_prompt=FALSE;
412 static void *prompt_reader_thread(void *arg){
414 char tmp[PROMPT_MAX_LEN];
415 while ((ret=fgets(tmp,sizeof(tmp),stdin))!=NULL){
416 ms_mutex_lock(&prompt_mutex);
417 strcpy(received_prompt,ret);
419 ms_mutex_unlock(&prompt_mutex);
424 static void start_prompt_reader(void){
426 ms_mutex_init(&prompt_mutex,NULL);
427 ortp_thread_create(&th,NULL,prompt_reader_thread,NULL);
429 #if !defined(_WIN32_WCE)
430 static ortp_pipe_t create_server_socket(void){
433 snprintf(path,sizeof(path)-1,"linphonec-%i",getuid());
437 DWORD size=sizeof(username)-1;
438 GetUserName(username,&size);
439 snprintf(path,sizeof(path)-1,"linphonec-%s",username);
442 return ortp_server_pipe_create(path);
446 static void *pipe_thread(void*p){
448 server_sock=create_server_socket();
449 if (server_sock==ORTP_PIPE_INVALID) return NULL;
450 while(pipe_reader_run){
451 while(client_sock!=ORTP_PIPE_INVALID){ /*sleep until the last command is finished*/
458 client_sock=ortp_server_pipe_accept_client(server_sock);
459 if (client_sock!=ORTP_PIPE_INVALID){
461 /*now read from the client */
462 if ((len=ortp_pipe_read(client_sock,(uint8_t*)tmp,sizeof(tmp)-1))>0){
463 ortp_mutex_lock(&prompt_mutex);
465 strcpy(received_prompt,tmp);
466 printf("Receiving command '%s'\n",received_prompt);fflush(stdout);
468 ortp_mutex_unlock(&prompt_mutex);
470 printf("read nothing\n");fflush(stdout);
471 ortp_server_pipe_close_client(client_sock);
472 client_sock=ORTP_PIPE_INVALID;
476 if (pipe_reader_run) fprintf(stderr,"accept() failed: %s\n",strerror(errno));
479 ms_message("Exiting pipe_reader_thread.");
484 static void start_pipe_reader(void){
485 ms_mutex_init(&prompt_mutex,NULL);
486 pipe_reader_run=TRUE;
487 ortp_thread_create(&pipe_reader_th,NULL,pipe_thread,NULL);
490 static void stop_pipe_reader(void){
491 pipe_reader_run=FALSE;
492 linphonec_command_finished();
493 ortp_server_pipe_close(server_sock);
494 ortp_thread_join(pipe_reader_th,NULL);
496 #endif /*_WIN32_WCE*/
499 #define BOOL_HAVE_READLINE 1
501 #define BOOL_HAVE_READLINE 0
504 char *linphonec_readline(char *prompt){
505 if (unix_socket || !BOOL_HAVE_READLINE ){
506 static bool_t prompt_reader_started=FALSE;
507 static bool_t pipe_reader_started=FALSE;
508 if (!prompt_reader_started){
509 start_prompt_reader();
510 prompt_reader_started=TRUE;
512 if (unix_socket && !pipe_reader_started){
513 #if !defined(_WIN32_WCE)
515 pipe_reader_started=TRUE;
516 #endif /*_WIN32_WCE*/
518 fprintf(stdout,"%s",prompt);
521 ms_mutex_lock(&prompt_mutex);
523 char *ret=strdup(received_prompt);
525 ms_mutex_unlock(&prompt_mutex);
528 ms_mutex_unlock(&prompt_mutex);
529 linphonec_idle_call();
532 /* Following is to get the video window going as it
533 should. Maybe should we only have this on when the option -V
537 if (PeekMessage(&msg, NULL, 0, 0,1)) {
538 TranslateMessage(&msg);
539 DispatchMessage(&msg);
547 return readline(prompt);
552 void linphonec_out(const char *fmt,...){
555 va_start (args, fmt);
556 res=ortp_strdup_vprintf(fmt,args);
560 #if !defined(_WIN32_WCE)
561 if (client_sock!=ORTP_PIPE_INVALID){
562 if (ortp_pipe_write(client_sock,(uint8_t*)res,strlen(res))==-1){
563 fprintf(stderr,"Fail to send output via pipe: %s",strerror(errno));
566 #endif /*_WIN32_WCE*/
570 void linphonec_command_finished(void){
571 #if !defined(_WIN32_WCE)
572 if (client_sock!=ORTP_PIPE_INVALID){
573 ortp_server_pipe_close_client(client_sock);
574 client_sock=ORTP_PIPE_INVALID;
576 #endif /*_WIN32_WCE*/
579 void linphonec_set_autoanswer(bool_t enabled){
583 bool_t linphonec_get_autoanswer(){
587 LinphoneCoreVTable linphonec_vtable={0};
589 /***************************************************************************/
595 * - char *histfile_name
598 #if defined (_WIN32_WCE)
600 char **convert_args_to_ascii(int argc, _TCHAR **wargv){
602 char **result=malloc(argc*sizeof(char*));
605 wcstombs(argtmp,wargv[i],sizeof(argtmp));
606 result[i]=strdup(argtmp);
611 int _tmain(int argc, _TCHAR* wargv[]) {
612 char **argv=convert_args_to_ascii(argc,wargv);
617 main (int argc, char *argv[]) {
619 linphonec_vtable.call_state_changed=linphonec_call_state_changed;
620 linphonec_vtable.notify_presence_recv = linphonec_notify_presence_received;
621 linphonec_vtable.new_subscription_request = linphonec_new_unknown_subscriber;
622 linphonec_vtable.auth_info_requested = linphonec_prompt_for_auth;
623 linphonec_vtable.display_status = linphonec_display_status;
624 linphonec_vtable.display_message=linphonec_display_something;
625 linphonec_vtable.display_warning=linphonec_display_warning;
626 linphonec_vtable.display_url=linphonec_display_url;
627 linphonec_vtable.text_received=linphonec_text_received;
628 linphonec_vtable.dtmf_received=linphonec_dtmf_received;
629 linphonec_vtable.refer_received=linphonec_display_refer;
630 linphonec_vtable.notify_recv=linphonec_notify_received;
632 if (! linphonec_init(argc, argv) ) exit(EXIT_FAILURE);
634 linphonec_main_loop (linphonec, sipAddr);
636 linphonec_finish(EXIT_SUCCESS);
638 exit(EXIT_SUCCESS); /* should never reach here */
642 * Initialize linphonec
645 linphonec_init(int argc, char **argv)
648 //g_mem_set_vtable(&dbgtable);
651 * Set initial values for global variables
657 snprintf(configfile_name, PATH_MAX, "%s/.linphonerc",
659 #elif defined(_WIN32_WCE)
660 strncpy(configfile_name,PACKAGE_DIR "\\linphonerc",PATH_MAX);
661 mylogfile=fopen(PACKAGE_DIR "\\" "linphonec.log","w");
662 printf("Logs are redirected in" PACKAGE_DIR "\\linphonec.log");
664 snprintf(configfile_name, PATH_MAX, "%s/Linphone/linphonerc",
667 /* Handle configuration filename changes */
668 switch (handle_configfile_migration())
670 case -1: /* error during file copies */
672 "Error in configuration file migration\n");
675 case 0: /* nothing done */
676 case 1: /* migrated */
682 if (NULL == bindtextdomain (GETTEXT_PACKAGE, PACKAGE_LOCALE_DIR))
683 perror ("bindtextdomain failed");
685 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
687 textdomain (GETTEXT_PACKAGE);
689 printf ("NLS disabled.\n");
692 linphonec_parse_cmdline(argc, argv);
696 if (logfile_name != NULL)
697 mylogfile = fopen (logfile_name, "w+");
699 if (mylogfile == NULL)
703 "INFO: no logfile, logging to stdout\n");
705 linphone_core_enable_logs(mylogfile);
709 linphone_core_disable_logs();
712 * Initialize auth stack
717 * Initialize linphone core
719 linphonec=linphone_core_new (&linphonec_vtable, configfile_name, factory_configfile_name, NULL);
720 linphone_core_enable_video(linphonec,vcap_enabled,display_enabled);
721 if (display_enabled && window_id != 0)
723 printf ("Setting window_id: 0x%x\n", window_id);
724 linphone_core_set_native_video_window_id(linphonec,window_id);
727 linphone_core_enable_video_preview(linphonec,preview_enabled);
728 if (!(vcap_enabled || display_enabled)) printf("Warning: video is disabled in linphonec, use -V or -C or -D to enable.\n");
731 * Initialize readline
733 linphonec_initialize_readline();
735 #if !defined(_WIN32_WCE)
737 * Initialize signal handlers
739 signal(SIGTERM, linphonec_finish);
740 signal(SIGINT, linphonec_finish);
741 #endif /*_WIN32_WCE*/
746 void linphonec_main_loop_exit(void){
747 linphonec_running=FALSE;
751 * Close linphonec, cleanly terminating
755 linphonec_finish(int exit_status)
757 // Do not allow concurrent destroying to prevent glibc errors
758 static bool_t terminating=FALSE;
759 if (terminating) return;
761 linphonec_out("Terminating...\n");
763 /* Terminate any pending call */
764 linphone_core_terminate_all_calls(linphonec);
766 linphonec_finish_readline();
768 #if !defined(_WIN32_WCE)
771 #endif /*_WIN32_WCE*/
773 linphone_core_destroy (linphonec);
775 if (mylogfile != NULL && mylogfile != stdout)
785 * This is called from idle_call() whenever
786 * pending_auth != NULL.
788 * It prompts user for a password.
789 * Hitting ^D (EOF) would make this function
791 * Any other input would try to set linphone core
792 * auth_password for the pending_auth, add the auth_info
796 linphonec_prompt_for_auth_final(LinphoneCore *lc)
799 char auth_prompt[256];
801 rl_hook_func_t *old_event_hook;
803 LinphoneAuthInfo *pending_auth=auth_stack.elem[auth_stack.nitems-1];
805 snprintf(auth_prompt, 256, "Password for %s on %s: ",
806 pending_auth->username, pending_auth->realm);
811 * Disable event hook to avoid entering an
812 * infinite loop. This would prevent idle_call
813 * from being called during authentication reads.
814 * Note that it might be undesiderable...
816 old_event_hook=rl_event_hook;
822 input=linphonec_readline(auth_prompt);
825 * If EOF (^D) is sent you probably don't want
826 * to provide an auth password... should give up
827 * the operation, but there's no mechanism to
828 * send this info back to caller currently...
832 printf("Cancel requested, but not implemented.\n");
837 iptr=lpc_strip_blanks(input);
840 * Only blanks, continue asking
848 /* Something typed, let's try */
853 * No check is done here to ensure password is correct.
854 * I guess password will be asked again later.
856 linphone_auth_info_set_passwd(pending_auth, input);
857 linphone_core_add_auth_info(lc, pending_auth);
858 linphone_auth_info_destroy(pending_auth);
859 auth_stack.elem[auth_stack.nitems-1]=0;
860 --(auth_stack.nitems);
863 * Reset line_buffer, to avoid the password
864 * to be used again from outer readline
866 rl_line_buffer[0]='\0';
867 rl_event_hook=old_event_hook;
873 print_usage (int exit_status)
875 fprintf (stdout, "\n\
876 usage: linphonec [-c file] [-s sipaddr] [-a] [-V] [-d level ] [-l logfile]\n\
879 -b file specify path of readonly factory configuration file.\n\
880 -c file specify path of configuration file.\n\
881 -d level be verbose. 0 is no output. 6 is all output\n\
882 -l logfile specify the log file for your SIP phone\n\
883 -s sipaddress specify the sip call to do at startup\n\
884 -a enable auto answering for incoming calls\n\
885 -V enable video features globally (disabled by default)\n\
886 -C enable video capture only (disabled by default)\n\
887 -D enable video display only (disabled by default)\n\
888 -S show general state messages (disabled by default)\n\
889 --wid windowid force embedding of video window into provided windowid (disabled by default)\n\
890 -v or --version display version and exits.\n");
897 #ifdef HAVE_X11_XLIB_H
898 static void x11_apply_video_params(VideoParams *params, Window window){
900 unsigned int flags=0;
901 static Display *display = NULL;
902 const char *dname=getenv("DISPLAY");
904 if (display==NULL && dname!=NULL){
905 display=XOpenDisplay(dname);
909 ms_error("Could not open display %s",dname);
912 memset(&wc,0,sizeof(wc));
921 flags|=CWWidth|CWHeight;
923 /*printf("XConfigureWindow x=%i,y=%i,w=%i,h=%i\n",
924 wc.x, wc.y ,wc.width, wc.height);*/
925 XConfigureWindow(display,window,flags,&wc);
927 XMapWindow(display,window);
929 XUnmapWindow(display,window);
930 XSync(display,FALSE);
935 static void lpc_apply_video_params(){
936 static unsigned long old_wid=0,old_pwid=0;
937 unsigned long wid=linphone_core_get_native_video_window_id (linphonec);
938 unsigned long pwid=linphone_core_get_native_preview_window_id (linphonec);
940 if (wid!=0 && (lpc_video_params.refresh || old_wid!=wid)){
941 lpc_video_params.refresh=FALSE;
942 #ifdef HAVE_X11_XLIB_H
943 if (lpc_video_params.wid==0){ // do not manage window if embedded
944 x11_apply_video_params(&lpc_video_params,wid);
949 if (pwid!=0 && (lpc_preview_params.refresh || old_pwid!=pwid)){
950 lpc_preview_params.refresh=FALSE;
951 #ifdef HAVE_X11_XLIB_H
952 /*printf("wid=%lu pwid=%lu\n",wid,pwid);*/
953 if (lpc_preview_params.wid==0){ // do not manage window if embedded
954 printf("Refreshing\n");
955 x11_apply_video_params(&lpc_preview_params,pwid);
967 * Called every second from main read loop.
969 * Will use the following globals:
971 * - LinphoneCore linphonec
972 * - LPC_AUTH_STACK auth_stack;
976 linphonec_idle_call ()
978 LinphoneCore *opm=linphonec;
980 /* Uncomment the following to verify being called */
983 linphone_core_iterate(opm);
985 fprintf (stdout, "-------auto answering to call-------\n" );
986 linphone_core_accept_call(opm,NULL);
990 if ( auth_stack.nitems )
993 * Inhibit command completion
994 * during password prompts
997 rl_inhibit_completion=1;
999 linphonec_prompt_for_auth_final(opm);
1000 #ifdef HAVE_READLINE
1001 rl_inhibit_completion=0;
1005 #ifdef VIDEO_ENABLED
1006 lpc_apply_video_params();
1012 #ifdef HAVE_READLINE
1016 * - char *histfile_name (also sets this)
1017 * - char *last_in_history (allocates it)
1020 linphonec_initialize_readline()
1022 /*rl_bind_key('\t', rl_insert);*/
1024 /* Allow conditional parsing of ~/.inputrc */
1025 rl_readline_name = "linphonec";
1027 /* Call idle_call() every second */
1028 rl_set_keyboard_input_timeout(LPC_READLINE_TIMEOUT);
1029 rl_event_hook=linphonec_idle_call;
1031 /* Set history file and read it */
1032 histfile_name = ms_strdup_printf ("%s/.linphonec_history",
1034 read_history(histfile_name);
1036 /* Initialized last_in_history cache*/
1037 last_in_history[0] = '\0';
1039 /* Register a completion function */
1040 rl_attempted_completion_function = linephonec_readline_completion;
1042 /* printf("Readline initialized.\n"); */
1050 * - char *histfile_name (writes history to file and frees it)
1051 * - char *last_in_history (frees it)
1055 linphonec_finish_readline()
1058 stifle_history(HISTSIZE);
1059 write_history(histfile_name);
1060 free(histfile_name);
1067 static void print_prompt(LinphoneCore *opm){
1068 #ifdef IDENTITY_AS_PROMPT
1069 snprintf(prompt, PROMPT_MAX_LEN, "%s> ",
1070 linphone_core_get_primary_contact(opm));
1072 snprintf(prompt, PROMPT_MAX_LEN, "linphonec> ");
1077 linphonec_main_loop (LinphoneCore * opm, char * sipAddr)
1079 char buf[LINE_MAX_LEN]; /* auto call handling */
1085 /* auto call handling */
1086 if (sipAddr != NULL )
1088 snprintf (buf, sizeof(buf),"call %s", sipAddr);
1089 linphonec_parse_command_line(linphonec, buf);
1092 while (linphonec_running && (input=linphonec_readline(prompt)))
1094 char *iptr; /* input and input pointer */
1098 iptr=lpc_strip_blanks(input);
1100 input_len = strlen(iptr);
1103 * Do nothing but release memory
1104 * if only blanks are read
1112 #ifdef HAVE_READLINE
1114 * Only add to history if not already
1115 * last item in it, and only if the command
1116 * doesn't start with a space (to allow for
1119 if ( iptr == input && strcmp(last_in_history, iptr) )
1121 strncpy(last_in_history,iptr,sizeof(last_in_history));
1122 last_in_history[sizeof(last_in_history)-1]='\0';
1127 linphonec_parse_command_line(linphonec, iptr);
1128 linphonec_command_finished();
1136 * Parse command line switches
1141 * - char *logfile_name
1142 * - char *configfile_name
1146 linphonec_parse_cmdline(int argc, char **argv)
1150 while (arg_num < argc)
1152 int old_arg_num = arg_num;
1153 if (strncmp ("-d", argv[arg_num], 2) == 0)
1157 trace_level = atoi (argv[arg_num]);
1161 else if (strncmp ("-l", argv[arg_num], 2) == 0)
1165 logfile_name = argv[arg_num];
1167 else if (strncmp ("-c", argv[arg_num], 2) == 0)
1169 if ( ++arg_num >= argc ) print_usage(EXIT_FAILURE);
1170 #if !defined(_WIN32_WCE)
1171 if (access(argv[arg_num],F_OK)!=0 )
1174 "Cannot open config file %s.\n",
1178 #endif /*_WIN32_WCE*/
1179 snprintf(configfile_name, PATH_MAX, "%s", argv[arg_num]);
1181 else if (strncmp ("-b", argv[arg_num], 2) == 0)
1183 if ( ++arg_num >= argc ) print_usage(EXIT_FAILURE);
1184 #if !defined(_WIN32_WCE)
1185 if (access(argv[arg_num],F_OK)!=0 )
1188 "Cannot open config file %s.\n",
1192 #endif /*_WIN32_WCE*/
1193 factory_configfile_name = argv[arg_num];
1195 else if (strncmp ("-s", argv[arg_num], 2) == 0)
1199 sipAddr = argv[arg_num];
1201 else if (strncmp ("-a", argv[arg_num], 2) == 0)
1205 else if (strncmp ("-C", argv[arg_num], 2) == 0)
1207 vcap_enabled = TRUE;
1209 else if (strncmp ("-D", argv[arg_num], 2) == 0)
1211 display_enabled = TRUE;
1213 else if (strncmp ("-V", argv[arg_num], 2) == 0)
1215 display_enabled = TRUE;
1216 vcap_enabled = TRUE;
1217 preview_enabled=TRUE;
1219 else if ((strncmp ("-v", argv[arg_num], 2) == 0)
1222 ("--version", argv[arg_num],
1223 strlen ("--version")) == 0))
1225 #if !defined(_WIN32_WCE)
1226 printf ("version: " LINPHONE_VERSION "\n");
1228 exit (EXIT_SUCCESS);
1230 else if (strncmp ("-S", argv[arg_num], 2) == 0)
1232 show_general_state = TRUE;
1234 else if (strncmp ("--pipe", argv[arg_num], 6) == 0)
1238 else if (strncmp ("--wid", argv[arg_num], 5) == 0)
1241 if (arg_num < argc) {
1243 window_id = strtol( argv[arg_num], &tmp, 0 );
1246 else if (old_arg_num == arg_num)
1248 fprintf (stderr, "ERROR: bad arguments\n");
1249 print_usage (EXIT_FAILURE);
1258 * Up to version 1.2.1 linphone used ~/.linphonec for
1259 * CLI and ~/.gnome2/linphone for GUI as configuration file.
1260 * In newer version both interfaces will use ~/.linphonerc.
1262 * This function helps transparently migrating from one
1263 * to the other layout using the following heuristic:
1265 * IF new_config EXISTS => do nothing
1266 * ELSE IF old_cli_config EXISTS => copy to new_config
1267 * ELSE IF old_gui_config EXISTS => copy to new_config
1270 * 0 if it did nothing
1271 * 1 if it migrated successfully
1275 handle_configfile_migration()
1277 #if !defined(_WIN32_WCE)
1281 #if !defined(_WIN32_WCE)
1282 const char *home = getenv("HOME");
1284 const char *home = ".";
1285 #endif /*_WIN32_WCE*/
1286 new_cfg = ms_strdup_printf("%s/.linphonerc", home);
1289 * If the *NEW* configuration already exists
1292 if (access(new_cfg,F_OK)==0)
1298 old_cfg_cli = ms_strdup_printf("%s/.linphonec", home);
1301 * If the *OLD* CLI configurations exist copy it to
1302 * the new file and make it a symlink.
1304 if (access(old_cfg_cli, F_OK)==0)
1306 if ( ! copy_file(old_cfg_cli, new_cfg) )
1312 printf("%s copied to %s\n", old_cfg_cli, new_cfg);
1319 old_cfg_gui = ms_strdup_printf("%s/.gnome2/linphone", home);
1322 * If the *OLD* GUI configurations exist copy it to
1323 * the new file and make it a symlink.
1325 if (access(old_cfg_gui, F_OK)==0)
1327 if ( ! copy_file(old_cfg_gui, new_cfg) )
1334 printf("%s copied to %s\n", old_cfg_gui, new_cfg);
1342 #endif /*_WIN32_WCE*/
1345 #if !defined(_WIN32_WCE)
1347 * Copy file "from" to file "to".
1348 * Destination file is truncated if existing.
1349 * Return 1 on success, 0 on error (printing an error).
1352 copy_file(const char *from, const char *to)
1359 /* Open "from" file for reading */
1360 in=fopen(from, "r");
1363 snprintf(message, 255, "Can't open %s for reading: %s\n",
1364 from, strerror(errno));
1365 fprintf(stderr, "%s", message);
1369 /* Open "to" file for writing (will truncate existing files) */
1373 snprintf(message, 255, "Can't open %s for writing: %s\n",
1374 to, strerror(errno));
1375 fprintf(stderr, "%s", message);
1379 /* Copy data from "in" to "out" */
1380 while ( (n=fread(buf, 1, sizeof buf, in)) > 0 )
1382 if ( ! fwrite(buf, 1, n, out) )
1393 #endif /*_WIN32_WCE*/
1395 #ifdef HAVE_READLINE
1397 linephonec_readline_completion(const char *text, int start, int end)
1399 char **matches = NULL;
1402 * Prevent readline from falling
1403 * back to filename-completion
1405 rl_attempted_completion_over=1;
1408 * If this is the start of line we complete with commands
1412 return rl_completion_matches(text, linphonec_command_generator);
1416 * Otherwise, we should peek at command name
1417 * or context to implement a smart completion.
1418 * For example: "call .." could return
1419 * friends' sip-uri as matches
1428 * Strip blanks from a string.
1429 * Return a pointer into the provided string.
1430 * Modifies input adding a NULL at first
1431 * of trailing blanks.
1434 lpc_strip_blanks(char *input)
1438 /* Find first non-blank */
1439 while(*input && isspace(*input)) ++input;
1441 /* Find last non-blank */
1442 iptr=input+strlen(input);
1444 while(isspace(*--iptr));
1451 /****************************************************************************
1453 * $Log: linphonec.c,v $
1454 * Revision 1.57 2007/11/14 13:40:27 smorlat
1455 * fix --disable-video build.
1457 * Revision 1.56 2007/09/26 14:07:27 fixkowalski
1458 * - ANSI/C++ compilation issues with non-GCC compilers
1459 * - Faster epm-based packaging
1460 * - Ability to build & run on FC6's eXosip/osip
1462 * Revision 1.55 2007/09/24 16:01:58 smorlat
1465 * Revision 1.54 2007/08/22 14:06:11 smorlat
1466 * authentication bugs fixed.
1468 * Revision 1.53 2007/02/13 21:31:01 smorlat
1469 * added patch for general state.
1470 * new doxygen for oRTP
1473 * Revision 1.52 2007/01/10 14:11:24 smorlat
1474 * add --video to linphonec.
1476 * Revision 1.51 2006/08/21 12:49:59 smorlat
1477 * merged several little patches.
1479 * Revision 1.50 2006/07/26 08:17:28 smorlat
1482 * Revision 1.49 2006/07/17 18:45:00 smorlat
1483 * support for several event queues in ortp.
1484 * glib dependency removed from coreapi/ and console/
1486 * Revision 1.48 2006/04/09 12:45:32 smorlat
1487 * linphonec improvements.
1489 * Revision 1.47 2006/04/04 08:04:34 smorlat
1490 * switched to mediastreamer2, most bugs fixed.
1492 * Revision 1.46 2006/03/16 17:17:40 smorlat
1495 * Revision 1.45 2006/03/12 21:48:31 smorlat
1496 * gcc-2.95 compile error fixed.
1497 * mediastreamer2 in progress
1499 * Revision 1.44 2006/03/04 11:17:10 smorlat
1500 * mediastreamer2 in progress.
1502 * Revision 1.43 2006/02/13 09:50:50 strk
1503 * fixed unused variable warning.
1505 * Revision 1.42 2006/02/02 15:39:18 strk
1506 * - Added 'friend list' and 'friend call' commands
1507 * - Allowed for multiple DTFM send in a single line
1508 * - Added status-specific callback (bare version)
1510 * Revision 1.41 2006/02/02 13:30:05 strk
1511 * - Padded vtable with missing callbacks
1512 * (fixing a segfault on friends subscription)
1513 * - Handled friends notify (bare version)
1514 * - Handled text messages receive (bare version)
1515 * - Printed message on subscription request (bare version)
1517 * Revision 1.40 2006/01/26 09:48:05 strk
1518 * Added limits.h include
1520 * Revision 1.39 2006/01/26 02:11:01 strk
1521 * Removed unused variables, fixed copyright date
1523 * Revision 1.38 2006/01/25 18:33:02 strk
1524 * Removed the -t swich, terminate_on_close made the default behaviour
1526 * Revision 1.37 2006/01/20 14:12:34 strk
1527 * Added linphonec_init() and linphonec_finish() functions.
1528 * Handled SIGINT and SIGTERM to invoke linphonec_finish().
1529 * Handling of auto-termination (-t) moved to linphonec_finish().
1530 * Reworked main (input read) loop to not rely on 'terminate'
1531 * and 'run' variable (dropped). configfile_name allocated on stack
1532 * using PATH_MAX limit. Changed print_usage signature to allow
1533 * for an exit_status specification.
1535 * Revision 1.36 2006/01/18 09:25:32 strk
1536 * Command completion inhibited in proxy addition and auth request prompts.
1537 * Avoided use of readline's internal filename completion.
1539 * Revision 1.35 2006/01/14 13:29:32 strk
1540 * Reworked commands interface to use a table structure,
1541 * used by command line parser and help function.
1542 * Implemented first level of completion (commands).
1543 * Added notification of invalid "answer" and "terminate"
1544 * commands (no incoming call, no active call).
1545 * Forbidden "call" intialization when a call is already active.
1546 * Cleaned up all commands, adding more feedback and error checks.
1548 * Revision 1.34 2006/01/13 13:00:29 strk
1549 * Added linphonec.h. Code layout change (added comments, forward decl,
1550 * globals on top, copyright notices and Logs). Handled out-of-memory
1551 * condition on history management. Removed assumption on sizeof(char).
1552 * Fixed bug in authentication prompt (introduced by readline).
1553 * Added support for multiple authentication requests (up to MAX_PENDING_AUTH).
1556 ****************************************************************************/