summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
Diffstat (limited to 'examples')
-rw-r--r--examples/game.c18
-rw-r--r--examples/hello.c12
-rw-r--r--examples/notepad.c66
-rw-r--r--examples/simple_window.c17
-rw-r--r--examples/tablet.c16
5 files changed, 77 insertions, 52 deletions
diff --git a/examples/game.c b/examples/game.c
index 4517519..33c2f6e 100644
--- a/examples/game.c
+++ b/examples/game.c
@@ -147,13 +147,14 @@ static enum yasl_app_result on_init(void *ud, struct yasl_display *d)
struct yasl_window_spec spec = {
.title = "YASL 3D Game Loop (Approach A)",
- .size = {800, 600},
+ .size_logical = {800, 600},
};
game->surface = yasl_surface_create_toplevel(d, &spec);
- if (!game->surface) return YASL_APP_QUIT;
+ if (!game->surface) return YASL_APP_ERROR;
/* Drive continuous 60/144Hz ticks */
- yasl_surface_set_pacing(game->surface, YASL_PACING_CONTINUOUS);
+ if (!yasl_surface_set_pacing(game->surface, YASL_PACING_CONTINUOUS))
+ return YASL_APP_ERROR;
game->cam_z = 3.5f;
game->relative_mouse = false;
@@ -237,13 +238,13 @@ static enum yasl_app_result on_event(void *ud, const struct yasl_event *ev)
return YASL_APP_CONTINUE;
}
-static void on_frame(void *ud, struct yasl_surface *s, struct yasl_frame_context *ctx)
+static void on_frame(void *ud, struct yasl_surface *s, struct yasl_frame *ctx)
{
(void)s;
struct game_state *game = ud;
- int w = ctx->size.x;
- int h = ctx->size.y;
- uint32_t *pix = ctx->cpu.pixels;
+ int w = ctx->target.size.x;
+ int h = ctx->target.size.y;
+ uint32_t *pix = ctx->target.pixels;
if (!pix || w <= 0 || h <= 0) return;
/* Update game simulation */
@@ -339,7 +340,8 @@ static void on_frame(void *ud, struct yasl_surface *s, struct yasl_frame_context
draw_number(pix, w, h, 16 * scale, 16 * scale, (int)game->current_fps, 0xff00ff00, scale);
/* Submit frame */
- yasl_frame_present_cpu(ctx, NULL, 0);
+ if (!yasl_frame_present(ctx, NULL, 0))
+ yasl_app_exit(yasl_surface_display(s), YASL_APP_ERROR);
}
int main(void)
diff --git a/examples/hello.c b/examples/hello.c
index 4c171ec..31ab18b 100644
--- a/examples/hello.c
+++ b/examples/hello.c
@@ -3,12 +3,12 @@
#include <stdio.h>
static bool failed;
-static void draw(void *ud, struct yasl_surface *surface, struct yasl_frame_context *frame)
+static void draw(void *ud, struct yasl_surface *surface, struct yasl_frame *frame)
{
(void)ud;
- int w = frame->size.x, h = frame->size.y;
+ int w = frame->target.size.x, h = frame->target.size.y;
for (int y = 0; y < h; y++) {
- uint32_t *row = frame->cpu.pixels + (size_t)y * (frame->cpu.stride_bytes / 4);
+ uint32_t *row = frame->target.pixels + (size_t)y * (frame->target.stride_bytes / 4);
for (int x = 0; x < w; x++) {
/* Red grows rightward, green downward, blue fades; opaque premultiplied ARGB. */
uint32_t r = (uint32_t)(x * 255 / (w > 1 ? w - 1 : 1));
@@ -17,16 +17,16 @@ static void draw(void *ud, struct yasl_surface *surface, struct yasl_frame_conte
}
}
- if (!yasl_frame_present_cpu(frame, NULL, 0)) {
+ if (!yasl_frame_present(frame, NULL, 0)) {
fprintf(stderr, "Could not present the draw target\n");
failed = true;
}
- if (failed) yasl_surface_destroy(surface);
+ if (failed) yasl_app_exit(yasl_surface_display(surface), YASL_APP_ERROR);
}
int main(void)
{
- struct yasl_window_spec window = {.title = "Hello YASL", .size = {640, 480}};
+ struct yasl_window_spec window = {.title = "Hello YASL", .size_logical = {640, 480}};
struct yasl_app_desc desc = {
.app_id = "org.example.hello",
.window = &window,
diff --git a/examples/notepad.c b/examples/notepad.c
index ea74a27..0b331e8 100644
--- a/examples/notepad.c
+++ b/examples/notepad.c
@@ -14,7 +14,7 @@
* - Zoom In / Out / Reset (Ctrl+=, Ctrl+-, Ctrl+0)
* - Mouse selection with click, drag, double-click (word), triple-click (line)
* - Unsaved changes confirmation on exit
- * - Real-time asynchronous background stats via yasl_post_event()
+ * - Real-time asynchronous background stats via yasl_display_post()
* - 0% CPU consumption when idle (YASL_PACING_ON_DEMAND)
*/
@@ -211,7 +211,9 @@ struct editor {
/* Background stats from async worker */
struct doc_stats stats;
- bool worker_running;
+ _Atomic bool worker_running;
+ bool worker_started, worker_mutex_ready, worker_cond_ready;
+ uint64_t worker_surface_id;
bool worker_dirty;
pthread_t worker_tid;
pthread_mutex_t worker_mutex;
@@ -361,7 +363,10 @@ static void *worker_thread(void *arg)
if (s) {
s->words = word_count;
s->chars = char_count;
- yasl_post_event(ed->display, ed->surface, MSG_STATS_READY, s, sizeof(*s));
+ if (!yasl_display_post(ed->display, &(struct yasl_message){
+ .target_id = ed->worker_surface_id, .code = MSG_STATS_READY,
+ .payload = s, .size = sizeof(*s), .release = free}))
+ free(s);
}
}
return NULL;
@@ -580,7 +585,8 @@ static void editor_copy(struct editor *ed)
snprintf(buf, (size_t)MAX_LINES * MAX_LINE_LEN, "%s\n", ed->lines[ed->cursor_row]);
}
- yasl_display_copy(ed->display, YASL_CLIPBOARD, buf);
+ if (!yasl_clipboard_set(ed->display, YASL_CLIPBOARD, buf))
+ editor_set_status(ed, "Clipboard copy failed");
free(buf);
editor_set_status(ed, "Copied to clipboard");
}
@@ -997,18 +1003,23 @@ static enum yasl_app_result on_init(void *ud, struct yasl_display *d)
struct yasl_window_spec spec = {
.title = "Untitled - YASL Notepad",
- .size = {960, 640},
+ .size_logical = {960, 640},
};
ed->surface = yasl_surface_create_toplevel(d, &spec);
- if (!ed->surface) return YASL_APP_QUIT;
+ if (!ed->surface) return YASL_APP_ERROR;
+ ed->worker_surface_id = yasl_surface_id(ed->surface);
yasl_surface_set_pacing(ed->surface, YASL_PACING_ON_DEMAND);
- yasl_app_add_timer(d, YASL_MS_TO_NS(500), on_blink_timer, ed);
+ if (!yasl_surface_add_timer(ed->surface, YASL_MS_TO_NS(500), on_blink_timer, ed))
+ return YASL_APP_ERROR;
ed->worker_running = true;
- pthread_mutex_init(&ed->worker_mutex, NULL);
- pthread_cond_init(&ed->worker_cond, NULL);
- pthread_create(&ed->worker_tid, NULL, worker_thread, ed);
+ if (pthread_mutex_init(&ed->worker_mutex, NULL)) return YASL_APP_ERROR;
+ ed->worker_mutex_ready = true;
+ if (pthread_cond_init(&ed->worker_cond, NULL)) return YASL_APP_ERROR;
+ ed->worker_cond_ready = true;
+ if (pthread_create(&ed->worker_tid, NULL, worker_thread, ed)) return YASL_APP_ERROR;
+ ed->worker_started = true;
if (ed->has_file) {
editor_open_file(ed, ed->file_path);
@@ -1041,6 +1052,12 @@ static enum yasl_app_result on_event(void *ud, const struct yasl_event *ev)
break;
case YASL_EVENT_PASTE:
+ if (ev->paste.status != YASL_TRANSFER_OK) {
+ editor_set_status(ed, ev->paste.status == YASL_TRANSFER_UNAVAILABLE
+ ? "Clipboard has no text" : "Clipboard transfer failed");
+ yasl_surface_request_frame(ed->surface);
+ break;
+ }
if (ev->text.data && ev->text.len > 0) {
insert_text(ed, ev->text.data, ev->text.len);
yasl_surface_request_frame(ed->surface);
@@ -1167,7 +1184,8 @@ static enum yasl_app_result on_event(void *ud, const struct yasl_event *ev)
yasl_surface_request_frame(ed->surface);
return YASL_APP_CONTINUE;
case YASL_KEY_V:
- yasl_display_paste(ed->display, YASL_CLIPBOARD, ed->surface);
+ if (!yasl_clipboard_request(ed->surface, YASL_CLIPBOARD))
+ editor_set_status(ed, "Clipboard paste failed");
return YASL_APP_CONTINUE;
case YASL_KEY_Z:
if (shift) editor_redo(ed);
@@ -1397,7 +1415,6 @@ static enum yasl_app_result on_event(void *ud, const struct yasl_event *ev)
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);
yasl_surface_request_frame(ed->surface);
}
break;
@@ -1412,13 +1429,13 @@ static enum yasl_app_result on_event(void *ud, const struct yasl_event *ev)
return YASL_APP_CONTINUE;
}
-static void on_frame(void *ud, struct yasl_surface *s, struct yasl_frame_context *ctx)
+static void on_frame(void *ud, struct yasl_surface *s, struct yasl_frame *ctx)
{
(void)s;
struct editor *ed = ud;
- int w = ctx->size.x;
- int h = ctx->size.y;
- uint32_t *pix = ctx->cpu.pixels;
+ int w = ctx->target.size.x;
+ int h = ctx->target.size.y;
+ uint32_t *pix = ctx->target.pixels;
if (!pix || w <= 0 || h <= 0) return;
int scale = (ctx->scale_120 >= 240 ? 2 : 1) + ed->zoom_offset;
@@ -1554,7 +1571,8 @@ static void on_frame(void *ud, struct yasl_surface *s, struct yasl_frame_context
ed->is_modified ? 0xfffab387 : status_accent, scale);
}
- yasl_frame_present_cpu(ctx, NULL, 0);
+ if (!yasl_frame_present(ctx, NULL, 0))
+ yasl_app_exit(yasl_surface_display(s), YASL_APP_ERROR);
}
static void on_quit(void *ud, struct yasl_display *d)
@@ -1562,12 +1580,14 @@ static void on_quit(void *ud, struct yasl_display *d)
(void)d;
struct editor *ed = ud;
ed->worker_running = false;
- pthread_mutex_lock(&ed->worker_mutex);
- pthread_cond_broadcast(&ed->worker_cond);
- pthread_mutex_unlock(&ed->worker_mutex);
- pthread_join(ed->worker_tid, NULL);
- pthread_mutex_destroy(&ed->worker_mutex);
- pthread_cond_destroy(&ed->worker_cond);
+ if (ed->worker_started) {
+ pthread_mutex_lock(&ed->worker_mutex);
+ pthread_cond_broadcast(&ed->worker_cond);
+ pthread_mutex_unlock(&ed->worker_mutex);
+ pthread_join(ed->worker_tid, NULL);
+ }
+ if (ed->worker_cond_ready) pthread_cond_destroy(&ed->worker_cond);
+ if (ed->worker_mutex_ready) pthread_mutex_destroy(&ed->worker_mutex);
}
int main(int argc, char **argv)
diff --git a/examples/simple_window.c b/examples/simple_window.c
index fca2f75..5a8b314 100644
--- a/examples/simple_window.c
+++ b/examples/simple_window.c
@@ -22,12 +22,12 @@ static const char *device_name(enum yasl_pointer_device dev)
}
}
-static void on_draw(void *ud, struct yasl_surface *s, struct yasl_frame_context *frame)
+static void on_draw(void *ud, struct yasl_surface *s, struct yasl_frame *frame)
{
(void)ud;
- int w = frame->size.x;
- int h = frame->size.y;
- uint32_t *pixels = frame->cpu.pixels;
+ 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;
@@ -80,11 +80,12 @@ static void on_draw(void *ud, struct yasl_surface *s, struct yasl_frame_context
}
}
- if (!yasl_frame_present_cpu(frame, NULL, 0)) {
+ if (!yasl_frame_present(frame, NULL, 0)) {
fprintf(stderr, "Failed to present frame\n");
failed = true;
}
- if (failed || smoke_test) yasl_surface_destroy(s);
+ if (failed) yasl_app_exit(yasl_surface_display(s), YASL_APP_ERROR);
+ else if (smoke_test) yasl_surface_destroy(s);
}
static enum yasl_app_result on_init(void *ud, struct yasl_display *display)
@@ -92,13 +93,13 @@ static enum yasl_app_result on_init(void *ud, struct yasl_display *display)
(void)ud;
struct yasl_window_spec window_spec = {
.title = "YASL Interactive Window (Pen & Touch Enabled)",
- .size = { .x = 640, .y = 480 },
+ .size_logical = { .x = 640, .y = 480 },
};
struct yasl_surface *s = yasl_surface_create_toplevel(display, &window_spec);
if (!s) {
fprintf(stderr, "Failed to create surface\n");
failed = true;
- return YASL_APP_QUIT;
+ return YASL_APP_ERROR;
}
yasl_surface_request_frame(s);
diff --git a/examples/tablet.c b/examples/tablet.c
index 01dcdfc..ba1a94f 100644
--- a/examples/tablet.c
+++ b/examples/tablet.c
@@ -148,7 +148,7 @@ static enum yasl_app_result input(void *ud, const struct yasl_event *event)
if (event->type == YASL_EVENT_WINDOW_RESIZE) {
if (!resize_canvas(app, event->resize.geometry)) {
app->failed = true;
- return YASL_APP_QUIT;
+ return YASL_APP_ERROR;
}
} else if (event->type == YASL_EVENT_INPUT_RESET || event->type == YASL_EVENT_POINTER_LEAVE ||
(event->type == YASL_EVENT_FOCUS && !event->focus.focused)) {
@@ -233,18 +233,19 @@ static void text(struct yasl_framebuffer *fb, double dpi, int x, int y,
}
}
-static void draw(void *ud, struct yasl_surface *surface, struct yasl_frame_context *frame)
+static void draw(void *ud, struct yasl_surface *surface, struct yasl_frame *frame)
{
struct app *app = ud;
- struct yasl_geometry geometry = {frame->size, frame->scale_120};
+ struct yasl_geometry geometry = {frame->target.size, frame->scale_120};
if (!resize_canvas(app, geometry)) {
fprintf(stderr, "Cannot allocate drawing canvas\n");
app->failed = true;
+ yasl_app_exit(yasl_surface_display(surface), YASL_APP_ERROR);
yasl_surface_destroy(surface);
app->surface = NULL;
return;
}
- struct yasl_framebuffer target = {.size = frame->size, .pixels = frame->cpu.pixels};
+ struct yasl_framebuffer target = {.size = frame->target.size, .pixels = frame->target.pixels};
struct yasl_framebuffer *fb = &target;
memcpy(fb->pixels, app->canvas, (size_t)geometry.size.x * geometry.size.y * sizeof(uint32_t));
double dpi = (double)app->geometry.scale_120 / YASL_SCALE_UNIT;
@@ -280,9 +281,10 @@ static void draw(void *ud, struct yasl_surface *surface, struct yasl_frame_conte
for (int i = 0; i <= 64; i++)
rect(fb, (int)lround(cx + tx * i / 64), (int)lround(cy + ty * i / 64), 2, 2, ACCENT);
}
- if (!yasl_frame_present_cpu(frame, NULL, 0)) {
+ if (!yasl_frame_present(frame, NULL, 0)) {
app->failed = true;
}
+ if (app->failed) yasl_app_exit(yasl_surface_display(surface), YASL_APP_ERROR);
if (app->failed || app->smoke_test) {
yasl_surface_destroy(surface);
app->surface = NULL;
@@ -295,13 +297,13 @@ static enum yasl_app_result init(void *ud, struct yasl_display *display)
app->backend = yasl_display_backend(display);
app->tablet_supported = (yasl_display_caps(display) & YASL_CAP_TABLET) != 0;
struct yasl_window_spec spec = {
- .title = "YASL Tablet - pressure and tilt", .size = {900, 640},
+ .title = "YASL Tablet - pressure and tilt", .size_logical = {900, 640},
};
app->surface = yasl_surface_create_toplevel(display, &spec);
if (!app->surface || !resize_canvas(app, yasl_surface_geometry(app->surface))) {
fprintf(stderr, "Cannot create drawing surface\n");
app->failed = true;
- return YASL_APP_QUIT;
+ return YASL_APP_ERROR;
}
yasl_surface_set_cursor(app->surface, YASL_CURSOR_HIDDEN);
yasl_surface_request_frame(app->surface);