summaryrefslogtreecommitdiff
path: root/addons/fake/fake.c
blob: 648dcacee4938a56f926dcec57ed1893ba327c48 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/* 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. It paints
 * which view it is painted for and that view's size as `view` told 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];
  uint16_t cols[8], rows[8]; /* each view's, by id */
} 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 void tick(void *st, pa_tick *t) {
  fake *f = st;
  f->ticks++;
  t->next = f->ticks < 3 ? t->now + 100000000u : 0; /* ten a second, three times */
  memcpy(t->prefix, "fake", 4), t->prefix_len = 4;
}

static void view(void *st, const pa_view *v) {
  fake *f = st;
  if (v->id == 0 || v->id > 8) return;
  f->cols[v->id - 1] = v->cols, f->rows[v->id - 1] = v->rows;
}

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);
  if (f && w->view >= 1 && w->view <= 8) {
    char v[48] = "view ";
    v[5] = (char)('0' + w->view), v[6] = ' ';
    num(v + 7, f->cols[w->view - 1]);
    char *e = v + strlen(v);
    *e++ = 'x';
    num(e, f->rows[w->view - 1]);
    if (w->glyphs) memcpy(v + strlen(v), " glyphs", 8);
    pa_put(w, 3, 0, v, PA_DEFAULT, PA_DEFAULT, 0);
  }
  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') { /* FakeJob cancel: its answer dropped */
    uint32_t token = api->job("abc", 3);
    if (token && n > 8) api->cancel(token);
    return token ? 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;
}

/* `/`: a row naming the count, at once. */
static void search(void *st, const char *pat, uint32_t n) {
  fake *f = st;
  char row[64] = "fake:1 count ";
  num(row + 13, f->count);
  size_t k = strlen(row);
  row[k++] = ' ';
  for (uint32_t i = 0; i < n && k < sizeof row - 1; i++) row[k++] = pat[i];
  row[k++] = '\n';
  api->found(row, (uint32_t)k);
}

#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_OPTIONAL, 0, "count a job's answer (cancel: drop it)"),
};

const pa_ops pardes_addon = {
  .head = PA_ADDON(FAKE_NAME, 1, PA_PICTURES, ".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, .search = search, .view = view,
#ifndef FAKE_NO_DUMP
  .dump = dump, .load = load,
#endif
};