2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
24 #include <libsyscommon/libgdbus.h>
25 #include <libsyscommon/list.h>
26 #include <system_info.h>
31 #include "display-internal.h"
34 #define SIGNAL_FLASH_STATE "ChangeFlashState"
36 struct device_cb_info {
42 #define DEVICE_CALLBACK_KEY_INPUT_SIZE (DEVICE_CALLBACK_KEY_INPUT_MAX - DEVICE_CALLBACK_KEY_INPUT_MIN)
44 static GList *device_cb_list[DEVICE_CALLBACK_MAX];
45 static GList *device_key_cb_list;
46 static int flash_sigid;
49 //LCOV_EXCL_START Not called Callback
50 static void battery_capacity_cb(keynode_t *key, void *data)
52 static device_callback_e type = DEVICE_CALLBACK_BATTERY_CAPACITY;
53 struct device_cb_info *cb_info;
54 GList *elem, *elem_next;
57 val = vconf_keynode_get_int(key);
59 /* invoke the each callback with value */
60 SYS_G_LIST_FOREACH_SAFE(device_cb_list[type], elem, elem_next, cb_info)
61 cb_info->cb(type, (void*)(intptr_t)val, cb_info->data);
65 //LCOV_EXCL_START Not called Callback
66 static void battery_charging_cb(keynode_t *key, void *data)
68 static device_callback_e type = DEVICE_CALLBACK_BATTERY_CHARGING;
69 struct device_cb_info *cb_info;
70 GList *elem, *elem_next;
73 val = vconf_keynode_get_int(key);
75 /* invoke the each callback with value */
76 SYS_G_LIST_FOREACH_SAFE(device_cb_list[type], elem, elem_next, cb_info)
77 cb_info->cb(type, (void*)(intptr_t)val, cb_info->data);
81 //LCOV_EXCL_START Not called Callback
82 static void battery_level_cb(keynode_t *key, void *data)
84 static device_callback_e type = DEVICE_CALLBACK_BATTERY_LEVEL;
85 struct device_cb_info *cb_info;
86 GList *elem, *elem_next;
89 val = vconf_keynode_get_int(key);
91 if (val == VCONFKEY_SYSMAN_BAT_LEVEL_EMPTY)
92 status = DEVICE_BATTERY_LEVEL_EMPTY;
93 else if (val == VCONFKEY_SYSMAN_BAT_LEVEL_CRITICAL)
94 status = DEVICE_BATTERY_LEVEL_CRITICAL;
95 else if (val == VCONFKEY_SYSMAN_BAT_LEVEL_LOW)
96 status = DEVICE_BATTERY_LEVEL_LOW;
97 else if (val == VCONFKEY_SYSMAN_BAT_LEVEL_HIGH)
98 status = DEVICE_BATTERY_LEVEL_HIGH;
99 else if (val == VCONFKEY_SYSMAN_BAT_LEVEL_FULL)
100 status = DEVICE_BATTERY_LEVEL_FULL;
104 /* invoke the each callback with value */
105 SYS_G_LIST_FOREACH_SAFE(device_cb_list[type], elem, elem_next, cb_info)
106 cb_info->cb(type, (void*)(intptr_t)status, cb_info->data);
110 //LCOV_EXCL_START Not called Callback
111 static void display_changed_cb(keynode_t *key, void *data)
113 static device_callback_e type = DEVICE_CALLBACK_DISPLAY_STATE;
114 struct device_cb_info *cb_info;
115 GList *elem, *elem_next;
116 display_state_e state;
119 val = vconf_keynode_get_int(key);
122 case 1: state = DISPLAY_STATE_NORMAL;
124 case 2: state = DISPLAY_STATE_SCREEN_DIM;
126 case 3: state = DISPLAY_STATE_SCREEN_OFF;
132 /* invoke the each callback with value */
133 SYS_G_LIST_FOREACH_SAFE(device_cb_list[type], elem, elem_next, cb_info)
134 cb_info->cb(type, (void*)state, cb_info->data);
138 //LCOV_EXCL_START Not called Callback
139 static void flash_state_cb(GDBusConnection *conn,
142 const gchar *interface,
144 GVariant *parameters,
147 static int type = DEVICE_CALLBACK_FLASH_BRIGHTNESS;
148 struct device_cb_info *cb_info;
149 GList *elem, *elem_next;
152 if (strncmp(signal, SIGNAL_FLASH_STATE,
153 sizeof(SIGNAL_FLASH_STATE)) != 0) {
154 _E("wrong parameter : signal(%s)", signal);
158 /* get camera value */
159 g_variant_get(parameters, "(i)", &val);
160 _D("%s - %d", signal, val);
162 /* invoke the each callback with value */
163 SYS_G_LIST_FOREACH_SAFE(device_cb_list[type], elem, elem_next, cb_info)
164 cb_info->cb(type, (void*)(intptr_t)val, cb_info->data);
168 //LCOV_EXCL_START Target specific callback
169 static void key_input_cb(GDBusConnection *conn,
172 const gchar *interface,
174 GVariant *parameters,
177 struct device_cb_info *cb_info;
178 GList *elem, *elem_next;
181 g_variant_get(parameters, "(i)", &type);
182 if (type < DEVICE_CALLBACK_KEY_INPUT_MIN || type >= DEVICE_CALLBACK_KEY_INPUT_MAX)
185 SYS_G_LIST_FOREACH_SAFE(device_key_cb_list, elem, elem_next, cb_info) {
186 if (cb_info->type == type)
187 cb_info->cb(type, NULL, cb_info->data);
192 static int register_signal(const char *bus_name,
193 const char *object_path,
194 const char *interface_name,
196 GDBusSignalCallback callback,
200 GDBusConnection *conn;
203 #if !GLIB_CHECK_VERSION(2, 35, 0)
207 conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &err);
209 //LCOV_EXCL_START System Error
210 _E("fail to get dbus connection : %s", err->message);
216 /* subscribe signal */
217 id = g_dbus_connection_signal_subscribe(conn,
220 signal, /* null to match on all signals */
222 NULL, /* null to match on all kinds of arguments */
223 G_DBUS_SIGNAL_FLAGS_NONE,
228 //LCOV_EXCL_START System Error
229 _E("fail to connect %s signal", signal);
240 static int unregister_signal(int *sig_id)
243 GDBusConnection *conn;
248 conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &err);
250 //LCOV_EXCL_START System Error
251 _E("fail to get dbus connection : %s", err->message);
257 /* unsubscribe signal */
258 g_dbus_connection_signal_unsubscribe(conn, *sig_id);
264 static int register_request(int type)
267 case DEVICE_CALLBACK_BATTERY_CAPACITY:
268 return vconf_notify_key_changed(VCONFKEY_SYSMAN_BATTERY_CAPACITY,
269 battery_capacity_cb, NULL);
270 case DEVICE_CALLBACK_BATTERY_LEVEL:
271 return vconf_notify_key_changed(VCONFKEY_SYSMAN_BATTERY_LEVEL_STATUS,
272 battery_level_cb, NULL);
273 case DEVICE_CALLBACK_BATTERY_CHARGING:
274 return vconf_notify_key_changed(VCONFKEY_SYSMAN_BATTERY_CHARGE_NOW,
275 battery_charging_cb, NULL);
276 case DEVICE_CALLBACK_DISPLAY_STATE:
277 return vconf_notify_key_changed(VCONFKEY_PM_STATE,
278 display_changed_cb, NULL);
279 case DEVICE_CALLBACK_FLASH_BRIGHTNESS:
280 /* sig_id begins with 1. */
283 return register_signal(DEVICED_BUS_NAME,
285 DEVICED_INTERFACE_LED,
286 SIGNAL_FLASH_STATE, flash_state_cb, &flash_sigid);
288 if (type >= DEVICE_CALLBACK_KEY_INPUT_MIN && type < DEVICE_CALLBACK_KEY_INPUT_MAX) {
291 return register_signal(DEVICED_BUS_NAME,
293 DEVICED_INTERFACE_INPUT,
294 "Key", key_input_cb, &key_sigid);
302 static int release_request(int type)
305 case DEVICE_CALLBACK_BATTERY_CAPACITY:
306 return vconf_ignore_key_changed(VCONFKEY_SYSMAN_BATTERY_CAPACITY,
307 battery_capacity_cb);
308 case DEVICE_CALLBACK_BATTERY_LEVEL:
309 return vconf_ignore_key_changed(VCONFKEY_SYSMAN_BATTERY_LEVEL_STATUS,
311 case DEVICE_CALLBACK_BATTERY_CHARGING:
312 return vconf_ignore_key_changed(VCONFKEY_SYSMAN_BATTERY_CHARGE_NOW,
313 battery_charging_cb);
314 case DEVICE_CALLBACK_DISPLAY_STATE:
315 return vconf_ignore_key_changed(VCONFKEY_PM_STATE,
317 case DEVICE_CALLBACK_FLASH_BRIGHTNESS:
320 return unregister_signal(&flash_sigid);
322 if (type >= DEVICE_CALLBACK_KEY_INPUT_MIN && type < DEVICE_CALLBACK_KEY_INPUT_MAX) {
325 return unregister_signal(&key_sigid);
333 static int device_callback_check_feature_supported(int type)
335 static bool f_display = false;
336 static bool f_battery = false;
337 static bool f_flash = false;
343 case DEVICE_CALLBACK_BATTERY_CAPACITY:
344 case DEVICE_CALLBACK_BATTERY_LEVEL:
345 case DEVICE_CALLBACK_BATTERY_CHARGING:
349 retval = system_info_get_platform_bool("http://tizen.org/feature/battery", &supported);
350 if (retval != SYSTEM_INFO_ERROR_NONE || !supported)
355 case DEVICE_CALLBACK_DISPLAY_STATE:
359 retval = system_info_get_platform_bool("http://tizen.org/feature/display", &supported);
360 if (retval != SYSTEM_INFO_ERROR_NONE || !supported)
362 retval = system_info_get_platform_bool("http://tizen.org/feature/display.state", &supported);
363 if (retval != SYSTEM_INFO_ERROR_NONE || !supported)
368 case DEVICE_CALLBACK_FLASH_BRIGHTNESS:
372 retval = system_info_get_platform_bool("http://tizen.org/feature/camera.back.flash", &supported);
373 if (retval != SYSTEM_INFO_ERROR_NONE || !supported)
383 int device_add_callback(device_callback_e enumtype, device_changed_cb cb, void *data)
385 struct device_cb_info *cb_info;
386 GList *elem, *elem_next;
389 int type = (int) enumtype;
391 if ((type < 0 || type >= DEVICE_CALLBACK_MAX)
392 && (type < DEVICE_CALLBACK_KEY_INPUT_MIN || type >= DEVICE_CALLBACK_KEY_INPUT_MAX))
393 return DEVICE_ERROR_INVALID_PARAMETER;
396 return DEVICE_ERROR_INVALID_PARAMETER;
398 if (device_callback_check_feature_supported(type) != 0)
399 return DEVICE_ERROR_NOT_SUPPORTED;
401 if (type >= 0 && type < DEVICE_CALLBACK_MAX)
402 cb_list = &device_cb_list[type];
403 else if (type >= DEVICE_CALLBACK_KEY_INPUT_MIN && type < DEVICE_CALLBACK_KEY_INPUT_MAX)
404 cb_list = &device_key_cb_list;
406 return DEVICE_ERROR_INVALID_PARAMETER;
408 /* check if it is the first request */
409 n = SYS_G_LIST_LENGTH(*cb_list);
411 ret_request = register_request(type);
413 return DEVICE_ERROR_OPERATION_FAILED;
416 /* check for the same request */
417 SYS_G_LIST_FOREACH_SAFE(*cb_list, elem, elem_next, cb_info) {
418 if (cb_info->cb == cb)
419 return DEVICE_ERROR_ALREADY_IN_PROGRESS;
422 /* add device changed callback to list (local) */
423 cb_info = malloc(sizeof(struct device_cb_info));
425 return DEVICE_ERROR_OPERATION_FAILED;
428 cb_info->data = data;
429 cb_info->type = type;
431 SYS_G_LIST_APPEND(*cb_list, cb_info);
433 return DEVICE_ERROR_NONE;
436 int device_remove_callback(device_callback_e enumtype, device_changed_cb cb)
438 struct device_cb_info *cb_info;
439 GList *elem, *elem_next;
442 int type = (int) enumtype;
444 if ((type < 0 || type >= DEVICE_CALLBACK_MAX)
445 && (type < DEVICE_CALLBACK_KEY_INPUT_MIN || type >= DEVICE_CALLBACK_KEY_INPUT_MAX))
446 return DEVICE_ERROR_INVALID_PARAMETER;
449 return DEVICE_ERROR_INVALID_PARAMETER;
451 if (device_callback_check_feature_supported(type) != 0)
452 return DEVICE_ERROR_NOT_SUPPORTED;
454 if (type >= 0 && type < DEVICE_CALLBACK_MAX)
455 cb_list = &device_cb_list[type];
456 else if (type >= DEVICE_CALLBACK_KEY_INPUT_MIN && type < DEVICE_CALLBACK_KEY_INPUT_MAX)
457 cb_list = &device_key_cb_list;
459 return DEVICE_ERROR_INVALID_PARAMETER;
461 /* search for the same element with callback */
462 SYS_G_LIST_FOREACH_SAFE(*cb_list, elem, elem_next, cb_info) {
463 if (cb_info->cb == cb)
468 return DEVICE_ERROR_INVALID_PARAMETER;
470 /* remove device callback from list (local) */
471 SYS_G_LIST_REMOVE(*cb_list, cb_info);
474 /* check if this callback is last element */
475 n = SYS_G_LIST_LENGTH(*cb_list);
477 ret_request = release_request(type);
479 return DEVICE_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE System Error
482 return DEVICE_ERROR_NONE;