Browse Source

Use PascalCase for constants

June 2 months ago
parent
commit
c751cfd493
Signed by: Curtis McEnroe <june@causal.agency> GPG Key ID: CEA2F97ADCFCD77C
7 changed files with 210 additions and 219 deletions
  1. 2
    2
      Makefile
  2. 103
    103
      client.c
  3. 2
    2
      help.h
  4. 11
    11
      merge.c
  5. 2
    2
      meta.c
  6. 56
    56
      server.c
  7. 34
    43
      torus.h

+ 2
- 2
Makefile View File

@@ -17,9 +17,9 @@ client.o: help.h
17 17
 
18 18
 help.h:
19 19
 	head -c 4096 torus.dat \
20
-		| file2c -s -x 'static const uint8_t HELP_DATA[] = {' '};' \
20
+		| file2c -s -x 'static const uint8_t HelpData[] = {' '};' \
21 21
 		> help.h
22
-	echo 'static const struct Tile *HELP = (const struct Tile *)HELP_DATA;' \
22
+	echo 'static const struct Tile *Help = (const struct Tile *)HelpData;' \
23 23
 		>> help.h
24 24
 
25 25
 tags: *.h *.c

+ 103
- 103
client.c View File

@@ -42,8 +42,8 @@
42 42
 
43 43
 #define CTRL(ch) ((ch) ^ 0x40)
44 44
 enum {
45
-	ESC = 0x1B,
46
-	DEL = 0x7F,
45
+	Esc = 0x1B,
46
+	Del = 0x7F,
47 47
 };
48 48
 
49 49
 static uint32_t log2(uint32_t n) {
@@ -62,13 +62,13 @@ static void curse(void) {
62 62
 		fprintf(stderr, "If you think it should, check TERM.\n");
63 63
 		exit(EX_CONFIG);
64 64
 	}
65
-	if (LINES < CELL_ROWS || COLS < CELL_COLS) {
65
+	if (LINES < CellRows || COLS < CellCols) {
66 66
 		endwin();
67 67
 		fprintf(
68 68
 			stderr,
69 69
 			"Sorry, your terminal is too small!\n"
70 70
 			"It must be at least %ux%u characters.\n",
71
-			CELL_COLS, CELL_ROWS
71
+			CellCols, CellRows
72 72
 		);
73 73
 		exit(EX_CONFIG);
74 74
 	}
@@ -84,12 +84,12 @@ static void curse(void) {
84 84
 		}
85 85
 	}
86 86
 
87
-	color_set(COLOR_WHITE, NULL);
88
-	bool hline = (LINES > CELL_ROWS);
89
-	bool vline = (COLS > CELL_COLS);
90
-	if (hline) mvhline(CELL_ROWS, 0, 0, CELL_COLS);
91
-	if (vline) mvvline(0, CELL_COLS, 0, CELL_ROWS);
92
-	if (hline && vline) mvaddch(CELL_ROWS, CELL_COLS, ACS_LRCORNER);
87
+	color_set(ColorWhite, NULL);
88
+	bool hline = (LINES > CellRows);
89
+	bool vline = (COLS > CellCols);
90
+	if (hline) mvhline(CellRows, 0, 0, CellCols);
91
+	if (vline) mvvline(0, CellCols, 0, CellRows);
92
+	if (hline && vline) mvaddch(CellRows, CellCols, ACS_LRCORNER);
93 93
 	color_set(0, NULL);
94 94
 
95 95
 	cbreak();
@@ -100,7 +100,7 @@ static void curse(void) {
100 100
 
101 101
 static attr_t colorAttr(uint8_t color) {
102 102
 	if (COLORS >= 16) return A_NORMAL;
103
-	return (color & COLOR_BRIGHT) ? A_BOLD : A_NORMAL;
103
+	return (color & ColorBright) ? A_BOLD : A_NORMAL;
104 104
 }
105 105
 static short colorPair(uint8_t color) {
106 106
 	if (COLORS >= 16) return color;
@@ -120,8 +120,8 @@ static void drawCell(
120 120
 }
121 121
 
122 122
 static void drawTile(const struct Tile *tile) {
123
-	for (uint8_t cellY = 0; cellY < CELL_ROWS; ++cellY) {
124
-		for (uint8_t cellX = 0; cellX < CELL_COLS; ++cellX) {
123
+	for (uint8_t cellY = 0; cellY < CellRows; ++cellY) {
124
+		for (uint8_t cellX = 0; cellX < CellCols; ++cellX) {
125 125
 			drawCell(tile, cellX, cellY, A_NORMAL);
126 126
 		}
127 127
 	}
@@ -152,32 +152,32 @@ static void serverPut(struct ServerMessage msg) {
152 152
 }
153 153
 
154 154
 static void serverCursor(struct ServerMessage msg) {
155
-	if (msg.cursor.oldCellX != CURSOR_NONE) {
155
+	if (msg.cursor.oldCellX != CursorNone) {
156 156
 		drawCell(&tile, msg.cursor.oldCellX, msg.cursor.oldCellY, A_NORMAL);
157 157
 	}
158
-	if (msg.cursor.newCellX != CURSOR_NONE) {
158
+	if (msg.cursor.newCellX != CursorNone) {
159 159
 		drawCell(&tile, msg.cursor.newCellX, msg.cursor.newCellY, A_REVERSE);
160 160
 	}
161 161
 }
162 162
 
163
-static const uint8_t MAP_X = (CELL_COLS / 2) - (3 * MAP_COLS / 2);
164
-static const uint8_t MAP_Y = (CELL_ROWS / 2) - (MAP_ROWS / 2);
163
+static const uint8_t MapX = (CellCols / 2) - (3 * MapCols / 2);
164
+static const uint8_t MapY = (CellRows / 2) - (MapRows / 2);
165 165
 
166
-static const wchar_t MAP_CELLS[5] = L" ░▒▓█";
167
-static const uint8_t MAP_COLORS[] = {
168
-	COLOR_BLUE, COLOR_CYAN, COLOR_GREEN, COLOR_YELLOW, COLOR_RED,
166
+static const wchar_t MapCells[5] = L" ░▒▓█";
167
+static const uint8_t MapColors[] = {
168
+	ColorBlue, ColorCyan, ColorGreen, ColorYellow, ColorRed,
169 169
 };
170 170
 
171 171
 static void serverMap(void) {
172
-	int t = MAP_Y - 1;
173
-	int l = MAP_X - 1;
174
-	int b = MAP_Y + MAP_ROWS;
175
-	int r = MAP_X + 3 * MAP_COLS;
176
-	color_set(colorPair(COLOR_WHITE), NULL);
177
-	mvhline(t, MAP_X, ACS_HLINE, 3 * MAP_COLS);
178
-	mvhline(b, MAP_X, ACS_HLINE, 3 * MAP_COLS);
179
-	mvvline(MAP_Y, l, ACS_VLINE, MAP_ROWS);
180
-	mvvline(MAP_Y, r, ACS_VLINE, MAP_ROWS);
172
+	int t = MapY - 1;
173
+	int l = MapX - 1;
174
+	int b = MapY + MapRows;
175
+	int r = MapX + 3 * MapCols;
176
+	color_set(colorPair(ColorWhite), NULL);
177
+	mvhline(t, MapX, ACS_HLINE, 3 * MapCols);
178
+	mvhline(b, MapX, ACS_HLINE, 3 * MapCols);
179
+	mvvline(MapY, l, ACS_VLINE, MapRows);
180
+	mvvline(MapY, r, ACS_VLINE, MapRows);
181 181
 	mvaddch(t, l, ACS_ULCORNER);
182 182
 	mvaddch(t, r, ACS_URCORNER);
183 183
 	mvaddch(b, l, ACS_LLCORNER);
@@ -192,14 +192,14 @@ static void serverMap(void) {
192 192
 	if (0 == map.max.modifyCount) return;
193 193
 	if (0 == map.now - map.min.createTime) return;
194 194
 
195
-	for (uint8_t y = 0; y < MAP_ROWS; ++y) {
196
-		for (uint8_t x = 0; x < MAP_COLS; ++x) {
195
+	for (uint8_t y = 0; y < MapRows; ++y) {
196
+		for (uint8_t x = 0; x < MapCols; ++x) {
197 197
 			struct Meta meta = map.meta[y][x];
198 198
 
199 199
 			uint32_t count = 0;
200 200
 			if (meta.modifyCount && log2(map.max.modifyCount)) {
201 201
 				count = DIV_ROUND(
202
-					(ARRAY_LEN(MAP_CELLS) - 1) * log2(meta.modifyCount),
202
+					(ARRAY_LEN(MapCells) - 1) * log2(meta.modifyCount),
203 203
 					log2(map.max.modifyCount)
204 204
 				);
205 205
 			}
@@ -207,16 +207,16 @@ static void serverMap(void) {
207 207
 			if (meta.modifyTime) {
208 208
 				uint32_t modify = meta.modifyTime - map.min.createTime;
209 209
 				time = DIV_ROUND(
210
-					(ARRAY_LEN(MAP_COLORS) - 1) * modify,
210
+					(ARRAY_LEN(MapColors) - 1) * modify,
211 211
 					map.now - map.min.createTime
212 212
 				);
213 213
 			}
214 214
 
215
-			wchar_t cell = MAP_CELLS[count];
216
-			uint8_t color = MAP_COLORS[time];
215
+			wchar_t cell = MapCells[count];
216
+			uint8_t color = MapColors[time];
217 217
 			wchar_t tile[] = { cell, cell, cell, L'\0' };
218 218
 			attr_set(colorAttr(color), colorPair(color), NULL);
219
-			mvaddwstr(MAP_Y + y, MAP_X + 3 * x, tile);
219
+			mvaddwstr(MapY + y, MapX + 3 * x, tile);
220 220
 		}
221 221
 	}
222 222
 	attr_set(A_NORMAL, 0, NULL);
@@ -229,11 +229,11 @@ static void readMessage(void) {
229 229
 	if ((size_t)size < sizeof(msg)) errx(EX_PROTOCOL, "truncated message");
230 230
 
231 231
 	switch (msg.type) {
232
-		break; case SERVER_TILE:   serverTile();
233
-		break; case SERVER_MOVE:   serverMove(msg);
234
-		break; case SERVER_PUT:    serverPut(msg);
235
-		break; case SERVER_CURSOR: serverCursor(msg);
236
-		break; case SERVER_MAP:    serverMap();
232
+		break; case ServerTile:   serverTile();
233
+		break; case ServerMove:   serverMove(msg);
234
+		break; case ServerPut:    serverPut(msg);
235
+		break; case ServerCursor: serverCursor(msg);
236
+		break; case ServerMap:    serverMap();
237 237
 		break; default: errx(EX_PROTOCOL, "unknown message type %d", msg.type);
238 238
 	}
239 239
 	move(cellY, cellX);
@@ -246,46 +246,46 @@ static void clientMessage(struct ClientMessage msg) {
246 246
 
247 247
 static void clientMove(int8_t dx, int8_t dy) {
248 248
 	struct ClientMessage msg = {
249
-		.type = CLIENT_MOVE,
249
+		.type = ClientMove,
250 250
 		.move = { .dx = dx, .dy = dy },
251 251
 	};
252 252
 	clientMessage(msg);
253 253
 }
254 254
 
255 255
 static void clientFlip(void) {
256
-	struct ClientMessage msg = { .type = CLIENT_FLIP };
256
+	struct ClientMessage msg = { .type = ClientFlip };
257 257
 	clientMessage(msg);
258 258
 }
259 259
 
260 260
 static void clientPut(uint8_t color, uint8_t cell) {
261 261
 	struct ClientMessage msg = {
262
-		.type = CLIENT_PUT,
262
+		.type = ClientPut,
263 263
 		.put = { .color = color, .cell = cell },
264 264
 	};
265 265
 	clientMessage(msg);
266 266
 }
267 267
 
268 268
 static void clientMap(void) {
269
-	struct ClientMessage msg = { .type = CLIENT_MAP };
269
+	struct ClientMessage msg = { .type = ClientMap };
270 270
 	clientMessage(msg);
271 271
 }
272 272
 
273 273
 static struct {
274 274
 	enum {
275
-		MODE_NORMAL,
276
-		MODE_HELP,
277
-		MODE_MAP,
278
-		MODE_DIRECTION,
279
-		MODE_INSERT,
280
-		MODE_REPLACE,
281
-		MODE_DRAW,
282
-		MODE_LINE,
275
+		ModeNormal,
276
+		ModeHelp,
277
+		ModeMap,
278
+		ModeDirection,
279
+		ModeInsert,
280
+		ModeReplace,
281
+		ModeDraw,
282
+		ModeLine,
283 283
 	} mode;
284 284
 	uint8_t color;
285 285
 	uint8_t shift;
286 286
 	uint8_t draw;
287 287
 } input = {
288
-	.color = COLOR_WHITE,
288
+	.color = ColorWhite,
289 289
 };
290 290
 
291 291
 static struct {
@@ -302,36 +302,36 @@ static struct {
302 302
 static void modeNormal(void) {
303 303
 	curs_set(1);
304 304
 	move(cellY, cellX);
305
-	input.mode = MODE_NORMAL;
305
+	input.mode = ModeNormal;
306 306
 }
307 307
 static void modeHelp(void) {
308 308
 	curs_set(0);
309
-	drawTile(HELP);
310
-	input.mode = MODE_HELP;
309
+	drawTile(Help);
310
+	input.mode = ModeHelp;
311 311
 }
312 312
 static void modeMap(void) {
313 313
 	curs_set(0);
314 314
 	clientMap();
315
-	input.mode = MODE_MAP;
315
+	input.mode = ModeMap;
316 316
 }
317 317
 static void modeDirection(void) {
318
-	input.mode = MODE_DIRECTION;
318
+	input.mode = ModeDirection;
319 319
 }
320 320
 static void modeInsert(int8_t dx, int8_t dy) {
321 321
 	insert.dx = dx;
322 322
 	insert.dy = dy;
323 323
 	insert.len = 0;
324
-	input.mode = MODE_INSERT;
324
+	input.mode = ModeInsert;
325 325
 }
326 326
 static void modeReplace(void) {
327
-	input.mode = MODE_REPLACE;
327
+	input.mode = ModeReplace;
328 328
 }
329 329
 static void modeDraw(void) {
330 330
 	input.draw = 0;
331
-	input.mode = MODE_DRAW;
331
+	input.mode = ModeDraw;
332 332
 }
333 333
 static void modeLine(void) {
334
-	input.mode = MODE_LINE;
334
+	input.mode = ModeLine;
335 335
 }
336 336
 
337 337
 static void colorFg(uint8_t fg) {
@@ -353,8 +353,8 @@ static void cellCopy(void) {
353 353
 }
354 354
 
355 355
 static void cellSwap(int8_t dx, int8_t dy) {
356
-	if ((uint8_t)(cellX + dx) >= CELL_COLS) return;
357
-	if ((uint8_t)(cellY + dy) >= CELL_ROWS) return;
356
+	if ((uint8_t)(cellX + dx) >= CellCols) return;
357
+	if ((uint8_t)(cellY + dy) >= CellRows) return;
358 358
 
359 359
 	uint8_t aColor = tile.colors[cellY][cellX];
360 360
 	uint8_t aCell = tile.cells[cellY][cellX];
@@ -396,7 +396,7 @@ static void inputNormal(bool keyCode, wchar_t ch) {
396 396
 	switch (ch) {
397 397
 		break; case CTRL('L'): clearok(curscr, true);
398 398
 
399
-		break; case ESC: modeNormal(); input.shift = 0;
399
+		break; case Esc: modeNormal(); input.shift = 0;
400 400
 		break; case 'q': endwin(); exit(EX_OK);
401 401
 
402 402
 		break; case 'g': clientFlip();
@@ -409,25 +409,25 @@ static void inputNormal(bool keyCode, wchar_t ch) {
409 409
 		break; case 'b': clientMove(-1,  1);
410 410
 		break; case 'n': clientMove( 1,  1);
411 411
 
412
-		break; case '0': colorFg(COLOR_BLACK);
413
-		break; case '1': colorFg(COLOR_RED);
414
-		break; case '2': colorFg(COLOR_GREEN);
415
-		break; case '3': colorFg(COLOR_YELLOW);
416
-		break; case '4': colorFg(COLOR_BLUE);
417
-		break; case '5': colorFg(COLOR_MAGENTA);
418
-		break; case '6': colorFg(COLOR_CYAN);
419
-		break; case '7': colorFg(COLOR_WHITE);
420
-
421
-		break; case ')': colorBg(COLOR_BLACK);
422
-		break; case '!': colorBg(COLOR_RED);
423
-		break; case '@': colorBg(COLOR_GREEN);
424
-		break; case '#': colorBg(COLOR_YELLOW);
425
-		break; case '$': colorBg(COLOR_BLUE);
426
-		break; case '%': colorBg(COLOR_MAGENTA);
427
-		break; case '^': colorBg(COLOR_CYAN);
428
-		break; case '&': colorBg(COLOR_WHITE);
429
-
430
-		break; case '8': input.color ^= COLOR_BRIGHT;
412
+		break; case '0': colorFg(ColorBlack);
413
+		break; case '1': colorFg(ColorRed);
414
+		break; case '2': colorFg(ColorGreen);
415
+		break; case '3': colorFg(ColorYellow);
416
+		break; case '4': colorFg(ColorBlue);
417
+		break; case '5': colorFg(ColorMagenta);
418
+		break; case '6': colorFg(ColorCyan);
419
+		break; case '7': colorFg(ColorWhite);
420
+
421
+		break; case ')': colorBg(ColorBlack);
422
+		break; case '!': colorBg(ColorRed);
423
+		break; case '@': colorBg(ColorGreen);
424
+		break; case '#': colorBg(ColorYellow);
425
+		break; case '$': colorBg(ColorBlue);
426
+		break; case '%': colorBg(ColorMagenta);
427
+		break; case '^': colorBg(ColorCyan);
428
+		break; case '&': colorBg(ColorWhite);
429
+
430
+		break; case '8': input.color ^= ColorBright;
431 431
 		break; case '9': input.color = colorInvert(input.color);
432 432
 		break; case '`': input.color = tile.colors[cellY][cellX];
433 433
 
@@ -451,7 +451,7 @@ static void inputNormal(bool keyCode, wchar_t ch) {
451 451
 		}
452 452
 		break; case '*': {
453 453
 			clientPut(
454
-				tile.colors[cellY][cellX] ^ COLOR_BRIGHT,
454
+				tile.colors[cellY][cellX] ^ ColorBright,
455 455
 				tile.cells[cellY][cellX]
456 456
 			);
457 457
 			clientMove(1, 0);
@@ -499,7 +499,7 @@ static void inputMap(bool keyCode, wchar_t ch) {
499 499
 static void inputDirection(bool keyCode, wchar_t ch) {
500 500
 	if (keyCode) return;
501 501
 	switch (ch) {
502
-		break; case ESC: modeNormal();
502
+		break; case Esc: modeNormal();
503 503
 		break; case 'h': modeInsert(-1,  0);
504 504
 		break; case 'l': modeInsert( 1,  0);
505 505
 		break; case 'k': modeInsert( 0, -1);
@@ -517,11 +517,11 @@ static void inputInsert(bool keyCode, wchar_t ch) {
517 517
 		return;
518 518
 	}
519 519
 	switch (ch) {
520
-		break; case ESC: {
520
+		break; case Esc: {
521 521
 			clientMove(-insert.dx, -insert.dy);
522 522
 			modeNormal();
523 523
 		}
524
-		break; case '\b': case DEL: {
524
+		break; case '\b': case Del: {
525 525
 			clientMove(-insert.dx, -insert.dy);
526 526
 			clientPut(input.color, ' ');
527 527
 			insert.len--;
@@ -546,7 +546,7 @@ static void inputReplace(bool keyCode, wchar_t ch) {
546 546
 		inputNormal(keyCode, ch);
547 547
 		return;
548 548
 	}
549
-	if (ch != ESC) {
549
+	if (ch != Esc) {
550 550
 		uint8_t cell = inputCell(ch);
551 551
 		if (!cell) return;
552 552
 		clientPut(tile.colors[cellY][cellX], cell);
@@ -555,7 +555,7 @@ static void inputReplace(bool keyCode, wchar_t ch) {
555 555
 }
556 556
 
557 557
 static void inputDraw(bool keyCode, wchar_t ch) {
558
-	if (!keyCode && ch == ESC) {
558
+	if (!keyCode && ch == Esc) {
559 559
 		modeNormal();
560 560
 		return;
561 561
 	}
@@ -633,7 +633,7 @@ static void inputLine(bool keyCode, wchar_t ch) {
633 633
 		}
634 634
 	} else {
635 635
 		switch (ch) {
636
-			break; case ESC: case '.': modeNormal(); return;
636
+			break; case Esc: case '.': modeNormal(); return;
637 637
 			break; case 'h': dx = -1;
638 638
 			break; case 'l': dx =  1;
639 639
 			break; case 'k': dy = -1;
@@ -641,8 +641,8 @@ static void inputLine(bool keyCode, wchar_t ch) {
641 641
 			break; default: return;
642 642
 		}
643 643
 	}
644
-	if ((uint8_t)(cellX + dx) >= CELL_COLS) return;
645
-	if ((uint8_t)(cellY + dy) >= CELL_ROWS) return;
644
+	if ((uint8_t)(cellX + dx) >= CellCols) return;
645
+	if ((uint8_t)(cellY + dy) >= CellRows) return;
646 646
 
647 647
 	uint8_t leave = lineCell(tile.cells[cellY][cellX], dx, dy);
648 648
 	uint8_t enter = lineCell(tile.cells[cellY + dy][cellX + dx], -dx, -dy);
@@ -656,14 +656,14 @@ static void readInput(void) {
656 656
 	wint_t ch;
657 657
 	bool keyCode = (KEY_CODE_YES == get_wch(&ch));
658 658
 	switch (input.mode) {
659
-		break; case MODE_NORMAL:    inputNormal(keyCode, ch);
660
-		break; case MODE_HELP:      inputHelp(keyCode, ch);
661
-		break; case MODE_MAP:       inputMap(keyCode, ch);
662
-		break; case MODE_DIRECTION: inputDirection(keyCode, ch);
663
-		break; case MODE_INSERT:    inputInsert(keyCode, ch);
664
-		break; case MODE_REPLACE:   inputReplace(keyCode, ch);
665
-		break; case MODE_DRAW:      inputDraw(keyCode, ch);
666
-		break; case MODE_LINE:      inputLine(keyCode, ch);
659
+		break; case ModeNormal:    inputNormal(keyCode, ch);
660
+		break; case ModeHelp:      inputHelp(keyCode, ch);
661
+		break; case ModeMap:       inputMap(keyCode, ch);
662
+		break; case ModeDirection: inputDirection(keyCode, ch);
663
+		break; case ModeInsert:    inputInsert(keyCode, ch);
664
+		break; case ModeReplace:   inputReplace(keyCode, ch);
665
+		break; case ModeDraw:      inputDraw(keyCode, ch);
666
+		break; case ModeLine:      inputLine(keyCode, ch);
667 667
 	}
668 668
 }
669 669
 
@@ -671,7 +671,7 @@ int main(int argc, char *argv[]) {
671 671
 	int opt;
672 672
 	while (0 < (opt = getopt(argc, argv, "h"))) {
673 673
 		if (opt == 'h') {
674
-			fwrite(HELP_DATA, sizeof(HELP_DATA), 1, stdout);
674
+			fwrite(HelpData, sizeof(HelpData), 1, stdout);
675 675
 			return EX_OK;
676 676
 		} else {
677 677
 			return EX_USAGE;

+ 2
- 2
help.h View File

@@ -1,4 +1,4 @@
1
-static const uint8_t HELP_DATA[] = {
1
+static const uint8_t HelpData[] = {
2 2
 	0xda, 0xc4, 0xc4, 0xc4, 0xc4, 0xc4, 0xc4, 0xc4, 0xc4, 0xc4, 0xc4,
3 3
 	0xc4, 0xc4, 0xc4, 0xc4, 0xc4, 0xc4, 0xc4, 0xc4, 0xc4, 0xc4, 0xc4,
4 4
 	0xc4, 0xc4, 0xc4, 0xc4, 0xc4, 0xc4, 0xc4, 0xc4, 0xc4, 0xc4, 0xc4,
@@ -373,4 +373,4 @@ static const uint8_t HELP_DATA[] = {
373 373
 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
374 374
 	0x00, 0x00, 0x00, 0x00
375 375
 };
376
-static const struct Tile *HELP = (const struct Tile *)HELP_DATA;
376
+static const struct Tile *Help = (const struct Tile *)HelpData;

+ 11
- 11
merge.c View File

@@ -43,12 +43,12 @@ static void curse(void) {
43 43
 		}
44 44
 	}
45 45
 
46
-	color_set(COLOR_WHITE, NULL);
47
-	mvhline(CELL_ROWS, 0, 0, CELL_COLS);
48
-	mvhline(CELL_ROWS * 2 + 1, 0, 0, CELL_COLS);
49
-	mvvline(0, CELL_COLS, 0, CELL_ROWS * 2 + 1);
50
-	mvaddch(CELL_ROWS, CELL_COLS, ACS_RTEE);
51
-	mvaddch(CELL_ROWS * 2 + 1, CELL_COLS, ACS_LRCORNER);
46
+	color_set(ColorWhite, NULL);
47
+	mvhline(CellRows, 0, 0, CellCols);
48
+	mvhline(CellRows * 2 + 1, 0, 0, CellCols);
49
+	mvvline(0, CellCols, 0, CellRows * 2 + 1);
50
+	mvaddch(CellRows, CellCols, ACS_RTEE);
51
+	mvaddch(CellRows * 2 + 1, CellCols, ACS_LRCORNER);
52 52
 	color_set(0, NULL);
53 53
 
54 54
 	cbreak();
@@ -59,7 +59,7 @@ static void curse(void) {
59 59
 
60 60
 static attr_t colorAttr(uint8_t color) {
61 61
 	if (COLORS >= 16) return A_NORMAL;
62
-	return (color & COLOR_BRIGHT) ? A_BOLD : A_NORMAL;
62
+	return (color & ColorBright) ? A_BOLD : A_NORMAL;
63 63
 }
64 64
 static short colorPair(uint8_t color) {
65 65
 	if (COLORS >= 16) return color;
@@ -67,8 +67,8 @@ static short colorPair(uint8_t color) {
67 67
 }
68 68
 
69 69
 static void drawTile(int offsetY, const struct Tile *tile) {
70
-	for (uint8_t cellY = 0; cellY < CELL_ROWS; ++cellY) {
71
-		for (uint8_t cellX = 0; cellX < CELL_COLS; ++cellX) {
70
+	for (uint8_t cellY = 0; cellY < CellRows; ++cellY) {
71
+		for (uint8_t cellX = 0; cellX < CellCols; ++cellX) {
72 72
 			uint8_t color = tile->colors[cellY][cellX];
73 73
 			uint8_t cell = tile->cells[cellY][cellX];
74 74
 
@@ -111,8 +111,8 @@ int main(int argc, char *argv[]) {
111 111
 
112 112
 		if (tileA.meta.modifyTime != tileB.meta.modifyTime) {
113 113
 			drawTile(0, &tileA);
114
-			drawTile(CELL_ROWS + 1, &tileB);
115
-			move(CELL_ROWS * 2 + 2, 0);
114
+			drawTile(CellRows + 1, &tileB);
115
+			move(CellRows * 2 + 2, 0);
116 116
 			refresh();
117 117
 
118 118
 			int c;

+ 2
- 2
meta.c View File

@@ -30,8 +30,8 @@ int main() {
30 30
 
31 31
 		printf(
32 32
 			"%d,%d,%jd,%u,%jd,%u,%jd\n",
33
-			i % TILE_COLS,
34
-			i / TILE_COLS,
33
+			i % TileCols,
34
+			i / TileCols,
35 35
 			tile.meta.createTime,
36 36
 			tile.meta.modifyCount,
37 37
 			tile.meta.modifyTime,

+ 56
- 56
server.c View File

@@ -41,26 +41,26 @@ static void tilesMap(void) {
41 41
 	int fd = open("torus.dat", O_CREAT | O_RDWR, 0644);
42 42
 	if (fd < 0) err(EX_CANTCREAT, "torus.dat");
43 43
 
44
-	int error = ftruncate(fd, TILES_SIZE);
44
+	int error = ftruncate(fd, TilesSize);
45 45
 	if (error) err(EX_IOERR, "ftruncate");
46 46
 
47
-	tiles = mmap(NULL, TILES_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
47
+	tiles = mmap(NULL, TilesSize, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
48 48
 	if (tiles == MAP_FAILED) err(EX_OSERR, "mmap");
49 49
 
50
-	error = madvise(tiles, TILES_SIZE, MADV_RANDOM);
50
+	error = madvise(tiles, TilesSize, MADV_RANDOM);
51 51
 	if (error) err(EX_OSERR, "madvise");
52 52
 
53 53
 #ifdef MADV_NOCORE
54
-	error = madvise(tiles, TILES_SIZE, MADV_NOCORE);
54
+	error = madvise(tiles, TilesSize, MADV_NOCORE);
55 55
 	if (error) err(EX_OSERR, "madvise");
56 56
 #endif
57 57
 }
58 58
 
59 59
 static struct Tile *tileGet(uint32_t tileX, uint32_t tileY) {
60
-	struct Tile *tile = &tiles[tileY * TILE_ROWS + tileX];
60
+	struct Tile *tile = &tiles[tileY * TileRows + tileX];
61 61
 	if (!tile->meta.createTime) {
62
-		memset(tile->cells, ' ', CELLS_SIZE);
63
-		memset(tile->colors, COLOR_WHITE, CELLS_SIZE);
62
+		memset(tile->cells, ' ', CellsSize);
63
+		memset(tile->colors, ColorWhite, CellsSize);
64 64
 		tile->meta.createTime = time(NULL);
65 65
 	}
66 66
 	return tile;
@@ -97,10 +97,10 @@ static struct Client *clientAdd(int fd) {
97 97
 	if (!client) err(EX_OSERR, "malloc");
98 98
 
99 99
 	client->fd = fd;
100
-	client->tileX = TILE_INIT_X;
101
-	client->tileY = TILE_INIT_Y;
102
-	client->cellX = CELL_INIT_X;
103
-	client->cellY = CELL_INIT_Y;
100
+	client->tileX = TileInitX;
101
+	client->tileY = TileInitY;
102
+	client->cellX = CellInitX;
103
+	client->cellY = CellInitY;
104 104
 
105 105
 	client->prev = NULL;
106 106
 	if (clientHead) {
@@ -118,7 +118,7 @@ static bool clientSend(const struct Client *client, struct ServerMessage msg) {
118 118
 	ssize_t size = send(client->fd, &msg, sizeof(msg), 0);
119 119
 	if (size < 0) return false;
120 120
 
121
-	if (msg.type == SERVER_TILE) {
121
+	if (msg.type == ServerTile) {
122 122
 		struct Tile *tile = tileAccess(client->tileX, client->tileY);
123 123
 		size = send(client->fd, tile, sizeof(*tile), 0);
124 124
 		if (size < 0) return false;
@@ -142,10 +142,10 @@ static void clientRemove(struct Client *client) {
142 142
 	if (clientHead == client) clientHead = client->next;
143 143
 
144 144
 	struct ServerMessage msg = {
145
-		.type = SERVER_CURSOR,
145
+		.type = ServerCursor,
146 146
 		.cursor = {
147 147
 			.oldCellX = client->cellX, .oldCellY = client->cellY,
148
-			.newCellX = CURSOR_NONE,   .newCellY = CURSOR_NONE,
148
+			.newCellX = CursorNone,    .newCellY = CursorNone,
149 149
 		},
150 150
 	};
151 151
 	clientCast(client, msg);
@@ -156,8 +156,8 @@ static void clientRemove(struct Client *client) {
156 156
 
157 157
 static bool clientCursors(const struct Client *client) {
158 158
 	struct ServerMessage msg = {
159
-		.type = SERVER_CURSOR,
160
-		.cursor = { .oldCellX = CURSOR_NONE, .oldCellY = CURSOR_NONE },
159
+		.type = ServerCursor,
160
+		.cursor = { .oldCellX = CursorNone, .oldCellY = CursorNone },
161 161
 	};
162 162
 
163 163
 	for (struct Client *friend = clientHead; friend; friend = friend->next) {
@@ -174,30 +174,30 @@ static bool clientCursors(const struct Client *client) {
174 174
 
175 175
 static bool clientUpdate(struct Client *client, const struct Client *old) {
176 176
 	struct ServerMessage msg = {
177
-		.type = SERVER_MOVE,
177
+		.type = ServerMove,
178 178
 		.move = { .cellX = client->cellX, .cellY = client->cellY },
179 179
 	};
180 180
 	if (!clientSend(client, msg)) return false;
181 181
 
182 182
 	if (client->tileX != old->tileX || client->tileY != old->tileY) {
183
-		msg.type = SERVER_TILE;
183
+		msg.type = ServerTile;
184 184
 		if (!clientSend(client, msg)) return false;
185 185
 
186 186
 		if (!clientCursors(client)) return false;
187 187
 
188 188
 		msg = (struct ServerMessage) {
189
-			.type = SERVER_CURSOR,
189
+			.type = ServerCursor,
190 190
 			.cursor = {
191
-				.oldCellX = old->cellX,  .oldCellY = old->cellY,
192
-				.newCellX = CURSOR_NONE, .newCellY = CURSOR_NONE,
191
+				.oldCellX = old->cellX, .oldCellY = old->cellY,
192
+				.newCellX = CursorNone, .newCellY = CursorNone,
193 193
 			},
194 194
 		};
195 195
 		clientCast(old, msg);
196 196
 
197 197
 		msg = (struct ServerMessage) {
198
-			.type = SERVER_CURSOR,
198
+			.type = ServerCursor,
199 199
 			.cursor = {
200
-				.oldCellX = CURSOR_NONE,   .oldCellY = CURSOR_NONE,
200
+				.oldCellX = CursorNone,    .oldCellY = CursorNone,
201 201
 				.newCellX = client->cellX, .newCellY = client->cellY,
202 202
 			},
203 203
 		};
@@ -205,7 +205,7 @@ static bool clientUpdate(struct Client *client, const struct Client *old) {
205 205
 
206 206
 	} else {
207 207
 		msg = (struct ServerMessage) {
208
-			.type = SERVER_CURSOR,
208
+			.type = ServerCursor,
209 209
 			.cursor = {
210 210
 				.oldCellX = old->cellX,    .oldCellY = old->cellY,
211 211
 				.newCellX = client->cellX, .newCellY = client->cellY,
@@ -220,48 +220,48 @@ static bool clientUpdate(struct Client *client, const struct Client *old) {
220 220
 static bool clientMove(struct Client *client, int8_t dx, int8_t dy) {
221 221
 	struct Client old = *client;
222 222
 
223
-	if (dx > CELL_COLS - client->cellX) dx = CELL_COLS - client->cellX;
224
-	if (dx < -client->cellX - 1)        dx = -client->cellX - 1;
225
-	if (dy > CELL_ROWS - client->cellY) dy = CELL_ROWS - client->cellY;
226
-	if (dy < -client->cellY - 1)        dy = -client->cellY - 1;
223
+	if (dx > CellCols - client->cellX) dx = CellCols - client->cellX;
224
+	if (dx < -client->cellX - 1)       dx = -client->cellX - 1;
225
+	if (dy > CellRows - client->cellY) dy = CellRows - client->cellY;
226
+	if (dy < -client->cellY - 1)       dy = -client->cellY - 1;
227 227
 
228 228
 	client->cellX += dx;
229 229
 	client->cellY += dy;
230 230
 
231
-	if (client->cellX == CELL_COLS) {
231
+	if (client->cellX == CellCols) {
232 232
 		client->tileX++;
233 233
 		client->cellX = 0;
234 234
 	}
235 235
 	if (client->cellX == UINT8_MAX) {
236 236
 		client->tileX--;
237
-		client->cellX = CELL_COLS - 1;
237
+		client->cellX = CellCols - 1;
238 238
 	}
239
-	if (client->cellY == CELL_ROWS) {
239
+	if (client->cellY == CellRows) {
240 240
 		client->tileY++;
241 241
 		client->cellY = 0;
242 242
 	}
243 243
 	if (client->cellY == UINT8_MAX) {
244 244
 		client->tileY--;
245
-		client->cellY = CELL_ROWS - 1;
245
+		client->cellY = CellRows - 1;
246 246
 	}
247 247
 
248
-	if (client->tileX == TILE_COLS)  client->tileX = 0;
249
-	if (client->tileX == UINT32_MAX) client->tileX = TILE_COLS - 1;
250
-	if (client->tileY == TILE_ROWS)  client->tileY = 0;
251
-	if (client->tileY == UINT32_MAX) client->tileY = TILE_ROWS - 1;
248
+	if (client->tileX == TileCols)  client->tileX = 0;
249
+	if (client->tileX == UINT32_MAX) client->tileX = TileCols - 1;
250
+	if (client->tileY == TileRows)  client->tileY = 0;
251
+	if (client->tileY == UINT32_MAX) client->tileY = TileRows - 1;
252 252
 
253
-	assert(client->cellX < CELL_COLS);
254
-	assert(client->cellY < CELL_ROWS);
255
-	assert(client->tileX < TILE_COLS);
256
-	assert(client->tileY < TILE_ROWS);
253
+	assert(client->cellX < CellCols);
254
+	assert(client->cellY < CellRows);
255
+	assert(client->tileX < TileCols);
256
+	assert(client->tileY < TileRows);
257 257
 
258 258
 	return clientUpdate(client, &old);
259 259
 }
260 260
 
261 261
 static bool clientFlip(struct Client *client) {
262 262
 	struct Client old = *client;
263
-	client->tileX = (client->tileX + TILE_COLS / 2) % TILE_COLS;
264
-	client->tileY = (client->tileY + TILE_ROWS / 2) % TILE_ROWS;
263
+	client->tileX = (client->tileX + TileCols / 2) % TileCols;
264
+	client->tileY = (client->tileY + TileRows / 2) % TileRows;
265 265
 	return clientUpdate(client, &old);
266 266
 }
267 267
 
@@ -271,7 +271,7 @@ static bool clientPut(const struct Client *client, uint8_t color, uint8_t cell)
271 271
 	tile->cells[client->cellY][client->cellX] = cell;
272 272
 
273 273
 	struct ServerMessage msg = {
274
-		.type = SERVER_PUT,
274
+		.type = ServerPut,
275 275
 		.put = {
276 276
 			.cellX = client->cellX,
277 277
 			.cellY = client->cellY,
@@ -285,8 +285,8 @@ static bool clientPut(const struct Client *client, uint8_t color, uint8_t cell)
285 285
 }
286 286
 
287 287
 static bool clientMap(const struct Client *client) {
288
-	int32_t mapY = (int32_t)client->tileY - MAP_ROWS / 2;
289
-	int32_t mapX = (int32_t)client->tileX - MAP_COLS / 2;
288
+	int32_t mapY = (int32_t)client->tileY - MapRows / 2;
289
+	int32_t mapX = (int32_t)client->tileX - MapCols / 2;
290 290
 
291 291
 	time_t now = time(NULL);
292 292
 	struct Map map = {
@@ -300,11 +300,11 @@ static bool clientMap(const struct Client *client) {
300 300
 		},
301 301
 	};
302 302
 
303
-	for (int32_t y = 0; y < MAP_ROWS; ++y) {
304
-		for (int32_t x = 0; x < MAP_COLS; ++x) {
305
-			uint32_t tileY = ((mapY + y) % TILE_ROWS + TILE_ROWS) % TILE_ROWS;
306
-			uint32_t tileX = ((mapX + x) % TILE_COLS + TILE_COLS) % TILE_COLS;
307
-			struct Meta meta = tiles[tileY * TILE_ROWS + tileX].meta;
303
+	for (int32_t y = 0; y < MapRows; ++y) {
304
+		for (int32_t x = 0; x < MapCols; ++x) {
305
+			uint32_t tileY = ((mapY + y) % TileRows + TileRows) % TileRows;
306
+			uint32_t tileX = ((mapX + x) % TileCols + TileCols) % TileCols;
307
+			struct Meta meta = tiles[tileY * TileRows + tileX].meta;
308 308
 
309 309
 			if (meta.createTime) {
310 310
 				if (meta.createTime < map.min.createTime) {
@@ -347,7 +347,7 @@ static bool clientMap(const struct Client *client) {
347 347
 		}
348 348
 	}
349 349
 
350
-	struct ServerMessage msg = { .type = SERVER_MAP };
350
+	struct ServerMessage msg = { .type = ServerMap };
351 351
 	if (!clientSend(client, msg)) return false;
352 352
 	if (0 > send(client->fd, &map, sizeof(map), 0)) return false;
353 353
 	return true;
@@ -405,7 +405,7 @@ int main() {
405 405
 			nevents = kevent(kq, &event, 1, NULL, 0, NULL);
406 406
 			if (nevents < 0) err(EX_IOERR, "kevent");
407 407
 
408
-			struct ServerMessage msg = { .type = SERVER_TILE };
408
+			struct ServerMessage msg = { .type = ServerTile };
409 409
 			bool success = clientSend(client, msg)
410 410
 				&& clientMove(client, 0, 0)
411 411
 				&& clientCursors(client);
@@ -429,16 +429,16 @@ int main() {
429 429
 
430 430
 		bool success = false;
431 431
 		switch (msg.type) {
432
-			break; case CLIENT_MOVE: {
432
+			break; case ClientMove: {
433 433
 				success = clientMove(client, msg.move.dx, msg.move.dy);
434 434
 			}
435
-			break; case CLIENT_FLIP: {
435
+			break; case ClientFlip: {
436 436
 				success = clientFlip(client);
437 437
 			}
438
-			break; case CLIENT_PUT: {
438
+			break; case ClientPut: {
439 439
 				success = clientPut(client, msg.put.color, msg.put.cell);
440 440
 			}
441
-			break; case CLIENT_MAP: {
441
+			break; case ClientMap: {
442 442
 				success = clientMap(client);
443 443
 			}
444 444
 		}

+ 34
- 43
torus.h View File

@@ -25,25 +25,16 @@
25 25
 
26 26
 #define ARRAY_LEN(a) (sizeof(a) / sizeof((a)[0]))
27 27
 
28
-#undef COLOR_BLACK
29
-#undef COLOR_RED
30
-#undef COLOR_GREEN
31
-#undef COLOR_YELLOW
32
-#undef COLOR_BLUE
33
-#undef COLOR_MAGENTA
34
-#undef COLOR_CYAN
35
-#undef COLOR_WHITE
36
-
37 28
 enum {
38
-	COLOR_BLACK,
39
-	COLOR_RED,
40
-	COLOR_GREEN,
41
-	COLOR_YELLOW,
42
-	COLOR_BLUE,
43
-	COLOR_MAGENTA,
44
-	COLOR_CYAN,
45
-	COLOR_WHITE,
46
-	COLOR_BRIGHT,
29
+	ColorBlack,
30
+	ColorRed,
31
+	ColorGreen,
32
+	ColorYellow,
33
+	ColorBlue,
34
+	ColorMagenta,
35
+	ColorCyan,
36
+	ColorWhite,
37
+	ColorBright,
47 38
 };
48 39
 
49 40
 static const wchar_t CP437[256] = (
@@ -66,13 +57,13 @@ static const wchar_t CP437[256] = (
66 57
 );
67 58
 
68 59
 enum {
69
-	CELL_ROWS = 24,
70
-	CELL_COLS = 80,
60
+	CellRows = 24,
61
+	CellCols = 80,
71 62
 };
72
-static const size_t CELLS_SIZE = sizeof(uint8_t[CELL_ROWS][CELL_COLS]);
63
+static const size_t CellsSize = sizeof(uint8_t[CellRows][CellCols]);
73 64
 
74
-static const uint8_t CELL_INIT_X = CELL_COLS / 2;
75
-static const uint8_t CELL_INIT_Y = CELL_ROWS / 2;
65
+static const uint8_t CellInitX = CellCols / 2;
66
+static const uint8_t CellInitY = CellRows / 2;
76 67
 
77 68
 struct Meta {
78 69
 	time_t createTime;
@@ -83,40 +74,40 @@ struct Meta {
83 74
 };
84 75
 
85 76
 struct Tile {
86
-	alignas(4096) uint8_t cells[CELL_ROWS][CELL_COLS];
87
-	uint8_t colors[CELL_ROWS][CELL_COLS];
77
+	alignas(4096) uint8_t cells[CellRows][CellCols];
78
+	uint8_t colors[CellRows][CellCols];
88 79
 	struct Meta meta;
89 80
 };
90 81
 static_assert(4096 == sizeof(struct Tile), "struct Tile is page-sized");
91 82
 
92 83
 enum {
93
-	TILE_ROWS = 64,
94
-	TILE_COLS = 64,
84
+	TileRows = 64,
85
+	TileCols = 64,
95 86
 };
96
-static const size_t TILES_SIZE = sizeof(struct Tile[TILE_ROWS][TILE_COLS]);
87
+static const size_t TilesSize = sizeof(struct Tile[TileRows][TileCols]);
97 88
 
98
-static const uint32_t TILE_INIT_X = TILE_COLS / 2;
99
-static const uint32_t TILE_INIT_Y = TILE_ROWS / 2;
89
+static const uint32_t TileInitX = TileCols / 2;
90
+static const uint32_t TileInitY = TileRows / 2;
100 91
 
101 92
 enum {
102
-	MAP_ROWS = 7,
103
-	MAP_COLS = 7,
93
+	MapRows = 7,
94
+	MapCols = 7,
104 95
 };
105 96
 
106 97
 struct Map {
107 98
 	time_t now;
108 99
 	struct Meta min;
109 100
 	struct Meta max;
110
-	struct Meta meta[MAP_ROWS][MAP_COLS];
101
+	struct Meta meta[MapRows][MapCols];
111 102
 };
112 103
 
113 104
 struct ServerMessage {
114 105
 	enum {
115
-		SERVER_TILE,
116
-		SERVER_MOVE,
117
-		SERVER_PUT,
118
-		SERVER_CURSOR,
119
-		SERVER_MAP,
106
+		ServerTile,
107
+		ServerMove,
108
+		ServerPut,
109
+		ServerCursor,
110
+		ServerMap,
120 111
 	} type;
121 112
 	union {
122 113
 		struct {
@@ -138,14 +129,14 @@ struct ServerMessage {
138 129
 	};
139 130
 };
140 131
 
141
-static const uint8_t CURSOR_NONE = UINT8_MAX;
132
+static const uint8_t CursorNone = UINT8_MAX;
142 133
 
143 134
 struct ClientMessage {
144 135
 	enum {
145
-		CLIENT_MOVE,
146
-		CLIENT_FLIP,
147
-		CLIENT_PUT,
148
-		CLIENT_MAP,
136
+		ClientMove,
137
+		ClientFlip,
138
+		ClientPut,
139
+		ClientMap,
149 140
 	} type;
150 141
 	union {
151 142
 		struct {

Loading…
Cancel
Save