86c6373994e038ec87174ba93c8f4c865e9e47c0
[platform/core/system/system-server.git] / src / shared / display.c
1 /*
2  * deviced
3  *
4  * Copyright (c) 2012 - 2013 Samsung Electronics Co., Ltd.
5  *
6  * Licensed under the Apache License, Version 2.0 (the License);
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18
19
20 #include <stdio.h>
21 #include <string.h>
22 #include <vconf.h>
23 #include <errno.h>
24 #include <device-node.h>
25 #include <sys/types.h>
26 #include <sys/socket.h>
27 #include <sys/un.h>
28 #include <unistd.h>
29 #include <linux/limits.h>
30
31 #include "log.h"
32 #include "dbus.h"
33 #include "dd-display.h"
34
35 #define DISPLAY_MAX_BRIGHTNESS  100
36 #define DISPLAY_MIN_BRIGHTNESS  1
37 #define DISPLAY_DIM_BRIGHTNESS  0
38
39 #define HOLDKEY_BLOCK_BIT               0x1
40 #define STANDBY_MODE_BIT                0x2
41
42 #define METHOD_SET_FRAME_RATE           "setframerate"
43 #define METHOD_LOCK_STATE               "lockstate"
44 #define METHOD_UNLOCK_STATE             "unlockstate"
45 #define METHOD_CHANGE_STATE             "changestate"
46 #define METHOD_GET_DISPLAY_COUNT        "GetDisplayCount"
47 #define METHOD_GET_BRIGHTNESS   "GetBrightness"
48 #define METHOD_SET_BRIGHTNESS   "SetBrightness"
49 #define METHOD_HOLD_BRIGHTNESS  "HoldBrightness"
50 #define METHOD_RELEASE_BRIGHTNESS       "ReleaseBrightness"
51 #define METHOD_GET_ACL_STATUS   "GetAclStatus"
52 #define METHOD_SET_ACL_STATUS   "SetAclStatus"
53
54 #define STR_LCD_OFF   "lcdoff"
55 #define STR_LCD_DIM   "lcddim"
56 #define STR_LCD_ON    "lcdon"
57 #define STR_SUSPEND   "suspend"
58
59 #define STR_STAYCURSTATE "staycurstate"
60 #define STR_GOTOSTATENOW "gotostatenow"
61
62 #define STR_HOLDKEYBLOCK "holdkeyblock"
63 #define STR_STANDBYMODE  "standbymode"
64 #define STR_NULL         "NULL"
65
66 #define STR_SLEEP_MARGIN "sleepmargin"
67 #define STR_RESET_TIMER  "resettimer"
68 #define STR_KEEP_TIMER   "keeptimer"
69
70 API int display_get_count(void)
71 {
72         DBusError err;
73         DBusMessage *msg;
74         int ret, ret_val;
75
76         msg = deviced_dbus_method_sync(BUS_NAME, DEVICED_PATH_DISPLAY, DEVICED_INTERFACE_DISPLAY,
77                         METHOD_GET_DISPLAY_COUNT, NULL, NULL);
78         if (!msg)
79                 return -EBADMSG;
80
81         dbus_error_init(&err);
82
83         ret = dbus_message_get_args(msg, &err, DBUS_TYPE_INT32, &ret_val, DBUS_TYPE_INVALID);
84         if (!ret) {
85                 _E("no message : [%s:%s]", err.name, err.message);
86                 ret_val = -EBADMSG;
87         }
88
89         dbus_message_unref(msg);
90         dbus_error_free(&err);
91         return ret_val;
92 }
93
94 API int display_get_max_brightness(void)
95 {
96         return DISPLAY_MAX_BRIGHTNESS;
97 }
98
99 API int display_get_min_brightness(void)
100 {
101         return DISPLAY_MIN_BRIGHTNESS;
102 }
103
104 API int display_get_brightness(void)
105 {
106         DBusError err;
107         DBusMessage *msg;
108         int ret, ret_val;
109
110         msg = deviced_dbus_method_sync(BUS_NAME, DEVICED_PATH_DISPLAY, DEVICED_INTERFACE_DISPLAY,
111                         METHOD_GET_BRIGHTNESS, NULL, NULL);
112         if (!msg)
113                 return -EBADMSG;
114
115         dbus_error_init(&err);
116
117         ret = dbus_message_get_args(msg, &err, DBUS_TYPE_INT32, &ret_val, DBUS_TYPE_INVALID);
118         if (!ret) {
119                 _E("no message : [%s:%s]", err.name, err.message);
120                 ret_val = -EBADMSG;
121         }
122
123         dbus_message_unref(msg);
124         dbus_error_free(&err);
125         return ret_val;
126 }
127
128 API int display_set_brightness_with_setting(int val)
129 {
130         DBusError err;
131         DBusMessage *msg;
132         char str_val[32];
133         char *arr[1];
134         int ret, ret_val;
135
136         snprintf(str_val, sizeof(str_val), "%d", val);
137         arr[0] = str_val;
138
139         msg = deviced_dbus_method_sync(BUS_NAME, DEVICED_PATH_DISPLAY, DEVICED_INTERFACE_DISPLAY,
140                         METHOD_SET_BRIGHTNESS, "i", arr);
141         if (!msg)
142                 return -EBADMSG;
143
144         dbus_error_init(&err);
145
146         ret = dbus_message_get_args(msg, &err, DBUS_TYPE_INT32, &ret_val, DBUS_TYPE_INVALID);
147         if (!ret) {
148                 _E("no message : [%s:%s]", err.name, err.message);
149                 ret_val = -EBADMSG;
150         }
151
152         dbus_message_unref(msg);
153         dbus_error_free(&err);
154         return ret_val;
155 }
156
157 API int display_set_brightness(int val)
158 {
159         DBusError err;
160         DBusMessage *msg;
161         char str_val[32];
162         char *arr[1];
163         int ret, ret_val;
164
165         snprintf(str_val, sizeof(str_val), "%d", val);
166         arr[0] = str_val;
167
168         msg = deviced_dbus_method_sync(BUS_NAME, DEVICED_PATH_DISPLAY, DEVICED_INTERFACE_DISPLAY,
169                         METHOD_HOLD_BRIGHTNESS, "i", arr);
170         if (!msg)
171                 return -EBADMSG;
172
173         dbus_error_init(&err);
174
175         ret = dbus_message_get_args(msg, &err, DBUS_TYPE_INT32, &ret_val, DBUS_TYPE_INVALID);
176         if (!ret) {
177                 _E("no message : [%s:%s]", err.name, err.message);
178                 ret_val = -EBADMSG;
179         }
180
181         dbus_message_unref(msg);
182         dbus_error_free(&err);
183         return ret_val;
184 }
185
186 API int display_release_brightness(void)
187 {
188         DBusError err;
189         DBusMessage *msg;
190         int ret, ret_val;
191
192         msg = deviced_dbus_method_sync(BUS_NAME, DEVICED_PATH_DISPLAY, DEVICED_INTERFACE_DISPLAY,
193                         METHOD_RELEASE_BRIGHTNESS, NULL, NULL);
194         if (!msg)
195                 return -EBADMSG;
196
197         dbus_error_init(&err);
198
199         ret = dbus_message_get_args(msg, &err, DBUS_TYPE_INT32, &ret_val, DBUS_TYPE_INVALID);
200         if (!ret) {
201                 _E("no message : [%s:%s]", err.name, err.message);
202                 ret_val = -EBADMSG;
203         }
204
205         dbus_message_unref(msg);
206         dbus_error_free(&err);
207         return ret_val;
208 }
209
210 API int display_get_acl_status(void)
211 {
212         DBusError err;
213         DBusMessage *msg;
214         int ret, ret_val;
215
216         msg = deviced_dbus_method_sync(BUS_NAME, DEVICED_PATH_DISPLAY, DEVICED_INTERFACE_DISPLAY,
217                         METHOD_GET_ACL_STATUS, NULL, NULL);
218         if (!msg)
219                 return -EBADMSG;
220
221         dbus_error_init(&err);
222
223         ret = dbus_message_get_args(msg, &err, DBUS_TYPE_INT32, &ret_val, DBUS_TYPE_INVALID);
224         if (!ret) {
225                 _E("no message : [%s:%s]", err.name, err.message);
226                 ret_val = -EBADMSG;
227         }
228
229         dbus_message_unref(msg);
230         dbus_error_free(&err);
231         return ret_val;
232 }
233
234 API int display_set_acl_status(int val)
235 {
236         DBusError err;
237         DBusMessage *msg;
238         char str_val[32];
239         char *arr[1];
240         int ret, ret_val;
241
242         snprintf(str_val, sizeof(str_val), "%d", val);
243         arr[0] = str_val;
244
245         msg = deviced_dbus_method_sync(BUS_NAME, DEVICED_PATH_DISPLAY, DEVICED_INTERFACE_DISPLAY,
246                         METHOD_SET_ACL_STATUS, "i", arr);
247         if (!msg)
248                 return -EBADMSG;
249
250         dbus_error_init(&err);
251
252         ret = dbus_message_get_args(msg, &err, DBUS_TYPE_INT32, &ret_val, DBUS_TYPE_INVALID);
253         if (!ret) {
254                 _E("no message : [%s:%s]", err.name, err.message);
255                 ret_val = -EBADMSG;
256         }
257
258         dbus_message_unref(msg);
259         dbus_error_free(&err);
260         return ret_val;
261 }
262
263 API int display_set_frame_rate(int val)
264 {
265         DBusError err;
266         DBusMessage *msg;
267         char str_val[32];
268         char *arr[1];
269         int ret, ret_val;
270
271         snprintf(str_val, sizeof(str_val), "%d", val);
272         arr[0] = str_val;
273
274         msg = deviced_dbus_method_sync(BUS_NAME, DEVICED_PATH_DISPLAY, DEVICED_INTERFACE_DISPLAY,
275                         METHOD_SET_FRAME_RATE, "i", arr);
276         if (!msg)
277                 return -EBADMSG;
278
279         dbus_error_init(&err);
280
281         ret = dbus_message_get_args(msg, &err, DBUS_TYPE_INT32, &ret_val, DBUS_TYPE_INVALID);
282         if (!ret) {
283                 _E("no message : [%s:%s]", err.name, err.message);
284                 ret_val = -EBADMSG;
285         }
286
287         dbus_message_unref(msg);
288         dbus_error_free(&err);
289         return ret_val;
290 }
291
292 static inline char *get_lcd_str(unsigned int val)
293 {
294         switch (val) {
295         case LCD_NORMAL:
296                 return STR_LCD_ON;
297         case LCD_DIM:
298                 return STR_LCD_DIM;
299         case LCD_OFF:
300                 return STR_LCD_OFF;
301         case SUSPEND:
302                 return STR_SUSPEND;
303         default:
304                 return NULL;
305         }
306 }
307
308 API int display_change_state(unsigned int s_bits)
309 {
310         DBusError err;
311         DBusMessage *msg;
312         char *p, *pa[1];
313         int ret, val;
314
315         p = get_lcd_str(s_bits);
316         if (!p)
317                 return -EINVAL;
318         pa[0] = p;
319
320         msg = deviced_dbus_method_sync(BUS_NAME, DEVICED_PATH_DISPLAY, DEVICED_INTERFACE_DISPLAY,
321                         METHOD_CHANGE_STATE, "s", pa);
322         if (!msg)
323                 return -EBADMSG;
324
325         dbus_error_init(&err);
326
327         ret = dbus_message_get_args(msg, &err, DBUS_TYPE_INT32, &val, DBUS_TYPE_INVALID);
328         if (!ret) {
329                 _E("no message : [%s:%s]", err.name, err.message);
330                 val = -EBADMSG;
331         }
332
333         dbus_message_unref(msg);
334         dbus_error_free(&err);
335
336         _D("%s-%s : %d", DEVICED_INTERFACE_DISPLAY, METHOD_CHANGE_STATE, val);
337         return val;
338 }
339
340 API int display_lock_state(unsigned int s_bits, unsigned int flag,
341                       unsigned int timeout)
342 {
343         DBusError err;
344         DBusMessage *msg;
345         char *p, *pa[4];
346         char str_timeout[32];
347         int ret, val;
348
349         p = get_lcd_str(s_bits);
350         if (!p)
351                 return -EINVAL;
352         pa[0] = p;
353
354         if (flag & GOTO_STATE_NOW)
355                 /* if the flag is true, go to the locking state directly */
356                 p = STR_GOTOSTATENOW;
357         else
358                 p = STR_STAYCURSTATE;
359         pa[1] = p;
360
361         if (flag & HOLD_KEY_BLOCK)
362                 p = STR_HOLDKEYBLOCK;
363         else if (flag & STANDBY_MODE)
364                 p = STR_STANDBYMODE;
365         else
366                 p = STR_NULL;
367         pa[2] = p;
368
369         snprintf(str_timeout, sizeof(str_timeout), "%d", timeout);
370         pa[3] = str_timeout;
371
372         msg = deviced_dbus_method_sync(BUS_NAME, DEVICED_PATH_DISPLAY, DEVICED_INTERFACE_DISPLAY,
373                         METHOD_LOCK_STATE, "sssi", pa);
374         if (!msg)
375                 return -EBADMSG;
376
377         dbus_error_init(&err);
378
379         ret = dbus_message_get_args(msg, &err, DBUS_TYPE_INT32, &val, DBUS_TYPE_INVALID);
380         if (!ret) {
381                 _E("no message : [%s:%s]", err.name, err.message);
382                 val = -EBADMSG;
383         }
384         dbus_message_unref(msg);
385         dbus_error_free(&err);
386
387         _D("%s-%s : %d", DEVICED_INTERFACE_DISPLAY, METHOD_LOCK_STATE, val);
388         return val;
389 }
390
391 API int display_unlock_state(unsigned int s_bits, unsigned int flag)
392 {
393         DBusError err;
394         DBusMessage *msg;
395         char *p, *pa[2];
396         int ret, val;
397
398         p = get_lcd_str(s_bits);
399         if (!p)
400                 return -EINVAL;
401         pa[0] = p;
402
403         switch (flag) {
404         case PM_SLEEP_MARGIN:
405                 p = STR_SLEEP_MARGIN;
406                 break;
407         case PM_RESET_TIMER:
408                 p = STR_RESET_TIMER;
409                 break;
410         case PM_KEEP_TIMER:
411                 p = STR_KEEP_TIMER;
412                 break;
413         default:
414                 return -EINVAL;
415         }
416         pa[1] = p;
417
418         msg = deviced_dbus_method_sync(BUS_NAME, DEVICED_PATH_DISPLAY, DEVICED_INTERFACE_DISPLAY,
419                         METHOD_UNLOCK_STATE, "ss", pa);
420         if (!msg)
421                 return -EBADMSG;
422
423         dbus_error_init(&err);
424
425         ret = dbus_message_get_args(msg, &err, DBUS_TYPE_INT32, &val, DBUS_TYPE_INVALID);
426         if (!ret) {
427                 _E("no message : [%s:%s]", err.name, err.message);
428                 val = -EBADMSG;
429         }
430
431         dbus_message_unref(msg);
432         dbus_error_free(&err);
433
434         _D("%s-%s : %d", DEVICED_INTERFACE_DISPLAY, METHOD_UNLOCK_STATE, val);
435         return val;
436
437 }