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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
|
#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 uint16_t pen_pressure = 0;
static int8_t pen_tilt_x = 0;
static int8_t pen_tilt_y = 0;
static enum yasl_pointer_device pointer_device = YASL_PTR_DEV_MOUSE;
static const char *device_name(enum yasl_pointer_device dev)
{
switch (dev) {
case YASL_PTR_DEV_PEN: return "Pen/Stylus";
case YASL_PTR_DEV_ERASER: return "Eraser";
case YASL_PTR_DEV_TOUCH: return "Touch";
default: return "Mouse";
}
}
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 / stylus 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;
/* Pressure-sensitive radius: scales dynamically from 8px to 64px */
int radius;
if (pen_pressure > 0) {
radius = 8 + (int)(((uint32_t)pen_pressure * 56) / 65535);
} else {
radius = mouse_down ? 36 : 18;
}
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)
{
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 (Pen & Touch Enabled)",
.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(" - Stylus/Pen: full pressure (0..65535) and tilt detection\n");
printf(" - Mouse/Touch: interactive click/motion highlights\n");
printf(" - Space: cycle background themes\n");
printf(" - Escape: exit\n\n");
while (running) {
yasl_display_dispatch(d, -1);
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 == YASL_KEY_ESCAPE) { /* Escape */
running = false;
} else if (ev.key.key == YASL_KEY_SPACE || ev.key.key == YASL_KEY_ENTER) { /* Space or Return */
color_theme++;
yasl_surface_schedule_redraw(s);
}
}
} else if (ev.kind == YASL_INPUT_MOUSE) {
mouse_x = ev.mouse.pos.x;
mouse_y = ev.mouse.pos.y;
pen_pressure = ev.mouse.pen.pressure;
pen_tilt_x = ev.mouse.pen.tilt_x;
pen_tilt_y = ev.mouse.pen.tilt_y;
pointer_device = ev.mouse.device;
if (ev.mouse.kind == YASL_PTR_PRESS) {
mouse_down = true;
if (pen_pressure > 0 || pointer_device == YASL_PTR_DEV_PEN) {
printf("[Pen Down] Pressure: %5u/65535 (%.1f%%) | Tilt: (%+3d°, %+3d°) | Device: %s\n",
pen_pressure, (pen_pressure * 100.0) / 65535.0,
pen_tilt_x, pen_tilt_y, device_name(pointer_device));
}
} else if (ev.mouse.kind == YASL_PTR_RELEASE) {
mouse_down = false;
pen_pressure = 0;
} else if (ev.mouse.kind == YASL_PTR_MOTION && pen_pressure > 0) {
printf("[Pen Move] Pressure: %5u/65535 (%.1f%%) | Tilt: (%+3d°, %+3d°) | Device: %s\n",
pen_pressure, (pen_pressure * 100.0) / 65535.0,
pen_tilt_x, pen_tilt_y, device_name(pointer_device));
} else if (ev.mouse.kind == YASL_PTR_LEAVE) {
mouse_x = -1;
mouse_y = -1;
mouse_down = false;
pen_pressure = 0;
}
yasl_surface_schedule_redraw(s);
}
}
yasl_display_flush(d);
}
yasl_surface_destroy(s);
yasl_display_close(d);
return 0;
}
|