]> sjero.net Git - linphone/blob - gtk/singleinstance.c
Merge branch 'master' of git.savannah.nongnu.org:/srv/git/linphone
[linphone] / gtk / singleinstance.c
1 /*
2 linphone, gtk interface.
3 Copyright (C) 2011 Belledonne Communications SARL
4 Author: Simon MORLAT (simon.morlat@linphone.org)
5
6 This program is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License
8 as published by the Free Software Foundation; either version 2
9 of the License, or (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19 */
20
21 #include "linphone.h"
22
23 static ms_thread_t pipe_thread;
24 static ortp_pipe_t server_pipe=(ortp_pipe_t)-1;
25 static gboolean server_pipe_running=TRUE;
26 static char *pipe_name;
27
28 static gboolean execute_wakeup(char *uri){
29         linphone_gtk_show_main_window();
30         if (strlen(uri)>0)      
31                 linphone_gtk_refer_received(linphone_gtk_get_core(),uri);
32         g_free(uri);
33         return FALSE;
34 }
35
36 static void * server_pipe_thread(void *pointer){
37         ortp_pipe_t child;
38         
39         do{
40                 child=ortp_server_pipe_accept_client(server_pipe);
41                 if (server_pipe_running && child!=(ortp_pipe_t)-1){
42                         char buf[256]={0};
43                         if (ortp_pipe_read(child,(uint8_t*)buf,sizeof(buf))>0){
44                                 g_message("Received wakeup command with arg %s",buf);
45                                 gdk_threads_enter();
46                                 g_timeout_add(20,(GSourceFunc)execute_wakeup,g_strdup(buf));
47                                 gdk_threads_leave();
48                         }
49                         ortp_server_pipe_close_client(child);
50                 }
51         }while(server_pipe_running);
52         ortp_server_pipe_close(server_pipe);
53         return NULL;
54 }
55
56 static void linphone_gtk_init_pipe(const char *name){
57         pipe_name=g_strdup(name);
58         server_pipe=ortp_server_pipe_create(name);
59         if (server_pipe==(ortp_pipe_t)-1){
60                 g_warning("Fail to create server pipe for name %s: %s",name,strerror(errno));
61         }
62         ms_thread_create(&pipe_thread,NULL,server_pipe_thread,NULL);
63 }
64
65 bool_t linphone_gtk_init_instance(const char *app_name, const char *addr_to_call){
66         ortp_pipe_t p=ortp_client_pipe_connect(app_name);
67         if (p!=(ortp_pipe_t)-1){
68                 uint8_t buf[256]={0};
69                 g_message("There is already a running instance.");
70                 if (addr_to_call!=NULL){
71                         strncpy((char*)buf,addr_to_call,sizeof(buf)-1);
72                 }
73                 if (ortp_pipe_write(p,buf,sizeof(buf))==-1){
74                         g_error("Fail to send wakeup command to running instance: %s",strerror(errno));
75                 }else{
76                         g_message("Message to running instance sent.");
77                 }
78                 ortp_client_pipe_close(p);
79                 return FALSE;
80         }else{
81                 linphone_gtk_init_pipe(app_name);
82         }
83         return TRUE;
84 }
85
86 void linphone_gtk_uninit_instance(void){
87         if (server_pipe!=(ortp_pipe_t)-1){
88                 ortp_pipe_t client;
89                 server_pipe_running=FALSE;
90                 /*this is to unblock the accept() of the server pipe*/
91                 client=ortp_client_pipe_connect(pipe_name);
92                 ortp_pipe_write(client,(uint8_t*)" ",1);
93                 ortp_client_pipe_close(client);
94                 ms_thread_join(pipe_thread,NULL);
95                 server_pipe=(ortp_pipe_t)-1;
96                 g_free(pipe_name);
97                 pipe_name=NULL;
98         }
99 }