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.
21 #include <libsyscommon/dbus-system.h>
23 #include "hal-display.h"
26 #define METHOD_GET_DISPLAY_COUNT "GetDisplayCount"
27 #define METHOD_GET_MAX_BRIGHTNESS "GetMaxBrightness"
28 #define METHOD_GET_BRIGHTNESS "GetBrightness"
29 #define METHOD_SET_BRIGHTNESS "SetBrightness"
30 #define METHOD_CHANGE_STATE "changestate"
31 #define METHOD_CHANGE_STATE_BY_REASON "ChangeStateByReason"
33 #define STR_LCD_OFF "lcdoff"
34 #define STR_LCD_DIM "lcddim"
35 #define STR_LCD_ON "lcdon"
37 static int display_cnt = -1;
43 static int alloc_display(void)
50 display_arr = malloc(sizeof(struct display) * display_cnt);
54 for (i = 0; i < display_cnt; ++i) {
55 display_arr[i].normal_max = -1;
56 display_arr[i].dim_max = -1;
62 int device_display_get_numbers(int *device_number)
66 ret = is_display_supported();
68 return DEVICE_ERROR_NOT_SUPPORTED;
71 return DEVICE_ERROR_INVALID_PARAMETER;
73 /* if it is a first request */
74 if (display_cnt < 0) {
75 ret = dbus_handle_method_sync_var(DEVICED_BUS_NAME,
76 DEVICED_PATH_DISPLAY, DEVICED_INTERFACE_DISPLAY,
77 METHOD_GET_DISPLAY_COUNT, NULL);
79 return errno_to_device_error(ret); //LCOV_EXCL_LINE System Error
84 *device_number = display_cnt;
85 _I("device_number : %d", *device_number);
86 return DEVICE_ERROR_NONE;
89 int device_display_get_max_brightness(int display_index, int *max_brightness)
93 ret = is_display_supported();
95 return DEVICE_ERROR_NOT_SUPPORTED;
98 return DEVICE_ERROR_INVALID_PARAMETER;
100 if (display_cnt < 0) {
101 ret = device_display_get_numbers(&display_cnt);
102 if (ret != DEVICE_ERROR_NONE) //LCOV_EXCL_LINE System Error
106 if (display_index < 0 || display_index >= display_cnt)
107 return DEVICE_ERROR_INVALID_PARAMETER;
109 if (!display_arr && alloc_display() < 0)
110 return DEVICE_ERROR_OPERATION_FAILED;
112 if (display_arr[display_index].normal_max < 0) {
113 ret = dbus_handle_method_sync_var(DEVICED_BUS_NAME,
114 DEVICED_PATH_DISPLAY, DEVICED_INTERFACE_DISPLAY,
115 METHOD_GET_MAX_BRIGHTNESS, g_variant_new("(i)", (int)DISPLAY_STATE_NORMAL));
117 return errno_to_device_error(ret); //LCOV_EXCL_LINE System Error
118 display_arr[display_index].normal_max = ret;
121 *max_brightness = display_arr[display_index].normal_max;
122 return DEVICE_ERROR_NONE;
125 int device_display_get_brightness(int display_index, int *brightness)
129 ret = is_display_supported();
131 return DEVICE_ERROR_NOT_SUPPORTED;
134 return DEVICE_ERROR_INVALID_PARAMETER;
136 if (display_cnt < 0) {
137 ret = device_display_get_numbers(&display_cnt);
138 if (ret != DEVICE_ERROR_NONE) //LCOV_EXCL_LINE System Error
142 if (display_index < 0 || display_index >= display_cnt)
143 return DEVICE_ERROR_INVALID_PARAMETER;
145 ret = dbus_handle_method_sync_var(DEVICED_BUS_NAME,
146 DEVICED_PATH_DISPLAY, DEVICED_INTERFACE_DISPLAY,
147 METHOD_GET_BRIGHTNESS, g_variant_new("(i)", (int)DISPLAY_STATE_NORMAL));
149 return errno_to_device_error(ret); //LCOV_EXCL_LINE System Error
152 return DEVICE_ERROR_NONE;
155 int device_display_set_brightness(int display_index, int brightness)
159 ret = is_display_supported();
161 return DEVICE_ERROR_NOT_SUPPORTED;
163 if (display_cnt < 0) {
164 ret = device_display_get_numbers(&display_cnt);
165 if (ret != DEVICE_ERROR_NONE) //LCOV_EXCL_LINE System Error
169 if (display_index < 0 || display_index >= display_cnt)
170 return DEVICE_ERROR_INVALID_PARAMETER;
172 if (display_arr[display_index].normal_max < 0)
173 device_display_get_max_brightness(display_index, &max);
175 if (brightness < 0 || brightness > display_arr[display_index].normal_max)
176 return DEVICE_ERROR_INVALID_PARAMETER;
178 ret = dbus_handle_method_sync_var(DEVICED_BUS_NAME,
179 DEVICED_PATH_DISPLAY, DEVICED_INTERFACE_DISPLAY,
180 METHOD_SET_BRIGHTNESS, g_variant_new("(ii)", (int)DISPLAY_STATE_NORMAL, brightness));
182 return errno_to_device_error(ret); //LCOV_EXCL_LINE System Error
184 return DEVICE_ERROR_NONE;
187 int device_display_get_state(display_state_e *state)
191 ret = is_display_supported();
193 return DEVICE_ERROR_NOT_SUPPORTED;
196 return DEVICE_ERROR_INVALID_PARAMETER;
198 ret = vconf_get_int(VCONFKEY_PM_STATE, &val);
200 return DEVICE_ERROR_OPERATION_FAILED;
202 if (val == VCONFKEY_PM_STATE_NORMAL)
203 *state = DISPLAY_STATE_NORMAL;
204 else if (val == VCONFKEY_PM_STATE_LCDDIM)
205 *state = DISPLAY_STATE_SCREEN_DIM;
206 else if (val == VCONFKEY_PM_STATE_LCDOFF || val == VCONFKEY_PM_STATE_SLEEP)
207 *state = DISPLAY_STATE_SCREEN_OFF;
209 return DEVICE_ERROR_OPERATION_FAILED;
211 return DEVICE_ERROR_NONE;
214 static char *get_state_str(display_state_e state)
217 case DISPLAY_STATE_NORMAL:
219 case DISPLAY_STATE_SCREEN_DIM:
221 case DISPLAY_STATE_SCREEN_OFF:
229 static void change_cb(GVariant *result, void *data, GError *err)
234 //LCOV_EXCL_START System Error
235 _E("no message : %s", err->message);
240 g_variant_get(result, "(i)", &ret);
241 _D("%s-%s : %d", DEVICED_INTERFACE_DISPLAY, METHOD_CHANGE_STATE, ret);
244 int device_display_change_state(display_state_e state)
248 static int privilege = -1;
249 static long num_calls = 0;
251 ret = is_display_state_supported();
253 return DEVICE_ERROR_NOT_SUPPORTED;
255 if (check_async_call_rate(&num_calls) < 0) {
256 //LCOV_EXCL_START System Error
257 _E("Rejected by too frequent calls; %d (calls per sec.) limit is violated."
258 , CHECK_RATE_THRESHOLD);
259 return DEVICE_ERROR_OPERATION_FAILED;
263 if (state < DISPLAY_STATE_NORMAL || state > DISPLAY_STATE_SCREEN_OFF)
264 return DEVICE_ERROR_INVALID_PARAMETER;
267 ret = dbus_handle_method_sync_var(DEVICED_BUS_NAME,
268 DEVICED_PATH_DISPLAY, DEVICED_INTERFACE_DISPLAY,
269 METHOD_CHANGE_STATE, g_variant_new("(s)", "privilege check"));
270 //LCOV_EXCL_START System Error
272 return errno_to_device_error(ret); //LCOV_EXCL_LINE System Error
278 str = get_state_str(state);
280 return DEVICE_ERROR_INVALID_PARAMETER;
282 ret = dbus_handle_method_async_with_reply_var(DEVICED_BUS_NAME,
283 DEVICED_PATH_DISPLAY, DEVICED_INTERFACE_DISPLAY,
284 METHOD_CHANGE_STATE, g_variant_new("(s)", str), change_cb, -1, NULL);
287 ret = errno_to_device_error(ret); //LCOV_EXCL_LINE System Error
292 int device_display_get_max_brightness_state(int display_index, display_state_e state, int *brightness)
296 ret = is_display_supported();
298 return DEVICE_ERROR_NOT_SUPPORTED;
301 return DEVICE_ERROR_INVALID_PARAMETER;
303 if (state > DISPLAY_STATE_SCREEN_DIM)
304 return DEVICE_ERROR_INVALID_PARAMETER;
306 if (display_cnt < 0) {
307 ret = device_display_get_numbers(&display_cnt);
308 if (ret != DEVICE_ERROR_NONE) //LCOV_EXCL_LINE System Error
312 if (display_index < 0 || display_index >= display_cnt)
313 return DEVICE_ERROR_INVALID_PARAMETER;
315 if (!display_arr && alloc_display() < 0)
316 return DEVICE_ERROR_OPERATION_FAILED;
318 ret = dbus_handle_method_sync_var(DEVICED_BUS_NAME,
319 DEVICED_PATH_DISPLAY, DEVICED_INTERFACE_DISPLAY,
320 METHOD_GET_MAX_BRIGHTNESS, g_variant_new("(i)", (int)state));
322 return errno_to_device_error(ret); //LCOV_EXCL_LINE System Error
324 if (state == DISPLAY_STATE_NORMAL) {
325 display_arr[display_index].normal_max = ret;
326 *brightness = display_arr[display_index].normal_max;
328 display_arr[display_index].dim_max = ret;
329 *brightness = display_arr[display_index].dim_max;
332 return DEVICE_ERROR_NONE;
336 int device_display_get_brightness_state(int display_index, display_state_e state, int *brightness)
340 ret = is_display_supported();
342 return DEVICE_ERROR_NOT_SUPPORTED;
345 return DEVICE_ERROR_INVALID_PARAMETER;
347 if (state > DISPLAY_STATE_SCREEN_DIM)
348 return DEVICE_ERROR_INVALID_PARAMETER;
350 if (display_cnt < 0) {
351 ret = device_display_get_numbers(&display_cnt);
352 if (ret != DEVICE_ERROR_NONE) //LCOV_EXCL_LINE System Error
356 if (display_index < 0 || display_index >= display_cnt)
357 return DEVICE_ERROR_INVALID_PARAMETER;
359 ret = dbus_handle_method_sync_var(DEVICED_BUS_NAME,
360 DEVICED_PATH_DISPLAY, DEVICED_INTERFACE_DISPLAY,
361 METHOD_GET_BRIGHTNESS, g_variant_new("(i)", (int)state));
363 return errno_to_device_error(ret); //LCOV_EXCL_LINE System Error
367 return DEVICE_ERROR_NONE;
370 int device_display_set_brightness_state(int display_index, display_state_e state, int brightness)
374 ret = is_display_supported();
376 return DEVICE_ERROR_NOT_SUPPORTED;
378 if (display_cnt < 0) {
379 ret = device_display_get_numbers(&display_cnt);
380 if (ret != DEVICE_ERROR_NONE) //LCOV_EXCL_LINE System Error
384 if (state > DISPLAY_STATE_SCREEN_DIM)
385 return DEVICE_ERROR_INVALID_PARAMETER;
387 if (display_index < 0 || display_index >= display_cnt)
388 return DEVICE_ERROR_INVALID_PARAMETER;
391 case DISPLAY_STATE_NORMAL:
392 if (display_arr[display_index].normal_max < 0)
393 device_display_get_max_brightness_state(display_index, DISPLAY_STATE_NORMAL, &max);
394 if (brightness < 0 || brightness > display_arr[display_index].normal_max)
395 return DEVICE_ERROR_INVALID_PARAMETER;
397 case DISPLAY_STATE_SCREEN_DIM:
398 if (display_arr[display_index].dim_max < 0)
399 device_display_get_max_brightness_state(display_index, DISPLAY_STATE_SCREEN_DIM, &max);
400 if (brightness < 0 || brightness > display_arr[display_index].dim_max)
401 return DEVICE_ERROR_INVALID_PARAMETER;
407 ret = dbus_handle_method_sync_var(DEVICED_BUS_NAME,
408 DEVICED_PATH_DISPLAY, DEVICED_INTERFACE_DISPLAY,
409 METHOD_SET_BRIGHTNESS, g_variant_new("(ii)", (int)state, brightness));
411 return errno_to_device_error(ret); //LCOV_EXCL_LINE System Error
413 return DEVICE_ERROR_NONE;
416 int device_display_change_state_by_reason(display_state_e type, const char *reason, int timeout, dbus_pending_cb cb)
420 ret = dbus_handle_method_async_with_reply_var(DEVICED_BUS_NAME,
421 DEVICED_PATH_DISPLAY, DEVICED_INTERFACE_DISPLAY,
422 METHOD_CHANGE_STATE_BY_REASON, g_variant_new("(isi)", (int)type, reason, timeout), cb, -1, NULL);
424 return errno_to_device_error(ret);