summaryrefslogtreecommitdiff
path: root/examples/notepad.c
diff options
context:
space:
mode:
authorBerke Güzel <wenekar1@gmail.com>2026-10-13 21:23:03 +0300
committerBerke Güzel <wenekar1@gmail.com>2026-10-13 21:23:03 +0300
commitdd1775a9927922c14f8679ccbc4d3ca8d5917e20 (patch)
tree3acc442bb4a3470364b74aa82ff67cb84d555c45 /examples/notepad.c
parentecb81aeea4c0c880b3a60a0dc8418c4bf2a7cf85 (diff)
public api refactor
Diffstat (limited to 'examples/notepad.c')
-rw-r--r--examples/notepad.c66
1 files changed, 43 insertions, 23 deletions
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)