]>
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 sscanf (argv
[optind
], "%dx%dx%d", &(f
.w
), &(f
.h
), &(f
.m
));
243 /* check boundaries */
244 if (f
.m
> (f
.w
-1) * (f
.h
-1)) {
245 f
.m
= (f
.w
-1) * (f
.h
-1);
246 fprintf (stderr
, "too many mines. reduced to %d.\r\n", f
.m
);
251 f
.c
= alloc_array (f
.h
, f
.w
);
261 struct line_col markers
[26];
262 for (int i
=26; i
; markers
[--i
].l
= -1);
264 /* switch to alternate screen */
265 printf ("\033[?47h");
267 /* reset cursor, clear screen */
268 printf ("\033[H\033[J");
270 /* swich charset, if necessary */
271 if (op
.scheme
->init_seq
!= NULL
) print (op
.scheme
->init_seq
);
273 show_minefield (NORMAL
);
275 /* enable mouse, hide cursor */
276 printf ("\033[?1000h\033[?25l");
281 unsigned char mouse
[3];
283 action
= getch(mouse
);
286 if (f
.s
== MODE_OPEN
||
287 f
.c
[f
.p
[0]][f
.p
[1]].o
== OPENED
) {
288 switch (do_uncover(&is_newgame
)) {
289 case GAME_LOST
: goto lose
;
290 case GAME_WON
: goto win
;
292 } else if (f
.s
== MODE_FLAG
) {
293 flag_square (f
.p
[0], f
.p
[1]);
294 } else if (f
.s
== MODE_QUESM
) {
295 quesm_square (f
.p
[0], f
.p
[1]);
299 f
.s
= (f
.s
+1)%(op
.mode
+1);
300 show_minefield (cheatmode
?SHOWMINES
:NORMAL
);
302 case CTRSEQ_MOUSE_LEFT
:
303 f
.p
[0] = screen2field_l (mouse
[2]);
304 f
.p
[1] = screen2field_c (mouse
[1]);
305 /* :D clicked: TODO: won't work in single-width mode! */
306 if (mouse
[2] == LINE_OFFSET
-1 &&
307 (mouse
[1] == f
.w
+COL_OFFSET
||
308 mouse
[1] == f
.w
+COL_OFFSET
+1)) {
312 if (f
.p
[1] < 0 || f
.p
[1] >= f
.w
||
313 f
.p
[0] < 0 || f
.p
[0] >= f
.h
) break; /*out of bound*/
316 switch (do_uncover(&is_newgame
)) {
317 case GAME_LOST
: goto lose
;
318 case GAME_WON
: goto win
;
321 case CTRSEQ_MOUSE_RIGHT
:
322 f
.p
[0] = screen2field_l (mouse
[2]);
323 f
.p
[1] = screen2field_c (mouse
[1]);
324 if (f
.p
[1] < 0 || f
.p
[1] >= f
.w
||
325 f
.p
[0] < 0 || f
.p
[0] >= f
.h
) break; /*out of bound*/
327 case 'i': flag_square (f
.p
[0], f
.p
[1]); break;
328 case '?':quesm_square (f
.p
[0], f
.p
[1]); break;
329 case 'h': cursor_move (f
.p
[0], f
.p
[1]-1 ); break;
330 case 'j': cursor_move (f
.p
[0]+1, f
.p
[1] ); break;
331 case 'k': cursor_move (f
.p
[0]-1, f
.p
[1] ); break;
332 case 'l': cursor_move (f
.p
[0], f
.p
[1]+1 ); break;
333 case 'w': to_next_boundary (f
.p
[0], f
.p
[1], '>'); break;
334 case 'b': to_next_boundary (f
.p
[0], f
.p
[1], '<'); break;
335 case 'u': to_next_boundary (f
.p
[0], f
.p
[1], '^'); break;
336 case 'd': to_next_boundary (f
.p
[0], f
.p
[1], 'v'); break;
337 case '0': /* fallthrough */
338 case '^': cursor_move (f
.p
[0], 0 ); break;
339 case '$': cursor_move (f
.p
[0], f
.w
-1 ); break;
340 case 'g': cursor_move (0, f
.p
[1] ); break;
341 case 'G': cursor_move (f
.h
-1, f
.p
[1] ); break;
343 action
= tolower(getch(mouse
));
344 if (action
< 'a' || action
> 'z') break;/*out of bound*/
345 markers
[action
-'a'].l
= f
.p
[0];
346 markers
[action
-'a'].c
= f
.p
[1];
348 case'\'': /* fallthrough */
350 action
= tolower(getch(mouse
));
351 if (action
< 'a' || action
> 'z' /* out of bound or */
352 || markers
[action
-'a'].l
== -1) break; /* unset */
353 cursor_move (markers
[action
-'a'].l
, markers
[action
-'a'].c
);
355 case 'r': /* start a new game */
360 case '\014': /* Ctrl-L -- redraw */
361 show_minefield (NORMAL
);
366 fill_minefield (-1, -1);
369 show_minefield (cheatmode
?NORMAL
:SHOWMINES
);
370 cheatmode
= !cheatmode
;
377 timer_setup(0); /* stop timer */
378 show_minefield (SHOWMINES
);
381 unsigned char mouse
[3];
382 gotaction
= getch(mouse
);
383 /* :D clicked: TODO: won't work in single-width mode! */
384 if (gotaction
==CTRSEQ_MOUSE_LEFT
&& mouse
[2]==LINE_OFFSET
-1 &&
385 (mouse
[1]==f
.w
+COL_OFFSET
|| mouse
[1]==f
.w
+COL_OFFSET
+1)) {
388 } else if (gotaction
== 'r') {
391 } else if (gotaction
== 'q') {
401 move(0,0); //move(f.h+LINE_OFFSET+2, 0);
402 /* disable mouse, show cursor */
403 printf ("\033[?9l\033[?25h");
404 /* reset charset, if necessary */
405 if (op
.scheme
&& op
.scheme
->reset_seq
) print (op
.scheme
->reset_seq
);
406 /* revert to primary screen */
407 if (alt_screen
) printf ("\033[?47l");
409 restore_term_mode(saved_term_mode
);
412 /* I haven't won as long as a cell exists, that
413 - I haven't opened, and
415 int everything_opened () {
416 for (int row
= 0; row
< f
.h
; row
++)
417 for (int col
= 0; col
< f
.w
; col
++)
418 if (f
.c
[row
][col
].o
== CLOSED
&&
419 f
.c
[row
][col
].m
== NO_MINE
) return 0;
423 int wait_mouse_up (int l
, int c
) {
424 unsigned char mouse2
[3];
429 move (1, field2screen_c (f
.w
/2)-1); print (EMOT(OHH
));
431 if (!(l
< 0 || l
>= f
.h
|| c
< 0 || c
>= f
.w
)) {
432 /* show a pushed-in button if cursor is on minefield */
433 move (l
+LINE_OFFSET
, field2screen_c(c
));
434 fputs (op
.scheme
->mouse_highlight
, stdout
);
438 if (getctrlseq (mouse2
) == CTRSEQ_MOUSE
) {
439 /* ignore mouse wheel events: */
440 if (mouse2
[0] & 0x40) continue;
442 else if (mouse2
[0]&3 == 3) level
--; /* release event */
443 else level
++; /* another button pressed */
447 move (1, field2screen_c (f
.w
/2)-1); print (EMOT(SMILE
));
448 if (!(l
< 0 || l
>= f
.h
|| c
< 0 || c
>= f
.w
)) {
449 partial_show_minefield (l
, c
, NORMAL
);
451 c2
= screen2field_c(mouse2
[1]);
452 l2
= screen2field_l(mouse2
[2]);
453 return ((l2
== l
) && (c2
== c
));
456 int choord_square (int line
, int col
) {
457 for (int l
= MAX(line
-1, 0); l
<= MIN(line
+1, f
.h
-1); l
++) {
458 for (int c
= MAX(col
-1, 0); c
<= MIN(col
+1, f
.w
-1); c
++) {
459 if (f
.c
[l
][c
].f
!= FLAG
) {
460 if (uncover_square (l
, c
))
469 int uncover_square (int l
, int c
) {
470 f
.c
[l
][c
].o
= OPENED
;
471 f
.c
[l
][c
].f
= NOFLAG
; /*must not be QUESM, otherwise rendering issues*/
472 partial_show_minefield (l
, c
, NORMAL
);
475 f
.c
[l
][c
].m
= DEATH_MINE
;
479 /* check for chording */
480 if (f
.c
[l
][c
].n
== 0) {
481 for (int choord_l
= -1; choord_l
<= 1; choord_l
++) {
482 for (int choord_c
= -1; choord_c
<= 1; choord_c
++) {
483 int newl
= l
+ choord_l
;
484 int newc
= c
+ choord_c
;
485 if (newl
>= 0 && newl
< f
.h
&&
486 newc
>= 0 && newc
< f
.w
&&
487 f
.c
[newl
][newc
].o
== CLOSED
&&
488 uncover_square (newl
, newc
)) {
498 void flag_square (int l
, int c
) {
499 if (f
.c
[l
][c
].o
!= CLOSED
) return;
500 /* cycles through flag/quesm/noflag (uses op.mode to detect which ones
502 f
.c
[l
][c
].f
= (f
.c
[l
][c
].f
+ 1) % (op
.mode
+ 1);
503 if (f
.c
[l
][c
].f
==FLAG
) f
.f
++;
504 else f
.f
--; //WARN: breaks on `-q'!
505 partial_show_minefield (l
, c
, NORMAL
);
506 move (1, op
.scheme
->cell_width
);
507 printf ("[%03d]", f
.f
);
510 void quesm_square (int l
, int c
) {
511 /* toggle question mark / none. won't turn flags into question marks.
512 unlike flag_square, this function doesn't respect `-q'. */
513 if (f
.c
[l
][c
].o
!= CLOSED
) return;
514 else if (f
.c
[l
][c
].f
== NOFLAG
) f
.c
[l
][c
].f
= QUESM
;
515 else if (f
.c
[l
][c
].f
== QUESM
) f
.c
[l
][c
].f
= NOFLAG
;
516 partial_show_minefield (l
, c
, NORMAL
);
519 int do_uncover (int* is_newgame
) {
522 fill_minefield (f
.p
[0], f
.p
[1]);
526 if (f
.c
[f
.p
[0]][f
.p
[1]].f
== FLAG
) return GAME_INPROGRESS
;
527 if (f
.c
[f
.p
[0]][f
.p
[1]].o
== CLOSED
) {
528 if (uncover_square (f
.p
[0], f
.p
[1])) return GAME_LOST
;
529 } else if (get_neighbours (f
.p
[0], f
.p
[1], 1) == 0) {
530 if (choord_square (f
.p
[0], f
.p
[1])) return GAME_LOST
;
532 if (everything_opened()) return GAME_WON
;
534 return GAME_INPROGRESS
;
537 void fill_minefield (int l
, int c
) {
541 int line
= rand() % f
.h
;
542 int col
= rand() % f
.w
;
544 if (f
.c
[line
][col
].m
) {
545 /* skip if field already has a mine */
547 } else if ((line
== l
) && (col
== c
)) {
548 /* don't put a mine on the already opened (first click) field */
552 f
.c
[line
][col
].m
= STD_MINE
;
556 /* precalculate neighbours */
557 for (int l
=0; l
< f
.h
; l
++)
558 for (int c
=0; c
< f
.w
; c
++)
559 f
.c
[l
][c
].n
= get_neighbours (l
, c
, NORMAL
);
562 void move (int line
, int col
) {
563 printf ("\033[%d;%dH", line
+1, col
+1);
566 /* absolute coordinates! */
567 void cursor_move (int l
, int c
) {
568 partial_show_minefield (f
.p
[0], f
.p
[1], NORMAL
);
570 f
.p
[0] = CLAMP(l
, 0, f
.h
-1);
571 f
.p
[1] = CLAMP(c
, 0, f
.w
-1);
572 move (f
.p
[0]+LINE_OFFSET
, field2screen_c(f
.p
[1]));
573 //fputs (op.scheme->mouse_highlight, stdout);
575 if (!f
.c
[f
.p
[0]][f
.p
[1]].f
) print("\033[7m");//invert unless ! or ?
576 partial_show_minefield (f
.p
[0], f
.p
[1], HIGHLIGHT
);
577 print("\033[0m");//un-invert
580 /* to_next_boundary(): move into the supplied direction until a change in open-
581 state or flag-state is found and move there. falls back to BIG_MOVE. */
582 #define FIND_NEXT(X, L, C, L1, C1, MAX, OP) do {\
583 new_ ## X OP ## = BIG_MOVE;\
584 for (int i = X OP 1; i > 0 && i < f.MAX-1; i OP ## OP)\
585 if ((f.c[L ][C ].o<<2 + f.c[L ][C ].f) \
586 != (f.c[L1][C1].o<<2 + f.c[L1][C1].f)) {\
591 void to_next_boundary (int l
, int c
, char direction
) {
595 case '>': FIND_NEXT(c
, l
, i
, l
, i
+1, w
, +); break;
596 case '<': FIND_NEXT(c
, l
, i
, l
, i
-1, w
, -); break;
597 case '^': FIND_NEXT(l
, i
, c
, i
-1, c
, h
, -); break;
598 case 'v': FIND_NEXT(l
, i
, c
, i
+1, c
, h
, +); break;
601 cursor_move (new_l
, new_c
);
605 char* cell2schema (int l
, int c
, int mode
) {
606 struct minecell cell
= f
.c
[l
][c
];
607 /* move past invert-ctrlsequence when highlighting the cursor: */
608 int offset
= ((mode
==HIGHLIGHT
)*op
.scheme
->flag_offset
);
610 if (mode
== SHOWMINES
) return (
611 cell
.f
== FLAG
&& cell
.m
? op
.scheme
->field_flagged
+offset
:
612 cell
.f
== FLAG
&& !cell
.m
? op
.scheme
->mine_wrongf
+offset
:
613 cell
.m
== STD_MINE
? op
.scheme
->mine_normal
:
614 cell
.m
== DEATH_MINE
? op
.scheme
->mine_death
:
615 cell
.o
== CLOSED
? op
.scheme
->field_closed
:
616 /*.......................*/ op
.scheme
->number
[f
.c
[l
][c
].n
]);
618 cell
.f
== FLAG
? op
.scheme
->field_flagged
+offset
:
619 cell
.f
== QUESM
? op
.scheme
->field_question
+offset
:
620 cell
.o
== CLOSED
? op
.scheme
->field_closed
:
621 cell
.m
== STD_MINE
? op
.scheme
->mine_normal
:
622 cell
.m
== DEATH_MINE
? op
.scheme
->mine_death
:
623 /*.......................*/ op
.scheme
->number
[f
.c
[l
][c
].n
]);
626 void partial_show_minefield (int l
, int c
, int mode
) {
627 move (l
+LINE_OFFSET
, field2screen_c(c
));
629 print (cell2schema(l
, c
, mode
));
632 void show_minefield (int mode
) {
634 static char modechar
[] = {'*', '!', '?'};
641 dtime
= difftime (time(NULL
), f
.t
);
645 print (op
.scheme
->border_top_l
);
646 printm (f
.w
*op
.scheme
->cell_width
,op
.scheme
->border_top_m
);
647 printf ("%s\r\n", op
.scheme
->border_top_r
);
649 print (op
.scheme
->border_status_l
);
650 printf("[%03d]", f
.f
);
651 printm (f
.w
*op
.scheme
->cell_width
/2-6, " ");
652 printf ("%s", mode
==SHOWMINES
?everything_opened()?
653 EMOT(WON
) : EMOT(DEAD
) : EMOT(SMILE
));
654 printm (f
.w
*op
.scheme
->cell_width
/2-6-4, " ");
655 printf ("[%c] [%03d]", modechar
[f
.s
], dtime
);
656 print (op
.scheme
->border_status_r
);
659 print (op
.scheme
->border_spacer_l
);
660 printm (f
.w
*op
.scheme
->cell_width
,op
.scheme
->border_spacer_m
);
661 print (op
.scheme
->border_spacer_r
);
664 for (int l
= 0; l
< f
.h
; l
++) {
665 print (op
.scheme
->border_field_l
);
666 for (int c
= 0; c
< f
.w
; c
++) {
667 print (cell2schema(l
, c
, mode
));
669 print (op
.scheme
->border_field_r
); print ("\r\n");
672 print (op
.scheme
->border_bottom_l
);
673 printm (f
.w
*op
.scheme
->cell_width
,op
.scheme
->border_bottom_m
);
674 print (op
.scheme
->border_bottom_r
);
678 int get_neighbours (int line
, int col
, int reduced_mode
) {
679 /* counts mines surrounding a square
680 modes: 0=normal; 1=reduced */
684 for (int l
= MAX(line
-1, 0); l
<= MIN(line
+1, f
.h
-1); l
++) {
685 for (int c
= MAX(col
-1, 0); c
<= MIN(col
+1, f
.w
-1); c
++) {
686 if (!l
&& !c
) continue;
688 count
+= !!f
.c
[l
][c
].m
;
689 count
-= reduced_mode
* f
.c
[l
][c
].f
==FLAG
;
695 struct minecell
** alloc_array (int lines
, int cols
) {
696 struct minecell
** a
= malloc (lines
* sizeof(struct minecell
*));
697 if (a
== NULL
) return NULL
;
698 for (int l
= 0; l
< lines
; l
++) {
699 a
[l
] = calloc (cols
, sizeof(struct minecell
));
700 if (a
[l
] == NULL
) goto unalloc
;
705 for (int l
= 0; l
< lines
; l
++)
711 if (f
.c
== NULL
) return;
712 for (int l
= 0; l
< f
.h
; l
++) {
718 int screen2field_l (int l
) {
719 return (l
-LINE_OFFSET
) - 1;
721 /* some trickery is required to extract the mouse position from the cell width,
722 depending on wheather we are using full width characters or double line width.
723 WARN: tested only with scheme.cell_width = 1 and scheme.cell_width = 2. */
724 int screen2field_c (int c
) {
725 return (c
-COL_OFFSET
+1 - 2*(op
.scheme
->cell_width
%2))/2 - op
.scheme
->cell_width
/2;
727 int field2screen_l (int l
) {
728 return 0; //TODO: is never used, therefore not implemented
730 int field2screen_c (int c
) {
731 return (op
.scheme
->cell_width
*c
+COL_OFFSET
- (op
.scheme
->cell_width
%2));
740 int getctrlseq (unsigned char* buf
) {
743 int offset
= 0x20; /* never sends control chars as data */
744 while ((c
= getchar()) != EOF
) {
748 case '\033': state
=ESC_SENT
; break;
754 case '[': state
=CSI_SENT
; break;
755 default: return CTRSEQ_INVALID
;
760 case 'M': state
=MOUSE_EVENT
; break;
761 default: return CTRSEQ_INVALID
;
766 buf
[1] = getchar() - offset
;
767 buf
[2] = getchar() - offset
;
770 return CTRSEQ_INVALID
;
776 int getch(unsigned char* buf
) {
777 /* returns a character, EOF, or constant for an escape/control sequence - NOT
778 compatible with the ncurses implementation of same name */
779 int action
= getctrlseq(buf
);
783 l
= screen2field_l (buf
[2]);
784 c
= screen2field_c (buf
[1]);
786 if (buf
[0] > 3) break; /* ignore all but left/middle/right/up */
787 int success
= wait_mouse_up(l
, c
);
789 /* mouse moved while pressed: */
790 if (!success
) return CTRSEQ_INVALID
;
793 case 0: return CTRSEQ_MOUSE_LEFT
;
794 case 1: return CTRSEQ_MOUSE_MIDDLE
;
795 case 2: return CTRSEQ_MOUSE_RIGHT
;
802 void timer_setup (int enable
) {
803 static struct itimerval tbuf
;
804 tbuf
.it_interval
.tv_sec
= 1;
805 tbuf
.it_interval
.tv_usec
= 0;
809 tbuf
.it_value
.tv_sec
= 1;
810 tbuf
.it_value
.tv_usec
= 0;
811 if (setitimer(ITIMER_REAL
, &tbuf
, NULL
) == -1) {
816 tbuf
.it_value
.tv_sec
= 0;
817 tbuf
.it_value
.tv_usec
= 0;
818 if ( setitimer(ITIMER_REAL
, &tbuf
, NULL
) == -1 ) {