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
|
// MIT License, Copyright (c) 2021 Marvin Borner
#include <assert.h>
#include <def.h>
#include <errno.h>
#include <libgui/gui.h>
#include <libgui/msg.h>
#include <list.h>
#include <print.h>
struct gui_widget {
u32 id;
vec2 pos;
/* struct context ctx; */
struct context fg;
struct context bg;
struct list *children;
struct {
void (*mousemove)(u32 widget_id, vec2 pos);
void (*mouseclick)(u32 widget_id, vec2 pos);
} event;
};
struct gui_window {
u32 id;
vec2 pos;
struct context ctx;
struct list *widgets;
};
struct list *windows = NULL;
#define gui_error(error) \
err(1, "%s:%d in %s: GUI Error: %s\n", __FILE__, __LINE__, __func__, strerror(error))
/**
* Resolve/find stuff
*/
static struct gui_window *gui_window_by_id(u32 win_id)
{
assert(windows);
struct node *iterator = windows->head;
while (iterator) {
struct gui_window *win = iterator->data;
if (win->id == win_id)
return iterator->data;
iterator = iterator->next;
}
return NULL;
}
static struct gui_widget *gui_widget_in_widget(struct gui_widget *parent, u32 widget_id)
{
if (!parent)
return NULL;
if (parent->id == widget_id)
return parent;
if (!parent->children)
return NULL;
struct node *iterator = parent->children->head;
while (iterator) {
struct gui_widget *widget = iterator->data;
if (widget->id == widget_id)
return iterator->data;
struct gui_widget *sub = gui_widget_in_widget(widget, widget_id);
if (sub && sub->id == widget_id)
return sub;
iterator = iterator->next;
}
return NULL;
}
static struct gui_widget *gui_widget_in_win(struct gui_window *win, u32 widget_id)
{
if (!win || !win->widgets)
return NULL;
struct node *iterator = win->widgets->head;
while (iterator) {
struct gui_widget *widget = iterator->data;
if (widget->id == widget_id)
return iterator->data;
struct gui_widget *sub = gui_widget_in_widget(widget, widget_id);
if (sub && sub->id == widget_id)
return sub;
iterator = iterator->next;
}
return NULL;
}
static struct gui_widget *gui_widget_by_id(u32 win_id, u32 widget_id)
{
struct gui_window *win = gui_window_by_id(win_id);
return gui_widget_in_win(win, widget_id);
}
/**
* Bus stuff
*/
static u32 wm_conn = 0;
static void gui_connect_wm(void)
{
if (wm_conn) {
assert(msg_connect_conn(wm_conn) == EOK);
} else {
assert(msg_connect_bus("wm", &wm_conn) == EOK);
}
}
/**
* GFX wrappers
*/
void gui_clear(u32 win_id, u32 widget_id, enum gui_layer layer)
{
struct gui_widget *widget = gui_widget_by_id(win_id, widget_id);
if (!widget)
gui_error(ENOENT);
if (layer == GUI_LAYER_BG)
gfx_clear(&widget->bg);
else if (layer == GUI_LAYER_FG)
gfx_clear(&widget->fg);
else
gui_error(EINVAL);
}
void gui_fill(u32 win_id, u32 widget_id, enum gui_layer layer, u32 c)
{
struct gui_widget *widget = gui_widget_by_id(win_id, widget_id);
if (!widget)
gui_error(ENOENT);
if (layer == GUI_LAYER_BG)
gfx_fill(&widget->bg, c);
else if (layer == GUI_LAYER_FG)
gfx_fill(&widget->fg, c);
else
gui_error(EINVAL);
}
void gui_write(u32 win_id, u32 widget_id, enum gui_layer layer, vec2 pos, enum font_type font_type,
u32 c, const char *text)
{
struct gui_widget *widget = gui_widget_by_id(win_id, widget_id);
if (!widget)
gui_error(ENOENT);
if (layer == GUI_LAYER_BG)
gfx_write(&widget->bg, pos, font_type, c, text);
else if (layer == GUI_LAYER_FG)
gfx_write(&widget->fg, pos, font_type, c, text);
else
gui_error(EINVAL);
}
void gui_load_image_filter(u32 win_id, u32 widget_id, enum gui_layer layer, vec2 pos, vec2 size,
enum gfx_filter filter, const char *path)
{
UNUSED(size); // TODO: Add image scaling
struct gui_widget *widget = gui_widget_by_id(win_id, widget_id);
if (!widget)
gui_error(ENOENT);
if (layer == GUI_LAYER_BG)
gfx_load_image_filter(&widget->bg, pos, filter, path);
else if (layer == GUI_LAYER_FG)
gfx_load_image_filter(&widget->fg, pos, filter, path);
else
gui_error(EINVAL);
}
void gui_load_image(u32 win_id, u32 widget_id, enum gui_layer layer, vec2 pos, vec2 size,
const char *path)
{
gui_load_image_filter(win_id, widget_id, layer, pos, size, GFX_FILTER_NONE, path);
}
/**
* Widgets
*/
static u8 gui_sub_widget_at(struct gui_widget *widget, vec2 pos, struct gui_widget *buf)
{
if (!widget || !widget->children || !buf)
gui_error(EFAULT);
struct gui_widget *ret = NULL;
struct gui_widget sub = { 0 };
struct node *iterator = widget->children->head;
while (iterator) {
struct gui_widget *w = iterator->data;
if (pos.x >= w->pos.x && pos.x <= w->pos.x + w->bg.size.x && pos.y >= w->pos.y &&
pos.y <= w->pos.y + w->bg.size.y)
ret = w;
if (w->children->head && gui_sub_widget_at(w, pos, &sub))
ret = ⊂
iterator = iterator->next;
}
if (!ret)
return 0;
*buf = *ret;
return 1;
}
static u8 gui_widget_at(u32 win_id, vec2 pos, struct gui_widget *widget)
{
if (!widget)
gui_error(EFAULT);
struct gui_window *win = gui_window_by_id(win_id);
if (!win)
gui_error(ENOENT);
if (!win->widgets)
return 1;
struct gui_widget *ret = NULL;
struct gui_widget sub = { 0 };
struct node *iterator = win->widgets->head;
while (iterator) {
struct gui_widget *w = iterator->data;
if (pos.x >= w->pos.x && pos.x <= w->pos.x + w->bg.size.x && pos.y >= w->pos.y &&
pos.y <= w->pos.y + w->bg.size.y)
ret = w;
if (w->children->head && gui_sub_widget_at(w, pos, &sub))
ret = ⊂
iterator = iterator->next;
}
if (!ret)
return 0;
*widget = *ret;
return 1;
}
static void gui_sync_sub_widgets(struct gui_widget *widget)
{
if (!widget)
gui_error(EFAULT);
if (!widget->children)
return;
struct node *iterator = widget->children->head;
while (iterator) {
struct gui_widget *w = iterator->data;
gfx_ctx_on_ctx(&widget->bg, &w->bg, w->pos, GFX_NON_ALPHA);
gfx_ctx_on_ctx(&widget->fg, &w->fg, w->pos, GFX_NON_ALPHA);
iterator = iterator->next;
}
}
static void gui_sync_widget(u32 win_id, u32 widget_id)
{
struct gui_window *win = gui_window_by_id(win_id);
struct gui_widget *widget = gui_widget_in_win(win, widget_id);
if (!widget)
gui_error(ENOENT);
gui_sync_sub_widgets(widget);
gfx_ctx_on_ctx(&win->ctx, &widget->bg, widget->pos, GFX_ALPHA);
gfx_ctx_on_ctx(&win->ctx, &widget->fg, widget->pos, GFX_ALPHA);
}
static void gui_sync_widgets(u32 win_id)
{
struct gui_window *win = gui_window_by_id(win_id);
if (!win)
gui_error(ENOENT);
if (!win->widgets)
return;
struct node *iterator = win->widgets->head;
while (iterator) {
struct gui_widget *widget = iterator->data;
gui_sync_sub_widgets(widget);
gfx_ctx_on_ctx(&win->ctx, &widget->bg, widget->pos, GFX_ALPHA);
gfx_ctx_on_ctx(&win->ctx, &widget->fg, widget->pos, GFX_ALPHA);
iterator = iterator->next;
}
}
static struct gui_widget *gui_main_widget(struct gui_window *win)
{
assert(win && win->widgets && win->widgets->head);
return win->widgets->head->data;
}
static u32 gui_main_widget_id(struct gui_window *win)
{
assert(win && win->widgets && win->widgets->head);
return ((struct gui_widget *)win->widgets->head->data)->id;
}
// TODO: This is very recursive and inefficient -> improve!
static vec2 gui_offset_widget(struct gui_widget *parent, struct gui_widget *child)
{
if (!parent || !parent->children || !child)
return vec2(0, 0);
vec2 offset = vec2(0, 0);
struct node *iterator = parent->children->head;
while (iterator) {
struct gui_widget *w = iterator->data;
if (w == child) {
offset = vec2_add(offset, w->pos);
break;
}
struct gui_widget *sub = gui_widget_in_widget(w, child->id);
if (sub) {
offset = vec2_add(offset, w->pos);
gui_offset_widget(w, child);
break;
}
iterator = iterator->next;
}
return offset;
}
static struct gui_widget *gui_new_plain_widget(vec2 pos, vec2 size, u8 bpp)
{
struct gui_widget *widget = zalloc(sizeof(*widget));
struct context *bg = zalloc(sizeof(*bg));
struct context *fg = zalloc(sizeof(*fg));
static u32 id = 0;
widget->id = id++;
widget->pos = pos;
widget->bg = *gfx_new_ctx(bg, size, bpp);
widget->fg = *gfx_new_ctx(fg, size, bpp);
widget->children = list_new();
return widget;
}
void gui_add_widget(u32 *widget, u32 win_id, u32 widget_id, vec2 pos, vec2 size)
{
struct gui_widget *parent = gui_widget_by_id(win_id, widget_id);
if (!parent)
gui_error(ENOENT);
struct gui_widget *child = gui_new_plain_widget(pos, size, parent->bg.bpp);
list_add(parent->children, child);
*widget = child->id;
}
void gui_new_widget(u32 *widget, u32 win_id, vec2 pos, vec2 size)
{
struct gui_window *win = gui_window_by_id(win_id);
if (!win)
gui_error(ENOENT);
gui_add_widget(widget, win->id, gui_main_widget(win)->id, pos, size);
}
void gui_listen_widget(u32 win_id, u32 widget_id, enum gui_listener listener, u32 func)
{
if (!func)
gui_error(EFAULT);
struct gui_widget *widget = gui_widget_by_id(win_id, widget_id);
if (!widget)
gui_error(ENOENT);
switch (listener) {
case GUI_LISTEN_MOUSEMOVE:
widget->event.mousemove = (void (*)(u32, vec2))func;
break;
case GUI_LISTEN_MOUSECLICK:
widget->event.mouseclick = (void (*)(u32, vec2))func;
break;
default:
gui_error(ENOENT);
}
}
void gui_redraw_widget(u32 win_id, u32 widget_id)
{
gui_sync_widget(win_id, widget_id);
gui_redraw_window(win_id);
}
/**
* Popups/alerts
*/
#define POPUP_WIDTH 200
#define POPUP_HEIGHT 100
void gui_popup(const char *text)
{
vec2 pos = vec2(200, 200);
u32 popup;
gui_new_custom_window(&popup, pos, vec2(POPUP_WIDTH, POPUP_HEIGHT));
struct gui_window *win = gui_window_by_id(popup);
gui_fill(popup, gui_main_widget_id(win), GUI_LAYER_BG, COLOR_WHITE);
u32 widget;
gui_new_widget(&widget, popup, vec2(0, 0), vec2(POPUP_WIDTH, 32));
gui_fill(popup, widget, GUI_LAYER_BG, COLOR_WHITE);
gui_write(popup, widget, GUI_LAYER_FG, vec2(0, 0), FONT_32, COLOR_BLACK, text);
gui_redraw_window(popup);
}
/**
* Window data getters
*/
vec2 gui_window_size(u32 win_id)
{
struct gui_window *win = gui_window_by_id(win_id);
if (!win)
gui_error(ENOENT);
return win->ctx.size;
}
/**
* Window manager interfaces
*/
void gui_new_custom_window(u32 *id, vec2 pos, vec2 size)
{
if (!windows)
windows = list_new();
if (vec2_sum(pos) == 0)
pos = vec2(200, 200);
if (vec2_sum(size) == 0)
size = vec2(600, 400);
struct gui_window *win = zalloc(sizeof(*win));
struct message_new_window msg = { .header.state = MSG_NEED_ANSWER,
.pos = pos,
.size = size };
gui_connect_wm();
if (msg_send(GUI_NEW_WINDOW, &msg, sizeof(msg)) > 0 && msg_receive(&msg, sizeof(msg)) > 0 &&
msg.header.type == (GUI_NEW_WINDOW | MSG_SUCCESS)) {
win->id = msg.id;
win->ctx = msg.ctx;
win->pos = msg.pos;
u32 buf_size;
res ret = shaccess(msg.shid, (u32 *)&win->ctx.fb, &buf_size);
if (ret < 0 || !win->ctx.fb)
gui_error(-MIN(ret, -EFAULT));
// Apply offset
win->ctx.size = vec2_sub(win->ctx.size, msg.off);
win->ctx.bytes -= msg.off.y * msg.ctx.pitch;
win->ctx.fb += msg.off.y * msg.ctx.pitch;
list_add(windows, win);
win->widgets = list_new();
list_add(win->widgets,
gui_new_plain_widget(vec2(0, 0), win->ctx.size, win->ctx.bpp));
*id = win->id;
return;
}
gui_error(EINVAL);
}
INLINE void gui_new_window(u32 *id)
{
gui_new_custom_window(id, vec2(0, 0), vec2(0, 0));
}
void gui_redraw_window(u32 id)
{
gui_sync_widgets(id);
struct message_redraw_window msg = { .id = id, .header.state = MSG_NEED_ANSWER };
gui_connect_wm();
if (msg_send(GUI_REDRAW_WINDOW, &msg, sizeof(msg)) > 0 &&
msg_receive(&msg, sizeof(msg)) > 0 &&
msg.header.type == (GUI_REDRAW_WINDOW | MSG_SUCCESS))
return;
gui_error(EINVAL);
}
/**
* Message handling
*/
static void gui_handle_ping(struct message_ping *msg)
{
if (msg->ping != MSG_PING_SEND)
gui_error(EINVAL);
msg->header.type |= MSG_SUCCESS;
msg->ping = MSG_PING_RECV;
if (msg_connect_conn(msg->header.bus.conn) == EOK &&
msg_send(GUI_PING, msg, sizeof(msg)) == EOK)
return;
gui_error(errno);
}
static void gui_handle_mouse(struct message_mouse *msg)
{
if (msg->header.state == MSG_NEED_ANSWER) {
if (msg_connect_conn(msg->header.bus.conn) == EOK &&
msg_send(msg->header.type | MSG_SUCCESS, msg, sizeof(msg)) == EOK)
return;
gui_error(errno);
}
struct gui_widget widget = { 0 };
gui_widget_at(msg->id, msg->pos, &widget);
struct gui_window *win = gui_window_by_id(msg->id);
vec2 offset = gui_offset_widget(gui_main_widget(win), &widget);
if (widget.event.mousemove)
widget.event.mousemove(widget.id, vec2_sub(msg->pos, offset));
if (widget.event.mouseclick && msg->bits.click)
widget.event.mouseclick(widget.id, vec2_sub(msg->pos, offset));
}
static void gui_handle_exit(void)
{
if (!windows)
return;
struct node *iterator = windows->head;
while (iterator) {
struct gui_window *win = iterator->data;
struct message_destroy_window msg = { .id = win->id };
gui_connect_wm();
msg_send(GUI_DESTROY_WINDOW, &msg, sizeof(msg));
iterator = iterator->next;
}
list_destroy(windows);
}
/**
* Main loop
*/
void gui_loop(void)
{
atexit(gui_handle_exit);
if (!windows)
err(1, "Create some windows first\n");
void *msg = zalloc(4096);
while (gui_connect_wm(), msg_receive(msg, 4096) > 0) {
struct message_header *head = msg;
switch (head->type) {
case GUI_PING:
gui_handle_ping(msg);
break;
case GUI_MOUSE:
gui_handle_mouse(msg);
break;
default:
// TODO: Fix random unknown msg types
gui_error(EINVAL);
}
}
free(msg);
err(1, "Gui loop failed\n");
}
|