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 <linux/input.h>
30 #include "ambient-mode.h"
34 #include "display-actor.h"
35 #include "display-ops.h"
36 #include "shared/common.h"
37 #include "shared/devices.h"
38 #include "shared/device-notifier.h"
39 #include "shared/common.h"
40 #include "shared/plugin.h"
41 #include "shared/apps.h"
42 #include "power/power-off.h"
43 #include "power/power-suspend.h"
44 #include "led/touch-key.h"
45 #include "display/display-lock.h"
47 #ifndef KEY_SCREENLOCK
48 #define KEY_SCREENLOCK 0x98
54 #define PREDEF_LEAVESLEEP "leavesleep"
55 #define POWEROFF_ACT "poweroff"
56 #define USEC_PER_SEC 1000000
57 #define USEC_PER_MSEC 1000
58 #define CSC_CONFIG_MODE_RUNNING 1
60 #define CAPTURE_COMBINATION_INTERVAL 0.5 /* 0.5 second */
61 #define TORCH_COMBINATION_INTERVAL 0.1 /* 0.1 second */
62 #define DEFAULT_COMBINATION_INTERVAL 0.1 /* 0.1 second */
64 #define LONGKEY_PRESSED_TIME 4 /* 4 second */
66 #define KEY_MAX_DELAY_TIME 700 /* ms */
68 #define SIGNAL_CHANGE_HARDKEY "ChangeHardkey"
69 #define SIGNAL_LCDON_BY_POWERKEY "LCDOnByPowerkey"
70 #define SIGNAL_LCDOFF_BY_POWERKEY "LCDOffByPowerkey"
72 #define NORMAL_POWER 0
73 #define KEY_TEST_MODE_POWER 2
77 enum key_combination_flags {
78 KEY_COMBINATION_STOP = 0,
79 KEY_COMBINATION_POWERKEY = BIT(0),
80 KEY_COMBINATION_MENUKEY = BIT(1),
81 KEY_COMBINATION_VOLUMEUP = BIT(2),
82 KEY_COMBINATION_VOLUMEDOWN = BIT(3),
85 enum combination_process {
86 COMBINATION_STOP = KEY_COMBINATION_STOP,
87 COMBINATION_SCREENCAPTURE = KEY_COMBINATION_POWERKEY | KEY_COMBINATION_MENUKEY,
88 COMBINATION_TORCH = KEY_COMBINATION_POWERKEY | KEY_COMBINATION_VOLUMEUP,
89 COMBINATION_QUICKTALK = KEY_COMBINATION_POWERKEY | KEY_COMBINATION_VOLUMEDOWN,
92 static struct display_plugin *disp_plgn;
93 static struct _backlight_ops *backlight_ops;
94 static struct timeval pressed_time;
95 static guint longkey_timeout_id = 0;
96 static guint longkey_restore_id = 0;
97 static guint displayon_by_powerkey_timeout_id = 0;
98 static int cancel_lcdoff;
99 static int key_combination = KEY_COMBINATION_STOP;
100 static double combination_pressed_time;
101 static bool touch_pressed = false;
102 static int skip_lcd_off = false;
103 static int skip_combination = false;
104 static int bezel_wakeup = true;
105 static int booting_check = true;
107 static inline int current_state_in_on(void)
109 return ((get_pm_cur_state() == S_LCDDIM) || (get_pm_cur_state() == S_NORMAL));
112 static inline void restore_custom_brightness(void)
114 if ((get_pm_cur_state() == S_LCDDIM) &&
115 backlight_ops->get_custom_status())
116 backlight_ops->custom_update();
119 static void longkey_pressed(void)
123 _I("Power key long pressed!");
126 caps = display_get_caps(DISPLAY_ACTOR_POWER_KEY);
128 if (display_has_caps(caps, DISPLAY_CAPA_LCDON)) {
129 /* change state - LCD on */
130 if (disp_plgn->pm_change_internal)
131 disp_plgn->pm_change_internal(INTERNAL_LOCK_POWERKEY, LCD_NORMAL);
132 poll_callback(INPUT_POLL_EVENT, NULL);
135 if (!display_has_caps(caps, DISPLAY_CAPA_LCDOFF)) {
136 _D("No poweroff capability!");
141 static gboolean longkey_restore_cb(void *data)
143 device_notify(DEVICE_NOTIFIER_LONGKEY_RESTORE, (void *)NULL);
144 longkey_restore_id = 0;
146 return G_SOURCE_REMOVE;
149 static gboolean longkey_pressed_cb(void *data)
152 longkey_timeout_id = 0;
154 return G_SOURCE_REMOVE;
157 static unsigned long timediff_usec(struct timeval t1, struct timeval t2)
161 udiff = (t2.tv_sec - t1.tv_sec) * USEC_PER_SEC;
162 udiff += (t2.tv_usec - t1.tv_usec);
167 static inline void check_key_pair(int code, int new, int *old)
170 _E("key pair is not matched! (%d, %d)", code, new);
175 static inline void broadcast_lcdon_by_powerkey(void)
177 gdbus_signal_emit(NULL,
178 DEVICED_PATH_DISPLAY,
179 DEVICED_INTERFACE_DISPLAY,
180 SIGNAL_LCDON_BY_POWERKEY,
184 static inline void broadcast_lcdoff_by_powerkey(void)
186 gdbus_signal_emit(NULL,
187 DEVICED_PATH_DISPLAY,
188 DEVICED_INTERFACE_DISPLAY,
189 SIGNAL_LCDOFF_BY_POWERKEY,
193 static inline bool switch_on_lcd(enum device_flags flags)
197 ret = is_lcdon_blocked();
198 if (ret != LCDON_BLOCK_NONE) {
199 _W("LCDON is blocked, %d.", ret);
203 if (current_state_in_on())
206 if (backlight_ops->get_lcd_power() == DPMS_ON) {
207 if (ambient_get_state() == false)
211 if (flags & LCD_ON_BY_POWER_KEY)
212 broadcast_lcdon_by_powerkey();
213 else if (flags & LCD_ON_BY_TOUCH)
214 _I("Display on by Touch_wakeup event");
216 lcd_on_direct(flags);
221 static inline void switch_off_lcd(void)
223 if (!current_state_in_on())
226 if (backlight_ops->get_lcd_power() == DPMS_OFF)
229 broadcast_lcdoff_by_powerkey();
231 lcd_off_procedure(LCD_OFF_BY_POWER_KEY);
234 static void check_key_combination(struct input_event *pinput)
236 double press_time, diff_time;
237 press_time = (pinput->time).tv_sec + USEC_TO_SEC((pinput->time).tv_usec);
238 diff_time = press_time - combination_pressed_time;
240 switch (key_combination) {
241 case COMBINATION_SCREENCAPTURE:
242 if (diff_time <= CAPTURE_COMBINATION_INTERVAL) {
243 _I("Combination key : SCREENCAPTURE mode");
244 skip_combination = true;
247 case COMBINATION_TORCH:
248 if (diff_time <= TORCH_COMBINATION_INTERVAL) {
249 /* When torch combination, display control should be not change. */
250 if (displayon_by_powerkey_timeout_id) {
251 g_source_remove(displayon_by_powerkey_timeout_id);
252 displayon_by_powerkey_timeout_id = 0;
254 _I("Combination key : TORCH mode");
255 skip_combination = true;
257 key_combination = COMBINATION_STOP;
259 case COMBINATION_QUICKTALK:
260 if (diff_time <= DEFAULT_COMBINATION_INTERVAL) {
261 _I("Combination key : QUICK-TALK mode");
262 skip_combination = true;
263 if (longkey_timeout_id) {
264 g_source_remove(longkey_timeout_id);
265 longkey_timeout_id = 0;
270 combination_pressed_time = press_time;
276 static void start_key_combination(struct input_event *pinput)
278 switch (pinput->code) {
280 key_combination |= KEY_COMBINATION_POWERKEY;
283 key_combination |= KEY_COMBINATION_MENUKEY;
286 key_combination |= KEY_COMBINATION_VOLUMEUP;
289 key_combination |= KEY_COMBINATION_VOLUMEDOWN;
295 check_key_combination(pinput);
298 static void stop_key_combination(struct input_event *pinput)
300 if (pinput == NULL) {
301 key_combination = KEY_COMBINATION_STOP;
305 switch (pinput->code) {
307 key_combination &= ~KEY_COMBINATION_POWERKEY;
310 key_combination &= ~KEY_COMBINATION_MENUKEY;
313 key_combination &= ~KEY_COMBINATION_VOLUMEUP;
316 key_combination &= ~KEY_COMBINATION_VOLUMEDOWN;
319 _E("This code(%d) is not combination type.", pinput->code);
324 static void process_combination_key(struct input_event *pinput)
326 if (pinput->value == KEY_PRESSED)
327 start_key_combination(pinput);
328 else if (pinput->value == KEY_RELEASED)
329 stop_key_combination(pinput);
332 static int process_menu_key(struct input_event *pinput)
336 caps = display_get_caps(DISPLAY_ACTOR_MENU_KEY);
338 if (!display_has_caps(caps, DISPLAY_CAPA_LCDON)) {
339 if (current_state_in_on())
341 _D("No lcd-on capability!");
343 } else if (pinput->value == KEY_PRESSED)
344 switch_on_lcd(LCD_ON_BY_POWER_KEY);
349 static int decide_lcdoff(void)
351 /* It's not needed if it's already LCD off state */
352 if (!current_state_in_on() &&
353 backlight_ops->get_lcd_power() != DPMS_ON)
357 * This flag is set at the moment
358 * that LCD is turned on by power key
359 * LCD has not to turned off in the situation.
364 /* LCD is not turned off when powerkey is pressed,not released */
365 if (key_combination == KEY_COMBINATION_POWERKEY)
368 /* LCD-off is blocked at the moment poweroff popup shows */
372 /* LCD-off is blocked when powerkey and volmedown key are pressed */
373 if (skip_combination)
376 /* At booting time, display must do not turn off */
383 static int lcdoff_powerkey(void)
387 if (decide_lcdoff() == true) {
388 check_processes(S_NORMAL);
389 check_processes(S_LCDDIM);
391 if (!check_holdkey_block(S_NORMAL) &&
392 !check_holdkey_block(S_LCDDIM)) {
393 if (display_info.update_auto_brightness)
394 display_info.update_auto_brightness(false);
396 delete_condition(S_NORMAL);
397 delete_condition(S_LCDDIM);
398 update_lcdoff_source(VCONFKEY_PM_LCDOFF_BY_POWERKEY);
399 if (disp_plgn->pm_change_internal)
400 disp_plgn->pm_change_internal(INTERNAL_LOCK_POWERKEY, LCD_OFF);
404 skip_combination = false;
411 static int process_back_key(struct input_event *pinput)
415 if (pinput->value == KEY_PRESSED) {
416 switch_on_lcd(LCD_ON_BY_BACK_KEY);
417 _I("back key pressed");
424 static int process_power_key(struct input_event *pinput)
427 static int value = KEY_RELEASED;
429 const struct display_config *display_conf = get_var_display_config();
431 _E("Failed to get display configuration variable.");
435 caps = display_get_caps(DISPLAY_ACTOR_POWER_KEY);
437 switch (pinput->value) {
439 check_key_pair(pinput->code, pinput->value, &value);
441 if (!display_conf->powerkey_doublepress) {
442 if (display_has_caps(caps, DISPLAY_CAPA_LCDOFF))
445 _D("No lcdoff capability!");
446 } else if (skip_lcd_off)
449 if (!display_has_caps(caps, DISPLAY_CAPA_LCDON))
452 if (longkey_timeout_id > 0) {
453 g_source_remove(longkey_timeout_id);
454 longkey_timeout_id = 0;
457 if (longkey_restore_id > 0) {
458 g_source_remove(longkey_restore_id);
459 longkey_restore_id = 0;
464 if (display_has_caps(caps, DISPLAY_CAPA_LCDON)) {
465 skip_lcd_off = switch_on_lcd(LCD_ON_BY_POWER_KEY);
467 _D("No lcdon capability!");
468 skip_lcd_off = false;
470 check_key_pair(pinput->code, pinput->value, &value);
471 _I("power key pressed");
472 pressed_time.tv_sec = (pinput->time).tv_sec;
473 pressed_time.tv_usec = (pinput->time).tv_usec;
474 if (key_combination == KEY_COMBINATION_POWERKEY) {
475 /* add long key timer */
476 longkey_timeout_id = g_timeout_add(
477 display_conf->longpress_interval,
478 longkey_pressed_cb, NULL);
479 /* add long key restore timer */
480 longkey_restore_id = g_timeout_add_seconds(
481 LONGKEY_PRESSED_TIME,
482 longkey_restore_cb, NULL);
487 case KEY_BEING_PRESSED:
488 if (timediff_usec(pressed_time, pinput->time) >
489 (display_conf->longpress_interval * USEC_PER_MSEC))
496 static int process_screenlock_key(struct input_event *pinput)
498 if (pinput->value != KEY_RELEASED) {
499 stop_key_combination(NULL);
503 if (!current_state_in_on())
506 check_processes(S_NORMAL);
507 check_processes(S_LCDDIM);
509 if (!check_holdkey_block(S_NORMAL) && !check_holdkey_block(S_LCDDIM)) {
510 delete_condition(S_NORMAL);
511 delete_condition(S_LCDDIM);
512 update_lcdoff_source(VCONFKEY_PM_LCDOFF_BY_POWERKEY);
515 if (disp_plgn->pm_change_internal)
516 disp_plgn->pm_change_internal(-1, LCD_OFF);
522 static void update_vital_state(struct input_event *pinput)
526 /* Change vital state to VITAL_EXIT only if vital mode is active */
530 /* Touch or Menu Key Release Event */
531 if (pinput->type == EV_ABS || (pinput->type == EV_KEY &&
532 pinput->value == KEY_RELEASED && pinput->code == KEY_MENU)) {
533 /* Enable all services upon receiving user input, else maintain same state */
535 device_notify(DEVICE_NOTIFIER_VITAL_STATE, &type);
539 static int check_key(struct input_event *pinput, int fd)
543 process_combination_key(pinput);
544 switch (pinput->code) {
546 ignore = process_menu_key(pinput);
549 ignore = process_power_key(pinput);
550 if (current_state_in_on())
554 ignore = process_screenlock_key(pinput);
557 ignore = process_back_key(pinput);
558 stop_key_combination(NULL);
559 if (current_state_in_on())
563 stop_key_combination(NULL);
564 if (current_state_in_on())
569 if (current_state_in_on())
582 case KEY_PREVIOUSSONG:
584 case KEY_FASTFORWARD:
585 stop_key_combination(NULL);
586 if (current_state_in_on())
593 stop_key_combination(NULL);
596 stop_key_combination(NULL);
600 if (pinput->value == KEY_PRESSED)
601 pm_history_save(PM_LOG_KEY_PRESS, pinput->code);
602 else if (pinput->value == KEY_RELEASED)
603 pm_history_save(PM_LOG_KEY_RELEASE, pinput->code);
608 static int check_key_filter(void *data, int fd)
610 struct input_event *pinput = data;
612 static int old_fd, code, value;
616 switch (pinput->type) {
618 if (pinput->code == BTN_TOUCH &&
619 pinput->value == KEY_RELEASED)
620 touch_pressed = false;
622 * Normally, touch press/release events don't occur
623 * in lcd off state. But touch release events can occur
624 * in the state abnormally. Then touch events are ignored
625 * when lcd is off state.
627 if (pinput->code == BTN_TOUCH && !current_state_in_on())
629 if (pinput->code == code && pinput->value == value) {
630 _E("Same key(%d, %d) is polled [%d,%d]",
631 code, value, old_fd, fd);
635 value = pinput->value;
637 update_vital_state(pinput);
638 ignore = check_key(pinput, fd);
639 restore_custom_brightness();
643 if ((get_pm_cur_state() == S_LCDOFF) && bezel_wakeup) {
644 switch_on_lcd(LCD_ON_BY_BEZEL);
646 } else if (get_pm_cur_state() != S_LCDOFF)
650 update_vital_state(pinput);
651 if (pinput->value == KEY_PRESSED) {
652 switch_on_lcd(LCD_ON_BY_TOUCH);
656 if (current_state_in_on())
659 restore_custom_brightness();
661 if (pinput->value == KEY_PRESSED)
662 touch_pressed = true;
663 else if (pinput->value == KEY_RELEASED)
664 touch_pressed = false;
676 static int delayed_init_done(void *data)
683 static int bezel_wakeup_cb(void *data)
685 bezel_wakeup = (int)((intptr_t)data);
692 * powerkey := LCDON | LCDOFF | POWEROFF
695 static struct display_actor_ops display_powerkey_actor = {
696 .id = DISPLAY_ACTOR_POWER_KEY,
697 .caps = DISPLAY_CAPA_LCDON |
698 DISPLAY_CAPA_LCDOFF |
699 DISPLAY_CAPA_POWEROFF,
702 static struct display_actor_ops display_menukey_actor = {
703 .id = DISPLAY_ACTOR_MENU_KEY,
704 .caps = DISPLAY_CAPA_LCDON,
707 static void keyfilter_init(void)
709 display_add_actor(&display_powerkey_actor);
710 display_add_actor(&display_menukey_actor);
712 register_notifier(DEVICE_NOTIFIER_DELAYED_INIT, delayed_init_done);
713 register_notifier(DEVICE_NOTIFIER_BEZEL_WAKEUP, bezel_wakeup_cb);
716 static const struct display_keyfilter_ops normal_keyfilter_ops = {
717 .init = keyfilter_init,
718 .check = check_key_filter,
719 .set_powerkey_ignore = NULL,
720 .powerkey_lcdoff = NULL,
721 .backlight_enable = NULL,
724 const struct display_keyfilter_ops *keyfilter_ops = &normal_keyfilter_ops;
726 static void __CONSTRUCTOR__ initialize(void)
728 disp_plgn = get_var_display_plugin();
730 _E("Failed to get display plugin variable.");
732 backlight_ops = get_var_backlight_ops();
734 _E("Failed to get backlight operator variable.");