]>
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 - 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*/
24 #include <sys/ioctl.h>
36 #define MIN(a,b) (a>b?b:a)
37 #define MAX(a,b) (a>b?a:b)
38 #define CLAMP(a,m,M) (a<m?m:(a>M?M:a))
39 #define printm(n, s) for (int _loop = 0; _loop < n; _loop++) fputs (s, stdout)
40 #define print(str) fputs (str?str:"", stdout)
41 #define EMOT(e) op.scheme->emoticons[EMOT_ ## e]
42 #define BORDER(l, c) op.scheme->border[B_ ## l][B_ ## c]
45 unsigned m
:2; /* mine?1:killmine?2:0 */
46 unsigned o
:1; /* open?1:0 */
47 unsigned f
:2; /* flagged?1:questioned?2:0 */
48 unsigned n
:4; /* 0<= neighbours <=8 */
54 int m
; /* number of mines */
56 int f
; /* flags counter */
57 int t
; /* time of game start */
58 int p
[2]; /* cursor position {line, col} */
59 int s
; /* space mode */
64 struct minescheme
* scheme
;
65 int mode
; /* allow flags? quesm? */
73 void fill_minefield (int, int);
74 void move_ph (int, int);
75 void move_hi (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 (void);
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);
90 void free_field (void);
91 char* get_emoticon(void);
92 int screen2field_l (int);
93 int screen2field_c (int);
94 int field2screen_c (int);
95 int clicked_emoticon (unsigned char*);
97 void signal_handler (int signum
);
98 void timer_setup (int);
111 enum fieldopenstates
{
127 /* for getctrlseq() */
133 CTRSEQ_MOUSE_LEFT
= -4,
134 CTRSEQ_MOUSE_MIDDLE
= -5,
135 CTRSEQ_MOUSE_RIGHT
= -6,
136 CTRSEQ_CURSOR_LEFT
= -7,
137 CTRSEQ_CURSOR_DOWN
= -8,
138 CTRSEQ_CURSOR_UP
= -9,
139 CTRSEQ_CURSOR_RIGHT
= -10,
147 #define CW op.scheme->cell_width
148 void signal_handler (int signum
) {
152 dtime
= difftime (time(NULL
), f
.t
);
153 move_ph (1, f
.w
*CW
-(CW
%2)-3-(dtime
>999));
154 printf ("[%03d]", f
.t
?dtime
:0);
162 /* http://users.csc.calpoly.edu/~phatalsk/357/lectures/code/sigalrm.c */
163 struct termios saved_term_mode
;
164 struct termios
set_raw_term_mode() {
165 struct termios cur_term_mode
, raw_term_mode
;
167 tcgetattr(STDIN_FILENO
, &cur_term_mode
);
168 raw_term_mode
= cur_term_mode
;
169 raw_term_mode
.c_lflag
&= ~(ICANON
| ECHO
);
170 raw_term_mode
.c_cc
[VMIN
] = 1 ;
171 raw_term_mode
.c_cc
[VTIME
] = 0;
172 tcsetattr(STDIN_FILENO
, TCSAFLUSH
, &raw_term_mode
);
174 return cur_term_mode
;
176 void restore_term_mode(struct termios saved_term_mode
) {
177 tcsetattr(STDIN_FILENO
, TCSAFLUSH
, &saved_term_mode
);
181 int main (int argc
, char** argv
) {
182 struct sigaction saction
;
184 saction
.sa_handler
= signal_handler
;
185 sigemptyset(&saction
.sa_mask
);
186 saction
.sa_flags
= 0;
187 if (sigaction(SIGALRM
, &saction
, NULL
) < 0 ) {
192 if (sigaction(SIGINT
, &saction
, NULL
) < 0 ) {
196 /* end screen setup */
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 " WxH[xM] (width 'x' height 'x' mines)\n"
222 " defaults to 30x16x99; mines default to ~20%%\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"
228 " z: center cursor on minefield\n"
231 " space:modeful cursor (either open or flag)\n"
232 " a: toggle mode for space (open/flag)\n"
233 " mX: set a mark for letter X\n"
234 " `X: move to mark X (aliased to ')\n"
235 " r: start a new game\n"
236 " q: quit\n", argv
[0]);
237 _exit(optget
=='h'?0:1);
240 /* end parse options*/
241 if (optind
< argc
) { /* parse Fieldspec */
242 int n
= sscanf (argv
[optind
], "%dx%dx%d", &f
.w
, &f
.h
, &f
.m
);
244 ioctl(STDOUT_FILENO
, TIOCGWINSZ
, &w
);
245 #define CW op.scheme->cell_width
246 #define WW w.ws_col /*window width */
247 #define WH w.ws_row /*window height */
248 #define FW f.w /* field width */
249 #define FH f.h /* field height */
250 #define MM 231 /* mouse maximum*/
251 #define LB 2 /* left border */
252 #define RB 2 /* right border */
253 #define TB 3 /* top border */
254 #define BB 2 /*bottom border */
256 if (LB
+ FW
*CW
+ RB
> WW
) FW
= WW
/CW
- (LB
+RB
);
257 if (TB
+ FH
+ BB
> WH
) FH
= WH
- (TB
+BB
);
258 if (LB
+ FW
*CW
> MM
) FW
= MM
/CW
- LB
;
259 if (TB
+ FH
> MM
) FH
= MM
- TB
;
262 fprintf (stderr
, "FIELDSPEC: WxH[xM]"
263 " (width 'x' height 'x' mines)\n");
266 if (f
.w
< 30) f
.m
= f
.w
*f
.h
*.15625;
267 else f
.m
= f
.w
*f
.h
*.20625;
279 } else { /* use defaults */
284 /* check boundaries */
285 if (f
.m
> (f
.w
-1) * (f
.h
-1)) {
286 f
.m
= (f
.w
-1) * (f
.h
-1);
287 fprintf (stderr
, "too many mines. reduced to %d.\r\n", f
.m
);
292 f
.c
= alloc_array (f
.h
, f
.w
);
303 struct line_col markers
[26];
304 for (int i
=26; i
; markers
[--i
].l
= -1);
306 /* setup the screen: */
307 saved_term_mode
= set_raw_term_mode();
309 /* switch to alternate screen */
310 printf ("\033[?47h");
311 /* reset cursor, clear screen */
312 printf ("\033[H\033[J");
314 /* swich charset, if necessary */
315 if (op
.scheme
->init_seq
!= NULL
) print (op
.scheme
->init_seq
);
317 show_minefield (NORMAL
);
319 /* enable mouse, hide cursor */
320 printf ("\033[?1000h\033[?25l");
324 unsigned char mouse
[3];
326 action
= getch(mouse
);
329 if (f
.s
== MODE_OPEN
||
330 f
.c
[f
.p
[0]][f
.p
[1]].o
== OPENED
) {
331 switch (do_uncover(&is_newgame
)) {
332 case GAME_LOST
: goto lose
;
333 case GAME_WON
: goto win
;
335 } else if (f
.s
== MODE_FLAG
) {
336 flag_square (f
.p
[0], f
.p
[1]);
337 } else if (f
.s
== MODE_QUESM
) {
338 quesm_square (f
.p
[0], f
.p
[1]);
342 f
.s
= (f
.s
+1)%(op
.mode
+1);
343 show_minefield (cheatmode
?SHOWMINES
:NORMAL
);
345 case CTRSEQ_MOUSE_LEFT
:
346 if (clicked_emoticon(mouse
)) {
350 if (screen2field_c (mouse
[1]) < 0 ||
351 screen2field_c (mouse
[1]) >= f
.w
||
352 screen2field_l (mouse
[2]) < 0 ||
353 screen2field_l (mouse
[2]) >= f
.h
) break;
354 f
.p
[0] = screen2field_l (mouse
[2]);
355 f
.p
[1] = screen2field_c (mouse
[1]);
358 switch (do_uncover(&is_newgame
)) {
359 case GAME_LOST
: goto lose
;
360 case GAME_WON
: goto win
;
363 case CTRSEQ_MOUSE_RIGHT
:
364 if (screen2field_c (mouse
[1]) < 0 ||
365 screen2field_c (mouse
[1]) >= f
.w
||
366 screen2field_l (mouse
[2]) < 0 ||
367 screen2field_l (mouse
[2]) >= f
.h
) break;
368 f
.p
[0] = screen2field_l (mouse
[2]);
369 f
.p
[1] = screen2field_c (mouse
[1]);
371 case 'i': flag_square (f
.p
[0], f
.p
[1]); break;
372 case '?':quesm_square (f
.p
[0], f
.p
[1]); break;
373 case CTRSEQ_CURSOR_LEFT
:
374 case 'h': move_hi (f
.p
[0], f
.p
[1]-1 ); break;
375 case CTRSEQ_CURSOR_DOWN
:
376 case 'j': move_hi (f
.p
[0]+1, f
.p
[1] ); break;
377 case CTRSEQ_CURSOR_UP
:
378 case 'k': move_hi (f
.p
[0]-1, f
.p
[1] ); break;
379 case CTRSEQ_CURSOR_RIGHT
:
380 case 'l': move_hi (f
.p
[0], f
.p
[1]+1 ); break;
381 case 'w': to_next_boundary (f
.p
[0], f
.p
[1], '>'); break;
382 case 'b': to_next_boundary (f
.p
[0], f
.p
[1], '<'); break;
383 case 'u': to_next_boundary (f
.p
[0], f
.p
[1], '^'); break;
384 case 'd': to_next_boundary (f
.p
[0], f
.p
[1], 'v'); break;
385 case '0': /* fallthrough */
386 case '^': move_hi (f
.p
[0], 0 ); break;
387 case '$': move_hi (f
.p
[0], f
.w
-1 ); break;
388 case 'g': move_hi (0, f
.p
[1] ); break;
389 case 'G': move_hi (f
.h
-1, f
.p
[1] ); break;
390 case 'z': move_hi (f
.h
/2, f
.w
/2); break;
392 action
= tolower(getch(mouse
));
393 if (action
< 'a' || action
> 'z') break;/*out of bound*/
394 markers
[action
-'a'].l
= f
.p
[0];
395 markers
[action
-'a'].c
= f
.p
[1];
397 case'\'': /* fallthrough */
399 action
= tolower(getch(mouse
));
400 if (action
< 'a' || action
> 'z' /* out of bound or */
401 || markers
[action
-'a'].l
== -1) break; /* unset */
402 move_hi (markers
[action
-'a'].l
, markers
[action
-'a'].c
);
404 case 'r': /* start a new game */
409 case '\014': /* Ctrl-L -- redraw */
410 show_minefield (NORMAL
);
415 fill_minefield (-1, -1);
418 show_minefield (cheatmode
?NORMAL
:SHOWMINES
);
419 cheatmode
= !cheatmode
;
424 win
: f
.o
= GAME_WON
; goto endgame
;
425 lose
: f
.o
= GAME_LOST
; goto endgame
;
427 timer_setup(0); /* stop timer */
428 show_minefield (SHOWMINES
);
431 unsigned char mouse
[3];
432 gotaction
= getch(mouse
);
433 if (gotaction
==CTRSEQ_MOUSE_LEFT
&& clicked_emoticon(mouse
)) {
436 } else if (gotaction
== 'r') {
439 } else if (gotaction
== 'q') {
450 printf ("\033[?9l\033[?25h"); /* disable mouse, show cursor */
451 print (op
.scheme
->reset_seq
); /* reset charset, if necessary */
452 printf ("\033[?47l"); /* revert to primary screen */
454 restore_term_mode(saved_term_mode
);
457 /* I haven't won as long as a cell exists, that
458 - I haven't opened, and
460 int everything_opened (void) {
461 for (int row
= 0; row
< f
.h
; row
++)
462 for (int col
= 0; col
< f
.w
; col
++)
463 if (f
.c
[row
][col
].o
== CLOSED
&&
464 f
.c
[row
][col
].m
== NO_MINE
) return 0;
468 int wait_mouse_up (int l
, int c
) {
469 unsigned char mouse2
[3];
474 move_ph (1, field2screen_c (f
.w
/2)-1); print (EMOT(OHH
));
476 if (!(l
< 0 || l
>= f
.h
|| c
< 0 || c
>= f
.w
)) {
477 /* show a pushed-in button if cursor is on minefield */
478 move_ph (l
+LINE_OFFSET
, field2screen_c(c
));
479 fputs (op
.scheme
->mouse_highlight
, stdout
);
483 if (getctrlseq (mouse2
) == CTRSEQ_MOUSE
) {
484 /* ignore mouse wheel events: */
485 if (mouse2
[0] & 0x40) continue;
487 else if (mouse2
[0]&3 == 3) level
--; /* release event */
488 else level
++; /* another button pressed */
492 move_ph (1, field2screen_c (f
.w
/2)-1); print (get_emoticon());
494 if (!(l
< 0 || l
>= f
.h
|| c
< 0 || c
>= f
.w
)) {
495 partial_show_minefield (l
, c
, NORMAL
);
497 c2
= screen2field_c(mouse2
[1]);
498 l2
= screen2field_l(mouse2
[2]);
499 return ((l2
== l
) && (c2
== c
));
502 int choord_square (int line
, int col
) {
503 for (int l
= MAX(line
-1, 0); l
<= MIN(line
+1, f
.h
-1); l
++) {
504 for (int c
= MAX(col
-1, 0); c
<= MIN(col
+1, f
.w
-1); c
++) {
505 if (f
.c
[l
][c
].f
!= FLAG
) {
506 if (uncover_square (l
, c
))
515 int uncover_square (int l
, int c
) {
516 f
.c
[l
][c
].o
= OPENED
;
517 f
.c
[l
][c
].f
= NOFLAG
; /*must not be QUESM, otherwise rendering issues*/
518 partial_show_minefield (l
, c
, NORMAL
);
521 f
.c
[l
][c
].m
= DEATH_MINE
;
525 /* check for chording */
526 if (f
.c
[l
][c
].n
== 0) {
527 for (int choord_l
= -1; choord_l
<= 1; choord_l
++) {
528 for (int choord_c
= -1; choord_c
<= 1; choord_c
++) {
529 int newl
= l
+ choord_l
;
530 int newc
= c
+ choord_c
;
531 if (newl
>= 0 && newl
< f
.h
&&
532 newc
>= 0 && newc
< f
.w
&&
533 f
.c
[newl
][newc
].o
== CLOSED
&&
534 uncover_square (newl
, newc
)) {
544 void flag_square (int l
, int c
) {
545 static char modechar
[] = {'*', '!', '?'};
547 if (f
.c
[l
][c
].o
!= CLOSED
) return;
548 /* cycle through flag/quesm/noflag: */
549 f
.c
[l
][c
].f
= (f
.c
[l
][c
].f
+ 1) % (op
.mode
+ 1);
550 if (f
.c
[l
][c
].f
==FLAG
) f
.f
++;
551 else if ((op
.mode
==FLAG
&& f
.c
[l
][c
].f
==NOFLAG
) ||
552 (op
.mode
==QUESM
&& f
.c
[l
][c
].f
==QUESM
)) f
.f
--;
553 partial_show_minefield (l
, c
, NORMAL
);
554 move_ph (1, op
.scheme
->cell_width
);
555 printf ("[%03d%c]", f
.m
- f
.f
, modechar
[f
.s
]);
558 void quesm_square (int l
, int c
) {
559 /* toggle question mark / none. won't turn flags into question marks.
560 unlike flag_square, this function doesn't respect `-q'. */
561 if (f
.c
[l
][c
].o
!= CLOSED
) return;
562 else if (f
.c
[l
][c
].f
== NOFLAG
) f
.c
[l
][c
].f
= QUESM
;
563 else if (f
.c
[l
][c
].f
== QUESM
) f
.c
[l
][c
].f
= NOFLAG
;
564 partial_show_minefield (l
, c
, NORMAL
);
567 int do_uncover (int* is_newgame
) {
570 fill_minefield (f
.p
[0], f
.p
[1]);
574 if (f
.c
[f
.p
[0]][f
.p
[1]].f
== FLAG
) return GAME_INPROGRESS
;
575 if (f
.c
[f
.p
[0]][f
.p
[1]].o
== CLOSED
) {
576 if (uncover_square (f
.p
[0], f
.p
[1])) return GAME_LOST
;
577 } else if (get_neighbours (f
.p
[0], f
.p
[1], 1) == 0) {
578 if (choord_square (f
.p
[0], f
.p
[1])) return GAME_LOST
;
580 if (everything_opened()) return GAME_WON
;
582 return GAME_INPROGRESS
;
585 void fill_minefield (int l
, int c
) {
589 int line
= rand() % f
.h
;
590 int col
= rand() % f
.w
;
592 if (f
.c
[line
][col
].m
) {
593 /* skip if field already has a mine */
595 } else if ((line
== l
) && (col
== c
)) {
596 /* don't put a mine on the already opened (first click) field */
600 f
.c
[line
][col
].m
= STD_MINE
;
604 /* precalculate neighbours */
605 for (int l
=0; l
< f
.h
; l
++)
606 for (int c
=0; c
< f
.w
; c
++)
607 f
.c
[l
][c
].n
= get_neighbours (l
, c
, NORMAL
);
610 void move_ph (int line
, int col
) {
611 /* move printhead to zero-indexed position */
612 printf ("\033[%d;%dH", line
+1, col
+1);
615 void move_hi (int l
, int c
) {
616 /* move cursor and highlight to absolute coordinates */
618 partial_show_minefield (f
.p
[0], f
.p
[1], NORMAL
);
620 f
.p
[0] = CLAMP(l
, 0, f
.h
-1);
621 f
.p
[1] = CLAMP(c
, 0, f
.w
-1);
622 move_ph (f
.p
[0]+LINE_OFFSET
, field2screen_c(f
.p
[1]));
624 print("\033[7m"); /* reverse video */
625 partial_show_minefield (f
.p
[0], f
.p
[1], HIGHLIGHT
);
626 print("\033[0m"); /* un-invert */
629 /* to_next_boundary(): move into the supplied direction until a change in open-
630 state or flag-state is found and move there. falls back to BIG_MOVE. */
631 #define FIND_NEXT(X, L, C, L1, C1, MAX, OP) do {\
632 new_ ## X OP ## = BIG_MOVE;\
633 for (int i = X OP 1; i > 0 && i < f.MAX-1; i OP ## OP)\
634 if (((f.c[L ][C ].o<<2) + f.c[L ][C ].f) \
635 != ((f.c[L1][C1].o<<2) + f.c[L1][C1].f)) {\
640 void to_next_boundary (int l
, int c
, char direction
) {
644 case '>': FIND_NEXT(c
, l
, i
, l
, i
+1, w
, +); break;
645 case '<': FIND_NEXT(c
, l
, i
, l
, i
-1, w
, -); break;
646 case '^': FIND_NEXT(l
, i
, c
, i
-1, c
, h
, -); break;
647 case 'v': FIND_NEXT(l
, i
, c
, i
+1, c
, h
, +); break;
650 move_hi (new_l
, new_c
);
654 char* cell2schema (int l
, int c
, int mode
) {
655 struct minecell cell
= f
.c
[l
][c
];
657 if (mode
== SHOWMINES
) return (
658 cell
.f
== FLAG
&& cell
.m
? op
.scheme
->field_flagged
:
659 cell
.f
== FLAG
&& !cell
.m
? op
.scheme
->mine_wrongf
:
660 cell
.m
== STD_MINE
? op
.scheme
->mine_normal
:
661 cell
.m
== DEATH_MINE
? op
.scheme
->mine_death
:
662 cell
.o
== CLOSED
? op
.scheme
->field_closed
:
663 /*.......................*/ op
.scheme
->number
[f
.c
[l
][c
].n
]);
665 cell
.f
== FLAG
? op
.scheme
->field_flagged
:
666 cell
.f
== QUESM
? op
.scheme
->field_question
:
667 cell
.o
== CLOSED
? op
.scheme
->field_closed
:
668 cell
.m
== STD_MINE
? op
.scheme
->mine_normal
:
669 cell
.m
== DEATH_MINE
? op
.scheme
->mine_death
:
670 /*.......................*/ op
.scheme
->number
[f
.c
[l
][c
].n
]);
673 void partial_show_minefield (int l
, int c
, int mode
) {
674 move_ph (l
+LINE_OFFSET
, field2screen_c(c
));
676 print (cell2schema(l
, c
, mode
));
679 char* get_emoticon(void) {
680 return f
.o
==GAME_WON
? EMOT(WON
):
681 f
.o
==GAME_LOST
? EMOT(DEAD
):
685 /* https://zserge.com/blog/c-for-loop-tricks.html */
686 #define print_line(which) \
687 for (int _break = (printf(BORDER(which,LEFT)), 1); _break; \
688 _break = 0, printf("%s\r\n", BORDER(which,RIGHT)))
689 #define print_border(which, width) \
690 print_line(which) printm (width, BORDER(which,MIDDLE))
691 void show_minefield (int mode
) {
692 int dtime
= difftime (time(NULL
), f
.t
)*!!f
.t
;
693 int half_spaces
= f
.w
*op
.scheme
->cell_width
/2;
694 int left_spaces
= MAX(0,half_spaces
-7-(f
.m
-f
.f
>999));
695 int right_spaces
= MAX(0,half_spaces
-6-(dtime
>999));
696 static char modechar
[] = {'*', '!', '?'};
700 print_border(TOP
, f
.w
);
702 printf("[%03d%c]%*s%s%*s[%03d]",
703 /* [ */ f
.m
- f
.f
, modechar
[f
.s
], /* ] */
704 left_spaces
,"", get_emoticon(), right_spaces
,"",
705 /* [ */ dtime
/* ] */);
707 print_border(DIVIDER
, f
.w
);
709 for (int l
= 0; l
< f
.h
; l
++) print_line(FIELD
)
710 printm (f
.w
, cell2schema(l
, _loop
, mode
));
711 print_border(BOTTOM
, f
.w
);
714 int get_neighbours (int line
, int col
, int reduced_mode
) {
715 /* counts mines surrounding a square
716 modes: 0=normal; 1=reduced */
720 for (int l
= MAX(line
-1, 0); l
<= MIN(line
+1, f
.h
-1); l
++) {
721 for (int c
= MAX(col
-1, 0); c
<= MIN(col
+1, f
.w
-1); c
++) {
722 if (!l
&& !c
) continue;
724 count
+= !!f
.c
[l
][c
].m
;
725 count
-= reduced_mode
* f
.c
[l
][c
].f
==FLAG
;
731 struct minecell
** alloc_array (int lines
, int cols
) {
732 struct minecell
** a
= malloc (lines
* sizeof(struct minecell
*));
733 if (a
== NULL
) return NULL
;
734 for (int l
= 0; l
< lines
; l
++) {
735 a
[l
] = calloc (cols
, sizeof(struct minecell
));
736 if (a
[l
] == NULL
) goto unalloc
;
741 for (int l
= 0; l
< lines
; l
++)
746 void free_field (void) {
747 if (f
.c
== NULL
) return;
748 for (int l
= 0; l
< f
.h
; l
++) {
756 #define CW op.scheme->cell_width
757 int screen2field_l (int l
) {
758 return (l
-LINE_OFFSET
) - 1;
760 int screen2field_c (int c
) {
761 /* this depends on the cell width and only works when it is 1 or 2. */
762 return (c
-COL_OFFSET
+1 - 2*(CW
%2))/2 - CW
/2;
764 int field2screen_c (int c
) {
765 return (CW
*c
+COL_OFFSET
- (CW
%2));
767 int clicked_emoticon (unsigned char* mouse
) {
768 return (mouse
[2] == LINE_OFFSET
-1 && (
769 mouse
[1] == f
.w
+COL_OFFSET
||
770 mouse
[1] == f
.w
+COL_OFFSET
+1));
780 int getctrlseq (unsigned char* buf
) {
783 int offset
= 0x20; /* never sends control chars as data */
784 while ((c
= getchar()) != EOF
) {
788 case '\033': state
=ESC_SENT
; break;
794 case '[': state
=CSI_SENT
; break;
795 default: return CTRSEQ_INVALID
;
800 case 'A': return CTRSEQ_CURSOR_UP
;
801 case 'B': return CTRSEQ_CURSOR_DOWN
;
802 case 'C': return CTRSEQ_CURSOR_RIGHT
;
803 case 'D': return CTRSEQ_CURSOR_LEFT
;
804 case 'M': state
=MOUSE_EVENT
; break;
805 default: return CTRSEQ_INVALID
;
810 buf
[1] = getchar() - offset
;
811 buf
[2] = getchar() - offset
;
814 return CTRSEQ_INVALID
;
820 int getch(unsigned char* buf
) {
821 /* returns a character, EOF, or constant for an escape/control sequence - NOT
822 compatible with the ncurses implementation of same name */
823 int action
= getctrlseq(buf
);
827 l
= screen2field_l (buf
[2]);
828 c
= screen2field_c (buf
[1]);
830 if (buf
[0] > 3) break; /* ignore all but left/middle/right/up */
831 int success
= wait_mouse_up(l
, c
);
833 /* mouse moved while pressed: */
834 if (!success
) return CTRSEQ_INVALID
;
837 case 0: return CTRSEQ_MOUSE_LEFT
;
838 case 1: return CTRSEQ_MOUSE_MIDDLE
;
839 case 2: return CTRSEQ_MOUSE_RIGHT
;
846 void timer_setup (int enable
) {
847 static struct itimerval tbuf
;
848 tbuf
.it_interval
.tv_sec
= 1;
849 tbuf
.it_interval
.tv_usec
= 0;
853 tbuf
.it_value
.tv_sec
= 1;
854 tbuf
.it_value
.tv_usec
= 0;
855 if (setitimer(ITIMER_REAL
, &tbuf
, NULL
) == -1) {
860 tbuf
.it_value
.tv_sec
= 0;
861 tbuf
.it_value
.tv_usec
= 0;
862 if ( setitimer(ITIMER_REAL
, &tbuf
, NULL
) == -1 ) {