Unified · Split
| @@ -7,229 +7,316 @@ · 6 unmodified lines | |||
| 7 | #include <keyboard.h> | 7 | #include <keyboard.h> |
| 8 | #include <thread.h> | 8 | #include <thread.h> |
| 9 | 9 | ||
| 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 | |||
| 10 | static struct { | 17 | static struct { |
| 11 | u8int u[4]; | 18 | u8int u[4]; |
| 12 | int at; | 19 | Usz at; |
| 13 | }noteoff[16*128]; /* 16 channels, 128 notes each */ | 20 | }noteoff[16*128]; /* 16 channels, 128 notes each */ |
| 14 | 21 | ||
| 15 | static char *buf; | 22 | static Rune *linebuf; |
| 16 | 23 | static Usz tick; | |
| 17 | Usz orca_round_up_power2(Usz x) { | 24 | static int gridw = 8, gridh = 8; |
| 18 | assert(x <= SIZE_MAX / 2 + 1); | 25 | static int curx, cury; |
| 19 | x -= 1; | 26 | static Image *curbg; |
| 20 | x |= x >> 1; | 27 | |
| 21 | x |= x >> 2; | 28 | Usz |
| 22 | x |= x >> 4; | 29 | orca_round_up_power2(Usz x) |
| 23 | x |= x >> 8; | 30 | { |
| 24 | x |= x >> 16; | 31 | x -= 1; |
| 25 | #if SIZE_MAX > UINT32_MAX | 32 | x |= x >> 1; |
| 26 | x |= x >> 32; | 33 | x |= x >> 2; |
| 27 | #endif | 34 | x |= x >> 4; |
| 28 | return x + 1; | 35 | x |= x >> 8; |
| 36 | x |= x >> 16; | ||
| 37 | return x + 1; | ||
| 29 | } | 38 | } |
| 30 | 39 | ||
| 31 | bool orca_is_valid_glyph(Glyph c) { | 40 | bool |
| 32 | if (c >= '0' && c <= '9') | 41 | orca_is_valid_glyph(Glyph c) |
| 33 | return true; | 42 | { |
| 34 | if (c >= 'A' && c <= 'Z') | 43 | if (c >= '0' && c <= '9') |
| 35 | return true; | 44 | return true; |
| 36 | if (c >= 'a' && c <= 'z') | 45 | if (c >= 'A' && c <= 'Z') |
| 37 | return true; | 46 | return true; |
| 38 | switch (c) { | 47 | if (c >= 'a' && c <= 'z') |
| 39 | case '!': | 48 | return true; |
| 40 | case '#': | 49 | switch (c) { |
| 41 | case '%': | 50 | case '!': |
| 42 | case '*': | 51 | case '#': |
| 43 | case '.': | 52 | case '%': |
| 44 | case ':': | 53 | case '*': |
| 45 | case ';': | 54 | case '.': |
| 46 | case '=': | 55 | case ':': |
| 47 | case '?': | 56 | case ';': |
| 48 | return true; | 57 | case '=': |
| 49 | } | 58 | case '?': |
| 50 | return false; | 59 | return true; |
| 60 | } | ||
| 61 | return false; | ||
| 51 | } | 62 | } |
| 52 | 63 | ||
| 53 | static void | 64 | 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) | ||
| 55 | { | 143 | { |
| 56 | print("usage: orca [-q] [-t ticks] [-f file]\n"); | 144 | Field_load_error e; |
| 57 | exits("usage"); | 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; | ||
| 58 | } | 152 | } |
| 59 | 153 | ||
| 60 | static void | 154 | static void |
| 61 | process(Oevent_list *el, int tick) | 155 | screensize(int *w, int *h) |
| 62 | { | 156 | { |
| 63 | int i, off; | 157 | *w = (Dx(screen->r) - 2*Txtoff) / stringwidth(font, "X"); |
| 64 | Oevent *e; | 158 | *h = ((Dy(screen->r) - 2*Txtoff) - 3*font->height) / font->height; |
| 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 | } | ||
| 93 | } | 159 | } |
| 94 | 160 | ||
| 95 | static void | 161 | static void |
| 96 | field_draw(Field *f, int tick) | 162 | select(void) |
| 97 | { | 163 | { |
| 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); | ||
| 123 | } | 164 | } |
| 124 | 165 | ||
| 166 | static char *menu3i[] = { | ||
| 167 | "load", | ||
| 168 | "save", | ||
| 169 | nil, | ||
| 170 | }; | ||
| 171 | |||
| 172 | static Menu menu3 = { | ||
| 173 | .item = menu3i, | ||
| 174 | }; | ||
| 175 | |||
| 125 | void | 176 | void |
| 126 | threadmain(int argc, char **argv) | 177 | threadmain(int argc, char **argv) |
| 127 | { | 178 | { |
| 128 | const char *input_file = "/fd/0"; | 179 | Mbuf_reusable mbuf; |
| 129 | int ticks = 1; | 180 | Oevent_list events; |
| 130 | bool print_output = true; | 181 | Field field; |
| 131 | Field field; | 182 | Mousectl *mctl; |
| 132 | Mousectl *mctl; | 183 | Keyboardctl *kctl; |
| 133 | Keyboardctl *kctl; | 184 | Rune key; |
| 134 | Rune key; | 185 | Mouse m; |
| 135 | Mouse m; | 186 | char tmp[256]; |
| 136 | Alt a[] = { | 187 | int oldw, oldh, w, h; |
| 137 | { nil, &m, CHANRCV }, | 188 | Alt a[] = { |
| 138 | { nil, nil, CHANRCV }, | 189 | { nil, &m, CHANRCV }, |
| 139 | { nil, &key, CHANRCV }, | 190 | { nil, nil, CHANRCV }, |
| 140 | { nil, nil, CHANNOBLK}, | 191 | { nil, &key, CHANRCV }, |
| 141 | }; | 192 | { nil, nil, CHANNOBLK}, |
| 142 | 193 | }; | |
| 143 | ARGBEGIN{ | 194 | |
| 144 | case 't': | 195 | USED(argc, argv); |
| 145 | ticks = atoi(EARGF(usage())); | 196 | |
| 146 | break; | 197 | srand(time(0)); |
| 147 | case 'f': | 198 | threadsetname("orca"); |
| 148 | input_file = EARGF(usage()); | 199 | |
| 149 | break; | 200 | if(initdraw(nil, nil, "orca") < 0) |
| 150 | case 'q': | 201 | sysfatal("initdraw: %r"); |
| 151 | print_output = false; | 202 | if ((mctl = initmouse(nil, screen)) == nil) |
| 152 | break; | 203 | sysfatal("initmouse: %r"); |
| 153 | }ARGEND; | 204 | if ((kctl = initkeyboard(nil)) == nil) |
| 154 | 205 | sysfatal("initkeyboard: %r"); | |
| 155 | field_init(&field); | 206 | |
| 156 | Field_load_error fle = field_load_file(input_file, &field); | 207 | a[0].c = mctl->c; |
| 157 | if (fle != Field_load_error_ok) { | 208 | a[1].c = mctl->resizec; |
| 158 | field_deinit(&field); | 209 | a[2].c = kctl->c; |
| 159 | char const *errstr = "Unknown"; | 210 | |
| 160 | switch (fle) { | 211 | curbg = allocimage(display, Rect(0, 0, 1, 1), RGBA32, 1, DYellow); |
| 161 | case Field_load_error_ok: | 212 | |
| 162 | break; | 213 | screensize(&w, &h); |
| 163 | case Field_load_error_cant_open_file: | 214 | field_init_fill(&field, h, w, '.'); |
| 164 | errstr = "Unable to open file"; | 215 | |
| 165 | break; | 216 | linebuf = malloc(sizeof(Rune)*MAX(w+1, 64)); |
| 166 | case Field_load_error_too_many_columns: | 217 | memset(noteoff, 0, sizeof(noteoff)); |
| 167 | errstr = "Grid file has too many columns"; | 218 | mbuf_reusable_init(&mbuf); |
| 168 | break; | 219 | mbuf_reusable_ensure_size(&mbuf, h, w); |
| 169 | case Field_load_error_too_many_rows: | 220 | oevent_list_init(&events); |
| 170 | errstr = "Grid file has too many rows"; | 221 | |
| 171 | break; | 222 | for (tick = 0;; tick++) { |
| 172 | case Field_load_error_no_rows_read: | 223 | mbuffer_clear(mbuf.buffer, h, w); |
| 173 | errstr = "Grid file has no rows"; | 224 | oevent_list_clear(&events); |
| 174 | break; | 225 | orca_run(field.buffer, mbuf.buffer, h, w, tick, &events, 0); |
| 175 | case Field_load_error_not_a_rectangle: | 226 | process(&events); |
| 176 | errstr = "Grid file is not a rectangle"; | 227 | redraw(&field); |
| 177 | break; | 228 | |
| 178 | } | 229 | oldw = w = field.width; |
| 179 | exits(errstr); | 230 | oldh = h = field.height; |
| 180 | } | 231 | |
| 181 | buf = malloc(field.width+1); | 232 | switch (alt(a)) { |
| 182 | memset(noteoff, 0, sizeof(noteoff)); | 233 | case 0: /* mouse */ |
| 183 | Mbuf_reusable mbuf_r; | 234 | switch (m.buttons & 7) { |
| 184 | mbuf_reusable_init(&mbuf_r); | 235 | case 1: |
| 185 | mbuf_reusable_ensure_size(&mbuf_r, field.height, field.width); | 236 | select(); |
| 186 | Oevent_list oevent_list; | 237 | break; |
| 187 | oevent_list_init(&oevent_list); | 238 | case 2: |
| 188 | Usz max_ticks = (Usz)ticks; | 239 | break; |
| 189 | 240 | case 4: | |
| 190 | if(initdraw(nil, nil, "orca") < 0) | 241 | switch (menuhit(3, mctl, &menu3, nil)) { |
| 191 | sysfatal("initdraw: %r"); | 242 | case 0: |
| 192 | if ((mctl = initmouse(nil, screen)) == nil) | 243 | tmp[0] = 0; |
| 193 | sysfatal("initmouse: %r"); | 244 | if (enter("load file:", tmp, sizeof(tmp), mctl, kctl, nil) > 0) { |
| 194 | if ((kctl = initkeyboard(nil)) == nil) | 245 | if (loadfield(&field, tmp) == 0) { |
| 195 | sysfatal("initkeyboard: %r"); | 246 | w = field.width; |
| 196 | 247 | h = field.height; | |
| 197 | a[0].c = mctl->c; | 248 | } else { |
| 198 | a[1].c = mctl->resizec; | 249 | /* FIXME display the error */ |
| 199 | a[2].c = kctl->c; | 250 | } |
| 200 | unlockdisplay(display); | 251 | } |
| 201 | 252 | break; | |
| 202 | srand(time(0)); | 253 | } |
| 203 | threadsetname("orca"); | 254 | break; |
| 204 | 255 | } | |
| 205 | for (Usz i = 0; i < max_ticks; ++i) { | 256 | break; |
| 206 | mbuffer_clear(mbuf_r.buffer, field.height, field.width); | 257 | |
| 207 | oevent_list_clear(&oevent_list); | 258 | case 1: /* resize */ |
| 208 | orca_run(field.buffer, mbuf_r.buffer, field.height, field.width, i, | 259 | getwindow(display, Refnone); |
| 209 | &oevent_list, 0); | 260 | break; |
| 210 | process(&oevent_list, i); | 261 | |
| 211 | field_draw(&field, i); | 262 | case 2: /* keyboard */ |
| 212 | 263 | switch (key) { | |
| 213 | switch (alt(a)) { | 264 | case Kup: |
| 214 | case 0: /* mouse */ | 265 | cury = MAX(0, cury-1); |
| 215 | break; | 266 | break; |
| 216 | case 1: /* resize */ | 267 | case Kdown: |
| 217 | getwindow(display, Refnone); | 268 | cury = MIN(h-1, cury+1); |
| 218 | break; | 269 | break; |
| 219 | case 2: /* keyboard */ | 270 | case Kleft: |
| 220 | switch (key) { | 271 | curx = MAX(0, curx-1); |
| 221 | case Kdel: | 272 | break; |
| 222 | goto end; | 273 | case Kright: |
| 223 | } | 274 | curx = MIN(w-1, curx+1); |
| 224 | } | 275 | break; |
| 225 | } | 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 | } | ||
| 226 | 315 | ||
| 227 | end: | 316 | end: |
| 228 | mbuf_reusable_deinit(&mbuf_r); | 317 | mbuf_reusable_deinit(&mbuf); |
| 229 | oevent_list_deinit(&oevent_list); | 318 | oevent_list_deinit(&events); |
| 230 | if (print_output) | 319 | field_deinit(&field); |
| 231 | field_fput(&field, stderr); | ||
| 232 | field_deinit(&field); | ||
| 233 | 320 | ||
| 234 | threadexitsall(nil); | 321 | threadexitsall(nil); |
| 235 | } | 322 | } |