cbhm - the service has been change to extend support to start by systemd
[framework/uifw/cbhm.git] / src / main.c
1 /*
2  * cbhm
3  * Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
4  *
5  * Licensed under the Apache License, Version 2.0 (the License);
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an AS IS BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18
19 #include <systemd/sd-daemon.h>
20 #include <appcore-efl.h>
21 #include <Ecore_X.h>
22 #include <X11/Xlib.h>
23 #include <X11/Xatom.h>
24 #include <X11/extensions/XInput.h>
25 #include <X11/extensions/XInput2.h>
26 #include <X11/extensions/XI2.h>
27 #include <X11/extensions/XIproto.h>
28
29 #include "cbhm.h"
30
31 #define CLIPBOARD_MANAGER_WINDOW_TITLE_STRING "X11_CLIPBOARD_HISTORY_MANAGER"
32 #define ATOM_CLIPBOARD_MANAGER_NAME "CLIPBOARD_MANAGER"
33
34 static AppData *g_main_ad = NULL;
35
36 void *d_malloc(char *func, int line, size_t size)
37 {
38         char *m = malloc(size);
39         printf("in %s, %d: 0x%x = malloc(%d)\n", func, line, m, size);
40         return m;
41 }
42 void *d_calloc(char *func, int line, size_t n, size_t size)
43 {
44         char *m = calloc(n, size);
45         printf("in %s, %d: 0x%x = calloc(%d)\n", func, line, m, size);
46         return m;
47 }
48 void d_free(char *func, int line, void *m)
49 {
50         printf("in %s, %d: free(0x%x)\n", func, line, m);
51         free(m);
52 }
53
54 static Eina_Bool setClipboardManager(AppData *ad)
55 {
56         ad->x_disp = ecore_x_display_get();
57         DMSG("x_disp: 0x%x\n", ad->x_disp);
58         if (ad->x_disp)
59         {
60                 Ecore_X_Atom clipboard_manager_atom = XInternAtom(ad->x_disp, ATOM_CLIPBOARD_MANAGER_NAME, False);
61                 Ecore_X_Window clipboard_manager = XGetSelectionOwner(ad->x_disp, clipboard_manager_atom);
62                 DMSG("clipboard_manager_window: 0x%x\n");
63                 if (!clipboard_manager)
64                 {
65                         ad->x_root_win = DefaultRootWindow(ad->x_disp);
66                         if (ad->x_root_win)
67                         {
68                                 ad->x_event_win = ecore_x_window_new(ad->x_root_win, 0, 0, 19, 19);
69                                 DMSG("x_event_win: 0x%x\n", ad->x_event_win);
70                                 if (ad->x_event_win)
71                                 {
72                                         XSetSelectionOwner(ad->x_disp, clipboard_manager_atom, ad->x_event_win, CurrentTime);
73                                         Ecore_X_Window clipboard_manager = XGetSelectionOwner(ad->x_disp, clipboard_manager_atom);
74                                         DMSG("clipboard_manager: 0x%x\n", clipboard_manager);
75                                         if (ad->x_event_win == clipboard_manager)
76                                         {
77                                                 return EINA_TRUE;
78                                         }
79                                 }
80                         }
81                 }
82         }
83         return EINA_FALSE;
84 }
85
86 static void set_x_window(Ecore_X_Window x_event_win, Ecore_X_Window x_root_win)
87 {
88         ecore_x_netwm_name_set(x_event_win, CLIPBOARD_MANAGER_WINDOW_TITLE_STRING);
89         ecore_x_event_mask_set(x_event_win,
90                         ECORE_X_EVENT_MASK_WINDOW_PROPERTY);
91         ecore_x_event_mask_set(x_root_win,
92                         ECORE_X_EVENT_MASK_WINDOW_CONFIGURE);
93         ecore_x_window_prop_property_set(
94                         x_root_win, ecore_x_atom_get("CBHM_XWIN"),
95                         XA_WINDOW, 32, &x_event_win, 1);
96         ecore_x_flush();
97 }
98
99 static int app_create(void *data)
100 {
101         AppData *ad = data;
102
103         if (!setClipboardManager(ad))
104         {
105                 DMSG("Clipboard Manager set failed\n");
106                 return EXIT_FAILURE;
107         }
108
109         set_x_window(ad->x_event_win, ad->x_root_win);
110
111         if (!ecore_init()) return EXIT_FAILURE;
112         if (!ecore_evas_init()) return EXIT_FAILURE;
113         if (!edje_init()) return EXIT_FAILURE;
114         ad->magic = CBHM_MAGIC;
115         init_target_atoms(ad);
116         if (!(ad->clipdrawer = init_clipdrawer(ad))) return EXIT_FAILURE;
117         if (!(ad->xhandler = init_xhandler(ad))) return EXIT_FAILURE;
118         if (!(ad->storage = init_storage(ad))) return EXIT_FAILURE;
119         slot_item_count_set(ad);
120
121         set_selection_owner(ad, ECORE_X_SELECTION_CLIPBOARD, NULL);
122         return 0;
123 }
124
125 static int app_terminate(void *data)
126 {
127         AppData *ad = data;
128
129         depose_clipdrawer(ad->clipdrawer);
130         depose_xhandler(ad->xhandler);
131         depose_storage(ad->storage);
132         item_clear_all(ad);
133         depose_target_atoms(ad);
134         FREE(ad);
135
136         return 0;
137 }
138
139 static int app_pause(void *data)
140 {
141         AppData *ad = data;
142         return 0;
143 }
144
145 static int app_resume(void *data)
146 {
147         AppData *ad = data;
148         return 0;
149 }
150
151 static int app_reset(bundle *b, void *data)
152 {
153         AppData *ad = data;
154
155         return 0;
156 }
157
158 int main(int argc, char *argv[])
159 {
160         AppData *ad;
161         struct appcore_ops ops = {
162                 .create = app_create,
163                 .terminate = app_terminate,
164                 .pause = app_pause,
165                 .resume = app_resume,
166                 .reset = app_reset,
167         };
168         ad = CALLOC(1, sizeof(AppData));
169         ops.data = ad;
170         g_main_ad = ad;
171
172         appcore_set_i18n(PACKAGE, LOCALEDIR);
173
174         // Notyfication to systemd
175         sd_notify(1, "READY=1");
176
177         return appcore_efl_main(PACKAGE, &argc, &argv, &ops);
178 }