]>
git.gir.st - minesVIiper.git/blob - mines.c
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 HI_CELL f.c[g.p[0]][g.p[1]]
52 #define AR_CELL f.c[ROW][COL] /* helper for AROUND() */
53 #define AROUND(l, c) /* gives us loop-variables ROW, COL, AR_CELL */ \
54 for (int ROW = MAX(l-1, 0); ROW <= MIN(l+1, f.h-1); ROW++) \
55 for (int COL = MAX(c-1, 0); COL <= MIN(c+1, f.w-1); COL++) \
56 if (!(ROW == l && COL == c)) /* skip itself */
62 int main (int argc
, char** argv
) {
67 op
.scheme
= &symbols_mono
;
71 opterr
= 0; /* don't print message on unrecognized option */
72 while ((optget
= getopt (argc
, argv
, "+hnfqcdx")) != -1) {
74 case 'n': op
.mode
= NOFLAG
; break;
75 case 'f': op
.mode
= FLAG
; break; /*default*/
76 case 'q': op
.mode
= QUESM
; break; /*WARN:implicitly sets -f*/
77 case 'c': op
.scheme
= &symbols_col1
; break;
78 case 'd': op
.scheme
= &symbols_doublewidth
; break;
81 fprintf (stderr
, SHORTHELP LONGHELP
, argv
[0]);
82 return !(optget
=='h');
84 } if (optind
< argc
) { /* parse Fieldspec */
85 if (parse_fieldspec (argv
[optind
])) {
86 fprintf (stderr
, SHORTHELP FIELDHELP
, argv
[0]);
96 switch (minesviiper()) {
97 case GAME_NEW
: goto newgame
;
98 case GAME_WON
: g
.o
= GAME_WON
; break;
99 case GAME_LOST
:g
.o
= GAME_LOST
;break;
100 case GAME_QUIT
:goto quit
;
103 timer_setup(0); /* stop timer */
104 show_minefield (SHOWMINES
);
106 switch(getch_wrapper()) {
107 case WRAPPER_EMOTICON
:
108 case 'r': goto newgame
;
110 case CTRL_
'L': //TODO: updates the timer (fix in show_minefield())
112 show_minefield (SHOWMINES
);
115 interactive_resize();
124 int minesviiper(void) {
125 f
.c
= alloc_array (f
.h
, f
.w
);
126 g
= (const struct game
){0}; /* reset all game-specific parameters */
128 show_minefield (NORMAL
);
131 int actor
= KEYBOARD
; /* WARN: feels like a hack :| */
132 switch (getch_wrapper()) {
134 if (HI_CELL
.o
== OPENED
) goto open_cell
;
135 if (g
.s
== MODE_OPEN
) goto open_cell
;
136 if (g
.s
== MODE_FLAG
) goto flag_cell
;
137 if (g
.s
== MODE_QUESM
) goto quesm_cell
;
140 g
.s
= (g
.s
+1)%(op
.mode
+1);
141 show_minefield (g
.c
?SHOWMINES
:NORMAL
);
143 case CTRSEQ_MOUSE_LEFT
:
148 switch (do_uncover(&(g
.n
), actor
)) {
149 case GAME_LOST
: return GAME_LOST
;
150 case GAME_WON
: return GAME_WON
;
154 case CTRSEQ_MOUSE_RIGHT
:
155 case 'i': flag_square (g
.p
[0], g
.p
[1]); break;
157 case '?':quesm_square (g
.p
[0], g
.p
[1]); break;
158 case CTRSEQ_CURSOR_LEFT
:
159 case 'h': move_hi (g
.p
[0], g
.p
[1]-1); break;
160 case CTRSEQ_CURSOR_DOWN
:
161 case 'j': move_hi (g
.p
[0]+1, g
.p
[1] ); break;
162 case CTRSEQ_CURSOR_UP
:
163 case 'k': move_hi (g
.p
[0]-1, g
.p
[1] ); break;
164 case CTRSEQ_CURSOR_RIGHT
:
165 case 'l': move_hi (g
.p
[0], g
.p
[1]+1); break;
166 case 'w': to_next_boundary (g
.p
[0], g
.p
[1], '>'); break;
167 case 'b': to_next_boundary (g
.p
[0], g
.p
[1], '<'); break;
168 case 'u': to_next_boundary (g
.p
[0], g
.p
[1], '^'); break;
169 case 'd': to_next_boundary (g
.p
[0], g
.p
[1], 'v'); break;
170 case '0': /* fallthrough */
171 case '^': move_hi (g
.p
[0], 0 ); break;
172 case '$': move_hi (g
.p
[0], f
.w
-1 ); break;
173 case 'g': move_hi (0, g
.p
[1] ); break;
174 case 'G': move_hi (f
.h
-1, g
.p
[1] ); break;
175 case 'z': move_hi (f
.h
/2, f
.w
/2); break;
176 case 'm': set_mark(); break;
177 case'\'': /* fallthrough */
178 case '`': jump_mark(); break;
179 case 'f': find(getch_wrapper(), '>'); break;
180 case 'F': find(getch_wrapper(), '<'); break;
181 case 't': till(getch_wrapper(), '>'); break;
182 case 'T': till(getch_wrapper(), '<'); break;
183 case WRAPPER_EMOTICON
:
184 case 'r': timer_setup(0); return GAME_NEW
;
185 case 'q': return GAME_QUIT
;
188 show_minefield (NORMAL
);
192 interactive_resize();
195 if (g
.n
== GAME_NEW
) break; /* must open a cell first */
197 show_minefield (g
.c
?SHOWMINES
:NORMAL
);
209 /* I haven't won as long as a cell exists, that
210 - I haven't opened, and
212 int everything_opened (void) {
213 for (int row
= 0; row
< f
.h
; row
++)
214 for (int col
= 0; col
< f
.w
; col
++)
215 if (f
.c
[row
][col
].o
== CLOSED
&&
216 f
.c
[row
][col
].m
== NO_MINE
) return 0;
220 int wait_mouse_up (int l
, int c
) { /* TODO: should not take minefield-coords but absolute ones */
221 unsigned char mouse2
[3];
226 move_ph (1, field2screen_c (f
.w
/2)-1); print (EMOT(OHH
));
228 if (!(l
< 0 || l
>= f
.h
|| c
< 0 || c
>= f
.w
)) {
229 /* show a pushed-in button if cursor is on minefield */
230 redraw_cell (l
, c
, HIGHLIGHT
);
236 if (getctrlseq (mouse2
) == CTRSEQ_MOUSE
) {
237 /* ignore mouse wheel events: */
238 if (mouse2
[0] & 0x40) continue;
240 else if((mouse2
[0]&3) == 3) level
--; /* release event */
241 else level
++; /* another button pressed */
245 move_ph (1, field2screen_c (f
.w
/2)-1); print (get_emoticon());
247 if (!(l
< 0 || l
>= f
.h
|| c
< 0 || c
>= f
.w
)) {
248 redraw_cell (l
, c
, g
.o
?SHOWMINES
:NORMAL
);
251 c2
= screen2field_c(mouse2
[1]);
252 l2
= screen2field_l(mouse2
[2]);
253 return ((l2
== l
) && (c2
== c
));
256 int choord_square (int line
, int col
) {
257 if (uncover_square (line
, col
)) return 1;
259 if (AR_CELL
.f
!= FLAG
) {
260 if (uncover_square (ROW
, COL
))
267 int uncover_square (int l
, int c
) {
268 if (f
.c
[l
][c
].o
== OPENED
)
269 return 0; /* nothing to do */
271 f
.c
[l
][c
].o
= OPENED
;
272 f
.c
[l
][c
].f
= NOFLAG
; /*must not be QUESM, otherwise rendering issues*/
273 redraw_cell (l
, c
, NORMAL
);
276 f
.c
[l
][c
].m
= DEATH_MINE
;
280 /* check for chording */
281 if (f
.c
[l
][c
].n
== 0) {
283 if (uncover_square (ROW
, COL
)) return 1;
289 void flag_square (int l
, int c
) {
290 static char modechar
[] = {'*', '!', '?'};
292 if (f
.c
[l
][c
].o
!= CLOSED
) return;
293 /* cycle through flag/quesm/noflag: */
294 f
.c
[l
][c
].f
= (f
.c
[l
][c
].f
+ 1) % (op
.mode
+ 1);
295 if (f
.c
[l
][c
].f
==FLAG
) g
.f
++;
296 else if ((op
.mode
==FLAG
&& f
.c
[l
][c
].f
==NOFLAG
) ||
297 (op
.mode
==QUESM
&& f
.c
[l
][c
].f
==QUESM
)) g
.f
--;
298 redraw_cell (l
, c
, NORMAL
);
299 move_ph (1, op
.scheme
->cell_width
);
300 printf ("[%03d%c]", f
.m
- g
.f
, modechar
[g
.s
]);
303 void quesm_square (int l
, int c
) {
304 /* toggle question mark / none. won't turn flags into question marks.
305 unlike flag_square, this function doesn't respect `-q'. */
306 if (f
.c
[l
][c
].o
!= CLOSED
) return;
307 else if (f
.c
[l
][c
].f
== NOFLAG
) f
.c
[l
][c
].f
= QUESM
;
308 else if (f
.c
[l
][c
].f
== QUESM
) f
.c
[l
][c
].f
= NOFLAG
;
309 redraw_cell (l
, c
, NORMAL
);
312 int do_uncover (int* is_newgame
, int actor
) {
313 if (*is_newgame
== GAME_NEW
) {
314 *is_newgame
= GAME_INPROGRESS
;
315 fill_minefield (g
.p
[0], g
.p
[1]);
319 if (HI_CELL
.f
== FLAG
) return GAME_INPROGRESS
;
320 if (HI_CELL
.o
== CLOSED
) {
321 if (uncover_square (g
.p
[0], g
.p
[1])) return GAME_LOST
;
322 } else if (get_neighbours (g
.p
[0], g
.p
[1], 1) == 0) {
323 if (choord_square (g
.p
[0], g
.p
[1])) return GAME_LOST
;
324 } else if (actor
!= MOUSE
) {
325 show_stomp(1, g
.p
[0], g
.p
[1]);
329 show_stomp(0, g
.p
[0], g
.p
[1]);
331 if (everything_opened()) return GAME_WON
;
333 return GAME_INPROGRESS
;
336 void set_mark(void) {
337 int mark
= tolower(getch_wrapper());
338 if (mark
< 'a' || mark
> 'z') return; /*out of bound*/
340 g
.m
[mark
-'a'].l
= g
.p
[0];
341 g
.m
[mark
-'a'].c
= g
.p
[1];
345 void jump_mark(void) {
346 int mark
= tolower(getch_wrapper());
347 /* check bounds and if set: */
348 if (mark
< 'a' || mark
> 'z' || !g
.m
[mark
-'a'].s
) return;
349 move_hi (g
.m
[mark
-'a'].l
, g
.m
[mark
-'a'].c
);
352 void fill_minefield (int l
, int c
) {
356 int line
= rand() % f
.h
;
357 int col
= rand() % f
.w
;
359 if (f
.c
[line
][col
].m
) {
360 /* skip if field already has a mine */
362 } else if ((line
== l
) && (col
== c
)) {
363 /* don't put a mine on the already opened (first click) field */
367 f
.c
[line
][col
].m
= STD_MINE
;
371 /* precalculate neighbours */
372 for (int l
=0; l
< f
.h
; l
++)
373 for (int c
=0; c
< f
.w
; c
++)
374 f
.c
[l
][c
].n
= get_neighbours (l
, c
, NORMAL
);
377 void move_ph (int line
, int col
) {
378 /* move printhead to zero-indexed position */
379 printf ("\033[%d;%dH", line
+1, col
+1);
382 void move_hi (int l
, int c
) {
383 /* move cursor and highlight to absolute coordinates */
385 redraw_cell (g
.p
[0], g
.p
[1], NORMAL
);
387 g
.p
[0] = CLAMP(l
, 0, f
.h
-1);
388 g
.p
[1] = CLAMP(c
, 0, f
.w
-1);
389 move_ph (g
.p
[0]+LINE_OFFSET
, field2screen_c(g
.p
[1]));
391 redraw_cell (g
.p
[0], g
.p
[1], HIGHLIGHT
);
394 /* to_next_boundary(): move into the supplied direction until a change in open-
395 state or flag-state is found and move there. falls back to BIG_MOVE. */
396 #define FIND_NEXT(X, L, C, L1, C1, MAX, OP) do {\
397 new_ ## X OP ## = BIG_MOVE;\
398 for (int i = X OP 1; i > 0 && i < f.MAX-1; i OP ## OP)\
399 if (((f.c[L ][C ].o<<2) + f.c[L ][C ].f) \
400 != ((f.c[L1][C1].o<<2) + f.c[L1][C1].f)) {\
405 void to_next_boundary (int l
, int c
, char direction
) {
409 case '>': FIND_NEXT(c
, l
, i
, l
, i
+1, w
, +); break;
410 case '<': FIND_NEXT(c
, l
, i
, l
, i
-1, w
, -); break;
411 case '^': FIND_NEXT(l
, i
, c
, i
-1, c
, h
, -); break;
412 case 'v': FIND_NEXT(l
, i
, c
, i
+1, c
, h
, +); break;
415 move_hi (new_l
, new_c
);
419 #define CELL f.c[g.p[0]][c]
420 #define FIND_WHAT(CONDITION, DIRECTION) do {\
421 int plusminus = DIRECTION-'='; /* -1 if '<', 1 if '>' */ \
422 for (int c = g.p[1]+plusminus; c >= 0 && c < f.w; c+=plusminus) \
424 move_hi (g.p[0], c); \
425 return 1; /* NOTE: break didn't work */ \
429 int find (int what
, char direction
) {
431 case ' ': what
= '0'; /* fallthrough */
432 case '0': case '1': case '2':
433 case '3': case '4': case '5':
434 case '6': case '7': case '8': /* numbers, opened */
435 FIND_WHAT(CELL
.o
&& CELL
.n
== what
-'0', direction
);
436 case 'o': FIND_WHAT(CELL
.o
, direction
); /* any opened */
437 case 'c': FIND_WHAT(!CELL
.o
, direction
); /* any closed */
438 case 'f': case 'i': FIND_WHAT(CELL
.f
==FLAG
, direction
); /* is flagged */
439 case '?': FIND_WHAT(CELL
.f
==QUESM
, direction
);/* questioned */
447 void till (int what
, char direction
) {
448 /* if we found what we were looking for move one cell back */
449 if (find (what
, direction
)) move_hi(g
.p
[0], g
.p
[1]-(direction
-'='));
452 char* cell2schema (int l
, int c
, int mode
) {
453 struct minecell cell
= f
.c
[l
][c
];
455 if (mode
== SHOWMINES
) return (
456 cell
.f
== FLAG
&& cell
.m
? op
.scheme
->field_flagged
:
457 cell
.f
== FLAG
&& !cell
.m
? op
.scheme
->mine_wrongf
:
458 cell
.m
== STD_MINE
? op
.scheme
->mine_normal
:
459 cell
.m
== DEATH_MINE
? op
.scheme
->mine_death
:
460 cell
.o
== CLOSED
? op
.scheme
->field_closed
:
461 /*.......................*/ op
.scheme
->number
[f
.c
[l
][c
].n
]);
463 cell
.f
== FLAG
? op
.scheme
->field_flagged
:
464 cell
.f
== QUESM
? op
.scheme
->field_question
:
465 cell
.o
== CLOSED
? op
.scheme
->field_closed
:
466 cell
.m
== STD_MINE
? op
.scheme
->mine_normal
:
467 cell
.m
== DEATH_MINE
? op
.scheme
->mine_death
:
468 /*.......................*/ op
.scheme
->number
[f
.c
[l
][c
].n
]);
471 void redraw_cell (int l
, int c
, int mode
) {
472 move_ph (l
+LINE_OFFSET
, field2screen_c(c
));
473 if (mode
== HIGHLIGHT
) printf ("\033[7m"); /* reverse video */
474 print (cell2schema(l
, c
, mode
));
475 if (mode
== HIGHLIGHT
) printf ("\033[0m"); /* reset all */
478 char* get_emoticon(void) {
479 return g
.o
==GAME_WON
? EMOT(WON
):
480 g
.o
==GAME_LOST
? EMOT(DEAD
):
484 /* https://zserge.com/blog/c-for-loop-tricks.html */
485 #define print_line(which) \
486 for (int _break = (printf("%s", BORDER(which,LEFT)), 1); _break; \
487 _break = 0, printf("%s\r\n", BORDER(which,RIGHT)))
488 #define print_border(which, width) \
489 print_line(which) printm (width, BORDER(which,MIDDLE))
490 void show_minefield (int mode
) {
491 //TODO: in `-d' mode if f.w is odd we are 1 char short in the status line
492 //TODO: in `-d' mode we need to specify a charset to print lowercase (-> resize mode)
493 int dtime
= difftime (time(NULL
), g
.t
)*!!g
.t
;
494 int half_spaces
= f
.w
*op
.scheme
->cell_width
/2;
495 int left_spaces
= MAX(0,half_spaces
-7-(f
.m
-g
.f
>999));
496 int right_spaces
= MAX(0,half_spaces
-6-(dtime
>999));
497 static char modechar
[] = {'*', '!', '?'};
501 print_border(TOP
, f
.w
);
503 if (mode
== RESIZEMODE
) printf ("%-*s", 2*half_spaces
,
504 f
.w
*CW
>53?"Resize Mode: HJKL to resize, Enter to set, Q to abort":
505 f
.w
*CW
>25?"Resize Mode: HJKL/Enter/Q":"HJKL/Enter/Q");
506 else printf("[%03d%c]%*s%s%*s[%03d]",
507 /* [ */ f
.m
- g
.f
, modechar
[g
.s
], /* ] */
508 left_spaces
,"", get_emoticon(), right_spaces
,"",
509 /* [ */ dtime
/* ] */);
511 print_border(DIVIDER
, f
.w
);
513 for (int l
= 0; l
< f
.h
; l
++) print_line(FIELD
)
514 printm (f
.w
, cell2schema(l
, _loop
, mode
));
515 print_border(BOTTOM
, f
.w
);
518 void show_stomp (int enable
, int l
, int c
) {
520 if (f
.c
[l
][c
].o
== OPENED
&& get_neighbours (l
, c
, 1) != 0) {
521 /* show the stomp radius if we aren't fully flagged */
523 if (AR_CELL
.o
== CLOSED
&& AR_CELL
.f
!= FLAG
)
524 redraw_cell (ROW
, COL
, HIGHLIGHT
);
525 fflush(stdout
); /* won't display without */
529 AROUND(l
, c
) redraw_cell (ROW
, COL
, g
.o
?SHOWMINES
:NORMAL
);
533 void wait_stomp (void) {
534 /* block SIGALRM, otherwise poll gets cancelled by the timer: */
537 sigaddset(&sig
, SIGALRM
);
538 sigprocmask (SIG_BLOCK
, &sig
, NULL
);
540 /* wait for timout or keypress: */
542 fds
.fd
= 0; fds
.events
= POLLIN
;
543 poll(&fds
, 1, STOMP_TIMEOUT
);
545 /* restore signal mask: */
546 sigprocmask (SIG_UNBLOCK
, &sig
, NULL
);
549 int get_neighbours (int line
, int col
, int reduced_mode
) {
550 /* counts mines surrounding a square
551 modes: 0=normal; 1=reduced */
555 count
+= !!AR_CELL
.m
;
556 count
-= reduced_mode
* AR_CELL
.f
==FLAG
;
561 void interactive_resize(void) {
562 unsigned char buf
[3];
566 screen_setup(1); /* clears the screen */
567 show_minefield (RESIZEMODE
);
568 /* show the new field size in the corner: */
569 move_ph(f
.h
+LINE_OFFSET
-1,COL_OFFSET
);
570 printf("%d x %d", f
.w
, f
.h
);
572 free_field(); /* must free before resizing! */
573 switch (getctrlseq(buf
)) {
574 case CTRSEQ_CURSOR_LEFT
: case 'h': f
.w
--; break;
575 case CTRSEQ_CURSOR_DOWN
: case 'j': f
.h
++; break;
576 case CTRSEQ_CURSOR_UP
: case 'k': f
.h
--; break;
577 case CTRSEQ_CURSOR_RIGHT
:case 'l': f
.w
++; break;
578 case 'w': f
.w
+=BIG_MOVE
; break;
579 case 'b': f
.w
-=BIG_MOVE
; break;
580 case 'u': f
.h
-=BIG_MOVE
; break;
581 case 'd': f
.h
+=BIG_MOVE
; break;
582 case 0xa: return; /* enter */
583 case 'q': /* abort */
591 f
.m
= mines_percentage(f
.w
, f
.h
);
592 f
.c
= alloc_array (f
.h
, f
.w
);
596 struct minecell
** alloc_array (int lines
, int cols
) {
597 free_field (); /* NOTE: this feels like a hack :| */
598 struct minecell
** a
= malloc (lines
* sizeof(struct minecell
*));
599 if (a
== NULL
) return NULL
;
600 for (int l
= 0; l
< lines
; l
++) {
601 a
[l
] = calloc (cols
, sizeof(struct minecell
));
602 if (a
[l
] == NULL
) goto unalloc
;
607 for (int l
= 0; l
< lines
; l
++)
612 void free_field (void) {
613 if (f
.c
== NULL
) return;
614 for (int l
= 0; l
< f
.h
; l
++) {
622 int screen2field_l (int l
) {
623 return (l
-LINE_OFFSET
) - 1;
625 int screen2field_c (int c
) {
626 /* this depends on the cell width and only works when it is 1 or 2. */
627 return (c
-COL_OFFSET
+1 - 2*(CW
%2))/2 - CW
/2;
629 int field2screen_c (int c
) {
630 return (CW
*c
+COL_OFFSET
- (CW
%2));
632 int clicked_emoticon (unsigned char* mouse
) {
633 return (mouse
[2] == LINE_OFFSET
-1 && (
634 mouse
[1] == f
.w
+COL_OFFSET
||
635 mouse
[1] == f
.w
+COL_OFFSET
+1));
644 int getctrlseq (unsigned char* buf
) {
647 int offset
= 0x20; /* never sends control chars as data */
648 while ((c
= getchar()) != EOF
) {
652 case '\033': state
=ESC_SENT
; break;
658 case '[': state
=CSI_SENT
; break;
659 default: return CTRSEQ_INVALID
;
664 case 'A': return CTRSEQ_CURSOR_UP
;
665 case 'B': return CTRSEQ_CURSOR_DOWN
;
666 case 'C': return CTRSEQ_CURSOR_RIGHT
;
667 case 'D': return CTRSEQ_CURSOR_LEFT
;
668 case 'M': state
=MOUSE_EVENT
; break;
669 default: return CTRSEQ_INVALID
;
674 buf
[1] = getchar() - offset
;
675 buf
[2] = getchar() - offset
;
678 return CTRSEQ_INVALID
;
684 int getch(unsigned char* buf
) {
685 /* returns a character, EOF, or constant for an escape/control sequence - NOT
686 compatible with the ncurses implementation of same name */
687 int action
= getctrlseq(buf
);
691 l
= screen2field_l (buf
[2]);
692 c
= screen2field_c (buf
[1]);
694 if (buf
[0] > 3) break; /* ignore all but left/middle/right/up */
695 int success
= wait_mouse_up(l
, c
);
697 /* mouse moved while pressed: */
698 if (!success
) return CTRSEQ_INVALID
;
701 case 0: return CTRSEQ_MOUSE_LEFT
;
702 case 1: return CTRSEQ_MOUSE_MIDDLE
;
703 case 2: return CTRSEQ_MOUSE_RIGHT
;
710 int getch_wrapper (void) {
711 unsigned char mouse
[3];
713 /*skip over ASCII_STX=0x02 that gets sent when returning from SIGALRM:*/
714 while ( (c
= getch(mouse
)) == 0x02);
716 if (c
== CTRSEQ_MOUSE_LEFT
|| c
== CTRSEQ_MOUSE_RIGHT
) {
717 if (clicked_emoticon(mouse
))
718 return WRAPPER_EMOTICON
;
720 if (screen2field_c (mouse
[1]) < 0
721 || screen2field_c (mouse
[1]) >= f
.w
722 || screen2field_l (mouse
[2]) < 0
723 || screen2field_l (mouse
[2]) >= f
.h
)
724 return CTRSEQ_INVALID
;
726 g
.p
[0] = screen2field_l (mouse
[2]);
727 g
.p
[1] = screen2field_c (mouse
[1]);
729 return c
; /* CTRSEQ_MOUSE_LEFT || CTRSEQ_MOUSE_RIGHT */
735 void clamp_fieldsize (void) {
736 /* clamp field size to terminal size and mouse maximum: */
738 ioctl(STDOUT_FILENO
, TIOCGWINSZ
, &w
);
740 if (f
.w
< 1) f
.w
= 1;
741 if (f
.h
< 1) f
.h
= 1;
743 if (COL_OFFSET
+ f
.w
*CW
+ COL_OFFSET
> w
.ws_col
)
744 f
.w
= (w
.ws_col
- COL_OFFSET
- COL_OFFSET
)/CW
; //TODO: does not work in `-d' (in xterm)
745 if (LINE_OFFSET
+ f
.h
+ LINES_AFTER
> w
.ws_row
)
746 f
.h
= w
.ws_row
- (LINE_OFFSET
+LINES_AFTER
);
747 if (COL_OFFSET
+ f
.w
*CW
> MOUSE_MAX
)
748 f
.w
= MOUSE_MAX
/CW
- COL_OFFSET
;
749 if (LINE_OFFSET
+ f
.h
> MOUSE_MAX
)
750 f
.h
= MOUSE_MAX
- LINE_OFFSET
;
753 int mines_percentage(int w
, int h
) {
754 return w
*h
*(w
*h
<30*16?.15625:.20625);
757 int parse_fieldspec(char* str
) {
758 /* parses the FIELDSPEC (WxHxM); returns 1 on error */
759 int n
= sscanf (str
, "%dx%dx%d", &f
.w
, &f
.h
, &f
.m
);
764 return 1; /* error */
766 f
.m
= mines_percentage(f
.w
, f
.h
);
769 f
.m
= MIN(f
.m
, (f
.w
-1) * (f
.h
-1)); /* limit mines */
774 void timer_setup (int enable
) {
775 static struct itimerval tbuf
;
776 tbuf
.it_interval
.tv_sec
= 1;
777 tbuf
.it_interval
.tv_usec
= 0;
781 tbuf
.it_value
.tv_sec
= 1;
782 tbuf
.it_value
.tv_usec
= 0;
783 if (setitimer(ITIMER_REAL
, &tbuf
, NULL
) == -1) {
788 tbuf
.it_value
.tv_sec
= 0;
789 tbuf
.it_value
.tv_usec
= 0;
790 if ( setitimer(ITIMER_REAL
, &tbuf
, NULL
) == -1 ) {
798 void signal_setup (void) {
799 struct sigaction saction
;
801 saction
.sa_handler
= signal_handler
;
802 sigemptyset(&saction
.sa_mask
);
803 saction
.sa_flags
= 0;
804 if (sigaction(SIGALRM
, &saction
, NULL
) < 0 ) {
809 if (sigaction(SIGINT
, &saction
, NULL
) < 0 ) {
815 void signal_handler (int signum
) {
819 dtime
= difftime (time(NULL
), g
.t
);
820 move_ph (1, f
.w
*CW
-(CW
%2)-3-(dtime
>999));
821 printf ("[%03d]", g
.t
?dtime
:0);
828 void screen_setup (int enable
) {
831 printf ("\033[s\033[?47h"); /* save cursor, alternate screen */
832 printf ("\033[H\033[J"); /* reset cursor, clear screen */
833 printf ("\033[?1000h\033[?25l"); /* enable mouse, hide cursor */
834 print (op
.scheme
->init_seq
); /* swich charset, if necessary */
836 print (op
.scheme
->reset_seq
); /* reset charset, if necessary */
837 printf ("\033[?9l\033[?25h"); /* disable mouse, show cursor */
838 printf ("\033[?47l\033[u"); /* primary screen, restore cursor */
843 /* http://users.csc.calpoly.edu/~phatalsk/357/lectures/code/sigalrm.c */
844 void raw_mode(int enable
) {
845 static struct termios saved_term_mode
;
846 struct termios raw_term_mode
;
849 tcgetattr(STDIN_FILENO
, &saved_term_mode
);
850 raw_term_mode
= saved_term_mode
;
851 raw_term_mode
.c_lflag
&= ~(ICANON
| ECHO
);
852 raw_term_mode
.c_cc
[VMIN
] = 1 ;
853 raw_term_mode
.c_cc
[VTIME
] = 0;
854 tcsetattr(STDIN_FILENO
, TCSAFLUSH
, &raw_term_mode
);
856 tcsetattr(STDIN_FILENO
, TCSAFLUSH
, &saved_term_mode
);