forked from nemomobile/mce
-
Notifications
You must be signed in to change notification settings - Fork 0
/
powerkey.c
714 lines (610 loc) · 17.4 KB
/
powerkey.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
/**
* @file powerkey.c
* Power key logic for the Mode Control Entity
* <p>
* Copyright © 2004-2011 Nokia Corporation and/or its subsidiary(-ies).
* <p>
* @author David Weinehall <[email protected]>
*
* mce is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License
* version 2.1 as published by the Free Software Foundation.
*
* mce is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with mce. If not, see <http://www.gnu.org/licenses/>.
*/
#include <glib.h>
#include <stdlib.h> /* exit(), EXIT_FAILURE */
#include <string.h> /* strcmp() */
#include <linux/input.h> /* struct input_event */
#include "mce.h" /* mce_get_submode_int32(),
* mce_add_submode_int32(),
* mce_rem_submode_int32(),
* submode_pipe,
* system_state_pipe,
* tk_lock_pipe,
* keypress_pipe,
* system_state_t,
* submode_t
*/
#include "powerkey.h"
#include "mce-log.h" /* mce_log(), LL_* */
#include "mce-conf.h" /* mce_conf_get_int(),
* mce_conf_get_string()
*/
#include "mce-dbus.h" /* mce_dbus_handler_add(),
* dbus_send(),
* dbus_send_message(),
* dbus_new_method_reply(),
* dbus_message_get_no_reply(),
* dbus_message_iter_init(),
* dbus_message_iter_get_arg_type(),
* dbus_message_iter_get_basic(),
* dbus_message_iter_next(),
* dbus_error_init(),
* dbus_error_free(),
* DBUS_MESSAGE_TYPE_METHOD_CALL,
* DBUS_TYPE_BOOLEAN,
* DBUS_TYPE_UINT32,
* DBUS_TYPE_INVALID,
* DBusMessage, DBusMessageIter,
* DBusError,
* dbus_bool_t,
* dbus_uint32_t
*/
#include "mce-dsme.h" /* request_normal_shutdown(),
* request_soft_poweron(),
* request_soft_poweroff(),
* request_powerup(),
* request_reboot()
*/
#include "datapipe.h" /* execute_datapipe(),
* execute_datapipipe_output_triggers(),
* datapipe_get_gint(),
* append_input_trigger_to_datapipe(),
* remove_input_trigger_from_datapipe()
*/
/**
* The ID of the timeout used when determining
* whether the key press was short or long
*/
static guint powerkey_timeout_cb_id = 0;
/**
* The ID of the timeout used when determining
* whether the key press was a double press
*/
static guint doublepress_timeout_cb_id = 0;
/** Time in milliseconds before the key press is considered medium */
static gint mediumdelay = DEFAULT_POWER_MEDIUM_DELAY;
/** Time in milliseconds before the key press is considered long */
static gint longdelay = DEFAULT_POWER_LONG_DELAY;
/** Timeout in milliseconds during which key press is considered double */
static gint doublepressdelay = DEFAULT_POWER_DOUBLE_DELAY;
/** Action to perform on a short key press */
static poweraction_t shortpressaction = DEFAULT_POWERKEY_SHORT_ACTION;
/** Action to perform on a long key press */
static poweraction_t longpressaction = DEFAULT_POWERKEY_LONG_ACTION;
/** Action to perform on a double key press */
static poweraction_t doublepressaction = DEFAULT_POWERKEY_DOUBLE_ACTION;
/** D-Bus signal to send on short [power] press */
static gchar *shortpresssignal = NULL;
/** D-Bus signal to send on long [power] press */
static gchar *longpresssignal = NULL;
/** D-Bus signal to send on double [power] press */
static gchar *doublepresssignal = NULL;
static void cancel_powerkey_timeout(void);
/**
* Generic logic for key presses
*
* @param action The action to take
* @param dbus_signal A D-Bus signal to send
*/
static void generic_powerkey_handler(poweraction_t action,
gchar *dbus_signal)
{
alarm_ui_state_t alarm_ui_state =
datapipe_get_gint(alarm_ui_state_pipe);
submode_t submode = mce_get_submode_int32();
/* Ignore keypress if the alarm UI is visible */
if ((alarm_ui_state == MCE_ALARM_UI_VISIBLE_INT32) ||
(alarm_ui_state == MCE_ALARM_UI_RINGING_INT32))
goto EXIT;
switch (action) {
case POWER_DISABLED:
break;
case POWER_POWEROFF:
default:
/* Do not shutdown if the tklock is active
* or if we're in alarm state
*/
if ((submode & MCE_TKLOCK_SUBMODE) == 0) {
mce_log(LL_WARN,
"Requesting shutdown (action: %d)",
action);
request_normal_shutdown();
}
break;
case POWER_SOFT_POWEROFF:
/* Only soft poweroff if the tklock isn't active */
if ((submode & MCE_TKLOCK_SUBMODE) == 0) {
request_soft_poweroff();
}
break;
case POWER_TKLOCK_LOCK:
/* Request enabling of touchscreen/keypad lock
* if the tklock isn't already active
*/
if ((submode & MCE_TKLOCK_SUBMODE) == 0) {
execute_datapipe(&tk_lock_pipe,
GINT_TO_POINTER(LOCK_ON),
USE_INDATA, CACHE_INDATA);
} else if ((submode & MCE_MALF_SUBMODE) != 0) {
execute_datapipe(&tk_lock_pipe,
GINT_TO_POINTER(LOCK_OFF),
USE_INDATA, CACHE_INDATA);
}
break;
case POWER_TKLOCK_UNLOCK:
/* Request disabling of touchscreen/keypad lock
* if the tklock isn't already inactive
*/
if ((submode & MCE_TKLOCK_SUBMODE) != 0) {
execute_datapipe(&tk_lock_pipe,
GINT_TO_POINTER(LOCK_OFF),
USE_INDATA, CACHE_INDATA);
}
break;
case POWER_TKLOCK_BOTH:
/* Request enabling of touchscreen/keypad lock
* if the tklock isn't active,
* and disabling if the tklock is active
*/
if ((submode & MCE_TKLOCK_SUBMODE) == 0) {
execute_datapipe(&tk_lock_pipe,
GINT_TO_POINTER(LOCK_ON),
USE_INDATA, CACHE_INDATA);
} else {
execute_datapipe(&tk_lock_pipe,
GINT_TO_POINTER(LOCK_OFF),
USE_INDATA, CACHE_INDATA);
}
break;
case POWER_DBUS_SIGNAL:
/* Send a D-Bus signal */
dbus_send(NULL, MCE_REQUEST_PATH,
MCE_REQUEST_IF, dbus_signal,
NULL,
DBUS_TYPE_INVALID);
}
EXIT:
return;
}
/**
* Timeout callback for double key press
*
* @param data Unused
* @return Always returns FALSE, to disable the timeout
*/
static gboolean doublepress_timeout_cb(gpointer data)
{
system_state_t system_state = datapipe_get_gint(system_state_pipe);
(void)data;
doublepress_timeout_cb_id = 0;
/* doublepress timer expired without any secondary press;
* thus this was a short press
*/
if (system_state == MCE_STATE_USER)
generic_powerkey_handler(shortpressaction,
shortpresssignal);
return FALSE;
}
/**
* Cancel doublepress timeout
*/
static void cancel_doublepress_timeout(void)
{
/* Remove the timeout source for the [power] double key press handler */
if (doublepress_timeout_cb_id != 0) {
g_source_remove(doublepress_timeout_cb_id);
doublepress_timeout_cb_id = 0;
}
}
/**
* Setup doublepress timeout
*
* @return TRUE if the doublepress action was setup,
* FALSE if no action was setup
*/
static gboolean setup_doublepress_timeout(void)
{
submode_t submode = mce_get_submode_int32();
gboolean status = FALSE;
/* Only setup the doublepress timeout when needed */
if (doublepressaction == POWER_DISABLED)
goto EXIT;
cancel_doublepress_timeout();
/* If the tklock is enabled, but doublepress to unlock is disabled,
* or if the tklock isn't enabled and short press to lock is enabled,
* exit
*/
if (doublepressaction != POWER_DBUS_SIGNAL) {
if ((submode & MCE_TKLOCK_SUBMODE) != 0) {
if ((doublepressaction != POWER_TKLOCK_UNLOCK) &&
(doublepressaction != POWER_TKLOCK_BOTH))
goto EXIT;
} else {
if ((shortpressaction == POWER_TKLOCK_LOCK) ||
(shortpressaction == POWER_TKLOCK_BOTH))
goto EXIT;
}
}
/* Setup new timeout */
doublepress_timeout_cb_id =
g_timeout_add(doublepressdelay, doublepress_timeout_cb, NULL);
status = TRUE;
EXIT:
return status;
}
/**
* Logic for short key press
*/
static void handle_shortpress(void)
{
cancel_powerkey_timeout();
if (doublepress_timeout_cb_id == 0) {
if (setup_doublepress_timeout() == FALSE)
generic_powerkey_handler(shortpressaction,
shortpresssignal);
} else {
cancel_doublepress_timeout();
generic_powerkey_handler(doublepressaction,
doublepresssignal);
}
}
/**
* Logic for long key press
*
* @return TRUE on success, FALSE on failure
*/
static gboolean handle_longpress(void)
{
system_state_t state = datapipe_get_gint(system_state_pipe);
alarm_ui_state_t alarm_ui_state =
datapipe_get_gint(alarm_ui_state_pipe);
submode_t submode = mce_get_submode_int32();
gboolean status = TRUE;
/* Ignore keypress if the alarm UI is visible */
if ((alarm_ui_state == MCE_ALARM_UI_VISIBLE_INT32) ||
(alarm_ui_state == MCE_ALARM_UI_RINGING_INT32))
goto EXIT;
/* Ignore if we're already shutting down/rebooting */
switch (state) {
case MCE_STATE_SHUTDOWN:
case MCE_STATE_REBOOT:
status = FALSE;
break;
case MCE_STATE_ACTDEAD:
request_powerup();
break;
case MCE_STATE_USER:
/* If softoff is enabled, wake up
* Otherwise, perform long press action
*/
if ((submode & MCE_SOFTOFF_SUBMODE)) {
request_soft_poweron();
} else {
generic_powerkey_handler(longpressaction,
longpresssignal);
}
break;
default:
/* If no special cases are needed,
* just do a regular shutdown
*/
mce_log(LL_WARN,
"Requesting shutdown; state: %d",
state);
request_normal_shutdown();
break;
}
EXIT:
return status;
}
/**
* Timeout callback for long key press
*
* @param data Unused
* @return Always returns FALSE, to disable the timeout
*/
static gboolean powerkey_timeout_cb(gpointer data)
{
(void)data;
powerkey_timeout_cb_id = 0;
handle_longpress();
return FALSE;
}
/**
* Cancel powerkey timeout
*/
static void cancel_powerkey_timeout(void)
{
/* Remove the timeout source for the [power] long key press handler */
if (powerkey_timeout_cb_id != 0) {
g_source_remove(powerkey_timeout_cb_id);
powerkey_timeout_cb_id = 0;
}
}
/**
* Setup powerkey timeout
*/
static void setup_powerkey_timeout(gint powerkeydelay)
{
cancel_powerkey_timeout();
/* Setup new timeout */
powerkey_timeout_cb_id =
g_timeout_add(powerkeydelay, powerkey_timeout_cb, NULL);
}
/**
* D-Bus callback for powerkey event triggering
*
* @param msg D-Bus message
* @return TRUE on success, FALSE on failure
*/
static gboolean trigger_powerkey_event_req_dbus_cb(DBusMessage *const msg)
{
dbus_bool_t no_reply = dbus_message_get_no_reply(msg);
DBusMessageIter iter;
dbus_uint32_t uintval;
dbus_bool_t boolval;
gint argcount = 0;
gint argtype;
gboolean status = FALSE;
DBusError error;
/* Register error channel */
dbus_error_init(&error);
mce_log(LL_DEBUG, "Received [power] button trigger request");
if (dbus_message_iter_init(msg, &iter) == FALSE) {
// XXX: should we return an error instead?
mce_log(LL_ERR,
"Failed to initialise D-Bus message iterator; "
"message has no arguments");
goto EXIT;
}
argtype = dbus_message_iter_get_arg_type(&iter);
argcount++;
switch (argtype) {
case DBUS_TYPE_BOOLEAN:
dbus_message_iter_get_basic(&iter, &boolval);
uintval = (boolval == TRUE) ? 1 : 0;
break;
case DBUS_TYPE_UINT32:
dbus_message_iter_get_basic(&iter, &uintval);
if (uintval > 2) {
mce_log(LL_ERR,
"Incorrect powerkey event passed to %s.%s; "
"ignoring request",
MCE_REQUEST_IF, MCE_TRIGGER_POWERKEY_EVENT_REQ);
goto EXIT;
}
break;
default:
mce_log(LL_ERR,
"Argument %d passed to %s.%s has incorrect type",
argcount,
MCE_REQUEST_IF, MCE_TRIGGER_POWERKEY_EVENT_REQ);
goto EXIT;
}
while (dbus_message_iter_next(&iter) == TRUE)
argcount++;
if (argcount > 1) {
mce_log(LL_WARN,
"Too many arguments passed to %s.%s; "
"got %d, expected %d -- ignoring extra arguments",
MCE_REQUEST_IF, MCE_TRIGGER_POWERKEY_EVENT_REQ,
argcount, 1);
}
mce_log(LL_DEBUG, "[power] button event trigger value: %d", uintval);
cancel_powerkey_timeout();
cancel_doublepress_timeout();
switch (uintval) {
default:
case 0:
/* short press */
generic_powerkey_handler(shortpressaction,
shortpresssignal);
break;
case 1:
/* long press */
handle_longpress();
break;
case 2:
/* double press */
generic_powerkey_handler(doublepressaction,
doublepresssignal);
break;
}
if (no_reply == FALSE) {
DBusMessage *reply = dbus_new_method_reply(msg);
status = dbus_send_message(reply);
} else {
status = TRUE;
}
EXIT:
return status;
}
/**
* Datapipe trigger for the [power] key
*
* @param data A pointer to the input_event struct
*/
static void powerkey_trigger(gconstpointer const data)
{
system_state_t system_state = datapipe_get_gint(system_state_pipe);
submode_t submode = mce_get_submode_int32();
struct input_event const *const *evp;
struct input_event const *ev;
/* Don't dereference until we know it's safe */
if (data == NULL)
goto EXIT;
evp = data;
ev = *evp;
if ((ev != NULL) && (ev->code == KEY_POWER)) {
/* If set, the [power] key was pressed */
if (ev->value == 1) {
mce_log(LL_DEBUG, "[power] pressed");
/* Are we waiting for a doublepress? */
if (doublepress_timeout_cb_id != 0) {
handle_shortpress();
} else if ((system_state == MCE_STATE_ACTDEAD) ||
((submode & MCE_SOFTOFF_SUBMODE) != 0)) {
/* Setup new timeout */
execute_datapipe_output_triggers(&led_pattern_activate_pipe, MCE_LED_PATTERN_POWER_ON, USE_INDATA);
/* Shorter delay for startup
* than for shutdown
*/
setup_powerkey_timeout(mediumdelay);
} else {
setup_powerkey_timeout(longdelay);
}
} else if (ev->value == 0) {
mce_log(LL_DEBUG, "[power] released");
/* Short key press */
if (powerkey_timeout_cb_id != 0) {
handle_shortpress();
if ((system_state == MCE_STATE_ACTDEAD) ||
((submode & MCE_SOFTOFF_SUBMODE) != 0)) {
execute_datapipe_output_triggers(&led_pattern_deactivate_pipe, MCE_LED_PATTERN_POWER_ON, USE_INDATA);
}
}
}
}
EXIT:
return;
}
/**
* Parse the [power] action string
*
* @todo Implement this using string to enum mappings instead,
* to allow for better debugging messages and a generic parser
*
* @param string The string to parse
* @param dbus_signal A D-Bus signal to send
* @param action A pointer to the variable to store the action in
* @return TRUE if the string contained a valid action,
* FALSE if the action was invalid
*/
static gboolean parse_action(char *string,
char **dbus_signal,
poweraction_t *action)
{
gboolean status = FALSE;
if (!strcmp(string, POWER_DISABLED_STR)) {
*action = POWER_DISABLED;
} else if (!strcmp(string, POWER_MENU_STR)) {
*action = POWER_MENU;
} else if (!strcmp(string, POWER_POWEROFF_STR)) {
*action = POWER_POWEROFF;
} else if (!strcmp(string, POWER_SOFT_POWEROFF_STR)) {
*action = POWER_SOFT_POWEROFF;
} else if (!strcmp(string, POWER_TKLOCK_LOCK_STR)) {
*action = POWER_TKLOCK_LOCK;
} else if (!strcmp(string, POWER_TKLOCK_UNLOCK_STR)) {
*action = POWER_TKLOCK_UNLOCK;
} else if (!strcmp(string, POWER_TKLOCK_BOTH_STR)) {
*action = POWER_TKLOCK_BOTH;
} else if (!strncmp(string,
POWER_DBUS_SIGNAL_STR,
strlen(POWER_DBUS_SIGNAL_STR))) {
gchar *tmp = string + strlen(POWER_DBUS_SIGNAL_STR);
if (strlen(tmp) == 0) {
mce_log(LL_ERR,
"No signal name provided to action "
"`dbus-signal-'; ignoring");
goto EXIT;
}
*action = POWER_DBUS_SIGNAL;
*dbus_signal = g_strdup(tmp);
} else {
mce_log(LL_WARN,
"Unknown [power] action; "
"using default");
goto EXIT;
}
status = TRUE;
EXIT:
return status;
}
/**
* Init function for the powerkey component
*
* @return TRUE on success, FALSE on failure
*/
gboolean mce_powerkey_init(void)
{
gboolean status = FALSE;
gchar *tmp = NULL;
/* Append triggers/filters to datapipes */
append_input_trigger_to_datapipe(&keypress_pipe,
powerkey_trigger);
/* req_trigger_powerkey_event */
if (mce_dbus_handler_add(MCE_REQUEST_IF,
MCE_TRIGGER_POWERKEY_EVENT_REQ,
NULL,
DBUS_MESSAGE_TYPE_METHOD_CALL,
trigger_powerkey_event_req_dbus_cb) == NULL)
goto EXIT;
/* Get configuration options */
longdelay = mce_conf_get_int(MCE_CONF_POWERKEY_GROUP,
MCE_CONF_POWERKEY_LONG_DELAY,
DEFAULT_POWER_LONG_DELAY);
mediumdelay = mce_conf_get_int(MCE_CONF_POWERKEY_GROUP,
MCE_CONF_POWERKEY_MEDIUM_DELAY,
DEFAULT_POWER_MEDIUM_DELAY);
tmp = mce_conf_get_string(MCE_CONF_POWERKEY_GROUP,
MCE_CONF_POWERKEY_SHORT_ACTION,
"");
/* Since we've set a default, error handling is unnecessary */
(void)parse_action(tmp, &shortpresssignal, &shortpressaction);
g_free(tmp);
tmp = mce_conf_get_string(MCE_CONF_POWERKEY_GROUP,
MCE_CONF_POWERKEY_LONG_ACTION,
"");
/* Since we've set a default, error handling is unnecessary */
(void)parse_action(tmp, &longpresssignal, &longpressaction);
g_free(tmp);
doublepressdelay = mce_conf_get_int(MCE_CONF_POWERKEY_GROUP,
MCE_CONF_POWERKEY_DOUBLE_DELAY,
DEFAULT_POWER_DOUBLE_DELAY);
tmp = mce_conf_get_string(MCE_CONF_POWERKEY_GROUP,
MCE_CONF_POWERKEY_DOUBLE_ACTION,
"");
/* Since we've set a default, error handling is unnecessary */
(void)parse_action(tmp, &doublepresssignal, &doublepressaction);
g_free(tmp);
status = TRUE;
EXIT:
return status;
}
/**
* Exit function for the powerkey component
*
* @todo D-Bus unregistration
*/
void mce_powerkey_exit(void)
{
/* Remove triggers/filters from datapipes */
remove_input_trigger_from_datapipe(&keypress_pipe,
powerkey_trigger);
/* Remove all timer sources */
cancel_powerkey_timeout();
cancel_doublepress_timeout();
g_free(doublepresssignal);
g_free(longpresssignal);
g_free(shortpresssignal);
return;
}