5c493d47519e89dd3f073582ec1b99467e3a1d19
[framework/appfw/app-core.git] / src / appcore-rotation.c
1 /*
2  *  app-core
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Jayoun Lee <airjany@samsung.com>, Sewook Park <sewook7.park@samsung.com>, Jaeho Lee <jaeho81.lee@samsung.com>
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  */
21
22
23 #include <errno.h>
24 #include <stdlib.h>
25 #include <unistd.h>
26
27 #include <sensor.h>
28 #include <vconf.h>
29 #include <Ecore_X.h>
30 #include <Ecore.h>
31 #include <X11/Xlib.h>
32
33 #include "appcore-internal.h"
34
35 #define _MAKE_ATOM(a, s)                              \
36    do {                                               \
37         a = ecore_x_atom_get(s);                      \
38         if (!a)                                       \
39           _ERR("##s creation failed.\n");             \
40    } while(0)
41
42 #define STR_ATOM_ROTATION_LOCK                "_E_ROTATION_LOCK"
43
44 static Ecore_X_Atom ATOM_ROTATION_LOCK = 0;
45 static Ecore_X_Window root;
46
47 struct rot_s {
48         int handle;
49         int (*callback) (enum appcore_rm, void *);
50         enum appcore_rm mode;
51         int lock;
52         void *cbdata;
53         int cb_set;
54         int sf_started;
55
56         struct ui_wm_rotate* wm_rotate;
57 };
58 static struct rot_s rot;
59
60 struct rot_evt {
61         enum accelerometer_rotate_state re;
62         enum appcore_rm rm;
63 };
64
65 static struct rot_evt re_to_rm[] = {
66         {
67          ROTATION_EVENT_0,
68           APPCORE_RM_PORTRAIT_NORMAL,
69         },
70         {
71          ROTATION_EVENT_90,
72          APPCORE_RM_LANDSCAPE_NORMAL,
73         },
74         {
75          ROTATION_EVENT_180,
76          APPCORE_RM_PORTRAIT_REVERSE,
77         },
78         {
79          ROTATION_EVENT_270,
80          APPCORE_RM_LANDSCAPE_REVERSE,
81         },
82 };
83
84 static enum appcore_rm __get_mode(int event_data)
85 {
86         int i;
87         enum appcore_rm m;
88
89         m = APPCORE_RM_UNKNOWN;
90
91         for (i = 0; i < sizeof(re_to_rm) / sizeof(re_to_rm[0]); i++) {
92                 if (re_to_rm[i].re == event_data) {
93                         m = re_to_rm[i].rm;
94                         break;
95                 }
96         }
97
98         return m;
99 }
100
101 static void __changed_cb(unsigned int event_type, sensor_event_data_t *event,
102                        void *data)
103 {
104         int *cb_event_data;
105         enum appcore_rm m;
106         int ret;
107
108         if (rot.lock)
109                 return;
110
111         if (event_type != ACCELEROMETER_EVENT_ROTATION_CHECK) {
112                 errno = EINVAL;
113                 return;
114         }
115
116         cb_event_data = (int *)(event->event_data);
117
118         m = __get_mode(*cb_event_data);
119
120         _DBG("[APP %d] Rotation: %d -> %d", getpid(), rot.mode, m);
121
122         if (rot.callback) {
123                 if (rot.cb_set && rot.mode != m) {
124                         _DBG("[APP %d] Rotation: %d -> %d", getpid(), rot.mode, m);
125                         rot.callback(m, data);
126                         rot.mode = m;
127                 }
128         }
129 }
130
131 static void __lock_cb(keynode_t *node, void *data)
132 {
133         int r;
134         enum appcore_rm m;
135         int ret;
136
137         rot.lock = !vconf_keynode_get_bool(node);
138
139         if (rot.lock) {
140                 m = APPCORE_RM_PORTRAIT_NORMAL;
141                 if (rot.mode != m) {
142                         rot.callback(m, data);
143                         rot.mode = m;
144                 }
145                 _DBG("[APP %d] Rotation locked", getpid());
146                 return;
147         }
148
149         _DBG("[APP %d] Rotation unlocked", getpid());
150         if (rot.callback) {
151                 if (rot.cb_set) {
152                         r = appcore_get_rotation_state(&m);
153                         _DBG("[APP %d] Rotmode prev %d -> curr %d", getpid(),
154                              rot.mode, m);
155                         if (!r && rot.mode != m) {
156                                 rot.callback(m, data);
157                                 rot.mode = m;
158                         }
159                 }
160         }
161 }
162
163 static void __add_rotlock(void *data)
164 {
165         int r;
166         int lock;
167
168         lock = 0;
169         r = vconf_get_bool(VCONFKEY_SETAPPL_AUTO_ROTATE_SCREEN_BOOL, &lock);
170         if (r) {
171                 _DBG("[APP %d] Rotation vconf get bool failed", getpid());
172         }
173
174         rot.lock = !lock;
175
176         vconf_notify_key_changed(VCONFKEY_SETAPPL_AUTO_ROTATE_SCREEN_BOOL, __lock_cb,
177                                  data);
178 }
179
180 static void __del_rotlock(void)
181 {
182         vconf_ignore_key_changed(VCONFKEY_SETAPPL_AUTO_ROTATE_SCREEN_BOOL, __lock_cb);
183         rot.lock = 0;
184 }
185
186 EXPORT_API int appcore_set_rotation_cb(int (*cb) (enum appcore_rm, void *),
187                                        void *data)
188 {
189         if (rot.wm_rotate) {
190                 return rot.wm_rotate->set_rotation_cb(cb, data);
191         }
192         else {
193                 int r;
194                 int handle;
195
196                 if (cb == NULL) {
197                         errno = EINVAL;
198                         return -1;
199                 }
200
201                 if (rot.callback != NULL) {
202                         errno = EALREADY;
203                         return -1;
204                 }
205
206                 handle = sf_connect(ACCELEROMETER_SENSOR);
207                 if (handle < 0) {
208                         _ERR("sf_connect failed: %d", handle);
209                         return -1;
210                 }
211
212                 r = sf_register_event(handle, ACCELEROMETER_EVENT_ROTATION_CHECK,
213                                       NULL, __changed_cb, data);
214                 if (r < 0) {
215                         _ERR("sf_register_event failed: %d", r);
216                         sf_disconnect(handle);
217                         return -1;
218                 }
219
220                 rot.cb_set = 1;
221                 rot.callback = cb;
222                 rot.cbdata = data;
223
224                 r = sf_start(handle, 0);
225                 if (r < 0) {
226                         _ERR("sf_start failed: %d", r);
227                         r = sf_unregister_event(handle, ACCELEROMETER_EVENT_ROTATION_CHECK);
228                         if (r < 0) {
229                                 _ERR("sf_unregister_event failed: %d", r);
230                         }
231                         rot.callback = NULL;
232                         rot.cbdata = NULL;
233                         rot.cb_set = 0;
234                         rot.sf_started = 0;
235                         sf_disconnect(handle);
236                         return -1;
237                 }
238                 rot.sf_started = 1;
239
240                 rot.handle = handle;
241                 __add_rotlock(data);
242
243                 _MAKE_ATOM(ATOM_ROTATION_LOCK, STR_ATOM_ROTATION_LOCK );
244                 root =  ecore_x_window_root_first_get();
245                 XSelectInput(ecore_x_display_get(), root, PropertyChangeMask);
246         }
247         return 0;
248 }
249
250 EXPORT_API int appcore_unset_rotation_cb(void)
251 {
252         if (rot.wm_rotate) {
253                 return rot.wm_rotate->unset_rotation_cb();
254         }
255         else {
256                 int r;
257
258                 _retv_if(rot.callback == NULL, 0);
259
260                 __del_rotlock();
261
262                 if (rot.cb_set) {
263                         r = sf_unregister_event(rot.handle,
264                                                 ACCELEROMETER_EVENT_ROTATION_CHECK);
265                         if (r < 0) {
266                                 _ERR("sf_unregister_event failed: %d", r);
267                                 return -1;
268                         }
269                         rot.cb_set = 0;
270                 }
271                 rot.callback = NULL;
272                 rot.cbdata = NULL;
273
274                 if (rot.sf_started == 1) {
275                         r = sf_stop(rot.handle);
276                         if (r < 0) {
277                                 _ERR("sf_stop failed: %d", r);
278                                 return -1;
279                         }
280                         rot.sf_started = 0;
281                 }
282
283                 r = sf_disconnect(rot.handle);
284                 if (r < 0) {
285                         _ERR("sf_disconnect failed: %d", r);
286                         return -1;
287                 }
288                 rot.handle = -1;
289         }
290         return 0;
291 }
292
293 EXPORT_API int appcore_get_rotation_state(enum appcore_rm *curr)
294 {
295         if (rot.wm_rotate) {
296                 return rot.wm_rotate->get_rotation_state(curr);
297         }
298         else {
299                 int r;
300                 unsigned long event;
301
302                 if (curr == NULL) {
303                         errno = EINVAL;
304                         return -1;
305                 }
306
307                 r = sf_check_rotation(&event);
308                 if (r < 0) {
309                         _ERR("sf_check_rotation failed: %d", r);
310                         *curr = APPCORE_RM_UNKNOWN;
311                         return -1;
312                 }
313
314                 *curr = __get_mode(event);
315         }
316         return 0;
317 }
318
319 EXPORT_API int appcore_pause_rotation_cb(void)
320 {
321         if (rot.wm_rotate) {
322                 return rot.wm_rotate->pause_rotation_cb();
323         }
324         else {
325                 int r;
326
327                 _retv_if(rot.callback == NULL, 0);
328                 _DBG("[APP %d] appcore_pause_rotation_cb is called", getpid());
329
330                 __del_rotlock();
331
332                 if (rot.cb_set) {
333                         r = sf_unregister_event(rot.handle,
334                                                 ACCELEROMETER_EVENT_ROTATION_CHECK);
335                         if (r < 0) {
336                                 _ERR("sf_unregister_event in appcore_internal_sf_stop failed: %d", r);
337                                 return -1;
338                         }
339                         rot.cb_set = 0;
340                 }
341
342                 if (rot.sf_started == 1) {
343                         r = sf_stop(rot.handle);
344                         if (r < 0) {
345                                 _ERR("sf_stop in appcore_internal_sf_stop failed: %d",
346                                      r);
347                                 return -1;
348                         }
349                         rot.sf_started = 0;
350                 }
351         }
352
353         return 0;
354 }
355
356 EXPORT_API int appcore_resume_rotation_cb(void)
357 {
358         if (rot.wm_rotate) {
359                 return rot.wm_rotate->resume_rotation_cb();
360         }
361         else {
362                 int r;
363                 enum appcore_rm m;
364
365                 _retv_if(rot.callback == NULL, 0);
366                 _DBG("[APP %d] appcore_resume_rotation_cb is called", getpid());
367
368                 if (rot.cb_set == 0) {
369                         r = sf_register_event(rot.handle,
370                                               ACCELEROMETER_EVENT_ROTATION_CHECK, NULL,
371                                               __changed_cb, rot.cbdata);
372                         if (r < 0) {
373                                 _ERR("sf_register_event in appcore_internal_sf_start failed: %d", r);
374                                 return -1;
375                         }
376                         rot.cb_set = 1;
377                 }
378
379                 if (rot.sf_started == 0) {
380                         r = sf_start(rot.handle, 0);
381                         if (r < 0) {
382                                 _ERR("sf_start in appcore_internal_sf_start failed: %d",
383                                      r);
384                                 r = sf_unregister_event(rot.handle,
385                                                     ACCELEROMETER_EVENT_ROTATION_CHECK);
386                                 if (r < 0) {
387                                         _ERR("sf_unregister_event failed: %d", r);
388                                 }
389                                 rot.cb_set = 0;
390                                 return -1;
391                         }
392                         rot.sf_started = 1;
393                 }
394
395                 __add_rotlock(rot.cbdata);
396
397                 r = appcore_get_rotation_state(&m);
398                 _DBG("[APP %d] Rotmode prev %d -> curr %d", getpid(), rot.mode, m);
399                 if (!r && rot.mode != m && rot.lock == 0) {
400                         rot.callback(m, rot.cbdata);
401                         rot.mode = m;
402                 }
403         }
404         return 0;
405 }
406
407 EXPORT_API int appcore_set_wm_rotation(struct ui_wm_rotate* wm_rotate)
408 {
409         if (!wm_rotate) return -1;
410
411         if (rot.callback) {
412                 wm_rotate->set_rotation_cb(rot.callback, rot.cbdata);
413                 appcore_unset_rotation_cb();
414         }
415         rot.wm_rotate = wm_rotate;
416         _DBG("[APP %d] Support wm rotate:%p", getpid(), wm_rotate);
417         return 0;
418 }