summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
Diffstat (limited to 'examples')
-rw-r--r--examples/game.c31
-rw-r--r--examples/hello.c6
-rw-r--r--examples/notepad.c94
-rw-r--r--examples/simple_window.c31
-rw-r--r--examples/tablet.c38
5 files changed, 100 insertions, 100 deletions
diff --git a/examples/game.c b/examples/game.c
index 7ec28e1..065ff99 100644
--- a/examples/game.c
+++ b/examples/game.c
@@ -6,7 +6,7 @@
* - Continuous display-synchronized ticks (YASL_PACING_CONTINUOUS)
* - 3D software rendering (rotating shaded cube + starfield)
* - WASD continuous player movement & key state tracking
- * - Relative mouse mode (YASL_MOUSE_RELATIVE) for smooth first-person camera orbit
+ * - Relative pointer mode (YASL_POINTER_RELATIVE) for smooth first-person camera orbit
* - Spacebar toggles pointer lock (Relative vs Normal cursor)
* - Real-time FPS counter and frame latency display
* - Dynamic window resizing with automatic aspect ratio adjustment
@@ -140,7 +140,7 @@ static bool project_point(struct vec3 p, int w, int h, int *out_x, int *out_y)
return true;
}
-static int on_init(void *ud, struct yasl_display *d)
+static enum yasl_app_result on_init(void *ud, struct yasl_display *d)
{
struct game_state *game = ud;
game->display = d;
@@ -169,14 +169,13 @@ static int on_init(void *ud, struct yasl_display *d)
return YASL_APP_CONTINUE;
}
-static int on_event(void *ud, const struct yasl_event *ev)
+static enum yasl_app_result on_event(void *ud, const struct yasl_event *ev)
{
struct game_state *game = ud;
switch (ev->type) {
case YASL_EVENT_KEY: {
- bool down = (ev->key.action == YASL_KEY_PRESS || ev->key.action == YASL_KEY_REPEAT);
- if (ev->key.action == YASL_KEY_RELEASE) down = false;
+ bool down = ev->key.action != YASL_KEY_RELEASE;
switch (ev->key.key) {
case YASL_KEY_W: game->key_w = down; break;
@@ -187,17 +186,17 @@ static int on_event(void *ud, const struct yasl_event *ev)
case YASL_KEY_E: game->key_e = down; break;
case YASL_KEY_SPACE:
if (ev->key.action == YASL_KEY_PRESS) {
- /* Toggle relative mouse mode */
+ /* Toggle relative pointer mode */
game->relative_mouse = !game->relative_mouse;
- yasl_surface_set_mouse_mode(game->surface,
- game->relative_mouse ? YASL_MOUSE_RELATIVE : YASL_MOUSE_NORMAL);
+ yasl_surface_set_pointer_mode(game->surface,
+ game->relative_mouse ? YASL_POINTER_RELATIVE : YASL_POINTER_NORMAL);
}
break;
case YASL_KEY_ESCAPE:
if (ev->key.action == YASL_KEY_PRESS) {
if (game->relative_mouse) {
game->relative_mouse = false;
- yasl_surface_set_mouse_mode(game->surface, YASL_MOUSE_NORMAL);
+ yasl_surface_set_pointer_mode(game->surface, YASL_POINTER_NORMAL);
} else {
return YASL_APP_QUIT;
}
@@ -209,25 +208,25 @@ static int on_event(void *ud, const struct yasl_event *ev)
break;
}
- case YASL_EVENT_MOUSE_MOTION:
+ case YASL_EVENT_POINTER_MOTION:
if (game->relative_mouse) {
/* Smooth first-person camera orbit */
- game->cam_yaw += (float)ev->mouse_motion.delta.x * 0.005f;
- game->cam_pitch += (float)ev->mouse_motion.delta.y * 0.005f;
+ game->cam_yaw += (float)ev->pointer.delta.x * 0.005f;
+ game->cam_pitch += (float)ev->pointer.delta.y * 0.005f;
if (game->cam_pitch > 1.4f) game->cam_pitch = 1.4f;
if (game->cam_pitch < -1.4f) game->cam_pitch = -1.4f;
}
break;
- case YASL_EVENT_MOUSE_BUTTON:
- if (ev->mouse_button.pressed && !game->relative_mouse) {
+ case YASL_EVENT_POINTER_DOWN:
+ if (!game->relative_mouse) {
/* Click window to capture cursor */
game->relative_mouse = true;
- yasl_surface_set_mouse_mode(game->surface, YASL_MOUSE_RELATIVE);
+ yasl_surface_set_pointer_mode(game->surface, YASL_POINTER_RELATIVE);
}
break;
- case YASL_EVENT_WINDOW_CLOSE_REQUEST:
+ case YASL_EVENT_WINDOW_CLOSE:
return YASL_APP_QUIT;
default:
diff --git a/examples/hello.c b/examples/hello.c
index 4153394..ded4235 100644
--- a/examples/hello.c
+++ b/examples/hello.c
@@ -7,7 +7,7 @@ struct app {
bool failed;
};
-static int init(void *ud, struct yasl_display *display)
+static enum yasl_app_result init(void *ud, struct yasl_display *display)
{
struct yasl_window_spec spec = {.title = "Hello YASL", .size = {640, 480}};
struct yasl_surface *surface = yasl_surface_create_toplevel(display, &spec);
@@ -20,10 +20,10 @@ static int init(void *ud, struct yasl_display *display)
return YASL_APP_CONTINUE;
}
-static int event(void *ud, const struct yasl_event *event)
+static enum yasl_app_result event(void *ud, const struct yasl_event *event)
{
(void)ud;
- return event->type == YASL_EVENT_WINDOW_CLOSE_REQUEST ? YASL_APP_QUIT : YASL_APP_CONTINUE;
+ return event->type == YASL_EVENT_WINDOW_CLOSE ? YASL_APP_QUIT : YASL_APP_CONTINUE;
}
static void draw(void *ud, struct yasl_surface *surface, struct yasl_frame_context *frame)
diff --git a/examples/notepad.c b/examples/notepad.c
index 6fb90e9..ea74a27 100644
--- a/examples/notepad.c
+++ b/examples/notepad.c
@@ -990,7 +990,7 @@ static void prompt_execute(struct editor *ed)
}
/* YASL App callbacks */
-static int on_init(void *ud, struct yasl_display *d)
+static enum yasl_app_result on_init(void *ud, struct yasl_display *d)
{
struct editor *ed = ud;
ed->display = d;
@@ -1021,7 +1021,7 @@ static int on_init(void *ud, struct yasl_display *d)
return YASL_APP_CONTINUE;
}
-static int on_event(void *ud, const struct yasl_event *ev)
+static enum yasl_app_result on_event(void *ud, const struct yasl_event *ev)
{
struct editor *ed = ud;
@@ -1029,28 +1029,28 @@ static int on_event(void *ud, const struct yasl_event *ev)
case YASL_EVENT_TEXT:
if (ed->prompt != PROMPT_NONE) {
for (size_t i = 0; i < ev->text.len && ed->prompt_len < (int)sizeof(ed->prompt_buf) - 1; i++) {
- if (ev->text.text[i] >= 32 && ev->text.text[i] <= 126) {
- ed->prompt_buf[ed->prompt_len++] = ev->text.text[i];
+ if (ev->text.data[i] >= 32 && ev->text.data[i] <= 126) {
+ ed->prompt_buf[ed->prompt_len++] = ev->text.data[i];
ed->prompt_buf[ed->prompt_len] = '\0';
}
}
} else {
- insert_text(ed, ev->text.text, ev->text.len);
+ insert_text(ed, ev->text.data, ev->text.len);
}
yasl_surface_request_frame(ed->surface);
break;
case YASL_EVENT_PASTE:
- if (ev->paste.text && ev->paste.len > 0) {
- insert_text(ed, ev->paste.text, ev->paste.len);
+ if (ev->text.data && ev->text.len > 0) {
+ insert_text(ed, ev->text.data, ev->text.len);
yasl_surface_request_frame(ed->surface);
}
break;
- case YASL_EVENT_DROP_FILES:
- if (ev->drop_files.files && ev->drop_files.len > 0) {
+ case YASL_EVENT_DROP:
+ if (ev->text.data && ev->text.len > 0) {
char path[512];
- decode_uri_path(ev->drop_files.files, path, sizeof(path));
+ decode_uri_path(ev->text.data, path, sizeof(path));
if (path[0]) {
editor_open_file(ed, path);
yasl_surface_request_frame(ed->surface);
@@ -1287,34 +1287,34 @@ static int on_event(void *ud, const struct yasl_event *ev)
}
break;
- case YASL_EVENT_MOUSE_BUTTON: {
- struct yasl_geometry geom = yasl_surface_geometry(ed->surface);
- int scale = (geom.scale_120 >= 240 ? 2 : 1) + ed->zoom_offset;
- if (scale < 1) scale = 1;
- int char_w = 8 * scale;
- int line_h = 16 * scale;
- int gutter_w = 48 * scale;
- int header_h = 24 * scale;
-
- int row = (ev->mouse_button.pos.y - header_h) / line_h + ed->scroll_y;
- int col = (ev->mouse_button.pos.x - gutter_w) / char_w + (ed->word_wrap ? 0 : ed->scroll_x);
- if (row < 0) row = 0;
- if (row >= ed->line_count) row = ed->line_count - 1;
- int len = (int)strlen(ed->lines[row]);
- if (col < 0) col = 0;
- if (col > len) col = len;
-
- if (ev->mouse_button.pressed && ev->mouse_button.button == 0) {
+ case YASL_EVENT_POINTER_DOWN: {
+ if (ev->pointer.button == YASL_BUTTON_PRIMARY) {
+ struct yasl_geometry geom = yasl_surface_geometry(ed->surface);
+ int scale = (geom.scale_120 >= 240 ? 2 : 1) + ed->zoom_offset;
+ if (scale < 1) scale = 1;
+ int char_w = 8 * scale;
+ int line_h = 16 * scale;
+ int gutter_w = 48 * scale;
+ int header_h = 24 * scale;
+
+ int row = (ev->pointer.pos.y - header_h) / line_h + ed->scroll_y;
+ int col = (ev->pointer.pos.x - gutter_w) / char_w + (ed->word_wrap ? 0 : ed->scroll_x);
+ if (row < 0) row = 0;
+ if (row >= ed->line_count) row = ed->line_count - 1;
+ int len = (int)strlen(ed->lines[row]);
+ if (col < 0) col = 0;
+ if (col > len) col = len;
+
uint64_t now = yasl_loop_now_ns();
if (now - ed->last_click_time < 350000000ull &&
- abs(ev->mouse_button.pos.x - ed->last_click_pos.x) < 8 &&
- abs(ev->mouse_button.pos.y - ed->last_click_pos.y) < 8) {
+ abs(ev->pointer.pos.x - ed->last_click_pos.x) < 8 &&
+ abs(ev->pointer.pos.y - ed->last_click_pos.y) < 8) {
ed->click_count++;
} else {
ed->click_count = 1;
}
ed->last_click_time = now;
- ed->last_click_pos = ev->mouse_button.pos;
+ ed->last_click_pos = ev->pointer.pos;
if (ed->click_count == 2) {
/* Double click: select word */
@@ -1340,13 +1340,17 @@ static int on_event(void *ud, const struct yasl_event *ev)
}
ed->caret_visible = true;
yasl_surface_request_frame(ed->surface);
- } else if (!ev->mouse_button.pressed && ev->mouse_button.button == 0) {
- ed->mouse_dragging = false;
}
break;
}
- case YASL_EVENT_MOUSE_MOTION:
+ case YASL_EVENT_POINTER_UP:
+ if (ev->pointer.button == YASL_BUTTON_PRIMARY) {
+ ed->mouse_dragging = false;
+ }
+ break;
+
+ case YASL_EVENT_POINTER_MOTION:
if (ed->mouse_dragging) {
struct yasl_geometry geom = yasl_surface_geometry(ed->surface);
int scale = (geom.scale_120 >= 240 ? 2 : 1) + ed->zoom_offset;
@@ -1356,8 +1360,8 @@ static int on_event(void *ud, const struct yasl_event *ev)
int gutter_w = 48 * scale;
int header_h = 24 * scale;
- int row = (ev->mouse_motion.pos.y - header_h) / line_h + ed->scroll_y;
- int col = (ev->mouse_motion.pos.x - gutter_w) / char_w + (ed->word_wrap ? 0 : ed->scroll_x);
+ int row = (ev->pointer.pos.y - header_h) / line_h + ed->scroll_y;
+ int col = (ev->pointer.pos.x - gutter_w) / char_w + (ed->word_wrap ? 0 : ed->scroll_x);
if (row < 0) row = 0;
if (row >= ed->line_count) row = ed->line_count - 1;
int len = (int)strlen(ed->lines[row]);
@@ -1371,18 +1375,18 @@ static int on_event(void *ud, const struct yasl_event *ev)
}
break;
- case YASL_EVENT_MOUSE_SCROLL:
- if (ev->mouse_scroll.mods & (YASL_MOD_CTRL | YASL_MOD_SUPER)) {
+ case YASL_EVENT_POINTER_SCROLL:
+ if (ev->pointer.mods & (YASL_MOD_CTRL | YASL_MOD_SUPER)) {
/* Ctrl + Wheel = Zoom */
- if (ev->mouse_scroll.scroll.y > 0 && ed->zoom_offset < 2) ed->zoom_offset++;
- else if (ev->mouse_scroll.scroll.y < 0 && ed->zoom_offset > -1) ed->zoom_offset--;
- } else if (ev->mouse_scroll.mods & YASL_MOD_SHIFT) {
+ if (ev->pointer.scroll.y > 0 && ed->zoom_offset < 2) ed->zoom_offset++;
+ else if (ev->pointer.scroll.y < 0 && ed->zoom_offset > -1) ed->zoom_offset--;
+ } else if (ev->pointer.mods & YASL_MOD_SHIFT) {
/* Shift + Wheel = Horizontal scroll */
- ed->scroll_x -= (int)(ev->mouse_scroll.scroll.y * 3.0);
+ ed->scroll_x -= (int)(ev->pointer.scroll.y * 3.0);
if (ed->scroll_x < 0) ed->scroll_x = 0;
} else {
/* Normal vertical scroll */
- ed->scroll_y -= (int)(ev->mouse_scroll.scroll.y * 3.0);
+ ed->scroll_y -= (int)(ev->pointer.scroll.y * 3.0);
if (ed->scroll_y < 0) ed->scroll_y = 0;
if (ed->scroll_y >= ed->line_count) ed->scroll_y = ed->line_count - 1;
}
@@ -1390,7 +1394,7 @@ static int on_event(void *ud, const struct yasl_event *ev)
break;
case YASL_EVENT_USER:
- if (ev->user.type == MSG_STATS_READY && ev->user.payload) {
+ if (ev->user.code == MSG_STATS_READY && ev->user.payload) {
struct doc_stats *s = (struct doc_stats *)ev->user.payload;
ed->stats = *s;
free(s);
@@ -1398,7 +1402,7 @@ static int on_event(void *ud, const struct yasl_event *ev)
}
break;
- case YASL_EVENT_WINDOW_CLOSE_REQUEST:
+ case YASL_EVENT_WINDOW_CLOSE:
return YASL_APP_QUIT;
default:
diff --git a/examples/simple_window.c b/examples/simple_window.c
index e9e6545..fca2f75 100644
--- a/examples/simple_window.c
+++ b/examples/simple_window.c
@@ -87,7 +87,7 @@ static void on_draw(void *ud, struct yasl_surface *s, struct yasl_frame_context
if (failed || smoke_test) yasl_surface_destroy(s);
}
-static int on_init(void *ud, struct yasl_display *display)
+static enum yasl_app_result on_init(void *ud, struct yasl_display *display)
{
(void)ud;
struct yasl_window_spec window_spec = {
@@ -123,19 +123,19 @@ static void pointer_sample(struct yasl_vector2i pos, enum yasl_pointer_device de
pen_tilt_y = pen.tilt_y;
}
-static int on_event(void *ud, const struct yasl_event *ev)
+static enum yasl_app_result on_event(void *ud, const struct yasl_event *ev)
{
(void)ud;
switch (ev->type) {
- case YASL_EVENT_WINDOW_CLOSE_REQUEST:
+ case YASL_EVENT_WINDOW_CLOSE:
return YASL_APP_QUIT;
case YASL_EVENT_WINDOW_RESIZE: {
uint32_t flags = yasl_surface_state(ev->surface);
const char *mode = flags & YASL_STATE_FULLSCREEN ? "fullscreen" :
flags & YASL_STATE_MAXIMIZED ? "maximized" :
flags & YASL_STATE_TILED ? "tiled" : "windowed";
- printf("[YASL] Resized to %dx%d (scale: %.2fx, %s)\n", ev->resize.size.x,
- ev->resize.size.y, (double)ev->resize.scale_120 / YASL_SCALE_UNIT, mode);
+ printf("[YASL] Resized to %dx%d (scale: %.2fx, %s)\n", ev->resize.geometry.size.x,
+ ev->resize.geometry.size.y, (double)ev->resize.geometry.scale_120 / YASL_SCALE_UNIT, mode);
break;
}
case YASL_EVENT_KEY:
@@ -144,22 +144,23 @@ static int on_event(void *ud, const struct yasl_event *ev)
if (ev->key.key == YASL_KEY_SPACE || ev->key.key == YASL_KEY_ENTER)
color_theme++;
break;
- case YASL_EVENT_MOUSE_BUTTON:
- pointer_sample(ev->mouse_button.pos, ev->mouse_button.device, ev->mouse_button.pen);
- if (ev->mouse_button.button == 0) {
- mouse_down = ev->mouse_button.pressed;
- if (!mouse_down) pen_pressure = 0;
+ case YASL_EVENT_POINTER_DOWN:
+ case YASL_EVENT_POINTER_UP:
+ case YASL_EVENT_POINTER_MOTION:
+ pointer_sample(ev->pointer.pos, ev->pointer.device, ev->pointer.pen);
+ if (ev->type == YASL_EVENT_POINTER_DOWN && ev->pointer.button == YASL_BUTTON_PRIMARY) {
+ mouse_down = true;
+ } else if (ev->type == YASL_EVENT_POINTER_UP && ev->pointer.button == YASL_BUTTON_PRIMARY) {
+ mouse_down = false;
+ pen_pressure = 0;
}
break;
- case YASL_EVENT_MOUSE_MOTION:
- pointer_sample(ev->mouse_motion.pos, ev->mouse_motion.device, ev->mouse_motion.pen);
- break;
- case YASL_EVENT_WINDOW_FOCUS:
+ case YASL_EVENT_FOCUS:
if (ev->focus.focused) return YASL_APP_CONTINUE;
/* Fall through to cancel held input on focus loss. */
[[fallthrough]];
case YASL_EVENT_INPUT_RESET:
- case YASL_EVENT_MOUSE_LEAVE:
+ case YASL_EVENT_POINTER_LEAVE:
mouse_x = mouse_y = -1;
mouse_down = false;
pen_pressure = 0;
diff --git a/examples/tablet.c b/examples/tablet.c
index f392cdc..01dcdfc 100644
--- a/examples/tablet.c
+++ b/examples/tablet.c
@@ -140,44 +140,40 @@ static void stroke(struct app *app)
app->previous = true;
}
-static int input(void *ud, const struct yasl_event *event)
+static enum yasl_app_result input(void *ud, const struct yasl_event *event)
{
struct app *app = ud;
- if (event->type == YASL_EVENT_WINDOW_CLOSE_REQUEST)
+ if (event->type == YASL_EVENT_WINDOW_CLOSE)
return YASL_APP_QUIT;
if (event->type == YASL_EVENT_WINDOW_RESIZE) {
- if (!resize_canvas(app, (struct yasl_geometry){event->resize.size, event->resize.scale_120})) {
+ if (!resize_canvas(app, event->resize.geometry)) {
app->failed = true;
return YASL_APP_QUIT;
}
- } else if (event->type == YASL_EVENT_INPUT_RESET || event->type == YASL_EVENT_MOUSE_LEAVE ||
- (event->type == YASL_EVENT_WINDOW_FOCUS && !event->focus.focused)) {
+ } else if (event->type == YASL_EVENT_INPUT_RESET || event->type == YASL_EVENT_POINTER_LEAVE ||
+ (event->type == YASL_EVENT_FOCUS && !event->focus.focused)) {
cancel_stroke(app);
} else if (event->type == YASL_EVENT_KEY && event->key.action == YASL_KEY_PRESS) {
if (event->key.key == YASL_KEY_ESCAPE) return YASL_APP_QUIT;
if (event->key.key == YASL_KEY_C || event->key.key == YASL_KEY_SPACE)
clear_canvas(app);
- } else if (event->type == YASL_EVENT_MOUSE_BUTTON) {
- if (app->pointer.device != event->mouse_button.device) cancel_stroke(app);
- app->pointer.pos = event->mouse_button.pos;
- app->pointer.device = event->mouse_button.device;
- app->pointer.pen = event->mouse_button.pen;
+ } else if (event->type == YASL_EVENT_POINTER_DOWN || event->type == YASL_EVENT_POINTER_UP ||
+ event->type == YASL_EVENT_POINTER_MOTION) {
+ if (app->pointer.device != event->pointer.device) cancel_stroke(app);
+ app->pointer.pos = event->pointer.pos;
+ app->pointer.device = event->pointer.device;
+ app->pointer.pen = event->pointer.pen;
app->visible = true;
- if (event->mouse_button.pressed && event->mouse_button.button == 0) {
+ if (event->type == YASL_EVENT_POINTER_DOWN && event->pointer.button == YASL_BUTTON_PRIMARY) {
app->down = true;
app->previous = false;
stroke(app);
- } else if (!event->mouse_button.pressed && event->mouse_button.button == 0) {
+ } else if (event->type == YASL_EVENT_POINTER_UP && event->pointer.button == YASL_BUTTON_PRIMARY) {
app->down = app->previous = false;
app->pointer.pen.pressure = 0;
+ } else if (event->type == YASL_EVENT_POINTER_MOTION) {
+ if (app->down) stroke(app);
}
- } else if (event->type == YASL_EVENT_MOUSE_MOTION) {
- if (app->pointer.device != event->mouse_motion.device) cancel_stroke(app);
- app->pointer.pos = event->mouse_motion.pos;
- app->pointer.device = event->mouse_motion.device;
- app->pointer.pen = event->mouse_motion.pen;
- app->visible = true;
- if (app->down) stroke(app);
} else {
return YASL_APP_CONTINUE;
}
@@ -293,7 +289,7 @@ static void draw(void *ud, struct yasl_surface *surface, struct yasl_frame_conte
}
}
-static int init(void *ud, struct yasl_display *display)
+static enum yasl_app_result init(void *ud, struct yasl_display *display)
{
struct app *app = ud;
app->backend = yasl_display_backend(display);
@@ -307,7 +303,7 @@ static int init(void *ud, struct yasl_display *display)
app->failed = true;
return YASL_APP_QUIT;
}
- yasl_surface_set_mouse_mode(app->surface, YASL_MOUSE_HIDDEN);
+ yasl_surface_set_cursor(app->surface, YASL_CURSOR_HIDDEN);
yasl_surface_request_frame(app->surface);
printf("YASL tablet on %s: pressure changes brush width; eraser removes ink.\n"
"Mouse: fixed-width drawing. C/Space: clear. Escape: quit.\n",