-
Notifications
You must be signed in to change notification settings - Fork 12
/
loop.c
150 lines (121 loc) · 3.37 KB
/
loop.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
#define _POSIX_C_SOURCE 2
#include "input.h"
#include "atomic.h"
#include "modeset.h"
#include "wayland.h"
#include "xkb.h"
#include <skalibs/iopause.h>
#include <skalibs/strerr.h>
#include <linux/input-event-codes.h>
#include <assert.h>
#include <fcntl.h>
#include <errno.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#define FDS 3
static int kill1;
static void spawn_client(char **argv) {
if (fork() == 0) {
execvp(argv[0], argv);
fprintf(stderr, "execvp %s: %s\n", argv[0], strerror(errno));
_exit(EXIT_FAILURE);
/* NOTREACHED */
}
}
static int handler_key(struct libinput_event *event) {
struct libinput_event_keyboard *event_keyboard =
libinput_event_get_keyboard_event(event);
assert(event_keyboard);
uint32_t key = libinput_event_keyboard_get_key(event_keyboard);
int state = libinput_event_keyboard_get_key_state(event_keyboard); // WARNING
xkb_update(key, state);
unsigned int mods_depressed, mods_latched, mods_locked, group;
xkb_get_modifiers(&mods_depressed, &mods_latched, &mods_locked, &group);
if (xkb_test_ctrlalt() && key == (kill1 ? KEY_DELETE : KEY_BACKSPACE))
return 1; // exit
if (xkb_test_ctrlalt() && key == KEY_ENTER) {
const char* child_argv[] = {"weston-terminal", NULL};
spawn_client((char**)child_argv); // emergency terminal
return 0;
}
wayland_send_key_and_mods(key, state, mods_depressed, mods_latched, mods_locked, group);
return 0;
}
static void handler_pointer(struct libinput_event *event) {
}
static int handler_input(struct libinput *li) {
int r;
r = libinput_dispatch(li);
assert(!r);
int exit = 0;
struct libinput_event *event;
while ((event = libinput_get_event(li))) {
switch (libinput_event_get_type(event)) {
case LIBINPUT_EVENT_KEYBOARD_KEY:
exit = exit || handler_key(event);
break;
case LIBINPUT_EVENT_POINTER_MOTION:
handler_pointer(event);
break;
default: break;
}
libinput_event_destroy(event);
}
return exit;
}
static void check_3_open() {
if (fcntl(3, F_GETFD) < 0) strerr_die1x(1, "readiness notification");
}
const char *usage = "usage: xyzzy [-k] [client]\n\
\n\
Experimental Wayland system compositor\n\
\n\
positional arguments:\n\
client client to exec\n\
\n\
optional arguments:\n\
-h show this help message and exit\n\
-k kill PID 1 on exit (becomes Ctrl+Alt+Del)\n\
-3 notify readiness on fd 3\n";
int main(int argc, char *argv[]) {
int notif = 0;
int opt;
while ((opt = getopt(argc, argv, "hk3")) != -1) {
switch (opt) {
case 'k': kill1 = 1; break;
case '3': check_3_open(); notif = 1; break;
default:
fprintf(stderr, usage);
return EXIT_FAILURE;
}
}
struct libinput *li = input_init();
modeset_init();
atomic_init();
wayland_init();
xkb_init();
if (notif) {
write(3, "\n", 1); // ready to accept connections
close(3);
}
if (argc > optind) spawn_client(argv+optind);
iopause_fd x[FDS] = {
{ libinput_get_fd(li), IOPAUSE_READ, 0 },
{ wayland_get_fd(), IOPAUSE_READ, 0 },
{ modeset_get_fd(), IOPAUSE_READ, 0 },
};
int exit = 0;
while (!exit) {
wayland_flush();
iopause(x, FDS, NULL, NULL);
if (x[0].revents & IOPAUSE_READ) exit = handler_input(li);
if (x[1].revents & IOPAUSE_READ) wayland_read();
if (x[2].revents & IOPAUSE_READ) drm_read();
}
modeset_cleanup();
input_fini(li);
if (kill1) kill(1, SIGINT);
}