display: Add rotation state internal API sandbox/cw00.choi/display-rotation
authorTaeminYeom <taemin.yeom@samsung.com>
Tue, 11 Oct 2022 01:14:47 +0000 (10:14 +0900)
committerChanwoo Choi <cw00.choi@samsung.com>
Mon, 19 Dec 2022 06:00:09 +0000 (15:00 +0900)
rotation state means "physical display" direction.
Clients can rotate display or check current display state.

API function
-device_display_get_rotation_state
-device_display_set_rotation_state

Enumeration
-display_rotation_state_e
-display_rotation_direction_e

Change-Id: Iae840b217977e2540883fec31f5e2631aa93b967
Signed-off-by: TaeminYeom <taemin.yeom@samsung.com>
CMakeLists.txt
include/display-internal.h
packaging/capi-system-device.spec
src/display.c

index b10ff46fbf007132e5d1698b8fe37bf15c15e12d..0631b8d62619c5a99028a8c177f8bb6199f43866 100644 (file)
@@ -15,6 +15,8 @@ SET(PKG_MODULES
                vconf
                capi-base-common
                capi-system-info
+               capi-system-sensor
+               sensor
                gio-2.0
                tracker
                libsyscommon
index f80e58e0b49e68226931f9c686204483e223da7c..6cb146094999429eecbdb0a59eb050dd7e585c63 100644 (file)
 extern "C" {
 #endif
 
+/**
+ * @brief Enumeration for the available display rotation states.
+ * @since_tizen 7.0
+ */
+
+typedef enum
+{
+       DISPLAY_ROTATION_STATE_UNKNOWN = -1,
+       DISPLAY_ROTATION_STATE_PORTRAIT,
+       DISPLAY_ROTATION_STATE_LANDSCAPE,
+       DISPLAY_ROTATION_STATE_PORTRAIT_R,
+       DISPLAY_ROTATION_STATE_LANDSCAPE_R,
+} display_rotation_state_e;
+
+
+/**
+ * @brief Enumeration for the display rotation directions.
+ * @since_tizen 7.0
+ */
+
+typedef enum
+{
+       DISPLAY_ROTATION_CLOCKWISE,
+       DISPLAY_ROTATION_COUNTER_CLOCKWISE,
+} display_rotation_direction_e;
+
+
 /**
  * @brief Gets the display brightness value.
  * @since_tizen @if MOBILE 5.0 @elseif WEARABLE 5.0 @endif
@@ -128,6 +155,55 @@ int device_display_change_state_by_reason(display_state_e type, const char *reas
  */
 int is_feature_display_supported(void);
 
+
+/**
+ * @brief Get display rotation state
+ * @since_tizen 7.0
+ * @privilege %http://tizen.org/privilege/display
+ * @remarks It shows the physical display state, not SW screen state.
+ * @param[in] display_index The index of the display \n
+ *                          It can be greater than or equal to @c 0 and less than the number of displays returned by device_display_get_numbers(). \n
+ *                          The index zero is always assigned to the main display
+ * @param[out] rotation The type is display rotation state\n
+ *                 DISPLAY_ROTATION_STATE_UNKNOWN\n
+ *                 DISPLAY_ROTATION_STATE_PORTRAIT\n
+ *                 DISPLAY_ROTATION_STATE_LANDSCAPE\n
+ *                 DISPLAY_ROTATION_STATE_PORTRAIT_R\n
+ *                 DISPLAY_ROTATION_STATE_LANDSCAPE_R\n
+ * @return @c 0 on success,
+ *         otherwise a negative error value
+ * @retval #DEVICE_ERROR_NONE Successful
+ * @retval #DEVICE_ERROR_INVALID_PARAMETER Invalid parameter
+ * @retval #DEVICE_ERROR_PERMISSION_DENIED Permission denied
+ * @retval #DEVICE_ERROR_OPERATION_FAILED Operation failed
+ */
+int device_display_get_rotation_state(int display_index, display_rotation_state_e *rotation);
+
+/**
+ * @brief Set display rotation state
+ * @since_tizen 7.0
+ * @privilege %http://tizen.org/privilege/display
+ * @remarks It shows the physical display state, not SW screen state.
+ * @param[in] display_index The index of the display \n
+ *                          It can be greater than or equal to @c 0 and less than the number of displays returned by device_display_get_numbers(). \n
+ *                          The index zero is always assigned to the main display
+ * @param[in] rotation The type is display rotation state\n
+ *                 DISPLAY_ROTATION_STATE_PORTRAIT\n
+ *                 DISPLAY_ROTATION_STATE_LANDSCAPE\n
+ *                 DISPLAY_ROTATION_STATE_PORTRAIT_R\n
+ *                 DISPLAY_ROTATION_STATE_LANDSCAPE_R\n
+ * @param[in] direction The type is display rotation direction\n
+ *                    DEVICE_DISPLAY_ROTATION_CLOCKWISE\n
+ *                    DEVICE_DISPLAY_ROTATION_COUNTER_CLOCKWISE\n
+ * @return @c 0 on success,
+ *         otherwise a negative error value
+ * @retval #DEVICE_ERROR_NONE Successful
+ * @retval #DEVICE_ERROR_INVALID_PARAMETER Invalid parameter
+ * @retval #DEVICE_ERROR_PERMISSION_DENIED Permission denied
+ * @retval #DEVICE_ERROR_OPERATION_FAILED Operation failed
+ */
+int device_display_set_rotation_state(int display_index, display_rotation_state_e rotation, display_rotation_direction_e direction);
+
 /**
  * @platform
  * @brief Check display state feature(http://tizen.org/feature/display.state)
index f6a904e692f89bac8de58adecf2ab03cba3c48b3..3711d7a7d9abd86c55014ca77c9a00737a919398 100644 (file)
@@ -9,6 +9,8 @@ Source1:    capi-system-device.manifest
 BuildRequires:  cmake
 BuildRequires:  pkgconfig(capi-base-common)
 BuildRequires:  pkgconfig(capi-system-info)
+BuildRequires:  pkgconfig(capi-system-sensor)
+BuildRequires:  pkgconfig(sensor)
 BuildRequires:  pkgconfig(dlog)
 BuildRequires:  pkgconfig(vconf)
 BuildRequires:  pkgconfig(gio-2.0)
index e8e25c89e7949f2114f0d2761f809849d26ef21d..c9301524fabf2f97208632ed819c96fd19b70161 100644 (file)
 
 #include <stdio.h>
 #include <errno.h>
+
 #include <vconf.h>
 #include <libsyscommon/libgdbus.h>
 #include <system_info.h>
+#include <sensor.h>
+#include <sensor_internal.h>
+#include <glib.h>
 
 #include "display.h"
 #include "display-internal.h"
@@ -34,6 +38,8 @@
 #define METHOD_SET_BRIGHTNESS           "SetBrightness"
 #define METHOD_CHANGE_STATE             "changestate"
 #define METHOD_CHANGE_STATE_BY_REASON   "ChangeStateByReason"
+#define METHOD_GET_ROTATION_STATE       "GetRotationState"
+#define METHOD_SET_ROTATION_STATE       "SetRotationState"
 
 #define STR_LCD_OFF   "lcdoff"
 #define STR_LCD_DIM   "lcddim"
@@ -45,6 +51,9 @@ struct display {
        int dim_max;
 } *display_arr;
 
+static bool sensor_inited;
+static sensor_listener_h listener_handle;
+
 static int alloc_display(void)
 {
        int i;
@@ -468,6 +477,170 @@ int is_feature_display_supported(void)
                return true;
 }
 
+static int start_sensor(void)
+{
+       sensor_h *sensor_handle = NULL;
+       int count, ret;
+       bool supported = false;
+
+       if (!sensor_inited) {
+               sensor_is_supported(AUTO_ROTATION_SENSOR, &supported);
+               if (!supported) {
+                       _E("Auto rotation sensor is not supported in this device");
+                       return DEVICE_ERROR_OPERATION_FAILED;
+               }
+
+               ret = sensor_get_sensor_list(AUTO_ROTATION_SENSOR, &sensor_handle, &count);
+               if (ret < 0 || !sensor_handle || count <= 0) {
+                       _E("Failed to get sensor list");
+                       return DEVICE_ERROR_OPERATION_FAILED;
+               }
+
+               ret = sensor_create_listener(sensor_handle[0], &listener_handle);
+               if (ret < 0) {
+                       _E("Failed to create sensor listener");
+                       free(sensor_handle);
+                       return DEVICE_ERROR_OPERATION_FAILED;
+               }
+
+               free(sensor_handle);
+               sensor_inited = true;
+       }
+
+       ret = sensor_listener_start(listener_handle);
+       if (ret < 0) {
+               _E("Failed to start auto-rotation sensor");
+               return DEVICE_ERROR_OPERATION_FAILED;
+       }
+
+       return DEVICE_ERROR_NONE;
+}
+
+static int stop_sensor(void)
+{
+       int ret;
+       ret = sensor_listener_stop(listener_handle);
+       if (ret < 0)
+               return ret;
+
+       return DEVICE_ERROR_NONE;
+}
+
+static int sensor_get_rotation_state(display_rotation_state_e *rotation)
+{
+       int ret, count, event;
+       sensor_event_s *events = NULL;
+
+       ret = start_sensor();
+       if (ret < 0)
+               return ret;
+
+       ret = sensor_listener_read_data_list(listener_handle, &events, &count);
+       if (ret < 0 || count <= 0) {
+               _E("Failed to read the value of auto-rotation sensor");
+               stop_sensor();
+               return DEVICE_ERROR_OPERATION_FAILED;
+       }
+
+       event = (int)events->values[0];
+       switch (event) {
+       case AUTO_ROTATION_DEGREE_0:
+               *rotation = DISPLAY_ROTATION_STATE_PORTRAIT;
+               break;
+       case AUTO_ROTATION_DEGREE_90:
+               *rotation = DISPLAY_ROTATION_STATE_LANDSCAPE;
+               break;
+       case AUTO_ROTATION_DEGREE_180:
+               *rotation = DISPLAY_ROTATION_STATE_PORTRAIT_R;
+               break;
+       case AUTO_ROTATION_DEGREE_270:
+               *rotation = DISPLAY_ROTATION_STATE_LANDSCAPE_R;
+               break;
+       default:
+               *rotation = DISPLAY_ROTATION_STATE_UNKNOWN;
+               break;
+       }
+
+       free(events);
+       return stop_sensor();
+}
+
+int device_display_get_rotation_state(int display_index, display_rotation_state_e *rotation)
+{
+       int ret = 0, reply_val = 0;
+       GVariant *reply;
+
+       if (!rotation)
+               return DEVICE_ERROR_INVALID_PARAMETER;
+
+       /*
+       if (display_cnt < 0) {
+               ret = device_display_get_numbers(&display_cnt);
+               if (ret < 0)
+                       return ret;
+       }
+
+       if (display_index < 0 || display_index >= display_cnt || !rotation)
+               return DEVICE_ERROR_INVALID_PARAMETER;
+       */
+
+       ret = gdbus_call_sync_with_reply(DEVICED_BUS_NAME,
+                                                       DEVICED_PATH_DISPLAY, DEVICED_INTERFACE_DISPLAY,
+                                                       METHOD_GET_ROTATION_STATE, g_variant_new("(i)", display_index), &reply);
+       if (ret < 0) {
+               _E("Failed to call dbus method");
+               return ret;
+       }
+
+       g_variant_get(reply, "(ii)", &ret, &reply_val);
+       g_variant_unref(reply);
+       if (ret < 0 && ret != -ENODEV) {
+               _E("Failed to get display rotation state");
+               return ret;
+       }
+       else if (ret >= 0) {
+               *rotation = reply_val;
+               return DEVICE_ERROR_NONE;
+       }
+
+       return sensor_get_rotation_state(rotation);
+}
+
+int device_display_set_rotation_state(int display_index, display_rotation_state_e rotation, display_rotation_direction_e direction)
+{
+       int ret, reply;
+
+       /*
+       if (display_cnt < 0) {
+               ret = device_display_get_numbers(&display_cnt);
+               if (ret < 0)
+                       return ret;
+       }
+
+       if (display_index < 0 || display_index >= display_cnt)
+               return DEVICE_ERROR_INVALID_PARAMETER;
+       */
+
+       ret = gdbus_call_sync_with_reply_int(DEVICED_BUS_NAME,
+                                                       DEVICED_PATH_DISPLAY, DEVICED_INTERFACE_DISPLAY,
+                                                       METHOD_SET_ROTATION_STATE, g_variant_new("(iii)", display_index, rotation, direction), &reply);
+       if (ret < 0) {
+               _E("Failed to call dbus method");
+               return ret;
+       }
+
+       if (reply == -ENODEV) {
+               _E("Set display rotation is not supported");
+               return DEVICE_ERROR_OPERATION_FAILED;
+       }
+       else if (ret < 0) {
+               _E("Failed to set display rotation state");
+               return DEVICE_ERROR_OPERATION_FAILED;
+       }
+
+       return DEVICE_ERROR_NONE;
+}
+
 int is_feature_display_state_supported(void)
 {
        int ret_val;