diff options
-rw-r--r-- | Makefile | 4 | ||||
-rw-r--r-- | client.c | 206 | ||||
-rw-r--r-- | help.h | 4 | ||||
-rw-r--r-- | merge.c | 22 | ||||
-rw-r--r-- | meta.c | 4 | ||||
-rw-r--r-- | server.c | 112 | ||||
-rw-r--r-- | torus.h | 77 |
7 files changed, 210 insertions, 219 deletions
diff --git a/Makefile b/Makefile index 9199a7b..018550f 100644 --- a/Makefile +++ b/Makefile @@ -17,9 +17,9 @@ client.o: help.h help.h: head -c 4096 torus.dat \ - | file2c -s -x 'static const uint8_t HELP_DATA[] = {' '};' \ + | file2c -s -x 'static const uint8_t HelpData[] = {' '};' \ > help.h - echo 'static const struct Tile *HELP = (const struct Tile *)HELP_DATA;' \ + echo 'static const struct Tile *Help = (const struct Tile *)HelpData;' \ >> help.h tags: *.h *.c diff --git a/client.c b/client.c index 66e6d08..d1c11ac 100644 --- a/client.c +++ b/client.c @@ -42,8 +42,8 @@ #define CTRL(ch) ((ch) ^ 0x40) enum { - ESC = 0x1B, - DEL = 0x7F, + Esc = 0x1B, + Del = 0x7F, }; static uint32_t log2(uint32_t n) { @@ -62,13 +62,13 @@ static void curse(void) { fprintf(stderr, "If you think it should, check TERM.\n"); exit(EX_CONFIG); } - if (LINES < CELL_ROWS || COLS < CELL_COLS) { + if (LINES < CellRows || COLS < CellCols) { endwin(); fprintf( stderr, "Sorry, your terminal is too small!\n" "It must be at least %ux%u characters.\n", - CELL_COLS, CELL_ROWS + CellCols, CellRows ); exit(EX_CONFIG); } @@ -84,12 +84,12 @@ static void curse(void) { } } - color_set(COLOR_WHITE, NULL); - bool hline = (LINES > CELL_ROWS); - bool vline = (COLS > CELL_COLS); - if (hline) mvhline(CELL_ROWS, 0, 0, CELL_COLS); - if (vline) mvvline(0, CELL_COLS, 0, CELL_ROWS); - if (hline && vline) mvaddch(CELL_ROWS, CELL_COLS, ACS_LRCORNER); + color_set(ColorWhite, NULL); + bool hline = (LINES > CellRows); + bool vline = (COLS > CellCols); + if (hline) mvhline(CellRows, 0, 0, CellCols); + if (vline) mvvline(0, CellCols, 0, CellRows); + if (hline && vline) mvaddch(CellRows, CellCols, ACS_LRCORNER); color_set(0, NULL); cbreak(); @@ -100,7 +100,7 @@ static void curse(void) { static attr_t colorAttr(uint8_t color) { if (COLORS >= 16) return A_NORMAL; - return (color & COLOR_BRIGHT) ? A_BOLD : A_NORMAL; + return (color & ColorBright) ? A_BOLD : A_NORMAL; } static short colorPair(uint8_t color) { if (COLORS >= 16) return color; @@ -120,8 +120,8 @@ static void drawCell( } static void drawTile(const struct Tile *tile) { - for (uint8_t cellY = 0; cellY < CELL_ROWS; ++cellY) { - for (uint8_t cellX = 0; cellX < CELL_COLS; ++cellX) { + for (uint8_t cellY = 0; cellY < CellRows; ++cellY) { + for (uint8_t cellX = 0; cellX < CellCols; ++cellX) { drawCell(tile, cellX, cellY, A_NORMAL); } } @@ -152,32 +152,32 @@ static void serverPut(struct ServerMessage msg) { } static void serverCursor(struct ServerMessage msg) { - if (msg.cursor.oldCellX != CURSOR_NONE) { + if (msg.cursor.oldCellX != CursorNone) { drawCell(&tile, msg.cursor.oldCellX, msg.cursor.oldCellY, A_NORMAL); } - if (msg.cursor.newCellX != CURSOR_NONE) { + if (msg.cursor.newCellX != CursorNone) { drawCell(&tile, msg.cursor.newCellX, msg.cursor.newCellY, A_REVERSE); } } -static const uint8_t MAP_X = (CELL_COLS / 2) - (3 * MAP_COLS / 2); -static const uint8_t MAP_Y = (CELL_ROWS / 2) - (MAP_ROWS / 2); +static const uint8_t MapX = (CellCols / 2) - (3 * MapCols / 2); +static const uint8_t MapY = (CellRows / 2) - (MapRows / 2); -static const wchar_t MAP_CELLS[5] = L" ░▒▓█"; -static const uint8_t MAP_COLORS[] = { - COLOR_BLUE, COLOR_CYAN, COLOR_GREEN, COLOR_YELLOW, COLOR_RED, +static const wchar_t MapCells[5] = L" ░▒▓█"; +static const uint8_t MapColors[] = { + ColorBlue, ColorCyan, ColorGreen, ColorYellow, ColorRed, }; static void serverMap(void) { - int t = MAP_Y - 1; - int l = MAP_X - 1; - int b = MAP_Y + MAP_ROWS; - int r = MAP_X + 3 * MAP_COLS; - color_set(colorPair(COLOR_WHITE), NULL); - mvhline(t, MAP_X, ACS_HLINE, 3 * MAP_COLS); - mvhline(b, MAP_X, ACS_HLINE, 3 * MAP_COLS); - mvvline(MAP_Y, l, ACS_VLINE, MAP_ROWS); - mvvline(MAP_Y, r, ACS_VLINE, MAP_ROWS); + int t = MapY - 1; + int l = MapX - 1; + int b = MapY + MapRows; + int r = MapX + 3 * MapCols; + color_set(colorPair(ColorWhite), NULL); + mvhline(t, MapX, ACS_HLINE, 3 * MapCols); + mvhline(b, MapX, ACS_HLINE, 3 * MapCols); + mvvline(MapY, l, ACS_VLINE, MapRows); + mvvline(MapY, r, ACS_VLINE, MapRows); mvaddch(t, l, ACS_ULCORNER); mvaddch(t, r, ACS_URCORNER); mvaddch(b, l, ACS_LLCORNER); @@ -192,14 +192,14 @@ static void serverMap(void) { if (0 == map.max.modifyCount) return; if (0 == map.now - map.min.createTime) return; - for (uint8_t y = 0; y < MAP_ROWS; ++y) { - for (uint8_t x = 0; x < MAP_COLS; ++x) { + for (uint8_t y = 0; y < MapRows; ++y) { + for (uint8_t x = 0; x < MapCols; ++x) { struct Meta meta = map.meta[y][x]; uint32_t count = 0; if (meta.modifyCount && log2(map.max.modifyCount)) { count = DIV_ROUND( - (ARRAY_LEN(MAP_CELLS) - 1) * log2(meta.modifyCount), + (ARRAY_LEN(MapCells) - 1) * log2(meta.modifyCount), log2(map.max.modifyCount) ); } @@ -207,16 +207,16 @@ static void serverMap(void) { if (meta.modifyTime) { uint32_t modify = meta.modifyTime - map.min.createTime; time = DIV_ROUND( - (ARRAY_LEN(MAP_COLORS) - 1) * modify, + (ARRAY_LEN(MapColors) - 1) * modify, map.now - map.min.createTime ); } - wchar_t cell = MAP_CELLS[count]; - uint8_t color = MAP_COLORS[time]; + wchar_t cell = MapCells[count]; + uint8_t color = MapColors[time]; wchar_t tile[] = { cell, cell, cell, L'\0' }; attr_set(colorAttr(color), colorPair(color), NULL); - mvaddwstr(MAP_Y + y, MAP_X + 3 * x, tile); + mvaddwstr(MapY + y, MapX + 3 * x, tile); } } attr_set(A_NORMAL, 0, NULL); @@ -229,11 +229,11 @@ static void readMessage(void) { if ((size_t)size < sizeof(msg)) errx(EX_PROTOCOL, "truncated message"); switch (msg.type) { - break; case SERVER_TILE: serverTile(); - break; case SERVER_MOVE: serverMove(msg); - break; case SERVER_PUT: serverPut(msg); - break; case SERVER_CURSOR: serverCursor(msg); - break; case SERVER_MAP: serverMap(); + break; case ServerTile: serverTile(); + break; case ServerMove: serverMove(msg); + break; case ServerPut: serverPut(msg); + break; case ServerCursor: serverCursor(msg); + break; case ServerMap: serverMap(); break; default: errx(EX_PROTOCOL, "unknown message type %d", msg.type); } move(cellY, cellX); @@ -246,46 +246,46 @@ static void clientMessage(struct ClientMessage msg) { static void clientMove(int8_t dx, int8_t dy) { struct ClientMessage msg = { - .type = CLIENT_MOVE, + .type = ClientMove, .move = { .dx = dx, .dy = dy }, }; clientMessage(msg); } static void clientFlip(void) { - struct ClientMessage msg = { .type = CLIENT_FLIP }; + struct ClientMessage msg = { .type = ClientFlip }; clientMessage(msg); } static void clientPut(uint8_t color, uint8_t cell) { struct ClientMessage msg = { - .type = CLIENT_PUT, + .type = ClientPut, .put = { .color = color, .cell = cell }, }; clientMessage(msg); } static void clientMap(void) { - struct ClientMessage msg = { .type = CLIENT_MAP }; + struct ClientMessage msg = { .type = ClientMap }; clientMessage(msg); } static struct { enum { - MODE_NORMAL, - MODE_HELP, - MODE_MAP, - MODE_DIRECTION, - MODE_INSERT, - MODE_REPLACE, - MODE_DRAW, - MODE_LINE, + ModeNormal, + ModeHelp, + ModeMap, + ModeDirection, + ModeInsert, + ModeReplace, + ModeDraw, + ModeLine, } mode; uint8_t color; uint8_t shift; uint8_t draw; } input = { - .color = COLOR_WHITE, + .color = ColorWhite, }; static struct { @@ -302,36 +302,36 @@ static struct { static void modeNormal(void) { curs_set(1); move(cellY, cellX); - input.mode = MODE_NORMAL; + input.mode = ModeNormal; } static void modeHelp(void) { curs_set(0); - drawTile(HELP); - input.mode = MODE_HELP; + drawTile(Help); + input.mode = ModeHelp; } static void modeMap(void) { curs_set(0); clientMap(); - input.mode = MODE_MAP; + input.mode = ModeMap; } static void modeDirection(void) { - input.mode = MODE_DIRECTION; + input.mode = ModeDirection; } static void modeInsert(int8_t dx, int8_t dy) { insert.dx = dx; insert.dy = dy; insert.len = 0; - input.mode = MODE_INSERT; + input.mode = ModeInsert; } static void modeReplace(void) { - input.mode = MODE_REPLACE; + input.mode = ModeReplace; } static void modeDraw(void) { input.draw = 0; - input.mode = MODE_DRAW; + input.mode = ModeDraw; } static void modeLine(void) { - input.mode = MODE_LINE; + input.mode = ModeLine; } static void colorFg(uint8_t fg) { @@ -353,8 +353,8 @@ static void cellCopy(void) { } static void cellSwap(int8_t dx, int8_t dy) { - if ((uint8_t)(cellX + dx) >= CELL_COLS) return; - if ((uint8_t)(cellY + dy) >= CELL_ROWS) return; + if ((uint8_t)(cellX + dx) >= CellCols) return; + if ((uint8_t)(cellY + dy) >= CellRows) return; uint8_t aColor = tile.colors[cellY][cellX]; uint8_t aCell = tile.cells[cellY][cellX]; @@ -396,7 +396,7 @@ static void inputNormal(bool keyCode, wchar_t ch) { switch (ch) { break; case CTRL('L'): clearok(curscr, true); - break; case ESC: modeNormal(); input.shift = 0; + break; case Esc: modeNormal(); input.shift = 0; break; case 'q': endwin(); exit(EX_OK); break; case 'g': clientFlip(); @@ -409,25 +409,25 @@ static void inputNormal(bool keyCode, wchar_t ch) { break; case 'b': clientMove(-1, 1); break; case 'n': clientMove( 1, 1); - break; case '0': colorFg(COLOR_BLACK); - break; case '1': colorFg(COLOR_RED); - break; case '2': colorFg(COLOR_GREEN); - break; case '3': colorFg(COLOR_YELLOW); - break; case '4': colorFg(COLOR_BLUE); - break; case '5': colorFg(COLOR_MAGENTA); - break; case '6': colorFg(COLOR_CYAN); - break; case '7': colorFg(COLOR_WHITE); - - break; case ')': colorBg(COLOR_BLACK); - break; case '!': colorBg(COLOR_RED); - break; case '@': colorBg(COLOR_GREEN); - break; case '#': colorBg(COLOR_YELLOW); - break; case '$': colorBg(COLOR_BLUE); - break; case '%': colorBg(COLOR_MAGENTA); - break; case '^': colorBg(COLOR_CYAN); - break; case '&': colorBg(COLOR_WHITE); - - break; case '8': input.color ^= COLOR_BRIGHT; + break; case '0': colorFg(ColorBlack); + break; case '1': colorFg(ColorRed); + break; case '2': colorFg(ColorGreen); + break; case '3': colorFg(ColorYellow); + break; case '4': colorFg(ColorBlue); + break; case '5': colorFg(ColorMagenta); + break; case '6': colorFg(ColorCyan); + break; case '7': colorFg(ColorWhite); + + break; case ')': colorBg(ColorBlack); + break; case '!': colorBg(ColorRed); + break; case '@': colorBg(ColorGreen); + break; case '#': colorBg(ColorYellow); + break; case '$': colorBg(ColorBlue); + break; case '%': colorBg(ColorMagenta); + break; case '^': colorBg(ColorCyan); + break; case '&': colorBg(ColorWhite); + + break; case '8': input.color ^= ColorBright; break; case '9': input.color = colorInvert(input.color); break; case '`': input.color = tile.colors[cellY][cellX]; @@ -451,7 +451,7 @@ static void inputNormal(bool keyCode, wchar_t ch) { } break; case '*': { clientPut( - tile.colors[cellY][cellX] ^ COLOR_BRIGHT, + tile.colors[cellY][cellX] ^ ColorBright, tile.cells[cellY][cellX] ); clientMove(1, 0); @@ -499,7 +499,7 @@ static void inputMap(bool keyCode, wchar_t ch) { static void inputDirection(bool keyCode, wchar_t ch) { if (keyCode) return; switch (ch) { - break; case ESC: modeNormal(); + break; case Esc: modeNormal(); break; case 'h': modeInsert(-1, 0); break; case 'l': modeInsert( 1, 0); break; case 'k': modeInsert( 0, -1); @@ -517,11 +517,11 @@ static void inputInsert(bool keyCode, wchar_t ch) { return; } switch (ch) { - break; case ESC: { + break; case Esc: { clientMove(-insert.dx, -insert.dy); modeNormal(); } - break; case '\b': case DEL: { + break; case '\b': case Del: { clientMove(-insert.dx, -insert.dy); clientPut(input.color, ' '); insert.len--; @@ -546,7 +546,7 @@ static void inputReplace(bool keyCode, wchar_t ch) { inputNormal(keyCode, ch); return; } - if (ch != ESC) { + if (ch != Esc) { uint8_t cell = inputCell(ch); if (!cell) return; clientPut(tile.colors[cellY][cellX], cell); @@ -555,7 +555,7 @@ static void inputReplace(bool keyCode, wchar_t ch) { } static void inputDraw(bool keyCode, wchar_t ch) { - if (!keyCode && ch == ESC) { + if (!keyCode && ch == Esc) { modeNormal(); return; } @@ -633,7 +633,7 @@ static void inputLine(bool keyCode, wchar_t ch) { } } else { switch (ch) { - break; case ESC: case '.': modeNormal(); return; + break; case Esc: case '.': modeNormal(); return; break; case 'h': dx = -1; break; case 'l': dx = 1; break; case 'k': dy = -1; @@ -641,8 +641,8 @@ static void inputLine(bool keyCode, wchar_t ch) { break; default: return; } } - if ((uint8_t)(cellX + dx) >= CELL_COLS) return; - if ((uint8_t)(cellY + dy) >= CELL_ROWS) return; + if ((uint8_t)(cellX + dx) >= CellCols) return; + if ((uint8_t)(cellY + dy) >= CellRows) return; uint8_t leave = lineCell(tile.cells[cellY][cellX], dx, dy); uint8_t enter = lineCell(tile.cells[cellY + dy][cellX + dx], -dx, -dy); @@ -656,14 +656,14 @@ static void readInput(void) { wint_t ch; bool keyCode = (KEY_CODE_YES == get_wch(&ch)); switch (input.mode) { - break; case MODE_NORMAL: inputNormal(keyCode, ch); - break; case MODE_HELP: inputHelp(keyCode, ch); - break; case MODE_MAP: inputMap(keyCode, ch); - break; case MODE_DIRECTION: inputDirection(keyCode, ch); - break; case MODE_INSERT: inputInsert(keyCode, ch); - break; case MODE_REPLACE: inputReplace(keyCode, ch); - break; case MODE_DRAW: inputDraw(keyCode, ch); - break; case MODE_LINE: inputLine(keyCode, ch); + break; case ModeNormal: inputNormal(keyCode, ch); + break; case ModeHelp: inputHelp(keyCode, ch); + break; case ModeMap: inputMap(keyCode, ch); + break; case ModeDirection: inputDirection(keyCode, ch); + break; case ModeInsert: inputInsert(keyCode, ch); + break; case ModeReplace: inputReplace(keyCode, ch); + break; case ModeDraw: inputDraw(keyCode, ch); + break; case ModeLine: inputLine(keyCode, ch); } } @@ -671,7 +671,7 @@ int main(int argc, char *argv[]) { int opt; while (0 < (opt = getopt(argc, argv, "h"))) { if (opt == 'h') { - fwrite(HELP_DATA, sizeof(HELP_DATA), 1, stdout); + fwrite(HelpData, sizeof(HelpData), 1, stdout); return EX_OK; } else { return EX_USAGE; diff --git a/help.h b/help.h index 8c092ed..1858af0 100644 --- a/help.h +++ b/help.h @@ -1,4 +1,4 @@ -static const uint8_t HELP_DATA[] = { +static const uint8_t HelpData[] = { 0xda, 0xc4, 0xc4, 0xc4, 0xc4, 0xc4, 0xc4, 0xc4, 0xc4, 0xc4, 0xc4, 0xc4, 0xc4, 0xc4, 0xc4, 0xc4, 0xc4, 0xc4, 0xc4, 0xc4, 0xc4, 0xc4, 0xc4, 0xc4, 0xc4, 0xc4, 0xc4, 0xc4, 0xc4, 0xc4, 0xc4, 0xc4, 0xc4, @@ -373,4 +373,4 @@ static const uint8_t HELP_DATA[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; -static const struct Tile *HELP = (const struct Tile *)HELP_DATA; +static const struct Tile *Help = (const struct Tile *)HelpData; diff --git a/merge.c b/merge.c index 22b0eac..ce5dbef 100644 --- a/merge.c +++ b/merge.c @@ -43,12 +43,12 @@ static void curse(void) { } } - color_set(COLOR_WHITE, NULL); - mvhline(CELL_ROWS, 0, 0, CELL_COLS); - mvhline(CELL_ROWS * 2 + 1, 0, 0, CELL_COLS); - mvvline(0, CELL_COLS, 0, CELL_ROWS * 2 + 1); - mvaddch(CELL_ROWS, CELL_COLS, ACS_RTEE); - mvaddch(CELL_ROWS * 2 + 1, CELL_COLS, ACS_LRCORNER); + color_set(ColorWhite, NULL); + mvhline(CellRows, 0, 0, CellCols); + mvhline(CellRows * 2 + 1, 0, 0, CellCols); + mvvline(0, CellCols, 0, CellRows * 2 + 1); + mvaddch(CellRows, CellCols, ACS_RTEE); + mvaddch(CellRows * 2 + 1, CellCols, ACS_LRCORNER); color_set(0, NULL); cbreak(); @@ -59,7 +59,7 @@ static void curse(void) { static attr_t colorAttr(uint8_t color) { if (COLORS >= 16) return A_NORMAL; - return (color & COLOR_BRIGHT) ? A_BOLD : A_NORMAL; + return (color & ColorBright) ? A_BOLD : A_NORMAL; } static short colorPair(uint8_t color) { if (COLORS >= 16) return color; @@ -67,8 +67,8 @@ static short colorPair(uint8_t color) { } static void drawTile(int offsetY, const struct Tile *tile) { - for (uint8_t cellY = 0; cellY < CELL_ROWS; ++cellY) { - for (uint8_t cellX = 0; cellX < CELL_COLS; ++cellX) { + for (uint8_t cellY = 0; cellY < CellRows; ++cellY) { + for (uint8_t cellX = 0; cellX < CellCols; ++cellX) { uint8_t color = tile->colors[cellY][cellX]; uint8_t cell = tile->cells[cellY][cellX]; @@ -111,8 +111,8 @@ int main(int argc, char *argv[]) { if (tileA.meta.modifyTime != tileB.meta.modifyTime) { drawTile(0, &tileA); - drawTile(CELL_ROWS + 1, &tileB); - move(CELL_ROWS * 2 + 2, 0); + drawTile(CellRows + 1, &tileB); + move(CellRows * 2 + 2, 0); refresh(); int c; diff --git a/meta.c b/meta.c index 990928a..ff80bd1 100644 --- a/meta.c +++ b/meta.c @@ -30,8 +30,8 @@ int main() { printf( "%d,%d,%jd,%u,%jd,%u,%jd\n", - i % TILE_COLS, - i / TILE_COLS, + i % TileCols, + i / TileCols, tile.meta.createTime, tile.meta.modifyCount, tile.meta.modifyTime, diff --git a/server.c b/server.c index b8cf76a..c48f0b8 100644 --- a/server.c +++ b/server.c @@ -41,26 +41,26 @@ static void tilesMap(void) { int fd = open("torus.dat", O_CREAT | O_RDWR, 0644); if (fd < 0) err(EX_CANTCREAT, "torus.dat"); - int error = ftruncate(fd, TILES_SIZE); + int error = ftruncate(fd, TilesSize); if (error) err(EX_IOERR, "ftruncate"); - tiles = mmap(NULL, TILES_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); + tiles = mmap(NULL, TilesSize, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); if (tiles == MAP_FAILED) err(EX_OSERR, "mmap"); - error = madvise(tiles, TILES_SIZE, MADV_RANDOM); + error = madvise(tiles, TilesSize, MADV_RANDOM); if (error) err(EX_OSERR, "madvise"); #ifdef MADV_NOCORE - error = madvise(tiles, TILES_SIZE, MADV_NOCORE); + error = madvise(tiles, TilesSize, MADV_NOCORE); if (error) err(EX_OSERR, "madvise"); #endif } static struct Tile *tileGet(uint32_t tileX, uint32_t tileY) { - struct Tile *tile = &tiles[tileY * TILE_ROWS + tileX]; + struct Tile *tile = &tiles[tileY * TileRows + tileX]; if (!tile->meta.createTime) { - memset(tile->cells, ' ', CELLS_SIZE); - memset(tile->colors, COLOR_WHITE, CELLS_SIZE); + memset(tile->cells, ' ', CellsSize); + memset(tile->colors, ColorWhite, CellsSize); tile->meta.createTime = time(NULL); } return tile; @@ -97,10 +97,10 @@ static struct Client *clientAdd(int fd) { if (!client) err(EX_OSERR, "malloc"); client->fd = fd; - client->tileX = TILE_INIT_X; - client->tileY = TILE_INIT_Y; - client->cellX = CELL_INIT_X; - client->cellY = CELL_INIT_Y; + client->tileX = TileInitX; + client->tileY = TileInitY; + client->cellX = CellInitX; + client->cellY = CellInitY; client->prev = NULL; if (clientHead) { @@ -118,7 +118,7 @@ static bool clientSend(const struct Client *client, struct ServerMessage msg) { ssize_t size = send(client->fd, &msg, sizeof(msg), 0); if (size < 0) return false; - if (msg.type == SERVER_TILE) { + if (msg.type == ServerTile) { struct Tile *tile = tileAccess(client->tileX, client->tileY); size = send(client->fd, tile, sizeof(*tile), 0); if (size < 0) return false; @@ -142,10 +142,10 @@ static void clientRemove(struct Client *client) { if (clientHead == client) clientHead = client->next; struct ServerMessage msg = { - .type = SERVER_CURSOR, + .type = ServerCursor, .cursor = { .oldCellX = client->cellX, .oldCellY = client->cellY, - .newCellX = CURSOR_NONE, .newCellY = CURSOR_NONE, + .newCellX = CursorNone, .newCellY = CursorNone, }, }; clientCast(client, msg); @@ -156,8 +156,8 @@ static void clientRemove(struct Client *client) { static bool clientCursors(const struct Client *client) { struct ServerMessage msg = { - .type = SERVER_CURSOR, - .cursor = { .oldCellX = CURSOR_NONE, .oldCellY = CURSOR_NONE }, + .type = ServerCursor, + .cursor = { .oldCellX = CursorNone, .oldCellY = CursorNone }, }; for (struct Client *friend = clientHead; friend; friend = friend->next) { @@ -174,30 +174,30 @@ static bool clientCursors(const struct Client *client) { static bool clientUpdate(struct Client *client, const struct Client *old) { struct ServerMessage msg = { - .type = SERVER_MOVE, + .type = ServerMove, .move = { .cellX = client->cellX, .cellY = client->cellY }, }; if (!clientSend(client, msg)) return false; if (client->tileX != old->tileX || client->tileY != old->tileY) { - msg.type = SERVER_TILE; + msg.type = ServerTile; if (!clientSend(client, msg)) return false; if (!clientCursors(client)) return false; msg = (struct ServerMessage) { - .type = SERVER_CURSOR, + .type = ServerCursor, .cursor = { - .oldCellX = old->cellX, .oldCellY = old->cellY, - .newCellX = CURSOR_NONE, .newCellY = CURSOR_NONE, + .oldCellX = old->cellX, .oldCellY = old->cellY, + .newCellX = CursorNone, .newCellY = CursorNone, }, }; clientCast(old, msg); msg = (struct ServerMessage) { - .type = SERVER_CURSOR, + .type = ServerCursor, .cursor = { - .oldCellX = CURSOR_NONE, .oldCellY = CURSOR_NONE, + .oldCellX = CursorNone, .oldCellY = CursorNone, .newCellX = client->cellX, .newCellY = client->cellY, }, }; @@ -205,7 +205,7 @@ static bool clientUpdate(struct Client *client, const struct Client *old) { } else { msg = (struct ServerMessage) { - .type = SERVER_CURSOR, + .type = ServerCursor, .cursor = { .oldCellX = old->cellX, .oldCellY = old->cellY, .newCellX = client->cellX, .newCellY = client->cellY, @@ -220,48 +220,48 @@ static bool clientUpdate(struct Client *client, const struct Client *old) { static bool clientMove(struct Client *client, int8_t dx, int8_t dy) { struct Client old = *client; - if (dx > CELL_COLS - client->cellX) dx = CELL_COLS - client->cellX; - if (dx < -client->cellX - 1) dx = -client->cellX - 1; - if (dy > CELL_ROWS - client->cellY) dy = CELL_ROWS - client->cellY; - if (dy < -client->cellY - 1) dy = -client->cellY - 1; + if (dx > CellCols - client->cellX) dx = CellCols - client->cellX; + if (dx < -client->cellX - 1) dx = -client->cellX - 1; + if (dy > CellRows - client->cellY) dy = CellRows - client->cellY; + if (dy < -client->cellY - 1) dy = -client->cellY - 1; client->cellX += dx; client->cellY += dy; - if (client->cellX == CELL_COLS) { + if (client->cellX == CellCols) { client->tileX++; client->cellX = 0; } if (client->cellX == UINT8_MAX) { client->tileX--; - client->cellX = CELL_COLS - 1; + client->cellX = CellCols - 1; } - if (client->cellY == CELL_ROWS) { + if (client->cellY == CellRows) { client->tileY++; client->cellY = 0; } if (client->cellY == UINT8_MAX) { client->tileY--; - client->cellY = CELL_ROWS - 1; + client->cellY = CellRows - 1; } - if (client->tileX == TILE_COLS) client->tileX = 0; - if (client->tileX == UINT32_MAX) client->tileX = TILE_COLS - 1; - if (client->tileY == TILE_ROWS) client->tileY = 0; - if (client->tileY == UINT32_MAX) client->tileY = TILE_ROWS - 1; + if (client->tileX == TileCols) client->tileX = 0; + if (client->tileX == UINT32_MAX) client->tileX = TileCols - 1; + if (client->tileY == TileRows) client->tileY = 0; + if (client->tileY == UINT32_MAX) client->tileY = TileRows - 1; - assert(client->cellX < CELL_COLS); - assert(client->cellY < CELL_ROWS); - assert(client->tileX < TILE_COLS); - assert(client->tileY < TILE_ROWS); + assert(client->cellX < CellCols); + assert(client->cellY < CellRows); + assert(client->tileX < TileCols); + assert(client->tileY < TileRows); return clientUpdate(client, &old); } static bool clientFlip(struct Client *client) { struct Client old = *client; - client->tileX = (client->tileX + TILE_COLS / 2) % TILE_COLS; - client->tileY = (client->tileY + TILE_ROWS / 2) % TILE_ROWS; + client->tileX = (client->tileX + TileCols / 2) % TileCols; + client->tileY = (client->tileY + TileRows / 2) % TileRows; return clientUpdate(client, &old); } @@ -271,7 +271,7 @@ static bool clientPut(const struct Client *client, uint8_t color, uint8_t cell) tile->cells[client->cellY][client->cellX] = cell; struct ServerMessage msg = { - .type = SERVER_PUT, + .type = ServerPut, .put = { .cellX = client->cellX, .cellY = client->cellY, @@ -285,8 +285,8 @@ static bool clientPut(const struct Client *client, uint8_t color, uint8_t cell) } static bool clientMap(const struct Client *client) { - int32_t mapY = (int32_t)client->tileY - MAP_ROWS / 2; - int32_t mapX = (int32_t)client->tileX - MAP_COLS / 2; + int32_t mapY = (int32_t)client->tileY - MapRows / 2; + int32_t mapX = (int32_t)client->tileX - MapCols / 2; time_t now = time(NULL); struct Map map = { @@ -300,11 +300,11 @@ static bool clientMap(const struct Client *client) { }, }; - for (int32_t y = 0; y < MAP_ROWS; ++y) { - for (int32_t x = 0; x < MAP_COLS; ++x) { - uint32_t tileY = ((mapY + y) % TILE_ROWS + TILE_ROWS) % TILE_ROWS; - uint32_t tileX = ((mapX + x) % TILE_COLS + TILE_COLS) % TILE_COLS; - struct Meta meta = tiles[tileY * TILE_ROWS + tileX].meta; + for (int32_t y = 0; y < MapRows; ++y) { + for (int32_t x = 0; x < MapCols; ++x) { + uint32_t tileY = ((mapY + y) % TileRows + TileRows) % TileRows; + uint32_t tileX = ((mapX + x) % TileCols + TileCols) % TileCols; + struct Meta meta = tiles[tileY * TileRows + tileX].meta; if (meta.createTime) { if (meta.createTime < map.min.createTime) { @@ -347,7 +347,7 @@ static bool clientMap(const struct Client *client) { } } - struct ServerMessage msg = { .type = SERVER_MAP }; + struct ServerMessage msg = { .type = ServerMap }; if (!clientSend(client, msg)) return false; if (0 > send(client->fd, &map, sizeof(map), 0)) return false; return true; @@ -405,7 +405,7 @@ int main() { nevents = kevent(kq, &event, 1, NULL, 0, NULL); if (nevents < 0) err(EX_IOERR, "kevent"); - struct ServerMessage msg = { .type = SERVER_TILE }; + struct ServerMessage msg = { .type = ServerTile }; bool success = clientSend(client, msg) && clientMove(client, 0, 0) && clientCursors(client); @@ -429,16 +429,16 @@ int main() { bool success = false; switch (msg.type) { - break; case CLIENT_MOVE: { + break; case ClientMove: { success = clientMove(client, msg.move.dx, msg.move.dy); } - break; case CLIENT_FLIP: { + break; case ClientFlip: { success = clientFlip(client); } - break; case CLIENT_PUT: { + break; case ClientPut: { success = clientPut(client, msg.put.color, msg.put.cell); } - break; case CLIENT_MAP: { + break; case ClientMap: { success = clientMap(client); } } diff --git a/torus.h b/torus.h index 3b4d133..41c662b 100644 --- a/torus.h +++ b/torus.h @@ -25,25 +25,16 @@ #define ARRAY_LEN(a) (sizeof(a) / sizeof((a)[0])) -#undef COLOR_BLACK -#undef COLOR_RED -#undef COLOR_GREEN -#undef COLOR_YELLOW -#undef COLOR_BLUE -#undef COLOR_MAGENTA -#undef COLOR_CYAN -#undef COLOR_WHITE - enum { - COLOR_BLACK, - COLOR_RED, - COLOR_GREEN, - COLOR_YELLOW, - COLOR_BLUE, - COLOR_MAGENTA, - COLOR_CYAN, - COLOR_WHITE, - COLOR_BRIGHT, + ColorBlack, + ColorRed, + ColorGreen, + ColorYellow, + ColorBlue, + ColorMagenta, + ColorCyan, + ColorWhite, + ColorBright, }; static const wchar_t CP437[256] = ( @@ -66,13 +57,13 @@ static const wchar_t CP437[256] = ( ); enum { - CELL_ROWS = 24, - CELL_COLS = 80, + CellRows = 24, + CellCols = 80, }; -static const size_t CELLS_SIZE = sizeof(uint8_t[CELL_ROWS][CELL_COLS]); +static const size_t CellsSize = sizeof(uint8_t[CellRows][CellCols]); -static const uint8_t CELL_INIT_X = CELL_COLS / 2; -static const uint8_t CELL_INIT_Y = CELL_ROWS / 2; +static const uint8_t CellInitX = CellCols / 2; +static const uint8_t CellInitY = CellRows / 2; struct Meta { time_t createTime; @@ -83,40 +74,40 @@ struct Meta { }; struct Tile { - alignas(4096) uint8_t cells[CELL_ROWS][CELL_COLS]; - uint8_t colors[CELL_ROWS][CELL_COLS]; + alignas(4096) uint8_t cells[CellRows][CellCols]; + uint8_t colors[CellRows][CellCols]; struct Meta meta; }; static_assert(4096 == sizeof(struct Tile), "struct Tile is page-sized"); enum { - TILE_ROWS = 64, - TILE_COLS = 64, + TileRows = 64, + TileCols = 64, }; -static const size_t TILES_SIZE = sizeof(struct Tile[TILE_ROWS][TILE_COLS]); +static const size_t TilesSize = sizeof(struct Tile[TileRows][TileCols]); -static const uint32_t TILE_INIT_X = TILE_COLS / 2; -static const uint32_t TILE_INIT_Y = TILE_ROWS / 2; +static const uint32_t TileInitX = TileCols / 2; +static const uint32_t TileInitY = TileRows / 2; enum { - MAP_ROWS = 7, - MAP_COLS = 7, + MapRows = 7, + MapCols = 7, }; struct Map { time_t now; struct Meta min; struct Meta max; - struct Meta meta[MAP_ROWS][MAP_COLS]; + struct Meta meta[MapRows][MapCols]; }; struct ServerMessage { enum { - SERVER_TILE, - SERVER_MOVE, - SERVER_PUT, - SERVER_CURSOR, - SERVER_MAP, + ServerTile, + ServerMove, + ServerPut, + ServerCursor, + ServerMap, } type; union { struct { @@ -138,14 +129,14 @@ struct ServerMessage { }; }; -static const uint8_t CURSOR_NONE = UINT8_MAX; +static const uint8_t CursorNone = UINT8_MAX; struct ClientMessage { enum { - CLIENT_MOVE, - CLIENT_FLIP, - CLIENT_PUT, - CLIENT_MAP, + ClientMove, + ClientFlip, + ClientPut, + ClientMap, } type; union { struct { |