/
display.c
935 lines (784 loc) · 23.3 KB
/
display.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
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
/**
* @file display.c
* Display module -- this implements display handling for MCE
* <p>
* Copyright © 2007-2010 Nokia Corporation and/or its subsidiary(-ies).
* <p>
* @author David Weinehall <david.weinehall@nokia.com>
* @author Jonathan Wilson <jfwfreo@tpgi.com.au>
* @author Carl Philipp Klemm <carl@uvos.xyz>
*
* 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 <gmodule.h>
#include <glib/gstdio.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdbool.h>
#include <linux/fb.h>
#include <sys/ioctl.h>
#include <mce/mode-names.h>
#include "mce.h"
#include "display.h"
#include "mce-io.h"
#include "mce-lib.h"
#include "mce-log.h"
#include "mce-dbus.h"
#include "mce-rtconf.h"
#include "mce-conf.h"
#include "datapipe.h"
/** Module name */
#define MODULE_NAME "display"
/** Functionality provided by this module */
static const gchar *const provides[] = { MODULE_NAME, NULL };
/** Module information */
G_MODULE_EXPORT module_info_struct module_info = {
/** Name of the module */
.name = MODULE_NAME,
/** Module provides */
.provides = provides,
/** Module priority */
.priority = 250
};
static gint dim_brightness;
/** GConf callback ID for display brightness setting */
static guint disp_brightness_gconf_cb_id = 0;
/** Display blanking timeout setting */
static gint disp_blank_timeout = DEFAULT_BLANK_TIMEOUT;
/** Cached brightness */
static gint cached_brightness = -1;
/** Target brightness */
static gint target_brightness = -1;
static gint set_brightness = -1;
static gint set_brightness_unfiltered = -1;
/** Fadeout step length */
static gint brightness_fade_steplength = 2;
/** Brightness fade timeout callback ID */
static gint brightness_fade_timeout_cb_id = 0;
/** Display blanking timeout callback ID */
static gint blank_timeout_cb_id = 0;
/** Maximum display brightness */
static gint maximum_display_brightness = DEFAULT_MAXIMUM_DISPLAY_BRIGHTNESS;
static gchar *brightness_file = NULL;
static gchar *max_brightness_file = NULL;
static bool hw_display_fading = false;
static bool is_tvout_state_changed = false;
static bool blank_during_shutdown = true;
static gboolean display_brightness_dbus_signal(void);
/**
* Timeout callback for the brightness fade
*
* @param data Unused
* @return Returns true to repeat, until the cached brightness has reached
* the destination value; when this happens, false is returned
*/
static gboolean brightness_fade_timeout_cb(gpointer data)
{
bool retval = true;
(void)data;
if ((cached_brightness == -1) ||
(ABS(cached_brightness -
target_brightness) < brightness_fade_steplength)) {
cached_brightness = target_brightness;
retval = false;
} else if (target_brightness > cached_brightness) {
cached_brightness += brightness_fade_steplength;
} else {
cached_brightness -= brightness_fade_steplength;
}
mce_write_number_string_to_file(brightness_file,
cached_brightness);
if (retval == false)
brightness_fade_timeout_cb_id = 0;
return retval;
}
/**
* Cancel the brightness fade timeout
*/
static void cancel_brightness_fade_timeout(void)
{
/* Remove the timeout source for the display brightness fade */
if (brightness_fade_timeout_cb_id != 0) {
g_source_remove(brightness_fade_timeout_cb_id);
brightness_fade_timeout_cb_id = 0;
}
}
/**
* Setup the brightness fade timeout
*
* @param step_time The time between each brightness step
*/
static void setup_brightness_fade_timeout(gint step_time)
{
cancel_brightness_fade_timeout();
/* Setup new timeout */
brightness_fade_timeout_cb_id =
g_timeout_add(step_time, brightness_fade_timeout_cb, NULL);
}
/**
* Update brightness fade
*
* Will fade from current value to new value
*
* @param new_brightness The new brightness to fade to
*/
static void update_brightness_fade(gint new_brightness)
{
gint step_time = 10;
if (hw_display_fading == true) {
cancel_brightness_fade_timeout();
cached_brightness = new_brightness;
target_brightness = new_brightness;
mce_write_number_string_to_file(brightness_file,
new_brightness);
goto EXIT;
}
/* If we're already fading towards the right brightness,
* don't change anything
*/
if (target_brightness == new_brightness)
goto EXIT;
target_brightness = new_brightness;
brightness_fade_steplength = 2;
setup_brightness_fade_timeout(step_time);
EXIT:
return;
}
/**
* Blank display
*/
static void display_blank(void)
{
cancel_brightness_fade_timeout();
cached_brightness = 0;
target_brightness = 0;
mce_write_number_string_to_file(brightness_file, 0);
}
/**
* Dim display
*/
static void display_dim(void)
{
update_brightness_fade((maximum_display_brightness * dim_brightness) / 100);
}
/**
* Unblank display
*/
static void display_unblank(void)
{
/* If we unblank, switch on display immediately */
if (cached_brightness == 0) {
cached_brightness = set_brightness;
target_brightness = set_brightness;
mce_write_number_string_to_file(brightness_file,
set_brightness);
} else {
update_brightness_fade(set_brightness);
}
}
/**
* Display brightness trigger
*
* @note A brightness request is only sent if the value changed
* @param data The display brightness stored in a pointer
*/
static void display_brightness_trigger(gconstpointer data)
{
display_state_t display_state = datapipe_get_gint(display_state_pipe);
gint new_brightness = GPOINTER_TO_INT(data);
/* If the pipe is choked, ignore the value */
if (new_brightness == 0)
goto EXIT;
/* Adjust the value, since it's a percentage value */
new_brightness = (maximum_display_brightness * new_brightness) / 100;
/* If we're just rehashing the same brightness value, don't bother */
if ((new_brightness == cached_brightness) && (cached_brightness != -1))
goto EXIT;
/* The value we have here is for non-dimmed screen only */
set_brightness = new_brightness;
if ((display_state == MCE_DISPLAY_OFF) ||
(display_state == MCE_DISPLAY_DIM))
goto EXIT;
update_brightness_fade(new_brightness);
EXIT:
return;
}
/**
* Timeout callback for display blanking
*
* @param data Unused
* @return Always returns false, to disable the timeout
*/
static gboolean blank_timeout_cb(gpointer data)
{
(void)data;
blank_timeout_cb_id = 0;
(void)execute_datapipe(&display_state_pipe,
GINT_TO_POINTER(MCE_DISPLAY_OFF),
USE_INDATA, CACHE_INDATA);
return false;
}
/**
* Cancel the display blanking timeout
*/
static void cancel_blank_timeout(void)
{
/* Remove the timeout source for display blanking */
if (blank_timeout_cb_id != 0) {
g_source_remove(blank_timeout_cb_id);
blank_timeout_cb_id = 0;
}
}
/**
* Setup blank timeout
*/
static void setup_blank_timeout(void)
{
cancel_blank_timeout();
/* Setup new timeout */
blank_timeout_cb_id =
g_timeout_add_seconds(disp_blank_timeout,
blank_timeout_cb, NULL);
}
/**
* rtconf callback for display related settings
*
* @param key Unused
* @param cb_id Connection ID from gconf_client_notify_add()
* @param user_data Unused
*/
static void display_rtconf_cb(const gchar *key, guint cb_id, void *user_data)
{
(void)key;
(void)user_data;
if (cb_id == disp_brightness_gconf_cb_id) {
gint tmp;
if (mce_rtconf_get_int(MCE_BRIGHTNESS_KEY, &tmp)) {
execute_datapipe(&display_brightness_pipe, GINT_TO_POINTER(tmp), USE_INDATA, CACHE_INDATA);
set_brightness_unfiltered = tmp;
display_brightness_dbus_signal();
}
} else {
mce_log(LL_WARN, "%s: Spurious rtconf value received; confused!", MODULE_NAME);
}
}
/**
* Send a display status reply or signal
*
* @param method_call A DBusMessage to reply to;
* pass NULL to send a display status signal instead
* @return true on success, false on failure
*/
static gboolean send_display_status(DBusMessage *const method_call)
{
display_state_t display_state = datapipe_get_gint(display_state_pipe);
bool is_tvout_on = datapipe_get_gint(tvout_pipe);
DBusMessage *msg = NULL;
const gchar *state = NULL;
bool status = false;
switch (display_state) {
case MCE_DISPLAY_OFF:
state = MCE_DISPLAY_OFF_STRING;
break;
case MCE_DISPLAY_DIM:
state = MCE_DISPLAY_DIM_STRING;
break;
case MCE_DISPLAY_ON:
default:
state = MCE_DISPLAY_ON_STRING;
break;
}
if ((is_tvout_state_changed == true) && (display_state == MCE_DISPLAY_OFF)) {
state = is_tvout_on ? MCE_DISPLAY_ON_STRING : MCE_DISPLAY_OFF_STRING;
}
mce_log(LL_DEBUG,
"%s: Sending display status: %s", MODULE_NAME,
state);
/* If method_call is set, send a reply,
* otherwise, send a signal
*/
if ((is_tvout_on) && (display_state == MCE_DISPLAY_OFF) && (!is_tvout_state_changed)){
goto EXIT;
}
else
{
if (method_call != NULL) {
msg = dbus_new_method_reply(method_call);
} else {
/* display_status_ind */
msg = dbus_new_signal(MCE_SIGNAL_PATH, MCE_SIGNAL_IF,
MCE_DISPLAY_SIG);
}
/* Append the display status */
if (dbus_message_append_args(msg,
DBUS_TYPE_STRING, &state,
DBUS_TYPE_INVALID) == false) {
mce_log(LL_CRIT,
"Failed to append %sargument to D-Bus message "
"for %s.%s",
method_call ? "reply " : "",
method_call ? MCE_REQUEST_IF :
MCE_SIGNAL_IF,
method_call ? MCE_DISPLAY_STATUS_GET :
MCE_DISPLAY_SIG);
dbus_message_unref(msg);
goto EXIT;
}
/* Send the message */
status = dbus_send_message(msg);
}
EXIT:
return status;
}
/**
* D-Bus callback for the get display status method call
*
* @param msg The D-Bus message
* @return true on success, false on failure
*/
static gboolean display_status_get_dbus_cb(DBusMessage *const msg)
{
bool status = false;
mce_log(LL_DEBUG,
"Received display status get request");
/* Try to send a reply that contains the current display status */
if (send_display_status(msg) == false)
goto EXIT;
status = true;
EXIT:
return status;
}
/**
* D-Bus callback for the display on method call
*
* @param msg The D-Bus message
* @return true on success, false on failure
*/
static gboolean display_on_req_dbus_cb(DBusMessage *const msg)
{
dbus_bool_t no_reply = dbus_message_get_no_reply(msg);
submode_t submode = mce_get_submode_int32();
bool status = false;
mce_log(LL_DEBUG,
"Received display on request");
if ((submode & MCE_TKLOCK_SUBMODE) == 0) {
mce_log(LL_DEBUG, "MCE_DISPLAY_ON in %s %s %d",__FILE__, __func__, __LINE__);
(void)execute_datapipe(&display_state_pipe,
GINT_TO_POINTER(MCE_DISPLAY_ON),
USE_INDATA, CACHE_INDATA);
}
if (no_reply == false) {
DBusMessage *reply = dbus_new_method_reply(msg);
status = dbus_send_message(reply);
} else {
status = true;
}
return status;
}
/**
* D-Bus callback for the display dim method call
*
* @param msg The D-Bus message
* @return true on success, false on failure
*/
static gboolean display_dim_req_dbus_cb(DBusMessage *const msg)
{
dbus_bool_t no_reply = dbus_message_get_no_reply(msg);
submode_t submode = mce_get_submode_int32();
bool status = false;
mce_log(LL_DEBUG,
"Received display dim request");
/* If the tklock is active, ignore the request */
if ((submode & MCE_TKLOCK_SUBMODE) == 0) {
(void)execute_datapipe(&display_state_pipe,
GINT_TO_POINTER(MCE_DISPLAY_DIM),
USE_INDATA, CACHE_INDATA);
}
if (no_reply == false) {
DBusMessage *reply = dbus_new_method_reply(msg);
status = dbus_send_message(reply);
} else {
status = true;
}
return status;
}
/**
* D-Bus callback for the display off method call
*
* @param msg The D-Bus message
* @return true on success, false on failure
*/
static gboolean display_off_req_dbus_cb(DBusMessage *const msg)
{
dbus_bool_t no_reply = dbus_message_get_no_reply(msg);
bool status = false;
mce_log(LL_DEBUG,
"Received display off request");
(void)execute_datapipe(&display_state_pipe,
GINT_TO_POINTER(MCE_DISPLAY_OFF),
USE_INDATA, CACHE_INDATA);
if (no_reply == false) {
DBusMessage *reply = dbus_new_method_reply(msg);
status = dbus_send_message(reply);
} else {
status = true;
}
return status;
}
static gboolean display_brightness_dbus_signal(void)
{
DBusMessage *msg = NULL;
bool status = false;
mce_log(LL_DEBUG,
"%s: Sending display brightness state: %i", MODULE_NAME,
set_brightness_unfiltered);
msg = dbus_new_signal(MCE_SIGNAL_PATH, MCE_SIGNAL_IF,
MCE_DISPLAY_BRIGTNESS_SIG);
dbus_int32_t tmp = set_brightness_unfiltered;
/* Append the inactivity status */
if (dbus_message_append_args(msg,
DBUS_TYPE_INT32, &tmp,
DBUS_TYPE_INVALID) == false) {
mce_log(LL_CRIT, "%s: "
"Failed to append reply argument to D-Bus message "
"for %s.%s", MODULE_NAME,
MCE_SIGNAL_IF,
MCE_DISPLAY_BRIGTNESS_SIG);
dbus_message_unref(msg);
return status;
}
/* Send the message */
status = dbus_send_message(msg);
return status;
}
static gboolean display_brightness_set_dbus_cb(DBusMessage *const msg)
{
dbus_bool_t no_reply = dbus_message_get_no_reply(msg);
bool status = false;
DBusError error;
dbus_error_init(&error);
mce_log(LL_DEBUG, "%s: Received display brightness set request", MODULE_NAME);
dbus_int32_t brightness;
if (dbus_message_get_args(msg, &error,
DBUS_TYPE_INT32, &brightness,
DBUS_TYPE_INVALID) == false) {
mce_log(LL_CRIT,
"Failed to get argument from %s.%s: %s",
MCE_REQUEST_IF, MCE_DISPLAY_BRIGTNESS_SET,
error.message);
dbus_error_free(&error);
return false;
}
set_brightness_unfiltered = brightness;
execute_datapipe(&display_brightness_pipe, GINT_TO_POINTER(brightness), USE_INDATA, CACHE_INDATA);
mce_rtconf_set_int(MCE_BRIGHTNESS_KEY, set_brightness_unfiltered);
display_brightness_dbus_signal();
if (no_reply == false) {
DBusMessage *reply = dbus_new_method_reply(msg);
status = dbus_send_message(reply);
} else {
status = true;
}
return status;
}
static gboolean display_brightness_get_dbus_cb(DBusMessage *const msg)
{
dbus_bool_t no_reply = dbus_message_get_no_reply(msg);
bool status = false;
mce_log(LL_DEBUG,
"Received display brightness get request");
if (no_reply == false) {
DBusMessage *reply = dbus_new_method_reply(msg);
dbus_int32_t tmp = set_brightness_unfiltered;
if (!dbus_message_append_args(reply,
DBUS_TYPE_INT32, &tmp,
DBUS_TYPE_INVALID)) {
mce_log(LL_ERR, "%s: Failed to append dbus arguments", MODULE_NAME);
return false;
}
status = dbus_send_message(reply);
} else {
status = true;
}
return status;
}
/**
* Handle display state change
*
* @param data The display state stored in a pointer
*/
static void display_state_trigger(gconstpointer data)
{
/** Cached display state */
static display_state_t cached_display_state = MCE_DISPLAY_UNDEF;
display_state_t display_state = GPOINTER_TO_INT(data);
switch (display_state) {
case MCE_DISPLAY_OFF:
cancel_blank_timeout();
break;
case MCE_DISPLAY_DIM:
setup_blank_timeout();
break;
case MCE_DISPLAY_ON:
default:
cancel_blank_timeout();
break;
}
/* If we already have the right state,
* we're done here
*/
if (cached_display_state == display_state)
goto EXIT;
switch (display_state) {
case MCE_DISPLAY_OFF:
display_blank();
break;
case MCE_DISPLAY_DIM:
display_dim();
break;
case MCE_DISPLAY_ON:
default:
display_unblank();
break;
}
/* This will send the correct state
* since the pipe contains the new value
*/
send_display_status(NULL);
/* Update the cached value */
cached_display_state = display_state;
EXIT:
return;
}
/**
* Datapipe trigger for device inactivity
*
* @param data The inactivity stored in a pointer;
* true if the device is inactive,
* false if the device is active
*/
static void device_inactive_trigger(gconstpointer data)
{
system_state_t system_state = datapipe_get_gint(system_state_pipe);
display_state_t display_state = datapipe_get_gint(display_state_pipe);
alarm_ui_state_t alarm_ui_state = datapipe_get_gint(alarm_ui_state_pipe);
bool device_inactive = GPOINTER_TO_INT(data);
/* Unblank screen on device activity,
* unless the device is in acting dead and no alarm is visible
*/
if (((system_state == MCE_STATE_USER) ||
((system_state == MCE_STATE_ACTDEAD) &&
((alarm_ui_state == MCE_ALARM_UI_VISIBLE_INT32) ||
(alarm_ui_state == MCE_ALARM_UI_RINGING_INT32)))) &&
(device_inactive == false)) {
(void)execute_datapipe(&display_state_pipe,
GINT_TO_POINTER(MCE_DISPLAY_ON),
USE_INDATA, CACHE_INDATA);
} else if ((system_state == MCE_STATE_USER || system_state == MCE_STATE_ACTDEAD) &&
device_inactive == true && display_state == MCE_DISPLAY_ON) {
(void)execute_datapipe(&display_state_pipe,
GINT_TO_POINTER(MCE_DISPLAY_DIM),
USE_INDATA, CACHE_INDATA);
}
}
static void tvout_trigger(gconstpointer data)
{
display_state_t display_state = datapipe_get_gint(display_state_pipe);
bool is_tvout_on = GPOINTER_TO_INT(data);
mce_log(LL_DEBUG, "Recieved tvout state changing: is_tvout_on = %d", is_tvout_on);
if (display_state == MCE_DISPLAY_OFF) {
is_tvout_state_changed = true;
send_display_status(NULL);
is_tvout_state_changed = false;
}
return;
}
static void system_state_trigger(gconstpointer data)
{
const system_state_t system_state = GPOINTER_TO_INT(data);
if ((system_state == MCE_STATE_SHUTDOWN || system_state == MCE_STATE_REBOOT) && blank_during_shutdown)
execute_datapipe(&display_state_pipe, GINT_TO_POINTER(MCE_DISPLAY_OFF), USE_INDATA, CACHE_INDATA);
}
/**
* Get the display type
*
* @return The display type
*/
static bool get_display(void)
{
bool ret = false;
gchar *bright_file = NULL;
gchar *max_bright_file = NULL;
const char *path;
GDir* dir;
/* Attempt to find first entry in /backlight */
dir = g_dir_open(DISPLAY_GENERIC_PATH, 0, NULL);
if (dir) {
path = g_dir_read_name(dir);
if (path) {
bright_file = g_strconcat(DISPLAY_GENERIC_PATH, path, DISPLAY_GENERIC_BRIGHTNESS_FILE, NULL);
max_bright_file = g_strconcat(DISPLAY_GENERIC_PATH, path, DISPLAY_GENERIC_MAX_BRIGHTNESS_FILE, NULL);
if ((g_access(bright_file, W_OK) == 0) && (g_access(max_bright_file, W_OK) == 0)) {
/* These will be freed later on, during module unload */
brightness_file = bright_file;
max_brightness_file = max_bright_file;
ret = true;
} else {
g_free(bright_file);
g_free(max_bright_file);
}
}
}
g_dir_close(dir);
if (bright_file)
mce_log(LL_DEBUG, "%s: using %s as backlight brightness", MODULE_NAME, bright_file);
return ret;
}
G_MODULE_EXPORT const gchar *g_module_check_init(GModule *module);
const gchar *g_module_check_init(GModule *module)
{
gint disp_brightness = DEFAULT_DISP_BRIGHTNESS;
gulong tmp;
(void)module;
if (!get_display()) {
/* TODO - do not provide brightness interface if we can't
control it. Be very careful when implementing the change as
there might be mission critical applications that rely on it.
*/
mce_log(LL_WARN, "%s: Could not find display backlight", MODULE_NAME);
}
dim_brightness = mce_conf_get_int("DisplayBrightness", "Dim", DEFAULT_DIM_BRIGHTNESS, NULL);
blank_during_shutdown = mce_conf_get_bool("Display", "BlankDuringShutdown", true, NULL);
/* Append triggers/filters to datapipes */
append_output_trigger_to_datapipe(&display_brightness_pipe,
display_brightness_trigger);
append_output_trigger_to_datapipe(&display_state_pipe,
display_state_trigger);
append_output_trigger_to_datapipe(&device_inactive_pipe,
device_inactive_trigger);
append_output_trigger_to_datapipe(&tvout_pipe,
tvout_trigger);
append_output_trigger_to_datapipe(&system_state_pipe,
system_state_trigger);
/* Get maximum brightness */
if (mce_read_number_string_from_file(max_brightness_file,
&tmp) == false) {
mce_log(LL_ERR,
"%s: Could not read the maximum brightness from %s; "
"defaulting to %d", MODULE_NAME,
max_brightness_file,
DEFAULT_MAXIMUM_DISPLAY_BRIGHTNESS);
tmp = DEFAULT_MAXIMUM_DISPLAY_BRIGHTNESS;
}
maximum_display_brightness = tmp;
/* Display brightness */
/* Since we've set a default, error handling is unnecessary */
(void)mce_rtconf_get_int(MCE_BRIGHTNESS_KEY,
&disp_brightness);
set_brightness_unfiltered = disp_brightness;
/* Use the current brightness as cached brightness on startup,
* and fade from that value
*/
if (mce_read_number_string_from_file(brightness_file,
&tmp) == false) {
mce_log(LL_ERR,
"%s: Could not read the current brightness from %s", MODULE_NAME, brightness_file);
cached_brightness = -1;
} else {
cached_brightness = tmp;
}
(void)execute_datapipe(&display_brightness_pipe,
GINT_TO_POINTER(disp_brightness),
USE_INDATA, CACHE_INDATA);
if (mce_rtconf_notifier_add(MCE_BRIGHTNESS_KEY,
display_rtconf_cb, NULL,
&disp_brightness_gconf_cb_id) == false)
goto EXIT;
/* Display blank */
/* Since we've set a default, error handling is unnecessary */
disp_blank_timeout = mce_conf_get_int(MCE_CONF_DISPLAY_GROUP, MCE_CONF_DISPLAY_BLANK_KEY,
DEFAULT_BLANK_TIMEOUT, NULL);
/* get_display_status */
if (mce_dbus_handler_add(MCE_REQUEST_IF,
MCE_DISPLAY_STATUS_GET,
NULL,
DBUS_MESSAGE_TYPE_METHOD_CALL,
display_status_get_dbus_cb) == NULL)
goto EXIT;
/* req_display_state_on */
if (mce_dbus_handler_add(MCE_REQUEST_IF,
MCE_DISPLAY_ON_REQ,
NULL,
DBUS_MESSAGE_TYPE_METHOD_CALL,
display_on_req_dbus_cb) == NULL)
goto EXIT;
/* req_display_state_dim */
if (mce_dbus_handler_add(MCE_REQUEST_IF,
MCE_DISPLAY_DIM_REQ,
NULL,
DBUS_MESSAGE_TYPE_METHOD_CALL,
display_dim_req_dbus_cb) == NULL)
goto EXIT;
/* req_display_state_off */
if (mce_dbus_handler_add(MCE_REQUEST_IF,
MCE_DISPLAY_OFF_REQ,
NULL,
DBUS_MESSAGE_TYPE_METHOD_CALL,
display_off_req_dbus_cb) == NULL)
goto EXIT;
if (mce_dbus_handler_add(MCE_REQUEST_IF,
MCE_DISPLAY_BRIGTNESS_SET,
NULL,
DBUS_MESSAGE_TYPE_METHOD_CALL,
display_brightness_set_dbus_cb) == NULL)
goto EXIT;
if (mce_dbus_handler_add(MCE_REQUEST_IF,
MCE_DISPLAY_BRIGTNESS_GET,
NULL,
DBUS_MESSAGE_TYPE_METHOD_CALL,
display_brightness_get_dbus_cb) == NULL)
goto EXIT;
/* Request display on to get the state machine in sync */
(void)execute_datapipe(&display_state_pipe,
GINT_TO_POINTER(MCE_DISPLAY_ON),
USE_INDATA, CACHE_INDATA);
EXIT:
return NULL;
}
G_MODULE_EXPORT void g_module_unload(GModule *module);
void g_module_unload(GModule *module)
{
(void)module;
/* Remove triggers/filters from datapipes */
remove_output_trigger_from_datapipe(&tvout_pipe,
tvout_trigger);
remove_output_trigger_from_datapipe(&device_inactive_pipe,
device_inactive_trigger);
remove_output_trigger_from_datapipe(&display_state_pipe,
display_state_trigger);
remove_output_trigger_from_datapipe(&display_brightness_pipe,
display_brightness_trigger);
remove_output_trigger_from_datapipe(&system_state_pipe,
system_state_trigger);
/* Free strings */
g_free(brightness_file);
g_free(max_brightness_file);
/* Remove all timer sources */
cancel_brightness_fade_timeout();
cancel_blank_timeout();
return;
}