This repository was archived by the owner on Jun 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
145 lines (120 loc) · 3.72 KB
/
main.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
#include <assert.h>
#include <unistd.h>
#include <wlr/util/log.h>
#include <wlr/backend/multi.h>
#include <wlr/backend/libinput.h>
#include <wlr/types/wlr_compositor.h>
#include <wlr/types/wlr_xdg_shell.h>
#include "gbm_allocator.h"
#include "backend/backend.h"
#include "gl_renderer.h"
#include "server.h"
static enum wlr_log_importance log_importance_liftoff_to_wlr(
enum liftoff_log_importance importance) {
switch (importance) {
case LIFTOFF_SILENT:
return WLR_SILENT;
case LIFTOFF_ERROR:
return WLR_ERROR;
case LIFTOFF_DEBUG:
return WLR_DEBUG;
}
abort();
}
static void handle_liftoff_log(enum liftoff_log_importance importance,
const char *fmt, va_list args) {
char wlr_fmt[1024];
int n = snprintf(wlr_fmt, sizeof(wlr_fmt), "[liftoff] %s", fmt);
assert(n >= 0 && n < (int)sizeof(wlr_fmt));
_wlr_vlog(log_importance_liftoff_to_wlr(importance), wlr_fmt, args);
}
int main(int argc, char *argv[]) {
struct glider_server server = {0};
wl_list_init(&server.outputs);
wl_list_init(&server.surfaces);
wlr_log_init(WLR_DEBUG, NULL);
liftoff_log_init(LIFTOFF_DEBUG, handle_liftoff_log);
const char *startup_cmd = NULL;
int c;
while ((c = getopt(argc, argv, "s:h")) != -1) {
switch (c) {
case 's':
startup_cmd = optarg;
break;
default:
printf("usage: %s [-s startup command]\n", argv[0]);
return 0;
}
}
server.display = wl_display_create();
if (server.display == NULL) {
wlr_log(WLR_ERROR, "wl_display_create failed");
return 1;
}
struct wlr_session *session = wlr_session_create(server.display);
if (session == NULL) {
return 1;
}
server.backend = wlr_multi_backend_create(server.display);
struct wlr_backend *drm_backend =
glider_drm_backend_create(server.display, session);
if (drm_backend == NULL) {
return 1;
}
wlr_multi_backend_add(server.backend, drm_backend);
struct wlr_backend *libinput_backend =
wlr_libinput_backend_create(server.display, session);
if (libinput_backend == NULL) {
return 1;
}
wlr_multi_backend_add(server.backend, libinput_backend);
// TODO: multi-GPU
int fd = glider_drm_backend_get_render_fd(drm_backend);
if (fd < 0) {
return 1;
}
struct glider_gbm_allocator *gbm_allocator =
glider_gbm_allocator_create(fd);
if (gbm_allocator == NULL) {
return 1;
}
server.allocator = &gbm_allocator->base;
server.renderer =
glider_gl_gbm_renderer_create(gbm_allocator->gbm_device);
if (server.renderer == NULL) {
return 1;
}
wlr_renderer_init_wl_display(server.renderer->renderer, server.display);
wlr_compositor_create(server.display, server.renderer->renderer);
server.xdg_shell = wlr_xdg_shell_create(server.display);
server.new_output.notify = handle_new_output;
wl_signal_add(&server.backend->events.new_output, &server.new_output);
server.new_input.notify = handle_new_input;
wl_signal_add(&server.backend->events.new_input, &server.new_input);
server.new_xdg_surface.notify = handle_new_xdg_surface;
wl_signal_add(&server.xdg_shell->events.new_surface,
&server.new_xdg_surface);
if (!wlr_backend_start(server.backend)) {
return 1;
}
const char *socket = wl_display_add_socket_auto(server.display);
setenv("WAYLAND_DISPLAY", socket, true);
wlr_log(WLR_INFO, "Running Wayland compositor on WAYLAND_DISPLAY=%s",
socket);
if (startup_cmd) {
wlr_log(WLR_DEBUG, "Running startup command: %s", startup_cmd);
pid_t pid = fork();
if (pid < 0) {
wlr_log_errno(WLR_ERROR, "fork failed");
} else if (pid == 0) {
execl("/bin/sh", "/bin/sh", "-c", startup_cmd, (void *)NULL);
wlr_log_errno(WLR_ERROR, "execl failed");
}
}
wl_display_run(server.display);
glider_gl_renderer_destroy(server.renderer);
wl_display_destroy_clients(server.display);
wl_display_destroy(server.display);
glider_allocator_destroy(server.allocator);
return 0;
}