tizen 2.4 release
[framework/uifw/cbhm.git] / 2.3-mobile / src / main.c
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *  Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
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 #include <appcore-efl.h>
18 #include <Ecore_X.h>
19 #include <systemd/sd-daemon.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 int _log_domain = -1;
35
36 void *d_malloc(const char *func, int line, size_t size)
37 {
38         char *m = malloc(size);
39         DBG("in %s, %d: 0x%p = malloc(%d)", func, line, m, size);
40         return m;
41 }
42 void *d_calloc(const char *func, int line, size_t n, size_t size)
43 {
44         char *m = calloc(n, size);
45         DBG("in %s, %d: 0x%p = calloc(%d)", func, line, m, size);
46         return m;
47 }
48 void d_free(const char *func, int line, void *m)
49 {
50         DBG("in %s, %d: free(0x%p)", func, line, m);
51         free(m);
52 }
53
54 static Eina_Bool setClipboardManager(AppData *ad)
55 {
56         if (!ad) return EINA_FALSE;
57
58         if(!ecore_file_mkpath(COPIED_DATA_STORAGE_DIR))
59                 DBG("ecore_file_mkpath fail");
60
61         ad->x_disp = ecore_x_display_get();
62         DBG("x_disp: 0x%p", ad->x_disp);
63         if (ad->x_disp)
64         {
65                 Ecore_X_Atom clipboard_manager_atom = XInternAtom(ad->x_disp, ATOM_CLIPBOARD_MANAGER_NAME, False);
66                 Ecore_X_Window clipboard_manager = XGetSelectionOwner(ad->x_disp, clipboard_manager_atom);
67                 DBG("clipboard_manager_window: 0x%x", clipboard_manager);
68                 if (!clipboard_manager)
69                 {
70                         ad->x_root_win = DefaultRootWindow(ad->x_disp);
71                         if (ad->x_root_win)
72                         {
73                                 ad->x_event_win = ecore_x_window_new(ad->x_root_win, 0, 0, 19, 19);
74                                 DBG("x_event_win: 0x%x", ad->x_event_win);
75                                 if (ad->x_event_win)
76                                 {
77                                         XSetSelectionOwner(ad->x_disp, clipboard_manager_atom, ad->x_event_win, CurrentTime);
78                                         Ecore_X_Window clipboard_manager = XGetSelectionOwner(ad->x_disp, clipboard_manager_atom);
79                                         DBG("clipboard_manager: 0x%x", clipboard_manager);
80                                         if (ad->x_event_win == clipboard_manager)
81                                         {
82                                                 return EINA_TRUE;
83                                         }
84                                 }
85                         }
86                 }
87         }
88         return EINA_FALSE;
89 }
90
91 static void set_x_window(Ecore_X_Window x_event_win, Ecore_X_Window x_root_win)
92 {
93         ecore_x_netwm_name_set(x_event_win, CLIPBOARD_MANAGER_WINDOW_TITLE_STRING);
94         ecore_x_event_mask_set(x_event_win,
95                         ECORE_X_EVENT_MASK_WINDOW_PROPERTY);
96         ecore_x_event_mask_set(x_root_win,
97                         ECORE_X_EVENT_MASK_WINDOW_CONFIGURE);
98         ecore_x_window_prop_property_set(
99                         x_root_win, ecore_x_atom_get("CBHM_XWIN"),
100                         XA_WINDOW, 32, &x_event_win, 1);
101         ecore_x_flush();
102 }
103
104 static int app_create(void *data)
105 {
106         AppData *ad = (AppData *)data;
107
108         elm_app_base_scale_set(2.6);
109         ecore_x_init(ad->x_disp);
110         _log_domain = eina_log_domain_register("cbhm", EINA_COLOR_LIGHTBLUE);
111         if (!_log_domain)
112                 {
113                         EINA_LOG_ERR("could not register cbhm log domain.");
114                         _log_domain = EINA_LOG_DOMAIN_GLOBAL;
115                 }
116
117         if (!setClipboardManager(ad))
118         {
119                 DBG("Clipboard Manager set failed");
120                 return EXIT_FAILURE;
121         }
122
123         set_x_window(ad->x_event_win, ad->x_root_win);
124
125         if (!ecore_init()) return EXIT_FAILURE;
126         if (!ecore_evas_init()) return EXIT_FAILURE;
127         if (!edje_init()) return EXIT_FAILURE;
128         ad->magic = CBHM_MAGIC;
129         init_target_atoms(ad);
130         if (!(ad->clipdrawer = init_clipdrawer(ad))) return EXIT_FAILURE;
131
132         //set env for root.
133         setenv("HOME", "/", 1);
134         if (!(ad->xhandler = init_xhandler(ad))) return EXIT_FAILURE;
135         if (!(ad->storage = init_storage(ad))) return EXIT_FAILURE;
136         slot_item_count_set(ad);
137
138         set_selection_owner(ad, ECORE_X_SELECTION_CLIPBOARD, NULL);
139         return 0;
140 }
141
142 static int app_terminate(void *data)
143 {
144         AppData *ad = data;
145
146         item_clear_all(ad);
147         depose_clipdrawer(ad->clipdrawer);
148         depose_xhandler(ad->xhandler);
149         depose_storage(ad->storage);
150         depose_target_atoms(ad);
151         FREE(ad);
152
153         eina_log_domain_unregister(_log_domain);
154         _log_domain = -1;
155
156         return 0;
157 }
158
159 static int app_pause(void *data)
160 {
161         AppData *ad = data;
162         Ecore_X_Illume_Clipboard_State state = ecore_x_e_illume_clipboard_state_get(ad->x_active_win);
163         if(state == ECORE_X_ILLUME_CLIPBOARD_STATE_ON)
164         {
165                 clipdrawer_lower_view(ad);
166         }
167         return 0;
168 }
169
170 static int app_resume(void *data)
171 {
172         return 0;
173 }
174
175 static int app_reset(bundle *b, void *data)
176 {
177         return 0;
178 }
179
180 static int _lang_changed(void *data)
181 {
182         return 0;
183 }
184
185 int main(int argc, char *argv[])
186 {
187         AppData *ad;
188
189         struct appcore_ops ops = {
190                 .create = app_create,
191                 .terminate = app_terminate,
192                 .pause = app_pause,
193                 .resume = app_resume,
194                 .reset = app_reset,
195         };
196
197         ad = calloc(1, sizeof(AppData));
198         ops.data = ad;
199         g_main_ad = ad;
200
201         appcore_set_i18n(PACKAGE, LOCALEDIR);
202         appcore_set_event_callback(APPCORE_EVENT_LANG_CHANGE, _lang_changed, NULL);
203
204         // Notyfication to systemd
205         sd_notify(1, "READY=1");
206
207         //set env app for changeable UI.
208         setenv("HOME", "/opt/home/app" , 1);
209
210         return appcore_efl_main(PACKAGE, &argc, &argv, &ops);
211 }