]>
git.gir.st - minesVIiper.git/blob - mines_2017.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 - bduw to jump left/down/up/right by 5 cells
12 - o to open and choord
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*/
35 #define MIN(a,b) (a>b?b:a)
36 #define MAX(a,b) (a>b?a:b)
37 #define CLAMP(a,m,M) (a<m?m:(a>M?M:a))
38 #define printm(num, str) for (int i = 0; i < num; i++) fputs (str, stdout)
39 #define print(str) fputs (str, stdout)
40 #define EMOT(e) op.scheme->emoticons[EMOT_ ## e]
43 unsigned m
:2; /* mine?1:killmine?2:0 */
44 unsigned o
:1; /* open?1:0 */
45 unsigned f
:2; /* flagged?1:questioned?2:0 */
46 unsigned n
:4; /* 0<= neighbours <=8 */
50 //TODO: rename w, h to L, C
53 int m
; /* number of mines */
55 int f
; /* flags counter */
56 int t
; /* time of game start */
57 int p
[2]; /* cursor position {line, col} */
58 int s
; /* space mode */
62 struct minescheme
* scheme
;
63 int mode
; /* allow flags? quesm? */
73 void fill_minefield (int, int);
75 void cursor_move (int, int);
76 void to_next_boundary (int l
, int c
, char direction
);
77 int getch (unsigned char*);
78 int getctrlseq (unsigned char*);
79 int everything_opened ();
80 int wait_mouse_up (int, int);
81 void partial_show_minefield (int, int, int);
82 void show_minefield (int);
83 int get_neighbours (int, int, int);
84 int uncover_square (int, int);
85 void flag_square (int, int);
86 void quesm_square (int, int);
87 int choord_square (int, int);
88 int do_uncover (int*);
89 struct minecell
** alloc_array (int, int);
91 int screen2field_l (int);
92 int screen2field_c (int);
93 int field2screen_l (int);
94 int field2screen_c (int);
96 void signal_handler (int signum
);
97 void timer_setup (int);
110 enum fieldopenstates
{
126 /* for getctrlseq() */
132 CTRSEQ_MOUSE_LEFT
= -4,
133 CTRSEQ_MOUSE_MIDDLE
= -5,
134 CTRSEQ_MOUSE_RIGHT
= -6,
142 void signal_handler (int signum
) {
145 move (1, f
.w
*op
.scheme
->cell_width
-(op
.scheme
->cell_width
%2)-3);
146 printf ("[%03d]", f
.t
?(int)difftime (time(NULL
), f
.t
):0);
153 /* http://users.csc.calpoly.edu/~phatalsk/357/lectures/code/sigalrm.c */
154 struct termios saved_term_mode
;
155 struct termios
set_raw_term_mode() {
156 struct termios cur_term_mode
, raw_term_mode
;
158 tcgetattr(STDIN_FILENO
, &cur_term_mode
);
159 raw_term_mode
= cur_term_mode
;
160 raw_term_mode
.c_lflag
&= ~(ICANON
| ECHO
);
161 raw_term_mode
.c_cc
[VMIN
] = 1 ;
162 raw_term_mode
.c_cc
[VTIME
] = 0;
163 tcsetattr(STDIN_FILENO
, TCSAFLUSH
, &raw_term_mode
);
165 return cur_term_mode
;
167 void restore_term_mode(struct termios saved_term_mode
) {
168 tcsetattr(STDIN_FILENO
, TCSAFLUSH
, &saved_term_mode
);
172 int main (int argc
, char** argv
) {
173 struct sigaction saction
;
174 saved_term_mode
= set_raw_term_mode();
178 saction
.sa_handler
= signal_handler
;
179 sigemptyset(&saction
.sa_mask
);
180 saction
.sa_flags
= 0;
181 if (sigaction(SIGALRM
, &saction
, NULL
) < 0 ) {
186 if (sigaction(SIGINT
, &saction
, NULL
) < 0 ) {
190 /* end screen setup */
196 f
.c
= NULL
; /*to not free() array before it is allocated*/
198 op
.scheme
= &symbols_mono
;
203 opterr
= 0; /* don't print message on unrecognized option */
204 while ((optget
= getopt (argc
, argv
, "+hnfqcdx")) != -1) {
206 case 'n': op
.mode
= NOFLAG
; break;
207 case 'f': op
.mode
= FLAG
; break; /*default*/
208 case 'q': op
.mode
= QUESM
; break;
209 case 'c': op
.scheme
= &symbols_col1
; break;
210 case 'd': op
.scheme
= &symbols_doublewidth
; break;
213 fprintf (stderr
, "%s [OPTIONS] [FIELDSPEC]\n"
217 " -q(uestion marks)\n"
218 " -c(olored symbols)\n"
219 " -d(ec charset symbols)\n"
221 " WxHxM (width 'x' height 'x' mines)\n"
222 " defaults to 30x16x99\n"
225 " hjkl: move left/down/up/right\n"
226 " bduw: move to next boundary\n"
227 " ^Gg$: move to the left/bottom/top/right\n"
230 " space:modeful cursor (either open or flag)\n"
231 " a: toggle mode for space (open/flag)\n"
232 " mX: set a mark for letter X\n"
233 " `X: move to mark X (aliased to ')\n"
234 " r: start a new game\n"
235 " q: quit\n", argv
[0]);
239 /* end parse options*/
241 int n
= sscanf (argv
[optind
], "%dx%dx%d", &f
.w
, &f
.h
, &f
.m
);
242 if (n
== 2 && f
.w
== 8 && f
.h
== 8) f
.m
= 10;
243 if (n
== 2 && f
.w
== 16 && f
.h
== 16) f
.m
= 40;
245 /* check boundaries */
246 if (f
.m
> (f
.w
-1) * (f
.h
-1)) {
247 f
.m
= (f
.w
-1) * (f
.h
-1);
248 fprintf (stderr
, "too many mines. reduced to %d.\r\n", f
.m
);
253 f
.c
= alloc_array (f
.h
, f
.w
);
263 struct line_col markers
[26];
264 for (int i
=26; i
; markers
[--i
].l
= -1);
266 /* switch to alternate screen */
267 printf ("\033[?47h");
269 /* reset cursor, clear screen */
270 printf ("\033[H\033[J");
272 /* swich charset, if necessary */
273 if (op
.scheme
->init_seq
!= NULL
) print (op
.scheme
->init_seq
);
275 show_minefield (NORMAL
);
277 /* enable mouse, hide cursor */
278 printf ("\033[?1000h\033[?25l");
283 unsigned char mouse
[3];
285 action
= getch(mouse
);
288 if (f
.s
== MODE_OPEN
||
289 f
.c
[f
.p
[0]][f
.p
[1]].o
== OPENED
) {
290 switch (do_uncover(&is_newgame
)) {
291 case GAME_LOST
: goto lose
;
292 case GAME_WON
: goto win
;
294 } else if (f
.s
== MODE_FLAG
) {
295 flag_square (f
.p
[0], f
.p
[1]);
296 } else if (f
.s
== MODE_QUESM
) {
297 quesm_square (f
.p
[0], f
.p
[1]);
301 f
.s
= (f
.s
+1)%(op
.mode
+1);
302 show_minefield (cheatmode
?SHOWMINES
:NORMAL
);
304 case CTRSEQ_MOUSE_LEFT
:
305 f
.p
[0] = screen2field_l (mouse
[2]);
306 f
.p
[1] = screen2field_c (mouse
[1]);
307 /* :D clicked: TODO: won't work in single-width mode! */
308 if (mouse
[2] == LINE_OFFSET
-1 &&
309 (mouse
[1] == f
.w
+COL_OFFSET
||
310 mouse
[1] == f
.w
+COL_OFFSET
+1)) {
314 if (f
.p
[1] < 0 || f
.p
[1] >= f
.w
||
315 f
.p
[0] < 0 || f
.p
[0] >= f
.h
) break; /*out of bound*/
318 switch (do_uncover(&is_newgame
)) {
319 case GAME_LOST
: goto lose
;
320 case GAME_WON
: goto win
;
323 case CTRSEQ_MOUSE_RIGHT
:
324 f
.p
[0] = screen2field_l (mouse
[2]);
325 f
.p
[1] = screen2field_c (mouse
[1]);
326 if (f
.p
[1] < 0 || f
.p
[1] >= f
.w
||
327 f
.p
[0] < 0 || f
.p
[0] >= f
.h
) break; /*out of bound*/
329 case 'i': flag_square (f
.p
[0], f
.p
[1]); break;
330 case '?':quesm_square (f
.p
[0], f
.p
[1]); break;
331 case 'h': cursor_move (f
.p
[0], f
.p
[1]-1 ); break;
332 case 'j': cursor_move (f
.p
[0]+1, f
.p
[1] ); break;
333 case 'k': cursor_move (f
.p
[0]-1, f
.p
[1] ); break;
334 case 'l': cursor_move (f
.p
[0], f
.p
[1]+1 ); break;
335 case 'w': to_next_boundary (f
.p
[0], f
.p
[1], '>'); break;
336 case 'b': to_next_boundary (f
.p
[0], f
.p
[1], '<'); break;
337 case 'u': to_next_boundary (f
.p
[0], f
.p
[1], '^'); break;
338 case 'd': to_next_boundary (f
.p
[0], f
.p
[1], 'v'); break;
339 case '0': /* fallthrough */
340 case '^': cursor_move (f
.p
[0], 0 ); break;
341 case '$': cursor_move (f
.p
[0], f
.w
-1 ); break;
342 case 'g': cursor_move (0, f
.p
[1] ); break;
343 case 'G': cursor_move (f
.h
-1, f
.p
[1] ); break;
345 action
= tolower(getch(mouse
));
346 if (action
< 'a' || action
> 'z') break;/*out of bound*/
347 markers
[action
-'a'].l
= f
.p
[0];
348 markers
[action
-'a'].c
= f
.p
[1];
350 case'\'': /* fallthrough */
352 action
= tolower(getch(mouse
));
353 if (action
< 'a' || action
> 'z' /* out of bound or */
354 || markers
[action
-'a'].l
== -1) break; /* unset */
355 cursor_move (markers
[action
-'a'].l
, markers
[action
-'a'].c
);
357 case 'r': /* start a new game */
362 case '\014': /* Ctrl-L -- redraw */
363 show_minefield (NORMAL
);
368 fill_minefield (-1, -1);
371 show_minefield (cheatmode
?NORMAL
:SHOWMINES
);
372 cheatmode
= !cheatmode
;
379 timer_setup(0); /* stop timer */
380 show_minefield (SHOWMINES
);
383 unsigned char mouse
[3];
384 gotaction
= getch(mouse
);
385 /* :D clicked: TODO: won't work in single-width mode! */
386 if (gotaction
==CTRSEQ_MOUSE_LEFT
&& mouse
[2]==LINE_OFFSET
-1 &&
387 (mouse
[1]==f
.w
+COL_OFFSET
|| mouse
[1]==f
.w
+COL_OFFSET
+1)) {
390 } else if (gotaction
== 'r') {
393 } else if (gotaction
== 'q') {
403 move(0,0); //move(f.h+LINE_OFFSET+2, 0);
404 /* disable mouse, show cursor */
405 printf ("\033[?9l\033[?25h");
406 /* reset charset, if necessary */
407 if (op
.scheme
&& op
.scheme
->reset_seq
) print (op
.scheme
->reset_seq
);
408 /* revert to primary screen */
409 if (alt_screen
) printf ("\033[?47l");
411 restore_term_mode(saved_term_mode
);
414 /* I haven't won as long as a cell exists, that
415 - I haven't opened, and
417 int everything_opened () {
418 for (int row
= 0; row
< f
.h
; row
++)
419 for (int col
= 0; col
< f
.w
; col
++)
420 if (f
.c
[row
][col
].o
== CLOSED
&&
421 f
.c
[row
][col
].m
== NO_MINE
) return 0;
425 int wait_mouse_up (int l
, int c
) {
426 unsigned char mouse2
[3];
431 move (1, field2screen_c (f
.w
/2)-1); print (EMOT(OHH
));
433 if (!(l
< 0 || l
>= f
.h
|| c
< 0 || c
>= f
.w
)) {
434 /* show a pushed-in button if cursor is on minefield */
435 move (l
+LINE_OFFSET
, field2screen_c(c
));
436 fputs (op
.scheme
->mouse_highlight
, stdout
);
440 if (getctrlseq (mouse2
) == CTRSEQ_MOUSE
) {
441 /* ignore mouse wheel events: */
442 if (mouse2
[0] & 0x40) continue;
444 else if (mouse2
[0]&3 == 3) level
--; /* release event */
445 else level
++; /* another button pressed */
449 move (1, field2screen_c (f
.w
/2)-1); print (EMOT(SMILE
)); //TODO: does not respect mode (SHOWMINES, GAME_WON, GAME_LOST)
450 if (!(l
< 0 || l
>= f
.h
|| c
< 0 || c
>= f
.w
)) {
451 partial_show_minefield (l
, c
, NORMAL
);
453 c2
= screen2field_c(mouse2
[1]);
454 l2
= screen2field_l(mouse2
[2]);
455 return ((l2
== l
) && (c2
== c
));
458 int choord_square (int line
, int col
) {
459 for (int l
= MAX(line
-1, 0); l
<= MIN(line
+1, f
.h
-1); l
++) {
460 for (int c
= MAX(col
-1, 0); c
<= MIN(col
+1, f
.w
-1); c
++) {
461 if (f
.c
[l
][c
].f
!= FLAG
) {
462 if (uncover_square (l
, c
))
471 int uncover_square (int l
, int c
) {
472 f
.c
[l
][c
].o
= OPENED
;
473 f
.c
[l
][c
].f
= NOFLAG
; /*must not be QUESM, otherwise rendering issues*/
474 partial_show_minefield (l
, c
, NORMAL
);
477 f
.c
[l
][c
].m
= DEATH_MINE
;
481 /* check for chording */
482 if (f
.c
[l
][c
].n
== 0) {
483 for (int choord_l
= -1; choord_l
<= 1; choord_l
++) {
484 for (int choord_c
= -1; choord_c
<= 1; choord_c
++) {
485 int newl
= l
+ choord_l
;
486 int newc
= c
+ choord_c
;
487 if (newl
>= 0 && newl
< f
.h
&&
488 newc
>= 0 && newc
< f
.w
&&
489 f
.c
[newl
][newc
].o
== CLOSED
&&
490 uncover_square (newl
, newc
)) {
500 void flag_square (int l
, int c
) {
501 if (f
.c
[l
][c
].o
!= CLOSED
) return;
502 /* cycles through flag/quesm/noflag (uses op.mode to detect which ones
504 f
.c
[l
][c
].f
= (f
.c
[l
][c
].f
+ 1) % (op
.mode
+ 1);
505 if (f
.c
[l
][c
].f
==FLAG
) f
.f
++;
506 else f
.f
--; //WARN: breaks on `-q'!
507 partial_show_minefield (l
, c
, NORMAL
);
508 move (1, op
.scheme
->cell_width
);
509 printf ("[%03d]", f
.m
- f
.f
);
512 void quesm_square (int l
, int c
) {
513 /* toggle question mark / none. won't turn flags into question marks.
514 unlike flag_square, this function doesn't respect `-q'. */
515 if (f
.c
[l
][c
].o
!= CLOSED
) return;
516 else if (f
.c
[l
][c
].f
== NOFLAG
) f
.c
[l
][c
].f
= QUESM
;
517 else if (f
.c
[l
][c
].f
== QUESM
) f
.c
[l
][c
].f
= NOFLAG
;
518 partial_show_minefield (l
, c
, NORMAL
);
521 int do_uncover (int* is_newgame
) {
524 fill_minefield (f
.p
[0], f
.p
[1]);
528 if (f
.c
[f
.p
[0]][f
.p
[1]].f
== FLAG
) return GAME_INPROGRESS
;
529 if (f
.c
[f
.p
[0]][f
.p
[1]].o
== CLOSED
) {
530 if (uncover_square (f
.p
[0], f
.p
[1])) return GAME_LOST
;
531 } else if (get_neighbours (f
.p
[0], f
.p
[1], 1) == 0) {
532 if (choord_square (f
.p
[0], f
.p
[1])) return GAME_LOST
;
534 if (everything_opened()) return GAME_WON
;
536 return GAME_INPROGRESS
;
539 void fill_minefield (int l
, int c
) {
543 int line
= rand() % f
.h
;
544 int col
= rand() % f
.w
;
546 if (f
.c
[line
][col
].m
) {
547 /* skip if field already has a mine */
549 } else if ((line
== l
) && (col
== c
)) {
550 /* don't put a mine on the already opened (first click) field */
554 f
.c
[line
][col
].m
= STD_MINE
;
558 /* precalculate neighbours */
559 for (int l
=0; l
< f
.h
; l
++)
560 for (int c
=0; c
< f
.w
; c
++)
561 f
.c
[l
][c
].n
= get_neighbours (l
, c
, NORMAL
);
564 void move (int line
, int col
) {
565 printf ("\033[%d;%dH", line
+1, col
+1);
568 /* absolute coordinates! */
569 void cursor_move (int l
, int c
) {
570 partial_show_minefield (f
.p
[0], f
.p
[1], NORMAL
);
572 f
.p
[0] = CLAMP(l
, 0, f
.h
-1);
573 f
.p
[1] = CLAMP(c
, 0, f
.w
-1);
574 move (f
.p
[0]+LINE_OFFSET
, field2screen_c(f
.p
[1]));
575 //fputs (op.scheme->mouse_highlight, stdout);
577 if (!f
.c
[f
.p
[0]][f
.p
[1]].f
) print("\033[7m");//invert unless ! or ?
578 partial_show_minefield (f
.p
[0], f
.p
[1], HIGHLIGHT
);
579 print("\033[0m");//un-invert
582 /* to_next_boundary(): move into the supplied direction until a change in open-
583 state or flag-state is found and move there. falls back to BIG_MOVE. */
584 #define FIND_NEXT(X, L, C, L1, C1, MAX, OP) do {\
585 new_ ## X OP ## = BIG_MOVE;\
586 for (int i = X OP 1; i > 0 && i < f.MAX-1; i OP ## OP)\
587 if ((f.c[L ][C ].o<<2 + f.c[L ][C ].f) \
588 != (f.c[L1][C1].o<<2 + f.c[L1][C1].f)) {\
593 void to_next_boundary (int l
, int c
, char direction
) {
597 case '>': FIND_NEXT(c
, l
, i
, l
, i
+1, w
, +); break;
598 case '<': FIND_NEXT(c
, l
, i
, l
, i
-1, w
, -); break;
599 case '^': FIND_NEXT(l
, i
, c
, i
-1, c
, h
, -); break;
600 case 'v': FIND_NEXT(l
, i
, c
, i
+1, c
, h
, +); break;
603 cursor_move (new_l
, new_c
);
607 char* cell2schema (int l
, int c
, int mode
) {
608 struct minecell cell
= f
.c
[l
][c
];
609 /* move past invert-ctrlsequence when highlighting the cursor: */
610 int offset
= ((mode
==HIGHLIGHT
)*op
.scheme
->flag_offset
);
612 if (mode
== SHOWMINES
) return (
613 cell
.f
== FLAG
&& cell
.m
? op
.scheme
->field_flagged
+offset
:
614 cell
.f
== FLAG
&& !cell
.m
? op
.scheme
->mine_wrongf
+offset
:
615 cell
.m
== STD_MINE
? op
.scheme
->mine_normal
:
616 cell
.m
== DEATH_MINE
? op
.scheme
->mine_death
:
617 cell
.o
== CLOSED
? op
.scheme
->field_closed
:
618 /*.......................*/ op
.scheme
->number
[f
.c
[l
][c
].n
]);
620 cell
.f
== FLAG
? op
.scheme
->field_flagged
+offset
:
621 cell
.f
== QUESM
? op
.scheme
->field_question
+offset
:
622 cell
.o
== CLOSED
? op
.scheme
->field_closed
:
623 cell
.m
== STD_MINE
? op
.scheme
->mine_normal
:
624 cell
.m
== DEATH_MINE
? op
.scheme
->mine_death
:
625 /*.......................*/ op
.scheme
->number
[f
.c
[l
][c
].n
]);
628 void partial_show_minefield (int l
, int c
, int mode
) {
629 move (l
+LINE_OFFSET
, field2screen_c(c
));
631 print (cell2schema(l
, c
, mode
));
634 void show_minefield (int mode
) {
636 static char modechar
[] = {'*', '!', '?'};
643 dtime
= difftime (time(NULL
), f
.t
);
647 print (op
.scheme
->border_top_l
);
648 printm (f
.w
*op
.scheme
->cell_width
,op
.scheme
->border_top_m
);
649 printf ("%s\r\n", op
.scheme
->border_top_r
);
651 print (op
.scheme
->border_status_l
);
652 printf("[%03d]", f
.m
- f
.f
);
653 printm (f
.w
*op
.scheme
->cell_width
/2-6, " ");
654 printf ("%s", mode
==SHOWMINES
?everything_opened()?
655 EMOT(WON
) : EMOT(DEAD
) : EMOT(SMILE
));
656 printm (f
.w
*op
.scheme
->cell_width
/2-6-4, " ");
657 printf ("[%c] [%03d]", modechar
[f
.s
], dtime
); //TODO: modechar is too large for 8x8 field
658 print (op
.scheme
->border_status_r
);
661 print (op
.scheme
->border_spacer_l
);
662 printm (f
.w
*op
.scheme
->cell_width
,op
.scheme
->border_spacer_m
);
663 print (op
.scheme
->border_spacer_r
);
666 for (int l
= 0; l
< f
.h
; l
++) {
667 print (op
.scheme
->border_field_l
);
668 for (int c
= 0; c
< f
.w
; c
++) {
669 print (cell2schema(l
, c
, mode
));
671 print (op
.scheme
->border_field_r
); print ("\r\n");
674 print (op
.scheme
->border_bottom_l
);
675 printm (f
.w
*op
.scheme
->cell_width
,op
.scheme
->border_bottom_m
);
676 print (op
.scheme
->border_bottom_r
);
680 int get_neighbours (int line
, int col
, int reduced_mode
) {
681 /* counts mines surrounding a square
682 modes: 0=normal; 1=reduced */
686 for (int l
= MAX(line
-1, 0); l
<= MIN(line
+1, f
.h
-1); l
++) {
687 for (int c
= MAX(col
-1, 0); c
<= MIN(col
+1, f
.w
-1); c
++) {
688 if (!l
&& !c
) continue;
690 count
+= !!f
.c
[l
][c
].m
;
691 count
-= reduced_mode
* f
.c
[l
][c
].f
==FLAG
;
697 struct minecell
** alloc_array (int lines
, int cols
) {
698 struct minecell
** a
= malloc (lines
* sizeof(struct minecell
*));
699 if (a
== NULL
) return NULL
;
700 for (int l
= 0; l
< lines
; l
++) {
701 a
[l
] = calloc (cols
, sizeof(struct minecell
));
702 if (a
[l
] == NULL
) goto unalloc
;
707 for (int l
= 0; l
< lines
; l
++)
713 if (f
.c
== NULL
) return;
714 for (int l
= 0; l
< f
.h
; l
++) {
720 int screen2field_l (int l
) {
721 return (l
-LINE_OFFSET
) - 1;
723 /* some trickery is required to extract the mouse position from the cell width,
724 depending on wheather we are using full width characters or double line width.
725 WARN: tested only with scheme.cell_width = 1 and scheme.cell_width = 2. */
726 int screen2field_c (int c
) {
727 return (c
-COL_OFFSET
+1 - 2*(op
.scheme
->cell_width
%2))/2 - op
.scheme
->cell_width
/2;
729 int field2screen_l (int l
) {
730 return 0; //TODO: is never used, therefore not implemented
732 int field2screen_c (int c
) {
733 return (op
.scheme
->cell_width
*c
+COL_OFFSET
- (op
.scheme
->cell_width
%2));
742 int getctrlseq (unsigned char* buf
) {
745 int offset
= 0x20; /* never sends control chars as data */
746 while ((c
= getchar()) != EOF
) {
750 case '\033': state
=ESC_SENT
; break;
756 case '[': state
=CSI_SENT
; break;
757 default: return CTRSEQ_INVALID
;
762 case 'M': state
=MOUSE_EVENT
; break;
763 default: return CTRSEQ_INVALID
;
768 buf
[1] = getchar() - offset
;
769 buf
[2] = getchar() - offset
;
772 return CTRSEQ_INVALID
;
778 int getch(unsigned char* buf
) {
779 /* returns a character, EOF, or constant for an escape/control sequence - NOT
780 compatible with the ncurses implementation of same name */
781 int action
= getctrlseq(buf
);
785 l
= screen2field_l (buf
[2]);
786 c
= screen2field_c (buf
[1]);
788 if (buf
[0] > 3) break; /* ignore all but left/middle/right/up */
789 int success
= wait_mouse_up(l
, c
);
791 /* mouse moved while pressed: */
792 if (!success
) return CTRSEQ_INVALID
;
795 case 0: return CTRSEQ_MOUSE_LEFT
;
796 case 1: return CTRSEQ_MOUSE_MIDDLE
;
797 case 2: return CTRSEQ_MOUSE_RIGHT
;
804 void timer_setup (int enable
) {
805 static struct itimerval tbuf
;
806 tbuf
.it_interval
.tv_sec
= 1;
807 tbuf
.it_interval
.tv_usec
= 0;
811 tbuf
.it_value
.tv_sec
= 1;
812 tbuf
.it_value
.tv_usec
= 0;
813 if (setitimer(ITIMER_REAL
, &tbuf
, NULL
) == -1) {
818 tbuf
.it_value
.tv_sec
= 0;
819 tbuf
.it_value
.tv_usec
= 0;
820 if ( setitimer(ITIMER_REAL
, &tbuf
, NULL
) == -1 ) {