summaryrefslogtreecommitdiff
path: root/examples/simple_window.c
diff options
context:
space:
mode:
Diffstat (limited to 'examples/simple_window.c')
-rw-r--r--examples/simple_window.c200
1 files changed, 98 insertions, 102 deletions
diff --git a/examples/simple_window.c b/examples/simple_window.c
index 797de3b..e9e6545 100644
--- a/examples/simple_window.c
+++ b/examples/simple_window.c
@@ -1,9 +1,8 @@
-#include "yasl/yasl.h"
+#include <yasl/app.h>
#include <stdio.h>
-#include <stdlib.h>
-#include <stdbool.h>
+#include <string.h>
-static bool running = true;
+static bool failed, smoke_test;
static int mouse_x = -1;
static int mouse_y = -1;
static bool mouse_down = false;
@@ -23,12 +22,12 @@ static const char *device_name(enum yasl_pointer_device dev)
}
}
-static void on_draw(struct yasl_surface *s, struct yasl_frame *frame, void *ud)
+static void on_draw(void *ud, struct yasl_surface *s, struct yasl_frame_context *frame)
{
(void)ud;
- int w = frame->target.size.x;
- int h = frame->target.size.y;
- uint32_t *pixels = frame->target.pixels;
+ int w = frame->size.x;
+ int h = frame->size.y;
+ uint32_t *pixels = frame->cpu.pixels;
if (!pixels || w <= 0 || h <= 0)
return;
@@ -81,63 +80,28 @@ static void on_draw(struct yasl_surface *s, struct yasl_frame *frame, void *ud)
}
}
- 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;
+ if (!yasl_frame_present_cpu(frame, NULL, 0)) {
+ fprintf(stderr, "Failed to present frame\n");
+ failed = true;
+ }
+ if (failed || smoke_test) yasl_surface_destroy(s);
}
-static void on_quit(void *ud)
+static int on_init(void *ud, struct yasl_display *display)
{
(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_window_cbs window_cbs = {
- .draw = on_draw,
- .configure = on_configure,
- .close = on_close,
- };
struct yasl_window_spec window_spec = {
.title = "YASL Interactive Window (Pen & Touch Enabled)",
.size = { .x = 640, .y = 480 },
- .cbs = &window_cbs,
};
- struct yasl_surface *s = yasl_surface_create_toplevel(d, &window_spec);
+ struct yasl_surface *s = yasl_surface_create_toplevel(display, &window_spec);
if (!s) {
fprintf(stderr, "Failed to create surface\n");
- yasl_display_close(d);
- return 1;
+ failed = true;
+ return YASL_APP_QUIT;
}
- yasl_surface_schedule_redraw(s);
+ yasl_surface_request_frame(s);
printf("=== YASL Demo Running ===\n");
printf(" - Stylus/Pen: full pressure (0..65535) and tilt detection\n");
@@ -145,56 +109,88 @@ int main(void)
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);
- }
- }
+ return YASL_APP_CONTINUE;
+}
- yasl_display_flush(d);
+static void pointer_sample(struct yasl_vector2i pos, enum yasl_pointer_device device,
+ struct yasl_pointer_pen pen)
+{
+ mouse_x = pos.x;
+ mouse_y = pos.y;
+ pointer_device = device;
+ pen_pressure = pen.pressure;
+ pen_tilt_x = pen.tilt_x;
+ pen_tilt_y = pen.tilt_y;
+}
+
+static int on_event(void *ud, const struct yasl_event *ev)
+{
+ (void)ud;
+ switch (ev->type) {
+ case YASL_EVENT_WINDOW_CLOSE_REQUEST:
+ 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);
+ break;
}
+ case YASL_EVENT_KEY:
+ if (ev->key.action != YASL_KEY_PRESS) return YASL_APP_CONTINUE;
+ if (ev->key.key == YASL_KEY_ESCAPE) return YASL_APP_QUIT;
+ 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;
+ }
+ 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:
+ 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:
+ mouse_x = mouse_y = -1;
+ mouse_down = false;
+ pen_pressure = 0;
+ break;
+ default:
+ return YASL_APP_CONTINUE;
+ }
+ if (pen_pressure > 0) {
+ printf("[Pen] 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));
+ }
+ yasl_surface_request_frame(ev->surface);
+ return YASL_APP_CONTINUE;
+}
- yasl_surface_destroy(s);
- yasl_display_close(d);
- return 0;
+int main(int argc, char **argv)
+{
+ if (argc == 2 && strcmp(argv[1], "--smoke-test") == 0)
+ smoke_test = true;
+ else if (argc != 1) {
+ fprintf(stderr, "Usage: %s [--smoke-test]\n", argv[0]);
+ return 1;
+ }
+ struct yasl_app_desc desc = {
+ .app_id = "org.yasl.example",
+ .init = on_init,
+ .event = on_event,
+ .frame = on_draw,
+ };
+ int result = yasl_run(&desc);
+ if (result) perror("yasl_run");
+ return result || failed ? 1 : 0;
}