~bl4ckb0ne/wxrc

wxrc/example/gui.c -rw-r--r-- 15.3 KiB
f23b2087Simon Zeni meson: add missing tracy dep to wxrc a month ago
                                                                                
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
#include <cairo/cairo.h>
#include <microui.h>
#include <pango/pangocairo.h>
#include <linux/input-event-codes.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <wayland-client-core.h>

#include "shm-buffer.h"

#include "xdg-shell-client-protocol.h"

#define FONT_NAME "monospace 10"

struct pointer_data {
	int32_t x, y;
};

struct touch_data {
	int32_t x, y;
};

static struct wl_compositor *compositor = NULL;
static struct wl_shm *shm = NULL;
static struct xdg_wm_base *xdg_wm_base = NULL;

static struct wl_surface *surface = NULL;
static struct xdg_surface *xdg_surface = NULL;
static struct xdg_toplevel *xdg_toplevel = NULL;

static float bg[3] = { 90, 95, 100 };
static mu_Context ctx;

static int width = 800;
static int height = 600;

static struct shm_buffer *current;

static struct touch_data touch_slots[10];

static PangoFontDescription *font_desc;

static bool running = true;

static void
update(void)
{
	mu_begin(&ctx);

	int flags = MU_OPT_NOTITLE;
	if (mu_begin_window_ex(&ctx, "", mu_rect(0, 0, width, height), flags)) {
		// mu_begin_window_ex isnt resized every time
		mu_Container *win = mu_get_current_container(&ctx);
		win->rect.w = width;
		win->rect.h = height;

		if (mu_header_ex(&ctx, "Info", MU_OPT_EXPANDED)) {
			char buf[256] = {0};
			mu_layout_row(&ctx, 2, (int[]) { 50, -1 }, 0);

			mu_label(&ctx, "Size:");
			sprintf(buf, "%d, %d", win->rect.w, win->rect.h);
			mu_label(&ctx, buf);
		}

		if (mu_header(&ctx, "Test Buttons")) {
			mu_layout_row(&ctx, 3, (int[]) { 100, -100, -1 }, 0);
			mu_layout_row(&ctx, 3, NULL, 0);
			mu_label(&ctx, "Test buttons 1:");
			if (mu_button(&ctx, "Button 1")) {
				printf("Pressed button 1\n");
			}

			if (mu_button(&ctx, "Button 2")) {
				printf("Pressed button 2\n");
			}

			mu_label(&ctx, "Test buttons 2:");
			if (mu_button(&ctx, "Button 3")) {
				printf("Pressed button 3\n");
			}

			if (mu_button(&ctx, "Popup")) {
				mu_open_popup(&ctx, "Test Popup");
			}

			if (mu_begin_popup(&ctx, "Test Popup")) {
				mu_button(&ctx, "Hello");
				mu_button(&ctx, "World");
				mu_end_popup(&ctx);
			}
		}

		if (mu_header(&ctx, "Tree and Text")) {
			mu_layout_row(&ctx, 2, (int[]) { 180, -1 }, 0);
			mu_layout_begin_column(&ctx);
			if (mu_begin_treenode(&ctx, "Test 1")) {
				if (mu_begin_treenode(&ctx, "Test 1a")) {
					mu_label(&ctx, "Hello");
					mu_label(&ctx, "world");
					mu_end_treenode(&ctx);
				}
				if (mu_begin_treenode(&ctx, "Test 1b")) {
					if (mu_button(&ctx, "Button 1")) {
						printf("Pressed button 1");
					}

					if (mu_button(&ctx, "Button 2")) {
						printf("Pressed button 2");
					}
					mu_end_treenode(&ctx);
				}
				mu_end_treenode(&ctx);
			}

			if (mu_begin_treenode(&ctx, "Test 2")) {
				mu_layout_row(&ctx, 2, (int[]) { 70, 70 }, 0);
				if (mu_button(&ctx, "Button 3")) { printf("Pressed button 3"); }
				if (mu_button(&ctx, "Button 4")) { printf("Pressed button 4"); }
				if (mu_button(&ctx, "Button 5")) { printf("Pressed button 5"); }
				if (mu_button(&ctx, "Button 6")) { printf("Pressed button 6"); }
				mu_end_treenode(&ctx);
			}
			if (mu_begin_treenode(&ctx, "Test 3")) {
				static int checks[3] = { 1, 0, 1 };
				mu_checkbox(&ctx, "Checkbox 1", &checks[0]);
				mu_checkbox(&ctx, "Checkbox 2", &checks[1]);
				mu_checkbox(&ctx, "Checkbox 3", &checks[2]);
				mu_end_treenode(&ctx);
			}
			mu_layout_end_column(&ctx);

			mu_layout_begin_column(&ctx);
			mu_layout_row(&ctx, 1, (int[]) { -1 }, 0);
			//mu_layout_row(&ctx, 0, NULL, 0);
			//mu_layout_width(&ctx, -1)k;
			mu_text(&ctx, "Lorem ipsum dolor sit amet, consectetur adipiscing "
				"elit. Maecenas lacinia, sem eu lacinia molestie, mi risus "
				"faucibus ipsum, eu varius magna felis a nulla.");
			mu_layout_end_column(&ctx);
		}

		if (mu_header(&ctx, "Background Color")) {
				mu_layout_row(&ctx, 2, (int[]) { -78, -1 }, 74);
				mu_layout_begin_column(&ctx);
				mu_layout_row(&ctx, 2, (int[]) { 46, -1 }, 0);
				mu_label(&ctx, "Red:");   mu_slider(&ctx, &bg[0], 0, 255);
				mu_label(&ctx, "Green:"); mu_slider(&ctx, &bg[1], 0, 255);
				mu_label(&ctx, "Blue:");  mu_slider(&ctx, &bg[2], 0, 255);
				mu_layout_end_column(&ctx);
				mu_Rect r = mu_layout_next(&ctx);
				mu_draw_rect(&ctx, r, mu_color(bg[0], bg[1], bg[2], 255));
				char buf[32];
				sprintf(buf, "#%02X%02X%02X", (int) bg[0], (int) bg[1], (int) bg[2]);
				mu_draw_control_text(&ctx, buf, r, MU_COLOR_TEXT, MU_OPT_ALIGNCENTER);
		}

		mu_end_window(&ctx);
	}

	mu_end(&ctx);
}

static void
draw_rect(cairo_t *cr, mu_Rect *rect, mu_Color *color)
{
	cairo_save(cr);
	cairo_set_source_rgba(cr, color->r / 255.f, color->g / 255.f,
		color->b / 255.f, color->a / 255.f);
	cairo_rectangle(cr, rect->x, rect->y, rect->w, rect->h);
	cairo_fill(cr);
	cairo_restore(cr);
}

static void
draw_text(cairo_t *cr, mu_Vec2 *pos, mu_Color *color, const char *str)
{
	PangoLayout *layout = pango_cairo_create_layout(cr);
	pango_layout_set_text(layout, str, -1);
	pango_layout_set_font_description(layout, font_desc);

	cairo_save(cr);
	cairo_set_source_rgba(cr, color->r / 255.f, color->g / 255.f,
		color->b / 255.f, color->a / 255.f);

	pango_cairo_update_layout (cr, layout);

	int width, height;
	pango_layout_get_size (layout, &width, &height);
	cairo_move_to (cr, pos->x, pos->y);
	pango_cairo_show_layout (cr, layout);

	cairo_restore(cr);
	g_object_unref(layout);
}

static void
draw_clip(cairo_t *cr, mu_Rect *rect)
{
	cairo_save(cr);
	cairo_rectangle(cr, rect->x, rect->y, rect->w, rect->h);
	cairo_clip(cr);
	cairo_restore(cr);
}

static void
draw_icon(cairo_t *cr, mu_Rect *rect, mu_Color *color)
{
	mu_Rect r = {
		.x = rect->x + (rect->w / 4),
		.y = rect->y + (rect->h / 4),
		.w = rect->w / 2,
		.h = rect->h / 2,
	};
	draw_rect(cr, &r, color);
}

static void
draw(void)
{
	cairo_t *cr = current->cairo;

	// clear
	cairo_save(cr);
	cairo_set_source_rgba(cr, bg[0] / 255.f, bg[1] / 255.f, bg[2] / 255.f, 1.f);
	cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
	cairo_paint(cr);
	cairo_restore(cr);

	mu_Command *cmd = NULL;
	while (mu_next_command(&ctx, &cmd)) {
		switch (cmd->type) {
		case MU_COMMAND_RECT:
			draw_rect(cr, &cmd->rect.rect, &cmd->rect.color);
			break;
		case MU_COMMAND_TEXT:
			draw_text(cr, &cmd->text.pos, &cmd->text.color, cmd->text.str);
			break;
		case MU_COMMAND_CLIP:
			draw_clip(cr, &cmd->clip.rect);
			break;
		case MU_COMMAND_ICON:
			draw_icon(cr, &cmd->icon.rect, &cmd->icon.color);
			break;
		default:
			break;
		}
	}

	cairo_surface_flush(current->surface);
}

static void
frame(void)
{
	current = shm_buffer_get_next(shm, width, height);

	update();
	draw();

	wl_surface_attach(surface, current->wl_buffer, 0, 0);
	wl_surface_damage_buffer(surface, 0, 0, width, height);
	wl_surface_commit(surface);
}

static const struct wl_callback_listener frame_listener;

static void
frame_done(void *data, struct wl_callback *callback, uint32_t time)
{
	wl_callback_destroy(callback);

	callback = wl_surface_frame(surface);
	wl_callback_add_listener(callback, &frame_listener, NULL);

	frame();
}

static const struct wl_callback_listener frame_listener = {
	.done = frame_done,
};

static void
xdg_wm_base_ping(void *data, struct xdg_wm_base *xdg_wm_base, uint32_t serial)
{
	xdg_wm_base_pong(xdg_wm_base, serial);
}

static const struct xdg_wm_base_listener xdg_wm_base_listener = {
	.ping = xdg_wm_base_ping,
};

static void
xdg_surface_configure(void *data, struct xdg_surface *xdg_surface,
		uint32_t serial)
{
	xdg_surface_ack_configure(xdg_surface, serial);

	frame();
}

static const struct xdg_surface_listener xdg_surface_listener = {
	.configure = xdg_surface_configure,
};

static void
xdg_toplevel_configure(void *data, struct xdg_toplevel *toplevel, int32_t w,
		int32_t h, struct wl_array *state)
{
	if (w > 0) {
		width = w;
	}
	if (h > 0) {
		height = h;
	}
}

static void
xdg_toplevel_close(void *data, struct xdg_toplevel *xdg_toplevel)
{
	running = false;
}

static const struct xdg_toplevel_listener xdg_toplevel_listener = {
	.configure = xdg_toplevel_configure,
	.close = xdg_toplevel_close,
};

static void
pointer_enter(void *data, struct wl_pointer *wl_pointer, uint32_t serial,
		struct wl_surface *surface, wl_fixed_t surface_x, wl_fixed_t surface_y)
{
	struct pointer_data *pointer_data = calloc(1, sizeof(struct pointer_data));
	if (pointer_data == NULL) {
		fprintf(stderr, "failed to allocate pointer_data");
		return;
	}

	pointer_data->x = wl_fixed_to_int(surface_x);
	pointer_data->y = wl_fixed_to_int(surface_y);

	wl_pointer_set_user_data(wl_pointer, pointer_data);
}

static void
pointer_leave(void *data, struct wl_pointer *wl_pointer, uint32_t serial,
		struct wl_surface *surface)
{
	struct pointer_data *pointer_data = wl_pointer_get_user_data(wl_pointer);
	free(pointer_data);
}

static void
pointer_motion(void *data, struct wl_pointer *wl_pointer, uint32_t time,
		wl_fixed_t surface_x, wl_fixed_t surface_y)
{
	struct pointer_data *pointer_data = wl_pointer_get_user_data(wl_pointer);
	pointer_data->x = wl_fixed_to_int(surface_x);
	pointer_data->y = wl_fixed_to_int(surface_y);

	mu_input_mousemove(&ctx, pointer_data->x, pointer_data->y);
}

static void
pointer_axis(void *data, struct wl_pointer *wl_pointer, uint32_t time,
		uint32_t axis, int32_t value)
{
	float x = 0, y = 0;
	if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL) {
		y = value;
	} else if (axis == WL_POINTER_AXIS_HORIZONTAL_SCROLL) {
		x = value;
	}

	mu_input_scroll(&ctx, x, y);
}

static void
pointer_button(void *data, struct wl_pointer *wl_pointer, uint32_t serial,
		uint32_t time, uint32_t button, uint32_t state)
{
	int btn;
	switch (button) {
	case BTN_LEFT:
		btn = MU_MOUSE_LEFT;
		break;
	case BTN_RIGHT:
		btn = MU_MOUSE_RIGHT;
		break;
	case BTN_MIDDLE:
		btn = MU_MOUSE_MIDDLE;
		break;
	default:
		fprintf(stderr, "button not handled\n");
		return;
	}

	struct pointer_data *pointer_data = wl_pointer_get_user_data(wl_pointer);
	if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
		mu_input_mousedown(&ctx, pointer_data->x, pointer_data->y, btn);
	} else if (state == WL_POINTER_BUTTON_STATE_RELEASED) {
		mu_input_mouseup(&ctx, pointer_data->x, pointer_data->y, btn);
	}
}

static const struct wl_pointer_listener pointer_listener = {
	.enter = pointer_enter,
	.leave = pointer_leave,
	.motion = pointer_motion,
	.button = pointer_button,
	.axis = pointer_axis,
};

static void
touch_down (void *data, struct wl_touch *wl_touch, uint32_t serial,
		uint32_t time, struct wl_surface *wl_surface, int32_t id,
		wl_fixed_t x,  wl_fixed_t y)
{
	if (id >= 10) {
		fprintf(stderr, "id %"PRIi32" not processed\n", id);
		return;
	}

	touch_slots[id].x = wl_fixed_to_int(x);
	touch_slots[id].y = wl_fixed_to_int(y);

	mu_input_mousedown(&ctx, touch_slots[id].x, touch_slots[id].y,
		MU_MOUSE_LEFT);
}

static void
touch_up(void *data, struct wl_touch *wl_touch, uint32_t serial, uint32_t time,
		int32_t id)
{
	if (id >= 10) {
		return;
	}

	mu_input_mouseup(&ctx, touch_slots[id].x, touch_slots[id].y, MU_MOUSE_LEFT);
}

static void
touch_motion(void *data, struct wl_touch *wl_touch, uint32_t time, int32_t id,
	wl_fixed_t x, wl_fixed_t y)
{
	if (id >= 10) {
		fprintf(stderr, "id %"PRIu32" not processed\n", id);
		return;
	}

	touch_slots[id].x = wl_fixed_to_int(x);
	touch_slots[id].y = wl_fixed_to_int(y);

	mu_input_mousemove(&ctx, touch_slots[id].x, touch_slots[id].y);
}

static void
touch_frame(void *data, struct wl_touch *wl_touch)
{}

static void
touch_cancel(void *data, struct wl_touch *wl_touch)
{}

static void
touch_shape(void *data, struct wl_touch *wl_touch, int32_t id, wl_fixed_t major,
		wl_fixed_t minor)
{}

static void
touch_orientation(void *data, struct wl_touch *wl_touch, int32_t id,
		wl_fixed_t orientation)
{}

static const struct wl_touch_listener touch_listener = {
	.down = touch_down,
	.up = touch_up,
	.motion = touch_motion,
	.frame = touch_frame,
	.cancel = touch_cancel,
	.shape = touch_shape,
	.orientation = touch_orientation,
};

static void
seat_capabilities(void *data, struct wl_seat *seat, uint32_t caps)
{
	if (caps & WL_SEAT_CAPABILITY_POINTER) {
		struct wl_pointer *pointer = wl_seat_get_pointer(seat);
		wl_pointer_add_listener(pointer, &pointer_listener, seat);
	}

	if (caps & WL_SEAT_CAPABILITY_TOUCH) {
		struct wl_touch *touch = wl_seat_get_touch(seat);
		wl_touch_add_listener(touch, &touch_listener, seat);
	}
}

static const struct wl_seat_listener seat_listener = {
	.capabilities = seat_capabilities,
};

static void
registry_global(void *data, struct wl_registry *registry, uint32_t name,
		const char *interface, uint32_t version)
{
	if (strcmp(interface, wl_compositor_interface.name) == 0) {
		compositor =
			wl_registry_bind(registry, name, &wl_compositor_interface, 4);
	} else if (strcmp(interface, xdg_wm_base_interface.name) == 0) {
		xdg_wm_base =
			wl_registry_bind(registry, name, &xdg_wm_base_interface, 2);
		xdg_wm_base_add_listener(xdg_wm_base, &xdg_wm_base_listener, NULL);
	} else if (strcmp(interface, wl_seat_interface.name) == 0) {
		struct wl_seat *seat =
			wl_registry_bind(registry, name, &wl_seat_interface, 1);
		wl_seat_add_listener(seat, &seat_listener, NULL);
	} else if (strcmp(interface, wl_shm_interface.name) == 0) {
		shm = wl_registry_bind(registry, name, &wl_shm_interface, 1);
	}
}

static void
registry_global_remove(void *data, struct wl_registry *registry, uint32_t name)
{}

static const struct wl_registry_listener registry_listener = {
	.global = registry_global,
	.global_remove = registry_global_remove,
};

static int
text_width(mu_Font font, const char *str, int len)
{
	if (len == -1) {
		len = strlen(str);
	}

	PangoFontMetrics *metrics = pango_context_get_metrics(current->pango,
		font_desc, NULL);

	int w = pango_font_metrics_get_approximate_char_width(metrics) / PANGO_SCALE;

	pango_font_metrics_unref(metrics);

	return w * len;
}

static int
text_height(mu_Font font)
{
	PangoFontMetrics *metrics = pango_context_get_metrics(current->pango,
		font_desc, NULL);

	int h = pango_font_metrics_get_height(metrics) / PANGO_SCALE;

	pango_font_metrics_unref(metrics);

	return h;
}

int
main(int argc, char *argv[])
{
	struct wl_display *display = wl_display_connect(NULL);
	if (display == NULL) {
		fprintf(stderr, "failed to create display\n");
		return EXIT_FAILURE;
	}

	struct wl_registry *registry = wl_display_get_registry(display);
	wl_registry_add_listener(registry, &registry_listener, NULL);
	wl_display_dispatch(display);
	wl_display_roundtrip(display);

	if (compositor == NULL) {
		fprintf(stderr, "wl_compositor missing\n");
		return EXIT_FAILURE;
	} else if (xdg_wm_base == NULL) {
		fprintf(stderr, "no xdg_shell support\n");
		return EXIT_FAILURE;
	}

	surface = wl_compositor_create_surface(compositor);
	xdg_surface = xdg_wm_base_get_xdg_surface(xdg_wm_base, surface);
	xdg_surface_add_listener(xdg_surface, &xdg_surface_listener, NULL);

	xdg_toplevel = xdg_surface_get_toplevel(xdg_surface);
	xdg_toplevel_add_listener(xdg_toplevel, &xdg_toplevel_listener, NULL);

	xdg_toplevel_set_title(xdg_toplevel, "wxrc - gui example");

	wl_surface_commit(surface);

	font_desc = pango_font_description_from_string(FONT_NAME);

	mu_init(&ctx);
	ctx.text_width = text_width;
	ctx.text_height = text_height;

	struct wl_callback *callback = wl_surface_frame(surface);
	wl_callback_add_listener(callback, &frame_listener, NULL);

	while (wl_display_dispatch(display) != -1 && running);

	xdg_toplevel_destroy(xdg_toplevel);
	xdg_surface_destroy(xdg_surface);
	wl_surface_destroy(surface);
	wl_compositor_destroy(compositor);
	wl_registry_destroy(registry);
	wl_display_disconnect(display);

	return EXIT_SUCCESS;
}