Tizen 2.0 Release
[apps/home/settings.git] / src / plugin-resource / test_server.c
1 #include <stdio.h>
2 #include <dbus/dbus.h>
3 #include <Elementary.h>
4 #include <appcore-efl.h>
5 #include <Ecore_X.h>
6 #include <app.h>
7 #define TRACE(fmt, arg...) \
8         do {\
9                 printf("\n[SETTING]\033[0;32m" fmt "\033[0m\t%s:%d\n", \
10                                 ##arg, (char*)(strrchr(__FILE__, '/')+1), __LINE__);\
11         }while(0);
12
13
14 #define MAX_LOCAL_BUFSIZE 128
15 #define DBUS_PATH "/setting/dbus_handler"
16 #define DBUS_SIGNAL_INTERFACE "org.tizen.setting.signal"
17 #define DBUS_SIGNAL "test"
18
19
20
21 #define PACKAGE "com.samsung.dbus_server"
22
23 struct appdata
24 {
25         Evas* evas;
26         Evas_Object *win_main;
27         Evas_Object *bg;
28         Evas_Object *layout_main;
29         Evas_Object *navi;
30
31
32         Evas_Coord root_w;
33         Evas_Coord root_h;
34 };
35
36 static DBusConnection *bus;
37
38 static DBusHandlerResult __dubs_msg_cb(DBusConnection* conn, DBusMessage* message, void* user_data)
39 {
40     int my_pid = getpid();
41     int sender_pid = 0;
42     char* key = NULL;
43     char* value = NULL;
44
45
46     DBusError error;
47     dbus_error_init(&error);
48
49     if (dbus_message_is_signal(message, DBUS_SIGNAL_INTERFACE, DBUS_SIGNAL))
50     {
51         if (dbus_message_get_args(message, &error,
52                 DBUS_TYPE_UINT32, &sender_pid,
53                 DBUS_TYPE_STRING, &key,
54                 DBUS_TYPE_STRING, &value,
55                 DBUS_TYPE_INVALID) == FALSE)
56         {
57             TRACE("Fail to get data : %s", error.message);
58             dbus_error_free(&error);
59             return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
60         }
61     }
62
63     if (sender_pid != 0 && my_pid != sender_pid)
64     {
65         //TRACE("[SERVER] received key : %s , value : %s ", key, value);
66         if (key && '\0' != key[0] && value && '\0' != value[0] )
67         {
68             //parase "key"
69             char *p = NULL;
70             char *id = NULL;
71             char *title = NULL;
72             p = strchr(key, '|');
73             if (p && '\0' != p[0])
74             {
75                     *p = '\0';
76                     id = key;
77                     p++;
78                     title = p;
79             }
80             //TRACE("[SERVER] title[%s], id[%s]", title, id);
81
82             //parase "key"
83             p = strchr(value, '|');
84             if (p && '\0' != p[0])
85             {
86                     *p = '\0';
87                     char *type = value;
88                     p++;
89                     if (type && 0 == strcmp("INT", type))
90                     {
91                         int value = atoi(p);
92                         TRACE("[SERVER] The value of \"%s\"(its JasonID is %s) had changed to be :%d",
93                                 title, id, value);
94                     }
95                     else
96                     {
97                         char *value = p;
98                         TRACE("[SERVER] The value of \"%s\"(its JasonID is %s) had changed to be :%s",
99                                 title, id, value);
100                     }
101             }
102         }
103     }
104
105
106     return DBUS_HANDLER_RESULT_HANDLED;
107 }
108
109 int __dbus_msg_listen(void)
110 {
111     DBusError error;
112     char rule[MAX_LOCAL_BUFSIZE];
113
114     dbus_error_init(&error);
115     bus = dbus_bus_get_private(DBUS_BUS_SYSTEM, &error);
116     if (!bus)
117     {
118         TRACE("[SERVER] Fail to connect to the D-BUS daemon: %s ", error.message);
119         dbus_error_free(&error);
120         return -1;
121     }
122
123     dbus_connection_setup_with_g_main(bus, NULL);
124     snprintf(rule, MAX_LOCAL_BUFSIZE, "path='%s',type='signal',interface='%s'", DBUS_PATH, DBUS_SIGNAL_INTERFACE);
125
126     dbus_bus_add_match(bus, rule, &error);
127     if (dbus_error_is_set(&error))
128     {
129         TRACE("[SERVER] Fail to rule set; %s ", error.message);
130         dbus_error_free(&error);
131         return -1;
132     }
133
134     if (dbus_connection_add_filter(bus, __dubs_msg_cb, NULL, NULL) == FALSE)
135     {
136         return -1;
137     }
138
139     TRACE("[SERVER] app signal initialized ");
140
141     return 0;
142 }
143
144
145 int __dbus_msg_unlisten(void)
146 {
147     DBusError error;
148     char rule[MAX_LOCAL_BUFSIZE];
149
150     dbus_error_init(&error);
151     dbus_connection_remove_filter(bus, __dubs_msg_cb, NULL);
152     snprintf(rule, MAX_LOCAL_BUFSIZE, "path='%s',type='signal',interface='%s'", DBUS_PATH, DBUS_SIGNAL_INTERFACE);
153     dbus_bus_remove_match(bus, rule, &error);
154
155     if (dbus_error_is_set(&error))
156     {
157         TRACE("Fail to rule unset: %s ", error.message);
158         dbus_error_free(&error);
159         return -1;
160     }
161
162     dbus_connection_close(bus);
163     TRACE("[SERVER] app signal finalized");
164
165     return 0;
166 }
167
168 static void _quit_cb(void *data, Evas_Object* obj, void* event_info)
169 {
170     Evas_Object *win = (Evas_Object *) data;
171     elm_win_lower(win);
172 }
173
174 static Eina_Bool _keydown_event(void *data, int type, void *event)
175 {
176     Ecore_Event_Key *ev = event;
177
178     if (!strcmp(ev->keyname, "XF86Stop")) {
179         elm_exit();
180     }
181
182     return EINA_TRUE;
183 }
184
185 static Evas_Object* create_win(const char *name)
186 {
187     Evas_Object *eo;
188     int w, h;
189
190     eo = elm_win_add(NULL, name, ELM_WIN_BASIC);
191     if (!eo) return NULL;
192
193     elm_win_title_set(eo, name);
194     elm_win_borderless_set(eo, EINA_TRUE);
195     elm_win_autodel_set(eo, EINA_TRUE);
196     evas_object_smart_callback_add(eo, "delete,request", _quit_cb, NULL);
197     ecore_x_window_size_get(ecore_x_window_root_first_get(), &w, &h);
198     evas_object_resize(eo, w, h);
199
200     return eo;
201 }
202
203 static Evas_Object* _create_bg(Evas_Object *parent)
204 {
205     Evas_Object *bg;
206
207     bg = elm_bg_add(parent);
208     evas_object_size_hint_weight_set(bg, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
209     //elm_win_resize_object_add(parent, bg);
210     evas_object_show(bg);
211
212     return bg;
213 }
214
215 static Evas_Object* _create_layout_main(Evas_Object* parent)
216 {
217     Evas_Object *layout;
218
219     layout = elm_layout_add(parent);
220
221     elm_layout_theme_set(layout, "layout", "application", "default");
222     evas_object_size_hint_weight_set(layout, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
223     //elm_win_resize_object_add(parent, layout);
224
225     evas_object_show(layout);
226
227     return layout;
228 }
229
230 static Evas_Object* _create_naviframe_layout(Evas_Object* parent)
231 {
232     Evas_Object *navi;
233
234     navi = elm_naviframe_add(parent);
235     elm_object_part_content_set(parent, "elm.swallow.content", navi);
236
237     evas_object_show(navi);
238
239     return navi;
240 }
241
242
243 void list_item_touch_handler_exit(void* data, Evas_Object* obj, void* event_info)
244 {
245     elm_exit();
246 }
247
248 static Evas_Object* _create_list_winset(Evas_Object* parent, struct appdata* ad)
249 {
250     Evas_Object *li;
251     int idx = 0;
252     //struct _menu_item *menu_its;
253
254     li = elm_list_add(parent);
255     elm_list_mode_set(li, ELM_LIST_COMPRESS);
256
257         elm_list_item_append( li, "QUIT", NULL, NULL, list_item_touch_handler_exit, ad);
258
259     elm_list_go(li);
260     return li;
261 }
262
263 static void _create_view_layout(struct appdata *ad)
264 {
265     Evas_Object *list;
266     Evas_Object *btn;
267
268     list = _create_list_winset(ad->win_main, ad);
269     btn = elm_button_add(ad->navi);
270     elm_object_style_set(btn, "naviframe/end_btn/default");
271     evas_object_smart_callback_add(btn, "clicked", _quit_cb, ad->win_main);
272     elm_naviframe_item_push(ad->navi, _("Tizen UI - Server"), btn, NULL, list, NULL);
273 }
274
275
276
277 static void _mouse_down_cb(void *data, Evas *evas, Evas_Object *obj, void *event_info)
278 {
279     //struct appdata *ad = (struct appdata *)data;
280     //Evas_Event_Mouse_Down *ev = event_info;
281
282 }
283
284 static void _mouse_up_cb(void *data, Evas *evas, Evas_Object *obj, void *event_info)
285 {
286     //struct appdata *ad = (struct appdata *)data;
287
288 }
289
290 static void _mouse_move_cb(void *data, Evas *evas, Evas_Object *obj, void *event_info)
291 {
292     //struct appdata *ad = (struct appdata *)data;
293 }
294
295 static void _window_resize_cb(void *data, Evas * e, Evas_Object * obj, void *event_info)
296 {
297     struct appdata *ad = (struct appdata *)data;
298     evas_object_geometry_get(ad->win_main, NULL, NULL, &ad->root_w, &ad->root_h);
299 }
300
301 #if 0
302 static int _rotation_cb(enum appcore_rm mode, void *data)
303 {
304     struct appdata *ad = (struct appdata *)data;
305     int angle;
306
307     switch (mode) {
308         case APPCORE_RM_LANDSCAPE_NORMAL:
309             angle = -90;
310             break;
311
312         case APPCORE_RM_LANDSCAPE_REVERSE:
313             angle = 90;
314             break;
315
316         case APPCORE_RM_PORTRAIT_REVERSE:
317             angle = 180;
318             break;
319
320         case APPCORE_RM_UNKNOWN:
321         case APPCORE_RM_PORTRAIT_NORMAL:
322         default:
323             angle = 0;
324             break;
325     }
326
327     elm_win_rotation_with_resize_set(ad->win_main, angle);
328     // set_rotation_degree(angle);
329
330     // This is need for customized rotation process.
331     // rotate_for_winset(mode);
332
333     return 0;
334 }
335 #endif
336
337 int app_create(void *data)
338 {
339     struct appdata *ad = (struct appdata *) data;
340
341     // Set the engine to opengl_x11
342     //elm_config_engine_set("opengl_x11");
343
344     ad->win_main = create_win(PACKAGE);
345
346     evas_object_event_callback_add(ad->win_main, EVAS_CALLBACK_RESIZE, _window_resize_cb, ad);
347     evas_object_show(ad->win_main);
348     ad->evas = evas_object_evas_get(ad->win_main);
349 //    appcore_set_rotation_cb(_rotation_cb, ad);
350
351     // set_edj_name(NULL);
352
353         ad->bg = _create_bg(ad->win_main);
354         ad->layout_main = _create_layout_main(ad->win_main);
355         elm_win_indicator_mode_set(ad->win_main, ELM_WIN_INDICATOR_SHOW);
356
357         ad->navi = _create_naviframe_layout(ad->layout_main);
358
359         _create_view_layout(ad);
360
361     evas_object_event_callback_add(ad->navi, EVAS_CALLBACK_MOUSE_DOWN, _mouse_down_cb, ad);
362     evas_object_event_callback_add(ad->navi, EVAS_CALLBACK_MOUSE_UP, _mouse_up_cb,  ad);
363     evas_object_event_callback_add(ad->navi, EVAS_CALLBACK_MOUSE_MOVE, _mouse_move_cb, ad);
364
365     ecore_event_handler_add(ECORE_EVENT_KEY_DOWN, _keydown_event, NULL);
366
367         // init dbus handler
368         __dbus_msg_listen();
369
370         return 0;
371 }
372
373 int app_terminate(void *data)
374 {
375     //struct appdata *ad = data;
376
377         // terminate handler
378         __dbus_msg_unlisten();
379     return 0;
380 }
381
382 int app_pause(void *data)
383 {
384     //struct appdata *ad = data;
385
386     return 0;
387 }
388
389 int app_resume(void *data)
390 {
391     //struct appdata *ad = data;
392
393     return 0;
394 }
395
396 int app_reset(service_h service, void* data)
397 {
398         struct appdata* ad = data;
399
400         if (ad->win_main)
401                 elm_win_activate(ad->win_main);
402
403         return 0;
404 }
405
406
407 int main(int argc, char* argv[])
408 {
409         struct appdata ad;
410
411         struct appcore_ops ops = {
412                 .create = app_create,
413                 .terminate = app_terminate,
414                 .pause = app_pause,
415                 .resume = app_resume,
416                 .reset = app_reset,
417         };
418
419         ops.data = &ad;
420
421         return appcore_efl_main(PACKAGE, &argc, &argv, &ops);
422 }