History

plan9: field resizing, loading files, rudimentary editing

Sigrid Haflínudóttir committed 6 years ago

Commit f274c921 parent dd470b11 file · +287 −200Browse files

Unified · Split

plan9.c+287−200
@@ -7,229 +7,316 @@ · 6 unmodified lines
77 #include <keyboard.h>
88 #include <thread.h>
99
10+#define MIN(x,y) ((x)<(y)?(x):(y))
11+#define MAX(x,y) ((x)>(y)?(x):(y))
12+
13+enum {
14+ Txtoff = 16,
15+};
16+
1017 static struct {
11− u8int u[4];
12− int at;
18+ u8int u[4];
19+ Usz at;
1320 }noteoff[16*128]; /* 16 channels, 128 notes each */
1421
15−static char *buf;
16−
17−Usz orca_round_up_power2(Usz x) {
18− assert(x <= SIZE_MAX / 2 + 1);
19− x -= 1;
20− x |= x >> 1;
21− x |= x >> 2;
22− x |= x >> 4;
23− x |= x >> 8;
24− x |= x >> 16;
25−#if SIZE_MAX > UINT32_MAX
26− x |= x >> 32;
27−#endif
28− return x + 1;
22+static Rune *linebuf;
23+static Usz tick;
24+static int gridw = 8, gridh = 8;
25+static int curx, cury;
26+static Image *curbg;
27+
28+Usz
29+orca_round_up_power2(Usz x)
30+{
31+ x -= 1;
32+ x |= x >> 1;
33+ x |= x >> 2;
34+ x |= x >> 4;
35+ x |= x >> 8;
36+ x |= x >> 16;
37+ return x + 1;
2938 }
3039
31−bool orca_is_valid_glyph(Glyph c) {
32− if (c >= '0' && c <= '9')
33− return true;
34− if (c >= 'A' && c <= 'Z')
35− return true;
36− if (c >= 'a' && c <= 'z')
37− return true;
38− switch (c) {
39− case '!':
40− case '#':
41− case '%':
42− case '*':
43− case '.':
44− case ':':
45− case ';':
46− case '=':
47− case '?':
48− return true;
49− }
50− return false;
40+bool
41+orca_is_valid_glyph(Glyph c)
42+{
43+ if (c >= '0' && c <= '9')
44+ return true;
45+ if (c >= 'A' && c <= 'Z')
46+ return true;
47+ if (c >= 'a' && c <= 'z')
48+ return true;
49+ switch (c) {
50+ case '!':
51+ case '#':
52+ case '%':
53+ case '*':
54+ case '.':
55+ case ':':
56+ case ';':
57+ case '=':
58+ case '?':
59+ return true;
60+ }
61+ return false;
5162 }
5263
5364 static void
54−usage(void)
65+process(Oevent_list *events)
66+{
67+ int i, off;
68+ Oevent *e;
69+ u8int u[4];
70+
71+ for (e = events->buffer, i = 0; i < events->count; i++, e++) {
72+ if (e->any.oevent_type == Oevent_type_midi_note) {
73+ Oevent_midi_note const *n = &e->midi_note;
74+ u[0] = 1;
75+ u[1] = 0x90 | n->channel;
76+ u[2] = (n->octave + 1)*12 + n->note;
77+ u[3] = n->velocity;
78+ write(1, u, 4);
79+
80+ off = n->channel*128 + u[2];
81+ noteoff[off].u[1] = 0x80 | n->channel;
82+ noteoff[off].u[2] = u[2];
83+ noteoff[off].u[3] = 0;
84+ noteoff[off].at = tick + n->duration;
85+ }
86+ }
87+
88+ sleep(150);
89+
90+ for (i = 0; i < nelem(noteoff); i++) {
91+ if (noteoff[i].at > 0 && noteoff[i].at < tick) {
92+ write(1, noteoff[i].u, 4);
93+ noteoff[i].at = 0;
94+ }
95+ }
96+}
97+
98+static void
99+redraw(Field *f)
100+{
101+ Point p, top;
102+ Rune cursor;
103+ int x, y;
104+
105+ draw(screen, screen->r, display->black, nil, ZP);
106+ p = screen->r.min;
107+ p.x += Txtoff;
108+ p.y += Txtoff;
109+ top = p;
110+ for (y = 0; y < f->height; y++) {
111+ for (x = 0; x < f->width; x++) {
112+ Rune c = f->buffer[f->width*y + x];
113+ if (c == L'.')
114+ c = L'·';
115+ linebuf[x] = c;
116+ if (y == cury && x == curx)
117+ cursor = c;
118+ }
119+ linebuf[x] = 0;
120+ runestring(screen, p, display->white, ZP, font, linebuf);
121+ p.y += font->height;
122+ }
123+
124+ p.y += 2 * font->height;
125+ p.x = screen->r.min.x + Txtoff;
126+ runesprint(linebuf, "%udx%ud", f->width, f->height);
127+ runestring(screen, p, display->white, ZP, font, linebuf);
128+ p.x += stringwidth(font, "99999x99999 ");
129+ runesprint(linebuf, "%ludf", tick);
130+ runestring(screen, p, display->white, ZP, font, linebuf);
131+
132+ /* cursor */
133+ p = top;
134+ p.x += curx*stringwidth(font, " ");
135+ p.y += cury*font->height;
136+ runestringnbgop(screen, p, display->black, ZP, font, &cursor, 1, curbg, ZP, SoverD);
137+
138+ flushimage(display, 1);
139+}
140+
141+static int
142+loadfield(Field *field, char *path)
55143 {
56− print("usage: orca [-q] [-t ticks] [-f file]\n");
57− exits("usage");
144+ Field_load_error e;
145+
146+ if ((e = field_load_file(path, field)) != Field_load_error_ok) {
147+ werrstr(field_load_error_string(e));
148+ return -1;
149+ }
150+
151+ return 0;
58152 }
59153
60154 static void
61−process(Oevent_list *el, int tick)
155+screensize(int *w, int *h)
62156 {
63− int i, off;
64− Oevent *e;
65− u8int u[4];
66−
67− for (i = 0; i < el->count; i++) {
68− e = &el->buffer[i];
69− if (e->any.oevent_type == Oevent_type_midi_note) {
70− Oevent_midi_note const *n = &e->midi_note;
71− u[0] = 1;
72− u[1] = 0x90 | n->channel;
73− u[2] = (n->octave + 1)*12 + n->note;
74− u[3] = n->velocity;
75− write(1, u, 4);
76−
77− off = n->channel*128 + u[2];
78− noteoff[off].u[1] = 0x80 | n->channel;
79− noteoff[off].u[2] = u[2];
80− noteoff[off].u[3] = 0;//u[3];
81− noteoff[off].at = tick + n->duration;
82− }
83− }
84−
85− sleep(150);
86−
87− for (i = 0; i < nelem(noteoff); i++) {
88− if (noteoff[i].at > 0 && noteoff[i].at < tick) {
89− write(1, noteoff[i].u, 4);
90− noteoff[i].at = 0;
91− }
92− }
157+ *w = (Dx(screen->r) - 2*Txtoff) / stringwidth(font, "X");
158+ *h = ((Dy(screen->r) - 2*Txtoff) - 3*font->height) / font->height;
93159 }
94160
95161 static void
96−field_draw(Field *f, int tick)
162+select(void)
97163 {
98− Point p;
99− int x, y;
100−
101− lockdisplay(display);
102− draw(screen, screen->r, display->black, nil, ZP);
103− p = screen->r.min;
104− p.x += 8;
105− p.y += 8;
106− for (y = 0; y < f->height; y++) {
107− for (x = 0; x < f->width; x++) {
108− u8int c = f->buffer[f->width*y + x];
109− buf[x] = (c > '\n' && c < 128) ? c : '?';
110− }
111− buf[x] = 0;
112− string(screen, p, display->white, ZP, display->defaultfont, buf);
113− p.y += display->defaultfont->height;
114− }
115−
116− p.x = screen->r.min.x + 8;
117− p.y += 2 * display->defaultfont->height;
118− sprint(buf, "%df", tick);
119− string(screen, p, display->white, ZP, display->defaultfont, buf);
120−
121− flushimage(display, 1);
122− unlockdisplay(display);
123164 }
124165
166+static char *menu3i[] = {
167+ "load",
168+ "save",
169+ nil,
170+};
171+
172+static Menu menu3 = {
173+ .item = menu3i,
174+};
175+
125176 void
126177 threadmain(int argc, char **argv)
127178 {
128− const char *input_file = "/fd/0";
129− int ticks = 1;
130− bool print_output = true;
131− Field field;
132− Mousectl *mctl;
133− Keyboardctl *kctl;
134− Rune key;
135− Mouse m;
136− Alt a[] = {
137− { nil, &m, CHANRCV },
138− { nil, nil, CHANRCV },
139− { nil, &key, CHANRCV },
140− { nil, nil, CHANNOBLK},
141− };
142−
143− ARGBEGIN{
144− case 't':
145− ticks = atoi(EARGF(usage()));
146− break;
147− case 'f':
148− input_file = EARGF(usage());
149− break;
150− case 'q':
151− print_output = false;
152− break;
153− }ARGEND;
154−
155− field_init(&field);
156− Field_load_error fle = field_load_file(input_file, &field);
157− if (fle != Field_load_error_ok) {
158− field_deinit(&field);
159− char const *errstr = "Unknown";
160− switch (fle) {
161− case Field_load_error_ok:
162− break;
163− case Field_load_error_cant_open_file:
164− errstr = "Unable to open file";
165− break;
166− case Field_load_error_too_many_columns:
167− errstr = "Grid file has too many columns";
168− break;
169− case Field_load_error_too_many_rows:
170− errstr = "Grid file has too many rows";
171− break;
172− case Field_load_error_no_rows_read:
173− errstr = "Grid file has no rows";
174− break;
175− case Field_load_error_not_a_rectangle:
176− errstr = "Grid file is not a rectangle";
177− break;
178− }
179− exits(errstr);
180− }
181− buf = malloc(field.width+1);
182− memset(noteoff, 0, sizeof(noteoff));
183− Mbuf_reusable mbuf_r;
184− mbuf_reusable_init(&mbuf_r);
185− mbuf_reusable_ensure_size(&mbuf_r, field.height, field.width);
186− Oevent_list oevent_list;
187− oevent_list_init(&oevent_list);
188− Usz max_ticks = (Usz)ticks;
189−
190− if(initdraw(nil, nil, "orca") < 0)
191− sysfatal("initdraw: %r");
192− if ((mctl = initmouse(nil, screen)) == nil)
193− sysfatal("initmouse: %r");
194− if ((kctl = initkeyboard(nil)) == nil)
195− sysfatal("initkeyboard: %r");
196−
197− a[0].c = mctl->c;
198− a[1].c = mctl->resizec;
199− a[2].c = kctl->c;
200− unlockdisplay(display);
201−
202− srand(time(0));
203− threadsetname("orca");
204−
205− for (Usz i = 0; i < max_ticks; ++i) {
206− mbuffer_clear(mbuf_r.buffer, field.height, field.width);
207− oevent_list_clear(&oevent_list);
208− orca_run(field.buffer, mbuf_r.buffer, field.height, field.width, i,
209− &oevent_list, 0);
210− process(&oevent_list, i);
211− field_draw(&field, i);
212−
213− switch (alt(a)) {
214− case 0: /* mouse */
215− break;
216− case 1: /* resize */
217− getwindow(display, Refnone);
218− break;
219− case 2: /* keyboard */
220− switch (key) {
221− case Kdel:
222− goto end;
223− }
224− }
225− }
179+ Mbuf_reusable mbuf;
180+ Oevent_list events;
181+ Field field;
182+ Mousectl *mctl;
183+ Keyboardctl *kctl;
184+ Rune key;
185+ Mouse m;
186+ char tmp[256];
187+ int oldw, oldh, w, h;
188+ Alt a[] = {
189+ { nil, &m, CHANRCV },
190+ { nil, nil, CHANRCV },
191+ { nil, &key, CHANRCV },
192+ { nil, nil, CHANNOBLK},
193+ };
194+
195+ USED(argc, argv);
196+
197+ srand(time(0));
198+ threadsetname("orca");
199+
200+ if(initdraw(nil, nil, "orca") < 0)
201+ sysfatal("initdraw: %r");
202+ if ((mctl = initmouse(nil, screen)) == nil)
203+ sysfatal("initmouse: %r");
204+ if ((kctl = initkeyboard(nil)) == nil)
205+ sysfatal("initkeyboard: %r");
206+
207+ a[0].c = mctl->c;
208+ a[1].c = mctl->resizec;
209+ a[2].c = kctl->c;
210+
211+ curbg = allocimage(display, Rect(0, 0, 1, 1), RGBA32, 1, DYellow);
212+
213+ screensize(&w, &h);
214+ field_init_fill(&field, h, w, '.');
215+
216+ linebuf = malloc(sizeof(Rune)*MAX(w+1, 64));
217+ memset(noteoff, 0, sizeof(noteoff));
218+ mbuf_reusable_init(&mbuf);
219+ mbuf_reusable_ensure_size(&mbuf, h, w);
220+ oevent_list_init(&events);
221+
222+ for (tick = 0;; tick++) {
223+ mbuffer_clear(mbuf.buffer, h, w);
224+ oevent_list_clear(&events);
225+ orca_run(field.buffer, mbuf.buffer, h, w, tick, &events, 0);
226+ process(&events);
227+ redraw(&field);
228+
229+ oldw = w = field.width;
230+ oldh = h = field.height;
231+
232+ switch (alt(a)) {
233+ case 0: /* mouse */
234+ switch (m.buttons & 7) {
235+ case 1:
236+ select();
237+ break;
238+ case 2:
239+ break;
240+ case 4:
241+ switch (menuhit(3, mctl, &menu3, nil)) {
242+ case 0:
243+ tmp[0] = 0;
244+ if (enter("load file:", tmp, sizeof(tmp), mctl, kctl, nil) > 0) {
245+ if (loadfield(&field, tmp) == 0) {
246+ w = field.width;
247+ h = field.height;
248+ } else {
249+ /* FIXME display the error */
250+ }
251+ }
252+ break;
253+ }
254+ break;
255+ }
256+ break;
257+
258+ case 1: /* resize */
259+ getwindow(display, Refnone);
260+ break;
261+
262+ case 2: /* keyboard */
263+ switch (key) {
264+ case Kup:
265+ cury = MAX(0, cury-1);
266+ break;
267+ case Kdown:
268+ cury = MIN(h-1, cury+1);
269+ break;
270+ case Kleft:
271+ curx = MAX(0, curx-1);
272+ break;
273+ case Kright:
274+ curx = MIN(w-1, curx+1);
275+ break;
276+ case 0x11: /* C-q */
277+ goto end;
278+ case 0x12: /* C-r */
279+ tick = -1;
280+ break;
281+ case '(':
282+ w = MAX(1, w-gridw);
283+ break;
284+ case ')':
285+ w += gridw;
286+ break;
287+ case '_':
288+ h = MAX(1, h-gridh);
289+ break;
290+ case '+':
291+ h += gridh;
292+ break;
293+ default:
294+ if (key == Kdel)
295+ key = '.';
296+ if (orca_is_valid_glyph(key))
297+ field.buffer[curx + w*cury] = key;
298+ else
299+ fprint(2, "unhandled key %04x\n", key);
300+ break;
301+ }
302+
303+ if (w != oldw || h != oldh) {
304+ field_resize_raw(&field, h, w);
305+ if (w >= oldw && h >= oldh)
306+ memset(field.buffer + oldw*oldh, '.', w*h - oldw*oldh);
307+ }
308+ }
309+
310+ if (w != oldw || h != oldh) {
311+ mbuf_reusable_ensure_size(&mbuf, h, w);
312+ linebuf = realloc(linebuf, sizeof(Rune)*MAX(w+1, 64));
313+ }
314+ }
226315
227316 end:
228− mbuf_reusable_deinit(&mbuf_r);
229− oevent_list_deinit(&oevent_list);
230− if (print_output)
231− field_fput(&field, stderr);
232− field_deinit(&field);
317+ mbuf_reusable_deinit(&mbuf);
318+ oevent_list_deinit(&events);
319+ field_deinit(&field);
233320
234− threadexitsall(nil);
321+ threadexitsall(nil);
235322 }

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t