summaryrefslogtreecommitdiff
path: root/tests/common/test_tablet_example.c
blob: 77e79f640c2b494ed78220ca972b819f89530c70 (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
#define main tablet_example_main
#include "../../examples/tablet.c"
#undef main
#include <assert.h>

static void pointer(struct app *app, enum yasl_pointer_kind kind,
                    enum yasl_pointer_device device, int button, int x, int y, uint16_t pressure)
{
    struct yasl_event event = {0};
    if (kind == YASL_PTR_MOTION) {
        event.type = YASL_EVENT_MOUSE_MOTION;
        event.mouse_motion.pos = (struct yasl_vector2i){x, y};
        event.mouse_motion.device = device;
        event.mouse_motion.pen.pressure = pressure;
    } else {
        event.type = YASL_EVENT_MOUSE_BUTTON;
        event.mouse_button.pos = (struct yasl_vector2i){x, y};
        event.mouse_button.device = device;
        event.mouse_button.button = button;
        event.mouse_button.pressed = kind == YASL_PTR_PRESS;
        event.mouse_button.pen.pressure = pressure;
    }
    input(app, &event);
}

int main(void)
{
    struct app app = {0};
    assert(resize_canvas(&app, (struct yasl_geometry){{400, 300}, 120}));
    pointer(&app, YASL_PTR_MOTION, YASL_PTR_DEV_PEN, 3, 80, 160, 65535);
    /* Hover never paints. */
    assert(app.canvas[(size_t)160 * app.geometry.size.x + 80] == PAPER);
    pointer(&app, YASL_PTR_PRESS, YASL_PTR_DEV_PEN, 2, 80, 160, 65535);
    /* Side button isn't the tip. */
    assert(!app.down &&
            app.canvas[(size_t)160 * app.geometry.size.x + 80] == PAPER);
    pointer(&app, YASL_PTR_PRESS, YASL_PTR_DEV_PEN, 0, 80, 160, 0);
    assert(app.down &&
            app.canvas[(size_t)160 * app.geometry.size.x + 80] == INK &&
            app.canvas[(size_t)160 * app.geometry.size.x + 90] == PAPER);
    pointer(&app, YASL_PTR_MOTION, YASL_PTR_DEV_PEN, 0, 80, 160, 65535);
    /* Stationary pressure widens brush. */
    assert(app.canvas[(size_t)160 * app.geometry.size.x + 100] == INK);
    pointer(&app, YASL_PTR_MOTION, YASL_PTR_DEV_PEN, 0, 180, 160, 20000);
    /* Segments are continuous. */
    assert(app.canvas[(size_t)160 * app.geometry.size.x + 130] == INK);
    pointer(&app, YASL_PTR_RELEASE, YASL_PTR_DEV_PEN, 0, 180, 160, 0);
    pointer(&app, YASL_PTR_MOTION, YASL_PTR_DEV_PEN, 3, 250, 160, 65535);
    assert(!app.down && app.canvas[(size_t)160 * app.geometry.size.x + 250] == PAPER);
    pointer(&app, YASL_PTR_PRESS, YASL_PTR_DEV_ERASER, 0, 80, 160, 65535);
    assert(app.canvas[(size_t)160 * app.geometry.size.x + 80] == PAPER &&
            app.canvas[(size_t)160 * app.geometry.size.x + 130] == INK);
    struct yasl_event reset = {.type = YASL_EVENT_INPUT_RESET};
    input(&app, &reset);
    pointer(&app, YASL_PTR_MOTION, YASL_PTR_DEV_ERASER, 0, 130, 160, 65535);
    assert(!app.down && app.canvas[(size_t)160 * app.geometry.size.x + 130] == INK);
    pointer(&app, YASL_PTR_PRESS, YASL_PTR_DEV_MOUSE, 0, 300, 200, 0);
    assert(app.canvas[(size_t)200 * app.geometry.size.x + 300] == INK);
    struct yasl_event blur = {.type = YASL_EVENT_WINDOW_FOCUS, .focus.focused = false};
    input(&app, &blur);
    assert(!app.down);
    assert(resize_canvas(&app, (struct yasl_geometry){{800, 600}, 240}));
    /* DPI preserves logical drawing. */
    assert(app.canvas[(size_t)400 * app.geometry.size.x + 600] == INK &&
            !app.previous);
    clear_canvas(&app);
    assert(app.canvas[(size_t)400 * app.geometry.size.x + 600] == PAPER);
    free(app.canvas);
    return 0;
}