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