]> git.gir.st - minesVIiper.git/blob - mines.c
don't warn on too many mines; minor cleanup, ideas for stomp-radius
[minesVIiper.git] / mines.c
1 /*******************************************************************************
2 minesviiper 0.4
3 By Tobias Girstmair, 2015 - 2018
4
5 ./minesviiper 16x16x40
6 (see ./minesviiper -h for full list of options)
7
8 MOUSE MODE: - left click to open and choord
9 - right click to flag/unflag
10 VI MODE: - hjkl to move cursor left/down/up/right
11 - o/space to open and choord
12 - i to flag/unflag
13 - (see `./minesviiper -h' for all keybindings)
14
15 GNU GPL v3, see LICENSE or https://www.gnu.org/licenses/gpl-3.0.txt
16 *******************************************************************************/
17
18
19 #define _POSIX_C_SOURCE 2 /*for getopt, sigaction in c99*/
20 #include <ctype.h>
21 #include <signal.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <sys/ioctl.h>
25 #include <sys/time.h>
26 #include <termios.h>
27 #include <time.h>
28 #include <unistd.h>
29
30 #include "mines.h"
31 #include "schemes.h"
32
33 #define LINE_OFFSET 3
34 #define LINES_AFTER 2
35 #define COL_OFFSET 2
36 #define BIG_MOVE 5
37 #define MOUSE_MAX 231
38
39 #define MIN(a,b) (a>b?b:a)
40 #define MAX(a,b) (a>b?a:b)
41 #define CLAMP(a,m,M) (a<m?m:(a>M?M:a))
42 #define printm(n, s) for (int _loop = 0; _loop < n; _loop++) fputs (s, stdout)
43 #define print(str) fputs (str?str:"", stdout)
44 #define EMOT(e) op.scheme->emoticons[EMOT_ ## e]
45 #define BORDER(l, c) op.scheme->border[B_ ## l][B_ ## c]
46 #define CW op.scheme->cell_width /* for brevity */
47 #define HI_CELL f.c[g.p[0]][g.p[1]]
48 #define CTRL_ 0x1F &
49
50 #define AR_CELL f.c[ROW][COL] /* helper for AROUND() */
51 #define AROUND(l, c) /* gives us loop-variables ROW, COL, AR_CELL */ \
52 for (int ROW = MAX(l-1, 0); ROW <= MIN(l+1, f.h-1); ROW++) \
53 for (int COL = MAX(c-1, 0); COL <= MIN(c+1, f.w-1); COL++) \
54 if (!(ROW == l && COL == c)) /* skip itself */
55
56 struct minefield f;
57 struct game g;
58 struct opt op;
59
60 int main (int argc, char** argv) {
61 /* defaults: */
62 f.w = 30;
63 f.h = 16;
64 f.m = 99;
65 op.scheme = &symbols_mono;
66 op.mode = FLAG;
67
68 int optget;
69 opterr = 0; /* don't print message on unrecognized option */
70 while ((optget = getopt (argc, argv, "+hnfqcdx")) != -1) {
71 switch (optget) {
72 case 'n': op.mode = NOFLAG; break;
73 case 'f': op.mode = FLAG; break; /*default*/
74 case 'q': op.mode = QUESM; break; /*WARN:implicitly sets -f*/
75 case 'c': op.scheme = &symbols_col1; break;
76 case 'd': op.scheme = &symbols_doublewidth; break;
77 case 'h':
78 default:
79 fprintf (stderr, SHORTHELP LONGHELP, argv[0]);
80 return !(optget=='h');
81 }
82 } if (optind < argc) { /* parse Fieldspec */
83 if (parse_fieldspec (argv[optind])) {
84 fprintf (stderr, "FIELDSPEC: WxH[xM]"
85 " (width 'x' height 'x' mines)\n");
86 return 1;
87 }
88 }
89
90 signal_setup();
91 screen_setup(1);
92 atexit (*quit);
93
94 newgame:
95 switch (minesviiper()) {
96 case GAME_NEW: goto newgame;
97 case GAME_WON: g.o = GAME_WON; break;
98 case GAME_LOST:g.o = GAME_LOST;break;
99 case GAME_QUIT:goto quit;
100 }
101
102 timer_setup(0); /* stop timer */
103 show_minefield (SHOWMINES);
104 for(;;) {
105 switch(getch_wrapper()) {
106 case WRAPPER_EMOTICON:
107 case 'r': goto newgame;
108 case 'q': goto quit;
109 case CTRL_'R':
110 interactive_resize();
111 goto newgame;
112 }
113 }
114
115 quit:
116 return 0;
117 }
118
119 int minesviiper(void) {
120 f.c = alloc_array (f.h, f.w);
121 g = (const struct game){0}; /* reset all game-specific parameters */
122
123 show_minefield (NORMAL);
124
125 for(;;) {
126 switch (getch_wrapper()) {
127 case ' ':
128 if (HI_CELL.o == OPENED) goto open_cell;
129 if (g.s == MODE_OPEN) goto open_cell;
130 if (g.s == MODE_FLAG) goto flag_cell;
131 if (g.s == MODE_QUESM) goto quesm_cell;
132 break;
133 case 'a':
134 g.s = (g.s+1)%(op.mode+1);
135 show_minefield (g.c?SHOWMINES:NORMAL);
136 break;
137 open_cell:
138 case CTRSEQ_MOUSE_LEFT:
139 case 'o':
140 switch (do_uncover(&(g.n))) {
141 case GAME_LOST: return GAME_LOST;
142 case GAME_WON: return GAME_WON;
143 }
144 break;
145 flag_cell:
146 case CTRSEQ_MOUSE_RIGHT:
147 case 'i': flag_square (g.p[0], g.p[1]); break;
148 quesm_cell:
149 case '?':quesm_square (g.p[0], g.p[1]); break;
150 case CTRSEQ_CURSOR_LEFT:
151 case 'h': move_hi (g.p[0], g.p[1]-1); break;
152 case CTRSEQ_CURSOR_DOWN:
153 case 'j': move_hi (g.p[0]+1, g.p[1] ); break;
154 case CTRSEQ_CURSOR_UP:
155 case 'k': move_hi (g.p[0]-1, g.p[1] ); break;
156 case CTRSEQ_CURSOR_RIGHT:
157 case 'l': move_hi (g.p[0], g.p[1]+1); break;
158 case 'w': to_next_boundary (g.p[0], g.p[1], '>'); break;
159 case 'b': to_next_boundary (g.p[0], g.p[1], '<'); break;
160 case 'u': to_next_boundary (g.p[0], g.p[1], '^'); break;
161 case 'd': to_next_boundary (g.p[0], g.p[1], 'v'); break;
162 case '0': /* fallthrough */
163 case '^': move_hi (g.p[0], 0 ); break;
164 case '$': move_hi (g.p[0], f.w-1 ); break;
165 case 'g': move_hi (0, g.p[1] ); break;
166 case 'G': move_hi (f.h-1, g.p[1] ); break;
167 case 'z': move_hi (f.h/2, f.w/2); break;
168 case 'm': set_mark(); break;
169 case'\'': /* fallthrough */
170 case '`': jump_mark(); break;
171 case WRAPPER_EMOTICON:
172 case 'r': return GAME_NEW;
173 case 'q': return GAME_QUIT;
174 case CTRL_'L':
175 screen_setup(1);
176 show_minefield (NORMAL);
177 break;
178 case CTRL_'R':
179 interactive_resize();
180 return GAME_NEW;
181 case '\\':
182 if (g.n == GAME_NEW) break; /* must open a cell first */
183 g.c = !g.c;
184 show_minefield (g.c?SHOWMINES:NORMAL);
185 break;
186 }
187 }
188
189 }
190
191 void quit (void) {
192 screen_setup(0);
193 free_field ();
194 }
195
196 /* I haven't won as long as a cell exists, that
197 - I haven't opened, and
198 - is not a mine */
199 int everything_opened (void) {
200 for (int row = 0; row < f.h; row++)
201 for (int col = 0; col < f.w; col++)
202 if (f.c[row][col].o == CLOSED &&
203 f.c[row][col].m == NO_MINE ) return 0;
204 return 1;
205 }
206
207 int wait_mouse_up (int l, int c) { /* TODO: should not take minefield-coords but absolute ones */
208 unsigned char mouse2[3];
209 int level = 1;
210 int l2, c2;
211
212 /* show :o face */
213 move_ph (1, field2screen_c (f.w/2)-1); print (EMOT(OHH));
214
215 if (!(l < 0 || l >= f.h || c < 0 || c >= f.w)) {
216 /* show a pushed-in button if cursor is on minefield */
217 partial_show_minefield (l, c, HIGHLIGHT);
218 }
219
220 while (level > 0) {
221 if (getctrlseq (mouse2) == CTRSEQ_MOUSE) {
222 /* ignore mouse wheel events: */
223 if (mouse2[0] & 0x40) continue;
224
225 else if (mouse2[0]&3 == 3) level--; /* release event */
226 else level++; /* another button pressed */
227 }
228 }
229
230 move_ph (1, field2screen_c (f.w/2)-1); print (get_emoticon());
231
232 if (!(l < 0 || l >= f.h || c < 0 || c >= f.w)) {
233 partial_show_minefield (l, c, NORMAL);
234 }
235 c2 = screen2field_c(mouse2[1]);
236 l2 = screen2field_l(mouse2[2]);
237 return ((l2 == l) && (c2 == c));
238 }
239
240 int choord_square (int line, int col) {
241 if (uncover_square (line, col)) return 1;
242 AROUND(line,col)
243 if (AR_CELL.f != FLAG) {
244 if (uncover_square (ROW, COL))
245 return 1;
246 }
247
248 return 0;
249 }
250
251 int uncover_square (int l, int c) {
252 if (f.c[l][c].o == OPENED)
253 return 0; /* nothing to do */
254
255 f.c[l][c].o = OPENED;
256 f.c[l][c].f = NOFLAG; /*must not be QUESM, otherwise rendering issues*/
257 partial_show_minefield (l, c, NORMAL);
258
259 if (f.c[l][c].m) {
260 f.c[l][c].m = DEATH_MINE;
261 return 1;
262 }
263
264 /* check for chording */
265 if (f.c[l][c].n == 0) {
266 AROUND(l, c)
267 if (uncover_square (ROW, COL)) return 1;
268 }
269
270 return 0;
271 }
272
273 void flag_square (int l, int c) {
274 static char modechar[] = {'*', '!', '?'};
275
276 if (f.c[l][c].o != CLOSED) return;
277 /* cycle through flag/quesm/noflag: */
278 f.c[l][c].f = (f.c[l][c].f + 1) % (op.mode + 1);
279 if (f.c[l][c].f==FLAG) g.f++;
280 else if ((op.mode==FLAG && f.c[l][c].f==NOFLAG) ||
281 (op.mode==QUESM && f.c[l][c].f==QUESM)) g.f--;
282 partial_show_minefield (l, c, NORMAL);
283 move_ph (1, op.scheme->cell_width);
284 printf ("[%03d%c]", f.m - g.f, modechar[g.s]);
285 }
286
287 void quesm_square (int l, int c) {
288 /* toggle question mark / none. won't turn flags into question marks.
289 unlike flag_square, this function doesn't respect `-q'. */
290 if (f.c[l][c].o != CLOSED) return;
291 else if (f.c[l][c].f == NOFLAG) f.c[l][c].f = QUESM;
292 else if (f.c[l][c].f == QUESM) f.c[l][c].f = NOFLAG;
293 partial_show_minefield (l, c, NORMAL);
294 }
295
296 int do_uncover (int* is_newgame) {
297 if (*is_newgame == GAME_NEW) {
298 *is_newgame = GAME_INPROGRESS;
299 fill_minefield (g.p[0], g.p[1]);
300 timer_setup(1);
301 }
302
303 if (HI_CELL.f == FLAG ) return GAME_INPROGRESS;
304 if (HI_CELL.o == CLOSED) {
305 if (uncover_square (g.p[0], g.p[1])) return GAME_LOST;
306 } else if (get_neighbours (g.p[0], g.p[1], 1) == 0) {
307 if (choord_square (g.p[0], g.p[1])) return GAME_LOST;
308 } else {
309 /* show the stomp radius if we aren't fully flagged */
310 AROUND(g.p[0], g.p[1])
311 if (AR_CELL.o == CLOSED && AR_CELL.f !=FLAG)
312 partial_show_minefield (ROW, COL, HIGHLIGHT);
313 /* TODO: save g.p, then in ~.1s execute:
314 AROUND(saved_l, saved_r) partial_show_minefield (ROW, COL, NORMAL);
315 using setitimer is tricky, because we already use SIGALRM; and it
316 should also trigger when a key is pressed. maybe a getchar() with
317 a read(2)-timeout + ungetch()? (usleep did not show the stomp at all?!) */
318 }
319 if (everything_opened()) return GAME_WON;
320
321 return GAME_INPROGRESS;
322 }
323
324 void set_mark(void) {
325 int mark = tolower(getch_wrapper());
326 if (mark < 'a' || mark > 'z') return; /*out of bound*/
327
328 g.m[mark-'a'].l = g.p[0];
329 g.m[mark-'a'].c = g.p[1];
330 g.m[mark-'a'].s = 1;
331 }
332
333 void jump_mark(void) {
334 int mark = tolower(getch_wrapper());
335 /* check bounds and if set: */
336 if (mark < 'a' || mark > 'z' || !g.m[mark-'a'].s) return;
337 move_hi (g.m[mark-'a'].l, g.m[mark-'a'].c);
338 }
339
340 void fill_minefield (int l, int c) {
341 srand (time(0));
342 int mines_set = f.m;
343 while (mines_set) {
344 int line = rand() % f.h;
345 int col = rand() % f.w;
346
347 if (f.c[line][col].m) {
348 /* skip if field already has a mine */
349 continue;
350 } else if ((line == l) && (col == c)) {
351 /* don't put a mine on the already opened (first click) field */
352 continue;
353 } else {
354 mines_set--;
355 f.c[line][col].m = STD_MINE;
356 }
357 }
358
359 /* precalculate neighbours */
360 for (int l=0; l < f.h; l++)
361 for (int c=0; c < f.w; c++)
362 f.c[l][c].n = get_neighbours (l, c, NORMAL);
363 }
364
365 void move_ph (int line, int col) {
366 /* move printhead to zero-indexed position */
367 printf ("\033[%d;%dH", line+1, col+1);
368 }
369
370 void move_hi (int l, int c) {
371 /* move cursor and highlight to absolute coordinates */
372
373 partial_show_minefield (g.p[0], g.p[1], NORMAL);
374 /* update g.p */
375 g.p[0] = CLAMP(l, 0, f.h-1);
376 g.p[1] = CLAMP(c, 0, f.w-1);
377 move_ph (g.p[0]+LINE_OFFSET, field2screen_c(g.p[1]));
378
379 partial_show_minefield (g.p[0], g.p[1], HIGHLIGHT);
380 }
381
382 /* to_next_boundary(): move into the supplied direction until a change in open-
383 state or flag-state is found and move there. falls back to BIG_MOVE. */
384 #define FIND_NEXT(X, L, C, L1, C1, MAX, OP) do {\
385 new_ ## X OP ## = BIG_MOVE;\
386 for (int i = X OP 1; i > 0 && i < f.MAX-1; i OP ## OP)\
387 if (((f.c[L ][C ].o<<2) + f.c[L ][C ].f) \
388 != ((f.c[L1][C1].o<<2) + f.c[L1][C1].f)) {\
389 new_ ## X = i OP 1;\
390 break;\
391 }\
392 } while(0)
393 void to_next_boundary (int l, int c, char direction) {
394 int new_l = l;
395 int new_c = c;
396 switch (direction) {
397 case '>': FIND_NEXT(c, l, i, l, i+1, w, +); break;
398 case '<': FIND_NEXT(c, l, i, l, i-1, w, -); break;
399 case '^': FIND_NEXT(l, i, c, i-1, c, h, -); break;
400 case 'v': FIND_NEXT(l, i, c, i+1, c, h, +); break;
401 }
402
403 move_hi (new_l, new_c);
404 }
405 #undef FIND_NEXT
406
407 char* cell2schema (int l, int c, int mode) {
408 struct minecell cell = f.c[l][c];
409
410 if (mode == SHOWMINES) return (
411 cell.f == FLAG && cell.m ? op.scheme->field_flagged:
412 cell.f == FLAG && !cell.m ? op.scheme->mine_wrongf:
413 cell.m == STD_MINE ? op.scheme->mine_normal:
414 cell.m == DEATH_MINE ? op.scheme->mine_death:
415 cell.o == CLOSED ? op.scheme->field_closed:
416 /*.......................*/ op.scheme->number[f.c[l][c].n]);
417 else return (
418 cell.f == FLAG ? op.scheme->field_flagged:
419 cell.f == QUESM ? op.scheme->field_question:
420 cell.o == CLOSED ? op.scheme->field_closed:
421 cell.m == STD_MINE ? op.scheme->mine_normal:
422 cell.m == DEATH_MINE ? op.scheme->mine_death:
423 /*.......................*/ op.scheme->number[f.c[l][c].n]);
424 }
425
426 void partial_show_minefield (int l, int c, int mode) {
427 move_ph (l+LINE_OFFSET, field2screen_c(c));
428 if (mode == HIGHLIGHT) printf ("\033[7m"); /* reverse video */
429 print (cell2schema(l, c, mode));
430 if (mode == HIGHLIGHT) printf ("\033[0m"); /* reset all */
431 }
432
433 char* get_emoticon(void) {
434 return g.o==GAME_WON ? EMOT(WON):
435 g.o==GAME_LOST? EMOT(DEAD):
436 EMOT(SMILE);
437 }
438
439 /* https://zserge.com/blog/c-for-loop-tricks.html */
440 #define print_line(which) \
441 for (int _break = (printf("%s", BORDER(which,LEFT)), 1); _break; \
442 _break = 0, printf("%s\r\n", BORDER(which,RIGHT)))
443 #define print_border(which, width) \
444 print_line(which) printm (width, BORDER(which,MIDDLE))
445 void show_minefield (int mode) {
446 int dtime = difftime (time(NULL), g.t)*!!g.t;
447 int half_spaces = f.w*op.scheme->cell_width/2;
448 int left_spaces = MAX(0,half_spaces-7-(f.m-g.f>999));
449 int right_spaces = MAX(0,half_spaces-6-(dtime>999));
450 static char modechar[] = {'*', '!', '?'};
451
452 move_ph (0,0);
453
454 print_border(TOP, f.w);
455 print_line(STATUS) {
456 if (mode == RESIZEMODE) printf ("%-*s", 2*half_spaces,
457 f.w>20?"Resize Mode: HJKL to resize, Enter to set":
458 f.w>11?"Resize Mode: HJKL/Enter":"HJKL/Enter");
459 else printf("[%03d%c]%*s%s%*s[%03d]",
460 /* [ */ f.m - g.f, modechar[g.s], /* ] */
461 left_spaces,"", get_emoticon(), right_spaces,"",
462 /* [ */ dtime /* ] */);
463 }
464 print_border(DIVIDER, f.w);
465 /* main body */
466 for (int l = 0; l < f.h; l++) print_line(FIELD)
467 printm (f.w, cell2schema(l, _loop, mode));
468 print_border(BOTTOM, f.w);
469 }
470
471 int get_neighbours (int line, int col, int reduced_mode) {
472 /* counts mines surrounding a square
473 modes: 0=normal; 1=reduced */
474
475 int count = 0;
476 AROUND(line, col) {
477 count += !!AR_CELL.m;
478 count -= reduced_mode * AR_CELL.f==FLAG;
479 }
480 return count;
481 }
482
483 void interactive_resize(void) {
484 unsigned char buf[3];
485 for(;;) {
486 screen_setup(1); /* clears the screen */
487 show_minefield (RESIZEMODE);
488 /* show the new field size in the corner: */
489 move_ph(f.h+LINE_OFFSET-1,COL_OFFSET);
490 printf("%d x %d", f.w, f.h);
491
492 free_field(); /* must free before resizing! */
493 switch (getctrlseq(buf)) {
494 case 'h': f.w--; break;
495 case 'j': f.h++; break;
496 case 'k': f.h--; break;
497 case 'l': f.w++; break;
498 case 0xa: /* enter, fallthrough */
499 case 'q': return;
500 }
501
502 clamp_fieldsize();
503 f.m = mines_percentage(f.w, f.h);
504 f.c = alloc_array (f.h, f.w);
505 }
506 }
507
508 struct minecell** alloc_array (int lines, int cols) {
509 free_field (); /* NOTE: this feels like a hack :| */
510 struct minecell** a = malloc (lines * sizeof(struct minecell*));
511 if (a == NULL) return NULL;
512 for (int l = 0; l < lines; l++) {
513 a[l] = calloc (cols, sizeof(struct minecell));
514 if (a[l] == NULL) goto unalloc;
515 }
516
517 return a;
518 unalloc:
519 for (int l = 0; l < lines; l++)
520 free (a[l]);
521 return NULL;
522 }
523
524 void free_field (void) {
525 if (f.c == NULL) return;
526 for (int l = 0; l < f.h; l++) {
527 free (f.c[l]);
528 }
529
530 free (f.c);
531 f.c = NULL;
532 }
533
534 int screen2field_l (int l) {
535 return (l-LINE_OFFSET) - 1;
536 }
537 int screen2field_c (int c) {
538 /* this depends on the cell width and only works when it is 1 or 2. */
539 return (c-COL_OFFSET+1 - 2*(CW%2))/2 - CW/2;
540 }
541 int field2screen_c (int c) {
542 return (CW*c+COL_OFFSET - (CW%2));
543 }
544 int clicked_emoticon (unsigned char* mouse) {
545 return (mouse[2] == LINE_OFFSET-1 && (
546 mouse[1] == f.w+COL_OFFSET ||
547 mouse[1] == f.w+COL_OFFSET+1));
548 }
549
550 enum esc_states {
551 START,
552 ESC_SENT,
553 CSI_SENT,
554 MOUSE_EVENT,
555 };
556 int getctrlseq (unsigned char* buf) {
557 int c;
558 int state = START;
559 int offset = 0x20; /* never sends control chars as data */
560 while ((c = getchar()) != EOF) {
561 switch (state) {
562 case START:
563 switch (c) {
564 case '\033': state=ESC_SENT; break;
565 default: return c;
566 }
567 break;
568 case ESC_SENT:
569 switch (c) {
570 case '[': state=CSI_SENT; break;
571 default: return CTRSEQ_INVALID;
572 }
573 break;
574 case CSI_SENT:
575 switch (c) {
576 case 'A': return CTRSEQ_CURSOR_UP;
577 case 'B': return CTRSEQ_CURSOR_DOWN;
578 case 'C': return CTRSEQ_CURSOR_RIGHT;
579 case 'D': return CTRSEQ_CURSOR_LEFT;
580 case 'M': state=MOUSE_EVENT; break;
581 default: return CTRSEQ_INVALID;
582 }
583 break;
584 case MOUSE_EVENT:
585 buf[0] = c - offset;
586 buf[1] = getchar() - offset;
587 buf[2] = getchar() - offset;
588 return CTRSEQ_MOUSE;
589 default:
590 return CTRSEQ_INVALID;
591 }
592 }
593 return 2;
594 }
595
596 int getch(unsigned char* buf) {
597 /* returns a character, EOF, or constant for an escape/control sequence - NOT
598 compatible with the ncurses implementation of same name */
599 int action = getctrlseq(buf);
600 int l, c;
601 switch (action) {
602 case CTRSEQ_MOUSE:
603 l = screen2field_l (buf[2]);
604 c = screen2field_c (buf[1]);
605
606 if (buf[0] > 3) break; /* ignore all but left/middle/right/up */
607 int success = wait_mouse_up(l, c);
608
609 /* mouse moved while pressed: */
610 if (!success) return CTRSEQ_INVALID;
611
612 switch (buf[0]) {
613 case 0: return CTRSEQ_MOUSE_LEFT;
614 case 1: return CTRSEQ_MOUSE_MIDDLE;
615 case 2: return CTRSEQ_MOUSE_RIGHT;
616 }
617 }
618
619 return action;
620 }
621
622 int getch_wrapper (void) {
623 unsigned char mouse[3];
624 int c = getch(mouse);
625
626 if (c == CTRSEQ_MOUSE_LEFT || c == CTRSEQ_MOUSE_RIGHT) {
627 if (clicked_emoticon(mouse))
628 return WRAPPER_EMOTICON;
629
630 if (screen2field_c (mouse[1]) < 0
631 || screen2field_c (mouse[1]) >= f.w
632 || screen2field_l (mouse[2]) < 0
633 || screen2field_l (mouse[2]) >= f.h)
634 return CTRSEQ_INVALID;
635
636 g.p[0] = screen2field_l (mouse[2]);
637 g.p[1] = screen2field_c (mouse[1]);
638
639 return c; /* CTRSEQ_MOUSE_LEFT || CTRSEQ_MOUSE_RIGHT */
640 }
641
642 return c;
643 }
644
645 void clamp_fieldsize (void) {
646 /* clamp field size to terminal size and mouse maximum: */
647 struct winsize w;
648 ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
649
650 if (COL_OFFSET + f.w*CW + COL_OFFSET > w.ws_col)
651 f.w = w.ws_col/CW - (COL_OFFSET+COL_OFFSET);
652 if (LINE_OFFSET + f.h + LINES_AFTER > w.ws_row)
653 f.h = w.ws_row - (LINE_OFFSET+LINES_AFTER);
654 if (COL_OFFSET + f.w*CW > MOUSE_MAX)
655 f.w = MOUSE_MAX/CW - COL_OFFSET;
656 if (LINE_OFFSET + f.h > MOUSE_MAX)
657 f.h = MOUSE_MAX - LINE_OFFSET;
658 }
659
660 int mines_percentage(int w, int h) {
661 return w*h*(w*h<30*16?.15625:.20625);
662 }
663
664 int parse_fieldspec(char* str) {
665 /* parses the FIELDSPEC (WxHxM); returns 1 on error */
666 int n = sscanf (str, "%dx%dx%d", &f.w, &f.h, &f.m);
667
668 clamp_fieldsize();
669
670 if (n < 2) {
671 return 1; /* error */
672 } else if (n == 2) {
673 f.m = mines_percentage(f.w, f.h);
674 }
675
676 f.m = MIN(f.m, (f.w-1) * (f.h-1)); /* limit mines */
677
678 return 0;
679 }
680
681 void timer_setup (int enable) {
682 static struct itimerval tbuf;
683 tbuf.it_interval.tv_sec = 1;
684 tbuf.it_interval.tv_usec = 0;
685
686 if (enable) {
687 g.t = time(NULL);
688 tbuf.it_value.tv_sec = 1;
689 tbuf.it_value.tv_usec = 0;
690 if (setitimer(ITIMER_REAL, &tbuf, NULL) == -1) {
691 perror("setitimer");
692 exit(1);
693 }
694 } else {
695 tbuf.it_value.tv_sec = 0;
696 tbuf.it_value.tv_usec = 0;
697 if ( setitimer(ITIMER_REAL, &tbuf, NULL) == -1 ) {
698 perror("setitimer");
699 exit(1);
700 }
701 }
702
703 }
704
705 void signal_setup (void) {
706 struct sigaction saction;
707
708 saction.sa_handler = signal_handler;
709 sigemptyset(&saction.sa_mask);
710 saction.sa_flags = 0;
711 if (sigaction(SIGALRM, &saction, NULL) < 0 ) {
712 perror("SIGALRM");
713 exit(1);
714 }
715
716 if (sigaction(SIGINT, &saction, NULL) < 0 ) {
717 perror ("SIGINT");
718 exit (1);
719 }
720 }
721
722 void signal_handler (int signum) {
723 int dtime;
724 switch (signum) {
725 case SIGALRM:
726 dtime = difftime (time(NULL), g.t);
727 move_ph (1, f.w*CW-(CW%2)-3-(dtime>999));
728 printf ("[%03d]", g.t?dtime:0);
729 break;
730 case SIGINT:
731 exit(128+SIGINT);
732 }
733 }
734
735 void screen_setup (int enable) {
736 if (enable) {
737 raw_mode(1);
738 printf ("\033[s\033[?47h"); /* save cursor, alternate screen */
739 printf ("\033[H\033[J"); /* reset cursor, clear screen */
740 printf ("\033[?1000h\033[?25l"); /* enable mouse, hide cursor */
741 print (op.scheme->init_seq); /* swich charset, if necessary */
742 } else {
743 print (op.scheme->reset_seq); /* reset charset, if necessary */
744 printf ("\033[?9l\033[?25h"); /* disable mouse, show cursor */
745 printf ("\033[?47l\033[u"); /* primary screen, restore cursor */
746 raw_mode(0);
747 }
748 }
749
750 /* http://users.csc.calpoly.edu/~phatalsk/357/lectures/code/sigalrm.c */
751 void raw_mode(int enable) {
752 static struct termios saved_term_mode;
753 struct termios raw_term_mode;
754
755 if (enable) {
756 tcgetattr(STDIN_FILENO, &saved_term_mode);
757 raw_term_mode = saved_term_mode;
758 raw_term_mode.c_lflag &= ~(ICANON | ECHO);
759 raw_term_mode.c_cc[VMIN] = 1 ;
760 raw_term_mode.c_cc[VTIME] = 0;
761 tcsetattr(STDIN_FILENO, TCSAFLUSH, &raw_term_mode);
762 } else {
763 tcsetattr(STDIN_FILENO, TCSAFLUSH, &saved_term_mode);
764 }
765 }
Imprint / Impressum