197260aec02d518dba9069a708156dd03f156f18
[platform/core/system/deviced.git] / src / display / display-dbus.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 /**
21  * @file        display-dbus.c
22  * @brief       dbus interface
23  *
24  */
25
26 #include <error.h>
27 #include <stdbool.h>
28 #include <libsyscommon/libgdbus.h>
29 #include <device/display.h>
30
31 #include "ambient-mode.h"
32 #include "core/log.h"
33 #include "util.h"
34 #include "core.h"
35 #include "lock-detector.h"
36 #include "shared/common.h"
37 #include "shared/devices.h"
38 #include "shared/device-notifier.h"
39 #include "shared/apps.h"
40 #include "dd-display.h"
41 #include "display-actor.h"
42 #include "display-ops.h"
43 #include "display.h"
44 #include "shared/plugin.h"
45 #include "display-lock.h"
46
47 #define AUL_APPSTATUS_PATH              "/Org/Tizen/Aul/AppStatus"
48 #define AUL_APPSTATUS_INTERFACE         "org.tizen.aul.AppStatus"
49 #define APP_CHANGE_STATE                "AppStatusChange"
50 #define APP_TERMINATED                  "AppTerminated"
51
52 #define TELEPHONY_PATH                  "/org/tizen/telephony"
53 #define TELEPHONY_INTERFACE_SIM         "org.tizen.telephony.Manager"
54 #define SIGNAL_SIM_STATUS               "SimInserted"
55 #define SIM_CARD_NOT_PRESENT            (0x0)
56
57 #ifndef VCONFKEY_LCD_BRIGHTNESS_INIT
58 #define VCONFKEY_LCD_BRIGHTNESS_INIT    "db/private/deviced/lcd_brightness_init"
59 #endif
60
61 #define DISPLAY_DIM_BRIGHTNESS  0
62 #define DUMP_MODE_WATING_TIME   600000
63
64 #define LCDOFF_PROXI_STR                "proximity"
65 #define LCDOFF_GESTURE_STR              "gesture"
66
67 #define EXPIRED_POPUP_TYPE_POWER   "power_lock_expired"
68 #define EXPIRED_POPUP_PID    "_APP_PID_"
69 #define EXPIRED_POPUP_COMM   "_APP_COMM_"
70 #define EXPIRED_POPUP_ID     "_REQUEST_ID_"
71
72 static struct display_plugin *disp_plgn;
73 static struct _backlight_ops *backlight_ops;
74 static struct _display_white_balance_ops *display_white_balance_ops;
75 static struct display_config *display_conf;
76
77 static GVariant *dbus_start(GDBusConnection *conn,
78         const gchar *sender, const gchar *path, const gchar *iface, const gchar *name,
79         GVariant *param, GDBusMethodInvocation *invocation, gpointer user_data)
80 {
81         static const struct device_ops *display_device_ops = NULL;
82
83         if (!display_device_ops)
84                 display_device_ops = find_device("display");
85         if (NOT_SUPPORT_OPS(display_device_ops))
86                 goto out;
87
88         display_device_ops->start(CORE_LOGIC_MODE);
89 out:
90         return gdbus_new_g_variant_tuple();
91 }
92
93 static GVariant *dbus_stop(GDBusConnection *conn,
94         const gchar *sender, const gchar *path, const gchar *iface, const gchar *name,
95         GVariant *param, GDBusMethodInvocation *invocation, gpointer user_data)
96 {
97         static const struct device_ops *display_device_ops = NULL;
98
99         if (!display_device_ops)
100                 display_device_ops = find_device("display");
101         if (NOT_SUPPORT_OPS(display_device_ops))
102                 goto out;
103
104         if (disp_plgn->pm_change_internal)
105                 disp_plgn->pm_change_internal(INTERNAL_LOCK_PM, LCD_NORMAL);
106
107         display_device_ops->stop(CORE_LOGIC_MODE);
108 out:
109         return gdbus_new_g_variant_tuple();
110 }
111
112 static GVariant *dbus_lockstate(GDBusConnection *conn,
113         const gchar *sender, const gchar *path, const gchar *iface, const gchar *name,
114         GVariant *param, GDBusMethodInvocation *invocation, gpointer user_data)
115 {
116         char *state_str;
117         char *option1_str;
118         char *option2_str;
119         int timeout;
120         pid_t pid;
121         int state;
122         int flag;
123         int ret = 0;
124         unsigned int caps;
125
126         g_variant_get(param, "(sssi)", &state_str, &option1_str, &option2_str, &timeout);
127
128         if (!state_str || timeout < 0) {
129                 _E("message is invalid!");
130                 ret = -EINVAL;
131                 goto out;
132         }
133
134         if (!strcmp(state_str, "privilege check"))
135                 goto out;
136
137         pid = gdbus_connection_get_sender_pid(conn, sender);
138         if (pid == -1 || kill(pid, 0) == -1) {
139                 _E("%d process does not exist, dbus ignored!", pid);
140                 ret = -ESRCH;
141                 goto out;
142         }
143
144         if (!strcmp(state_str, PM_LCDON_STR))
145                 state = LCD_NORMAL;
146         else if (!strcmp(state_str, PM_LCDDIM_STR)) {
147                 if (!display_conf->dimming) {
148                         _E("LCD_DIM lock is not supported.");
149                         ret = -EINVAL;
150                         goto out;
151                 }
152                 state = LCD_DIM;
153         } else if (!strcmp(state_str, PM_LCDOFF_STR))
154                 state = LCD_OFF;
155         else {
156                 _E("%s state is invalid, dbus ignored.", state_str);
157                 ret = -EINVAL;
158                 goto out;
159         }
160
161         if (!strcmp(option1_str, STAYCURSTATE_STR))
162                 flag = STAY_CUR_STATE;
163         else if (!strcmp(option1_str, GOTOSTATENOW_STR))
164                 flag = GOTO_STATE_NOW;
165         else {
166                 _E("%s option is invalid. set default option!", option1_str);
167                 flag = STAY_CUR_STATE;
168         }
169
170         if (!strcmp(option2_str, HOLDKEYBLOCK_STR))
171                 flag |= HOLD_KEY_BLOCK;
172
173         if (flag & GOTO_STATE_NOW) {
174                 caps = display_get_caps(DISPLAY_ACTOR_API);
175
176                 if (!display_has_caps(caps, DISPLAY_CAPA_LCDON) &&
177                     state == LCD_NORMAL) {
178                         _D("No lcdon capability!");
179                         ret = -EPERM;
180                         goto out;
181                 }
182                 if (!display_has_caps(caps, DISPLAY_CAPA_LCDOFF) &&
183                     state == LCD_OFF) {
184                         _D("No lcdoff capability!");
185                         ret = -EPERM;
186                         goto out;
187                 }
188         }
189
190         if (check_dimstay(state, flag) == true) {
191                 _E("LCD state can not be changed to OFF state now!");
192                 flag &= ~GOTO_STATE_NOW;
193                 flag |= STAY_CUR_STATE;
194         }
195
196         if (disp_plgn->pm_lock_internal)
197                 ret = disp_plgn->pm_lock_internal(pid, state, flag, timeout);
198 out:
199         g_free(state_str);
200         g_free(option1_str);
201         g_free(option2_str);
202         return g_variant_new("(i)", ret);
203 }
204
205 static GVariant *dbus_unlockstate(GDBusConnection *conn,
206         const gchar *sender, const gchar *path, const gchar *iface, const gchar *name,
207         GVariant *param, GDBusMethodInvocation *invocation, gpointer user_data)
208 {
209         char *state_str;
210         char *option_str;
211         pid_t pid;
212         int state;
213         int flag;
214         int ret = 0;
215
216         g_variant_get(param, "(ss)", &state_str, &option_str);
217
218         if (!state_str) {
219                 _E("message is invalid!");
220                 ret = -EINVAL;
221                 goto out;
222         }
223
224         if (!strcmp(state_str, "privilege check"))
225                 goto out;
226
227         pid = gdbus_connection_get_sender_pid(conn, sender);
228         if (pid == -1 || kill(pid, 0) == -1) {
229                 _E("%d process does not exist, dbus ignored!", pid);
230                 ret = -ESRCH;
231                 goto out;
232         }
233
234         if (!strcmp(state_str, PM_LCDON_STR))
235                 state = LCD_NORMAL;
236         else if (!strcmp(state_str, PM_LCDDIM_STR)) {
237                 if (!display_conf->dimming) {
238                         _E("LCD_DIM unlock is not supported.");
239                         ret = -EINVAL;
240                         goto out;
241                 }
242                 state = LCD_DIM;
243         } else if (!strcmp(state_str, PM_LCDOFF_STR))
244                 state = LCD_OFF;
245         else {
246                 _E("%s state is invalid, dbus ignored!", state_str);
247                 ret = -EINVAL;
248                 goto out;
249         }
250
251         if (!strcmp(option_str, SLEEP_MARGIN_STR))
252                 flag = PM_SLEEP_MARGIN;
253         else if (!strcmp(option_str, RESET_TIMER_STR))
254                 flag = PM_RESET_TIMER;
255         else if (!strcmp(option_str, KEEP_TIMER_STR))
256                 flag = PM_KEEP_TIMER;
257         else {
258                 _E("%s option is invalid. set default option!", option_str);
259                 flag = PM_RESET_TIMER;
260         }
261
262         if (disp_plgn->pm_unlock_internal)
263                 ret = disp_plgn->pm_unlock_internal(pid, state, flag);
264 out:
265         g_free(state_str);
266         g_free(option_str);
267         return g_variant_new("(i)", ret);
268 }
269
270 static GVariant *dbus_changestate(GDBusConnection *conn,
271         const gchar *sender, const gchar *path, const gchar *iface, const gchar *name,
272         GVariant *param, GDBusMethodInvocation *invocation, gpointer user_data)
273 {
274         char *state_str;
275         pid_t pid;
276         int state;
277         int ret = 0, len, lcdon_blocked;
278         unsigned int caps;
279
280         g_variant_get(param, "(s)", &state_str);
281
282         if (!state_str) {
283                 _E("message is invalid!");
284                 ret = -EINVAL;
285                 goto out;
286         }
287
288         if (!strcmp(state_str, "privilege check"))
289                 goto out;
290
291         pid = gdbus_connection_get_sender_pid(conn, sender);
292         if (pid == -1 || kill(pid, 0) == -1) {
293                 _E("%d process does not exist, dbus ignored!", pid);
294                 ret = -ESRCH;
295                 goto out;
296         }
297
298         len = strlen(state_str);
299         if (len == 0) {
300                 ret = -EINVAL;
301                 goto out;
302         }
303
304         if (!strcmp(state_str, PM_LCDON_STR))
305                 state = LCD_NORMAL;
306         else if (!strcmp(state_str, PM_LCDDIM_STR)) {
307                 if (!display_conf->dimming) {
308                         _E("LCD_DIM state is not supported.");
309                         ret = -EINVAL;
310                         goto out;
311                 }
312                 state = LCD_DIM;
313         } else if (!strcmp(state_str, PM_LCDOFF_STR))
314                 state = LCD_OFF;
315         else if (!strcmp(state_str, PM_STANDBY_STR))
316                 state = STANDBY;
317         else if (!strcmp(state_str, PM_SUSPEND_STR))
318                 state = SUSPEND;
319         else {
320                 _E("%s state is invalid, dbus ignored!", state_str);
321                 ret = -EINVAL;
322                 goto out;
323         }
324
325         caps = display_get_caps(DISPLAY_ACTOR_API);
326
327         if (!display_has_caps(caps, DISPLAY_CAPA_LCDON) &&
328             state == LCD_NORMAL) {
329                 _D("No lcdon capability!");
330                 ret = -EPERM;
331                 goto out;
332         }
333         if (!display_has_caps(caps, DISPLAY_CAPA_LCDOFF) &&
334             state == LCD_OFF) {
335                 _D("No lcdoff capability!");
336                 ret = -EPERM;
337                 goto out;
338         }
339
340         lcdon_blocked = is_lcdon_blocked();
341         if (lcdon_blocked != LCDON_BLOCK_NONE) {
342                 if (state == LCD_NORMAL || state == LCD_DIM) {
343                         _W("LCDON is blocked, %d.", lcdon_blocked);
344                         ret = -ENOTSUP;
345                         goto out;
346                 }
347         }
348
349         if (check_dimstay(state, GOTO_STATE_NOW) == true) {
350                 _E("LCD state can not be changed to OFF state! by %d", pid);
351                 ret = -EBUSY;
352                 goto out;
353         }
354
355         if (disp_plgn->pm_change_internal)
356                 ret = disp_plgn->pm_change_internal(pid, state);
357
358         if (!ret && state == LCD_OFF)
359                 update_lcdoff_source(VCONFKEY_PM_LCDOFF_BY_TIMEOUT);
360 out:
361         g_free(state_str);
362         return g_variant_new("(i)", ret);
363 }
364
365 static GVariant *dbus_getdisplaycount(GDBusConnection *conn,
366         const gchar *sender, const gchar *path, const gchar *iface, const gchar *name,
367         GVariant *param, GDBusMethodInvocation *invocation, gpointer user_data)
368 {
369         int ret;
370
371         ret = DEFAULT_DISPLAY_COUNT;
372
373         return g_variant_new("(i)", ret);
374 }
375
376 static GVariant *dbus_getmaxbrightness(GDBusConnection *conn,
377         const gchar *sender, const gchar *path, const gchar *iface, const gchar *name,
378         GVariant *param, GDBusMethodInvocation *invocation, gpointer user_data)
379 {
380         int ret, state;
381
382         g_variant_get(param, "(i)", &state);
383         if (state == DISPLAY_STATE_NORMAL)
384                 ret = DEFAULT_DISPLAY_MAX_BRIGHTNESS;
385         else if (state == DISPLAY_STATE_SCREEN_DIM)
386                 ret = DEFAULT_DISPLAY_MAX_DIM_BRIGHTNESS;
387         else
388                 ret = -EINVAL;
389
390         return g_variant_new("(i)", ret);
391 }
392
393 static GVariant *dbus_setmaxbrightness(GDBusConnection *conn,
394         const gchar *sender, const gchar *path, const gchar *iface, const gchar *name,
395         GVariant *param, GDBusMethodInvocation *invocation, gpointer user_data)
396 {
397         int ret;
398
399         ret = -ENOTSUP;
400
401         return g_variant_new("(i)", ret);
402 }
403
404 static GVariant *dbus_getbrightness(GDBusConnection *conn,
405         const gchar *sender, const gchar *path, const gchar *iface, const gchar *name,
406         GVariant *param, GDBusMethodInvocation *invocation, gpointer user_data)
407 {
408         int brt = -1, ret, result;
409
410         if (get_pm_cur_state() == S_NORMAL) {
411                 ret = backlight_ops->get_brightness(&brt);
412                 if (ret < 0)
413                         result = 0;
414                 else
415                         result = brt;
416         } else if (get_pm_cur_state() == S_LCDDIM) {
417                 ret = vconf_get_int(VCONFKEY_SETAPPL_LCD_DIM_BRIGHTNESS, &brt);
418                 if (ret < 0) {
419                         _E("Failed to get vconf value for lcd dim brightness: %d", vconf_get_ext_errno());
420                         result = 0;
421                 } else {
422                         result = brt;
423                 }
424         } else {
425                 result = backlight_ops->get_default_brt();
426                 brt = 0;
427         }
428
429         _I("get brightness %d, %d", brt, result);
430
431         return g_variant_new("(i)", result);
432 }
433
434 static GVariant *dbus_setbrightness(GDBusConnection *conn,
435         const gchar *sender, const gchar *path, const gchar *iface, const gchar *name,
436         GVariant *param, GDBusMethodInvocation *invocation, gpointer user_data)
437 {
438         int state, brt, autobrt, ret = 0, caps;
439         pid_t pid;
440
441         caps = display_get_caps(DISPLAY_ACTOR_API);
442
443         if (!display_has_caps(caps, DISPLAY_CAPA_BRIGHTNESS)) {
444                 _D("No brightness changing capability!");
445                 ret = -EPERM;
446                 goto error;
447         }
448
449         g_variant_get(param, "(ii)", &state, &brt);
450
451         //Check if brt is same with DIM
452         if (brt == DISPLAY_DIM_BRIGHTNESS) {
453                 _E("application can not set this value(DIM VALUE:%d)", brt);
454                 ret = -EPERM;
455                 goto error;
456         }
457
458         ret = vconf_get_int(VCONFKEY_SETAPPL_BRIGHTNESS_AUTOMATIC_INT, &autobrt);
459         if (ret < 0) {
460                 _E("Failed to get vconf value for automatic brightness: %d", vconf_get_ext_errno());
461                 autobrt = SETTING_BRIGHTNESS_AUTOMATIC_OFF;
462         }
463
464         if (autobrt == SETTING_BRIGHTNESS_AUTOMATIC_ON) {
465                 _E("auto_brightness state is ON, can not change the brightness value");
466                 ret = -EPERM;
467                 goto error;
468         }
469
470         clear_pm_status_flag(DIM_MASK);
471         if (state == DISPLAY_STATE_NORMAL) {
472                 if (disp_plgn->auto_brightness_control) {
473                         ret = disp_plgn->auto_brightness_control(BR_SET_BRIGHTNESS, brt);
474                 } else {
475                         backlight_ops->set_default_brt(brt);
476                         ret = backlight_ops->set_brightness(brt);
477                         if (ret < 0)
478                                 goto error;
479                         ret = vconf_set_int(VCONFKEY_SETAPPL_LCD_BRIGHTNESS, brt);
480                         if (ret < 0)
481                                 _E("Failed to set vconf value for lcd brightness: %d", vconf_get_ext_errno());
482                 }
483         } else if (state == DISPLAY_STATE_SCREEN_DIM) {
484                 if (get_pm_cur_state() == S_LCDDIM) {
485                         ret = backlight_ops->set_brightness(brt);
486                         if (ret < 0)
487                                 goto error;
488                 }
489
490                 ret = vconf_set_int(VCONFKEY_SETAPPL_LCD_DIM_BRIGHTNESS, brt);
491                 if (ret < 0)
492                         _E("Failed to set vconf value for lcd dim brightness: %d", vconf_get_ext_errno());
493         } else
494                 ret = -EINVAL;
495
496         pid = gdbus_connection_get_sender_pid(conn, sender);
497         _I("Set brightness pid=%d brt=%d ret=%d", pid, brt, ret);
498
499 error:
500         return g_variant_new("(i)", ret);
501 }
502
503 static GVariant *dbus_holdbrightness(GDBusConnection *conn,
504         const gchar *sender, const gchar *path, const gchar *iface, const gchar *name,
505         GVariant *param, GDBusMethodInvocation *invocation, gpointer user_data)
506 {
507         int brt, autobrt, ret, caps;
508         pid_t pid;
509
510         caps = display_get_caps(DISPLAY_ACTOR_API);
511
512         if (!display_has_caps(caps, DISPLAY_CAPA_BRIGHTNESS)) {
513                 _D("No brightness changing capability!");
514                 ret = -EPERM;
515                 goto error;
516         }
517
518         g_variant_get(param, "(i)", &brt);
519
520         if (brt == DISPLAY_DIM_BRIGHTNESS) {
521                 _E("application can not set this value(DIM VALUE:%d)", brt);
522                 ret = -EPERM;
523                 goto error;
524         }
525
526         ret = vconf_get_int(VCONFKEY_SETAPPL_BRIGHTNESS_AUTOMATIC_INT, &autobrt);
527         if (ret < 0) {
528                 _E("Failed to get vconf value for automatic brightness: %d", vconf_get_ext_errno());
529                 autobrt = SETTING_BRIGHTNESS_AUTOMATIC_OFF;
530         }
531
532         ret = vconf_set_int(VCONFKEY_PM_CUSTOM_BRIGHTNESS_STATUS, VCONFKEY_PM_CUSTOM_BRIGHTNESS_ON);
533         if (ret < 0)
534                 _E("Failed to set vconf value for custom brightness status: %d", vconf_get_ext_errno());
535
536         if (disp_plgn->auto_brightness_control)
537                 ret = disp_plgn->auto_brightness_control(BR_HOLD_BRIGHTNESS, brt);
538         else
539                 ret = backlight_ops->set_brightness(brt);
540
541         if (ret < 0)
542                 goto error;
543
544         if (autobrt == SETTING_BRIGHTNESS_AUTOMATIC_ON) {
545                 _D("Auto brightness will be paused");
546                 ret = vconf_set_int(VCONFKEY_SETAPPL_BRIGHTNESS_AUTOMATIC_INT, SETTING_BRIGHTNESS_AUTOMATIC_PAUSE);
547                 if (ret < 0)
548                         _E("Failed to set vconf value for automatic brightness: %d", vconf_get_ext_errno());
549         }
550
551         pid = gdbus_connection_get_sender_pid(conn, sender);
552         _I("Hold brightness pid=%d brt=%d ret=%d", pid, brt, ret);
553
554 error:
555         return g_variant_new("(i)", ret);
556 }
557
558 static GVariant *dbus_releasebrightness(GDBusConnection *conn,
559         const gchar *sender, const gchar *path, const gchar *iface, const gchar *name,
560         GVariant *param, GDBusMethodInvocation *invocation, gpointer user_data)
561 {
562         int bat, charger, changed, setting, brt, autobrt, ret = 0;
563         pid_t pid;
564
565         pid = gdbus_connection_get_sender_pid(conn, sender);
566         _I("Release brightness pid=%d", pid);
567
568         ret = vconf_get_int(VCONFKEY_SYSMAN_BATTERY_STATUS_LOW, &bat);
569         if (ret < 0) {
570                 _E("Failed to get vconf value for battery status low: %d", vconf_get_ext_errno());
571                 ret = -EPERM;
572                 goto error;
573         }
574
575         ret = vconf_get_int(VCONFKEY_SYSMAN_CHARGER_STATUS, &charger);
576         if (ret < 0) {
577                 _E("Failed to get vconf value for charger status: %d", vconf_get_ext_errno());
578                 ret = -EPERM;
579                 goto error;
580         }
581
582         ret = vconf_get_bool(VCONFKEY_PM_BRIGHTNESS_CHANGED_IN_LPM, &changed);
583         if (ret < 0) {
584                 _E("Failed to get vconf value for brightness changed in lpm: %d", vconf_get_ext_errno());
585                 ret = -EPERM;
586                 goto error;
587         }
588
589         ret = vconf_get_int(VCONFKEY_SETAPPL_LCD_BRIGHTNESS, &setting);
590         if (ret < 0) {
591                 _E("Failed to get vconf value for lcd brightness: %d", vconf_get_ext_errno());
592                 ret = -EPERM;
593                 goto error;
594         }
595
596         ret = vconf_get_int(VCONFKEY_SETAPPL_BRIGHTNESS_AUTOMATIC_INT, &autobrt);
597         if (ret < 0) {
598                 _E("Failed to get vconf value for automatic brightness: %d", vconf_get_ext_errno());
599                 ret = -EPERM;
600                 goto error;
601         }
602
603         ret = vconf_set_int(VCONFKEY_PM_CUSTOM_BRIGHTNESS_STATUS, VCONFKEY_PM_CUSTOM_BRIGHTNESS_OFF);
604         if (ret < 0)
605                 _E("Failed to set vconf value for custom brightness status: %d", vconf_get_ext_errno());
606
607         ret = backlight_ops->get_brightness(&brt);
608         if (ret < 0)
609                 brt = ret;
610
611         // check dim state
612         if (!disp_plgn->auto_brightness_control && low_battery_state(bat) &&
613             charger == VCONFKEY_SYSMAN_CHARGER_DISCONNECTED && !changed) {
614                 _D("batt warning low : brightness is not changed!");
615                 if (brt != 0)
616                         backlight_ops->set_brightness(0);
617                 goto error;
618         }
619
620         if (autobrt == SETTING_BRIGHTNESS_AUTOMATIC_OFF) {
621                 if (disp_plgn->auto_brightness_control) {
622                         disp_plgn->auto_brightness_control(BR_RELEASE_BRIGHTNESS, BR_IMPLICIT);
623                 } else {
624                         if (brt != setting)
625                                 backlight_ops->set_brightness(setting);
626                 }
627         } else if (autobrt == SETTING_BRIGHTNESS_AUTOMATIC_PAUSE) {
628                 _D("Auto brightness will be enable");
629                 ret = vconf_set_int(VCONFKEY_SETAPPL_BRIGHTNESS_AUTOMATIC_INT, SETTING_BRIGHTNESS_AUTOMATIC_ON);
630                 if (ret < 0)
631                         _E("Failed to set vconf value for automatic brightness: %d", vconf_get_ext_errno());
632         }
633
634 error:
635         return g_variant_new("(i)", ret);
636 }
637
638 static GVariant *dbus_setrefreshrate(GDBusConnection *conn,
639         const gchar *sender, const gchar *path, const gchar *iface, const gchar *name,
640         GVariant *param, GDBusMethodInvocation *invocation, gpointer user_data)
641 {
642         int app, val, ret, control;
643
644         g_variant_get(param, "(ii)", &app, &val);
645
646         if (app < 0 || app >= ARRAY_SIZE(display_conf->framerate_app) || val < 0) {
647                 ret = -EINVAL;
648                 goto error;
649         }
650
651         if (!display_conf->framerate_app[app]) {
652                 _I("This case(%d) is not support in this target", app);
653                 ret = -EPERM;
654                 goto error;
655         }
656
657         control = display_conf->control_display;
658
659         if (control)
660                 backlight_ops->off(NORMAL_MODE);
661
662         _D("app : %d, value : %d", app, val);
663         ret = backlight_ops->set_frame_rate(val);
664         if (ret < 0)
665                 _E("Failed to set frame rate (%d)", ret);
666
667         if (control)
668                 backlight_ops->on(NORMAL_MODE);
669
670 error:
671         return g_variant_new("(i)", ret);
672 }
673
674 static GVariant *dbus_setautobrightnessmin(GDBusConnection *conn,
675         const gchar *sender, const gchar *path, const gchar *iface, const gchar *name,
676         GVariant *param, GDBusMethodInvocation *invocation, gpointer user_data)
677 {
678         int val, ret;
679         pid_t pid;
680         int id = 0;
681
682         if (!display_info.set_autobrightness_min) {
683                 ret = -EIO;
684                 goto error;
685         }
686         g_variant_get(param, "(i)", &val);
687
688         pid = gdbus_connection_get_sender_pid(conn, sender);
689         if (pid == -1 || kill(pid, 0) == -1) {
690                 _E("%d process does not exist, dbus ignored!", pid);
691                 ret = -ESRCH;
692                 goto error;
693         }
694         ret = display_info.set_autobrightness_min(val, (char *)sender);
695         if (ret) {
696                 _W("fail to set autobrightness min %d, %d by %d", val, ret, pid);
697                 goto error;
698         }
699         if (display_info.reset_autobrightness_min) {
700                 id = gdbus_watch_name(sender, display_info.reset_autobrightness_min, NULL, NULL, NULL);
701                 if (id <= 0) {
702                         _E("failed to watch name %s, id %d", sender, id);
703                         //todo: set return value
704                 }
705                 _I("set autobrightness min %d by %d", val, pid);
706         }
707 error:
708         return g_variant_new("(i)", ret);
709 }
710
711 static GVariant *dbus_setlcdtimeout(GDBusConnection *conn,
712         const gchar *sender, const gchar *path, const gchar *iface, const gchar *name,
713         GVariant *param, GDBusMethodInvocation *invocation, gpointer user_data)
714 {
715         int on, dim, holdkey_block, ret;
716         pid_t pid;
717         int id = 0;
718
719         g_variant_get(param, "(iii)", &on, &dim, &holdkey_block);
720
721         pid = gdbus_connection_get_sender_pid(conn, sender);
722         if (pid == -1 || kill(pid, 0) == -1) {
723                 _E("%d process does not exist, dbus ignored!", pid);
724                 ret = -ESRCH;
725                 goto error;
726         }
727
728         ret = set_lcd_timeout(on, dim, holdkey_block, sender);
729         if (ret) {
730                 _W("fail to set lcd timeout %d by %d", ret, pid);
731         } else {
732                 id = gdbus_watch_name(sender, reset_lcd_timeout, NULL, NULL, NULL);
733                 if (id <= 0) {
734                         _E("failed to watch name %s, id %d", sender, id);
735                         //todo: set return value
736                 }
737                 _I("set lcd timeout on %d, dim %d, holdblock %d by %d",
738                     on, dim, holdkey_block, pid);
739         }
740
741 error:
742         return g_variant_new("(i)", ret);
743 }
744
745 static GVariant *dbus_lockscreenbgon(GDBusConnection *conn,
746         const gchar *sender, const gchar *path, const gchar *iface, const gchar *name,
747         GVariant *param, GDBusMethodInvocation *invocation, gpointer user_data)
748 {
749         int ret = 0;
750         char *on = NULL;
751
752         g_variant_get(param, "(s)", &on);
753
754         if (!strcmp(on, "true")) {
755                 if (disp_plgn->update_pm_setting)
756                         disp_plgn->update_pm_setting(SETTING_LOCK_SCREEN_BG, true);
757         } else if (!strcmp(on, "false")) {
758                 if (disp_plgn->update_pm_setting)
759                         disp_plgn->update_pm_setting(SETTING_LOCK_SCREEN_BG, false);
760         } else
761                 ret = -EINVAL;
762
763         g_free(on);
764         return g_variant_new("(i)", ret);
765 }
766
767 static GVariant *dbus_dumpmode(GDBusConnection *conn,
768         const gchar *sender, const gchar *path, const gchar *iface, const gchar *name,
769         GVariant *param, GDBusMethodInvocation *invocation, gpointer user_data)
770 {
771         int ret = 0;
772         char *on;
773
774         g_variant_get(param, "(s)", &on);
775
776         if (!strcmp(on, "on")) {
777                 pm_save_logdump();
778                 if (disp_plgn->pm_lock_internal)
779                         disp_plgn->pm_lock_internal(INTERNAL_LOCK_DUMPMODE, LCD_OFF,
780                                 STAY_CUR_STATE, DUMP_MODE_WATING_TIME);
781                 backlight_ops->release_blink();
782         } else if (!strcmp(on, "off")) {
783                 if (disp_plgn->pm_unlock_internal)
784                         disp_plgn->pm_unlock_internal(INTERNAL_LOCK_DUMPMODE, LCD_OFF, PM_SLEEP_MARGIN);
785                 backlight_ops->blink(0);
786         } else if (!strcmp(on, "blink")) {
787                 backlight_ops->blink(500);
788         } else {
789                 ret = -EINVAL;
790         }
791
792         g_free(on);
793         return g_variant_new("(i)", ret);
794 }
795
796 static GVariant *dbus_savelog(GDBusConnection *conn,
797         const gchar *sender, const gchar *path, const gchar *iface, const gchar *name,
798         GVariant *param, GDBusMethodInvocation *invocation, gpointer user_data)
799 {
800         pm_save_logdump();
801         return gdbus_new_g_variant_tuple();
802 }
803
804 static GVariant *dbus_powerkeyignore(GDBusConnection *conn,
805         const gchar *sender, const gchar *path, const gchar *iface, const gchar *name,
806         GVariant *param, GDBusMethodInvocation *invocation, gpointer user_data)
807 {
808         int ret = 0;
809         int on;
810
811         g_variant_get(param, "(i)", &on);
812
813         if (CHECK_OPS(keyfilter_ops, set_powerkey_ignore))
814                 keyfilter_ops->set_powerkey_ignore(on == 1 ? true : false);
815
816         return g_variant_new("(i)", ret);
817 }
818
819 static GVariant *dbus_powerkeylcdoff(GDBusConnection *conn,
820         const gchar *sender, const gchar *path, const gchar *iface, const gchar *name,
821         GVariant *param, GDBusMethodInvocation *invocation, gpointer user_data)
822 {
823         int ret;
824
825         if (CHECK_OPS(keyfilter_ops, powerkey_lcdoff))
826                 ret = keyfilter_ops->powerkey_lcdoff();
827         else
828                 ret = -ENOSYS;
829
830         return g_variant_new("(i)", ret);
831 }
832
833 static GVariant *dbus_customlcdon(GDBusConnection *conn,
834         const gchar *sender, const gchar *path, const gchar *iface, const gchar *name,
835         GVariant *param, GDBusMethodInvocation *invocation, gpointer user_data)
836 {
837         int ret = 0;
838         int timeout, lcdon_blocked;
839         pid_t pid;
840
841         pid = gdbus_connection_get_sender_pid(conn, sender);
842         _I("Change state to S_LCDON pid=%d", pid);
843
844         g_variant_get(param, "(i)", &timeout);
845
846         lcdon_blocked = is_lcdon_blocked();
847         if (lcdon_blocked != LCDON_BLOCK_NONE) {
848                 _W("LCDON is blocked, %d.", lcdon_blocked);
849                 return g_variant_new("(i)", -ENOTSUP);
850         }
851
852         ret = custom_lcdon(timeout);
853
854         return g_variant_new("(i)", ret);
855 }
856
857 static GVariant *dbus_customlcdoff(GDBusConnection *conn,
858         const gchar *sender, const gchar *path, const gchar *iface, const gchar *name,
859         GVariant *param, GDBusMethodInvocation *invocation, gpointer user_data)
860 {
861         int ret = 0;
862         enum device_flags flag;
863         char *reason_str = NULL;
864         pid_t pid;
865
866         pid = gdbus_connection_get_sender_pid(conn, sender);
867         _I("Change state to S_LCDOFF pid=%d", pid);
868
869         g_variant_get(param, "(s)", &reason_str);
870
871         if (!strcmp(reason_str, LCDOFF_PROXI_STR))
872                 flag = LCD_OFF_BY_PROXIMITY;
873         else if (!strcmp(reason_str, LCDOFF_GESTURE_STR))
874                 flag = LCD_OFF_BY_GESTURE;
875         else {
876                 _E("%s resean is invalid, dbus ignored!", reason_str);
877                 ret = -EINVAL;
878                 goto out;
879         }
880
881         ret = custom_lcdoff(flag);
882
883 out:
884         g_free(reason_str);
885
886         return g_variant_new("(i)", ret);
887 }
888
889 static GVariant *dbus_changestatebyreason(GDBusConnection *conn,
890         const gchar *sender, const gchar *path, const gchar *iface, const gchar *name,
891         GVariant *param, GDBusMethodInvocation *invocation, gpointer user_data)
892 {
893         int ret = 0;
894         int next_state, timeout;
895         char *reason = NULL, *state = NULL;
896         pid_t pid;
897
898         g_variant_get(param, "(isi)", &next_state, &reason, &timeout);
899
900         if (next_state == DISPLAY_STATE_NORMAL) {
901                 state = "S_LCDON";
902                 ret = display_on_by_reason(reason, timeout);
903         } else if (next_state == DISPLAY_STATE_SCREEN_OFF) {
904                 state = "S_LCDOFF";
905                 ret = display_off_by_reason(reason);
906         } else if (next_state == DISPLAY_STATE_SCREEN_DIM) {
907                 state = "S_LCDDIM";
908                 ret = -EINVAL;
909         } else {
910                 state = "unknown";
911                 ret = -EINVAL;
912         }
913
914         pid = gdbus_connection_get_sender_pid(conn, sender);
915         _I("Change state pid=%d state=%s reason=%s ret=%d", pid, state, reason, ret);
916
917         g_free(reason);
918         return g_variant_new("(i)", ret);
919 }
920
921 static GVariant *dbus_staytouchscreenoff(GDBusConnection *conn,
922         const gchar *sender, const gchar *path, const gchar *iface, const gchar *name,
923         GVariant *param, GDBusMethodInvocation *invocation, gpointer user_data)
924 {
925         int ret = 0;
926         int val;
927         pid_t pid;
928
929         pid = gdbus_connection_get_sender_pid(conn, sender);
930         _I("Stay touchscreen off pid=%d", pid);
931
932         g_variant_get(param, "(i)", &val);
933
934         set_stay_touchscreen_off(val);
935
936         return g_variant_new("(i)", ret);
937 }
938
939 static GVariant *dbus_lcdpaneloffmode(GDBusConnection *conn,
940         const gchar *sender, const gchar *path, const gchar *iface, const gchar *name,
941         GVariant *param, GDBusMethodInvocation *invocation, gpointer user_data)
942 {
943         int ret = 0;
944         int val;
945         pid_t pid;
946
947         pid = gdbus_connection_get_sender_pid(conn, sender);
948         _I("Set lcd panel off mode pid=%d", pid);
949
950         g_variant_get(param, "(i)", &val);
951
952         set_lcd_paneloff_mode(val);
953
954         return g_variant_new("(i)", ret);
955 }
956
957 static GVariant *dbus_actorcontrol(GDBusConnection *conn,
958         const gchar *sender, const gchar *path, const gchar *iface, const gchar *name,
959         GVariant *param, GDBusMethodInvocation *invocation, gpointer user_data)
960 {
961         int ret = 0, val, actor;
962         char *op;
963
964         g_variant_get(param, "(sii)", &op, &actor, &val);
965
966         if (!strcmp(op, "set"))
967                 ret = display_set_caps(actor, val);
968         else if (!strcmp(op, "reset"))
969                 ret = display_reset_caps(actor, val);
970         else
971                 ret = -EINVAL;
972
973         g_free(op);
974         return g_variant_new("(i)", ret);
975 }
976
977 static GVariant *dbus_getcustombrightness(GDBusConnection *conn,
978         const gchar *sender, const gchar *path, const gchar *iface, const gchar *name,
979         GVariant *param, GDBusMethodInvocation *invocation, gpointer user_data)
980 {
981         int status = 0;
982
983         status = backlight_ops->get_custom_status();
984
985         return g_variant_new("(i)", status);
986 }
987
988 struct pmlock_expired_s {
989         char req_id[32];
990         GDBusMethodInvocation *invocation;
991         int value;
992         pid_t pid;
993 };
994
995 static GList *expired_req_list;
996
997 static GVariant *dbus_locktimeout_expired(GDBusConnection *conn,
998         const gchar *sender, const gchar *path, const gchar *iface, const gchar *name,
999         GVariant *param, GDBusMethodInvocation *invocation, gpointer user_data)
1000 {
1001         int ret;
1002         char *req_id;
1003         pid_t pid;
1004         char pid_str[16];
1005         char comm[NAME_MAX];
1006         struct pmlock_expired_s *ex = NULL;
1007         GVariant *gvar = NULL;
1008
1009         g_variant_get(param, "(s)", &req_id);
1010
1011         pid = gdbus_connection_get_sender_pid(conn, sender);
1012         if (pid == -1 || kill(pid, 0) == -1) {
1013                 _E("%d process does not exist, dbus ignored!", pid);
1014                 ret = -ESRCH;
1015                 goto out;
1016         }
1017
1018         ret = get_command(pid, comm, sizeof(comm));
1019         if (ret < 0) {
1020                 _E("Failed to get command (%d)", ret);
1021                 goto out;
1022         }
1023
1024         ex = calloc(1, sizeof(struct pmlock_expired_s));
1025         if (!ex) {
1026                 _E("calloc() failed");
1027                 ret = -ENOMEM;
1028                 goto out;
1029         }
1030
1031         snprintf(pid_str, sizeof(pid_str), "%d", pid);
1032         ret = launch_system_app(APP_DEFAULT, 8,
1033                         APP_KEY_TYPE,
1034                         EXPIRED_POPUP_TYPE_POWER,
1035                         EXPIRED_POPUP_PID,
1036                         pid_str,
1037                         EXPIRED_POPUP_COMM,
1038                         comm,
1039                         EXPIRED_POPUP_ID,
1040                         req_id);
1041         if (ret < 0) {
1042                 _E("Failed to launch pmlock expired popup(%d)", ret);
1043                 goto out;
1044         }
1045
1046         ex->pid = pid;
1047         snprintf(ex->req_id, sizeof(ex->req_id), "%s", req_id);
1048
1049         ex->invocation = invocation;
1050
1051         SYS_G_LIST_APPEND(expired_req_list, ex);
1052
1053         g_free(req_id);
1054         return NULL;
1055
1056 out:
1057         gvar = g_variant_new("(i)", ret);
1058         g_free(req_id);
1059         if (ex)
1060                 free(ex);
1061         return gvar;
1062 }
1063
1064 static gboolean app_term(gpointer data)
1065 {
1066         struct pmlock_expired_s *ex = data;
1067
1068         if (ex) {
1069                 if (ex->pid > 0 && kill(ex->pid, 0) != -1) {
1070                         if (kill(ex->pid, SIGKILL) == -1)
1071                                 _E("failed to kill pid(%d), err(%m)", ex->pid);
1072                 }
1073                 free(ex);
1074         }
1075         return G_SOURCE_REMOVE;
1076 }
1077
1078 static gboolean expired_deliver_result(gpointer data)
1079 {
1080         struct pmlock_expired_s *ex = data;
1081         struct pmlock_expired_s *item = NULL;
1082         GList *l;
1083         size_t len;
1084         int value;
1085         char *id;
1086
1087         if (!ex)
1088                 return G_SOURCE_REMOVE;
1089
1090         len = strlen(ex->req_id) + 1;
1091         SYS_G_LIST_FOREACH(expired_req_list, l, item) {
1092                 if (!strncmp(item->req_id, ex->req_id, len)) {
1093                         SYS_G_LIST_REMOVE(expired_req_list, item);
1094                         break;
1095                 }
1096         }
1097         if (!item)
1098                 goto out;
1099
1100         id = ex->req_id;
1101         value = ex->value;
1102
1103         _I("User input of req_id(%s) is (%s)", id, value == 0 ? "ALLOW power lock" :
1104                         (value == 2) ? "KILL app" : "Release power lock");
1105
1106         if (!item->invocation) {
1107                 _E("item->invocation is null");
1108                 goto out;
1109         }
1110         g_dbus_method_invocation_return_value(item->invocation, g_variant_new("(i)", 0));
1111
1112         if (value == 2) {
1113                 if (item->pid > 0 && kill(item->pid, SIGTERM) == -1)
1114                         _E("failed to kill pid(%d), err(%m)", item->pid);
1115                 g_timeout_add(3000, app_term, (gpointer)item);
1116                 item = NULL;
1117         }
1118
1119 out:
1120         free(ex);
1121         free(item);
1122
1123         return G_SOURCE_REMOVE;
1124 }
1125
1126 static GVariant *dbus_locktimeout_input(GDBusConnection *conn,
1127         const gchar *sender, const gchar *path, const gchar *iface, const gchar *name,
1128         GVariant *param, GDBusMethodInvocation *invocation, gpointer user_data)
1129 {
1130         int ret;
1131         char *req_id;
1132         struct pmlock_expired_s *ex;
1133         int value;
1134
1135         g_variant_get(param, "(si)", &req_id, &value);
1136
1137         ex = calloc(1, sizeof(struct pmlock_expired_s));
1138         if (!ex) {
1139                 _E("calloc() failed");
1140                 ret = -ENOMEM;
1141                 goto out;
1142         }
1143
1144         snprintf(ex->req_id, sizeof(ex->req_id), "%s", req_id);
1145         ex->value = value;
1146
1147         _I("req_id(%s), value(%d)", ex->req_id, ex->value);
1148
1149         g_idle_add(expired_deliver_result, (gpointer)ex);
1150
1151         ret = 0;
1152
1153 out:
1154         g_free(req_id);
1155         return g_variant_new("(i)", ret);
1156 }
1157
1158 static GVariant *dbus_dimstay_control(GDBusConnection *conn,
1159         const gchar *sender, const gchar *path, const gchar *iface, const gchar *name,
1160         GVariant *param, GDBusMethodInvocation *invocation, gpointer user_data)
1161 {
1162         int dimstay, ret = 0;
1163         pid_t pid;
1164
1165         g_variant_get(param, "(i)", &dimstay);
1166
1167         if (dimstay < 0) {
1168                 _E("Invalid dimstay set value %d.", dimstay);
1169                 ret = -EINVAL;
1170                 goto out;
1171         }
1172
1173         pid = gdbus_connection_get_sender_pid(conn, sender);
1174         if (dimstay) {
1175                 _I("Set DIM_FLAG pid=%d", pid);
1176                 set_pm_status_flag(DIM_FLAG);
1177         } else {
1178                 _I("Unset DIM_FLAG pid=%d", pid);
1179                 clear_pm_status_flag(DIM_FLAG);
1180         }
1181
1182         if (get_pm_cur_state() == S_NORMAL)
1183                 backlight_ops->update();
1184
1185 out:
1186         return g_variant_new("(i)", ret);
1187 }
1188
1189 static GVariant *dbus_getbrightnessinfo(GDBusConnection *conn,
1190         const gchar *sender, const gchar *path, const gchar *iface, const gchar *name,
1191         GVariant *param, GDBusMethodInvocation *invocation, gpointer user_data)
1192 {
1193         int default_brightness;
1194         int current_brightness;
1195
1196         default_brightness = backlight_ops->get_default_brt();
1197         backlight_ops->get_brightness_raw(&current_brightness);
1198
1199         return g_variant_new("(ii)", default_brightness, current_brightness);
1200 }
1201
1202 static GVariant *dbus_setwhitebalance(GDBusConnection *conn,
1203         const gchar *sender, const gchar *path, const gchar *iface, const gchar *name,
1204         GVariant *param, GDBusMethodInvocation *invocation, gpointer user_data)
1205 {
1206         int white_balance_type, value;
1207         int ret = 0, ret_val;
1208         pid_t pid;
1209
1210         g_variant_get(param, "(ii)", &white_balance_type, &value);
1211
1212         ret_val = display_white_balance_ops->set_white_balance(white_balance_type, value);
1213         if (ret_val < 0) {
1214                 _E("Failed to set white balance");
1215                 ret = -EPERM;
1216         }
1217
1218         pid = gdbus_connection_get_sender_pid(conn, sender);
1219         _I("Set white balance pid=%d white balance type=%d value=%d", pid, white_balance_type, value);
1220
1221         return g_variant_new("(i)", ret);
1222 }
1223
1224 static GVariant *dbus_getwhitebalance(GDBusConnection *conn,
1225         const gchar *sender, const gchar *path, const gchar *iface, const gchar *name,
1226         GVariant *param, GDBusMethodInvocation *invocation, gpointer user_data)
1227 {
1228         int white_balance_type;
1229         int ret = 0, ret_val;
1230         pid_t pid;
1231
1232         g_variant_get(param, "(i)", &white_balance_type);
1233
1234         ret = display_white_balance_ops->get_white_balance(white_balance_type, &ret_val);
1235         if (ret < 0) {
1236                 _E("Failed to get white balance");
1237                 ret = -EPERM;
1238         }
1239         else
1240                 ret = ret_val;
1241
1242         pid = gdbus_connection_get_sender_pid(conn, sender);
1243         _I("Get white balance pid=%d white balance type=%d", pid, white_balance_type);
1244
1245         return g_variant_new("(i)", ret);
1246 }
1247
1248 static const dbus_method_s dbus_methods[] = {
1249         { "start",           NULL,  NULL, dbus_start },
1250         { "stop",            NULL,  NULL, dbus_stop },
1251         { "lockstate",     "sssi",   "i", dbus_lockstate },
1252         { "unlockstate",     "ss",   "i", dbus_unlockstate },
1253         { "changestate",      "s",   "i", dbus_changestate },
1254         { "ChangeState",      "s",   "i", dbus_changestate },
1255         { "getbrightness",    "i",   "i", dbus_getbrightness }, /* deprecated */
1256         { "setbrightness",   "ii",   "i", dbus_setbrightness }, /* deprecated */
1257         { "setframerate",    "ii",   "i", dbus_setrefreshrate },        /* deprecated */
1258         { "setautobrightnessmin", "i", "i", dbus_setautobrightnessmin },
1259         { "setlcdtimeout",  "iii",   "i", dbus_setlcdtimeout },
1260         { "LockScreenBgOn",   "s",   "i", dbus_lockscreenbgon },
1261         { "GetDisplayCount", NULL,   "i", dbus_getdisplaycount },
1262         { "GetMaxBrightness", "i",  "i", dbus_getmaxbrightness },
1263         { "SetMaxBrightness", "i",   "i", dbus_setmaxbrightness },
1264         { "GetBrightness",    "i",   "i", dbus_getbrightness },
1265         { "SetBrightness",   "ii",   "i", dbus_setbrightness },
1266         { "HoldBrightness",   "i",   "i", dbus_holdbrightness },
1267         { "ReleaseBrightness", NULL, "i", dbus_releasebrightness },
1268         { "SetRefreshRate",  "ii",   "i", dbus_setrefreshrate },
1269         { "Dumpmode",         "s",   "i", dbus_dumpmode },
1270         { "SaveLog",         NULL,  NULL, dbus_savelog },
1271         { "PowerKeyIgnore",   "i",   "i", dbus_powerkeyignore },
1272         { "PowerKeyLCDOff",  NULL,   "i", dbus_powerkeylcdoff },
1273         { "CustomLCDOn",      "i",   "i", dbus_customlcdon },
1274         { "CustomLCDOff",         "s",   "i", dbus_customlcdoff },
1275         { "ChangeStateByReason", "isi", "i", dbus_changestatebyreason },
1276         { "StayTouchScreenOff", "i", "i", dbus_staytouchscreenoff },
1277         { "LCDPanelOffMode",  "i",   "i", dbus_lcdpaneloffmode },
1278         { "ActorControl",   "sii",   "i", dbus_actorcontrol },
1279         { "CustomBrightness", NULL,  "i", dbus_getcustombrightness },
1280         { "CurrentBrightness", "i", "i", dbus_getbrightness }, /* deprecated. It is remained for tizen 2.4 */
1281         { "LockTimeoutExpired", "s", "i", dbus_locktimeout_expired },
1282         { "LockTimeoutInput",  "si", "i", dbus_locktimeout_input },
1283         { "DimStayControl",        "i",  "i", dbus_dimstay_control },
1284         { "GetBrightnessInfo", NULL, "ii", dbus_getbrightnessinfo},
1285         { "SetWhiteBalance", "ii", "i", dbus_setwhitebalance},
1286         { "GetWhiteBalance", "i", "i", dbus_getwhitebalance},
1287         /* Add methods here */
1288 };
1289
1290 static const dbus_interface_u dbus_interface = {
1291         .oh = NULL,
1292         .name = DEVICED_INTERFACE_DISPLAY,
1293         .methods = dbus_methods,
1294         .nr_methods = ARRAY_SIZE(dbus_methods),
1295 };
1296
1297 static void changestate_signal_handler(GDBusConnection  *conn,
1298                                                                                 const gchar      *sender,
1299                                                                                 const gchar      *path,
1300                                                                                 const gchar      *iface,
1301                                                                                 const gchar      *name,
1302                                                                                 GVariant         *param,
1303                                                                                 gpointer          data)
1304 {
1305         int val = 0;
1306         char *state = NULL;
1307         pid_t pid;
1308
1309         if (!g_variant_get_safe(param, "(issss)", &val, NULL, NULL, &state, NULL)) {
1310                 _E("failed to get params from gvariant. expected:%s, type:%s", "(issss)", g_variant_get_type_string(param));
1311                 goto out;
1312         }
1313
1314         if (state == NULL) {
1315                 _E("state is null.");
1316                 goto out;
1317         }
1318         pid = (pid_t)val;
1319
1320         if (!strcmp(state, "bg")) {
1321                 _D("process(%d) was going background.", pid);
1322                 set_app_state(pid, APPLICATION_BACKGROUND);
1323                 device_notify(DEVICE_NOTIFIER_APPLICATION_BACKGROUND, &pid);
1324         } else if (!strcmp(state, "fg")) {
1325                 _D("process(%d) was going foreground.", pid);
1326                 set_app_state(pid, APPLICATION_FOREGROUND);
1327                 device_notify(DEVICE_NOTIFIER_APPLICATION_FOREGROUND, &pid);
1328         }
1329
1330 out:
1331         if (state)
1332                 g_free(state);
1333 }
1334
1335 static void app_terminate_signal_handler(GDBusConnection  *conn,
1336                                                                                 const gchar      *sender,
1337                                                                                 const gchar      *path,
1338                                                                                 const gchar      *iface,
1339                                                                                 const gchar      *name,
1340                                                                                 GVariant         *param,
1341                                                                                 gpointer          data)
1342 {
1343         pid_t pid;
1344
1345         if (!g_variant_get_safe(param, "(i)", &pid)) {
1346                 _E("failed to get params from gvariant. expected:%s, type:%s", "(i)", g_variant_get_type_string(param));
1347                 return;
1348         }
1349
1350         set_app_state(pid, APPLICATION_TERMINATED);
1351         device_notify(DEVICE_NOTIFIER_APPLICATION_TERMINATED, &pid);
1352 }
1353
1354 /*
1355  * Default capability
1356  * api      := LCDON | LCDOFF | BRIGHTNESS
1357  * gesture  := LCDON
1358  */
1359 static struct display_actor_ops display_api_actor = {
1360         .id     = DISPLAY_ACTOR_API,
1361         .caps   = DISPLAY_CAPA_LCDON |
1362                   DISPLAY_CAPA_LCDOFF |
1363                   DISPLAY_CAPA_BRIGHTNESS,
1364 };
1365
1366 static struct display_actor_ops display_gesture_actor = {
1367         .id     = DISPLAY_ACTOR_GESTURE,
1368         .caps   = DISPLAY_CAPA_LCDON,
1369 };
1370
1371 int init_pm_dbus(void)
1372 {
1373         int ret;
1374
1375         display_add_actor(&display_api_actor);
1376         display_add_actor(&display_gesture_actor);
1377
1378         ret = gdbus_add_object(NULL, DEVICED_PATH_DISPLAY, &dbus_interface);
1379         if (ret < 0)
1380                 _E("fail to init dbus method(%d)", ret);
1381
1382
1383         ret = gdbus_signal_subscribe(NULL,
1384                                 AUL_APPSTATUS_PATH,
1385                                 AUL_APPSTATUS_INTERFACE,
1386                                 APP_CHANGE_STATE,
1387                                 changestate_signal_handler,
1388                                 NULL, NULL);
1389         if (ret <= 0)
1390                 _E("Failed to register signal handler: %d", ret);
1391
1392         ret = gdbus_signal_subscribe(NULL,
1393                         AUL_APPSTATUS_PATH,
1394                         AUL_APPSTATUS_INTERFACE,
1395                         APP_TERMINATED,
1396                         app_terminate_signal_handler,
1397                         NULL, NULL);
1398         if (ret <= 0)
1399                 _E("Failed to register signal handler: %d", ret);
1400
1401         return 0;
1402 }
1403
1404 static void __CONSTRUCTOR__ initialize(void)
1405 {
1406         disp_plgn = get_var_display_plugin();
1407         if (!disp_plgn) {
1408                 _E("Failed to get display plugin variable.");
1409         }
1410
1411         backlight_ops = get_var_backlight_ops();
1412         if (!backlight_ops)
1413                 _E("Failed to get backlight operator variable.");
1414
1415         display_white_balance_ops = get_var_display_white_balance_ops();
1416         if (!display_white_balance_ops)
1417                 _E("Failed to get display white balance operator variable.");
1418
1419         display_conf = get_var_display_config();
1420         if (!display_conf)
1421                 _E("Failed to get display configuration variable.");
1422 }