1 #define _POSIX_C_SOURCE 200112L
2 #include <GLES2/gl2.h>
3 #include <getopt.h>
4 #include <math.h>
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <string.h>
8 #include <strings.h>
9 #include <time.h>
10 #include <unistd.h>
11 #include <wayland-server-core.h>
12 #include <wlr/backend.h>
13 #include <wlr/backend/session.h>
14 #include <wlr/render/wlr_renderer.h>
15 #include <wlr/types/wlr_keyboard.h>
16 #include <wlr/types/wlr_output.h>
17 #include <wlr/types/wlr_output_layout.h>
18 #include <wlr/types/wlr_input_device.h>
19 #include <wlr/types/wlr_matrix.h>
20 #include <wlr/util/log.h>
21 #include <xkbcommon/xkbcommon.h>
22 #include "cat.h"
23
24 struct sample_state {
25 struct wl_display *display;
26 struct wl_listener new_output;
27 struct wl_listener new_input;
28 struct timespec last_frame;
29 struct wlr_renderer *renderer;
30 struct wlr_texture *cat_texture;
31 struct wl_list outputs;
32 enum wl_output_transform transform;
33 };
34
35 struct sample_output {
36 struct sample_state *sample;
37 struct wlr_output *output;
38 struct wl_listener frame;
39 struct wl_listener destroy;
40 float x_offs, y_offs;
41 float x_vel, y_vel;
42 struct wl_list link;
43 };
44
45 struct sample_keyboard {
46 struct sample_state *sample;
47 struct wlr_input_device *device;
48 struct wl_listener key;
49 struct wl_listener destroy;
50 };
51
output_frame_notify(struct wl_listener * listener,void * data)52 static void output_frame_notify(struct wl_listener *listener, void *data) {
53 struct sample_output *sample_output = wl_container_of(listener, sample_output, frame);
54 struct sample_state *sample = sample_output->sample;
55 struct wlr_output *wlr_output = sample_output->output;
56 struct timespec now;
57 clock_gettime(CLOCK_MONOTONIC, &now);
58
59 int32_t width, height;
60 wlr_output_effective_resolution(wlr_output, &width, &height);
61
62 wlr_output_attach_render(wlr_output, NULL);
63 wlr_renderer_begin(sample->renderer, wlr_output->width, wlr_output->height);
64 wlr_renderer_clear(sample->renderer, (float[]){0.25f, 0.25f, 0.25f, 1});
65
66 for (int y = -128 + (int)sample_output->y_offs; y < height; y += 128) {
67 for (int x = -128 + (int)sample_output->x_offs; x < width; x += 128) {
68 wlr_render_texture(sample->renderer, sample->cat_texture,
69 wlr_output->transform_matrix, x, y, 1.0f);
70 }
71 }
72
73 wlr_renderer_end(sample->renderer);
74 wlr_output_commit(wlr_output);
75
76 long ms = (now.tv_sec - sample->last_frame.tv_sec) * 1000 +
77 (now.tv_nsec - sample->last_frame.tv_nsec) / 1000000;
78 float seconds = ms / 1000.0f;
79
80 sample_output->x_offs += sample_output->x_vel * seconds;
81 sample_output->y_offs += sample_output->y_vel * seconds;
82 if (sample_output->x_offs > 128) {
83 sample_output->x_offs = 0;
84 }
85 if (sample_output->y_offs > 128) {
86 sample_output->y_offs = 0;
87 }
88 sample->last_frame = now;
89 }
90
update_velocities(struct sample_state * sample,float x_diff,float y_diff)91 static void update_velocities(struct sample_state *sample,
92 float x_diff, float y_diff) {
93 struct sample_output *sample_output;
94 wl_list_for_each(sample_output, &sample->outputs, link) {
95 sample_output->x_vel += x_diff;
96 sample_output->y_vel += y_diff;
97 }
98 }
99
output_remove_notify(struct wl_listener * listener,void * data)100 static void output_remove_notify(struct wl_listener *listener, void *data) {
101 struct sample_output *sample_output = wl_container_of(listener, sample_output, destroy);
102 wl_list_remove(&sample_output->frame.link);
103 wl_list_remove(&sample_output->destroy.link);
104 free(sample_output);
105 }
106
new_output_notify(struct wl_listener * listener,void * data)107 static void new_output_notify(struct wl_listener *listener, void *data) {
108 struct wlr_output *output = data;
109 struct sample_state *sample = wl_container_of(listener, sample, new_output);
110 struct sample_output *sample_output = calloc(1, sizeof(struct sample_output));
111 if (!wl_list_empty(&output->modes)) {
112 struct wlr_output_mode *mode = wl_container_of(output->modes.prev, mode, link);
113 wlr_output_set_mode(output, mode);
114 }
115 sample_output->x_offs = sample_output->y_offs = 0;
116 sample_output->x_vel = sample_output->y_vel = 128;
117
118 wlr_output_set_transform(output, sample->transform);
119 sample_output->output = output;
120 sample_output->sample = sample;
121 wl_signal_add(&output->events.frame, &sample_output->frame);
122 sample_output->frame.notify = output_frame_notify;
123 wl_signal_add(&output->events.destroy, &sample_output->destroy);
124 sample_output->destroy.notify = output_remove_notify;
125 wl_list_insert(&sample->outputs, &sample_output->link);
126
127 wlr_output_commit(output);
128 }
129
keyboard_key_notify(struct wl_listener * listener,void * data)130 static void keyboard_key_notify(struct wl_listener *listener, void *data) {
131 struct sample_keyboard *keyboard = wl_container_of(listener, keyboard, key);
132 struct sample_state *sample = keyboard->sample;
133 struct wlr_event_keyboard_key *event = data;
134 uint32_t keycode = event->keycode + 8;
135 const xkb_keysym_t *syms;
136 int nsyms = xkb_state_key_get_syms(keyboard->device->keyboard->xkb_state,
137 keycode, &syms);
138 for (int i = 0; i < nsyms; i++) {
139 xkb_keysym_t sym = syms[i];
140 if (sym == XKB_KEY_Escape) {
141 wl_display_terminate(sample->display);
142 }
143 if (event->state == WLR_KEY_PRESSED) {
144 switch (sym) {
145 case XKB_KEY_Left:
146 update_velocities(sample, -16, 0);
147 break;
148 case XKB_KEY_Right:
149 update_velocities(sample, 16, 0);
150 break;
151 case XKB_KEY_Up:
152 update_velocities(sample, 0, -16);
153 break;
154 case XKB_KEY_Down:
155 update_velocities(sample, 0, 16);
156 break;
157 }
158 }
159 }
160 }
161
keyboard_destroy_notify(struct wl_listener * listener,void * data)162 static void keyboard_destroy_notify(struct wl_listener *listener, void *data) {
163 struct sample_keyboard *keyboard = wl_container_of(listener, keyboard, destroy);
164 wl_list_remove(&keyboard->destroy.link);
165 wl_list_remove(&keyboard->key.link);
166 free(keyboard);
167 }
168
new_input_notify(struct wl_listener * listener,void * data)169 static void new_input_notify(struct wl_listener *listener, void *data) {
170 struct wlr_input_device *device = data;
171 struct sample_state *sample = wl_container_of(listener, sample, new_input);
172 switch (device->type) {
173 case WLR_INPUT_DEVICE_KEYBOARD:;
174 struct sample_keyboard *keyboard = calloc(1, sizeof(struct sample_keyboard));
175 keyboard->device = device;
176 keyboard->sample = sample;
177 wl_signal_add(&device->events.destroy, &keyboard->destroy);
178 keyboard->destroy.notify = keyboard_destroy_notify;
179 wl_signal_add(&device->keyboard->events.key, &keyboard->key);
180 keyboard->key.notify = keyboard_key_notify;
181 struct xkb_rule_names rules = { 0 };
182 rules.rules = getenv("XKB_DEFAULT_RULES");
183 rules.model = getenv("XKB_DEFAULT_MODEL");
184 rules.layout = getenv("XKB_DEFAULT_LAYOUT");
185 rules.variant = getenv("XKB_DEFAULT_VARIANT");
186 rules.options = getenv("XKB_DEFAULT_OPTIONS");
187 struct xkb_context *context = xkb_context_new(XKB_CONTEXT_NO_FLAGS);
188 if (!context) {
189 wlr_log(WLR_ERROR, "Failed to create XKB context");
190 exit(1);
191 }
192 struct xkb_keymap *keymap = xkb_map_new_from_names(context, &rules,
193 XKB_KEYMAP_COMPILE_NO_FLAGS);
194 if (!keymap) {
195 wlr_log(WLR_ERROR, "Failed to create XKB keymap");
196 exit(1);
197 }
198 wlr_keyboard_set_keymap(device->keyboard, keymap);
199 xkb_keymap_unref(keymap);
200 xkb_context_unref(context);
201 break;
202 default:
203 break;
204 }
205 }
206
207
main(int argc,char * argv[])208 int main(int argc, char *argv[]) {
209 int c;
210 enum wl_output_transform transform = WL_OUTPUT_TRANSFORM_NORMAL;
211 while ((c = getopt(argc, argv, "r:")) != -1) {
212 switch (c) {
213 case 'r':
214 if (strcmp(optarg, "90") == 0) {
215 transform = WL_OUTPUT_TRANSFORM_90;
216 } else if (strcmp(optarg, "180") == 0) {
217 transform = WL_OUTPUT_TRANSFORM_180;
218 } else if (strcmp(optarg, "270") == 0) {
219 transform = WL_OUTPUT_TRANSFORM_270;
220 } else if (strcmp(optarg, "flipped") == 0) {
221 transform = WL_OUTPUT_TRANSFORM_FLIPPED;
222 } else if (strcmp(optarg, "flipped-90") == 0) {
223 transform = WL_OUTPUT_TRANSFORM_FLIPPED_90;
224 } else if (strcmp(optarg, "flipped-180") == 0) {
225 transform = WL_OUTPUT_TRANSFORM_FLIPPED_180;
226 } else if (strcmp(optarg, "flipped-270") == 0) {
227 transform = WL_OUTPUT_TRANSFORM_FLIPPED_270;
228 } else {
229 wlr_log(WLR_ERROR, "got unknown transform value: %s", optarg);
230 }
231 break;
232 default:
233 break;
234 }
235 }
236 wlr_log_init(WLR_DEBUG, NULL);
237 struct wl_display *display = wl_display_create();
238 struct sample_state state = {
239 .display = display,
240 .transform = transform
241 };
242 wl_list_init(&state.outputs);
243
244 struct wlr_backend *wlr = wlr_backend_autocreate(display, NULL);
245 if (!wlr) {
246 exit(1);
247 }
248
249 wl_signal_add(&wlr->events.new_output, &state.new_output);
250 state.new_output.notify = new_output_notify;
251 wl_signal_add(&wlr->events.new_input, &state.new_input);
252 state.new_input.notify = new_input_notify;
253 clock_gettime(CLOCK_MONOTONIC, &state.last_frame);
254
255 state.renderer = wlr_backend_get_renderer(wlr);
256 if (!state.renderer) {
257 wlr_log(WLR_ERROR, "Could not start compositor, OOM");
258 wlr_backend_destroy(wlr);
259 exit(EXIT_FAILURE);
260 }
261 state.cat_texture = wlr_texture_from_pixels(state.renderer,
262 WL_SHM_FORMAT_ABGR8888, cat_tex.width * 4, cat_tex.width, cat_tex.height,
263 cat_tex.pixel_data);
264 if (!state.cat_texture) {
265 wlr_log(WLR_ERROR, "Could not start compositor, OOM");
266 exit(EXIT_FAILURE);
267 }
268
269 if (!wlr_backend_start(wlr)) {
270 wlr_log(WLR_ERROR, "Failed to start backend");
271 wlr_backend_destroy(wlr);
272 exit(1);
273 }
274 wl_display_run(display);
275
276 wlr_texture_destroy(state.cat_texture);
277 wl_display_destroy(display);
278 }
279