From: Youngjae Cho Date: Fri, 10 Dec 2021 03:58:38 +0000 (+0900) Subject: callback: add callback for key input signal X-Git-Tag: accepted/tizen/unified/20211213.133455^0 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=refs%2Fchanges%2F07%2F267807%2F8;p=platform%2Fcore%2Fapi%2Fdevice.git callback: add callback for key input signal Key callback is exclusive for a specific target. Therefore, manages regarding key enums using device_callback_internal_e, which ranges from 1000 to 1100. Change-Id: I1f7b60e48407d379c6cdf1af4d18e14b1b9f7155 Signed-off-by: Youngjae Cho --- diff --git a/include/callback.h b/include/callback.h index 977a36c..fad0d72 100644 --- a/include/callback.h +++ b/include/callback.h @@ -44,9 +44,14 @@ typedef enum { DEVICE_CALLBACK_BATTERY_CHARGING, /**< Called when battery charging state is changed */ DEVICE_CALLBACK_DISPLAY_STATE, /**< Called when a display state is changed */ DEVICE_CALLBACK_FLASH_BRIGHTNESS, /**< Called when a flash brightness is changed (Since Tizen @if Mobile 2.4 @elseif WEARABLE 3.0 @endif) */ - DEVICE_CALLBACK_MAX + DEVICE_CALLBACK_MAX, } device_callback_e; +typedef enum { + DEVICE_CALLBACK_KEY_INPUT_MIN = 1000, + DEVICE_CALLBACK_KEY_INPUT_MAX = 1100, +} device_callback_internal_e; + /** * @brief Called when a device status is changed. diff --git a/src/callback.c b/src/callback.c index 76c43d4..fd28ea6 100644 --- a/src/callback.c +++ b/src/callback.c @@ -36,10 +36,15 @@ struct device_cb_info { device_changed_cb cb; void *data; + int type; }; +#define DEVICE_CALLBACK_KEY_INPUT_SIZE (DEVICE_CALLBACK_KEY_INPUT_MAX - DEVICE_CALLBACK_KEY_INPUT_MIN) + static GList *device_cb_list[DEVICE_CALLBACK_MAX]; +static GList *device_key_cb_list; static int flash_sigid; +static int key_sigid; //LCOV_EXCL_START Not called Callback static void battery_capacity_cb(keynode_t *key, void *data) @@ -160,6 +165,30 @@ static void flash_state_cb(GDBusConnection *conn, } //LCOV_EXCL_STOP +//LCOV_EXCL_START Target specific callback +static void key_input_cb(GDBusConnection *conn, + const gchar *sender, + const gchar *object, + const gchar *interface, + const gchar *signal, + GVariant *parameters, + gpointer user_data) +{ + struct device_cb_info *cb_info; + GList *elem, *elem_next; + int type = 0; + + g_variant_get(parameters, "(i)", &type); + if (type < DEVICE_CALLBACK_KEY_INPUT_MIN || type >= DEVICE_CALLBACK_KEY_INPUT_MAX) + return; + + SYS_G_LIST_FOREACH_SAFE(device_key_cb_list, elem, elem_next, cb_info) { + if (cb_info->type == type) + cb_info->cb(type, NULL, cb_info->data); + } +} +//LCOV_EXCL_STOP + static int register_signal(const char *bus_name, const char *object_path, const char *interface_name, @@ -232,7 +261,7 @@ static int unregister_signal(int *sig_id) return 0; } -static int register_request(device_callback_e type) +static int register_request(int type) { switch (type) { case DEVICE_CALLBACK_BATTERY_CAPACITY: @@ -256,13 +285,21 @@ static int register_request(device_callback_e type) DEVICED_INTERFACE_LED, SIGNAL_FLASH_STATE, flash_state_cb, &flash_sigid); default: + if (type >= DEVICE_CALLBACK_KEY_INPUT_MIN && type < DEVICE_CALLBACK_KEY_INPUT_MAX) { + if (key_sigid) + return -EEXIST; + return register_signal(DEVICED_BUS_NAME, + DEVICED_PATH_INPUT, + DEVICED_INTERFACE_INPUT, + "Key", key_input_cb, &key_sigid); + } break; } return -EINVAL; } -static int release_request(device_callback_e type) +static int release_request(int type) { switch (type) { case DEVICE_CALLBACK_BATTERY_CAPACITY: @@ -282,13 +319,18 @@ static int release_request(device_callback_e type) return -ENOENT; return unregister_signal(&flash_sigid); default: + if (type >= DEVICE_CALLBACK_KEY_INPUT_MIN && type < DEVICE_CALLBACK_KEY_INPUT_MAX) { + if (!key_sigid) + return -ENOENT; + return unregister_signal(&key_sigid); + } break; } return -EINVAL; } -static int device_callback_check_feature_supported(device_callback_e type) +static int device_callback_check_feature_supported(int type) { static bool f_display = false; static bool f_battery = false; @@ -334,17 +376,20 @@ static int device_callback_check_feature_supported(device_callback_e type) f_flash = true; return 0; default: - return -EINVAL; + return 0; } } -int device_add_callback(device_callback_e type, device_changed_cb cb, void *data) +int device_add_callback(device_callback_e enumtype, device_changed_cb cb, void *data) { struct device_cb_info *cb_info; GList *elem, *elem_next; + GList **cb_list; int ret_request, n; + int type = (int) enumtype; - if (type < 0 || type >= DEVICE_CALLBACK_MAX) + if ((type < 0 || type >= DEVICE_CALLBACK_MAX) + && (type < DEVICE_CALLBACK_KEY_INPUT_MIN || type >= DEVICE_CALLBACK_KEY_INPUT_MAX)) return DEVICE_ERROR_INVALID_PARAMETER; if (!cb) @@ -353,8 +398,15 @@ int device_add_callback(device_callback_e type, device_changed_cb cb, void *data if (device_callback_check_feature_supported(type) != 0) return DEVICE_ERROR_NOT_SUPPORTED; + if (type >= 0 && type < DEVICE_CALLBACK_MAX) + cb_list = &device_cb_list[type]; + else if (type >= DEVICE_CALLBACK_KEY_INPUT_MIN && type < DEVICE_CALLBACK_KEY_INPUT_MAX) + cb_list = &device_key_cb_list; + else + return DEVICE_ERROR_INVALID_PARAMETER; + /* check if it is the first request */ - n = SYS_G_LIST_LENGTH(device_cb_list[type]); + n = SYS_G_LIST_LENGTH(*cb_list); if (n == 0) { ret_request = register_request(type); if (ret_request < 0) @@ -362,7 +414,7 @@ int device_add_callback(device_callback_e type, device_changed_cb cb, void *data } /* check for the same request */ - SYS_G_LIST_FOREACH_SAFE(device_cb_list[type], elem, elem_next, cb_info) { + SYS_G_LIST_FOREACH_SAFE(*cb_list, elem, elem_next, cb_info) { if (cb_info->cb == cb) return DEVICE_ERROR_ALREADY_IN_PROGRESS; } @@ -374,19 +426,23 @@ int device_add_callback(device_callback_e type, device_changed_cb cb, void *data cb_info->cb = cb; cb_info->data = data; + cb_info->type = type; - SYS_G_LIST_APPEND(device_cb_list[type], cb_info); + SYS_G_LIST_APPEND(*cb_list, cb_info); return DEVICE_ERROR_NONE; } -int device_remove_callback(device_callback_e type, device_changed_cb cb) +int device_remove_callback(device_callback_e enumtype, device_changed_cb cb) { struct device_cb_info *cb_info; GList *elem, *elem_next; + GList **cb_list; int ret_request, n; + int type = (int) enumtype; - if (type < 0 || type >= DEVICE_CALLBACK_MAX) + if ((type < 0 || type >= DEVICE_CALLBACK_MAX) + && (type < DEVICE_CALLBACK_KEY_INPUT_MIN || type >= DEVICE_CALLBACK_KEY_INPUT_MAX)) return DEVICE_ERROR_INVALID_PARAMETER; if (!cb) @@ -395,8 +451,15 @@ int device_remove_callback(device_callback_e type, device_changed_cb cb) if (device_callback_check_feature_supported(type) != 0) return DEVICE_ERROR_NOT_SUPPORTED; + if (type >= 0 && type < DEVICE_CALLBACK_MAX) + cb_list = &device_cb_list[type]; + else if (type >= DEVICE_CALLBACK_KEY_INPUT_MIN && type < DEVICE_CALLBACK_KEY_INPUT_MAX) + cb_list = &device_key_cb_list; + else + return DEVICE_ERROR_INVALID_PARAMETER; + /* search for the same element with callback */ - SYS_G_LIST_FOREACH_SAFE(device_cb_list[type], elem, elem_next, cb_info) { + SYS_G_LIST_FOREACH_SAFE(*cb_list, elem, elem_next, cb_info) { if (cb_info->cb == cb) break; } @@ -405,11 +468,11 @@ int device_remove_callback(device_callback_e type, device_changed_cb cb) return DEVICE_ERROR_INVALID_PARAMETER; /* remove device callback from list (local) */ - SYS_G_LIST_REMOVE(device_cb_list[type], cb_info); + SYS_G_LIST_REMOVE(*cb_list, cb_info); free(cb_info); /* check if this callback is last element */ - n = SYS_G_LIST_LENGTH(device_cb_list[type]); + n = SYS_G_LIST_LENGTH(*cb_list); if (n == 0) { ret_request = release_request(type); if (ret_request < 0)