summaryrefslogtreecommitdiff
path: root/examples/simple_window.c
blob: 41a5375383de20c2614d59c8059ff77ac777df6d (plain)
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
#include "yasl/yasl.h"
#include <stdio.h>
#include <stdlib.h>

static bool running = true;

static void on_draw(struct yasl_surface *s, struct yasl_frame *frame, void *ud)
{
    (void)ud;
    int w = frame->target.size.x;
    int h = frame->target.size.y;
    uint32_t *pixels = frame->target.pixels;
    if (pixels) {
        for (int y = 0; y < h; y++) {
            for (int x = 0; x < w; x++) {
                uint8_t r = (uint8_t)((x * 255) / (w > 0 ? w : 1));
                uint8_t g = (uint8_t)((y * 255) / (h > 0 ? h : 1));
                uint8_t b = 128;
                pixels[y * w + x] = (255u << 24) | (r << 16) | (g << 8) | b;
            }
        }
    }
    struct yasl_damage_list dmg = { .n = -1 };
    yasl_surface_present(s, &frame->target, &dmg);
}

static void on_close(struct yasl_surface *s, void *ud)
{
    (void)s;
    (void)ud;
    printf("Window close requested\n");
    running = false;
}

static void on_quit(void *ud)
{
    (void)ud;
    running = false;
}

int main(void)
{
    struct yasl_loop *loop = yasl_loop_create();
    if (!loop) {
        fprintf(stderr, "Failed to create event loop\n");
        return 1;
    }

    struct yasl_display_cbs disp_cbs = { .quit = on_quit };
    struct yasl_display_spec disp_spec = {
        .app_id = "org.yasl.example",
        .cbs = &disp_cbs,
    };
    struct yasl_display *d = yasl_display_open(loop, &disp_spec);
    if (!d) {
        fprintf(stderr, "Failed to open display\n");
        yasl_loop_destroy(loop);
        return 1;
    }

    struct yasl_surface_spec surf_spec = {
        .title = "YASL Simple Window",
        .size = { .x = 640, .y = 480 },
    };
    struct yasl_surface *s = yasl_surface_create_toplevel(d, &surf_spec);
    if (!s) {
        fprintf(stderr, "Failed to create surface\n");
        yasl_display_close(d);
        yasl_loop_destroy(loop);
        return 1;
    }

    struct yasl_surface_cbs surf_cbs = {
        .draw = on_draw,
        .close = on_close,
    };
    yasl_surface_set_callbacks(s, &surf_cbs, NULL);
    yasl_surface_schedule_redraw(s);

    while (running) {
        yasl_display_dispatch(d);
        struct yasl_input_event ev;
        while (yasl_surface_input_poll(s, &ev)) {
            if (ev.kind == YASL_INPUT_KEY && ev.key.action == YASL_KEY_PRESS) {
                printf("Key pressed: keysym=%u\n", ev.key.key);
                if (ev.key.key == 0xff1b) /* Escape */
                    running = false;
            }
        }
        yasl_display_flush(d);
    }

    yasl_surface_destroy(s);
    yasl_display_close(d);
    yasl_loop_destroy(loop);
    return 0;
}