Set device orientation changed event 24/118224/2
authorHwankyu Jhun <h.jhun@samsung.com>
Fri, 10 Mar 2017 10:01:14 +0000 (19:01 +0900)
committerHwankyu Jhun <h.jhun@samsung.com>
Fri, 10 Mar 2017 10:01:14 +0000 (19:01 +0900)
Change-Id: If89ed8516bdd6ae039756ace10c86ae352b945d8
Signed-off-by: Hwankyu Jhun <h.jhun@samsung.com>
src/base/appcore_base.c

index 58aef2f..348f500 100644 (file)
@@ -35,6 +35,7 @@
 #include <vconf.h>
 #include <aul.h>
 #include <bundle_internal.h>
+#include <sensor_internal.h>
 #include "appcore_base.h"
 #include "appcore_base_private.h"
 
@@ -60,6 +61,13 @@ typedef struct _appcore_base_event_node {
        void *data;
 } appcore_base_event_node;
 
+typedef struct _appcore_base_rotation {
+       int conn;
+       int lock;
+       int ref;
+       enum appcore_base_rm rm;
+} appcore_base_rotation;
+
 struct lang_info_s {
        char *parent;
        GList *list;
@@ -70,6 +78,7 @@ static GList *__events;
 static GDBusConnection *__bus;
 static guint __suspend_dbus_handler_initialized;
 static char *__locale_dir;
+static appcore_base_rotation __rotation;
 
 static void __invoke_callback(void *event, int type)
 {
@@ -100,6 +109,156 @@ static bool __exist_callback(int type)
        return false;
 }
 
+static enum appcore_base_rm __get_rm(sensor_data_t data)
+{
+       int event;
+       enum appcore_base_rm rm;
+
+       if (data.value_count <= 0) {
+               _ERR("Failed to get sensor data");
+               return APPCORE_BASE_RM_UNKNOWN;
+       }
+
+       event = data.values[0];
+       switch (event) {
+       case AUTO_ROTATION_DEGREE_0:
+               rm = APPCORE_BASE_RM_PORTRAIT_NORMAL;
+               break;
+       case AUTO_ROTATION_DEGREE_90:
+               rm = APPCORE_BASE_RM_LANDSCAPE_NORMAL;
+               break;
+       case AUTO_ROTATION_DEGREE_180:
+               rm = APPCORE_BASE_RM_PORTRAIT_REVERSE;
+               break;
+       case AUTO_ROTATION_DEGREE_270:
+               rm = APPCORE_BASE_RM_LANDSCAPE_REVERSE;
+               break;
+       default:
+               rm = APPCORE_BASE_RM_UNKNOWN;
+               break;
+       }
+
+       return rm;
+}
+
+static void __lock_cb(keynode_t *node, void *user_data)
+{
+       bool r;
+       sensor_data_t data;
+       enum appcore_base_rm rm;
+
+       __rotation.lock = !vconf_keynode_get_bool(node);
+       if (__rotation.lock) {
+               _DBG("Rotation locked");
+               rm = APPCORE_BASE_RM_PORTRAIT_NORMAL;
+       } else {
+               _DBG("Rotation unlocked");
+               r = sensord_get_data(__rotation.conn, AUTO_ROTATION_SENSOR, &data);
+               if (!r) {
+                       _ERR("Failed to get sensor data");
+                       return;
+               }
+
+               rm = __get_rm(data);
+               if (rm == APPCORE_BASE_RM_UNKNOWN) {
+                       _ERR("Unknown mode");
+                       return;
+               }
+       }
+
+       if (__rotation.rm == rm)
+               return;
+
+       _DBG("Rotation: %d -> %d", __rotation.rm, rm);
+       __rotation.rm = rm;
+       __invoke_callback((void *)&__rotation.rm, APPCORE_BASE_EVENT_DEVICE_ORIENTATION_CHANGED);
+}
+
+static void __auto_rotation_changed_cb(sensor_t sensor, unsigned int event_type,
+               sensor_data_t *data, void *user_data)
+{
+       enum appcore_base_rm rm;
+
+       if (data == NULL)
+               return;
+
+       if (__rotation.lock)
+               return;
+
+       if (event_type != AUTO_ROTATION_CHANGE_STATE_EVENT)
+               return;
+
+       rm = __get_rm(*data);
+       if (rm == APPCORE_BASE_RM_UNKNOWN) {
+               _ERR("Unknown mode");
+               return;
+       }
+
+       _DBG("Rotation: %d -> %d", __rotation.rm, rm);
+       __rotation.rm = rm;
+       __invoke_callback((void *)&__rotation.rm, APPCORE_BASE_EVENT_DEVICE_ORIENTATION_CHANGED);
+}
+
+static void __unregister_rotation_changed_event(void)
+{
+       if (!__rotation.ref)
+               return;
+
+       __rotation.ref--;
+       if (__rotation.ref > 1)
+               return;
+
+       vconf_ignore_key_changed(VCONFKEY_SETAPPL_AUTO_ROTATE_SCREEN_BOOL, __lock_cb);
+       sensord_unregister_event(__rotation.conn, AUTO_ROTATION_CHANGE_STATE_EVENT);
+       sensord_stop(__rotation.conn);
+       sensord_disconnect(__rotation.conn);
+
+       __rotation.lock = 0;
+       __rotation.ref = 0;
+}
+
+static void __register_rotation_changed_event(void)
+{
+       sensor_t sensor;
+       int lock;
+       bool r;
+
+       if (__rotation.ref) {
+               __rotation.ref++;
+               return;
+       }
+
+       sensor = sensord_get_sensor(AUTO_ROTATION_SENSOR);
+       __rotation.conn = sensord_connect(sensor);
+       if (__rotation.conn < 0) {
+               _ERR("Failed to connect sensord");
+               return;
+       }
+
+       r = sensord_register_event(__rotation.conn, AUTO_ROTATION_CHANGE_STATE_EVENT,
+                       SENSOR_INTERVAL_NORMAL, 0, __auto_rotation_changed_cb, NULL);
+       if (!r) {
+               _ERR("Failed to register auto rotation change event");
+               sensord_disconnect(__rotation.conn);
+               return;
+       }
+
+       r = sensord_start(__rotation.conn, 0);
+       if (!r) {
+               _ERR("Failed to start sensord");
+               sensord_unregister_event(__rotation.conn, AUTO_ROTATION_CHANGE_STATE_EVENT);
+               sensord_disconnect(__rotation.conn);
+               return;
+       }
+
+       lock = 0;
+       vconf_get_bool(VCONFKEY_SETAPPL_AUTO_ROTATE_SCREEN_BOOL, &lock);
+       vconf_notify_key_changed(VCONFKEY_SETAPPL_AUTO_ROTATE_SCREEN_BOOL, __lock_cb, NULL);
+
+       __rotation.lock = !lock;
+       __rotation.ref++;
+}
+
 static void __on_low_memory(keynode_t *key, void *data)
 {
        int val;
@@ -893,6 +1052,9 @@ EXPORT_API void appcore_base_on_set_event(enum appcore_base_event event)
        case APPCORE_BASE_EVENT_LANG_CHANGE:
                vconf_notify_key_changed(VCONFKEY_LANGSET, __on_language_change, NULL);
                break;
+       case APPCORE_BASE_EVENT_DEVICE_ORIENTATION_CHANGED:
+               __register_rotation_changed_event();
+               break;
        case APPCORE_BASE_EVENT_REGION_CHANGE:
                r = vconf_notify_key_changed(VCONFKEY_REGIONFORMAT, __on_region_change, NULL);
                if (r < 0)
@@ -923,6 +1085,9 @@ EXPORT_API void appcore_base_on_unset_event(enum appcore_base_event event)
        case APPCORE_BASE_EVENT_LANG_CHANGE:
                vconf_ignore_key_changed(VCONFKEY_LANGSET, __on_language_change);
                break;
+       case APPCORE_BASE_EVENT_DEVICE_ORIENTATION_CHANGED:
+               __unregister_rotation_changed_event();
+               break;
        case APPCORE_BASE_EVENT_REGION_CHANGE:
                r = vconf_ignore_key_changed(VCONFKEY_REGIONFORMAT, __on_region_change);
                if (r < 0)