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>
24 #include "display-internal.h"
27 #define METHOD_GET_DISPLAY_COUNT "GetDisplayCount"
28 #define METHOD_GET_MAX_BRIGHTNESS "GetMaxBrightness"
29 #define METHOD_GET_BRIGHTNESS "GetBrightness"
30 #define METHOD_SET_BRIGHTNESS "SetBrightness"
31 #define METHOD_CHANGE_STATE "changestate"
32 #define METHOD_CHANGE_STATE_BY_REASON "ChangeStateByReason"
34 #define STR_LCD_OFF "lcdoff"
35 #define STR_LCD_DIM "lcddim"
36 #define STR_LCD_ON "lcdon"
38 static int display_cnt = -1;
44 static int alloc_display(void)
51 display_arr = malloc(sizeof(struct display) * display_cnt);
55 for (i = 0; i < display_cnt; ++i) {
56 display_arr[i].normal_max = -1;
57 display_arr[i].dim_max = -1;
63 int device_display_get_numbers(int *device_number)
67 ret = is_display_supported();
69 return DEVICE_ERROR_NOT_SUPPORTED;
72 return DEVICE_ERROR_INVALID_PARAMETER;
74 /* if it is a first request */
75 if (display_cnt < 0) {
76 ret = dbus_handle_method_sync_var(DEVICED_BUS_NAME,
77 DEVICED_PATH_DISPLAY, DEVICED_INTERFACE_DISPLAY,
78 METHOD_GET_DISPLAY_COUNT, NULL);
80 return errno_to_device_error(ret); //LCOV_EXCL_LINE System Error
85 *device_number = display_cnt;
86 _I("device_number : %d", *device_number);
87 return DEVICE_ERROR_NONE;
90 int device_display_get_max_brightness(int display_index, int *max_brightness)
94 ret = is_display_supported();
96 return DEVICE_ERROR_NOT_SUPPORTED;
99 return DEVICE_ERROR_INVALID_PARAMETER;
101 if (display_cnt < 0) {
102 ret = device_display_get_numbers(&display_cnt);
103 if (ret != DEVICE_ERROR_NONE) //LCOV_EXCL_LINE System Error
107 if (display_index < 0 || display_index >= display_cnt)
108 return DEVICE_ERROR_INVALID_PARAMETER;
110 if (!display_arr && alloc_display() < 0)
111 return DEVICE_ERROR_OPERATION_FAILED;
113 if (display_arr[display_index].normal_max < 0) {
114 ret = dbus_handle_method_sync_var(DEVICED_BUS_NAME,
115 DEVICED_PATH_DISPLAY, DEVICED_INTERFACE_DISPLAY,
116 METHOD_GET_MAX_BRIGHTNESS, g_variant_new("(i)", (int)DISPLAY_STATE_NORMAL));
118 return errno_to_device_error(ret); //LCOV_EXCL_LINE System Error
119 display_arr[display_index].normal_max = ret;
122 *max_brightness = display_arr[display_index].normal_max;
123 return DEVICE_ERROR_NONE;
126 int device_display_get_brightness(int display_index, int *brightness)
130 ret = is_display_supported();
132 return DEVICE_ERROR_NOT_SUPPORTED;
135 return DEVICE_ERROR_INVALID_PARAMETER;
137 if (display_cnt < 0) {
138 ret = device_display_get_numbers(&display_cnt);
139 if (ret != DEVICE_ERROR_NONE) //LCOV_EXCL_LINE System Error
143 if (display_index < 0 || display_index >= display_cnt)
144 return DEVICE_ERROR_INVALID_PARAMETER;
146 ret = dbus_handle_method_sync_var(DEVICED_BUS_NAME,
147 DEVICED_PATH_DISPLAY, DEVICED_INTERFACE_DISPLAY,
148 METHOD_GET_BRIGHTNESS, g_variant_new("(i)", (int)DISPLAY_STATE_NORMAL));
150 return errno_to_device_error(ret); //LCOV_EXCL_LINE System Error
153 return DEVICE_ERROR_NONE;
156 int device_display_set_brightness(int display_index, int brightness)
160 ret = is_display_supported();
162 return DEVICE_ERROR_NOT_SUPPORTED;
164 if (display_cnt < 0) {
165 ret = device_display_get_numbers(&display_cnt);
166 if (ret != DEVICE_ERROR_NONE) //LCOV_EXCL_LINE System Error
170 if (display_index < 0 || display_index >= display_cnt)
171 return DEVICE_ERROR_INVALID_PARAMETER;
173 if (display_arr[display_index].normal_max < 0)
174 device_display_get_max_brightness(display_index, &max);
176 if (brightness < 0 || brightness > display_arr[display_index].normal_max)
177 return DEVICE_ERROR_INVALID_PARAMETER;
179 ret = dbus_handle_method_sync_var(DEVICED_BUS_NAME,
180 DEVICED_PATH_DISPLAY, DEVICED_INTERFACE_DISPLAY,
181 METHOD_SET_BRIGHTNESS, g_variant_new("(ii)", (int)DISPLAY_STATE_NORMAL, brightness));
183 return errno_to_device_error(ret); //LCOV_EXCL_LINE System Error
185 return DEVICE_ERROR_NONE;
188 int device_display_get_state(display_state_e *state)
192 ret = is_display_supported();
194 return DEVICE_ERROR_NOT_SUPPORTED;
197 return DEVICE_ERROR_INVALID_PARAMETER;
199 ret = vconf_get_int(VCONFKEY_PM_STATE, &val);
201 return DEVICE_ERROR_OPERATION_FAILED;
203 if (val == VCONFKEY_PM_STATE_NORMAL)
204 *state = DISPLAY_STATE_NORMAL;
205 else if (val == VCONFKEY_PM_STATE_LCDDIM)
206 *state = DISPLAY_STATE_SCREEN_DIM;
207 else if (val == VCONFKEY_PM_STATE_LCDOFF || val == VCONFKEY_PM_STATE_SLEEP)
208 *state = DISPLAY_STATE_SCREEN_OFF;
210 return DEVICE_ERROR_OPERATION_FAILED;
212 return DEVICE_ERROR_NONE;
215 static char *get_state_str(display_state_e state)
218 case DISPLAY_STATE_NORMAL:
220 case DISPLAY_STATE_SCREEN_DIM:
222 case DISPLAY_STATE_SCREEN_OFF:
230 static void change_cb(GVariant *result, void *data, GError *err)
235 //LCOV_EXCL_START System Error
236 _E("no message : %s", err->message);
241 g_variant_get(result, "(i)", &ret);
242 _D("%s-%s : %d", DEVICED_INTERFACE_DISPLAY, METHOD_CHANGE_STATE, ret);
245 int device_display_change_state(display_state_e state)
249 static int privilege = -1;
250 static long num_calls = 0;
252 ret = is_display_state_supported();
254 return DEVICE_ERROR_NOT_SUPPORTED;
256 if (check_async_call_rate(&num_calls) < 0) {
257 //LCOV_EXCL_START System Error
258 _E("Rejected by too frequent calls; %d (calls per sec.) limit is violated."
259 , CHECK_RATE_THRESHOLD);
260 return DEVICE_ERROR_OPERATION_FAILED;
264 if (state < DISPLAY_STATE_NORMAL || state > DISPLAY_STATE_SCREEN_OFF)
265 return DEVICE_ERROR_INVALID_PARAMETER;
268 ret = dbus_handle_method_sync_var(DEVICED_BUS_NAME,
269 DEVICED_PATH_DISPLAY, DEVICED_INTERFACE_DISPLAY,
270 METHOD_CHANGE_STATE, g_variant_new("(s)", "privilege check"));
271 //LCOV_EXCL_START System Error
279 str = get_state_str(state);
281 return DEVICE_ERROR_INVALID_PARAMETER;
283 return dbus_handle_method_async_with_reply_var(DEVICED_BUS_NAME,
284 DEVICED_PATH_DISPLAY, DEVICED_INTERFACE_DISPLAY,
285 METHOD_CHANGE_STATE, g_variant_new("(s)", str), change_cb, -1, NULL);
288 int device_display_get_max_brightness_state(int display_index, display_state_e state, int *brightness)
292 ret = is_display_supported();
294 return DEVICE_ERROR_NOT_SUPPORTED;
297 return DEVICE_ERROR_INVALID_PARAMETER;
299 if (state > DISPLAY_STATE_SCREEN_DIM)
300 return DEVICE_ERROR_INVALID_PARAMETER;
302 if (display_cnt < 0) {
303 ret = device_display_get_numbers(&display_cnt);
304 if (ret != DEVICE_ERROR_NONE) //LCOV_EXCL_LINE System Error
308 if (display_index < 0 || display_index >= display_cnt)
309 return DEVICE_ERROR_INVALID_PARAMETER;
311 if (!display_arr && alloc_display() < 0)
312 return DEVICE_ERROR_OPERATION_FAILED;
314 ret = dbus_handle_method_sync_var(DEVICED_BUS_NAME,
315 DEVICED_PATH_DISPLAY, DEVICED_INTERFACE_DISPLAY,
316 METHOD_GET_MAX_BRIGHTNESS, g_variant_new("(i)", (int)state));
318 return errno_to_device_error(ret); //LCOV_EXCL_LINE System Error
320 if (state == DISPLAY_STATE_NORMAL) {
321 display_arr[display_index].normal_max = ret;
322 *brightness = display_arr[display_index].normal_max;
324 display_arr[display_index].dim_max = ret;
325 *brightness = display_arr[display_index].dim_max;
328 return DEVICE_ERROR_NONE;
332 int device_display_get_brightness_state(int display_index, display_state_e state, int *brightness)
336 ret = is_display_supported();
338 return DEVICE_ERROR_NOT_SUPPORTED;
341 return DEVICE_ERROR_INVALID_PARAMETER;
343 if (state > DISPLAY_STATE_SCREEN_DIM)
344 return DEVICE_ERROR_INVALID_PARAMETER;
346 if (display_cnt < 0) {
347 ret = device_display_get_numbers(&display_cnt);
348 if (ret != DEVICE_ERROR_NONE) //LCOV_EXCL_LINE System Error
352 if (display_index < 0 || display_index >= display_cnt)
353 return DEVICE_ERROR_INVALID_PARAMETER;
355 ret = dbus_handle_method_sync_var(DEVICED_BUS_NAME,
356 DEVICED_PATH_DISPLAY, DEVICED_INTERFACE_DISPLAY,
357 METHOD_GET_BRIGHTNESS, g_variant_new("(i)", (int)state));
359 return errno_to_device_error(ret); //LCOV_EXCL_LINE System Error
363 return DEVICE_ERROR_NONE;
366 int device_display_set_brightness_state(int display_index, display_state_e state, int brightness)
370 ret = is_display_supported();
372 return DEVICE_ERROR_NOT_SUPPORTED;
374 if (display_cnt < 0) {
375 ret = device_display_get_numbers(&display_cnt);
376 if (ret != DEVICE_ERROR_NONE) //LCOV_EXCL_LINE System Error
380 if (state > DISPLAY_STATE_SCREEN_DIM)
381 return DEVICE_ERROR_INVALID_PARAMETER;
383 if (display_index < 0 || display_index >= display_cnt)
384 return DEVICE_ERROR_INVALID_PARAMETER;
387 case DISPLAY_STATE_NORMAL:
388 if (display_arr[display_index].normal_max < 0)
389 device_display_get_max_brightness_state(display_index, DISPLAY_STATE_NORMAL, &max);
390 if (brightness < 0 || brightness > display_arr[display_index].normal_max)
391 return DEVICE_ERROR_INVALID_PARAMETER;
393 case DISPLAY_STATE_SCREEN_DIM:
394 if (display_arr[display_index].dim_max < 0)
395 device_display_get_max_brightness_state(display_index, DISPLAY_STATE_SCREEN_DIM, &max);
396 if (brightness < 0 || brightness > display_arr[display_index].dim_max)
397 return DEVICE_ERROR_INVALID_PARAMETER;
403 ret = dbus_handle_method_sync_var(DEVICED_BUS_NAME,
404 DEVICED_PATH_DISPLAY, DEVICED_INTERFACE_DISPLAY,
405 METHOD_SET_BRIGHTNESS, g_variant_new("(ii)", (int)state, brightness));
407 return errno_to_device_error(ret); //LCOV_EXCL_LINE System Error
409 return DEVICE_ERROR_NONE;
412 int device_display_change_state_by_reason(display_state_e type, const char *reason, int timeout, dbus_pending_cb cb)
416 ret = dbus_handle_method_async_with_reply_var(DEVICED_BUS_NAME,
417 DEVICED_PATH_DISPLAY, DEVICED_INTERFACE_DISPLAY,
418 METHOD_CHANGE_STATE_BY_REASON, g_variant_new("(isi)", (int)type, reason, timeout), cb, -1, NULL);
420 return errno_to_device_error(ret);