/* fake.c: the template addon, every function of pardes_addon.h in a few * lines, and what the loader's tests drive (test/behaviour/addons.zig, * `pardes --addon-test`). * * cc -shared -fPIC -I src/kinds -o fake.so addons/fake/fake.c * * A pane of it counts: keys and clicks and ticks and words each add to a * count it paints, dumps and answers `where` with; `:N` sets it. */ #include "pardes_addon.h" /* The loader's tests build it wrong on purpose: another ABI, no export, * a word that is the core's; and once with no record of its own. */ #ifndef FAKE_NAME #define FAKE_NAME "fake" #endif #ifndef FAKE_WORD #define FAKE_WORD "Fake" #endif #ifdef FAKE_NO_EXPORT #define pardes_addon fake_not_exported #endif static const pa_api *api; typedef struct { unsigned count, ticks; char last[16]; uint8_t pixels[2 * 2 * 4]; } fake; static int init(const pa_api *a) { api = a; return PA_OK; } static void *open(const char *path, const uint8_t *bytes, uint64_t len) { fake *f = api->alloc(sizeof *f); if (!f) return 0; uint64_t seed = api->seed(); for (int i = 0; i < 16; i++) f->pixels[i] = (uint8_t)(i * 16 + (seed >> (i % 8)) % 2); f->count = bytes ? (unsigned)len : 0; (void)path; return f; } static void close(void *st) { (void)st; } static uint64_t tick(void *st, uint64_t now, uint32_t flags) { fake *f = st; (void)flags; f->ticks++; return f->ticks < 3 ? now + 100000000u : 0; /* ten a second, three times */ } static void num(char *out, unsigned n) { char t[12]; int k = 0; do t[k++] = (char)('0' + n % 10); while (n /= 10); while (k) *out++ = t[--k]; *out = 0; } static void paint(void *st, const pa_window *w, pa_paint *out) { fake *f = st; char line[48] = "count "; num(line + 6, f ? f->count : 0); pa_put(w, 0, 0, line, PA_INDEX(2), PA_DEFAULT, 0); if (f && f->last[0]) pa_put(w, 1, 0, f->last, PA_DEFAULT, PA_DEFAULT, 0); const char *p = "fake"; for (out->prefix_len = 0; p[out->prefix_len]; out->prefix_len++) out->prefix[out->prefix_len] = p[out->prefix_len]; if (f && out->cap > 0 && w->rows > 2) { pa_placement *pl = &out->placements[out->n++]; *pl = (pa_placement){.rgba = f->pixels, .w = 2, .h = 2, .src = {0, 0, 2, 2}, .dst = {0, 2, 2, 1}, .key = f->count}; } } static int key(void *st, const pa_key *k) { fake *f = st; if (k->cp == 'q' || k->cp == 0x1b) return 0; /* not taken: the core's (Esc leaves) */ f->count++; f->last[0] = 'k'; f->last[1] = k->cp < 128 ? (char)k->cp : '?'; f->last[2] = 0; return 1; } static int mouse(void *st, const pa_mouse *m) { fake *f = st; if (m->action != PA_PRESS) return 1; f->count++; f->last[0] = 'm'; num(f->last + 1, m->col); return 1; } static uint64_t body_len(void *st) { (void)st; return 6; } static uint32_t body(void *st, uint64_t off, char *buf, uint32_t n) { (void)st; return pa_copy("fake\n\n", 6, off, buf, n); } static int address(void *st, const char *text, uint32_t n) { fake *f = st; unsigned v = 0; uint32_t i = text[0] == ':' ? 1 : 0; if (i >= n) return 0; for (; i < n; i++) { if (text[i] < '0' || text[i] > '9') return 0; v = v * 10 + (unsigned)(text[i] - '0'); } f->count = v; return 1; } static uint32_t where(void *st, char *buf, uint32_t n) { fake *f = st; char t[12]; num(t, f->count); uint32_t k = 0; while (t[k] && k < n) { buf[k] = t[k]; k++; } return k; } static int ctl(void *st, const char *line, uint32_t n, char *reply, uint32_t cap) { fake *f = st; /* FakeBump [n]: adds n (1); FakeJob: counts the bytes a job answers. */ if (n >= 8 && line[4] == 'B') { unsigned by = 0; for (uint32_t i = 9; i < n && line[i] >= '0' && line[i] <= '9'; i++) by = by * 10 + (unsigned)(line[i] - '0'); f->count += by ? by : 1; return PA_OK; } if (n >= 7 && line[4] == 'J') return api->job("abc", 3) ? PA_OK : 1; const char *no = "no such fake word"; uint32_t k = 0; while (no[k] && k + 1 < cap) { reply[k] = no[k]; k++; } reply[k] = 0; return 1; } static uint32_t ctl_read(void *st, char *buf, uint32_t n) { fake *f = st; char t[24] = "count:"; num(t + 6, f->count); uint32_t k = 0; while (t[k] && k < n) { buf[k] = t[k]; k++; } return k; } static uint32_t job_run(const uint8_t *in, uint32_t n, pa_out *out) { uint8_t *o = out->alloc(out, n * 2); if (!o) return 0; for (uint32_t i = 0; i < n; i++) o[i] = o[n + i] = in[i]; return n * 2; } static void job_done(void *st, uint32_t token, const uint8_t *out, uint32_t n) { fake *f = st; (void)token; (void)out; f->count += n; } #ifndef FAKE_NO_DUMP static uint32_t dump(void *st, uint8_t *buf, uint32_t n) { fake *f = st; if (n >= 4) for (int i = 0; i < 4; i++) buf[i] = (uint8_t)(f->count >> (8 * i)); return 4; } static int load(void *st, const uint8_t *buf, uint32_t n, uint32_t version) { fake *f = st; if (n != 4 || version > 1) return 1; f->count = (unsigned)buf[0] | (unsigned)buf[1] << 8 | (unsigned)buf[2] << 16 | (unsigned)buf[3] << 24; return PA_OK; } #endif static const pa_word words[] = { PA_WORD(FAKE_WORD, "zf", PA_ARG_NONE, 1, "a fake pane, counting"), PA_WORD("FakeBump", 0, PA_ARG_OPTIONAL, 0, "add to the count"), PA_WORD("FakeJob", 0, PA_ARG_NONE, 0, "count a job's answer"), }; const pa_ops pardes_addon = { .head = PA_ADDON(FAKE_NAME, 1, ".fake", words, 3), #ifdef FAKE_ABI .head.abi = FAKE_ABI, #endif .init = init, .open = open, .close = close, .tick = tick, .paint = paint, .key = key, .mouse = mouse, .body_len = body_len, .body = body, .address = address, .where = where, .ctl = ctl, .ctl_read = ctl_read, .job_run = job_run, .job_done = job_done, #ifndef FAKE_NO_DUMP .dump = dump, .load = load, #endif };