-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathyamui.c
1602 lines (1382 loc) · 40.9 KB
/
yamui.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
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2014 - 2023 Jolla Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#define _DEFAULT_SOURCE
#include <stdio.h>
#include <stdbool.h>
#include <getopt.h>
#include <stdlib.h>
#include <unistd.h>
#include <poll.h>
#include <signal.h>
#include <sys/signalfd.h>
#include <sys/select.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <glib.h>
#include <gio/gio.h>
#include <systemd/sd-daemon.h>
#include "os-update.h"
#include "minui/minui.h"
#define IMAGES_MAX 30
/* ========================================================================= *
* Logging
* ========================================================================= */
#define VERBOSE 0
#define PFIX "yamui: "
#define log_emit(TAG, FMT, ARGS...) do {\
fprintf(stderr, PFIX TAG "%s(): " FMT "\n", __func__, ## ARGS);\
fflush(stderr);\
} while (0)
#define log_err( FMT, ARGS...) log_emit("E: ", FMT, ## ARGS)
#if VERBOSE
# define log_debug(FMT, ARGS...) log_emit("D: ", FMT, ## ARGS)
#else
# define log_debug(FMT, ARGS...) do {} while (0)
#endif
/* ========================================================================= *
* Prototypes
* ========================================================================= */
/* ------------------------------------------------------------------------- *
* DISPLAY
* ------------------------------------------------------------------------- */
static void display_acquire (void);
static void display_release (void);
static bool display_is_acquired (void);
static void display_set_updates_enabled(bool enabled);
static void display_set_blanked (bool blanked);
static bool display_can_be_drawn (void);
/* ------------------------------------------------------------------------- *
* SYSTEMBUS
* ------------------------------------------------------------------------- */
static bool systembus_is_available (void);
static void systembus_probe_socket (void);
static void systembus_socket_monitor_event_cb(GFileMonitor *mon, GFile *file, GFile *other_file, GFileMonitorEvent event_type, gpointer user_data);
static void systembus_quit_socket_monitor (void);
static bool systembus_init_socket_monitor (void);
/* ------------------------------------------------------------------------- *
* MAINLOOP
* ------------------------------------------------------------------------- */
static void mainloop_run (void);
static void mainloop_stop(void);
/* ------------------------------------------------------------------------- *
* SIGNALS
* ------------------------------------------------------------------------- */
static gboolean signals_iowatch_cb(GIOChannel *chn, GIOCondition cnd, gpointer aptr);
static bool signals_init (void);
static void signals_quit (void);
/* ------------------------------------------------------------------------- *
* UNIX_SERVER
* ------------------------------------------------------------------------- */
static bool unix_server_handle_client(void);
static gboolean unix_server_iowatch_cb (GIOChannel *chn, GIOCondition cnd, gpointer aptr);
static bool unix_server_addr (struct sockaddr_un *sa, socklen_t *sa_len);
static bool unix_server_init (void);
static void unix_server_quit (void);
/* ------------------------------------------------------------------------- *
* UNIX_CLIENT
* ------------------------------------------------------------------------- */
static bool unix_client_terminate_server(void);
/* ------------------------------------------------------------------------- *
* COMPOSITOR
* ------------------------------------------------------------------------- */
static void compositor_method_call_cb (GDBusConnection *connection, const gchar *sender, const gchar *object_path, const gchar *interface_name, const gchar *method_name, GVariant *parameters, GDBusMethodInvocation *invocation, gpointer user_data);
static GVariant *compositor_get_property_cb (GDBusConnection *connection, const gchar *sender, const gchar *object_path, const gchar *interface_name, const gchar *property_name, GError **error, gpointer user_data);
static gboolean compositor_set_property_cb (GDBusConnection *connection, const gchar *sender, const gchar *object_path, const gchar *interface_name, const gchar *property_name, GVariant *value, GError **error, gpointer user_data);
static void compositor_connected_cb (GDBusConnection *connection, const gchar *name, gpointer user_data);
static void compositor_name_acquired_cb(GDBusConnection *connection, const gchar *name, gpointer user_data);
static void compositor_name_lost_cb (GDBusConnection *connection, const gchar *name, gpointer user_data);
static gboolean compositor_connect_cb (gpointer aptr);
static void compositor_schedule_connect(void);
static void compositor_cancel_connect (void);
static void compositor_disconnect (void);
static bool compositor_init (void);
static void compositor_quit (void);
/* ------------------------------------------------------------------------- *
* APP
* ------------------------------------------------------------------------- */
static void app_notify_systemd (void);
static void app_on_enable_from_dbus (void);
static void app_add_image (const char *filename);
static void app_flush_images (void);
static void app_draw_ui (void);
static void app_draw_text (void);
static void app_draw_single_image_cb (void);
static void app_start_single_image (void);
static void app_draw_progress_bar_cb (void);
static gboolean app_update_progress_bar_cb (gpointer aptr);
static void app_start_progress_bar (void);
static void app_draw_animate_images_cb (void);
static gboolean app_update_animate_images_cb(gpointer aptr);
static void app_start_animate_images (void);
static gboolean app_start_cb (gpointer aptr);
static gboolean app_stop_cb (gpointer aptr);
static void app_print_short_help (void);
static void app_print_long_help (void);
/* ------------------------------------------------------------------------- *
* MAIN
* ------------------------------------------------------------------------- */
int main(int argc, char *argv[]);
/* ========================================================================= *
* DISPLAY
* ========================================================================= */
static bool display_acquired = false;
static bool display_released = false;
static bool display_enabled = false;
static bool display_blanked = false;
/** Acquire display
*
* Note: Tries for real only once, and only if display_release()
* has not been called yet.
*/
static void
display_acquire(void)
{
if (!display_acquired && !display_released) {
display_acquired = true;
if (gr_init(true) == -1) {
log_err("gr_init() failed");
display_release();
mainloop_stop();
}
else {
gr_color(0, 0, 0, 255);
gr_clear();
}
}
}
/** Release display
*
* Note: Blocks future display_acquire() calls
*/
static void
display_release(void)
{
if (!display_released) {
display_released = true;
freeLogo();
gr_exit();
}
}
/** Predicate for: app has the display
*/
static bool
display_is_acquired(void)
{
return display_acquired && !display_released;
}
/** Allow / deny UI from being drawn
*
* Called based on setUpdatesEnabled() dbus method calls from mce.
*/
static void
display_set_updates_enabled(bool enabled)
{
if (enabled)
display_acquire();
if (!display_is_acquired())
enabled = false;
if (display_enabled != enabled) {
if ((display_enabled = enabled)) {
display_set_blanked(false);
app_draw_ui();
}
else {
display_set_blanked(true);
}
}
}
/** Blank / unblank display
*
* Used for synchronizing display power state with
* setUpdatesEnabled() dbus method calls from mce.
*/
static void
display_set_blanked(bool blanked)
{
if (display_is_acquired()) {
if (display_blanked != blanked)
gr_fb_blank((display_blanked = blanked));
}
}
/** Predicate for: UI can draw
*/
static bool
display_can_be_drawn(void)
{
return display_is_acquired() && display_enabled && !display_blanked;
}
/* ========================================================================= *
* SYSTEMBUS
* ========================================================================= */
/** Path to D-Bus SystemBus socket */
#define SYSTEMBUS_SOCKET_PATH "/run/dbus/system_bus_socket"
static bool systembus_socket_exists = false;
static GFileMonitor *systembus_socket_monitor = NULL;
static gulong systembus_monitor_event_id = 0;
/** Predicate for: systembus connect can be attempted
*/
static bool
systembus_is_available(void)
{
return systembus_socket_exists;
}
/** Probe and react to systembus socket availability changes
*/
static void
systembus_probe_socket(void)
{
bool socket_exists = (access(SYSTEMBUS_SOCKET_PATH, F_OK) == 0);
if (systembus_socket_exists != socket_exists) {
log_debug("systembus_socket_exists: %s -> %s",
systembus_socket_exists ? "true" : "false",
socket_exists ? "true" : "false");
if ((systembus_socket_exists = socket_exists))
compositor_schedule_connect();
else
mainloop_stop();
}
}
/** Callback for handling systembus socket monitor events
*/
static void
systembus_socket_monitor_event_cb(GFileMonitor *mon,
GFile *file,
GFile *other_file,
GFileMonitorEvent event_type,
gpointer user_data)
{
(void)mon;
(void)file;
(void)other_file;
(void)event_type;
(void)user_data;
systembus_probe_socket();
}
/** Stop monitoring systembus socket
*/
static void
systembus_quit_socket_monitor(void)
{
if (systembus_socket_monitor) {
if (systembus_monitor_event_id) {
g_signal_handler_disconnect(systembus_socket_monitor,
systembus_monitor_event_id),
systembus_monitor_event_id = 0;
}
g_object_unref(systembus_socket_monitor),
systembus_socket_monitor = NULL;
}
}
/** Start monitoring systembus socket
*/
static bool
systembus_init_socket_monitor(void)
{
bool ack = false;
GFileMonitor *mon = NULL;
GFile *file = NULL;
GFileMonitorFlags flags = G_FILE_MONITOR_WATCH_MOVES;
GError *err = NULL;
gulong id = 0;
if (!(file = g_file_new_for_path(SYSTEMBUS_SOCKET_PATH))) {
log_err("%s: failed to create file object",
SYSTEMBUS_SOCKET_PATH);
goto cleanup;
}
if (!(mon = g_file_monitor_file(file, flags, NULL, &err))) {
log_err("%s: failed to create monitor object: %s",
SYSTEMBUS_SOCKET_PATH, err->message);
goto cleanup;
}
if (!(id = g_signal_connect(G_OBJECT(mon), "changed",
G_CALLBACK(systembus_socket_monitor_event_cb),
NULL))) {
log_err("%s: failed to subscribe monitor sginals",
SYSTEMBUS_SOCKET_PATH);
goto cleanup;
}
systembus_socket_monitor = mon, mon = NULL;
systembus_monitor_event_id = id, id = 0;
ack = true;
systembus_probe_socket();
cleanup:
if (id)
g_signal_handler_disconnect(mon, id);
if (mon)
g_object_unref(mon);
if (file)
g_object_unref(file);
g_clear_error(&err);
return ack;
}
/* ========================================================================= *
* MAINLOOP
* ========================================================================= */
static GMainLoop *mainloop_handle = NULL;
/** Run glib mainloop
*/
static void
mainloop_run(void)
{
mainloop_handle = g_main_loop_new(NULL, FALSE);
g_main_loop_run(mainloop_handle);
g_main_loop_unref(mainloop_handle),
mainloop_handle = NULL;
}
/** Stop glib mainloop
*
* Note: If glib mainloop is not running, makes immediate exit without
* returning to the caller.
*/
static void
mainloop_stop(void)
{
if (!mainloop_handle)
_exit(EXIT_FAILURE);
g_main_loop_quit(mainloop_handle);
}
/* ========================================================================= *
* SIGNALS
* ========================================================================= */
static int signals_signal_fd = -1;
static guint signals_iowatch_id = 0;
/** Callback for handling signalfd events
*/
static gboolean
signals_iowatch_cb(GIOChannel *chn, GIOCondition cnd, gpointer aptr)
{
(void)chn;
(void)cnd;
(void)aptr;
/* Acknowledge the signal as received */
struct signalfd_siginfo si = {};
if (read(signals_signal_fd, &si, sizeof si) == -1)
log_err("Could not read signal fd: %m");
else
log_err("Caught signal %u: %s",
(unsigned)si.ssi_signo, strsignal(si.ssi_signo));
/* Request exit from mainloop */
mainloop_stop();
/* Remove iowatch from bookkeeping, restore default signal handlers,
* and tell glib that the iowatch can be removed */
signals_iowatch_id = 0;
signals_quit();
return G_SOURCE_REMOVE;
}
/** Setup async signal handling
*
* Uses signalfd for forwarding signal processing in mainloop context.
*/
static bool
signals_init(void)
{
bool success = false;
int fd = -1;
GIOChannel *chn = NULL;
guint wid = 0;
GIOCondition cnd = G_IO_IN | G_IO_ERR | G_IO_HUP | G_IO_NVAL;
sigset_t mask;
sigemptyset(&mask);
sigaddset(&mask, SIGTERM);
sigaddset(&mask, SIGINT);
if ((fd = signalfd(-1, &mask, 0)) == -1) {
log_err("Could not create signal fd");
goto cleanup;
}
if (!(chn = g_io_channel_unix_new(fd))) {
log_err("Could not create signal fd io channel");
goto cleanup;
}
if (!(wid = g_io_add_watch(chn, cnd, signals_iowatch_cb, NULL))) {
log_err("Could not create add signal fd io watch");
goto cleanup;
}
if (sigprocmask(SIG_BLOCK, &mask, NULL) == -1) {
log_err("Could not block signals");
goto cleanup;
}
signals_signal_fd = fd, fd = -1;
signals_iowatch_id = wid, wid = 0;
success = true;
cleanup:
if (wid)
g_source_remove(wid);
if (chn)
g_io_channel_unref(chn);
if (fd != -1)
close(fd);
return success;
}
/** Restore default signal handling
*/
static void
signals_quit(void)
{
sigset_t mask;
sigemptyset(&mask);
sigaddset(&mask, SIGTERM);
sigaddset(&mask, SIGINT);
if (sigprocmask(SIG_UNBLOCK, &mask, NULL) == -1)
log_err("Could not unblock signals");
if (signals_iowatch_id) {
g_source_remove(signals_iowatch_id),
signals_iowatch_id = 0;
}
if (signals_signal_fd != -1) {
close(signals_signal_fd),
signals_signal_fd = -1;
}
}
/* ========================================================================= *
* UNIX_SERVER
* ========================================================================= */
static const char unix_server_path[] = "@yamuisplash";
static int unix_server_socket_fd = -1;
static guint unix_server_iowatch_id = 0;
static int unix_server_client_fd = -1;
/** Handle client connecting to unix socket
*/
static bool
unix_server_handle_client(void)
{
if (unix_server_socket_fd == -1)
goto cleanup;
struct sockaddr_un sa = { };
socklen_t sa_len = sizeof sa;
int fd = accept(unix_server_socket_fd, (struct sockaddr *)&sa, &sa_len);
if (fd == -1) {
log_err("%s: accept(): %m", unix_server_path);
goto cleanup;
}
/* What we want to happen is: client gets eof when this
* process is terminated. File descriptors are intentionally
* leaked and not explicitly closed to achieve this. */
unix_server_client_fd = fd;
log_debug("%s: server terminate requested", unix_server_path);
cleanup:
return unix_server_client_fd != -1;
}
/** I/O watch callback for handling connects to server socket
*/
static gboolean
unix_server_iowatch_cb(GIOChannel *chn, GIOCondition cnd, gpointer aptr)
{
(void)chn;
(void)aptr;
if (cnd & ~G_IO_IN) {
unix_server_iowatch_id = 0;
mainloop_stop();
return G_SOURCE_REMOVE;
}
if (unix_server_handle_client())
mainloop_stop();
return G_SOURCE_CONTINUE;
}
static bool
unix_server_addr(struct sockaddr_un *sa, socklen_t *sa_len)
{
socklen_t len = strnlen(unix_server_path, sizeof sa->sun_path) + 1;
if (len > sizeof sa->sun_path) {
log_err("%s: unix socket path too long", unix_server_path);
return false;
}
memset(sa, 0, sizeof *sa);
sa->sun_family = AF_UNIX;
strcpy(sa->sun_path, unix_server_path);
/* Starts with a '@' -> turn into abstract address */
if (sa->sun_path[0] == '@')
sa->sun_path[0] = 0;
len += offsetof(struct sockaddr_un, sun_path);
*sa_len = len;
return true;
}
/** Start unix socket server
*
* This is used for controlled terminating of an already running splashscreen
* application in situations where dbus systembus is not available yet and
* thus can't be used for controlling mutually exclusive access to graphics
* sw stack.
*/
static bool
unix_server_init(void)
{
int fd = -1;
GIOChannel *chn = NULL;
guint wid = 0;
GIOCondition cnd = G_IO_IN | G_IO_ERR | G_IO_HUP | G_IO_NVAL;
struct sockaddr_un sa = {};
socklen_t len = 0;
if (unix_server_iowatch_id != 0)
goto cleanup;
if (!unix_server_addr(&sa, &len))
goto cleanup;
if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
log_err("%s: socket(): %m", unix_server_path);
goto cleanup;
}
if (bind(fd, (struct sockaddr *)&sa, len) == -1) {
log_err("%s: bind(): %m", unix_server_path);
goto cleanup;
}
if (listen(fd, 1) == -1) {
log_err("%s: listen(): %m", unix_server_path);
goto cleanup;
}
if (!(chn = g_io_channel_unix_new(fd))) {
log_err("Could not create signal fd io channel");
goto cleanup;
}
if (!(wid = g_io_add_watch(chn, cnd, unix_server_iowatch_cb, NULL))) {
log_err("Could not create add signal fd io watch");
goto cleanup;
}
unix_server_socket_fd = fd, fd = -1;
unix_server_iowatch_id = wid, wid = 0;
cleanup:
if (wid)
g_source_remove(wid);
if (chn)
g_io_channel_unref(chn);
if (fd != -1)
close(fd);
return unix_server_iowatch_id != 0;
}
/** Stop unix socket server
*/
static void
unix_server_quit(void)
{
if (unix_server_iowatch_id)
g_source_remove(unix_server_iowatch_id), unix_server_iowatch_id = 0;
if (unix_server_socket_fd != -1)
close(unix_server_socket_fd), unix_server_socket_fd = -1;
}
/* ========================================================================= *
* UNIX_CLIENT
* ========================================================================= */
/** Terminate already running splashscreen application via unix socket ipc
*
* This is expected to work only when there is splashscreen application
* running that was started before systembus became available.
*/
static bool
unix_client_terminate_server(void)
{
bool ack = false;
int fd = -1;
struct sockaddr_un sa = {};
socklen_t len = 0;
if (!unix_server_addr(&sa, &len))
goto cleanup;
if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
log_err("%s: socket(): %m", unix_server_path);
goto cleanup;
}
if (connect(fd, (struct sockaddr *)&sa, len) == -1) {
if (errno == ECONNREFUSED) {
log_debug("%s: server not running", unix_server_path);
goto success;
}
log_err("%s: connect(): %m", unix_server_path);
goto cleanup;
}
char tmp[32];
int rc = read(fd, tmp, sizeof tmp);
if (rc == -1) {
log_err("%s: read(): %m", unix_server_path);
goto cleanup;
}
if (rc > 0) {
log_err("%s: read(): got data?", unix_server_path);
goto cleanup;
}
log_debug("%s: read(): got EOF", unix_server_path);
success:
ack = true;
cleanup:
if (fd != -1)
close(fd);
return ack;
}
/* ========================================================================= *
* COMPOSITOR
* ========================================================================= */
/** Well known dbus name of compositor service */
#define COMPOSITOR_SERVICE "org.nemomobile.compositor"
#define COMPOSITOR_PATH "/"
#define COMPOSITOR_IFACE "org.nemomobile.compositor"
/** Enabling/disabling display updates via compositor service */
#define COMPOSITOR_SET_UPDATES_ENABLED "setUpdatesEnabled"
/** Query owner of topmost ui window */
#define COMPOSITOR_GET_TOPMOST_WINDOW_PID "privateTopmostWindowProcessId"
/** Change notification for owner of topmost ui window */
#define COMPOSITOR_TOPMOST_WINDOW_PID_CHANGED "privateTopmostWindowProcessIdChanged"
/** Query requirements of this compositor process */
#define COMPOSITOR_GET_SETUP_ACTIONS "privateGetSetupActions"
/** Setup actions supported by mce */
#define COMPOSITOR_ACTION_NONE 0
#define COMPOSITOR_ACTION_STOP_HWC (1<<0)
#define COMPOSITOR_ACTION_START_HWC (1<<1)
#define COMPOSITOR_ACTION_RESTART_HWC (1<<2)
/** Introspect XML - needed for setting up glib based dbus service */
static const char introspect_xml[] = ""
"<!DOCTYPE node PUBLIC \"-//freedesktop//DTD D-BUS Object Introspection 1.0//EN\"\n"
"\"http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd\">\n"
"<node>\n"
" <interface name=\"" COMPOSITOR_IFACE "\">\n"
" <method name=\"" COMPOSITOR_SET_UPDATES_ENABLED "\">\n"
" <arg direction=\"in\" type=\"b\" name=\"enabled\"/>\n"
" </method>\n"
" <method name=\"" COMPOSITOR_GET_TOPMOST_WINDOW_PID "\">\n"
" <arg direction=\"out\" type=\"i\" name=\"pid\"/>\n"
" </method>\n"
" <method name=\"" COMPOSITOR_GET_SETUP_ACTIONS "\">\n"
" <arg direction=\"out\" type=\"u\" name=\"flags\"/>\n"
" </method>\n"
" <signal name=\"" COMPOSITOR_TOPMOST_WINDOW_PID_CHANGED "\">\n"
" <arg type=\"i\" name=\"pid\"/>\n"
" </signal>\n"
" <method name=\"privateTopmostWindowPolicyApplicationId\">\n"
" <arg direction=\"out\" type=\"s\" name=\"id\"/>\n"
" </method>\n"
" <signal name=\"privateTopmostWindowPolicyApplicationIdChanged\">\n"
" <arg type=\"s\" name=\"id\"/>\n"
" </signal>\n"
" </interface>\n"
"</node>\n";
static const GDBusInterfaceVTable compositor_interface_vtable =
{
compositor_method_call_cb,
compositor_get_property_cb,
compositor_set_property_cb,
};
static GDBusNodeInfo *compositor_introspect_data = NULL;
static guint compositor_name_owning_id = 0;
static guint compositor_connect_id = 0;
static bool compositor_name_acquired = false;
/** Callback for handling incoming method call messages
*/
static void
compositor_method_call_cb(GDBusConnection *connection,
const gchar *sender,
const gchar *object_path,
const gchar *interface_name,
const gchar *method_name,
GVariant *parameters,
GDBusMethodInvocation *invocation,
gpointer user_data)
{
(void)connection;
(void)sender;
(void)object_path;
(void)user_data;
log_debug("obj: %s method: %s.%s", object_path, interface_name, method_name);
if (!g_strcmp0(method_name, COMPOSITOR_SET_UPDATES_ENABLED)) {
gboolean enabled = FALSE;
g_variant_get(parameters, "(b)", &enabled);
log_debug("enabled := %s", enabled ? "true" : "false");
display_set_updates_enabled(enabled);
if (enabled)
app_on_enable_from_dbus();
g_dbus_method_invocation_return_value(invocation, NULL);
}
else if (!g_strcmp0(method_name, COMPOSITOR_GET_TOPMOST_WINDOW_PID)) {
gint pid = getpid();
log_debug("pid == %d", pid);
g_dbus_method_invocation_return_value(invocation,
g_variant_new("(i)", pid));
}
else if (!g_strcmp0(method_name, COMPOSITOR_GET_SETUP_ACTIONS)) {
guint flags = COMPOSITOR_ACTION_STOP_HWC;
log_debug("flags == 0x%x", flags);
g_dbus_method_invocation_return_value(invocation,
g_variant_new("(u)", flags));
}
else {
log_err("Unhandled method: %s.%s", interface_name, method_name);
g_dbus_method_invocation_return_error(invocation,
G_DBUS_ERROR,
G_DBUS_ERROR_NOT_SUPPORTED,
"unknown method: %s",
method_name);
}
}
/** Dummy callback for handling incoming dbus property Get method calls
*/
static GVariant *
compositor_get_property_cb(GDBusConnection *connection,
const gchar *sender,
const gchar *object_path,
const gchar *interface_name,
const gchar *property_name,
GError **error,
gpointer user_data)
{
(void)connection;
(void)sender;
(void)object_path;
(void)interface_name;
(void)property_name;
(void)error;
(void)user_data;
GVariant *res = NULL;
return res;
}
/** Dummy callback for handling incoming dbus property Set method calls
*/
static gboolean
compositor_set_property_cb(GDBusConnection *connection,
const gchar *sender,
const gchar *object_path,
const gchar *interface_name,
const gchar *property_name,
GVariant *value,
GError **error,
gpointer user_data)
{
(void)connection;
(void)sender;
(void)object_path;
(void)interface_name;
(void)property_name;
(void)value;
(void)error;
(void)user_data;
gboolean res = FALSE;
return res;
}
/** Callback for connected-to-dbus phase of compositor name owning
*/
static void
compositor_connected_cb(GDBusConnection *connection,
const gchar *name,
gpointer user_data)
{
(void)name;
(void)user_data;
log_debug("bus_acquired: %p %s", connection, name);
guint registration_id =
g_dbus_connection_register_object(connection,
COMPOSITOR_PATH,
compositor_introspect_data->interfaces[0],
&compositor_interface_vtable,
NULL, /* user_data */
NULL, /* user_data_free_func */
NULL); /* GError** */
if (!registration_id)
mainloop_stop();
}
/** Callback for name-acquired phase of compositor name owning
*/
static void
compositor_name_acquired_cb(GDBusConnection *connection,
const gchar *name,
gpointer user_data)
{
(void)connection;
(void)name;
(void)user_data;
log_debug("name_acquired: %p %s", connection, name);
compositor_name_acquired = true;
}
/** Callback for name-lost phase of compositor name owning
*/
static void
compositor_name_lost_cb(GDBusConnection *connection,
const gchar *name,
gpointer user_data)
{
(void)name;
(void)user_data;
log_debug("name_lost: %p %s", connection, name);
if (!connection) {
log_err("dbus connection failure");
mainloop_stop();
}
else if (compositor_name_acquired) {
log_debug("service handover");
mainloop_stop();
}
else {
log_debug("waiting for name...");
}
}
/** Idle callback for delayed handling of SystemBus available
*/
static gboolean
compositor_connect_cb(gpointer aptr)
{
(void)aptr;
compositor_connect_id = 0;
if (!systembus_is_available())
goto cleanup;
if (!compositor_name_owning_id) {
log_debug("dbus connect");
GBusNameOwnerFlags flags =
G_BUS_NAME_OWNER_FLAGS_REPLACE |
G_BUS_NAME_OWNER_FLAGS_ALLOW_REPLACEMENT;
log_debug("own name flags: 0x%x", flags);
compositor_name_owning_id =
g_bus_own_name(G_BUS_TYPE_SYSTEM,
COMPOSITOR_SERVICE,
flags,
compositor_connected_cb,
compositor_name_acquired_cb,
compositor_name_lost_cb,
NULL, NULL);
}
cleanup:
return G_SOURCE_REMOVE;
}
/** Schedule connect to systembus
*/
static void
compositor_schedule_connect(void)
{
if (!compositor_connect_id) {
compositor_connect_id =
g_timeout_add(50, compositor_connect_cb, NULL);
}
}
/** Cancel scheduled connect to systembus
*/
static void