]> sjero.net Git - linphone/blob - gtk/singleinstance.c
Aac-eld add missing header according to RFC3640 3.3.6
[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=NULL;
27
28 gchar *make_name(const char *appname){
29         const char *username=getenv("USER");
30         if (username){
31                 return g_strdup_printf("%s-%s",appname,username);
32         }
33         return g_strdup(appname);
34 }
35
36 static gboolean execute_wakeup(char *uri){
37         linphone_gtk_show_main_window();
38         if (strlen(uri)>0)      
39                 linphone_gtk_refer_received(linphone_gtk_get_core(),uri);
40         g_free(uri);
41         return FALSE;
42 }
43
44 static void * server_pipe_thread(void *pointer){
45         ortp_pipe_t child;
46         
47         do{
48                 child=ortp_server_pipe_accept_client(server_pipe);
49                 if (server_pipe_running && child!=(ortp_pipe_t)-1){
50                         char buf[256]={0};
51                         if (ortp_pipe_read(child,(uint8_t*)buf,sizeof(buf))>0){
52                                 g_message("Received wakeup command with arg %s",buf);
53                                 gdk_threads_enter();
54                                 g_timeout_add(20,(GSourceFunc)execute_wakeup,g_strdup(buf));
55                                 gdk_threads_leave();
56                         }
57                         ortp_server_pipe_close_client(child);
58                 }
59         }while(server_pipe_running);
60         ortp_server_pipe_close(server_pipe);
61         return NULL;
62 }
63
64 static void linphone_gtk_init_pipe(const char *name){
65         server_pipe=ortp_server_pipe_create(name);
66         if (server_pipe==(ortp_pipe_t)-1){
67                 g_warning("Fail to create server pipe for name %s: %s",name,strerror(errno));
68         }
69         ms_thread_create(&pipe_thread,NULL,server_pipe_thread,NULL);
70 }
71
72 bool_t linphone_gtk_init_instance(const char *app_name, const char *addr_to_call){
73         pipe_name=make_name(app_name);
74         ortp_pipe_t p=ortp_client_pipe_connect(pipe_name);
75         if (p!=(ortp_pipe_t)-1){
76                 uint8_t buf[256]={0};
77                 g_message("There is already a running instance.");
78                 if (addr_to_call!=NULL){
79                         strncpy((char*)buf,addr_to_call,sizeof(buf)-1);
80                 }
81                 if (ortp_pipe_write(p,buf,sizeof(buf))==-1){
82                         g_error("Fail to send wakeup command to running instance: %s",strerror(errno));
83                 }else{
84                         g_message("Message to running instance sent.");
85                 }
86                 ortp_client_pipe_close(p);
87                 return FALSE;
88         }else{
89                 linphone_gtk_init_pipe(pipe_name);
90         }
91         return TRUE;
92 }
93
94 void linphone_gtk_uninit_instance(void){
95         if (server_pipe!=(ortp_pipe_t)-1){
96                 ortp_pipe_t client;
97                 server_pipe_running=FALSE;
98                 /*this is to unblock the accept() of the server pipe*/
99                 client=ortp_client_pipe_connect(pipe_name);
100                 ortp_pipe_write(client,(uint8_t*)" ",1);
101                 ortp_client_pipe_close(client);
102                 ms_thread_join(pipe_thread,NULL);
103                 server_pipe=(ortp_pipe_t)-1;
104                 g_free(pipe_name);
105                 pipe_name=NULL;
106         }
107 }