summaryrefslogtreecommitdiff
path: root/tests/common/test_tablet_example.c
blob: 1658174e9ddf3715a6263dd2a8c808200b336b05 (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
#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_input_event event = {.kind = YASL_INPUT_MOUSE, .mouse = {
        .kind = kind, .device = device, .button = button, .pos = {x, y}, .pen.pressure = pressure}};
    input(app, &event);
}

static uint32_t pixel(const struct app *app, int x, int y)
{
    return app->canvas[(size_t)y * app->geometry.size.x + x];
}

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);
    assert(pixel(&app, 80, 160) == PAPER); /* Hover never paints. */
    pointer(&app, YASL_PTR_PRESS, YASL_PTR_DEV_PEN, 2, 80, 160, 65535);
    assert(!app.down && pixel(&app, 80, 160) == PAPER); /* Side button isn't the tip. */
    pointer(&app, YASL_PTR_PRESS, YASL_PTR_DEV_PEN, 0, 80, 160, 0);
    assert(app.down && pixel(&app, 80, 160) == INK && pixel(&app, 90, 160) == PAPER);
    pointer(&app, YASL_PTR_MOTION, YASL_PTR_DEV_PEN, 0, 80, 160, 65535);
    assert(pixel(&app, 100, 160) == INK); /* Stationary pressure widens brush. */
    pointer(&app, YASL_PTR_MOTION, YASL_PTR_DEV_PEN, 0, 180, 160, 20000);
    assert(pixel(&app, 130, 160) == INK); /* Segments are continuous. */
    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 && pixel(&app, 250, 160) == PAPER);
    pointer(&app, YASL_PTR_PRESS, YASL_PTR_DEV_ERASER, 0, 80, 160, 65535);
    assert(pixel(&app, 80, 160) == PAPER && pixel(&app, 130, 160) == INK);
    struct yasl_input_event reset = {.kind = YASL_INPUT_RESET};
    input(&app, &reset);
    pointer(&app, YASL_PTR_MOTION, YASL_PTR_DEV_ERASER, 0, 130, 160, 65535);
    assert(!app.down && pixel(&app, 130, 160) == INK);
    pointer(&app, YASL_PTR_PRESS, YASL_PTR_DEV_MOUSE, 0, 300, 200, 0);
    assert(pixel(&app, 300, 200) == INK);
    struct yasl_input_event blur = {.kind = YASL_INPUT_FOCUS, .focus.focused = false};
    input(&app, &blur);
    assert(!app.down);
    assert(resize_canvas(&app, (struct yasl_geometry){{800, 600}, 240}));
    assert(pixel(&app, 600, 400) == INK && !app.previous); /* DPI preserves logical drawing. */
    clear_canvas(&app);
    assert(pixel(&app, 600, 400) == PAPER);
    free(app.canvas);
    return 0;
}