4 * Copyright (c) 2012 - 2013 Samsung Electronics Co., Ltd.
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
26 #include <sys/types.h>
27 #include <libsyscommon/libgdbus.h>
28 #include <libsyscommon/resource-manager.h>
29 #include <system/syscommon-plugin-deviced-common-interface.h>
30 #include <system/syscommon-plugin-deviced-display-interface.h>
31 #include <linux/input.h>
35 #include "device-interface.h"
36 #include "display-actor.h"
37 #include "display-panel.h"
38 #include "display-backlight.h"
39 #include "display-ops.h"
40 #include "display-config.h"
41 #include "display-misc.h"
42 #include "display-state-transition.h"
43 #include "display-util.h"
44 #include "shared/common.h"
45 #include "shared/devices.h"
46 #include "shared/device-notifier.h"
47 #include "shared/common.h"
48 #include "shared/apps.h"
49 #include "shared/log.h"
50 #include "power/power-off.h"
51 #include "power/power-suspend.h"
52 #include "led/touch-key.h"
53 #include "display-lock.h"
54 #include "input/input.h"
56 #ifndef KEY_SCREENLOCK
57 #define KEY_SCREENLOCK 0x98
63 #define USEC_PER_SEC 1000000
65 #define CAPTURE_COMBINATION_INTERVAL 0.5 /* 0.5 second */
66 #define TORCH_COMBINATION_INTERVAL 0.1 /* 0.1 second */
67 #define DEFAULT_COMBINATION_INTERVAL 0.1 /* 0.1 second */
69 #define LONGKEY_PRESSED_TIME 4 /* 4 second */
71 #define SIGNAL_CHANGE_HARDKEY "ChangeHardkey"
72 #define SIGNAL_LCDON_BY_POWERKEY "LCDOnByPowerkey"
73 #define SIGNAL_LCDOFF_BY_POWERKEY "LCDOffByPowerkey"
75 enum key_combination_flags {
76 KEY_COMBINATION_STOP = 0,
77 KEY_COMBINATION_POWERKEY = BIT(0),
78 KEY_COMBINATION_MENUKEY = BIT(1),
79 KEY_COMBINATION_VOLUMEUP = BIT(2),
80 KEY_COMBINATION_VOLUMEDOWN = BIT(3),
83 enum combination_process {
84 COMBINATION_STOP = KEY_COMBINATION_STOP,
85 COMBINATION_SCREENCAPTURE = KEY_COMBINATION_POWERKEY | KEY_COMBINATION_MENUKEY,
86 COMBINATION_TORCH = KEY_COMBINATION_POWERKEY | KEY_COMBINATION_VOLUMEUP,
87 COMBINATION_QUICKTALK = KEY_COMBINATION_POWERKEY | KEY_COMBINATION_VOLUMEDOWN,
90 static struct timeval pressed_time;
91 static guint longkey_timeout_id = 0;
92 static guint longkey_restore_id = 0;
93 static guint displayon_by_powerkey_timeout_id = 0;
94 static int cancel_lcdoff;
95 static int key_combination = KEY_COMBINATION_STOP;
96 static double combination_pressed_time;
97 static bool touch_pressed = false;
98 static int skip_lcd_off = false;
99 static int skip_combination = false;
100 static int bezel_wakeup = true;
101 static int booting_check = true;
103 static inline int current_state_in_on(void)
106 enum deviced_display_state current;
108 ret = syscommon_resman_get_resource_attr_int(SYSCOMMON_RESOURCE_ID(DEVICED_RESOURCE_TYPE_DISPLAY),
109 DEVICED_DISPLAY_ATTR_INT_GET_CURRENT_STATE, (int32_t *) ¤t);
113 return ((current == DEVICED_DISPLAY_STATE_DIM) || (current == DEVICED_DISPLAY_STATE_ON));
116 static inline void restore_custom_brightness(void)
120 enum deviced_display_state current;
122 ret = syscommon_resman_get_resource_attr_int(SYSCOMMON_RESOURCE_ID(DEVICED_RESOURCE_TYPE_DISPLAY),
123 DEVICED_DISPLAY_ATTR_INT_GET_CURRENT_STATE, (int32_t *) ¤t);
127 display_backlight_get_custom_status(&custom_status);
128 if (current == DEVICED_DISPLAY_STATE_DIM && custom_status)
129 display_backlight_update_by_custom_brightness();
132 static void pwroff_popup(void)
136 ret = launch_system_app(APP_POWERKEY, 2, APP_KEY_TYPE, APP_POWERKEY);
138 _E("Failed to launch power off popup.");
141 static void longkey_pressed(void)
145 _I("Power key long pressed!");
148 caps = display_get_caps(DISPLAY_ACTOR_POWER_KEY);
150 if (display_has_caps(caps, DISPLAY_CAPA_LCDON)) {
151 /* change state - LCD on */
152 syscommon_resman_set_resource_attr_uint64_2(SYSCOMMON_RESOURCE_ID(DEVICED_RESOURCE_TYPE_DISPLAY),
153 DEVICED_DISPLAY_ATTR_TUPLE2_SET_CURRENT_STATE,
154 DEVICED_DISPLAY_STATE_ON, DEVICED_EVENT_INPUT_POWERKEY);
157 if (!display_has_caps(caps, DISPLAY_CAPA_LCDOFF)) {
158 _D("No poweroff capability!");
165 static gboolean longkey_restore_cb(void *data)
167 syscommon_notifier_emit_notify(DEVICED_NOTIFIER_LONGKEY_RESTORE, (void *)NULL);
168 longkey_restore_id = 0;
170 return G_SOURCE_REMOVE;
173 static gboolean longkey_pressed_cb(void *data)
176 longkey_timeout_id = 0;
178 return G_SOURCE_REMOVE;
181 static unsigned long timediff_usec(struct timeval t1, struct timeval t2)
185 udiff = (t2.tv_sec - t1.tv_sec) * USEC_PER_SEC;
186 udiff += (t2.tv_usec - t1.tv_usec);
191 static inline void check_key_pair(int code, int new, int *old)
194 _E("key pair is not matched! (%d, %d)", code, new);
199 static inline void broadcast_lcdon_by_powerkey(void)
201 gdbus_signal_emit(NULL,
202 DEVICED_PATH_DISPLAY,
203 DEVICED_INTERFACE_DISPLAY,
204 SIGNAL_LCDON_BY_POWERKEY,
208 static inline void broadcast_lcdoff_by_powerkey(void)
210 gdbus_signal_emit(NULL,
211 DEVICED_PATH_DISPLAY,
212 DEVICED_INTERFACE_DISPLAY,
213 SIGNAL_LCDOFF_BY_POWERKEY,
217 static inline bool switch_on_lcd(enum device_flags flags)
219 if (current_state_in_on())
222 if (display_panel_get_dpms_cached_state() == DPMS_ON)
225 if (flags & LCD_ON_BY_POWER_KEY)
226 broadcast_lcdon_by_powerkey();
227 else if (flags & LCD_ON_BY_TOUCH)
228 _I("Display on by Touch_wakeup event");
230 display_panel_lcd_on_direct(flags);
235 static inline void switch_off_lcd(void)
237 if (!current_state_in_on())
240 if (display_panel_get_dpms_cached_state() == DPMS_OFF)
243 broadcast_lcdoff_by_powerkey();
245 display_panel_lcd_off_procedure(LCD_OFF_BY_POWER_KEY);
248 static void check_key_combination(struct input_event *pinput)
250 double press_time, diff_time;
251 press_time = (pinput->time).tv_sec + USEC_TO_SEC((pinput->time).tv_usec);
252 diff_time = press_time - combination_pressed_time;
254 switch (key_combination) {
255 case COMBINATION_SCREENCAPTURE:
256 if (diff_time <= CAPTURE_COMBINATION_INTERVAL) {
257 _I("Combination key : SCREENCAPTURE mode");
258 skip_combination = true;
261 case COMBINATION_TORCH:
262 if (diff_time <= TORCH_COMBINATION_INTERVAL) {
263 /* When torch combination, display control should be not change. */
264 if (displayon_by_powerkey_timeout_id) {
265 g_source_remove(displayon_by_powerkey_timeout_id);
266 displayon_by_powerkey_timeout_id = 0;
268 _I("Combination key : TORCH mode");
269 skip_combination = true;
271 key_combination = COMBINATION_STOP;
273 case COMBINATION_QUICKTALK:
274 if (diff_time <= DEFAULT_COMBINATION_INTERVAL) {
275 _I("Combination key : QUICK-TALK mode");
276 skip_combination = true;
277 if (longkey_timeout_id) {
278 g_source_remove(longkey_timeout_id);
279 longkey_timeout_id = 0;
284 combination_pressed_time = press_time;
290 static void start_key_combination(struct input_event *pinput)
292 switch (pinput->code) {
294 key_combination |= KEY_COMBINATION_POWERKEY;
297 key_combination |= KEY_COMBINATION_MENUKEY;
300 key_combination |= KEY_COMBINATION_VOLUMEUP;
303 key_combination |= KEY_COMBINATION_VOLUMEDOWN;
309 check_key_combination(pinput);
312 static void stop_key_combination(struct input_event *pinput)
314 if (pinput == NULL) {
315 key_combination = KEY_COMBINATION_STOP;
319 switch (pinput->code) {
321 key_combination &= ~KEY_COMBINATION_POWERKEY;
324 key_combination &= ~KEY_COMBINATION_MENUKEY;
327 key_combination &= ~KEY_COMBINATION_VOLUMEUP;
330 key_combination &= ~KEY_COMBINATION_VOLUMEDOWN;
333 _E("This code(%d) is not combination type.", pinput->code);
338 static void process_combination_key(struct input_event *pinput)
340 if (pinput->value == KEY_PRESSED)
341 start_key_combination(pinput);
342 else if (pinput->value == KEY_RELEASED)
343 stop_key_combination(pinput);
346 static int process_menu_key(struct input_event *pinput)
350 caps = display_get_caps(DISPLAY_ACTOR_MENU_KEY);
352 if (!display_has_caps(caps, DISPLAY_CAPA_LCDON)) {
353 if (current_state_in_on())
355 _D("No lcd-on capability!");
357 } else if (pinput->value == KEY_PRESSED)
358 switch_on_lcd(LCD_ON_BY_POWER_KEY);
363 static int decide_lcdoff(void)
365 /* It's not needed if it's already LCD off state */
366 if (!current_state_in_on() &&
367 display_panel_get_dpms_cached_state() != DPMS_ON)
371 * This flag is set at the moment
372 * that LCD is turned on by power key
373 * LCD has not to turned off in the situation.
378 /* LCD is not turned off when powerkey is pressed,not released */
379 if (key_combination == KEY_COMBINATION_POWERKEY)
382 /* LCD-off is blocked at the moment poweroff popup shows */
386 /* LCD-off is blocked when powerkey and volmedown key are pressed */
387 if (skip_combination)
390 /* At booting time, display must do not turn off */
397 static int lcdoff_powerkey(void)
401 if (decide_lcdoff() == true) {
403 display_lock_release_lock_all(DEVICED_DISPLAY_STATE_ON);
404 display_lock_release_lock_all(DEVICED_DISPLAY_STATE_DIM);
405 display_state_transition_update_lcdoff_reason(VCONFKEY_PM_LCDOFF_BY_POWERKEY);
406 syscommon_resman_set_resource_attr_uint64_2(SYSCOMMON_RESOURCE_ID(DEVICED_RESOURCE_TYPE_DISPLAY),
407 DEVICED_DISPLAY_ATTR_TUPLE2_SET_CURRENT_STATE,
408 DEVICED_DISPLAY_STATE_OFF, DEVICED_EVENT_INPUT_POWERKEY);
411 skip_combination = false;
418 static int process_back_key(struct input_event *pinput)
422 if (pinput->value == KEY_PRESSED) {
423 switch_on_lcd(LCD_ON_BY_BACK_KEY);
424 _I("back key pressed");
431 static int process_power_key(struct input_event *pinput)
434 static int value = KEY_RELEASED;
436 const struct display_config *display_conf = get_var_display_config();
438 _E("Failed to get display configuration variable.");
442 caps = display_get_caps(DISPLAY_ACTOR_POWER_KEY);
444 switch (pinput->value) {
446 check_key_pair(pinput->code, pinput->value, &value);
448 if (!display_conf->powerkey_doublepress) {
449 if (display_has_caps(caps, DISPLAY_CAPA_LCDOFF))
452 _D("No lcdoff capability!");
453 } else if (skip_lcd_off)
456 if (!display_has_caps(caps, DISPLAY_CAPA_LCDON))
459 if (longkey_timeout_id > 0) {
460 g_source_remove(longkey_timeout_id);
461 longkey_timeout_id = 0;
464 if (longkey_restore_id > 0) {
465 g_source_remove(longkey_restore_id);
466 longkey_restore_id = 0;
471 if (display_has_caps(caps, DISPLAY_CAPA_LCDON)) {
472 skip_lcd_off = switch_on_lcd(LCD_ON_BY_POWER_KEY);
474 _D("No lcdon capability!");
475 skip_lcd_off = false;
477 check_key_pair(pinput->code, pinput->value, &value);
478 _I("power key pressed");
479 pressed_time.tv_sec = (pinput->time).tv_sec;
480 pressed_time.tv_usec = (pinput->time).tv_usec;
481 if (key_combination == KEY_COMBINATION_POWERKEY) {
482 /* add long key timer */
483 longkey_timeout_id = g_timeout_add_seconds(
484 display_conf->longpress_interval,
485 longkey_pressed_cb, NULL);
486 /* add long key restore timer */
487 longkey_restore_id = g_timeout_add_seconds(
488 LONGKEY_PRESSED_TIME,
489 longkey_restore_cb, NULL);
494 case KEY_BEING_PRESSED:
495 if (timediff_usec(pressed_time, pinput->time) >
496 (display_conf->longpress_interval * USEC_PER_SEC))
503 static int process_screenlock_key(struct input_event *pinput)
505 if (pinput->value != KEY_RELEASED) {
506 stop_key_combination(NULL);
510 if (!current_state_in_on())
513 display_lock_release_lock_all(DEVICED_DISPLAY_STATE_ON);
514 display_lock_release_lock_all(DEVICED_DISPLAY_STATE_DIM);
515 display_state_transition_update_lcdoff_reason(VCONFKEY_PM_LCDOFF_BY_POWERKEY);
518 syscommon_resman_set_resource_attr_uint64_2(SYSCOMMON_RESOURCE_ID(DEVICED_RESOURCE_TYPE_DISPLAY),
519 DEVICED_DISPLAY_ATTR_TUPLE2_SET_CURRENT_STATE,
520 DEVICED_DISPLAY_STATE_OFF, DEVICED_EVENT_INPUT_POWERKEY);
525 static void sound_vibrate_hardkey(void)
527 /* device notify(vibrator) */
529 /* Need to notify to deviced-vibrator. deviced-vibrator receives ChangedHardKey signal */
530 gdbus_signal_emit(NULL,
532 DEVICED_INTERFACE_KEY,
533 SIGNAL_CHANGE_HARDKEY,
537 static void process_hardkey_backlight(struct input_event *pinput)
539 _E("pinput->value : %d", pinput->value);
540 if (pinput->value == KEY_PRESSED) {
541 /* Sound & Vibrate only in unlock state */
542 if (__get_lock_screen_state() == VCONFKEY_IDLE_UNLOCK
543 || get_lock_screen_bg_state())
544 sound_vibrate_hardkey();
546 touchled_control_backlight(TOUCHLED_PRESS);
547 } else if (pinput->value == KEY_RELEASED) {
548 /* if lockscreen is idle lock */
549 if (__get_lock_screen_state() == VCONFKEY_IDLE_LOCK) {
550 _D("Lock state, key backlight is off when phone is unlocked!");
554 touchled_control_backlight(TOUCHLED_RELEASE);
558 static void update_vital_state(struct input_event *pinput)
562 /* Change vital state to VITAL_EXIT only if vital mode is active */
566 /* Touch or Menu Key Release Event */
567 if (pinput->type == EV_ABS || (pinput->type == EV_KEY &&
568 pinput->value == KEY_RELEASED && pinput->code == KEY_MENU)) {
569 /* Enable all services upon receiving user input, else maintain same state */
571 syscommon_notifier_emit_notify(DEVICED_NOTIFIER_VITAL_STATE, &type);
575 static int check_key(struct input_event *pinput)
579 process_combination_key(pinput);
580 switch (pinput->code) {
582 ignore = process_menu_key(pinput);
585 ignore = process_power_key(pinput);
586 if (current_state_in_on())
590 ignore = process_screenlock_key(pinput);
593 ignore = process_back_key(pinput);
594 stop_key_combination(NULL);
595 if (current_state_in_on()) {
596 process_hardkey_backlight(pinput);
601 stop_key_combination(NULL);
602 if (current_state_in_on()) {
603 process_hardkey_backlight(pinput);
609 if (current_state_in_on())
622 case KEY_PREVIOUSSONG:
624 case KEY_FASTFORWARD:
625 stop_key_combination(NULL);
626 if (current_state_in_on())
633 stop_key_combination(NULL);
636 stop_key_combination(NULL);
640 if (pinput->value == KEY_PRESSED)
641 pm_history_save(PM_LOG_KEY_PRESS, pinput->code);
642 else if (pinput->value == KEY_RELEASED)
643 pm_history_save(PM_LOG_KEY_RELEASE, pinput->code);
648 static void check_key_filter(struct timeval time, unsigned short type, unsigned short keycode, unsigned int keyvalue)
650 struct input_event *pinput = &(struct input_event) {
657 static int code, value;
659 enum deviced_display_state current;
663 switch (pinput->type) {
665 if (pinput->code == BTN_TOUCH &&
666 pinput->value == KEY_RELEASED)
667 touch_pressed = false;
669 * Normally, touch press/release events don't occur
670 * in lcd off state. But touch release events can occur
671 * in the state abnormally. Then touch events are ignored
672 * when lcd is off state.
674 if (pinput->code == BTN_TOUCH && !current_state_in_on())
676 if (pinput->code == code && pinput->value == value) {
677 _E("Same key(%d, %d) is polled", code, value);
679 gdbus_signal_emit(NULL,
681 DEVICED_INTERFACE_INPUT,
683 g_variant_new("(iiii)", pinput->code, pinput->value, pinput->time.tv_sec, pinput->time.tv_usec));
686 value = pinput->value;
688 update_vital_state(pinput);
689 ignore = check_key(pinput);
690 restore_custom_brightness();
694 ret = syscommon_resman_get_resource_attr_int(SYSCOMMON_RESOURCE_ID(DEVICED_RESOURCE_TYPE_DISPLAY),
695 DEVICED_DISPLAY_ATTR_INT_GET_CURRENT_STATE, (int32_t *) ¤t);
699 if (current == DEVICED_DISPLAY_STATE_OFF && bezel_wakeup) {
700 switch_on_lcd(LCD_ON_BY_BEZEL);
702 } else if (current != DEVICED_DISPLAY_STATE_OFF)
706 if (display_misc_is_touch_event_blocked()
707 && !g_display_plugin.config->touch_wakeup
708 && pinput->value == KEY_BEING_PRESSED)
711 update_vital_state(pinput);
712 if (pinput->value == KEY_PRESSED) {
713 switch_on_lcd(LCD_ON_BY_TOUCH);
717 if (current_state_in_on())
720 restore_custom_brightness();
722 if (pinput->value == KEY_PRESSED)
723 touch_pressed = true;
724 else if (pinput->value == KEY_RELEASED)
725 touch_pressed = false;
734 /* lcd on or update lcd timeout */
735 syscommon_resman_set_resource_attr_uint64_2(SYSCOMMON_RESOURCE_ID(DEVICED_RESOURCE_TYPE_DISPLAY),
736 DEVICED_DISPLAY_ATTR_TUPLE2_SET_CURRENT_STATE,
737 DEVICED_DISPLAY_STATE_ON, DEVICED_EVENT_INPUT);
740 static int delayed_init_done(void *data)
747 static int bezel_wakeup_cb(void *data)
749 bezel_wakeup = (int)((intptr_t)data);
756 * powerkey := LCDON | LCDOFF | POWEROFF
759 static struct display_actor_ops display_powerkey_actor = {
760 .id = DISPLAY_ACTOR_POWER_KEY,
761 .caps = DISPLAY_CAPA_LCDON |
762 DISPLAY_CAPA_LCDOFF |
763 DISPLAY_CAPA_POWEROFF,
766 static struct display_actor_ops display_menukey_actor = {
767 .id = DISPLAY_ACTOR_MENU_KEY,
768 .caps = DISPLAY_CAPA_LCDON,
771 static void __CONSTRUCTOR__ initialize(void)
773 display_add_actor(&display_powerkey_actor);
774 display_add_actor(&display_menukey_actor);
776 syscommon_notifier_subscribe_notify(DEVICED_NOTIFIER_DELAYED_INIT, delayed_init_done);
777 syscommon_notifier_subscribe_notify(DEVICED_NOTIFIER_BEZEL_WAKEUP, bezel_wakeup_cb);
779 input_register_event_callback(check_key_filter, NULL, NULL, NULL);