1 /*******************************************************************************
3 By Tobias Girstmair, 2015 - 2018
6 (see ./minesviiper -h for full list of options)
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
13 - (see `./minesviiper -h' for all keybindings)
15 GNU GPL v3, see LICENSE or https://www.gnu.org/licenses/gpl-3.0.txt
16 *******************************************************************************/
19 #define _POSIX_C_SOURCE 2 /*for getopt, sigaction in c99*/
25 #include <sys/ioctl.h>
39 #define STOMP_TIMEOUT 150 /*ms*/
41 #define MIN(a,b) (a>b?b:a)
42 #define MAX(a,b) (a>b?a:b)
43 #define CLAMP(a,m,M) (a<m?m:(a>M?M:a))
44 #define printm(n, s) for (int _loop = 0; _loop < n; _loop++) fputs (s, stdout)
45 #define print(str) fputs (str?str:"", stdout)
46 #define EMOT(e) op.scheme->emoticons[EMOT_ ## e]
47 #define BORDER(l, c) op.scheme->border[B_ ## l][B_ ## c]
48 #define CW op.scheme->cell_width /* for brevity */
49 #define DW op.scheme->display_width /* ditto. */
50 #define HI_CELL f.c[g.p[0]][g.p[1]]
51 #define LINE_BELOW LINE_OFFSET+f.h-1+LINES_AFTER
54 #define AR_CELL f.c[ROW][COL] /* helper for AROUND() */
55 #define AROUND(l, c) /* gives us loop-variables ROW, COL, AR_CELL */ \
56 for (int ROW = MAX(l-1, 0); ROW <= MIN(l+1, f.h-1); ROW++) \
57 for (int COL = MAX(c-1, 0); COL <= MIN(c+1, f.w-1); COL++) \
58 if (!(ROW == l && COL == c)) /* skip itself */
61 for (int _break=(timer_setup(0),1); _break; _break=0, timer_setup(g.t))
67 int main (int argc
, char** argv
) {
72 op
.scheme
= &symbols_mono
;
76 opterr
= 0; /* don't print message on unrecognized option */
77 while ((optget
= getopt (argc
, argv
, "+hnfqbcd")) != -1) {
79 case 'n': op
.mode
= NOFLAG
; break;
80 case 'f': op
.mode
= FLAG
; break; /*default*/
81 case 'q': op
.mode
= QUESM
; break; /*WARN:implicitly sets -f*/
82 case 'b': op
.scheme
= &symbols_mono
; break;
83 case 'c': op
.scheme
= &symbols_col1
; break;
84 case 'd': op
.scheme
= &symbols_doublewidth
; break;
87 fprintf (stderr
, SHORTHELP LONGHELP KEYHELP
, argv
[0]);
88 return !(optget
=='h');
90 } if (optind
< argc
) { /* parse Fieldspec */
91 if (parse_fieldspec (argv
[optind
])) {
92 fprintf (stderr
, SHORTHELP FIELDHELP
, argv
[0]);
102 switch (g
.o
= minesviiper()) {
103 case GAME_NEW
: goto newgame
;
104 case GAME_WON
: goto game_end
;
105 case GAME_LOST
:goto game_end
;
106 case GAME_QUIT
:goto quit
;
110 timer_setup(0); /* stop timer */
111 int show_how
= g
.o
==GAME_WON
?SHOWFLAGS
:SHOWMINES
;
112 show_minefield (show_how
);
114 switch(getch_wrapper()) {
115 case WRAPPER_EMOTICON
:
116 case 'r': goto newgame
;
119 /*WARN: updates the timer (would require fix in struct*/
121 show_minefield (show_how
);
124 interactive_resize();
133 int minesviiper(void) {
134 f
.c
= alloc_array (f
.h
, f
.w
);
135 g
= (const struct game
){0}; /* reset all game-specific parameters */
137 show_minefield (NORMAL
);
140 int actor
= KEYBOARD
; /* WARN: feels like a hack :| */
141 switch (getch_wrapper()) {
143 if (HI_CELL
.o
== OPENED
) goto open_cell
;
144 if (g
.s
== MODE_OPEN
) goto open_cell
;
145 if (g
.s
== MODE_FLAG
) goto flag_cell
;
146 if (g
.s
== MODE_QUESM
) goto quesm_cell
;
149 g
.s
= (g
.s
+1)%(op
.mode
+1);
150 show_minefield (g
.c
?SHOWMINES
:NORMAL
);
152 case CTRSEQ_MOUSE_LEFT
:
157 switch (do_uncover(&(g
.n
), actor
)) {
158 case GAME_LOST
: return GAME_LOST
;
159 case GAME_WON
: return GAME_WON
;
163 case CTRSEQ_MOUSE_RIGHT
:
164 case 'i': flag_square (g
.p
[0], g
.p
[1]); break;
166 case '?':quesm_square (g
.p
[0], g
.p
[1]); break;
167 case CTRSEQ_CURSOR_LEFT
:
168 case 'h': move_hi (g
.p
[0], g
.p
[1]-1); break;
169 case CTRSEQ_CURSOR_DOWN
:
170 case 'j': move_hi (g
.p
[0]+1, g
.p
[1] ); break;
171 case CTRSEQ_CURSOR_UP
:
172 case 'k': move_hi (g
.p
[0]-1, g
.p
[1] ); break;
173 case CTRSEQ_CURSOR_RIGHT
:
174 case 'l': move_hi (g
.p
[0], g
.p
[1]+1); break;
175 case 'w': to_next_boundary (g
.p
[0], g
.p
[1], '>', 1); break;
176 case 'b': to_next_boundary (g
.p
[0], g
.p
[1], '<', 1); break;
177 case 'u': to_next_boundary (g
.p
[0], g
.p
[1], '^', 1); break;
178 case 'd': to_next_boundary (g
.p
[0], g
.p
[1], 'v', 1); break;
179 case 'e': to_next_boundary (g
.p
[0], g
.p
[1], '>', 0); break;
180 case '0': /* fallthrough */
181 case '^': move_hi (g
.p
[0], 0 ); break;
182 case '$': move_hi (g
.p
[0], f
.w
-1 ); break;
183 case 'g': move_hi (0, g
.p
[1]); break;
184 case 'G': move_hi (f
.h
-1, g
.p
[1]); break;
185 case 'z': move_hi (f
.h
/2, f
.w
/2 ); break;
186 case 'm': set_mark(); break;
187 case'\'': /* fallthrough */
188 case '`': jump_mark(); break;
189 case 'f': find(getch_wrapper(), '>', 0); break;
190 case 'F': find(getch_wrapper(), '<', 0); break;
191 case 't': find(getch_wrapper(), '>',-1); break;
192 case 'T': find(getch_wrapper(), '<',-1); break;
193 case 'a': find(getch_wrapper(), '>',+1); break;
194 case 'A': find(getch_wrapper(), '<',+1); break;
195 //TODO: HJKL, ^H^J^K^L: <spacemode> up/down/left/right/45deg
196 case WRAPPER_EMOTICON
:
197 case 'r': timer_setup(0); return GAME_NEW
;
200 case EX_QUIT
: return GAME_QUIT
;
201 case EX_HELP
: printf (KEYHELP
); break;
204 interactive_resize();
207 printf ("\rinvalid command");
208 PAUSE_TIMER
ungetc(getchar(), stdin
);
209 printf("\033[2K"); /* clear line */
212 case 'q': //TODO: redefine `q' as macro functionality
216 show_minefield (NORMAL
);
219 if (g
.n
== GAME_NEW
) break; /* must open a cell first */
221 show_minefield (g
.c
?SHOWMINES
:NORMAL
);
233 /* I haven't won as long as a cell exists, that
234 - I haven't opened, and
236 int everything_opened (void) {
237 for (int row
= 0; row
< f
.h
; row
++)
238 for (int col
= 0; col
< f
.w
; col
++)
239 if (f
.c
[row
][col
].o
== CLOSED
&&
240 f
.c
[row
][col
].m
== NO_MINE
) return 0;
244 int wait_mouse_up (int l
, int c
) {
245 unsigned char mouse2
[3];
250 move_ph (1, field2screen_c (f
.w
/2)-1); print (EMOT(OHH
));
252 if (!(l
< 0 || l
>= f
.h
|| c
< 0 || c
>= f
.w
)) {
253 /* show a pushed-in button if cursor is on minefield */
254 redraw_cell (l
, c
, HIGHLIGHT
);
260 if (getctrlseq (mouse2
) == CTRSEQ_MOUSE
) {
261 /* ignore mouse wheel events: */
262 if (mouse2
[0] & 0x40) continue;
264 else if((mouse2
[0]&3) == 3) level
--; /* release event */
265 else level
++; /* another button pressed */
269 move_ph (1, field2screen_c (f
.w
/2)-1); print (get_emoticon());
271 if (!(l
< 0 || l
>= f
.h
|| c
< 0 || c
>= f
.w
)) {
272 redraw_cell (l
, c
, g
.o
?SHOWMINES
:NORMAL
);
275 c2
= screen2field_c(mouse2
[1]);
276 l2
= screen2field_l(mouse2
[2]);
277 return ((l2
== l
) && (c2
== c
));
280 int choord_square (int line
, int col
) {
281 if (uncover_square (line
, col
)) return 1;
283 if (AR_CELL
.f
!= FLAG
) {
284 if (uncover_square (ROW
, COL
))
291 int uncover_square (int l
, int c
) {
292 if (f
.c
[l
][c
].o
== OPENED
)
293 return 0; /* nothing to do */
295 f
.c
[l
][c
].o
= OPENED
;
296 f
.c
[l
][c
].f
= NOFLAG
; /*must not be QUESM, otherwise rendering issues*/
297 redraw_cell (l
, c
, NORMAL
);
300 f
.c
[l
][c
].m
= DEATH_MINE
;
304 /* check for chording */
305 if (f
.c
[l
][c
].n
== 0) {
307 if (uncover_square (ROW
, COL
)) return 1;
313 void flag_square (int l
, int c
) {
314 static char modechar
[] = {'*', '!', '?'};
316 if (f
.c
[l
][c
].o
!= CLOSED
) return;
317 /* cycle through flag/quesm/noflag: */
318 f
.c
[l
][c
].f
= (f
.c
[l
][c
].f
+ 1) % (op
.mode
+ 1);
319 if (f
.c
[l
][c
].f
==FLAG
) g
.f
++;
320 else if ((op
.mode
==FLAG
&& f
.c
[l
][c
].f
==NOFLAG
) ||
321 (op
.mode
==QUESM
&& f
.c
[l
][c
].f
==QUESM
)) g
.f
--;
322 redraw_cell (l
, c
, NORMAL
);
323 move_ph (1, op
.scheme
->cell_width
);
324 printf ("[%03d%c]", f
.m
- g
.f
, modechar
[g
.s
]);
327 void quesm_square (int l
, int c
) {
328 /* toggle question mark / none. won't turn flags into question marks.
329 unlike flag_square, this function doesn't respect `-q'. */
330 if (f
.c
[l
][c
].o
!= CLOSED
) return;
331 else if (f
.c
[l
][c
].f
== NOFLAG
) f
.c
[l
][c
].f
= QUESM
;
332 else if (f
.c
[l
][c
].f
== QUESM
) f
.c
[l
][c
].f
= NOFLAG
;
333 redraw_cell (l
, c
, NORMAL
);
336 int do_uncover (int* is_newgame
, int actor
) {
337 if (*is_newgame
== GAME_NEW
) {
338 *is_newgame
= GAME_INPROGRESS
;
339 fill_minefield (g
.p
[0], g
.p
[1]);
343 if (HI_CELL
.f
== FLAG
) return GAME_INPROGRESS
;
344 if (HI_CELL
.o
== CLOSED
) {
345 if (uncover_square (g
.p
[0], g
.p
[1])) return GAME_LOST
;
346 } else if (get_neighbours (g
.p
[0], g
.p
[1], 1) == 0) {
347 if (choord_square (g
.p
[0], g
.p
[1])) return GAME_LOST
;
348 } else if (actor
!= MOUSE
) {
349 show_stomp(1, g
.p
[0], g
.p
[1]);
353 show_stomp(0, g
.p
[0], g
.p
[1]);
355 if (everything_opened()) return GAME_WON
;
357 return GAME_INPROGRESS
;
362 printf ("\033[?9l\033[?25h"); /* disable mouse, show cursor */
363 PAUSE_TIMER
{ /* timer spams ASCII STX (0x02) chars */
364 move_ph(LINE_BELOW
, 0);
365 putchar(':'); /* prompt */
367 raw_mode(0);/*use cooked mode, so we get line editing for free*/
368 fgets(what
, 256, stdin
); //TODO: cancel on ^G, ESC, ^C, ...
371 move_ph(LINE_BELOW
, 0);
372 printf ("\033[0J"); /* clear to the end of the screen */
374 printf ("\033[?1000h\033[?25l"); /* enable mouse, hide cursor */
376 switch (what
[0]) { //TODO: urgh.
377 case 'q': return EX_QUIT
;
378 case 'h': return EX_HELP
;
379 case 'r': return EX_RESZ
;
380 default: return EX_INVALID
;
383 void set_mark(void) {
384 int mark
= tolower(getch_wrapper());
385 if (mark
< 'a' || mark
> 'z') return; /*out of bound*/
387 g
.m
[mark
-'a'].l
= g
.p
[0];
388 g
.m
[mark
-'a'].c
= g
.p
[1];
392 void jump_mark(void) {
393 int mark
= tolower(getch_wrapper());
394 /* check bounds and if set: */
395 if (mark
< 'a' || mark
> 'z' || !g
.m
[mark
-'a'].s
) return;
396 move_hi (g
.m
[mark
-'a'].l
, g
.m
[mark
-'a'].c
);
399 void fill_minefield (int l
, int c
) {
403 int line
= rand() % f
.h
;
404 int col
= rand() % f
.w
;
406 if (f
.c
[line
][col
].m
) {
407 /* skip if field already has a mine */
409 } else if ((line
== l
) && (col
== c
)) {
410 /* don't put a mine on the already opened (first click) field */
414 f
.c
[line
][col
].m
= STD_MINE
;
418 /* precalculate neighbours */
419 for (int l
=0; l
< f
.h
; l
++)
420 for (int c
=0; c
< f
.w
; c
++)
421 f
.c
[l
][c
].n
= get_neighbours (l
, c
, NORMAL
);
424 void move_ph (int line
, int col
) {
425 /* move printhead to zero-indexed position */
426 printf ("\033[%d;%dH", line
+1, col
+1);
429 void move_hi (int l
, int c
) {
430 /* move cursor and highlight to absolute coordinates */
432 redraw_cell (g
.p
[0], g
.p
[1], NORMAL
);
434 g
.p
[0] = CLAMP(l
, 0, f
.h
-1);
435 g
.p
[1] = CLAMP(c
, 0, f
.w
-1);
436 move_ph (g
.p
[0]+LINE_OFFSET
, field2screen_c(g
.p
[1]));
438 redraw_cell (g
.p
[0], g
.p
[1], HIGHLIGHT
);
441 /* to_next_boundary(): move into the supplied direction until a change in open-
442 state or flag-state is found and move there. falls back to BIG_MOVE. */
443 #define FIND_BOUND(X, L, C, L1, C1, MAX, OP) do {\
444 new_ ## X OP ## = BIG_MOVE;\
445 for (int i = X OP 1; i > 0 && i < f.MAX-1; i OP ## OP)\
446 if (((f.c[L ][C ].o<<2) + f.c[L ][C ].f) \
447 != ((f.c[L1][C1].o<<2) + f.c[L1][C1].f)) {\
448 new_ ## X = i OP advance;\
452 void to_next_boundary (int l
, int c
, char direction
, int advance
) {
456 case '>': FIND_BOUND(c
, l
, i
, l
, i
+1, w
, +); break;
457 case '<': FIND_BOUND(c
, l
, i
, l
, i
-1, w
, -); break;
458 case '^': FIND_BOUND(l
, i
, c
, i
-1, c
, h
, -); break;
459 case 'v': FIND_BOUND(l
, i
, c
, i
+1, c
, h
, +); break;
462 move_hi (new_l
, new_c
);
466 #define CELL f.c[g.p[0]][c]
467 #define FIND_NEXT(CONDITION, DIRECTION) do {\
468 int plusminus = DIRECTION-'='; /* -1 if '<', 1 if '>' */ \
469 for (int c = g.p[1]+plusminus; c >= 0 && c < f.w; c+=plusminus) \
471 move_hi (g.p[0], c+(till_after*plusminus)); \
472 return 1; /* NOTE: break didn't work */ \
476 int find (int what
, char direction
, int till_after
) {
478 case ' ': what
= '0'; /* fallthrough */
479 case '0': case '1': case '2':
480 case '3': case '4': case '5':
481 case '6': case '7': case '8': /* numbers, opened */
482 FIND_NEXT(CELL
.o
&& CELL
.n
== what
-'0', direction
);
483 case 'o': FIND_NEXT(CELL
.o
, direction
); /* any opened */
484 case 'c': FIND_NEXT(!CELL
.o
, direction
); /* any closed */
485 case 'f': case 'i': FIND_NEXT(CELL
.f
==FLAG
, direction
); /* is flagged */
486 case 'q': case '?': FIND_NEXT(CELL
.f
==QUESM
, direction
);/* questioned */
494 char* cell2schema (int l
, int c
, int mode
) {
495 struct minecell cell
= f
.c
[l
][c
];
497 if (mode
== SHOWMINES
) return (
498 cell
.f
== FLAG
&& cell
.m
? op
.scheme
->field_flagged
:
499 cell
.f
== FLAG
&& !cell
.m
? op
.scheme
->mine_wrongf
:
500 cell
.m
== STD_MINE
? op
.scheme
->mine_normal
:
501 cell
.m
== DEATH_MINE
? op
.scheme
->mine_death
:
502 cell
.o
== CLOSED
? op
.scheme
->field_closed
:
503 /*.......................*/ op
.scheme
->number
[f
.c
[l
][c
].n
]);
504 else if (mode
== SHOWFLAGS
) return (
505 /* on win (everything opened, only correct flags left) */
506 cell
.m
== STD_MINE
? op
.scheme
->field_flagged
:
507 /*.......................*/ op
.scheme
->number
[f
.c
[l
][c
].n
]);
509 cell
.f
== FLAG
? op
.scheme
->field_flagged
:
510 cell
.f
== QUESM
? op
.scheme
->field_question
:
511 cell
.o
== CLOSED
? op
.scheme
->field_closed
:
512 cell
.m
== STD_MINE
? op
.scheme
->mine_normal
:
513 cell
.m
== DEATH_MINE
? op
.scheme
->mine_death
:
514 /*.......................*/ op
.scheme
->number
[f
.c
[l
][c
].n
]);
517 void redraw_cell (int l
, int c
, int mode
) {
518 move_ph (l
+LINE_OFFSET
, field2screen_c(c
));
519 if (mode
== HIGHLIGHT
) printf ("\033[7m"); /* reverse video */
520 print (cell2schema(l
, c
, mode
));
521 if (mode
== HIGHLIGHT
) printf ("\033[0m"); /* reset all */
524 char* get_emoticon(void) {
525 return g
.o
==GAME_WON
? EMOT(WON
):
526 g
.o
==GAME_LOST
? EMOT(DEAD
):
530 /* https://zserge.com/blog/c-for-loop-tricks.html */
531 #define print_line(which) \
532 for (int _break = (printf("%s", BORDER(which,LEFT)), 1); _break; \
533 _break = 0, printf("%s\r\n", BORDER(which,RIGHT)))
534 #define print_border(which, width) \
535 print_line(which) printm (width, BORDER(which,MIDDLE))
536 void show_minefield (int mode
) {
537 int dtime
= difftime (time(NULL
), g
.t
)*!!g
.t
;
538 int half_spaces
= f
.w
*op
.scheme
->cell_width
/2;
539 int left_spaces
= MAX(0,half_spaces
-7-(f
.m
-g
.f
>999));
540 int right_spaces
= MAX(0,half_spaces
-6-(dtime
>999));
541 static char modechar
[] = {'*', '!', '?'};
545 print_border(TOP
, f
.w
);
547 printf("[%03d%c]%*s%s%*s[%03d]",
548 /* [ */ f
.m
- g
.f
, modechar
[g
.s
], /* ] */
549 left_spaces
,"", get_emoticon(), right_spaces
,"",
550 /* [ */ dtime
/* ] */);
552 print_border(DIVIDER
, f
.w
);
554 for (int l
= 0; l
< f
.h
; l
++) print_line(FIELD
)
555 printm (f
.w
, cell2schema(l
, _loop
, mode
));
556 print_border(BOTTOM
, f
.w
);
559 void show_stomp (int enable
, int l
, int c
) {
561 if (f
.c
[l
][c
].o
== OPENED
&& get_neighbours (l
, c
, 1) != 0) {
562 /* show the stomp radius if we aren't fully flagged */
564 if (AR_CELL
.o
== CLOSED
&& AR_CELL
.f
!= FLAG
)
565 redraw_cell (ROW
, COL
, HIGHLIGHT
);
566 fflush(stdout
); /* won't display without */
569 AROUND(l
, c
) redraw_cell (ROW
, COL
, g
.o
?SHOWMINES
:NORMAL
);
573 void wait_stomp (void) {
574 /* block SIGALRM, otherwise poll gets cancelled by the timer: */
577 sigaddset(&sig
, SIGALRM
);
578 sigprocmask (SIG_BLOCK
, &sig
, NULL
);
580 /* wait for timout or keypress: */
582 fds
.fd
= 0; fds
.events
= POLLIN
;
583 poll(&fds
, 1, STOMP_TIMEOUT
);
585 /* restore signal mask: */
586 sigprocmask (SIG_UNBLOCK
, &sig
, NULL
);
589 int get_neighbours (int line
, int col
, int reduced_mode
) {
590 /* counts mines surrounding a square
591 modes: 0=normal; 1=reduced */
595 count
+= !!AR_CELL
.m
;
596 count
-= reduced_mode
* AR_CELL
.f
==FLAG
;
601 void interactive_resize(void) {
602 unsigned char buf
[3];
606 screen_setup(1); /* clears the screen */
607 show_minefield (RESIZEMODE
);
608 /* show the new field size in the corner: */
609 move_ph(f
.h
+LINE_OFFSET
-1,COL_OFFSET
);
610 printf("%d x %d", f
.w
, f
.h
);
611 move_ph(LINE_BELOW
, 0);
612 printf("Resize Mode: Enter to set, Q to abort");
613 //TODO: need escape seq. to print lowercase in `-d' mode!
615 free_field(); /* must free before resizing! */
616 switch (getctrlseq(buf
)) {
617 case CTRSEQ_CURSOR_LEFT
: case 'h': f
.w
--; break;
618 case CTRSEQ_CURSOR_DOWN
: case 'j': f
.h
++; break;
619 case CTRSEQ_CURSOR_UP
: case 'k': f
.h
--; break;
620 case CTRSEQ_CURSOR_RIGHT
:case 'l': f
.w
++; break;
621 case 'w': f
.w
+=BIG_MOVE
; break;
622 case 'b': f
.w
-=BIG_MOVE
; break;
623 case 'u': f
.h
-=BIG_MOVE
; break;
624 case 'd': f
.h
+=BIG_MOVE
; break;
625 case 0xa: return; /* enter */
626 case 'q': /* abort */
634 f
.m
= mines_percentage(f
.w
, f
.h
);
635 f
.c
= alloc_array (f
.h
, f
.w
);
639 struct minecell
** alloc_array (int lines
, int cols
) {
640 free_field (); /* NOTE: this feels like a hack :| */
641 struct minecell
** a
= malloc (lines
* sizeof(struct minecell
*));
642 if (a
== NULL
) return NULL
;
643 for (int l
= 0; l
< lines
; l
++) {
644 a
[l
] = calloc (cols
, sizeof(struct minecell
));
645 if (a
[l
] == NULL
) goto unalloc
;
650 for (int l
= 0; l
< lines
; l
++)
655 void free_field (void) {
656 if (f
.c
== NULL
) return;
657 for (int l
= 0; l
< f
.h
; l
++) {
665 int screen2field_l (int l
) {
666 return (l
-LINE_OFFSET
) - 1;
668 int screen2field_c (int c
) {
669 /* this depends on the cell width and only works when it is 1 or 2. */
670 return (c
-COL_OFFSET
+1 - 2*(CW
%2))/2 - CW
/2;
672 int field2screen_c (int c
) {
673 return (CW
*c
+COL_OFFSET
- (CW
%2));
675 int clicked_emoticon (unsigned char* mouse
) {
676 return (mouse
[2] == LINE_OFFSET
-1 && (
677 mouse
[1] == f
.w
+COL_OFFSET
||
678 mouse
[1] == f
.w
+COL_OFFSET
+1));
687 int getctrlseq (unsigned char* buf
) {
690 int offset
= 0x20; /* never sends control chars as data */
691 while ((c
= getchar()) != EOF
) {
695 case '\033': state
=ESC_SENT
; break;
701 case '[': state
=CSI_SENT
; break;
702 default: return CTRSEQ_INVALID
;
707 case 'A': return CTRSEQ_CURSOR_UP
;
708 case 'B': return CTRSEQ_CURSOR_DOWN
;
709 case 'C': return CTRSEQ_CURSOR_RIGHT
;
710 case 'D': return CTRSEQ_CURSOR_LEFT
;
711 case 'M': state
=MOUSE_EVENT
; break;
712 default: return CTRSEQ_INVALID
;
717 buf
[1] = getchar() - offset
;
718 buf
[2] = getchar() - offset
;
721 return CTRSEQ_INVALID
;
727 int getch(unsigned char* buf
) {
728 /* returns a character, EOF, or constant for an escape/control sequence - NOT
729 compatible with the ncurses implementation of same name */
730 int action
= getctrlseq(buf
);
734 l
= screen2field_l (buf
[2]);
735 c
= screen2field_c (buf
[1]);
737 if (buf
[0] > 3) break; /* ignore all but left/middle/right/up */
738 int success
= wait_mouse_up(l
, c
);
740 /* mouse moved while pressed: */
741 if (!success
) return CTRSEQ_INVALID
;
744 case 0: return CTRSEQ_MOUSE_LEFT
;
745 case 1: return CTRSEQ_MOUSE_MIDDLE
;
746 case 2: return CTRSEQ_MOUSE_RIGHT
;
753 int getch_wrapper (void) {
754 unsigned char mouse
[3];
756 /*skip over ASCII_STX=0x02 that gets sent when returning from SIGALRM:*/
757 while ( (c
= getch(mouse
)) == 0x02);
759 if (c
== CTRSEQ_MOUSE_LEFT
|| c
== CTRSEQ_MOUSE_RIGHT
) {
760 if (clicked_emoticon(mouse
))
761 return WRAPPER_EMOTICON
;
763 if (screen2field_c (mouse
[1]) < 0
764 || screen2field_c (mouse
[1]) >= f
.w
765 || screen2field_l (mouse
[2]) < 0
766 || screen2field_l (mouse
[2]) >= f
.h
)
767 return CTRSEQ_INVALID
;
769 g
.p
[0] = screen2field_l (mouse
[2]);
770 g
.p
[1] = screen2field_c (mouse
[1]);
772 return c
; /* CTRSEQ_MOUSE_LEFT || CTRSEQ_MOUSE_RIGHT */
778 void clamp_fieldsize (void) {
779 /* clamp field size to terminal size and mouse maximum: */
781 ioctl(STDOUT_FILENO
, TIOCGWINSZ
, &w
);
783 /* seven is the minimum width of the status bar */
784 if (f
.w
< 7) f
.w
= 7;
785 if (f
.h
< 7) f
.h
= 7;
787 if (COL_OFFSET
+ f
.w
*CW
+ COL_OFFSET
> w
.ws_col
)
788 f
.w
= (w
.ws_col
- COL_OFFSET
- COL_OFFSET
)/DW
;
789 if (LINE_OFFSET
+ f
.h
+ LINES_AFTER
> w
.ws_row
)
790 f
.h
= w
.ws_row
- (LINE_OFFSET
+LINES_AFTER
);
791 if (COL_OFFSET
+ f
.w
*CW
> MOUSE_MAX
)
792 f
.w
= MOUSE_MAX
/CW
- COL_OFFSET
;
793 if (LINE_OFFSET
+ f
.h
> MOUSE_MAX
)
794 f
.h
= MOUSE_MAX
- LINE_OFFSET
;
797 int mines_percentage(int w
, int h
) {
798 return w
*h
*(w
*h
<30*16?.15625:.20625);
801 int parse_fieldspec(char* str
) {
802 /* parses the FIELDSPEC (WxHxM); returns 1 on error */
803 int n
= sscanf (str
, "%dx%dx%d", &f
.w
, &f
.h
, &f
.m
);
808 return 1; /* error */
810 f
.m
= mines_percentage(f
.w
, f
.h
);
813 f
.m
= MIN(f
.m
, (f
.w
-1) * (f
.h
-1)); /* limit mines */
818 void timer_setup (int enable
) {
819 static struct itimerval tbuf
;
820 tbuf
.it_interval
.tv_sec
= 1;
821 tbuf
.it_interval
.tv_usec
= 0;
825 tbuf
.it_value
.tv_sec
= 1;
826 tbuf
.it_value
.tv_usec
= 0;
827 if (setitimer(ITIMER_REAL
, &tbuf
, NULL
) == -1) {
832 tbuf
.it_value
.tv_sec
= 0;
833 tbuf
.it_value
.tv_usec
= 0;
834 if ( setitimer(ITIMER_REAL
, &tbuf
, NULL
) == -1 ) {
842 void signal_setup (void) {
843 struct sigaction saction
;
845 saction
.sa_handler
= signal_handler
;
846 sigemptyset(&saction
.sa_mask
);
847 saction
.sa_flags
= 0;
848 if (sigaction(SIGALRM
, &saction
, NULL
) < 0 ) {
853 if (sigaction(SIGINT
, &saction
, NULL
) < 0 ) {
859 void signal_handler (int signum
) {
863 dtime
= difftime (time(NULL
), g
.t
);
864 move_ph (1, f
.w
*CW
-(CW
%2)-3-(dtime
>999));
865 printf ("[%03d]", g
.t
?dtime
:0);
872 void screen_setup (int enable
) {
875 printf ("\033[s\033[?47h"); /* save cursor, alternate screen */
876 printf ("\033[H\033[J"); /* reset cursor, clear screen */
877 printf ("\033[?1000h\033[?25l"); /* enable mouse, hide cursor */
878 print (op
.scheme
->init_seq
); /* swich charset, if necessary */
880 print (op
.scheme
->reset_seq
); /* reset charset, if necessary */
881 printf ("\033[?9l\033[?25h"); /* disable mouse, show cursor */
882 printf ("\033[?47l\033[u"); /* primary screen, restore cursor */
887 /* http://users.csc.calpoly.edu/~phatalsk/357/lectures/code/sigalrm.c */
888 void raw_mode(int enable
) {
889 static struct termios saved_term_mode
;
890 struct termios raw_term_mode
;
893 tcgetattr(STDIN_FILENO
, &saved_term_mode
);
894 raw_term_mode
= saved_term_mode
;
895 raw_term_mode
.c_lflag
&= ~(ICANON
| ECHO
);
896 raw_term_mode
.c_cc
[VMIN
] = 1 ;
897 raw_term_mode
.c_cc
[VTIME
] = 0;
898 tcsetattr(STDIN_FILENO
, TCSAFLUSH
, &raw_term_mode
);
900 tcsetattr(STDIN_FILENO
, TCSAFLUSH
, &saved_term_mode
);