/
buttonbacklight.c
739 lines (604 loc) · 19.7 KB
/
buttonbacklight.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
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
/**
* @file buttonbacklight.c
*
* buttonbacklight module -- implements MENU/HOME/BACK button backlight policy
* <p>
* Copyright Copyright (C) 2017 Jolla Ltd.
* <p>
* @author Simo Piiroinen <simo.piiroinen@jollamobile.com>
*
* 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 "../mce.h"
#include "../mce-log.h"
#include "../mce-dbus.h"
#include "../mce-conf.h"
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include <mce/dbus-names.h>
#include <gmodule.h>
/* ========================================================================= *
* CONSTANTS
* ========================================================================= */
/** Module name */
#define MODULE_NAME "buttonbacklight"
/** Maximum number of concurrent button backlight enabler clients */
#define BBL_MAX_CLIENTS 15
/* ========================================================================= *
* PROTOTYPES
* ========================================================================= */
/* ------------------------------------------------------------------------- *
* GENRIC_UTILS
* ------------------------------------------------------------------------- */
static void bbl_write_sysfs (const char *path, const char *data);
/* ------------------------------------------------------------------------- *
* BACKLIGHT_STATE
* ------------------------------------------------------------------------- */
static void bbl_state_set (tristate_t state);
static void bbl_state_rethink (void);
/* ------------------------------------------------------------------------- *
* DATAPIPE_TRACKING
* ------------------------------------------------------------------------- */
static void bbl_datapipe_system_state_cb (gconstpointer data);
static void bbl_datapipe_display_state_curr_cb(gconstpointer data);
static void bbl_datapipe_submode_cb (gconstpointer data);
static void bbl_datapipes_init (void);
static void bbl_datapipes_quit (void);
/* ------------------------------------------------------------------------- *
* DBUS_TRACKING
* ------------------------------------------------------------------------- */
static gboolean bbl_dbus_client_exit_cb (DBusMessage *const sig);
static void bbl_dbus_add_client (const char *dbus_name);
static void bbl_dbus_remove_client (const char *dbus_name);
static void bbl_dbus_remove_all_clients (void);
static gboolean bbl_dbus_send_backlight_state (DBusMessage *const req);
static gboolean bbl_dbus_set_backlight_state_cb (DBusMessage *const req);
static gboolean bbl_dbus_get_button_backlight_cb(DBusMessage *const req);
static void bbl_dbus_init (void);
static void bbl_dbus_quit (void);
/* ------------------------------------------------------------------------- *
* STATIC_CONFIGURATION
* ------------------------------------------------------------------------- */
static bool bbl_config_exists (void);
static void bbl_config_init (void);
static void bbl_config_quit (void);
/* ------------------------------------------------------------------------- *
* MODULE_LOAD_UNLOAD
* ------------------------------------------------------------------------- */
G_MODULE_EXPORT const gchar *g_module_check_init(GModule *module);
G_MODULE_EXPORT void g_module_unload (GModule *module);
/* ========================================================================= *
* MODULE_DATA
* ========================================================================= */
/** Current system state; undefined initially */
static system_state_t system_state = MCE_SYSTEM_STATE_UNDEF;
/** Current display state; undefined initially */
static display_state_t display_state_curr = MCE_DISPLAY_UNDEF;
/** Current submode: Initialized to invalid placeholder value */
static submode_t submode = MCE_SUBMODE_INVALID;
/** Current backlight state: unknown initially */
static tristate_t backlight_state = TRISTATE_UNKNOWN;
/** List of monitored bus clients */
static GSList *bbl_dbus_monitored_clients = NULL;
/** Sysfs control file path for backlight */
static gchar *bbl_control_path = 0;
/** Value to write when enabling backlight */
static gchar *bbl_control_value_enable = 0;
/** Value to write when disabling backlight */
static gchar *bbl_control_value_disable = 0;
/* ========================================================================= *
* GENRIC_UTILS
* ========================================================================= */
/** Helper for writing to sysfs files
*/
static void
bbl_write_sysfs(const char *path, const char *data)
{
int fd = -1;
if( !path || !data )
goto EXIT;
if( (fd = TEMP_FAILURE_RETRY(open(path, O_WRONLY))) == -1 ) {
mce_log(LOG_ERR, "%s: %s: %m", path, "open");
goto EXIT;
}
if( TEMP_FAILURE_RETRY(write(fd, data, strlen(data))) == -1 ) {
mce_log(LOG_ERR, "%s: %s: %m", path, "write");
goto EXIT;
}
mce_log(LL_DEBUG, "%s << %s", path, data);
EXIT:
if( fd != -1 )
TEMP_FAILURE_RETRY(close(fd));
}
/* ========================================================================= *
* BACKLIGHT_STATE
* ========================================================================= */
/** Set current button backlight state
*
* @param state one of TRISTATE_TRUE, TRISTATE_FALSE, or TRISTATE_UNKNOWN
*/
static void
bbl_state_set(tristate_t state)
{
if( backlight_state == state )
goto EXIT;
mce_log(LL_DEBUG, "backlight_state: %s -> %s",
tristate_repr(backlight_state),
tristate_repr(state));
backlight_state = state;
bbl_dbus_send_backlight_state(0);
const char *value = 0;
switch( backlight_state ) {
case TRISTATE_TRUE:
value = bbl_control_value_enable;
break;
case TRISTATE_FALSE:
value = bbl_control_value_disable;
break;
default:
goto EXIT;
}
bbl_write_sysfs(bbl_control_path, value);
EXIT:
return;
}
/** Evaluate what the current button backlight state should be
*/
static void
bbl_state_rethink(void)
{
/* Assume button backlight needs to be disabled */
tristate_t state = TRISTATE_FALSE;
/* Any clients that have requested enabling ? */
if( bbl_dbus_monitored_clients == 0 )
goto EXIT;
/* Sane sysfs config has been defined ? */
if( !bbl_config_exists() )
goto EXIT;
/* Device running in USER mode ? */
switch( system_state ) {
case MCE_SYSTEM_STATE_USER:
break;
default:
goto EXIT;
}
/* Display is ON or DIM */
switch( display_state_curr ) {
case MCE_DISPLAY_ON:
case MCE_DISPLAY_DIM:
break;
default:
goto EXIT;
}
/* Lokcscreen is not active ? */
if( submode & (MCE_SUBMODE_TKLOCK | MCE_SUBMODE_INVALID) )
goto EXIT;
/* Button backlight should be enabled */
state = TRISTATE_TRUE;
EXIT:
bbl_state_set(state);
}
/* ========================================================================= *
* DATAPIPE_TRACKING
* ========================================================================= */
/** Handle system state change notifications
*
* @param data system state (as void pointer)
*/
static void
bbl_datapipe_system_state_cb(gconstpointer data)
{
system_state_t prev = system_state;
system_state = GPOINTER_TO_INT(data);
if( prev == system_state )
goto EXIT;
mce_log(LL_DEBUG, "system_state: %s -> %s",
system_state_repr(prev),
system_state_repr(system_state));
bbl_state_rethink();
EXIT:
return;
}
/** Handle display state change notifications
*
* @param data display state (as void pointer)
*/
static void
bbl_datapipe_display_state_curr_cb(gconstpointer data)
{
display_state_t prev = display_state_curr;
display_state_curr = GPOINTER_TO_INT(data);
if( display_state_curr == prev )
goto EXIT;
mce_log(LL_DEBUG, "display_state_curr = %s -> %s",
display_state_repr(prev),
display_state_repr(display_state_curr));
bbl_state_rethink();
EXIT:
return;
}
/** Handle submode change notifications
*
* @param data submode (as void pointer)
*/
static void
bbl_datapipe_submode_cb(gconstpointer data)
{
submode_t prev = submode;
submode = GPOINTER_TO_INT(data);
if( submode == prev )
goto EXIT;
mce_log(LL_DEBUG, "submode: %s", submode_change_repr(prev, submode));
bbl_state_rethink();
EXIT:
return;
}
/** Array of datapipe handlers */
static datapipe_handler_t bbl_datapipe_handlers[] =
{
{
.datapipe = &system_state_pipe,
.output_cb = bbl_datapipe_system_state_cb,
},
{
.datapipe = &display_state_curr_pipe,
.output_cb = bbl_datapipe_display_state_curr_cb,
},
{
.datapipe = &submode_pipe,
.output_cb = bbl_datapipe_submode_cb,
},
// sentinel
{
.datapipe = 0,
}
};
static datapipe_bindings_t bbl_datapipe_bindings =
{
.module = MODULE_NAME,
.handlers = bbl_datapipe_handlers,
};
/** Append triggers/filters to datapipes
*/
static void
bbl_datapipes_init(void)
{
mce_datapipe_init_bindings(&bbl_datapipe_bindings);
}
/** Remove triggers/filters from datapipes
*/
static void
bbl_datapipes_quit(void)
{
mce_datapipe_quit_bindings(&bbl_datapipe_bindings);
}
/* ========================================================================= *
* DBUS_TRACKING
* ========================================================================= */
/** Callback used for monitoring button backlight clients
*
* If processes that have enabled button backlight drop out
* from dbus, treat it as if they had asked for backlight disable.
*
* @param sig NameOwnerChanged D-Bus signal
*
* @return TRUE
*/
static gboolean
bbl_dbus_client_exit_cb(DBusMessage *const sig)
{
DBusError error = DBUS_ERROR_INIT;
const char *dbus_name = 0;
const char *old_owner = 0;
const char *new_owner = 0;
if( !dbus_message_get_args(sig, &error,
DBUS_TYPE_STRING, &dbus_name,
DBUS_TYPE_STRING, &old_owner,
DBUS_TYPE_STRING, &new_owner,
DBUS_TYPE_INVALID) ) {
mce_log(LL_ERR, "Failed to parse NameOwnerChanged: %s: %s",
error.name, error.message);
goto EXIT;
}
if( !*new_owner )
bbl_dbus_remove_client(dbus_name);
EXIT:
dbus_error_free(&error);
return TRUE;
}
/** Register a client that has enabled button backlight
*/
static void
bbl_dbus_add_client(const char *dbus_name)
{
gssize rc = mce_dbus_owner_monitor_add(dbus_name,
bbl_dbus_client_exit_cb,
&bbl_dbus_monitored_clients,
BBL_MAX_CLIENTS);
if( rc < 0 )
mce_log(LL_WARN, "client %s ignored; BBL_MAX_CLIENTS exceeded",
dbus_name);
else if( rc > 0 )
mce_log(LL_DEBUG, "client %s added for tracking", dbus_name);
else
mce_log(LL_DEBUG, "client %s already tracked", dbus_name);
bbl_state_rethink();
}
/** Unregister a client that has enabled button backlight
*/
static void
bbl_dbus_remove_client(const char *dbus_name)
{
gssize rc = mce_dbus_owner_monitor_remove(dbus_name,
&bbl_dbus_monitored_clients);
if( rc < 0 )
mce_log(LL_WARN, "client %s ignored; is not tracked",dbus_name);
else
mce_log(LL_DEBUG, "client %s removed from tracking", dbus_name);
bbl_state_rethink();
}
/** Unregister all clients that have enabled button backlight
*/
static void
bbl_dbus_remove_all_clients(void)
{
mce_dbus_owner_monitor_remove_all(&bbl_dbus_monitored_clients);
bbl_state_rethink();
}
/** Send the button backlight state
*
* @param req Method call to reply, or NULL to broadcast a signal
*
* @return TRUE
*/
static gboolean
bbl_dbus_send_backlight_state(DBusMessage *const req)
{
static tristate_t prev = TRISTATE_UNKNOWN;
tristate_t curr = backlight_state;
dbus_bool_t arg = FALSE;
DBusMessage *msg = 0;
/* Externally TRISTATE_UNKNOWN is signaled as TRISTATE_FALSE */
if( curr == TRISTATE_TRUE )
arg = TRUE;
else
curr = TRISTATE_FALSE;
if( req != 0 ) {
/* Send reply to explicit query */
if( dbus_message_get_no_reply(req) )
goto EXIT;
msg = dbus_new_method_reply(req);
}
else {
/* Broadcast change signal */
if( prev == curr )
goto EXIT;
prev = curr;
msg = dbus_new_signal(MCE_SIGNAL_PATH, MCE_SIGNAL_IF,
MCE_BUTTON_BACKLIGHT_SIG);
}
if( !dbus_message_append_args(msg,
DBUS_TYPE_BOOLEAN, &arg,
DBUS_TYPE_INVALID) ) {
mce_log(LL_ERR, "Failed to append arguments to D-Bus message");
goto EXIT;
}
mce_log(LL_DEVEL, "send button backlight %s: state=%s",
req ? "reply" : "signal", arg ? "enabled" : "disabled");
dbus_send_message(msg), msg = 0;
EXIT:
if( msg )
dbus_message_unref(msg);
return TRUE;
}
/** D-Bus callback for the button backlight state change request method call
*
* @param req Method call message to handle
*
* @return TRUE
*/
static gboolean
bbl_dbus_set_backlight_state_cb(DBusMessage *const req)
{
const char *sender = dbus_message_get_sender(req);
DBusError err = DBUS_ERROR_INIT;
dbus_bool_t enable = false;
DBusMessage *rsp = 0;
mce_log(LL_DEVEL, "button backlight request from %s",
mce_dbus_get_name_owner_ident(sender));
if( !dbus_message_get_args(req, &err,
DBUS_TYPE_BOOLEAN, &enable,
DBUS_TYPE_INVALID) ) {
mce_log(LL_ERR, "Failed to get argument from %s.%s: %s: %s",
MCE_REQUEST_IF, MCE_BUTTON_BACKLIGHT_CHANGE_REQ,
err.name, err.message);
enable = false;
rsp = dbus_message_new_error(req, err.name, err.message);
}
if( enable )
bbl_dbus_add_client(sender);
else
bbl_dbus_remove_client(sender);
if( !dbus_message_get_no_reply(req) ) {
if( !rsp )
rsp = dbus_new_method_reply(req);
dbus_send_message(rsp), rsp = 0;
}
if( rsp )
dbus_message_unref(rsp);
dbus_error_free(&err);
return TRUE;
}
/**
* D-Bus callback for the get button backlight state method call
*
* @param req Method call message to handle
*
* @return TRUE
*/
static gboolean
bbl_dbus_get_button_backlight_cb(DBusMessage *const req)
{
const char *sender = dbus_message_get_sender(req);
mce_log(LL_DEVEL, "button backlight query from %s",
mce_dbus_get_name_owner_ident(sender));
if( !dbus_message_get_no_reply(req) )
bbl_dbus_send_backlight_state(req);
return TRUE;
}
/** Array of dbus message handlers */
static mce_dbus_handler_t bbl_dbus_handlers[] =
{
/* signals - outbound (for Introspect purposes only) */
{
.interface = MCE_SIGNAL_IF,
.name = MCE_BUTTON_BACKLIGHT_SIG,
.type = DBUS_MESSAGE_TYPE_SIGNAL,
.args =
" <arg name=\"enabled\" type=\"b\"/>\n"
},
/* method calls */
{
.interface = MCE_REQUEST_IF,
.name = MCE_BUTTON_BACKLIGHT_CHANGE_REQ,
.type = DBUS_MESSAGE_TYPE_METHOD_CALL,
.callback = bbl_dbus_set_backlight_state_cb,
.args =
" <arg direction=\"in\" name=\"enable\" type=\"b\"/>\n"
},
{
.interface = MCE_REQUEST_IF,
.name = MCE_BUTTON_BACKLIGHT_GET,
.type = DBUS_MESSAGE_TYPE_METHOD_CALL,
.callback = bbl_dbus_get_button_backlight_cb,
.args =
" <arg direction=\"out\" name=\"enabled\" type=\"b\"/>\n"
},
/* sentinel */
{
.interface = 0
}
};
/** Setup dbus handlers
*/
static void
bbl_dbus_init(void)
{
mce_dbus_handler_register_array(bbl_dbus_handlers);
}
/** Remove dbus handlers
*/
static void
bbl_dbus_quit(void)
{
mce_dbus_handler_unregister_array(bbl_dbus_handlers);
bbl_dbus_remove_all_clients();
}
/* ========================================================================= *
* STATIC_CONFIGURATION
* ========================================================================= */
/** Predicate for: All required configuration items are available
*/
static bool
bbl_config_exists(void)
{
return (bbl_control_path &&
bbl_control_value_enable &&
bbl_control_value_disable);
}
/** Parse button backlight configuration
*/
static void
bbl_config_init(void)
{
bool ack = false;
/* Silently ignore if config group is missing altogether */
if( !mce_conf_has_group(MCE_CONF_BUTTON_BACKLIGHT_GROUP) )
goto EXIT;
bbl_control_path =
mce_conf_get_string(MCE_CONF_BUTTON_BACKLIGHT_GROUP,
MCE_CONF_BUTTON_BACKLIGHT_CONTROL_PATH, 0);
bbl_control_value_enable =
mce_conf_get_string(MCE_CONF_BUTTON_BACKLIGHT_GROUP,
MCE_CONF_BUTTON_BACKLIGHT_CONTROL_VALUE_ENABLE, 0);
bbl_control_value_disable =
mce_conf_get_string(MCE_CONF_BUTTON_BACKLIGHT_GROUP,
MCE_CONF_BUTTON_BACKLIGHT_CONTROL_VALUE_DISABLE, 0);
if( !bbl_config_exists() ) {
mce_log(LL_WARN, "Config group [%s] is missing required entries",
MCE_CONF_BUTTON_BACKLIGHT_GROUP);
goto EXIT;
}
if( access(bbl_control_path, W_OK) == -1 ) {
mce_log(LL_WARN, "%s: is not writable: %m", bbl_control_path);
goto EXIT;
}
ack = true;
EXIT:
/* All or nothing */
if( !ack )
bbl_config_quit();
}
/** Release button backlight configuration
*/
static void
bbl_config_quit(void)
{
g_free(bbl_control_path),
bbl_control_path = 0;
g_free(bbl_control_value_enable),
bbl_control_value_enable = 0;
g_free(bbl_control_value_disable),
bbl_control_value_disable = 0;
}
/* ========================================================================= *
* MODULE_LOAD_UNLOAD
* ========================================================================= */
/** Init function for the button backlight module
*
* @param module Unused
*
* @return NULL on success, a string with an error message on failure
*/
const gchar *
g_module_check_init(GModule *module)
{
(void)module;
/* Lookup static configuration */
bbl_config_init();
/* Install datapipe hooks */
bbl_datapipes_init();
/* Install dbus handlers */
bbl_dbus_init();
return NULL;
}
/** Exit function for the button backlight module
*
* @param module Unused
*/
void
g_module_unload(GModule *module)
{
(void)module;
/* Remove dbus handlers */
bbl_dbus_quit();
/* Remove datapipe hooks */
bbl_datapipes_quit();
/* Do not leave backlight on when mce is exiting */
bbl_state_set(TRISTATE_FALSE);
/* Release static configuration */
bbl_config_quit();
return;
}