-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.c
263 lines (219 loc) · 9.12 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
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
/*
* not-a-wm - The tiniest possible Wayland compositor based on libweston and libweston-desktop
*
* Copyright © 2017 Quentin "Sardem FF7" Glidic
*
* 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.
*
*/
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <signal.h>
#include <string.h>
#include <compositor.h>
#include <compositor-drm.h>
#include <compositor-wayland.h>
#include <compositor-x11.h>
#include <windowed-output-api.h>
#include <libweston-desktop.h>
typedef struct {
struct wl_display *display;
struct weston_compositor *compositor;
struct wl_listener output_pending_listener;
union {
struct weston_drm_backend_config drm;
struct weston_wayland_backend_config wayland;
struct weston_x11_backend_config x11;
} backend_config;
union {
const struct weston_windowed_output_api *windowed;
const struct weston_drm_output_api *drm;
} api;
struct weston_layer background_layer;
struct weston_surface *background;
struct weston_view *background_view;
struct weston_desktop *desktop;
struct weston_layer surfaces_layer;
} NawContext;
typedef struct {
struct weston_desktop_surface *desktop_surface;
struct weston_surface *surface;
struct weston_view *view;
} NawSurface;
static int
_naw_log(const char *format, va_list args)
{
return vfprintf(stderr, format, args);
}
static void
_naw_output_pending_drm(struct wl_listener *listener, void *data)
{
NawContext *context = wl_container_of(listener, context, output_pending_listener);
struct weston_output *woutput = data;
/* We enable all DRM outputs with their preferred mode */
context->api.drm->set_mode(woutput, WESTON_DRM_BACKEND_OUTPUT_PREFERRED, NULL);
context->api.drm->set_gbm_format(woutput, NULL);
context->api.drm->set_seat(woutput, NULL);
weston_output_set_scale(woutput, 1);
weston_output_set_transform(woutput, WL_OUTPUT_TRANSFORM_NORMAL);
weston_output_enable(woutput);
}
static void
_naw_output_pending_virtual(struct wl_listener *listener, void *data)
{
NawContext *context = wl_container_of(listener, context, output_pending_listener);
struct weston_output *woutput = data;
/* We enable all windowed outputs with a 800×600 mode */
weston_output_set_scale(woutput, 1);
weston_output_set_transform(woutput, WL_OUTPUT_TRANSFORM_NORMAL);
context->api.windowed->output_set_size(woutput, 800, 600);
weston_output_enable(woutput);
}
static void
_naw_exit(struct weston_compositor *compositor)
{
NawContext *context = weston_compositor_get_user_data(compositor);
wl_display_terminate(context->display);
}
static void
_naw_surface_added(struct weston_desktop_surface *surface, void *user_data)
{
NawContext *context = user_data;
/* Creating our own wrapper struct to store whatever we need, specifically the view */
NawSurface *self;
self = calloc(1, sizeof(NawSurface));
self->desktop_surface = surface;
weston_desktop_surface_set_user_data(self->desktop_surface, self);
self->surface = weston_desktop_surface_get_surface(self->desktop_surface);
self->view = weston_desktop_surface_create_view(self->desktop_surface);
weston_layer_entry_insert(&context->surfaces_layer.view_list, &self->view->layer_link);
/* No real WM work here, we just put all surfaces at (0,0) */
weston_view_set_position(self->view, 0, 0);
weston_surface_damage(self->surface);
weston_compositor_schedule_repaint(context->compositor);
/* TODO: There, you usually change focus if according to your defined policy */
}
static void
_naw_surface_removed(struct weston_desktop_surface *surface, void *user_data)
{
NawContext *context = user_data;
NawSurface *self = weston_desktop_surface_get_user_data(surface);
if ( self == NULL )
return;
weston_desktop_surface_unlink_view(self->view);
weston_view_destroy(self->view);
weston_desktop_surface_set_user_data(surface, NULL);
free(self);
/* TODO: There, you usually change focus if the removed surface was focused */
(void) context;
}
static const struct weston_desktop_api _naw_desktop_api = {
/* For backward ABI compatibility */
.struct_size = sizeof(struct weston_desktop_api),
/* These two are the minimal API allowed */
.surface_added = _naw_surface_added,
.surface_removed = _naw_surface_removed,
};
int
main()
{
NawContext context_ = { .display = NULL }, *context = &context_;
weston_log_set_handler(_naw_log, _naw_log);
/* Ignore SIGPIPE as it is useless */
signal(SIGPIPE, SIG_IGN);
context->display = wl_display_create();
context->compositor = weston_compositor_create(context->display, context);
/* Just using NULL here will use environment variables */
weston_compositor_set_xkb_rule_names(context->compositor, NULL);
enum weston_compositor_backend backend = WESTON_BACKEND_DRM;
if ( getenv("WAYLAND_DISPLAY") != NULL )
backend = WESTON_BACKEND_WAYLAND;
else if ( getenv("DISPLAY") != NULL )
backend = WESTON_BACKEND_X11;
switch ( backend )
{
case WESTON_BACKEND_DRM:
context->backend_config.drm.base.struct_version = WESTON_DRM_BACKEND_CONFIG_VERSION;
context->backend_config.drm.base.struct_size = sizeof(struct weston_drm_backend_config);
break;
case WESTON_BACKEND_WAYLAND:
context->backend_config.wayland.base.struct_version = WESTON_WAYLAND_BACKEND_CONFIG_VERSION;
context->backend_config.wayland.base.struct_size = sizeof(struct weston_wayland_backend_config);
break;
case WESTON_BACKEND_X11:
context->backend_config.x11.base.struct_version = WESTON_X11_BACKEND_CONFIG_VERSION;
context->backend_config.x11.base.struct_size = sizeof(struct weston_x11_backend_config);
break;
default:
/* Not supported */
return 1;
}
weston_compositor_load_backend(context->compositor, backend, &context->backend_config.drm.base);
switch ( backend )
{
case WESTON_BACKEND_DRM:
context->api.drm = weston_drm_output_get_api(context->compositor);
if ( context->api.drm == NULL )
return 1;
context->output_pending_listener.notify = _naw_output_pending_drm;
break;
case WESTON_BACKEND_X11:
case WESTON_BACKEND_WAYLAND:
{
context->api.windowed = weston_windowed_output_get_api(context->compositor);
if ( context->api.windowed == NULL )
return 1;
context->api.windowed->output_create(context->compositor, "W1");
context->output_pending_listener.notify = _naw_output_pending_virtual;
}
break;
default:
return 1;
}
wl_signal_add(&context->compositor->output_pending_signal, &context->output_pending_listener);
weston_pending_output_coldplug(context->compositor);
context->compositor->vt_switching = 1;
context->compositor->exit = _naw_exit;
/* We want a defined background */
weston_layer_init(&context->background_layer, context->compositor);
weston_layer_set_position(&context->background_layer, WESTON_LAYER_POSITION_BACKGROUND);
context->background = weston_surface_create(context->compositor);
weston_surface_set_size(context->background, 8096, 8096);
weston_surface_set_color(context->background, 0, 0.25, 0.5, 1);
context->background_view = weston_view_create(context->background);
weston_layer_entry_insert(&context->background_layer.view_list, &context->background_view->layer_link);
context->desktop = weston_desktop_create(context->compositor, &_naw_desktop_api, context);
weston_layer_init(&context->surfaces_layer, context->compositor);
weston_layer_set_position(&context->surfaces_layer, WESTON_LAYER_POSITION_NORMAL);
const char *socket_name;
socket_name = wl_display_add_socket_auto(context->display);
if ( socket_name == NULL )
{
weston_log("Couldn’t add socket: %s\n", strerror(errno));
return -1;
}
setenv("WAYLAND_DISPLAY", socket_name, 1);
unsetenv("DISPLAY");
weston_compositor_wake(context->compositor);
wl_display_run(context->display);
weston_desktop_destroy(context->desktop);
weston_compositor_destroy(context->compositor);
return 0;
}