No Description 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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720
  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. attr_set(colorAttr(color), colorPair(color), NULL);
  193. mvaddwstr(MapY + y, MapX + 3 * x, tile);
  194. }
  195. }
  196. attr_set(A_NORMAL, 0, NULL);
  197. }
  198. static void readMessage(void) {
  199. struct ServerMessage msg;
  200. ssize_t size = recv(client, &msg, sizeof(msg), 0);
  201. if (size < 0) err(EX_IOERR, "recv");
  202. if ((size_t)size < sizeof(msg)) errx(EX_PROTOCOL, "truncated message");
  203. switch (msg.type) {
  204. break; case ServerTile: serverTile();
  205. break; case ServerMove: serverMove(msg);
  206. break; case ServerPut: serverPut(msg);
  207. break; case ServerCursor: serverCursor(msg);
  208. break; case ServerMap: serverMap();
  209. break; default: errx(EX_PROTOCOL, "unknown message type %d", msg.type);
  210. }
  211. move(cellY, cellX);
  212. }
  213. static void clientMessage(struct ClientMessage msg) {
  214. ssize_t size = send(client, &msg, sizeof(msg), 0);
  215. if (size < 0) err(EX_IOERR, "send");
  216. }
  217. static void clientMove(int8_t dx, int8_t dy) {
  218. struct ClientMessage msg = {
  219. .type = ClientMove,
  220. .move = { .dx = dx, .dy = dy },
  221. };
  222. clientMessage(msg);
  223. }
  224. static void clientFlip(void) {
  225. struct ClientMessage msg = { .type = ClientFlip };
  226. clientMessage(msg);
  227. }
  228. static void clientPut(uint8_t color, uint8_t cell) {
  229. struct ClientMessage msg = {
  230. .type = ClientPut,
  231. .put = { .color = color, .cell = cell },
  232. };
  233. clientMessage(msg);
  234. }
  235. static void clientMap(void) {
  236. struct ClientMessage msg = { .type = ClientMap };
  237. clientMessage(msg);
  238. }
  239. static struct {
  240. enum {
  241. ModeNormal,
  242. ModeHelp,
  243. ModeMap,
  244. ModeDirection,
  245. ModeInsert,
  246. ModeReplace,
  247. ModeDraw,
  248. ModeLine,
  249. } mode;
  250. uint8_t color;
  251. uint8_t shift;
  252. uint8_t draw;
  253. } input = {
  254. .color = ColorWhite,
  255. };
  256. static struct {
  257. uint8_t color;
  258. uint8_t cell;
  259. } copy;
  260. static struct {
  261. int8_t dx;
  262. int8_t dy;
  263. uint8_t len;
  264. } insert;
  265. static void modeNormal(void) {
  266. curs_set(1);
  267. move(cellY, cellX);
  268. input.mode = ModeNormal;
  269. }
  270. static void modeHelp(void) {
  271. curs_set(0);
  272. drawTile(Help);
  273. input.mode = ModeHelp;
  274. }
  275. static void modeMap(void) {
  276. curs_set(0);
  277. clientMap();
  278. input.mode = ModeMap;
  279. }
  280. static void modeDirection(void) {
  281. input.mode = ModeDirection;
  282. }
  283. static void modeInsert(int8_t dx, int8_t dy) {
  284. insert.dx = dx;
  285. insert.dy = dy;
  286. insert.len = 0;
  287. input.mode = ModeInsert;
  288. }
  289. static void modeReplace(void) {
  290. input.mode = ModeReplace;
  291. }
  292. static void modeDraw(void) {
  293. input.draw = 0;
  294. input.mode = ModeDraw;
  295. }
  296. static void modeLine(void) {
  297. input.mode = ModeLine;
  298. }
  299. static void colorFg(uint8_t fg) {
  300. input.color = (input.color & 0x78) | (fg & 0x07);
  301. }
  302. static void colorBg(uint8_t bg) {
  303. input.color = (input.color & 0x0F) | (bg & 0x07) << 4;
  304. }
  305. static uint8_t colorInvert(uint8_t color) {
  306. return (color & 0x08)
  307. | (color & 0x70) >> 4
  308. | (color & 0x07) << 4;
  309. }
  310. static void cellCopy(void) {
  311. copy.color = tile.colors[cellY][cellX];
  312. copy.cell = tile.cells[cellY][cellX];
  313. }
  314. static void cellSwap(int8_t dx, int8_t dy) {
  315. if ((uint8_t)(cellX + dx) >= CellCols) return;
  316. if ((uint8_t)(cellY + dy) >= CellRows) return;
  317. uint8_t aColor = tile.colors[cellY][cellX];
  318. uint8_t aCell = tile.cells[cellY][cellX];
  319. uint8_t bColor = tile.colors[cellY + dy][cellX + dx];
  320. uint8_t bCell = tile.cells[cellY + dy][cellX + dx];
  321. clientPut(bColor, bCell);
  322. clientMove(dx, dy);
  323. clientPut(aColor, aCell);
  324. }
  325. static uint8_t inputCell(wchar_t ch) {
  326. if (ch == ' ') return ' ';
  327. if (ch < 0x80) return (uint8_t)ch + input.shift;
  328. for (size_t i = 0; i < ARRAY_LEN(CP437); ++i) {
  329. if (ch == CP437[i]) return i;
  330. }
  331. return 0;
  332. }
  333. static void inputNormal(bool keyCode, wchar_t ch) {
  334. if (keyCode) {
  335. switch (ch) {
  336. break; case KEY_LEFT: clientMove(-1, 0);
  337. break; case KEY_RIGHT: clientMove( 1, 0);
  338. break; case KEY_UP: clientMove( 0, -1);
  339. break; case KEY_DOWN: clientMove( 0, 1);
  340. break; case KEY_F(1): input.shift = 0x00;
  341. break; case KEY_F(2): input.shift = 0xC0;
  342. break; case KEY_F(3): input.shift = 0xA0;
  343. break; case KEY_F(4): input.shift = 0x70;
  344. break; case KEY_F(5): input.shift = 0x40;
  345. }
  346. return;
  347. }
  348. switch (ch) {
  349. break; case CTRL('L'): clearok(curscr, true);
  350. break; case Esc: modeNormal(); input.shift = 0;
  351. break; case 'q': endwin(); exit(EX_OK);
  352. break; case 'g': clientFlip();
  353. break; case 'h': clientMove(-1, 0);
  354. break; case 'l': clientMove( 1, 0);
  355. break; case 'k': clientMove( 0, -1);
  356. break; case 'j': clientMove( 0, 1);
  357. break; case 'y': clientMove(-1, -1);
  358. break; case 'u': clientMove( 1, -1);
  359. break; case 'b': clientMove(-1, 1);
  360. break; case 'n': clientMove( 1, 1);
  361. break; case '0': colorFg(ColorBlack);
  362. break; case '1': colorFg(ColorRed);
  363. break; case '2': colorFg(ColorGreen);
  364. break; case '3': colorFg(ColorYellow);
  365. break; case '4': colorFg(ColorBlue);
  366. break; case '5': colorFg(ColorMagenta);
  367. break; case '6': colorFg(ColorCyan);
  368. break; case '7': colorFg(ColorWhite);
  369. break; case ')': colorBg(ColorBlack);
  370. break; case '!': colorBg(ColorRed);
  371. break; case '@': colorBg(ColorGreen);
  372. break; case '#': colorBg(ColorYellow);
  373. break; case '$': colorBg(ColorBlue);
  374. break; case '%': colorBg(ColorMagenta);
  375. break; case '^': colorBg(ColorCyan);
  376. break; case '&': colorBg(ColorWhite);
  377. break; case '8': input.color ^= ColorBright;
  378. break; case '9': input.color = colorInvert(input.color);
  379. break; case '`': input.color = tile.colors[cellY][cellX];
  380. break; case 'H': cellSwap(-1, 0);
  381. break; case 'L': cellSwap( 1, 0);
  382. break; case 'K': cellSwap( 0, -1);
  383. break; case 'J': cellSwap( 0, 1);
  384. break; case 'Y': cellSwap(-1, -1);
  385. break; case 'U': cellSwap( 1, -1);
  386. break; case 'B': cellSwap(-1, 1);
  387. break; case 'N': cellSwap( 1, 1);
  388. break; case 's': cellCopy();
  389. break; case 'x': cellCopy(); clientPut(copy.color, ' ');
  390. break; case 'p': clientPut(copy.color, copy.cell);
  391. break; case '~': {
  392. cellCopy();
  393. clientPut(input.color, tile.cells[cellY][cellX]);
  394. clientMove(1, 0);
  395. }
  396. break; case '*': {
  397. clientPut(
  398. tile.colors[cellY][cellX] ^ ColorBright,
  399. tile.cells[cellY][cellX]
  400. );
  401. clientMove(1, 0);
  402. }
  403. break; case '(': {
  404. clientPut(
  405. colorInvert(tile.colors[cellY][cellX]),
  406. tile.cells[cellY][cellX]
  407. );
  408. clientMove(1, 0);
  409. }
  410. break; case CTRL('A'): {
  411. clientPut(tile.colors[cellY][cellX], tile.cells[cellY][cellX] + 1);
  412. }
  413. break; case CTRL('X'): {
  414. clientPut(tile.colors[cellY][cellX], tile.cells[cellY][cellX] - 1);
  415. }
  416. break; case '?': modeHelp();
  417. break; case 'm': modeMap();
  418. break; case 'I': modeDirection();
  419. break; case 'i': modeInsert(1, 0);
  420. break; case 'a': modeInsert(1, 0); clientMove(1, 0);
  421. break; case 'r': modeReplace(); cellCopy();
  422. break; case 'R': modeDraw();
  423. break; case '.': modeLine();
  424. }
  425. }
  426. static void inputHelp(bool keyCode, wchar_t ch) {
  427. (void)keyCode;
  428. (void)ch;
  429. if (tile.meta.createTime) drawTile(&tile);
  430. modeNormal();
  431. }
  432. static void inputMap(bool keyCode, wchar_t ch) {
  433. (void)keyCode;
  434. (void)ch;
  435. drawTile(&tile);
  436. modeNormal();
  437. }
  438. static void inputDirection(bool keyCode, wchar_t ch) {
  439. if (keyCode) return;
  440. switch (ch) {
  441. break; case Esc: modeNormal();
  442. break; case 'h': modeInsert(-1, 0);
  443. break; case 'l': modeInsert( 1, 0);
  444. break; case 'k': modeInsert( 0, -1);
  445. break; case 'j': modeInsert( 0, 1);
  446. break; case 'y': modeInsert(-1, -1);
  447. break; case 'u': modeInsert( 1, -1);
  448. break; case 'b': modeInsert(-1, 1);
  449. break; case 'n': modeInsert( 1, 1);
  450. }
  451. }
  452. static void inputInsert(bool keyCode, wchar_t ch) {
  453. if (keyCode) {
  454. inputNormal(keyCode, ch);
  455. return;
  456. }
  457. switch (ch) {
  458. break; case Esc: {
  459. clientMove(-insert.dx, -insert.dy);
  460. modeNormal();
  461. }
  462. break; case '\b': case Del: {
  463. clientMove(-insert.dx, -insert.dy);
  464. clientPut(input.color, ' ');
  465. insert.len--;
  466. }
  467. break; case '\n': {
  468. clientMove(insert.dy, insert.dx);
  469. clientMove(insert.len * -insert.dx, insert.len * -insert.dy);
  470. insert.len = 0;
  471. }
  472. break; default: {
  473. uint8_t cell = inputCell(ch);
  474. if (!cell) break;
  475. clientPut(input.color, cell);
  476. clientMove(insert.dx, insert.dy);
  477. insert.len++;
  478. }
  479. }
  480. }
  481. static void inputReplace(bool keyCode, wchar_t ch) {
  482. if (keyCode) {
  483. inputNormal(keyCode, ch);
  484. return;
  485. }
  486. if (ch != Esc) {
  487. uint8_t cell = inputCell(ch);
  488. if (!cell) return;
  489. clientPut(tile.colors[cellY][cellX], cell);
  490. }
  491. modeNormal();
  492. }
  493. static void inputDraw(bool keyCode, wchar_t ch) {
  494. if (!keyCode && ch == Esc) {
  495. modeNormal();
  496. return;
  497. }
  498. if (input.draw) {
  499. inputNormal(keyCode, ch);
  500. } else {
  501. if (keyCode) {
  502. inputNormal(keyCode, ch);
  503. return;
  504. }
  505. input.draw = inputCell(ch);
  506. }
  507. clientPut(input.color, input.draw);
  508. }
  509. static uint8_t lineCell(uint8_t cell, int8_t dx, int8_t dy) {
  510. if (dx < 0) {
  511. switch (CP437[cell]) {
  512. default: return inputCell(L'→');
  513. case L'←': return inputCell(L'─'); case L'─': return 0;
  514. case L'↑': return inputCell(L'┐'); case L'┐': return 0;
  515. case L'↓': return inputCell(L'┘'); case L'┘': return 0;
  516. case L'│': return inputCell(L'┤'); case L'┤': return 0;
  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. }
  521. } else if (dx > 0) {
  522. switch (CP437[cell]) {
  523. default: return inputCell(L'←');
  524. case L'→': return inputCell(L'─'); case L'─': return 0;
  525. case L'↑': return inputCell(L'┌'); case L'┌': return 0;
  526. case L'↓': return inputCell(L'└'); case L'└': return 0;
  527. case L'│': return inputCell(L'├'); case L'├': return 0;
  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. }
  532. } else if (dy < 0) {
  533. switch (CP437[cell]) {
  534. default: return inputCell(L'↓');
  535. case L'↑': return inputCell(L'│'); case L'│': return 0;
  536. case L'←': return inputCell(L'└'); case L'└': return 0;
  537. case L'→': return inputCell(L'┘'); case L'┘': return 0;
  538. case L'─': return inputCell(L'┴'); case L'┴': return 0;
  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. }
  543. } else if (dy > 0) {
  544. switch (CP437[cell]) {
  545. default: return inputCell(L'↑');
  546. case L'↓': return inputCell(L'│'); case L'│': return 0;
  547. case L'←': return inputCell(L'┌'); case L'┌': return 0;
  548. case L'→': return inputCell(L'┐'); case L'┐': return 0;
  549. case L'─': return inputCell(L'┬'); case L'┬': return 0;
  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. }
  554. }
  555. return 0;
  556. }
  557. static void inputLine(bool keyCode, wchar_t ch) {
  558. int8_t dx = 0;
  559. int8_t dy = 0;
  560. if (keyCode) {
  561. switch (ch) {
  562. break; case KEY_LEFT: dx = -1;
  563. break; case KEY_RIGHT: dx = 1;
  564. break; case KEY_UP: dy = -1;
  565. break; case KEY_DOWN: dy = 1;
  566. break; default: return;
  567. }
  568. } else {
  569. switch (ch) {
  570. break; case Esc: case '.': modeNormal(); return;
  571. break; case 'h': dx = -1;
  572. break; case 'l': dx = 1;
  573. break; case 'k': dy = -1;
  574. break; case 'j': dy = 1;
  575. break; default: return;
  576. }
  577. }
  578. if ((uint8_t)(cellX + dx) >= CellCols) return;
  579. if ((uint8_t)(cellY + dy) >= CellRows) return;
  580. uint8_t leave = lineCell(tile.cells[cellY][cellX], dx, dy);
  581. uint8_t enter = lineCell(tile.cells[cellY + dy][cellX + dx], -dx, -dy);
  582. if (leave) clientPut(input.color, leave);
  583. clientMove(dx, dy);
  584. if (enter) clientPut(input.color, enter);
  585. }
  586. static void readInput(void) {
  587. wint_t ch;
  588. bool keyCode = (KEY_CODE_YES == get_wch(&ch));
  589. switch (input.mode) {
  590. break; case ModeNormal: inputNormal(keyCode, ch);
  591. break; case ModeHelp: inputHelp(keyCode, ch);
  592. break; case ModeMap: inputMap(keyCode, ch);
  593. break; case ModeDirection: inputDirection(keyCode, ch);
  594. break; case ModeInsert: inputInsert(keyCode, ch);
  595. break; case ModeReplace: inputReplace(keyCode, ch);
  596. break; case ModeDraw: inputDraw(keyCode, ch);
  597. break; case ModeLine: inputLine(keyCode, ch);
  598. }
  599. }
  600. int main(int argc, char *argv[]) {
  601. const char *sockPath = "torus.sock";
  602. int opt;
  603. while (0 < (opt = getopt(argc, argv, "hs:"))) {
  604. switch (opt) {
  605. break; case 'h': {
  606. fwrite(HelpData, sizeof(HelpData), 1, stdout);
  607. return EX_OK;
  608. }
  609. break; case 's': sockPath = optarg;
  610. break; default: return EX_USAGE;
  611. }
  612. }
  613. curse();
  614. modeHelp();
  615. readInput();
  616. client = socket(PF_LOCAL, SOCK_STREAM, 0);
  617. if (client < 0) err(EX_OSERR, "socket");
  618. struct sockaddr_un addr = { .sun_family = AF_LOCAL };
  619. strlcpy(addr.sun_path, sockPath, sizeof(addr.sun_path));
  620. int error = connect(client, (struct sockaddr *)&addr, SUN_LEN(&addr));
  621. if (error) err(EX_NOINPUT, "%s", sockPath);
  622. #ifdef __FreeBSD__
  623. error = cap_enter();
  624. if (error) err(EX_OSERR, "cap_enter");
  625. #endif
  626. struct pollfd fds[2] = {
  627. { .fd = STDIN_FILENO, .events = POLLIN },
  628. { .fd = client, .events = POLLIN },
  629. };
  630. for (;;) {
  631. int nfds = poll(fds, 2, -1);
  632. if (nfds < 0 && errno == EINTR) continue;
  633. if (nfds < 0) err(EX_IOERR, "poll");
  634. if (fds[0].revents) readInput();
  635. if (fds[1].revents) readMessage();
  636. refresh();
  637. }
  638. }