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

static bool running = true;
static int mouse_x = -1;
static int mouse_y = -1;
static bool mouse_down = false;
static int color_theme = 0;

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 || w <= 0 || h <= 0)
        return;

    for (int y = 0; y < h; y++) {
        for (int x = 0; x < w; x++) {
            uint8_t r, g, b;
            uint8_t u = (uint8_t)((x * 255) / (w > 0 ? w : 1));
            uint8_t v = (uint8_t)((y * 255) / (h > 0 ? h : 1));

            switch (color_theme % 3) {
            case 0:
                r = u;
                g = v;
                b = 160;
                break;
            case 1:
                r = 40;
                g = u;
                b = v;
                break;
            default:
                r = v;
                g = 60;
                b = u;
                break;
            }

            /* Draw interactive pointer indicator */
            if (mouse_x >= 0 && mouse_y >= 0) {
                int dx = x - mouse_x;
                int dy = y - mouse_y;
                int dist_sq = dx * dx + dy * dy;
                int radius = mouse_down ? 36 : 20;
                if (dist_sq < radius * radius) {
                    r = (uint8_t)(255 - r / 2);
                    g = (uint8_t)(255 - g / 2);
                    b = (uint8_t)(255 - b / 2);
                }
            }

            pixels[y * w + x] = (255u << 24) | ((uint32_t)r << 16) | ((uint32_t)g << 8) | b;
        }
    }

    struct yasl_damage_list dmg = { .n = -1 };
    yasl_surface_present(s, &frame->target, &dmg);
}

static void on_configure(struct yasl_surface *s, struct yasl_geometry geo, void *ud)
{
    (void)ud;
    printf("[YASL] Resized to %dx%d (scale: %.2fx)\n",
           geo.size.x, geo.size.y, (double)geo.scale_120 / YASL_SCALE_UNIT);
    yasl_surface_schedule_redraw(s);
}

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

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

int main(void)
{
    /* YASL manages its event loop internally when NULL is passed */
    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(NULL, &disp_spec);
    if (!d) {
        fprintf(stderr, "Failed to open display\n");
        return 1;
    }

    struct yasl_surface_spec surf_spec = {
        .title = "YASL Interactive 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);
        return 1;
    }

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

    printf("=== YASL Demo Running ===\n");
    printf(" - Move or Click mouse inside window to interact\n");
    printf(" - Press Space to cycle background themes\n");
    printf(" - Press Escape or close window to exit\n\n");

    while (running) {
        yasl_display_dispatch(d);

        struct yasl_input_event ev;
        while (yasl_surface_input_poll(s, &ev)) {
            if (ev.kind == YASL_INPUT_KEY) {
                if (ev.key.action == YASL_KEY_PRESS) {
                    if (ev.key.key == 0xff1b) { /* Escape */
                        running = false;
                    } else if (ev.key.key == ' ' || ev.key.key == 0xff0d) { /* Space or Return */
                        color_theme++;
                        yasl_surface_schedule_redraw(s);
                    }
                }
            } else if (ev.kind == YASL_INPUT_MOUSE) {
                if (ev.mouse.kind == YASL_PTR_MOTION) {
                    mouse_x = ev.mouse.pos.x;
                    mouse_y = ev.mouse.pos.y;
                    yasl_surface_schedule_redraw(s);
                } else if (ev.mouse.kind == YASL_PTR_PRESS) {
                    mouse_down = true;
                    mouse_x = ev.mouse.pos.x;
                    mouse_y = ev.mouse.pos.y;
                    yasl_surface_schedule_redraw(s);
                } else if (ev.mouse.kind == YASL_PTR_RELEASE) {
                    mouse_down = false;
                    yasl_surface_schedule_redraw(s);
                } else if (ev.mouse.kind == YASL_PTR_LEAVE) {
                    mouse_x = -1;
                    mouse_y = -1;
                    mouse_down = false;
                    yasl_surface_schedule_redraw(s);
                }
            }
        }

        yasl_display_flush(d);
    }

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