summaryrefslogtreecommitdiff
path: root/examples/notepad.c
diff options
context:
space:
mode:
authorBerke Güzel <wenekar1@gmail.com>2026-10-14 13:56:48 +0300
committerBerke Güzel <wenekar1@gmail.com>2026-10-14 13:56:48 +0300
commit87279f6903343e621c0446b20d1e7d6a495f1125 (patch)
tree999b5fb5fb76702efb02f856f082147304f0859b /examples/notepad.c
parent1ab3e5a123109704789c2768e11a4f9af515821d (diff)
Simplify the public API into one dense header
yasl.h now holds everything except Vulkan: 45 functions instead of 62, and three installed headers instead of ten. - One constructor: yasl_surface_create takes CPU, GL or NATIVE, with zero-initialized GL options meaning 3.2 core, RGBA8, D24S8. - Events carry action, mods, x, y and text at the top level; pen positions are now logical like pointer positions. - yasl_cpu_pixels/yasl_cpu_present draw into one persistent canvas, cutting three full-frame copies per frame. - yasl_surface_get_info, yasl_surface_request, YASL_CURSOR_LOCKED, yasl_surface_set_text_input, yasl_surface_begin_move_resize and yasl_surface_get_native replace groups of narrower calls. - Interactive calls drop the decorative trigger event argument. - yasl_wait_event returns 0 only on timeout or yasl_wake, the refresh callback runs only in native modal loops, focus loss sends INPUT_RESET, and a lost event no longer fails every later wait. - Unsupported operations and bad arguments report ENOTSUP and EINVAL instead of EIO; the keycodes header no longer needs C23. - The shared library gets a versioned SONAME and version macros. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Diffstat (limited to 'examples/notepad.c')
-rw-r--r--examples/notepad.c111
1 files changed, 54 insertions, 57 deletions
diff --git a/examples/notepad.c b/examples/notepad.c
index ff12038..d0b38b9 100644
--- a/examples/notepad.c
+++ b/examples/notepad.c
@@ -19,7 +19,6 @@
#define _DEFAULT_SOURCE
#include <yasl/yasl.h>
-#include <yasl/cpu.h>
#include <ctype.h>
#include <math.h>
@@ -988,10 +987,9 @@ static void prompt_execute(struct editor *ed)
static bool editor_init(struct editor *ed)
{
- ed->surface = yasl_surface_create(&(struct yasl_surface_options){.title="Untitled - YASL Notepad", .width=960, .height=640, .type=YASL_SURFACE_CPU, .flags=0});
+ ed->surface = yasl_surface_create(&(struct yasl_surface_options){.title="Untitled - YASL Notepad", .width=960, .height=640});
if (!ed->surface) return false;
- ed->surface_scale = yasl_surface_get_scale(ed->surface);
- yasl_surface_set_text_input(ed->surface, true, YASL_TEXT_NORMAL);
+ ed->surface_scale = yasl_surface_get_info(ed->surface).scale;
ed->worker_running = true;
if (pthread_mutex_init(&ed->worker_mutex, NULL)) return false;
@@ -1034,19 +1032,21 @@ static void editor_update_ime(struct editor *ed)
ed->ime_snapshot_ready = true;
ed->text_revision++;
}
- if (snapshot_changed)
- yasl_surface_set_surrounding_text(ed->surface, ed->ime_snapshot, strlen(ed->ime_snapshot),
- ed->ime_begin, ed->ime_end, ed->text_revision);
- double scale = yasl_surface_get_scale(ed->surface);
+ double scale = yasl_surface_get_info(ed->surface).scale;
int zoom = (scale >= 2.0 ? 2 : 1) + ed->zoom_offset;
if (zoom < 1) zoom = 1;
double caret_x = (48.0 * zoom + 8.0 * zoom + ed->cursor_col * 8.0 * zoom) / scale;
double caret_y = (24.0 * zoom + (ed->cursor_row - ed->scroll_y) * 16.0 * zoom) / scale;
- if (!ed->ime_caret_ready || caret_x != ed->ime_caret_x || caret_y != ed->ime_caret_y) {
+ if (snapshot_changed || !ed->ime_caret_ready || caret_x != ed->ime_caret_x ||
+ caret_y != ed->ime_caret_y) {
ed->ime_caret_x = caret_x;
ed->ime_caret_y = caret_y;
ed->ime_caret_ready = true;
- yasl_surface_set_ime_caret(ed->surface, caret_x, caret_y, 2.0, 16.0);
+ yasl_surface_set_text_input(ed->surface, &(struct yasl_text_input){
+ .text = ed->ime_snapshot, .length = strlen(ed->ime_snapshot),
+ .selection_begin = ed->ime_begin, .selection_end = ed->ime_end,
+ .revision = ed->text_revision, .caret_x = caret_x, .caret_y = caret_y,
+ .caret_width = 2.0, .caret_height = 16.0});
}
}
@@ -1055,49 +1055,49 @@ static void on_event(struct editor *ed, const yasl_event *ev)
switch (ev->type) {
case YASL_EVENT_TEXT:
if (ed->prompt != PROMPT_NONE) {
- for (size_t i = 0; i < ev->text.length && ed->prompt_len < (int)sizeof(ed->prompt_buf) - 1; i++) {
- if (ev->text.data[i] >= 32 && ev->text.data[i] <= 126) {
- ed->prompt_buf[ed->prompt_len++] = ev->text.data[i];
+ for (size_t i = 0; i < ev->length && ed->prompt_len < (int)sizeof(ed->prompt_buf) - 1; i++) {
+ if (ev->text[i] >= 32 && ev->text[i] <= 126) {
+ ed->prompt_buf[ed->prompt_len++] = ev->text[i];
ed->prompt_buf[ed->prompt_len] = '\0';
}
}
} else {
- insert_text(ed, ev->text.data, ev->text.length);
+ insert_text(ed, ev->text, ev->length);
}
editor_request_render(ed);
break;
case YASL_EVENT_CLIPBOARD:
- if (ev->clipboard.status != YASL_TRANSFER_OK) {
- editor_set_status(ed, ev->clipboard.status == YASL_TRANSFER_UNAVAILABLE
+ if (ev->action != YASL_TRANSFER_OK) {
+ editor_set_status(ed, ev->action == YASL_TRANSFER_UNAVAILABLE
? "Clipboard has no text" : "Clipboard transfer failed");
editor_request_render(ed);
break;
}
- if (ev->clipboard.data && ev->clipboard.length > 0) {
- insert_text(ed, ev->clipboard.data, ev->clipboard.length);
+ if (ev->text && ev->length > 0) {
+ insert_text(ed, ev->text, ev->length);
editor_request_render(ed);
}
break;
case YASL_EVENT_TEXT_EDIT:
- if (ed->prompt == PROMPT_NONE && ev->text_edit.data &&
- ev->text_edit.revision == ed->text_revision &&
- ev->text_edit.begin <= ev->text_edit.end &&
- ev->text_edit.end <= strlen(ed->ime_snapshot)) {
- ed->cursor_col = (int)ev->text_edit.begin;
+ if (ed->prompt == PROMPT_NONE && ev->text &&
+ ev->ime.revision == ed->text_revision &&
+ ev->ime.begin <= ev->ime.end &&
+ ev->ime.end <= strlen(ed->ime_snapshot)) {
+ ed->cursor_col = (int)ev->ime.begin;
ed->sel_anchor_row = ed->cursor_row;
- ed->sel_anchor_col = (int)ev->text_edit.begin;
- ed->cursor_col = (int)ev->text_edit.end;
- ed->has_selection = ev->text_edit.begin != ev->text_edit.end;
- insert_text(ed, ev->text_edit.data, ev->text_edit.length);
+ ed->sel_anchor_col = (int)ev->ime.begin;
+ ed->cursor_col = (int)ev->ime.end;
+ ed->has_selection = ev->ime.begin != ev->ime.end;
+ insert_text(ed, ev->text, ev->length);
editor_request_render(ed);
}
break;
case YASL_EVENT_DROP:
- if (ev->drop.action == YASL_DROP_DATA) {
- const char *cursor = ev->drop.data;
+ if (ev->action == YASL_DROP_RELEASE) {
+ const char *cursor = ev->text;
char path[512];
size_t length;
while ((length = yasl_drop_next_path(&cursor, path, sizeof(path))) > 0) {
@@ -1111,9 +1111,9 @@ static void on_event(struct editor *ed, const yasl_event *ev)
break;
case YASL_EVENT_KEY:
- if (ev->key.action == YASL_KEY_PRESS || ev->key.action == YASL_KEY_REPEAT) {
- bool ctrl = (ev->key.mods & (YASL_MOD_CTRL | YASL_MOD_SUPER)) != 0;
- bool shift = (ev->key.mods & YASL_MOD_SHIFT) != 0;
+ if (ev->action == YASL_KEY_PRESS || ev->action == YASL_KEY_REPEAT) {
+ bool ctrl = (ev->mods & (YASL_MOD_CTRL | YASL_MOD_SUPER)) != 0;
+ bool shift = (ev->mods & YASL_MOD_SHIFT) != 0;
/* Interactive Prompt Mode */
if (ed->prompt != PROMPT_NONE) {
@@ -1219,7 +1219,7 @@ static void on_event(struct editor *ed, const yasl_event *ev)
editor_request_render(ed);
return;
case YASL_KEY_V:
- if (!yasl_clipboard_request(ed->surface, YASL_CLIPBOARD))
+ if (yasl_clipboard_request(ed->surface, YASL_CLIPBOARD) < 0)
editor_set_status(ed, "Clipboard paste failed");
return;
case YASL_KEY_Z:
@@ -1341,10 +1341,10 @@ static void on_event(struct editor *ed, const yasl_event *ev)
break;
case YASL_EVENT_POINTER: {
- double device_scale = yasl_surface_get_scale(ed->surface);
- int px = (int)lround(ev->pointer.x * device_scale);
- int py = (int)lround(ev->pointer.y * device_scale);
- if (ev->pointer.action == YASL_POINTER_DOWN && ev->pointer.button == YASL_BUTTON_PRIMARY) {
+ double device_scale = yasl_surface_get_info(ed->surface).scale;
+ int px = (int)lround(ev->x * device_scale);
+ int py = (int)lround(ev->y * device_scale);
+ if (ev->action == YASL_POINTER_DOWN && ev->pointer.button == YASL_BUTTON_PRIMARY) {
int scale = (device_scale >= 2.0 ? 2 : 1) + ed->zoom_offset;
if (scale < 1) scale = 1;
int char_w = 8 * scale;
@@ -1395,11 +1395,11 @@ static void on_event(struct editor *ed, const yasl_event *ev)
}
ed->caret_visible = true;
editor_request_render(ed);
- } else if (ev->pointer.action == YASL_POINTER_UP && ev->pointer.button == YASL_BUTTON_PRIMARY) {
+ } else if (ev->action == YASL_POINTER_UP && ev->pointer.button == YASL_BUTTON_PRIMARY) {
ed->mouse_dragging = false;
- } else if (ev->pointer.action == YASL_POINTER_LEAVE) {
+ } else if (ev->action == YASL_POINTER_LEAVE) {
ed->mouse_dragging = false;
- } else if (ev->pointer.action == YASL_POINTER_MOVE && ed->mouse_dragging) {
+ } else if (ev->action == YASL_POINTER_MOVE && ed->mouse_dragging) {
int scale = (device_scale >= 2.0 ? 2 : 1) + ed->zoom_offset;
if (scale < 1) scale = 1;
int char_w = 8 * scale;
@@ -1427,27 +1427,26 @@ static void on_event(struct editor *ed, const yasl_event *ev)
yasl_event pointer = {0};
pointer.type = YASL_EVENT_POINTER;
pointer.surface = ev->surface;
- pointer.pointer.x = ev->pen.x;
- pointer.pointer.y = ev->pen.y;
+ pointer.x = ev->x;
+ pointer.y = ev->y;
pointer.pointer.button = YASL_BUTTON_PRIMARY;
- pointer.pointer.mods = 0;
- switch (ev->pen.action) {
- case YASL_PEN_DOWN: pointer.pointer.action = YASL_POINTER_DOWN; break;
- case YASL_PEN_UP: pointer.pointer.action = YASL_POINTER_UP; break;
+ switch (ev->action) {
+ case YASL_PEN_DOWN: pointer.action = YASL_POINTER_DOWN; break;
+ case YASL_PEN_UP: pointer.action = YASL_POINTER_UP; break;
case YASL_PEN_LEAVE:
- case YASL_PEN_CANCEL: pointer.pointer.action = YASL_POINTER_LEAVE; break;
- default: pointer.pointer.action = YASL_POINTER_MOVE; break;
+ case YASL_PEN_CANCEL: pointer.action = YASL_POINTER_LEAVE; break;
+ default: pointer.action = YASL_POINTER_MOVE; break;
}
on_event(ed, &pointer);
break;
}
case YASL_EVENT_SCROLL:
- if (ev->scroll.mods & (YASL_MOD_CTRL | YASL_MOD_SUPER)) {
+ if (ev->mods & (YASL_MOD_CTRL | YASL_MOD_SUPER)) {
/* Ctrl + Wheel = Zoom */
if (ev->scroll.lines_y > 0 && ed->zoom_offset < 2) ed->zoom_offset++;
else if (ev->scroll.lines_y < 0 && ed->zoom_offset > -1) ed->zoom_offset--;
- } else if (ev->scroll.mods & YASL_MOD_SHIFT) {
+ } else if (ev->mods & YASL_MOD_SHIFT) {
/* Shift + Wheel = Horizontal scroll */
ed->scroll_x -= (int)(ev->scroll.lines_y * 3.0);
if (ed->scroll_x < 0) ed->scroll_x = 0;
@@ -1483,13 +1482,11 @@ static void on_event(struct editor *ed, const yasl_event *ev)
static void editor_render(struct editor *ed)
{
- struct yasl_cpu_buffer buffer;
- int acquired = yasl_cpu_acquire(ed->surface, &buffer);
- int w = buffer.width, h = buffer.height;
- uint32_t *pix = acquired == 0 ? buffer.pixels : NULL;
+ int w, h;
+ uint32_t *pix = yasl_cpu_pixels(ed->surface, &w, &h);
if (!pix) return;
- double display_scale = yasl_surface_get_scale(ed->surface);
+ double display_scale = yasl_surface_get_info(ed->surface).scale;
int scale = (display_scale >= 2.0 ? 2 : 1) + ed->zoom_offset;
if (scale < 1) scale = 1;
int char_w = 8 * scale;
@@ -1626,7 +1623,7 @@ static void editor_render(struct editor *ed)
ed->is_modified ? 0xfffab387 : status_accent, scale);
}
- if ((yasl_cpu_present(ed->surface, &buffer) < 0)) {
+ if (yasl_cpu_present(ed->surface) < 0) {
fprintf(stderr, "Could not present CPU image\n");
ed->failed = true;
ed->running = false;
@@ -1699,7 +1696,7 @@ int main(int argc, char **argv)
}
}
- if ((yasl_init(&(struct yasl_options){.app_id="org.yasl.notepad"}) < 0) || !editor_init(&ed)) {
+ if (yasl_init(YASL_BACKEND_AUTO, "org.yasl.notepad") < 0 || !editor_init(&ed)) {
fprintf(stderr, "Could not initialize YASL Notepad\n");
editor_shutdown(&ed);
yasl_shutdown();