Collaborative ASCII art https://ascii.town
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

client.c 19KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733
  1. /* Copyright (C) 2018 Curtis McEnroe <june@causal.agency>
  2. *
  3. * This program is free software: you can redistribute it and/or modify
  4. * it under the terms of the GNU Affero General Public License as published by
  5. * the Free Software Foundation, either version 3 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU Affero General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU Affero General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #define _XOPEN_SOURCE_EXTENDED
  17. #include <assert.h>
  18. #include <curses.h>
  19. #include <err.h>
  20. #include <errno.h>
  21. #include <locale.h>
  22. #include <poll.h>
  23. #include <stdbool.h>
  24. #include <stdint.h>
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #include <string.h>
  28. #include <sys/socket.h>
  29. #include <sys/un.h>
  30. #include <sysexits.h>
  31. #include <unistd.h>
  32. #include <wchar.h>
  33. #ifdef __FreeBSD__
  34. #include <sys/capsicum.h>
  35. #endif
  36. #include "torus.h"
  37. #include "help.h"
  38. #define err(...) do { endwin(); err(__VA_ARGS__); } while(0)
  39. #define errx(...) do { endwin(); errx(__VA_ARGS__); } while (0)
  40. #define DIV_ROUND(a, b) (((a) + (b) / 2) / (b))
  41. #define CTRL(ch) ((ch) ^ 0x40)
  42. enum {
  43. Esc = 0x1B,
  44. Del = 0x7F,
  45. };
  46. static uint32_t log2(uint32_t n) {
  47. assert(n > 0);
  48. return 32 - __builtin_clz(n) - 1;
  49. }
  50. static void curse(void) {
  51. setlocale(LC_CTYPE, "");
  52. initscr();
  53. start_color();
  54. if (!has_colors() || COLOR_PAIRS < 64) {
  55. endwin();
  56. fprintf(stderr, "Sorry, your terminal doesn't support colors!\n");
  57. fprintf(stderr, "If you think it should, check TERM.\n");
  58. exit(EX_CONFIG);
  59. }
  60. if (LINES < CellRows || COLS < CellCols) {
  61. endwin();
  62. fprintf(
  63. stderr,
  64. "Sorry, your terminal is too small!\n"
  65. "It must be at least %ux%u characters.\n",
  66. CellCols, CellRows
  67. );
  68. exit(EX_CONFIG);
  69. }
  70. assume_default_colors(0, 0);
  71. if (COLORS >= 16) {
  72. for (short pair = 1; pair < 0x80; ++pair) {
  73. init_pair(pair, pair & 0x0F, (pair & 0xF0) >> 4);
  74. }
  75. } else {
  76. for (short pair = 1; pair < 0100; ++pair) {
  77. init_pair(pair, pair & 007, (pair & 070) >> 3);
  78. }
  79. }
  80. color_set(ColorWhite, NULL);
  81. bool hline = (LINES > CellRows);
  82. bool vline = (COLS > CellCols);
  83. if (hline) mvhline(CellRows, 0, 0, CellCols);
  84. if (vline) mvvline(0, CellCols, 0, CellRows);
  85. if (hline && vline) mvaddch(CellRows, CellCols, ACS_LRCORNER);
  86. color_set(0, NULL);
  87. cbreak();
  88. noecho();
  89. keypad(stdscr, true);
  90. set_escdelay(100);
  91. }
  92. static attr_t colorAttr(uint8_t color) {
  93. if (COLORS >= 16) return A_NORMAL;
  94. return (color & ColorBright) ? A_BOLD : A_NORMAL;
  95. }
  96. static short colorPair(uint8_t color) {
  97. if (COLORS >= 16) return color;
  98. return (color & 0x70) >> 1 | (color & 0x07);
  99. }
  100. static void drawCell(
  101. const struct Tile *tile, uint8_t cellX, uint8_t cellY, attr_t attr
  102. ) {
  103. uint8_t color = tile->colors[cellY][cellX];
  104. uint8_t cell = tile->cells[cellY][cellX];
  105. cchar_t cch;
  106. wchar_t wch[] = { CP437[cell], L'\0' };
  107. setcchar(&cch, wch, attr | colorAttr(color), colorPair(color), NULL);
  108. mvadd_wch(cellY, cellX, &cch);
  109. }
  110. static void drawTile(const struct Tile *tile) {
  111. for (uint8_t cellY = 0; cellY < CellRows; ++cellY) {
  112. for (uint8_t cellX = 0; cellX < CellCols; ++cellX) {
  113. drawCell(tile, cellX, cellY, A_NORMAL);
  114. }
  115. }
  116. }
  117. static int client;
  118. static uint8_t cellX;
  119. static uint8_t cellY;
  120. static struct Tile tile;
  121. static void serverTile(void) {
  122. ssize_t size = recv(client, &tile, sizeof(tile), 0);
  123. if (size < 0) err(EX_IOERR, "recv");
  124. if ((size_t)size < sizeof(tile)) errx(EX_PROTOCOL, "truncated tile");
  125. drawTile(&tile);
  126. }
  127. static void serverMove(struct ServerMessage msg) {
  128. cellX = msg.move.cellX;
  129. cellY = msg.move.cellY;
  130. }
  131. static void serverPut(struct ServerMessage msg) {
  132. tile.colors[msg.put.cellY][msg.put.cellX] = msg.put.color;
  133. tile.cells[msg.put.cellY][msg.put.cellX] = msg.put.cell;
  134. drawCell(&tile, msg.put.cellX, msg.put.cellY, A_NORMAL);
  135. }
  136. static void serverCursor(struct ServerMessage msg) {
  137. if (msg.cursor.oldCellX != CursorNone) {
  138. drawCell(&tile, msg.cursor.oldCellX, msg.cursor.oldCellY, A_NORMAL);
  139. }
  140. if (msg.cursor.newCellX != CursorNone) {
  141. drawCell(&tile, msg.cursor.newCellX, msg.cursor.newCellY, A_REVERSE);
  142. }
  143. }
  144. static const uint8_t MapX = (CellCols / 2) - (3 * MapCols / 2);
  145. static const uint8_t MapY = (CellRows / 2) - (MapRows / 2);
  146. static const wchar_t MapCells[5] = L" ░▒▓█";
  147. static const uint8_t MapColors[] = {
  148. ColorBlue, ColorCyan, ColorGreen, ColorYellow, ColorRed,
  149. };
  150. static void serverMap(void) {
  151. int t = MapY - 1;
  152. int l = MapX - 1;
  153. int b = MapY + MapRows;
  154. int r = MapX + 3 * MapCols;
  155. color_set(colorPair(ColorWhite), NULL);
  156. mvhline(t, MapX, ACS_HLINE, 3 * MapCols);
  157. mvhline(b, MapX, ACS_HLINE, 3 * MapCols);
  158. mvvline(MapY, l, ACS_VLINE, MapRows);
  159. mvvline(MapY, r, ACS_VLINE, MapRows);
  160. mvaddch(t, l, ACS_ULCORNER);
  161. mvaddch(t, r, ACS_URCORNER);
  162. mvaddch(b, l, ACS_LLCORNER);
  163. mvaddch(b, r, ACS_LRCORNER);
  164. color_set(0, NULL);
  165. struct Map map;
  166. ssize_t size = recv(client, &map, sizeof(map), 0);
  167. if (size < 0) err(EX_IOERR, "recv");
  168. if ((size_t)size < sizeof(map)) errx(EX_PROTOCOL, "truncated map");
  169. if (0 == map.max.modifyCount) return;
  170. if (0 == map.now - map.min.createTime) return;
  171. for (uint8_t y = 0; y < MapRows; ++y) {
  172. for (uint8_t x = 0; x < MapCols; ++x) {
  173. struct Meta meta = map.meta[y][x];
  174. uint32_t count = 0;
  175. if (meta.modifyCount && log2(map.max.modifyCount)) {
  176. count = DIV_ROUND(
  177. (ARRAY_LEN(MapCells) - 1) * log2(meta.modifyCount),
  178. log2(map.max.modifyCount)
  179. );
  180. }
  181. uint32_t time = 0;
  182. if (meta.modifyTime) {
  183. uint32_t modify = meta.modifyTime - map.min.createTime;
  184. time = DIV_ROUND(
  185. (ARRAY_LEN(MapColors) - 1) * modify,
  186. map.now - map.min.createTime
  187. );
  188. }
  189. wchar_t cell = MapCells[count];
  190. uint8_t color = MapColors[time];
  191. wchar_t tile[] = { cell, cell, cell, L'\0' };
  192. if (y == MapRows / 2 && x == MapCols / 2) tile[1] = L'⌂';
  193. attr_set(colorAttr(color), colorPair(color), NULL);
  194. mvaddwstr(MapY + y, MapX + 3 * x, tile);
  195. }
  196. }
  197. attr_set(A_NORMAL, 0, NULL);
  198. }
  199. static void readMessage(void) {
  200. struct ServerMessage msg;
  201. ssize_t size = recv(client, &msg, sizeof(msg), 0);
  202. if (size < 0) err(EX_IOERR, "recv");
  203. if ((size_t)size < sizeof(msg)) errx(EX_PROTOCOL, "truncated message");
  204. switch (msg.type) {
  205. break; case ServerTile: serverTile();
  206. break; case ServerMove: serverMove(msg);
  207. break; case ServerPut: serverPut(msg);
  208. break; case ServerCursor: serverCursor(msg);
  209. break; case ServerMap: serverMap();
  210. break; default: errx(EX_PROTOCOL, "unknown message type %d", msg.type);
  211. }
  212. move(cellY, cellX);
  213. }
  214. static void clientMessage(struct ClientMessage msg) {
  215. ssize_t size = send(client, &msg, sizeof(msg), 0);
  216. if (size < 0) err(EX_IOERR, "send");
  217. }
  218. static void clientMove(int8_t dx, int8_t dy) {
  219. struct ClientMessage msg = {
  220. .type = ClientMove,
  221. .move = { .dx = dx, .dy = dy },
  222. };
  223. clientMessage(msg);
  224. }
  225. static void clientPut(uint8_t color, uint8_t cell) {
  226. struct ClientMessage msg = {
  227. .type = ClientPut,
  228. .put = { .color = color, .cell = cell },
  229. };
  230. clientMessage(msg);
  231. }
  232. static void clientMap(void) {
  233. struct ClientMessage msg = { .type = ClientMap };
  234. clientMessage(msg);
  235. }
  236. static void clientTele(uint8_t port) {
  237. struct ClientMessage msg = { .type = ClientTele, .port = port };
  238. clientMessage(msg);
  239. }
  240. static struct {
  241. enum {
  242. ModeNormal,
  243. ModeHelp,
  244. ModeMap,
  245. ModeDirection,
  246. ModeInsert,
  247. ModeReplace,
  248. ModeDraw,
  249. ModeLine,
  250. } mode;
  251. int8_t delta;
  252. uint8_t color;
  253. uint8_t shift;
  254. uint8_t draw;
  255. } input = {
  256. .delta = 1,
  257. .color = ColorWhite,
  258. };
  259. static struct {
  260. uint8_t color;
  261. uint8_t cell;
  262. } copy;
  263. static struct {
  264. int8_t dx;
  265. int8_t dy;
  266. uint8_t len;
  267. } insert;
  268. static void modeNormal(void) {
  269. curs_set(1);
  270. move(cellY, cellX);
  271. input.mode = ModeNormal;
  272. }
  273. static void modeHelp(void) {
  274. curs_set(0);
  275. drawTile(Help);
  276. input.mode = ModeHelp;
  277. }
  278. static void modeMap(void) {
  279. curs_set(0);
  280. clientMap();
  281. input.mode = ModeMap;
  282. }
  283. static void modeDirection(void) {
  284. input.mode = ModeDirection;
  285. }
  286. static void modeInsert(int8_t dx, int8_t dy) {
  287. insert.dx = dx;
  288. insert.dy = dy;
  289. insert.len = 0;
  290. input.mode = ModeInsert;
  291. }
  292. static void modeReplace(void) {
  293. input.mode = ModeReplace;
  294. }
  295. static void modeDraw(void) {
  296. input.draw = 0;
  297. input.mode = ModeDraw;
  298. }
  299. static void modeLine(void) {
  300. input.mode = ModeLine;
  301. }
  302. static void colorFg(uint8_t fg) {
  303. input.color = (input.color & 0x78) | (fg & 0x07);
  304. }
  305. static void colorBg(uint8_t bg) {
  306. input.color = (input.color & 0x0F) | (bg & 0x07) << 4;
  307. }
  308. static uint8_t colorInvert(uint8_t color) {
  309. return (color & 0x08)
  310. | (color & 0x70) >> 4
  311. | (color & 0x07) << 4;
  312. }
  313. static void cellCopy(void) {
  314. copy.color = tile.colors[cellY][cellX];
  315. copy.cell = tile.cells[cellY][cellX];
  316. }
  317. static void cellSwap(int8_t dx, int8_t dy) {
  318. if ((uint8_t)(cellX + dx) >= CellCols) return;
  319. if ((uint8_t)(cellY + dy) >= CellRows) return;
  320. uint8_t aColor = tile.colors[cellY][cellX];
  321. uint8_t aCell = tile.cells[cellY][cellX];
  322. uint8_t bColor = tile.colors[cellY + dy][cellX + dx];
  323. uint8_t bCell = tile.cells[cellY + dy][cellX + dx];
  324. clientPut(bColor, bCell);
  325. clientMove(dx, dy);
  326. clientPut(aColor, aCell);
  327. }
  328. static uint8_t inputCell(wchar_t ch) {
  329. if (ch == ' ') return ' ';
  330. if (ch < 0x80) return (uint8_t)ch + input.shift;
  331. for (size_t i = 0; i < ARRAY_LEN(CP437); ++i) {
  332. if (ch == CP437[i]) return i;
  333. }
  334. return 0;
  335. }
  336. static void inputNormal(bool keyCode, wchar_t ch) {
  337. if (keyCode) {
  338. switch (ch) {
  339. break; case KEY_LEFT: clientMove(-input.delta, 0);
  340. break; case KEY_RIGHT: clientMove( input.delta, 0);
  341. break; case KEY_UP: clientMove( 0, -input.delta);
  342. break; case KEY_DOWN: clientMove( 0, input.delta);
  343. break; case KEY_F(1): input.shift = 0x00;
  344. break; case KEY_F(2): input.shift = 0xC0;
  345. break; case KEY_F(3): input.shift = 0xA0;
  346. break; case KEY_F(4): input.shift = 0x70;
  347. break; case KEY_F(5): input.shift = 0x40;
  348. }
  349. return;
  350. }
  351. switch (ch) {
  352. break; case CTRL('L'): clearok(curscr, true);
  353. break; case Esc: modeNormal(); input.shift = 0;
  354. break; case 'q': endwin(); exit(EX_OK);
  355. break; case 'Q': clientTele(input.color % ARRAY_LEN(Ports));
  356. break; case '\\': input.delta = (input.delta == 1 ? 4 : 1);
  357. break; case 'h': clientMove(-input.delta, 0);
  358. break; case 'l': clientMove( input.delta, 0);
  359. break; case 'k': clientMove( 0, -input.delta);
  360. break; case 'j': clientMove( 0, input.delta);
  361. break; case 'y': clientMove(-input.delta, -input.delta);
  362. break; case 'u': clientMove( input.delta, -input.delta);
  363. break; case 'b': clientMove(-input.delta, input.delta);
  364. break; case 'n': clientMove( input.delta, input.delta);
  365. break; case '0': colorFg(ColorBlack);
  366. break; case '1': colorFg(ColorRed);
  367. break; case '2': colorFg(ColorGreen);
  368. break; case '3': colorFg(ColorYellow);
  369. break; case '4': colorFg(ColorBlue);
  370. break; case '5': colorFg(ColorMagenta);
  371. break; case '6': colorFg(ColorCyan);
  372. break; case '7': colorFg(ColorWhite);
  373. break; case ')': colorBg(ColorBlack);
  374. break; case '!': colorBg(ColorRed);
  375. break; case '@': colorBg(ColorGreen);
  376. break; case '#': colorBg(ColorYellow);
  377. break; case '$': colorBg(ColorBlue);
  378. break; case '%': colorBg(ColorMagenta);
  379. break; case '^': colorBg(ColorCyan);
  380. break; case '&': colorBg(ColorWhite);
  381. break; case '8': input.color ^= ColorBright;
  382. break; case '9': input.color = colorInvert(input.color);
  383. break; case '`': input.color = tile.colors[cellY][cellX];
  384. break; case 'H': cellSwap(-1, 0);
  385. break; case 'L': cellSwap( 1, 0);
  386. break; case 'K': cellSwap( 0, -1);
  387. break; case 'J': cellSwap( 0, 1);
  388. break; case 'Y': cellSwap(-1, -1);
  389. break; case 'U': cellSwap( 1, -1);
  390. break; case 'B': cellSwap(-1, 1);
  391. break; case 'N': cellSwap( 1, 1);
  392. break; case 's': cellCopy();
  393. break; case 'x': cellCopy(); clientPut(copy.color, ' ');
  394. break; case 'p': clientPut(copy.color, copy.cell);
  395. break; case '~': {
  396. cellCopy();
  397. clientPut(input.color, tile.cells[cellY][cellX]);
  398. clientMove(1, 0);
  399. }
  400. break; case '*': {
  401. clientPut(
  402. tile.colors[cellY][cellX] ^ ColorBright,
  403. tile.cells[cellY][cellX]
  404. );
  405. clientMove(1, 0);
  406. }
  407. break; case '(': {
  408. clientPut(
  409. colorInvert(tile.colors[cellY][cellX]),
  410. tile.cells[cellY][cellX]
  411. );
  412. clientMove(1, 0);
  413. }
  414. break; case CTRL('A'): {
  415. clientPut(tile.colors[cellY][cellX], tile.cells[cellY][cellX] + 1);
  416. }
  417. break; case CTRL('X'): {
  418. clientPut(tile.colors[cellY][cellX], tile.cells[cellY][cellX] - 1);
  419. }
  420. break; case '?': modeHelp();
  421. break; case 'm': modeMap();
  422. break; case 'I': modeDirection();
  423. break; case 'i': modeInsert(1, 0);
  424. break; case 'a': modeInsert(1, 0); clientMove(1, 0);
  425. break; case 'r': modeReplace(); cellCopy();
  426. break; case 'R': modeDraw();
  427. break; case '.': modeLine();
  428. }
  429. }
  430. static void inputHelp(bool keyCode, wchar_t ch) {
  431. (void)keyCode;
  432. (void)ch;
  433. if (tileMeta(&tile).createTime) drawTile(&tile);
  434. modeNormal();
  435. }
  436. static void inputMap(bool keyCode, wchar_t ch) {
  437. (void)keyCode;
  438. (void)ch;
  439. drawTile(&tile);
  440. modeNormal();
  441. }
  442. static void inputDirection(bool keyCode, wchar_t ch) {
  443. if (keyCode) return;
  444. switch (ch) {
  445. break; case Esc: modeNormal();
  446. break; case 'h': modeInsert(-1, 0);
  447. break; case 'l': modeInsert( 1, 0);
  448. break; case 'k': modeInsert( 0, -1);
  449. break; case 'j': modeInsert( 0, 1);
  450. break; case 'y': modeInsert(-1, -1);
  451. break; case 'u': modeInsert( 1, -1);
  452. break; case 'b': modeInsert(-1, 1);
  453. break; case 'n': modeInsert( 1, 1);
  454. }
  455. }
  456. static void inputInsert(bool keyCode, wchar_t ch) {
  457. if (keyCode) {
  458. inputNormal(keyCode, ch);
  459. return;
  460. }
  461. switch (ch) {
  462. break; case Esc: {
  463. clientMove(-insert.dx, -insert.dy);
  464. modeNormal();
  465. }
  466. break; case '\b': case Del: {
  467. clientMove(-insert.dx, -insert.dy);
  468. clientPut(input.color, ' ');
  469. insert.len--;
  470. }
  471. break; case '\n': {
  472. clientMove(insert.dy, insert.dx);
  473. clientMove(insert.len * -insert.dx, insert.len * -insert.dy);
  474. insert.len = 0;
  475. }
  476. break; default: {
  477. uint8_t cell = inputCell(ch);
  478. if (!cell) break;
  479. clientPut(input.color, cell);
  480. clientMove(insert.dx, insert.dy);
  481. insert.len++;
  482. }
  483. }
  484. }
  485. static void inputReplace(bool keyCode, wchar_t ch) {
  486. if (keyCode) {
  487. inputNormal(keyCode, ch);
  488. return;
  489. }
  490. if (ch != Esc) {
  491. uint8_t cell = inputCell(ch);
  492. if (!cell) return;
  493. clientPut(tile.colors[cellY][cellX], cell);
  494. }
  495. modeNormal();
  496. }
  497. static void inputDraw(bool keyCode, wchar_t ch) {
  498. if (!keyCode && ch == Esc) {
  499. modeNormal();
  500. return;
  501. }
  502. if (input.draw) {
  503. inputNormal(keyCode, ch);
  504. } else {
  505. if (keyCode) {
  506. inputNormal(keyCode, ch);
  507. return;
  508. }
  509. input.draw = inputCell(ch);
  510. }
  511. clientPut(input.color, input.draw);
  512. }
  513. static uint8_t lineCell(uint8_t cell, int8_t dx, int8_t dy) {
  514. if (dx < 0) {
  515. switch (CP437[cell]) {
  516. default: return inputCell(L'→');
  517. case L'←': return inputCell(L'─'); case L'─': return 0;
  518. case L'↑': return inputCell(L'┐'); case L'┐': return 0;
  519. case L'↓': return inputCell(L'┘'); case L'┘': return 0;
  520. case L'│': return inputCell(L'┤'); case L'┤': return 0;
  521. case L'└': return inputCell(L'┴'); case L'┴': return 0;
  522. case L'┌': return inputCell(L'┬'); case L'┬': return 0;
  523. case L'├': return inputCell(L'┼'); case L'┼': return 0;
  524. }
  525. } else if (dx > 0) {
  526. switch (CP437[cell]) {
  527. default: return inputCell(L'←');
  528. case L'→': return inputCell(L'─'); case L'─': return 0;
  529. case L'↑': return inputCell(L'┌'); case L'┌': return 0;
  530. case L'↓': return inputCell(L'└'); case L'└': return 0;
  531. case L'│': return inputCell(L'├'); case L'├': return 0;
  532. case L'┘': return inputCell(L'┴'); case L'┴': return 0;
  533. case L'┐': return inputCell(L'┬'); case L'┬': return 0;
  534. case L'┤': return inputCell(L'┼'); case L'┼': return 0;
  535. }
  536. } else if (dy < 0) {
  537. switch (CP437[cell]) {
  538. default: return inputCell(L'↓');
  539. case L'↑': return inputCell(L'│'); case L'│': return 0;
  540. case L'←': return inputCell(L'└'); case L'└': return 0;
  541. case L'→': return inputCell(L'┘'); case L'┘': return 0;
  542. case L'─': return inputCell(L'┴'); case L'┴': return 0;
  543. case L'┌': return inputCell(L'├'); case L'├': return 0;
  544. case L'┐': return inputCell(L'┤'); case L'┤': return 0;
  545. case L'┬': return inputCell(L'┼'); case L'┼': return 0;
  546. }
  547. } else if (dy > 0) {
  548. switch (CP437[cell]) {
  549. default: return inputCell(L'↑');
  550. case L'↓': return inputCell(L'│'); case L'│': return 0;
  551. case L'←': return inputCell(L'┌'); case L'┌': return 0;
  552. case L'→': return inputCell(L'┐'); case L'┐': return 0;
  553. case L'─': return inputCell(L'┬'); case L'┬': return 0;
  554. case L'└': return inputCell(L'├'); case L'├': return 0;
  555. case L'┘': return inputCell(L'┤'); case L'┤': return 0;
  556. case L'┴': return inputCell(L'┼'); case L'┼': return 0;
  557. }
  558. }
  559. return 0;
  560. }
  561. static void inputLine(bool keyCode, wchar_t ch) {
  562. int8_t dx = 0;
  563. int8_t dy = 0;
  564. if (keyCode) {
  565. switch (ch) {
  566. break; case KEY_LEFT: dx = -1;
  567. break; case KEY_RIGHT: dx = 1;
  568. break; case KEY_UP: dy = -1;
  569. break; case KEY_DOWN: dy = 1;
  570. break; default: return;
  571. }
  572. } else {
  573. switch (ch) {
  574. break; case Esc: case '.': modeNormal(); return;
  575. break; case 'h': dx = -1;
  576. break; case 'l': dx = 1;
  577. break; case 'k': dy = -1;
  578. break; case 'j': dy = 1;
  579. break; default: return;
  580. }
  581. }
  582. uint8_t leave = lineCell(tile.cells[cellY][cellX], dx, dy);
  583. uint8_t enter = lineCell(tile.cells[cellY + dy][cellX + dx], -dx, -dy);
  584. if (leave) clientPut(input.color, leave);
  585. if ((uint8_t)(cellX + dx) >= CellCols) return;
  586. if ((uint8_t)(cellY + dy) >= CellRows) return;
  587. clientMove(dx, dy);
  588. if (enter) clientPut(input.color, enter);
  589. }
  590. static void readInput(void) {
  591. wint_t ch;
  592. bool keyCode = (KEY_CODE_YES == get_wch(&ch));
  593. switch (input.mode) {
  594. break; case ModeNormal: inputNormal(keyCode, ch);
  595. break; case ModeHelp: inputHelp(keyCode, ch);
  596. break; case ModeMap: inputMap(keyCode, ch);
  597. break; case ModeDirection: inputDirection(keyCode, ch);
  598. break; case ModeInsert: inputInsert(keyCode, ch);
  599. break; case ModeReplace: inputReplace(keyCode, ch);
  600. break; case ModeDraw: inputDraw(keyCode, ch);
  601. break; case ModeLine: inputLine(keyCode, ch);
  602. }
  603. }
  604. int main(int argc, char *argv[]) {
  605. const char *sockPath = DefaultSockPath;
  606. int opt;
  607. while (0 < (opt = getopt(argc, argv, "hs:"))) {
  608. switch (opt) {
  609. break; case 'h': {
  610. fwrite(HelpData, sizeof(HelpData), 1, stdout);
  611. return EX_OK;
  612. }
  613. break; case 's': sockPath = optarg;
  614. break; default: return EX_USAGE;
  615. }
  616. }
  617. curse();
  618. modeHelp();
  619. readInput();
  620. client = socket(PF_LOCAL, SOCK_STREAM, 0);
  621. if (client < 0) err(EX_OSERR, "socket");
  622. struct sockaddr_un addr = { .sun_family = AF_LOCAL };
  623. strlcpy(addr.sun_path, sockPath, sizeof(addr.sun_path));
  624. int error = connect(client, (struct sockaddr *)&addr, SUN_LEN(&addr));
  625. if (error) err(EX_NOINPUT, "%s", sockPath);
  626. #ifdef __FreeBSD__
  627. error = cap_enter();
  628. if (error) err(EX_OSERR, "cap_enter");
  629. cap_rights_t rights;
  630. cap_rights_init(&rights, CAP_READ, CAP_WRITE, CAP_EVENT);
  631. error = cap_rights_limit(client, &rights);
  632. if (error) err(EX_OSERR, "cap_rights_limit");
  633. #endif
  634. struct pollfd fds[2] = {
  635. { .fd = STDIN_FILENO, .events = POLLIN },
  636. { .fd = client, .events = POLLIN },
  637. };
  638. for (;;) {
  639. int nfds = poll(fds, 2, -1);
  640. if (nfds < 0 && errno == EINTR) continue;
  641. if (nfds < 0) err(EX_IOERR, "poll");
  642. if (fds[0].revents) readInput();
  643. if (fds[1].revents) readMessage();
  644. refresh();
  645. }
  646. }