summaryrefslogtreecommitdiff
path: root/examples/tablet.c
blob: dad4b300ee7217dcd4c431c5a0cc4aa9c0d387f4 (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
/* Pen canvas: pressure, eraser and coherent strokes drawn straight into the CPU surface. */
#include <yasl/yasl.h>
#include <yasl/cpu.h>
#include <string.h>
#include <math.h>

#define PAPER 0xfff0eee8u
#define INK 0xff202830u

static void stamp(uint32_t *pixels, int w, int h, double x, double y, double radius, bool erase)
{
    int left = (int)floor(x - radius), top = (int)floor(y - radius);
    int right = (int)ceil(x + radius), bottom = (int)ceil(y + radius);
    if (left < 0) left = 0;
    if (top < 0) top = 0;
    if (right > w) right = w;
    if (bottom > h) bottom = h;
    for (int row = top; row < bottom; row++) {
        for (int col = left; col < right; col++) {
            double dx = col - x, dy = row - y;
            if (dx * dx + dy * dy <= radius * radius) pixels[(size_t)row * w + col] = erase ? PAPER : INK;
        }
    }
}

/* Paints paper over the area a resize added, or everything when clear is set. */
static uint32_t *canvas(yasl_surface *s, int *w, int *h, int *painted_w, int *painted_h, bool clear)
{
    uint32_t *pixels = yasl_cpu_pixels(s, w, h);
    if (!pixels) return NULL;
    if (clear) *painted_w = *painted_h = 0;
    for (int y = 0; y < *h; y++)
        for (int x = y < *painted_h ? *painted_w : 0; x < *w; x++) pixels[(size_t)y * *w + x] = PAPER;
    *painted_w = *w;
    *painted_h = *h;
    return pixels;
}

int main(int argc, char **argv)
{
    bool smoke = false;
    for (int i = 1; i < argc; i++) {
        if (!strcmp(argv[i], "--smoke-test")) smoke = true;
        else if (!strcmp(argv[i], "--backend") && i + 1 < argc) {
            const char *b = argv[++i];
            if (!yasl_set_backend(!strcmp(b, "wayland") ? YASL_BACKEND_WAYLAND :
                                  !strcmp(b, "x11") ? YASL_BACKEND_X11 : YASL_BACKEND_COCOA)) return 1;
        } else return 1;
    }
    yasl_surface *s = yasl_surface_create("YASL pen canvas — C clears, Escape exits", 800, 600, YASL_SURFACE_CPU);
    if (!s) return 1;
    int painted_w = 0, painted_h = 0, frames = 0, result = 1;
    bool contact = false;
    double last_x = 0, last_y = 0;
    yasl_event e;
    for (;;) {
        int n = yasl_wait_event(&e, smoke ? 20 : -1);
        if (n < 0 || e.type == YASL_EVENT_DISPLAY_LOST) break;
        if (smoke && frames && n == 0) { result = 0; break; }
        bool escape = e.type == YASL_EVENT_KEY && e.key.action == YASL_KEY_PRESS && e.key.key == YASL_KEY_ESCAPE;
        if (e.type == YASL_EVENT_CLOSE || e.type == YASL_EVENT_QUIT || escape) { result = 0; break; }
        bool clear = false, draw = false, erase = false, down = false, present = e.type == YASL_EVENT_REFRESH;
        double x = 0, y = 0, p = 0.5;
        switch (e.type) {
        case YASL_EVENT_KEY:
            if (e.key.action == YASL_KEY_PRESS && e.key.key == YASL_KEY_C) clear = present = true;
            break;
        case YASL_EVENT_PEN:
            x = e.pen.x; y = e.pen.y;
            p = (e.pen.axes_valid & YASL_PEN_PRESSURE) ? e.pen.pressure : 0.5;
            erase = e.pen.tool == YASL_PEN_ERASER;
            down = e.pen.in_contact;
            draw = down && (e.pen.action == YASL_PEN_DOWN || e.pen.action == YASL_PEN_MOVE);
            if (e.pen.action == YASL_PEN_LEAVE || e.pen.action == YASL_PEN_CANCEL) contact = false;
            break;
        case YASL_EVENT_POINTER:
            x = e.pointer.x; y = e.pointer.y;
            down = (e.pointer.buttons & YASL_BUTTON_BIT(YASL_BUTTON_PRIMARY)) != 0;
            draw = down && (e.pointer.action == YASL_POINTER_DOWN || e.pointer.action == YASL_POINTER_MOVE);
            break;
        case YASL_EVENT_INPUT_RESET:
            contact = false;
            break;
        default:
            break;
        }
        if (draw || present) {
            int w, h;
            int old_w = painted_w, old_h = painted_h;
            uint32_t *pixels = canvas(s, &w, &h, &painted_w, &painted_h, clear);
            if (!pixels) break;
            if (w != old_w || h != old_h) contact = false;
            if (draw) {
                double scale = yasl_surface_get_scale(s);
                x *= scale; y *= scale;
                double radius = (1.5 + 12 * p) * scale, dx = contact ? x - last_x : 0, dy = contact ? y - last_y : 0;
                int steps = (int)ceil(hypot(dx, dy) / (radius * 0.4));
                if (steps < 1) steps = 1;
                for (int i = 1; i <= steps; i++) stamp(pixels, w, h, x - dx + dx * i / steps, y - dy + dy * i / steps, radius, erase);
                last_x = x; last_y = y;
            }
            if (!yasl_cpu_present(s)) break;
            frames++;
        }
        if (e.type == YASL_EVENT_PEN || e.type == YASL_EVENT_POINTER) contact = down;
    }
    yasl_shutdown();
    return result;
}