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_encryption_changed(LinphoneCore *lc, LinphoneCall *call, bool_t encrypted, const char *auth_token) {
327 long id=(long)linphone_call_get_user_pointer (call);
329 linphonec_out("Call %i is not fully encrypted and auth token is %s.\n", id,
330 (auth_token != NULL) ? auth_token : "absent");
332 linphonec_out("Call %i is fully encrypted and auth token is %s.\n", id,
333 (auth_token != NULL) ? auth_token : "absent");
337 static void linphonec_call_state_changed(LinphoneCore *lc, LinphoneCall *call, LinphoneCallState st, const char *msg){
338 char *from=linphone_call_get_remote_address_as_string(call);
339 long id=(long)linphone_call_get_user_pointer (call);
341 case LinphoneCallEnd:
342 linphonec_out("Call %i with %s ended (%s).\n", id, from, linphone_reason_to_string(linphone_call_get_reason(call)));
344 case LinphoneCallResuming:
345 linphonec_out("Resuming call %i with %s.\n", id, from);
347 case LinphoneCallStreamsRunning:
348 linphonec_out("Media streams established with %s for call %i.\n", from,id);
350 case LinphoneCallPausing:
351 linphonec_out("Pausing call %i with %s.\n", id, from);
353 case LinphoneCallPaused:
354 linphonec_out("Call %i with %s is now paused.\n", id, from);
356 case LinphoneCallPausedByRemote:
357 linphonec_out("Call %i has been paused by %s.\n",id,from);
359 case LinphoneCallIncomingReceived:
360 linphonec_call_identify(call);
361 linphone_call_enable_camera (call,linphonec_camera_enabled);
362 id=(long)linphone_call_get_user_pointer (call);
363 linphonec_set_caller(from);
367 linphonec_out("Receiving new incoming call from %s, assigned id %i\n", from,id);
369 case LinphoneCallOutgoingInit:
370 linphonec_call_identify(call);
371 id=(long)linphone_call_get_user_pointer (call);
372 linphonec_out("Establishing call id to %s, assigned id %i\n", from,id);
374 case LinphoneCallUpdatedByRemote:
375 linphonec_call_updated(call);
377 case LinphoneCallOutgoingProgress:
378 linphonec_out("Call %i to %s in progress.\n", id, from);
380 case LinphoneCallOutgoingRinging:
381 linphonec_out("Call %i to %s ringing.\n", id, from);
383 case LinphoneCallConnected:
384 linphonec_out("Call %i with %s connected.\n", id, from);
386 case LinphoneCallOutgoingEarlyMedia:
387 linphonec_out("Call %i with %s early media.\n", id, from);
389 case LinphoneCallError:
390 linphonec_out("Call %i with %s error.\n", id, from);
399 * Linphone core callback
402 linphonec_text_received(LinphoneCore *lc, LinphoneChatRoom *cr,
403 const LinphoneAddress *from, const char *msg)
405 linphonec_out("Message received from %s: %s\n", linphone_address_as_string(from), msg);
406 // TODO: provide mechanism for answering.. ('say' command?)
410 static void linphonec_dtmf_received(LinphoneCore *lc, LinphoneCall *call, int dtmf){
411 char *from=linphone_call_get_remote_address_as_string(call);
412 fprintf(stdout,"Receiving tone %c from %s\n",dtmf,from);
417 static char received_prompt[PROMPT_MAX_LEN];
418 static ms_mutex_t prompt_mutex;
419 static bool_t have_prompt=FALSE;
421 static void *prompt_reader_thread(void *arg){
423 char tmp[PROMPT_MAX_LEN];
424 while ((ret=fgets(tmp,sizeof(tmp),stdin))!=NULL){
425 ms_mutex_lock(&prompt_mutex);
426 strcpy(received_prompt,ret);
428 ms_mutex_unlock(&prompt_mutex);
433 static void start_prompt_reader(void){
435 ms_mutex_init(&prompt_mutex,NULL);
436 ortp_thread_create(&th,NULL,prompt_reader_thread,NULL);
438 #if !defined(_WIN32_WCE)
439 static ortp_pipe_t create_server_socket(void){
442 snprintf(path,sizeof(path)-1,"linphonec-%i",getuid());
446 DWORD size=sizeof(username)-1;
447 GetUserName(username,&size);
448 snprintf(path,sizeof(path)-1,"linphonec-%s",username);
451 return ortp_server_pipe_create(path);
455 static void *pipe_thread(void*p){
457 server_sock=create_server_socket();
458 if (server_sock==ORTP_PIPE_INVALID) return NULL;
459 while(pipe_reader_run){
460 while(client_sock!=ORTP_PIPE_INVALID){ /*sleep until the last command is finished*/
467 client_sock=ortp_server_pipe_accept_client(server_sock);
468 if (client_sock!=ORTP_PIPE_INVALID){
470 /*now read from the client */
471 if ((len=ortp_pipe_read(client_sock,(uint8_t*)tmp,sizeof(tmp)-1))>0){
472 ortp_mutex_lock(&prompt_mutex);
474 strcpy(received_prompt,tmp);
475 printf("Receiving command '%s'\n",received_prompt);fflush(stdout);
477 ortp_mutex_unlock(&prompt_mutex);
479 printf("read nothing\n");fflush(stdout);
480 ortp_server_pipe_close_client(client_sock);
481 client_sock=ORTP_PIPE_INVALID;
485 if (pipe_reader_run) fprintf(stderr,"accept() failed: %s\n",strerror(errno));
488 ms_message("Exiting pipe_reader_thread.");
493 static void start_pipe_reader(void){
494 ms_mutex_init(&prompt_mutex,NULL);
495 pipe_reader_run=TRUE;
496 ortp_thread_create(&pipe_reader_th,NULL,pipe_thread,NULL);
499 static void stop_pipe_reader(void){
500 pipe_reader_run=FALSE;
501 linphonec_command_finished();
502 ortp_server_pipe_close(server_sock);
503 ortp_thread_join(pipe_reader_th,NULL);
505 #endif /*_WIN32_WCE*/
508 #define BOOL_HAVE_READLINE 1
510 #define BOOL_HAVE_READLINE 0
513 char *linphonec_readline(char *prompt){
514 if (unix_socket || !BOOL_HAVE_READLINE ){
515 static bool_t prompt_reader_started=FALSE;
516 static bool_t pipe_reader_started=FALSE;
517 if (!prompt_reader_started){
518 start_prompt_reader();
519 prompt_reader_started=TRUE;
521 if (unix_socket && !pipe_reader_started){
522 #if !defined(_WIN32_WCE)
524 pipe_reader_started=TRUE;
525 #endif /*_WIN32_WCE*/
527 fprintf(stdout,"%s",prompt);
530 ms_mutex_lock(&prompt_mutex);
532 char *ret=strdup(received_prompt);
534 ms_mutex_unlock(&prompt_mutex);
537 ms_mutex_unlock(&prompt_mutex);
538 linphonec_idle_call();
541 /* Following is to get the video window going as it
542 should. Maybe should we only have this on when the option -V
546 if (PeekMessage(&msg, NULL, 0, 0,1)) {
547 TranslateMessage(&msg);
548 DispatchMessage(&msg);
556 return readline(prompt);
561 void linphonec_out(const char *fmt,...){
564 va_start (args, fmt);
565 res=ortp_strdup_vprintf(fmt,args);
569 #if !defined(_WIN32_WCE)
570 if (client_sock!=ORTP_PIPE_INVALID){
571 if (ortp_pipe_write(client_sock,(uint8_t*)res,strlen(res))==-1){
572 fprintf(stderr,"Fail to send output via pipe: %s",strerror(errno));
575 #endif /*_WIN32_WCE*/
579 void linphonec_command_finished(void){
580 #if !defined(_WIN32_WCE)
581 if (client_sock!=ORTP_PIPE_INVALID){
582 ortp_server_pipe_close_client(client_sock);
583 client_sock=ORTP_PIPE_INVALID;
585 #endif /*_WIN32_WCE*/
588 void linphonec_set_autoanswer(bool_t enabled){
592 bool_t linphonec_get_autoanswer(){
596 LinphoneCoreVTable linphonec_vtable={0};
598 /***************************************************************************/
604 * - char *histfile_name
607 #if defined (_WIN32_WCE)
609 char **convert_args_to_ascii(int argc, _TCHAR **wargv){
611 char **result=malloc(argc*sizeof(char*));
614 wcstombs(argtmp,wargv[i],sizeof(argtmp));
615 result[i]=strdup(argtmp);
620 int _tmain(int argc, _TCHAR* wargv[]) {
621 char **argv=convert_args_to_ascii(argc,wargv);
626 main (int argc, char *argv[]) {
628 linphonec_vtable.call_state_changed=linphonec_call_state_changed;
629 linphonec_vtable.notify_presence_recv = linphonec_notify_presence_received;
630 linphonec_vtable.new_subscription_request = linphonec_new_unknown_subscriber;
631 linphonec_vtable.auth_info_requested = linphonec_prompt_for_auth;
632 linphonec_vtable.display_status = linphonec_display_status;
633 linphonec_vtable.display_message=linphonec_display_something;
634 linphonec_vtable.display_warning=linphonec_display_warning;
635 linphonec_vtable.display_url=linphonec_display_url;
636 linphonec_vtable.text_received=linphonec_text_received;
637 linphonec_vtable.dtmf_received=linphonec_dtmf_received;
638 linphonec_vtable.refer_received=linphonec_display_refer;
639 linphonec_vtable.notify_recv=linphonec_notify_received;
640 linphonec_vtable.call_encryption_changed=linphonec_call_encryption_changed;
642 if (! linphonec_init(argc, argv) ) exit(EXIT_FAILURE);
644 linphonec_main_loop (linphonec);
646 linphonec_finish(EXIT_SUCCESS);
648 exit(EXIT_SUCCESS); /* should never reach here */
652 * Initialize linphonec
655 linphonec_init(int argc, char **argv)
658 //g_mem_set_vtable(&dbgtable);
661 * Set initial values for global variables
667 snprintf(configfile_name, PATH_MAX, "%s/.linphonerc",
669 #elif defined(_WIN32_WCE)
670 strncpy(configfile_name,PACKAGE_DIR "\\linphonerc",PATH_MAX);
671 mylogfile=fopen(PACKAGE_DIR "\\" "linphonec.log","w");
672 printf("Logs are redirected in" PACKAGE_DIR "\\linphonec.log");
674 snprintf(configfile_name, PATH_MAX, "%s/Linphone/linphonerc",
677 /* Handle configuration filename changes */
678 switch (handle_configfile_migration())
680 case -1: /* error during file copies */
682 "Error in configuration file migration\n");
685 case 0: /* nothing done */
686 case 1: /* migrated */
692 if (NULL == bindtextdomain (GETTEXT_PACKAGE, PACKAGE_LOCALE_DIR))
693 perror ("bindtextdomain failed");
695 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
697 textdomain (GETTEXT_PACKAGE);
699 printf ("NLS disabled.\n");
702 linphonec_parse_cmdline(argc, argv);
706 if (logfile_name != NULL)
707 mylogfile = fopen (logfile_name, "w+");
709 if (mylogfile == NULL)
713 "INFO: no logfile, logging to stdout\n");
715 linphone_core_enable_logs(mylogfile);
719 linphone_core_disable_logs();
722 * Initialize auth stack
727 * Initialize linphone core
729 linphonec=linphone_core_new (&linphonec_vtable, configfile_name, factory_configfile_name, NULL);
730 linphone_core_enable_video(linphonec,vcap_enabled,display_enabled);
731 if (display_enabled && window_id != 0)
733 printf ("Setting window_id: 0x%x\n", window_id);
734 linphone_core_set_native_video_window_id(linphonec,window_id);
737 linphone_core_enable_video_preview(linphonec,preview_enabled);
738 if (!(vcap_enabled || display_enabled)) printf("Warning: video is disabled in linphonec, use -V or -C or -D to enable.\n");
741 * Initialize readline
743 linphonec_initialize_readline();
745 #if !defined(_WIN32_WCE)
747 * Initialize signal handlers
749 signal(SIGTERM, linphonec_finish);
750 signal(SIGINT, linphonec_finish);
751 #endif /*_WIN32_WCE*/
756 void linphonec_main_loop_exit(void){
757 linphonec_running=FALSE;
761 * Close linphonec, cleanly terminating
765 linphonec_finish(int exit_status)
767 // Do not allow concurrent destroying to prevent glibc errors
768 static bool_t terminating=FALSE;
769 if (terminating) return;
771 linphonec_out("Terminating...\n");
773 /* Terminate any pending call */
774 linphone_core_terminate_all_calls(linphonec);
776 linphonec_finish_readline();
778 #if !defined(_WIN32_WCE)
781 #endif /*_WIN32_WCE*/
783 linphone_core_destroy (linphonec);
785 if (mylogfile != NULL && mylogfile != stdout)
795 * This is called from idle_call() whenever
796 * pending_auth != NULL.
798 * It prompts user for a password.
799 * Hitting ^D (EOF) would make this function
801 * Any other input would try to set linphone core
802 * auth_password for the pending_auth, add the auth_info
806 linphonec_prompt_for_auth_final(LinphoneCore *lc)
808 static int reentrancy=0;
810 char auth_prompt[256];
812 rl_hook_func_t *old_event_hook;
815 if (reentrancy!=0) return 0;
819 LinphoneAuthInfo *pending_auth=auth_stack.elem[auth_stack.nitems-1];
821 snprintf(auth_prompt, 256, "Password for %s on %s: ",
822 pending_auth->username, pending_auth->realm);
827 * Disable event hook to avoid entering an
828 * infinite loop. This would prevent idle_call
829 * from being called during authentication reads.
830 * Note that it might be undesiderable...
832 old_event_hook=rl_event_hook;
838 input=linphonec_readline(auth_prompt);
841 * If EOF (^D) is sent you probably don't want
842 * to provide an auth password... should give up
843 * the operation, but there's no mechanism to
844 * send this info back to caller currently...
848 printf("Cancel requested, but not implemented.\n");
853 iptr=lpc_strip_blanks(input);
856 * Only blanks, continue asking
864 /* Something typed, let's try */
869 * No check is done here to ensure password is correct.
870 * I guess password will be asked again later.
872 linphone_auth_info_set_passwd(pending_auth, input);
873 linphone_core_add_auth_info(lc, pending_auth);
874 linphone_auth_info_destroy(pending_auth);
875 auth_stack.elem[auth_stack.nitems-1]=0;
876 --(auth_stack.nitems);
879 * Reset line_buffer, to avoid the password
880 * to be used again from outer readline
882 rl_line_buffer[0]='\0';
883 rl_event_hook=old_event_hook;
889 print_usage (int exit_status)
891 fprintf (stdout, "\n\
892 usage: linphonec [-c file] [-s sipaddr] [-a] [-V] [-d level ] [-l logfile]\n\
895 -b file specify path of readonly factory configuration file.\n\
896 -c file specify path of configuration file.\n\
897 -d level be verbose. 0 is no output. 6 is all output\n\
898 -l logfile specify the log file for your SIP phone\n\
899 -s sipaddress specify the sip call to do at startup\n\
900 -a enable auto answering for incoming calls\n\
901 -V enable video features globally (disabled by default)\n\
902 -C enable video capture only (disabled by default)\n\
903 -D enable video display only (disabled by default)\n\
904 -S show general state messages (disabled by default)\n\
905 --wid windowid force embedding of video window into provided windowid (disabled by default)\n\
906 -v or --version display version and exits.\n");
913 #ifdef HAVE_X11_XLIB_H
914 static void x11_apply_video_params(VideoParams *params, Window window){
916 unsigned int flags=0;
917 static Display *display = NULL;
918 const char *dname=getenv("DISPLAY");
920 if (display==NULL && dname!=NULL){
921 display=XOpenDisplay(dname);
925 ms_error("Could not open display %s",dname);
928 memset(&wc,0,sizeof(wc));
937 flags|=CWWidth|CWHeight;
939 /*printf("XConfigureWindow x=%i,y=%i,w=%i,h=%i\n",
940 wc.x, wc.y ,wc.width, wc.height);*/
941 XConfigureWindow(display,window,flags,&wc);
943 XMapWindow(display,window);
945 XUnmapWindow(display,window);
946 XSync(display,FALSE);
951 static void lpc_apply_video_params(){
952 static unsigned long old_wid=0,old_pwid=0;
953 unsigned long wid=linphone_core_get_native_video_window_id (linphonec);
954 unsigned long pwid=linphone_core_get_native_preview_window_id (linphonec);
956 if (wid!=0 && (lpc_video_params.refresh || old_wid!=wid)){
957 lpc_video_params.refresh=FALSE;
958 #ifdef HAVE_X11_XLIB_H
959 if (lpc_video_params.wid==0){ // do not manage window if embedded
960 x11_apply_video_params(&lpc_video_params,wid);
965 if (pwid!=0 && (lpc_preview_params.refresh || old_pwid!=pwid)){
966 lpc_preview_params.refresh=FALSE;
967 #ifdef HAVE_X11_XLIB_H
968 /*printf("wid=%lu pwid=%lu\n",wid,pwid);*/
969 if (lpc_preview_params.wid==0){ // do not manage window if embedded
970 printf("Refreshing\n");
971 x11_apply_video_params(&lpc_preview_params,pwid);
983 * Called every second from main read loop.
985 * Will use the following globals:
987 * - LinphoneCore linphonec
988 * - LPC_AUTH_STACK auth_stack;
992 linphonec_idle_call ()
994 LinphoneCore *opm=linphonec;
996 /* Uncomment the following to verify being called */
999 linphone_core_iterate(opm);
1001 fprintf (stdout, "-------auto answering to call-------\n" );
1002 linphone_core_accept_call(opm,NULL);
1005 /* auto call handling */
1006 if (sip_addr_to_call != NULL )
1009 snprintf (buf, sizeof(buf),"call %s", sip_addr_to_call);
1010 sip_addr_to_call=NULL;
1011 linphonec_parse_command_line(linphonec, buf);
1014 if ( auth_stack.nitems )
1017 * Inhibit command completion
1018 * during password prompts
1020 #ifdef HAVE_READLINE
1021 rl_inhibit_completion=1;
1023 linphonec_prompt_for_auth_final(opm);
1024 #ifdef HAVE_READLINE
1025 rl_inhibit_completion=0;
1029 #ifdef VIDEO_ENABLED
1030 lpc_apply_video_params();
1036 #ifdef HAVE_READLINE
1040 * - char *histfile_name (also sets this)
1041 * - char *last_in_history (allocates it)
1044 linphonec_initialize_readline()
1046 /*rl_bind_key('\t', rl_insert);*/
1048 /* Allow conditional parsing of ~/.inputrc */
1049 rl_readline_name = "linphonec";
1051 /* Call idle_call() every second */
1052 rl_set_keyboard_input_timeout(LPC_READLINE_TIMEOUT);
1053 rl_event_hook=linphonec_idle_call;
1055 /* Set history file and read it */
1056 histfile_name = ms_strdup_printf ("%s/.linphonec_history",
1058 read_history(histfile_name);
1060 /* Initialized last_in_history cache*/
1061 last_in_history[0] = '\0';
1063 /* Register a completion function */
1064 rl_attempted_completion_function = linephonec_readline_completion;
1066 /* printf("Readline initialized.\n"); */
1074 * - char *histfile_name (writes history to file and frees it)
1075 * - char *last_in_history (frees it)
1079 linphonec_finish_readline()
1082 stifle_history(HISTSIZE);
1083 write_history(histfile_name);
1084 free(histfile_name);
1091 static void print_prompt(LinphoneCore *opm){
1092 #ifdef IDENTITY_AS_PROMPT
1093 snprintf(prompt, PROMPT_MAX_LEN, "%s> ",
1094 linphone_core_get_primary_contact(opm));
1096 snprintf(prompt, PROMPT_MAX_LEN, "linphonec> ");
1101 linphonec_main_loop (LinphoneCore * opm)
1108 while (linphonec_running && (input=linphonec_readline(prompt)))
1110 char *iptr; /* input and input pointer */
1114 iptr=lpc_strip_blanks(input);
1116 input_len = strlen(iptr);
1119 * Do nothing but release memory
1120 * if only blanks are read
1128 #ifdef HAVE_READLINE
1130 * Only add to history if not already
1131 * last item in it, and only if the command
1132 * doesn't start with a space (to allow for
1135 if ( iptr == input && strcmp(last_in_history, iptr) )
1137 strncpy(last_in_history,iptr,sizeof(last_in_history));
1138 last_in_history[sizeof(last_in_history)-1]='\0';
1143 linphonec_parse_command_line(linphonec, iptr);
1144 linphonec_command_finished();
1152 * Parse command line switches
1157 * - char *logfile_name
1158 * - char *configfile_name
1162 linphonec_parse_cmdline(int argc, char **argv)
1166 while (arg_num < argc)
1168 int old_arg_num = arg_num;
1169 if (strncmp ("-d", argv[arg_num], 2) == 0)
1173 trace_level = atoi (argv[arg_num]);
1177 else if (strncmp ("-l", argv[arg_num], 2) == 0)
1181 logfile_name = argv[arg_num];
1183 else if (strncmp ("-c", argv[arg_num], 2) == 0)
1185 if ( ++arg_num >= argc ) print_usage(EXIT_FAILURE);
1186 #if !defined(_WIN32_WCE)
1187 if (access(argv[arg_num],F_OK)!=0 )
1190 "Cannot open config file %s.\n",
1194 #endif /*_WIN32_WCE*/
1195 snprintf(configfile_name, PATH_MAX, "%s", argv[arg_num]);
1197 else if (strncmp ("-b", argv[arg_num], 2) == 0)
1199 if ( ++arg_num >= argc ) print_usage(EXIT_FAILURE);
1200 #if !defined(_WIN32_WCE)
1201 if (access(argv[arg_num],F_OK)!=0 )
1204 "Cannot open config file %s.\n",
1208 #endif /*_WIN32_WCE*/
1209 factory_configfile_name = argv[arg_num];
1211 else if (strncmp ("-s", argv[arg_num], 2) == 0)
1215 sip_addr_to_call = argv[arg_num];
1217 else if (strncmp ("-a", argv[arg_num], 2) == 0)
1221 else if (strncmp ("-C", argv[arg_num], 2) == 0)
1223 vcap_enabled = TRUE;
1225 else if (strncmp ("-D", argv[arg_num], 2) == 0)
1227 display_enabled = TRUE;
1229 else if (strncmp ("-V", argv[arg_num], 2) == 0)
1231 display_enabled = TRUE;
1232 vcap_enabled = TRUE;
1233 preview_enabled=TRUE;
1235 else if ((strncmp ("-v", argv[arg_num], 2) == 0)
1238 ("--version", argv[arg_num],
1239 strlen ("--version")) == 0))
1241 #if !defined(_WIN32_WCE)
1242 printf ("version: " LINPHONE_VERSION "\n");
1244 exit (EXIT_SUCCESS);
1246 else if (strncmp ("-S", argv[arg_num], 2) == 0)
1248 show_general_state = TRUE;
1250 else if (strncmp ("--pipe", argv[arg_num], 6) == 0)
1254 else if (strncmp ("--wid", argv[arg_num], 5) == 0)
1257 if (arg_num < argc) {
1259 window_id = strtol( argv[arg_num], &tmp, 0 );
1262 else if (old_arg_num == arg_num)
1264 fprintf (stderr, "ERROR: bad arguments\n");
1265 print_usage (EXIT_FAILURE);
1274 * Up to version 1.2.1 linphone used ~/.linphonec for
1275 * CLI and ~/.gnome2/linphone for GUI as configuration file.
1276 * In newer version both interfaces will use ~/.linphonerc.
1278 * This function helps transparently migrating from one
1279 * to the other layout using the following heuristic:
1281 * IF new_config EXISTS => do nothing
1282 * ELSE IF old_cli_config EXISTS => copy to new_config
1283 * ELSE IF old_gui_config EXISTS => copy to new_config
1286 * 0 if it did nothing
1287 * 1 if it migrated successfully
1291 handle_configfile_migration()
1293 #if !defined(_WIN32_WCE)
1297 #if !defined(_WIN32_WCE)
1298 const char *home = getenv("HOME");
1300 const char *home = ".";
1301 #endif /*_WIN32_WCE*/
1302 new_cfg = ms_strdup_printf("%s/.linphonerc", home);
1305 * If the *NEW* configuration already exists
1308 if (access(new_cfg,F_OK)==0)
1314 old_cfg_cli = ms_strdup_printf("%s/.linphonec", home);
1317 * If the *OLD* CLI configurations exist copy it to
1318 * the new file and make it a symlink.
1320 if (access(old_cfg_cli, F_OK)==0)
1322 if ( ! copy_file(old_cfg_cli, new_cfg) )
1328 printf("%s copied to %s\n", old_cfg_cli, new_cfg);
1335 old_cfg_gui = ms_strdup_printf("%s/.gnome2/linphone", home);
1338 * If the *OLD* GUI configurations exist copy it to
1339 * the new file and make it a symlink.
1341 if (access(old_cfg_gui, F_OK)==0)
1343 if ( ! copy_file(old_cfg_gui, new_cfg) )
1350 printf("%s copied to %s\n", old_cfg_gui, new_cfg);
1358 #endif /*_WIN32_WCE*/
1361 #if !defined(_WIN32_WCE)
1363 * Copy file "from" to file "to".
1364 * Destination file is truncated if existing.
1365 * Return 1 on success, 0 on error (printing an error).
1368 copy_file(const char *from, const char *to)
1375 /* Open "from" file for reading */
1376 in=fopen(from, "r");
1379 snprintf(message, 255, "Can't open %s for reading: %s\n",
1380 from, strerror(errno));
1381 fprintf(stderr, "%s", message);
1385 /* Open "to" file for writing (will truncate existing files) */
1389 snprintf(message, 255, "Can't open %s for writing: %s\n",
1390 to, strerror(errno));
1391 fprintf(stderr, "%s", message);
1395 /* Copy data from "in" to "out" */
1396 while ( (n=fread(buf, 1, sizeof buf, in)) > 0 )
1398 if ( ! fwrite(buf, 1, n, out) )
1409 #endif /*_WIN32_WCE*/
1411 #ifdef HAVE_READLINE
1413 linephonec_readline_completion(const char *text, int start, int end)
1415 char **matches = NULL;
1418 * Prevent readline from falling
1419 * back to filename-completion
1421 rl_attempted_completion_over=1;
1424 * If this is the start of line we complete with commands
1428 return rl_completion_matches(text, linphonec_command_generator);
1432 * Otherwise, we should peek at command name
1433 * or context to implement a smart completion.
1434 * For example: "call .." could return
1435 * friends' sip-uri as matches
1444 * Strip blanks from a string.
1445 * Return a pointer into the provided string.
1446 * Modifies input adding a NULL at first
1447 * of trailing blanks.
1450 lpc_strip_blanks(char *input)
1454 /* Find first non-blank */
1455 while(*input && isspace(*input)) ++input;
1457 /* Find last non-blank */
1458 iptr=input+strlen(input);
1460 while(isspace(*--iptr));
1467 /****************************************************************************
1469 * $Log: linphonec.c,v $
1470 * Revision 1.57 2007/11/14 13:40:27 smorlat
1471 * fix --disable-video build.
1473 * Revision 1.56 2007/09/26 14:07:27 fixkowalski
1474 * - ANSI/C++ compilation issues with non-GCC compilers
1475 * - Faster epm-based packaging
1476 * - Ability to build & run on FC6's eXosip/osip
1478 * Revision 1.55 2007/09/24 16:01:58 smorlat
1481 * Revision 1.54 2007/08/22 14:06:11 smorlat
1482 * authentication bugs fixed.
1484 * Revision 1.53 2007/02/13 21:31:01 smorlat
1485 * added patch for general state.
1486 * new doxygen for oRTP
1489 * Revision 1.52 2007/01/10 14:11:24 smorlat
1490 * add --video to linphonec.
1492 * Revision 1.51 2006/08/21 12:49:59 smorlat
1493 * merged several little patches.
1495 * Revision 1.50 2006/07/26 08:17:28 smorlat
1498 * Revision 1.49 2006/07/17 18:45:00 smorlat
1499 * support for several event queues in ortp.
1500 * glib dependency removed from coreapi/ and console/
1502 * Revision 1.48 2006/04/09 12:45:32 smorlat
1503 * linphonec improvements.
1505 * Revision 1.47 2006/04/04 08:04:34 smorlat
1506 * switched to mediastreamer2, most bugs fixed.
1508 * Revision 1.46 2006/03/16 17:17:40 smorlat
1511 * Revision 1.45 2006/03/12 21:48:31 smorlat
1512 * gcc-2.95 compile error fixed.
1513 * mediastreamer2 in progress
1515 * Revision 1.44 2006/03/04 11:17:10 smorlat
1516 * mediastreamer2 in progress.
1518 * Revision 1.43 2006/02/13 09:50:50 strk
1519 * fixed unused variable warning.
1521 * Revision 1.42 2006/02/02 15:39:18 strk
1522 * - Added 'friend list' and 'friend call' commands
1523 * - Allowed for multiple DTFM send in a single line
1524 * - Added status-specific callback (bare version)
1526 * Revision 1.41 2006/02/02 13:30:05 strk
1527 * - Padded vtable with missing callbacks
1528 * (fixing a segfault on friends subscription)
1529 * - Handled friends notify (bare version)
1530 * - Handled text messages receive (bare version)
1531 * - Printed message on subscription request (bare version)
1533 * Revision 1.40 2006/01/26 09:48:05 strk
1534 * Added limits.h include
1536 * Revision 1.39 2006/01/26 02:11:01 strk
1537 * Removed unused variables, fixed copyright date
1539 * Revision 1.38 2006/01/25 18:33:02 strk
1540 * Removed the -t swich, terminate_on_close made the default behaviour
1542 * Revision 1.37 2006/01/20 14:12:34 strk
1543 * Added linphonec_init() and linphonec_finish() functions.
1544 * Handled SIGINT and SIGTERM to invoke linphonec_finish().
1545 * Handling of auto-termination (-t) moved to linphonec_finish().
1546 * Reworked main (input read) loop to not rely on 'terminate'
1547 * and 'run' variable (dropped). configfile_name allocated on stack
1548 * using PATH_MAX limit. Changed print_usage signature to allow
1549 * for an exit_status specification.
1551 * Revision 1.36 2006/01/18 09:25:32 strk
1552 * Command completion inhibited in proxy addition and auth request prompts.
1553 * Avoided use of readline's internal filename completion.
1555 * Revision 1.35 2006/01/14 13:29:32 strk
1556 * Reworked commands interface to use a table structure,
1557 * used by command line parser and help function.
1558 * Implemented first level of completion (commands).
1559 * Added notification of invalid "answer" and "terminate"
1560 * commands (no incoming call, no active call).
1561 * Forbidden "call" intialization when a call is already active.
1562 * Cleaned up all commands, adding more feedback and error checks.
1564 * Revision 1.34 2006/01/13 13:00:29 strk
1565 * Added linphonec.h. Code layout change (added comments, forward decl,
1566 * globals on top, copyright notices and Logs). Handled out-of-memory
1567 * condition on history management. Removed assumption on sizeof(char).
1568 * Fixed bug in authentication prompt (introduced by readline).
1569 * Added support for multiple authentication requests (up to MAX_PENDING_AUTH).
1572 ****************************************************************************/