changed rotation code
[platform/core/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                         sf_unregister_event(handle, ACCELEROMETER_EVENT_ROTATION_CHECK);
228                         rot.callback = NULL;
229                         rot.cbdata = NULL;
230                         rot.cb_set = 0;
231                         rot.sf_started = 0;
232                         sf_disconnect(handle);
233                         return -1;
234                 }
235                 rot.sf_started = 1;
236
237                 rot.handle = handle;
238                 __add_rotlock(data);
239
240                 _MAKE_ATOM(ATOM_ROTATION_LOCK, STR_ATOM_ROTATION_LOCK );
241                 root =  ecore_x_window_root_first_get();
242                 XSelectInput(ecore_x_display_get(), root, PropertyChangeMask);
243         }
244         return 0;
245 }
246
247 EXPORT_API int appcore_unset_rotation_cb(void)
248 {
249         if (rot.wm_rotate) {
250                 return rot.wm_rotate->unset_rotation_cb();
251         }
252         else {
253                 int r;
254
255                 _retv_if(rot.callback == NULL, 0);
256
257                 __del_rotlock();
258
259                 if (rot.cb_set) {
260                         r = sf_unregister_event(rot.handle,
261                                                 ACCELEROMETER_EVENT_ROTATION_CHECK);
262                         if (r < 0) {
263                                 _ERR("sf_unregister_event failed: %d", r);
264                                 return -1;
265                         }
266                         rot.cb_set = 0;
267                 }
268                 rot.callback = NULL;
269                 rot.cbdata = NULL;
270
271                 if (rot.sf_started == 1) {
272                         r = sf_stop(rot.handle);
273                         if (r < 0) {
274                                 _ERR("sf_stop failed: %d", r);
275                                 return -1;
276                         }
277                         rot.sf_started = 0;
278                 }
279
280                 r = sf_disconnect(rot.handle);
281                 if (r < 0) {
282                         _ERR("sf_disconnect failed: %d", r);
283                         return -1;
284                 }
285                 rot.handle = -1;
286         }
287         return 0;
288 }
289
290 EXPORT_API int appcore_get_rotation_state(enum appcore_rm *curr)
291 {
292         if (rot.wm_rotate) {
293                 return rot.wm_rotate->get_rotation_state(curr);
294         }
295         else {
296                 int r;
297                 unsigned long event;
298
299                 if (curr == NULL) {
300                         errno = EINVAL;
301                         return -1;
302                 }
303
304                 r = sf_check_rotation(&event);
305                 if (r < 0) {
306                         _ERR("sf_check_rotation failed: %d", r);
307                         *curr = APPCORE_RM_UNKNOWN;
308                         return -1;
309                 }
310
311                 *curr = __get_mode(event);
312         }
313         return 0;
314 }
315
316 EXPORT_API int appcore_pause_rotation_cb(void)
317 {
318         if (rot.wm_rotate) {
319                 return rot.wm_rotate->pause_rotation_cb();
320         }
321         else {
322                 int r;
323
324                 _retv_if(rot.callback == NULL, 0);
325                 _DBG("[APP %d] appcore_pause_rotation_cb is called", getpid());
326
327                 __del_rotlock();
328
329                 if (rot.cb_set) {
330                         r = sf_unregister_event(rot.handle,
331                                                 ACCELEROMETER_EVENT_ROTATION_CHECK);
332                         if (r < 0) {
333                                 _ERR("sf_unregister_event in appcore_internal_sf_stop failed: %d", r);
334                                 return -1;
335                         }
336                         rot.cb_set = 0;
337                 }
338
339                 if (rot.sf_started == 1) {
340                         r = sf_stop(rot.handle);
341                         if (r < 0) {
342                                 _ERR("sf_stop in appcore_internal_sf_stop failed: %d",
343                                      r);
344                                 return -1;
345                         }
346                         rot.sf_started = 0;
347                 }
348         }
349
350         return 0;
351 }
352
353 EXPORT_API int appcore_resume_rotation_cb(void)
354 {
355         if (rot.wm_rotate) {
356                 return rot.wm_rotate->resume_rotation_cb();
357         }
358         else {
359                 int r;
360                 enum appcore_rm m;
361
362                 _retv_if(rot.callback == NULL, 0);
363                 _DBG("[APP %d] appcore_resume_rotation_cb is called", getpid());
364
365                 if (rot.cb_set == 0) {
366                         r = sf_register_event(rot.handle,
367                                               ACCELEROMETER_EVENT_ROTATION_CHECK, NULL,
368                                               __changed_cb, rot.cbdata);
369                         if (r < 0) {
370                                 _ERR("sf_register_event in appcore_internal_sf_start failed: %d", r);
371                                 return -1;
372                         }
373                         rot.cb_set = 1;
374                 }
375
376                 if (rot.sf_started == 0) {
377                         r = sf_start(rot.handle, 0);
378                         if (r < 0) {
379                                 _ERR("sf_start in appcore_internal_sf_start failed: %d",
380                                      r);
381                                 sf_unregister_event(rot.handle,
382                                                     ACCELEROMETER_EVENT_ROTATION_CHECK);
383                                 rot.cb_set = 0;
384                                 return -1;
385                         }
386                         rot.sf_started = 1;
387                 }
388
389                 __add_rotlock(rot.cbdata);
390
391                 r = appcore_get_rotation_state(&m);
392                 _DBG("[APP %d] Rotmode prev %d -> curr %d", getpid(), rot.mode, m);
393                 if (!r && rot.mode != m && rot.lock == 0) {
394                         rot.callback(m, rot.cbdata);
395                         rot.mode = m;
396                 }
397         }
398         return 0;
399 }
400
401 EXPORT_API int appcore_set_wm_rotation(struct ui_wm_rotate* wm_rotate)
402 {
403         if (!wm_rotate) return -1;
404
405         if (rot.callback) {
406                 wm_rotate->set_rotation_cb(rot.callback, rot.cbdata);
407                 appcore_unset_rotation_cb();
408         }
409         rot.wm_rotate = wm_rotate;
410         _DBG("[APP %d] Support wm rotate:%p", getpid(), wm_rotate);
411         return 0;
412 }