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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
|
/*
* Seats
*
* Copyright (c) 2012 David Herrmann <dh.herrmann@googlemail.com>
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files
* (the "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
/*
* Seats
* A seat is a single session that is self-hosting and provides all the
* interaction for a single logged-in user.
*/
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include "conf.h"
#include "eloop.h"
#include "kmscon_conf.h"
#include "kmscon_dummy.h"
#include "kmscon_seat.h"
#include "kmscon_terminal.h"
#include "shl_dlist.h"
#include "shl_log.h"
#include "uterm_input.h"
#include "uterm_video.h"
#include "uterm_vt.h"
#define LOG_SUBSYSTEM "seat"
struct kmscon_session {
struct shl_dlist list;
unsigned long ref;
struct kmscon_seat *seat;
bool enabled;
bool foreground;
bool deactivating;
struct ev_timer *timer;
kmscon_session_cb_t cb;
void *data;
};
struct kmscon_display {
struct shl_dlist list;
struct kmscon_seat *seat;
struct uterm_display *disp;
bool activated;
};
enum kmscon_async_schedule {
SCHEDULE_SWITCH,
SCHEDULE_VT,
SCHEDULE_UNREGISTER,
};
struct kmscon_seat {
struct ev_eloop *eloop;
struct uterm_vt_master *vtm;
struct conf_ctx *conf_ctx;
struct kmscon_conf_t *conf;
char *name;
struct uterm_input *input;
struct uterm_vt *vt;
struct shl_dlist displays;
size_t session_count;
struct shl_dlist sessions;
bool awake;
bool foreground;
struct kmscon_session *current_sess;
struct kmscon_session *scheduled_sess;
struct kmscon_session *dummy_sess;
unsigned int async_schedule;
kmscon_seat_cb_t cb;
void *data;
};
static int session_call(struct kmscon_session *sess, unsigned int event,
struct uterm_display *disp)
{
struct kmscon_session_event ev;
if (!sess->cb)
return 0;
memset(&ev, 0, sizeof(ev));
ev.type = event;
ev.disp = disp;
return sess->cb(sess, &ev, sess->data);
}
static int session_call_activate(struct kmscon_session *sess)
{
log_debug("activate session %p", sess);
return session_call(sess, KMSCON_SESSION_ACTIVATE, NULL);
}
static int session_call_deactivate(struct kmscon_session *sess)
{
log_debug("deactivate session %p", sess);
return session_call(sess, KMSCON_SESSION_DEACTIVATE, NULL);
}
static void session_call_display_new(struct kmscon_session *sess,
struct uterm_display *disp)
{
session_call(sess, KMSCON_SESSION_DISPLAY_NEW, disp);
}
static void session_call_display_gone(struct kmscon_session *sess,
struct uterm_display *disp)
{
session_call(sess, KMSCON_SESSION_DISPLAY_GONE, disp);
}
static void session_call_display_refresh(struct kmscon_session *sess,
struct uterm_display *disp)
{
session_call(sess, KMSCON_SESSION_DISPLAY_REFRESH, disp);
}
static void activate_display(struct kmscon_display *d)
{
int ret;
struct shl_dlist *iter, *tmp;
struct kmscon_session *s;
struct kmscon_seat *seat = d->seat;
if (d->activated || !d->seat->awake || !d->seat->foreground)
return;
/* TODO: We always use the default mode for new displays but we should
* rather allow the user to specify different modes in the configuration
* files. */
if (uterm_display_get_state(d->disp) == UTERM_DISPLAY_INACTIVE) {
ret = uterm_display_activate(d->disp, NULL);
if (ret)
return;
d->activated = true;
ret = uterm_display_set_dpms(d->disp, UTERM_DPMS_ON);
if (ret)
log_warning("cannot set DPMS state to on for display: %d",
ret);
shl_dlist_for_each_safe(iter, tmp, &seat->sessions) {
s = shl_dlist_entry(iter, struct kmscon_session, list);
session_call_display_new(s, d->disp);
}
}
}
static int seat_go_foreground(struct kmscon_seat *seat, bool force)
{
int ret;
struct shl_dlist *iter;
struct kmscon_display *d;
if (seat->foreground)
return 0;
if (!seat->awake || (!force && seat->current_sess))
return -EBUSY;
if (seat->cb) {
ret = seat->cb(seat, KMSCON_SEAT_FOREGROUND, seat->data);
if (ret) {
log_warning("cannot put seat %s into foreground: %d",
seat->name, ret);
return ret;
}
}
seat->foreground = true;
shl_dlist_for_each(iter, &seat->displays) {
d = shl_dlist_entry(iter, struct kmscon_display, list);
activate_display(d);
}
return 0;
}
static int seat_go_background(struct kmscon_seat *seat, bool force)
{
int ret;
if (!seat->foreground)
return 0;
if (!seat->awake || (!force && seat->current_sess))
return -EBUSY;
if (seat->cb) {
ret = seat->cb(seat, KMSCON_SEAT_BACKGROUND, seat->data);
if (ret) {
log_warning("cannot put seat %s into background: %d",
seat->name, ret);
return ret;
}
}
seat->foreground = false;
return 0;
}
static int seat_go_asleep(struct kmscon_seat *seat, bool force)
{
int ret, err = 0;
if (!seat->awake)
return 0;
if (seat->current_sess || seat->foreground) {
if (force) {
seat->foreground = false;
seat->current_sess = NULL;
err = -EBUSY;
} else {
return -EBUSY;
}
}
if (seat->cb) {
ret = seat->cb(seat, KMSCON_SEAT_SLEEP, seat->data);
if (ret) {
log_warning("cannot put seat %s asleep: %d",
seat->name, ret);
if (!force)
return ret;
}
}
seat->awake = false;
uterm_input_sleep(seat->input);
return err;
}
static int seat_go_awake(struct kmscon_seat *seat)
{
int ret;
if (seat->awake)
return 0;
if (seat->cb) {
ret = seat->cb(seat, KMSCON_SEAT_WAKE_UP, seat->data);
if (ret) {
log_warning("cannot wake up seat %s: %d", seat->name,
ret);
return ret;
}
}
seat->awake = true;
uterm_input_wake_up(seat->input);
return 0;
}
static int seat_run(struct kmscon_seat *seat)
{
int ret;
struct kmscon_session *session;
if (!seat->awake)
return -EBUSY;
if (seat->current_sess)
return 0;
if (!seat->scheduled_sess) {
log_debug("no session scheduled to run (num %zu)",
seat->session_count);
return -ENOENT;
}
session = seat->scheduled_sess;
if (session->foreground && !seat->foreground) {
ret = seat_go_foreground(seat, false);
if (ret) {
log_warning("cannot put seat %s into foreground for session %p",
seat->name, session);
return ret;
}
} else if (!session->foreground && seat->foreground) {
ret = seat_go_background(seat, false);
if (ret) {
log_warning("cannot put seat %s into background for session %p",
seat->name, session);
return ret;
}
}
ret = session_call_activate(session);
if (ret) {
log_warning("cannot activate session %p: %d", session, ret);
return ret;
}
seat->current_sess = session;
return 0;
}
static void session_deactivate(struct kmscon_session *sess)
{
if (sess->seat->current_sess != sess)
return;
sess->seat->async_schedule = SCHEDULE_SWITCH;
sess->deactivating = false;
sess->seat->current_sess = NULL;
}
static int seat_pause(struct kmscon_seat *seat, bool force)
{
int ret;
if (!seat->current_sess)
return 0;
seat->current_sess->deactivating = true;
ret = session_call_deactivate(seat->current_sess);
if (ret) {
if (ret == -EINPROGRESS)
log_debug("pending deactivation for session %p",
seat->current_sess);
else
log_warning("cannot deactivate session %p: %d",
seat->current_sess, ret);
if (!force)
return ret;
}
session_deactivate(seat->current_sess);
return ret;
}
static void seat_reschedule(struct kmscon_seat *seat)
{
struct shl_dlist *iter, *start;
struct kmscon_session *sess;
if (seat->scheduled_sess && seat->scheduled_sess->enabled)
return;
if (seat->current_sess && seat->current_sess->enabled) {
seat->scheduled_sess = seat->current_sess;
return;
}
if (seat->current_sess)
start = &seat->current_sess->list;
else
start = &seat->sessions;
shl_dlist_for_each_but_one(iter, start, &seat->sessions) {
sess = shl_dlist_entry(iter, struct kmscon_session, list);
if (sess == seat->dummy_sess || !sess->enabled)
continue;
seat->scheduled_sess = sess;
return;
}
if (seat->dummy_sess && seat->dummy_sess->enabled)
seat->scheduled_sess = seat->dummy_sess;
else
seat->scheduled_sess = NULL;
}
static bool seat_has_schedule(struct kmscon_seat *seat)
{
return seat->scheduled_sess &&
seat->scheduled_sess != seat->current_sess;
}
static int seat_switch(struct kmscon_seat *seat)
{
int ret;
seat->async_schedule = SCHEDULE_SWITCH;
ret = seat_pause(seat, false);
if (ret)
return ret;
return seat_run(seat);
}
static void seat_next(struct kmscon_seat *seat)
{
struct shl_dlist *cur, *iter;
struct kmscon_session *s, *next;
if (seat->current_sess)
cur = &seat->current_sess->list;
else if (seat->session_count)
cur = &seat->sessions;
else
return;
next = NULL;
if (!seat->current_sess && seat->dummy_sess &&
seat->dummy_sess->enabled)
next = seat->dummy_sess;
shl_dlist_for_each_but_one(iter, cur, &seat->sessions) {
s = shl_dlist_entry(iter, struct kmscon_session, list);
if (!s->enabled || seat->dummy_sess == s)
continue;
next = s;
break;
}
if (!next)
return;
seat->scheduled_sess = next;
seat_switch(seat);
}
static void seat_prev(struct kmscon_seat *seat)
{
struct shl_dlist *cur, *iter;
struct kmscon_session *s, *prev;
if (seat->current_sess)
cur = &seat->current_sess->list;
else if (seat->session_count)
cur = &seat->sessions;
else
return;
prev = NULL;
if (!seat->current_sess && seat->dummy_sess &&
seat->dummy_sess->enabled)
prev = seat->dummy_sess;
shl_dlist_for_each_reverse_but_one(iter, cur, &seat->sessions) {
s = shl_dlist_entry(iter, struct kmscon_session, list);
if (!s->enabled || seat->dummy_sess == s)
continue;
prev = s;
break;
}
if (!prev)
return;
seat->scheduled_sess = prev;
seat_switch(seat);
}
static int seat_add_display(struct kmscon_seat *seat,
struct uterm_display *disp)
{
struct kmscon_display *d;
log_debug("add display %p to seat %s", disp, seat->name);
d = malloc(sizeof(*d));
if (!d)
return -ENOMEM;
memset(d, 0, sizeof(*d));
d->disp = disp;
d->seat = seat;
uterm_display_ref(d->disp);
shl_dlist_link(&seat->displays, &d->list);
activate_display(d);
return 0;
}
static void seat_remove_display(struct kmscon_seat *seat,
struct kmscon_display *d)
{
struct shl_dlist *iter, *tmp;
struct kmscon_session *s;
log_debug("remove display %p from seat %s", d->disp, seat->name);
shl_dlist_unlink(&d->list);
if (d->activated) {
shl_dlist_for_each_safe(iter, tmp, &seat->sessions) {
s = shl_dlist_entry(iter, struct kmscon_session, list);
session_call_display_gone(s, d->disp);
}
}
uterm_display_unref(d->disp);
free(d);
}
static void seat_refresh_display(struct kmscon_seat *seat,
struct kmscon_display *d)
{
struct shl_dlist *iter;
struct kmscon_session *s;
log_debug("refresh display %p from seat %s", d->disp, seat->name);
if (d->activated) {
shl_dlist_for_each(iter, &seat->sessions) {
s = shl_dlist_entry(iter, struct kmscon_session, list);
session_call_display_refresh(s, d->disp);
}
}
}
static int seat_vt_event(struct uterm_vt *vt, struct uterm_vt_event *ev,
void *data)
{
struct kmscon_seat *seat = data;
int ret;
switch (ev->action) {
case UTERM_VT_ACTIVATE:
ret = seat_go_awake(seat);
if (ret)
return ret;
seat_run(seat);
break;
case UTERM_VT_DEACTIVATE:
seat->async_schedule = SCHEDULE_VT;
ret = seat_pause(seat, false);
if (ret)
return ret;
ret = seat_go_background(seat, false);
if (ret)
return ret;
ret = seat_go_asleep(seat, false);
if (ret)
return ret;
break;
case UTERM_VT_HUP:
if (seat->cb)
seat->cb(seat, KMSCON_SEAT_HUP, seat->data);
break;
}
return 0;
}
static void seat_input_event(struct uterm_input *input,
struct uterm_input_event *ev,
void *data)
{
struct kmscon_seat *seat = data;
struct kmscon_session *s;
int ret;
if (ev->handled || !seat->awake)
return;
if (conf_grab_matches(seat->conf->grab_session_next,
ev->mods, ev->num_syms, ev->keysyms)) {
ev->handled = true;
if (!seat->conf->session_control)
return;
seat_next(seat);
return;
}
if (conf_grab_matches(seat->conf->grab_session_prev,
ev->mods, ev->num_syms, ev->keysyms)) {
ev->handled = true;
if (!seat->conf->session_control)
return;
seat_prev(seat);
return;
}
if (conf_grab_matches(seat->conf->grab_session_dummy,
ev->mods, ev->num_syms, ev->keysyms)) {
ev->handled = true;
if (!seat->conf->session_control)
return;
seat->scheduled_sess = seat->dummy_sess;
seat_switch(seat);
return;
}
if (conf_grab_matches(seat->conf->grab_session_close,
ev->mods, ev->num_syms, ev->keysyms)) {
ev->handled = true;
if (!seat->conf->session_control)
return;
s = seat->current_sess;
if (!s)
return;
if (s == seat->dummy_sess)
return;
/* First time this is invoked on a session, we simply try
* unloading it. If it fails, we give it some time. If this is
* invoked a second time, we notice that we already tried
* removing it and so we go straight to unregistering the
* session unconditionally. */
if (!s->deactivating) {
seat->async_schedule = SCHEDULE_UNREGISTER;
ret = seat_pause(seat, false);
if (ret)
return;
}
kmscon_session_unregister(s);
return;
}
if (conf_grab_matches(seat->conf->grab_terminal_new,
ev->mods, ev->num_syms, ev->keysyms)) {
ev->handled = true;
if (!seat->conf->session_control)
return;
ret = kmscon_terminal_register(&s, seat,
uterm_vt_get_num(seat->vt));
if (ret == -EOPNOTSUPP) {
log_notice("terminal support not compiled in");
} else if (ret) {
log_error("cannot register terminal session: %d", ret);
} else {
s->enabled = true;
seat->scheduled_sess = s;
seat_switch(seat);
}
return;
}
}
int kmscon_seat_new(struct kmscon_seat **out,
struct conf_ctx *main_conf,
struct ev_eloop *eloop,
struct uterm_vt_master *vtm,
unsigned int vt_types,
const char *seatname,
kmscon_seat_cb_t cb,
void *data)
{
struct kmscon_seat *seat;
int ret;
char *keymap;
if (!out || !eloop || !vtm || !seatname)
return -EINVAL;
seat = malloc(sizeof(*seat));
if (!seat)
return -ENOMEM;
memset(seat, 0, sizeof(*seat));
seat->eloop = eloop;
seat->vtm = vtm;
seat->cb = cb;
seat->data = data;
shl_dlist_init(&seat->displays);
shl_dlist_init(&seat->sessions);
seat->name = strdup(seatname);
if (!seat->name) {
log_error("cannot copy string");
ret = -ENOMEM;
goto err_free;
}
ret = kmscon_conf_new(&seat->conf_ctx);
if (ret) {
log_error("cannot create seat configuration object: %d", ret);
goto err_name;
}
seat->conf = conf_ctx_get_mem(seat->conf_ctx);
ret = kmscon_conf_load_seat(seat->conf_ctx, main_conf, seat->name);
if (ret) {
log_error("cannot parse seat configuration on seat %s: %d",
seat->name, ret);
goto err_conf;
}
/* TODO: The XKB-API currently requires zero-terminated strings as
* keymap input. Hence, we have to read it in instead of using mmap().
* We should fix this upstream! */
keymap = NULL;
if (seat->conf->xkb_keymap && *seat->conf->xkb_keymap) {
ret = shl_read_file(seat->conf->xkb_keymap, &keymap, NULL);
if (ret)
log_error("cannot read keymap file %s: %d",
seat->conf->xkb_keymap, ret);
}
ret = uterm_input_new(&seat->input, seat->eloop,
seat->conf->xkb_model,
seat->conf->xkb_layout,
seat->conf->xkb_variant,
seat->conf->xkb_options,
keymap,
seat->conf->xkb_repeat_delay,
seat->conf->xkb_repeat_rate,
log_llog, NULL);
free(keymap);
if (ret)
goto err_conf;
ret = uterm_input_register_cb(seat->input, seat_input_event, seat);
if (ret)
goto err_input;
ret = uterm_vt_allocate(seat->vtm, &seat->vt,
vt_types, seat->name,
seat->input, seat->conf->vt, seat_vt_event,
seat);
if (ret)
goto err_input_cb;
ev_eloop_ref(seat->eloop);
uterm_vt_master_ref(seat->vtm);
*out = seat;
return 0;
err_input_cb:
uterm_input_unregister_cb(seat->input, seat_input_event, seat);
err_input:
uterm_input_unref(seat->input);
err_conf:
kmscon_conf_free(seat->conf_ctx);
err_name:
free(seat->name);
err_free:
free(seat);
return ret;
}
void kmscon_seat_free(struct kmscon_seat *seat)
{
struct kmscon_display *d;
struct kmscon_session *s;
int ret;
if (!seat)
return;
ret = seat_pause(seat, true);
if (ret)
log_warning("destroying seat %s while session %p is active",
seat->name, seat->current_sess);
ret = seat_go_asleep(seat, true);
if (ret)
log_warning("destroying seat %s while still awake: %d",
seat->name, ret);
while (!shl_dlist_empty(&seat->sessions)) {
s = shl_dlist_entry(seat->sessions.next,
struct kmscon_session,
list);
kmscon_session_unregister(s);
}
while (!shl_dlist_empty(&seat->displays)) {
d = shl_dlist_entry(seat->displays.next,
struct kmscon_display,
list);
seat_remove_display(seat, d);
}
uterm_vt_deallocate(seat->vt);
uterm_input_unregister_cb(seat->input, seat_input_event, seat);
uterm_input_unref(seat->input);
kmscon_conf_free(seat->conf_ctx);
free(seat->name);
uterm_vt_master_unref(seat->vtm);
ev_eloop_unref(seat->eloop);
free(seat);
}
void kmscon_seat_startup(struct kmscon_seat *seat)
{
int ret;
struct kmscon_session *s;
if (!seat)
return;
ret = kmscon_dummy_register(&s, seat);
if (ret == -EOPNOTSUPP) {
log_notice("dummy sessions not compiled in");
} else if (ret) {
log_error("cannot register dummy session: %d", ret);
} else {
seat->dummy_sess = s;
kmscon_session_enable(s);
}
if (seat->conf->terminal_session) {
ret = kmscon_terminal_register(&s, seat,
uterm_vt_get_num(seat->vt));
if (ret == -EOPNOTSUPP)
log_notice("terminal support not compiled in");
else if (ret)
log_error("cannot register terminal session");
else
kmscon_session_enable(s);
}
if (seat->conf->switchvt ||
uterm_vt_get_type(seat->vt) == UTERM_VT_FAKE)
uterm_vt_activate(seat->vt);
}
int kmscon_seat_add_display(struct kmscon_seat *seat,
struct uterm_display *disp)
{
if (!seat || !disp)
return -EINVAL;
return seat_add_display(seat, disp);
}
void kmscon_seat_remove_display(struct kmscon_seat *seat,
struct uterm_display *disp)
{
struct shl_dlist *iter;
struct kmscon_display *d;
if (!seat || !disp)
return;
shl_dlist_for_each(iter, &seat->displays) {
d = shl_dlist_entry(iter, struct kmscon_display, list);
if (d->disp != disp)
continue;
seat_remove_display(seat, d);
break;
}
}
void kmscon_seat_refresh_display(struct kmscon_seat *seat,
struct uterm_display *disp)
{
struct shl_dlist *iter;
struct kmscon_display *d;
if (!seat || !disp)
return;
shl_dlist_for_each(iter, &seat->displays) {
d = shl_dlist_entry(iter, struct kmscon_display, list);
if (d->disp != disp)
continue;
seat_refresh_display(seat, d);
break;
}
}
int kmscon_seat_add_input(struct kmscon_seat *seat, const char *node)
{
if (!seat || !node)
return -EINVAL;
uterm_input_add_dev(seat->input, node);
return 0;
}
void kmscon_seat_remove_input(struct kmscon_seat *seat, const char *node)
{
if (!seat || !node)
return;
uterm_input_remove_dev(seat->input, node);
}
const char *kmscon_seat_get_name(struct kmscon_seat *seat)
{
if (!seat)
return NULL;
return seat->name;
}
struct uterm_input *kmscon_seat_get_input(struct kmscon_seat *seat)
{
if (!seat)
return NULL;
return seat->input;
}
struct ev_eloop *kmscon_seat_get_eloop(struct kmscon_seat *seat)
{
if (!seat)
return NULL;
return seat->eloop;
}
struct conf_ctx *kmscon_seat_get_conf(struct kmscon_seat *seat)
{
if (!seat)
return NULL;
return seat->conf_ctx;
}
void kmscon_seat_schedule(struct kmscon_seat *seat, unsigned int id)
{
struct shl_dlist *iter;
struct kmscon_session *s, *next;
if (!seat)
return;
next = seat->dummy_sess;
shl_dlist_for_each(iter, &seat->sessions) {
s = shl_dlist_entry(iter, struct kmscon_session, list);
if (!s->enabled || seat->dummy_sess == s ||
seat->current_sess == s)
continue;
next = s;
if (!id--)
break;
}
seat->scheduled_sess = next;
if (seat_has_schedule(seat))
seat_switch(seat);
}
int kmscon_seat_register_session(struct kmscon_seat *seat,
struct kmscon_session **out,
kmscon_session_cb_t cb,
void *data)
{
struct kmscon_session *sess;
struct shl_dlist *iter;
struct kmscon_display *d;
if (!seat || !out)
return -EINVAL;
if (seat->conf->session_max &&
seat->session_count >= seat->conf->session_max) {
log_warning("maximum number of sessions reached (%d), dropping new session",
seat->conf->session_max);
return -EOVERFLOW;
}
sess = malloc(sizeof(*sess));
if (!sess) {
log_error("cannot allocate memory for new session on seat %s",
seat->name);
return -ENOMEM;
}
log_debug("register session %p", sess);
memset(sess, 0, sizeof(*sess));
sess->ref = 1;
sess->seat = seat;
sess->cb = cb;
sess->data = data;
sess->foreground = true;
/* register new sessions next to the current one */
if (seat->current_sess)
shl_dlist_link(&seat->current_sess->list, &sess->list);
else
shl_dlist_link_tail(&seat->sessions, &sess->list);
++seat->session_count;
*out = sess;
shl_dlist_for_each(iter, &seat->displays) {
d = shl_dlist_entry(iter, struct kmscon_display, list);
session_call_display_new(sess, d->disp);
}
return 0;
}
void kmscon_session_ref(struct kmscon_session *sess)
{
if (!sess || !sess->ref)
return;
++sess->ref;
}
void kmscon_session_unref(struct kmscon_session *sess)
{
if (!sess || !sess->ref || --sess->ref)
return;
kmscon_session_unregister(sess);
free(sess);
}
void kmscon_session_unregister(struct kmscon_session *sess)
{
struct kmscon_seat *seat;
int ret;
bool forced = false;
if (!sess || !sess->seat)
return;
log_debug("unregister session %p", sess);
seat = sess->seat;
sess->enabled = false;
if (seat->dummy_sess == sess)
seat->dummy_sess = NULL;
seat_reschedule(seat);
if (seat->current_sess == sess) {
ret = seat_pause(seat, true);
if (ret) {
forced = true;
log_warning("unregistering active session %p; skipping automatic session-switch",
sess);
}
}
shl_dlist_unlink(&sess->list);
--seat->session_count;
sess->seat = NULL;
session_call(sess, KMSCON_SESSION_UNREGISTER, NULL);
kmscon_session_unref(sess);
/* If this session was active and we couldn't deactivate it, then it
* might still have resources allocated that couldn't get freed. In this
* case we should not automatically switch to the next session as it is
* very likely that it will not be able to start.
* Instead, we stay inactive and wait for user/external input to switch
* to another session. This delay will then hopefully be long enough so
* all resources got freed. */
if (!forced)
seat_run(seat);
}
bool kmscon_session_is_registered(struct kmscon_session *sess)
{
return sess && sess->seat;
}
bool kmscon_session_is_active(struct kmscon_session *sess)
{
return sess && sess->seat && sess->seat->current_sess == sess;
}
int kmscon_session_set_foreground(struct kmscon_session *sess)
{
struct kmscon_seat *seat;
int ret;
if (!sess)
return -EINVAL;
if (sess->foreground)
return 0;
seat = sess->seat;
if (seat && seat->current_sess == sess && !seat->foreground) {
ret = seat_go_foreground(seat, true);
if (ret)
return ret;
}
sess->foreground = true;
return 0;
}
int kmscon_session_set_background(struct kmscon_session *sess)
{
struct kmscon_seat *seat;
int ret;
if (!sess)
return -EINVAL;
if (!sess->foreground)
return 0;
seat = sess->seat;
if (seat && seat->current_sess == sess && seat->foreground) {
ret = seat_go_background(seat, true);
if (ret)
return ret;
}
sess->foreground = false;
return 0;
}
void kmscon_session_schedule(struct kmscon_session *sess)
{
struct kmscon_seat *seat;
if (!sess || !sess->seat)
return;
seat = sess->seat;
seat->scheduled_sess = sess;
seat_reschedule(seat);
if (seat_has_schedule(seat))
seat_switch(seat);
}
void kmscon_session_enable(struct kmscon_session *sess)
{
if (!sess || sess->enabled)
return;
log_debug("enable session %p", sess);
sess->enabled = true;
if (sess->seat &&
(!sess->seat->current_sess ||
sess->seat->current_sess == sess->seat->dummy_sess)) {
sess->seat->scheduled_sess = sess;
if (seat_has_schedule(sess->seat))
seat_switch(sess->seat);
}
}
void kmscon_session_disable(struct kmscon_session *sess)
{
if (!sess || !sess->enabled)
return;
log_debug("disable session %p", sess);
sess->enabled = false;
}
bool kmscon_session_is_enabled(struct kmscon_session *sess)
{
return sess && sess->enabled;
}
void kmscon_session_notify_deactivated(struct kmscon_session *sess)
{
struct kmscon_seat *seat;
int ret;
unsigned int sched;
if (!sess || !sess->seat)
return;
seat = sess->seat;
if (seat->current_sess != sess)
return;
sched = seat->async_schedule;
log_debug("session %p notified core about deactivation (schedule: %u)",
sess, sched);
session_deactivate(sess);
seat_reschedule(seat);
if (sched == SCHEDULE_VT) {
ret = seat_go_background(seat, false);
if (ret)
return;
ret = seat_go_asleep(seat, false);
if (ret)
return;
uterm_vt_retry(seat->vt);
} else if (sched == SCHEDULE_UNREGISTER) {
kmscon_session_unregister(sess);
} else {
seat_switch(seat);
}
}
|