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