Increased to 9 supported view
[livewp] / applet / src / livewp-dbus.c
1 /* vim: set sw=4 ts=4 et: */
2 /*
3  * This file is part of Live Wallpaper (livewp)
4  * 
5  * Copyright (C) 2010 Vlad Vasiliev
6  * Copyright (C) 2010 Tanya Makova
7  *       for the code
8  * 
9  * This software is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License
11  * as published by the Free Software Foundation; either version 2 of
12  * the License, or (at your option) any later version.
13  * 
14  * This software is distributed in the hope that it will be useful, but
15  * WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  * 
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this software; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
22  * 02110-1301 USA
23 */
24
25 /*******************************************************************************/
26 #include "livewp-common.h"
27 #include "livewp-dbus.h"
28 /*******************************************************************************/
29 void
30 livewp_initialize_dbus(Animation_WallpaperPrivate *priv){
31     gchar       *filter_string;
32     DBusError   error;
33
34     dbus_error_init (&error);
35     /* Add D-BUS signal handler for 'status_changed' */
36     priv->dbus_conn = (DBusConnection *) osso_get_sys_dbus_connection(priv->osso);
37     priv->dbus_conn_session = (DBusConnection *) osso_get_dbus_connection(priv->osso);
38
39     if (priv->dbus_conn_session){
40         filter_string =
41             g_strdup_printf("type='signal', interface='%s'", LIVEWP_SIGNAL_INTERFACE);
42         dbus_bus_add_match(priv->dbus_conn_session, filter_string, &error);
43         if (dbus_error_is_set(&error)){
44              fprintf(stderr,"dbus_bus_add_match failed: %s", error.message);
45              dbus_error_free(&error);
46         }
47         g_free(filter_string);
48         /* add the callback */
49         dbus_connection_add_filter(priv->dbus_conn_session,
50                                    (DBusHandleMessageFunction)get_livewp_signal_cb,
51                                    priv, NULL);
52 #if 0
53        filter_string =
54             g_strdup_printf("type='signal', interface='%s'", NOTIFY_SIGNAL_INTERFACE);
55         dbus_bus_add_match(priv->dbus_conn_session, filter_string, &error);
56         if (dbus_error_is_set(&error)){
57              fprintf(stderr,"dbus_bus_add_match failed: %s", error.message);
58              dbus_error_free(&error);
59         }
60         g_free(filter_string);
61         /* add the callback */
62         dbus_connection_add_filter(priv->dbus_conn_session,
63                                    (DBusHandleMessageFunction)get_livewp_signal_cb,
64                                    priv, NULL);
65 #endif
66
67     }
68 }
69 /*******************************************************************************/
70 void
71 livewp_deinitialize_dbus(Animation_WallpaperPrivate *priv){
72
73     gchar       *filter_string;
74     DBusError   error;
75
76     if (priv->dbus_conn_session){
77         filter_string =
78                 g_strdup_printf("type='signal', interface='%s'", LIVEWP_SIGNAL_INTERFACE);
79
80         dbus_error_init (&error);
81         dbus_bus_remove_match(priv->dbus_conn_session, filter_string, &error);
82         if (!dbus_error_is_set(&error)){
83             dbus_connection_remove_filter(priv->dbus_conn_session,
84                                           (DBusHandleMessageFunction)get_livewp_signal_cb, 
85                                           NULL);
86         }else{
87       
88             fprintf(stderr,"dbus_bus_add_match failed: %s", error.message);
89             dbus_error_free(&error);
90         }
91
92          g_free(filter_string);
93          /*
94          filter_string =
95                 g_strdup_printf("type='signal', interface='%s'", NOTIFY_SIGNAL_INTERFACE);
96
97         dbus_error_init (&error);
98         dbus_bus_remove_match(priv->dbus_conn_session, filter_string, &error);
99         if (!dbus_error_is_set(&error)){
100             dbus_connection_remove_filter(priv->dbus_conn_session,
101                                           (DBusHandleMessageFunction)get_livewp_signal_cb, 
102                                           NULL);
103         }else{
104       
105             fprintf(stderr,"dbus_bus_add_match failed: %s", error.message);
106             dbus_error_free(&error);
107         }
108
109          g_free(filter_string);
110 */
111     }
112
113 }
114 /*******************************************************************************/
115 void
116 send_dbus_signal (Animation_WallpaperPrivate *priv,
117                   const gchar *interface,
118                   const gchar *path,
119                   const gchar *member)
120 {
121   gboolean       success;
122 /*    
123   gint param = 0;
124   dbus_uint32_t serial = 0;
125   DBusMessageIter args;
126 */
127   
128   DBusMessage *message = dbus_message_new (DBUS_MESSAGE_TYPE_SIGNAL);
129   dbus_message_set_interface (message, interface);
130   dbus_message_set_path (message, path);
131   dbus_message_set_member (message, member);
132   /*
133   if (member == LIVEWP_PLAY_LIVEBG_ON_VIEW ||
134       member == LIVEWP_PAUSE_LIVEBG_ON_VIEW){
135       param = priv->view + 1;
136
137       fprintf(stderr, "dbus send try send %s %i\n", member, param);
138       dbus_message_iter_init_append(message, &args);
139       if (!dbus_message_iter_append_basic(&args, DBUS_TYPE_INT32, &param)){
140           fprintf(stderr, "error append param\n");
141       }
142   }
143   if (!dbus_connection_send(priv->dbus_conn_session, message, &serial)){
144       fprintf(stderr, "error send\n");
145   }
146   dbus_connection_flush(priv->dbus_conn_session);
147   */
148   success = dbus_connection_send (priv->dbus_conn_session, message, NULL);
149   dbus_connection_flush(priv->dbus_conn_session);
150   dbus_message_unref (message);
151   
152 #if 0
153   fprintf (stderr, "%s '%s' message.\n",
154                                  success ? "Sent" : "Failed to send",
155                                  member);
156 #endif
157
158 }
159
160 /*******************************************************************************/
161 DBusHandlerResult
162 get_livewp_signal_cb(DBusConnection *conn, DBusMessage *msg, Animation_WallpaperPrivate *priv)
163 {
164
165 #ifdef DEBUGFUNCTIONCALL
166     START_FUNCTION;
167 #endif
168
169 #if 0
170 #if  defined APPLICATION 
171     fprintf (stderr, "APPLICATION PATH11111111111111111111 %s %s %s\n",   dbus_message_get_path(msg),   dbus_message_get_interface (msg), dbus_message_get_member (msg)); 
172 #endif
173
174 #if  defined CONTROLPANEL
175     fprintf (stderr, "APPLICATION PATH11111111111111111111 %s %s %s\n",   dbus_message_get_path(msg),   dbus_message_get_interface (msg), dbus_message_get_member (msg)); 
176 #endif
177
178 #if !(defined CONTROLPANEL || defined APPLICATION) 
179     fprintf (stderr, "APPLET PATH11111111111111111111 %s %s %s\n",   dbus_message_get_path(msg),   dbus_message_get_interface (msg), dbus_message_get_member (msg)); 
180 #endif
181 #endif
182     if (!priv)
183         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
184
185     if (dbus_message_is_signal(msg, LIVEWP_SIGNAL_INTERFACE, LIVEWP_RELOAD_CONFIG)){
186         if(read_config(priv)){
187                 fprintf(stderr, "\nCan not read config file.\n");
188         }else{
189             read_config(priv);
190 #if  defined APPLICATION 
191             reload_scene(priv->desktop_plugin);
192 #endif
193         }
194     }
195 #if  defined APPLICATION
196     /* QUIT FROM APPLICATION */
197     if ((priv->view == 1 && dbus_message_is_signal(msg, LIVEWP_SIGNAL_INTERFACE, LIVEWP_STOP_LIVEBG_ON_VIEW1))||
198         (priv->view == 2 && dbus_message_is_signal(msg, LIVEWP_SIGNAL_INTERFACE, LIVEWP_STOP_LIVEBG_ON_VIEW2))||   
199         (priv->view == 3 && dbus_message_is_signal(msg, LIVEWP_SIGNAL_INTERFACE, LIVEWP_STOP_LIVEBG_ON_VIEW3))||   
200         (priv->view == 4 && dbus_message_is_signal(msg, LIVEWP_SIGNAL_INTERFACE, LIVEWP_STOP_LIVEBG_ON_VIEW4))||   
201         (priv->view == 5 && dbus_message_is_signal(msg, LIVEWP_SIGNAL_INTERFACE, LIVEWP_STOP_LIVEBG_ON_VIEW5))||   
202         (priv->view == 6 && dbus_message_is_signal(msg, LIVEWP_SIGNAL_INTERFACE, LIVEWP_STOP_LIVEBG_ON_VIEW6))||   
203         (priv->view == 7 && dbus_message_is_signal(msg, LIVEWP_SIGNAL_INTERFACE, LIVEWP_STOP_LIVEBG_ON_VIEW7))||   
204         (priv->view == 8 && dbus_message_is_signal(msg, LIVEWP_SIGNAL_INTERFACE, LIVEWP_STOP_LIVEBG_ON_VIEW8))||   
205         (priv->view == 9 && dbus_message_is_signal(msg, LIVEWP_SIGNAL_INTERFACE, LIVEWP_STOP_LIVEBG_ON_VIEW9))){
206
207         quit_from_program(priv);
208     }
209     /* PLAY SCENE */
210     if ((priv->view == 1 && dbus_message_is_signal(msg, LIVEWP_SIGNAL_INTERFACE, LIVEWP_PLAY_LIVEBG_ON_VIEW1))||
211         (priv->view == 2 && dbus_message_is_signal(msg, LIVEWP_SIGNAL_INTERFACE, LIVEWP_PLAY_LIVEBG_ON_VIEW2))||   
212         (priv->view == 3 && dbus_message_is_signal(msg, LIVEWP_SIGNAL_INTERFACE, LIVEWP_PLAY_LIVEBG_ON_VIEW3))||   
213         (priv->view == 4 && dbus_message_is_signal(msg, LIVEWP_SIGNAL_INTERFACE, LIVEWP_PLAY_LIVEBG_ON_VIEW4))||   
214         (priv->view == 5 && dbus_message_is_signal(msg, LIVEWP_SIGNAL_INTERFACE, LIVEWP_PLAY_LIVEBG_ON_VIEW5))||   
215         (priv->view == 6 && dbus_message_is_signal(msg, LIVEWP_SIGNAL_INTERFACE, LIVEWP_PLAY_LIVEBG_ON_VIEW6))||   
216         (priv->view == 7 && dbus_message_is_signal(msg, LIVEWP_SIGNAL_INTERFACE, LIVEWP_PLAY_LIVEBG_ON_VIEW7))||   
217         (priv->view == 8 && dbus_message_is_signal(msg, LIVEWP_SIGNAL_INTERFACE, LIVEWP_PLAY_LIVEBG_ON_VIEW8))||   
218         (priv->view == 9 && dbus_message_is_signal(msg, LIVEWP_SIGNAL_INTERFACE, LIVEWP_PLAY_LIVEBG_ON_VIEW9))){
219         /* fprintf(stderr, "Play scene visible %i\n", priv->visible); */
220         if (priv->visible != TRUE){
221             priv->visible = TRUE;
222             view_state_changed(priv);
223         }
224     }
225     /* PAUSE SCENE */
226     if ((priv->view == 1 && dbus_message_is_signal(msg, LIVEWP_SIGNAL_INTERFACE, LIVEWP_PAUSE_LIVEBG_ON_VIEW1))||
227         (priv->view == 2 && dbus_message_is_signal(msg, LIVEWP_SIGNAL_INTERFACE, LIVEWP_PAUSE_LIVEBG_ON_VIEW2))||   
228         (priv->view == 3 && dbus_message_is_signal(msg, LIVEWP_SIGNAL_INTERFACE, LIVEWP_PAUSE_LIVEBG_ON_VIEW3))||   
229         (priv->view == 4 && dbus_message_is_signal(msg, LIVEWP_SIGNAL_INTERFACE, LIVEWP_PAUSE_LIVEBG_ON_VIEW4))||   
230         (priv->view == 5 && dbus_message_is_signal(msg, LIVEWP_SIGNAL_INTERFACE, LIVEWP_PAUSE_LIVEBG_ON_VIEW5))||   
231         (priv->view == 6 && dbus_message_is_signal(msg, LIVEWP_SIGNAL_INTERFACE, LIVEWP_PAUSE_LIVEBG_ON_VIEW6))||   
232         (priv->view == 7 && dbus_message_is_signal(msg, LIVEWP_SIGNAL_INTERFACE, LIVEWP_PAUSE_LIVEBG_ON_VIEW7))||   
233         (priv->view == 8 && dbus_message_is_signal(msg, LIVEWP_SIGNAL_INTERFACE, LIVEWP_PAUSE_LIVEBG_ON_VIEW8))||   
234         (priv->view == 9 && dbus_message_is_signal(msg, LIVEWP_SIGNAL_INTERFACE, LIVEWP_PAUSE_LIVEBG_ON_VIEW9))){
235          /* fprintf(stderr, "Pause scene visible %i\n", priv->visible); */
236          if (priv->visible != FALSE){
237             priv->visible = FALSE;
238             view_state_changed(priv);
239          }
240     }
241     /* ALIVE APPLICATION */
242     if ((priv->view == 1 && dbus_message_is_signal(msg, LIVEWP_SIGNAL_INTERFACE, LIVEWP_ALIVE_LIVEBG_ON_VIEW1))||
243         (priv->view == 2 && dbus_message_is_signal(msg, LIVEWP_SIGNAL_INTERFACE, LIVEWP_ALIVE_LIVEBG_ON_VIEW2))||   
244         (priv->view == 3 && dbus_message_is_signal(msg, LIVEWP_SIGNAL_INTERFACE, LIVEWP_ALIVE_LIVEBG_ON_VIEW3))||   
245         (priv->view == 4 && dbus_message_is_signal(msg, LIVEWP_SIGNAL_INTERFACE, LIVEWP_ALIVE_LIVEBG_ON_VIEW4))||   
246         (priv->view == 5 && dbus_message_is_signal(msg, LIVEWP_SIGNAL_INTERFACE, LIVEWP_ALIVE_LIVEBG_ON_VIEW5))||   
247         (priv->view == 6 && dbus_message_is_signal(msg, LIVEWP_SIGNAL_INTERFACE, LIVEWP_ALIVE_LIVEBG_ON_VIEW6))||   
248         (priv->view == 7 && dbus_message_is_signal(msg, LIVEWP_SIGNAL_INTERFACE, LIVEWP_ALIVE_LIVEBG_ON_VIEW7))||   
249         (priv->view == 8 && dbus_message_is_signal(msg, LIVEWP_SIGNAL_INTERFACE, LIVEWP_ALIVE_LIVEBG_ON_VIEW8))||   
250         (priv->view == 9 && dbus_message_is_signal(msg, LIVEWP_SIGNAL_INTERFACE, LIVEWP_ALIVE_LIVEBG_ON_VIEW9))){
251
252         priv->last_alive_event = time(NULL);
253     }
254
255     /* Check notification of mail, sms, call */
256   //fprintf(stderr, "Type %i\n", dbus_message_get_type (msg));
257     if (dbus_message_is_method_call(msg, NOTIFY_SIGNAL_INTERFACE, NOTIFY_MEMBER)){
258         //fprintf(stderr,"read notifications.db notify\n");    
259         //read_notification(priv->desktop_plugin);
260         priv->scene->notification = time(NULL) + 5;
261     }
262     if (dbus_message_is_signal(msg, NOTIFY_SIGNAL_INTERFACE, CLOSENOTIFY_MEMBER)){
263         //fprintf(stderr,"read notifications.db close notify\n");    
264         //read_notification(priv->desktop_plugin);
265         priv->scene->notification = time(NULL) + 5;
266     }
267 #endif
268
269     return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
270 }
271