]>
git.gir.st - minesVIiper.git/blob - mines_2017.c
1 //TODO: segfault: click outside playfield, then use cursorkeys
2 /*******************************************************************************
4 By Tobias Girstmair, 2015 - 2018
7 (see ./minesviiper -h for full list of options)
9 MOUSE MODE: - left click to open and choord
10 - right click to flag/unflag
11 VI MODE: - hjkl to move cursor left/down/up/right
12 - bduw to jump left/down/up/right by 5 cells
13 - o to open and choord
16 GNU GPL v3, see LICENSE or https://www.gnu.org/licenses/gpl-3.0.txt
17 *******************************************************************************/
20 #define _POSIX_C_SOURCE 2 /*for getopt, sigaction in c99*/
25 #include <sys/ioctl.h>
37 #define MIN(a,b) (a>b?b:a)
38 #define MAX(a,b) (a>b?a:b)
39 #define CLAMP(a,m,M) (a<m?m:(a>M?M:a))
40 #define printm(n, s) for (int _loop = 0; _loop < n; _loop++) fputs (s, stdout)
41 #define print(str) fputs (str, stdout)
42 #define EMOT(e) op.scheme->emoticons[EMOT_ ## e]
43 #define BORDER(l, c) op.scheme->border[B_ ## l][B_ ## c]
46 unsigned m
:2; /* mine?1:killmine?2:0 */
47 unsigned o
:1; /* open?1:0 */
48 unsigned f
:2; /* flagged?1:questioned?2:0 */
49 unsigned n
:4; /* 0<= neighbours <=8 */
53 //TODO: rename w, h to L, C
56 int m
; /* number of mines */
58 int f
; /* flags counter */
59 int t
; /* time of game start */
60 int p
[2]; /* cursor position {line, col} */
61 int s
; /* space mode */
66 struct minescheme
* scheme
;
67 int mode
; /* allow flags? quesm? */
77 void fill_minefield (int, int);
79 void cursor_move (int, int);
80 void to_next_boundary (int l
, int c
, char direction
);
81 int getch (unsigned char*);
82 int getctrlseq (unsigned char*);
83 int everything_opened (void);
84 int wait_mouse_up (int, int);
85 void partial_show_minefield (int, int, int);
86 void show_minefield (int);
87 int get_neighbours (int, int, int);
88 int uncover_square (int, int);
89 void flag_square (int, int);
90 void quesm_square (int, int);
91 int choord_square (int, int);
92 int do_uncover (int*);
93 struct minecell
** alloc_array (int, int);
94 void free_field (void);
95 char* get_emoticon(int mode
);
96 int screen2field_l (int);
97 int screen2field_c (int);
98 int field2screen_l (int);
99 int field2screen_c (int);
101 void signal_handler (int signum
);
102 void timer_setup (int);
115 enum fieldopenstates
{
131 /* for getctrlseq() */
137 CTRSEQ_MOUSE_LEFT
= -4,
138 CTRSEQ_MOUSE_MIDDLE
= -5,
139 CTRSEQ_MOUSE_RIGHT
= -6,
147 #define CW op.scheme->cell_width
148 void signal_handler (int signum
) {
152 dtime
= difftime (time(NULL
), f
.t
);
153 move (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
;
183 saved_term_mode
= set_raw_term_mode();
187 saction
.sa_handler
= signal_handler
;
188 sigemptyset(&saction
.sa_mask
);
189 saction
.sa_flags
= 0;
190 if (sigaction(SIGALRM
, &saction
, NULL
) < 0 ) {
195 if (sigaction(SIGINT
, &saction
, NULL
) < 0 ) {
199 /* end screen setup */
201 op
.scheme
= &symbols_mono
;
206 opterr
= 0; /* don't print message on unrecognized option */
207 while ((optget
= getopt (argc
, argv
, "+hnfqcdx")) != -1) {
209 case 'n': op
.mode
= NOFLAG
; break;
210 case 'f': op
.mode
= FLAG
; break; /*default*/
211 case 'q': op
.mode
= QUESM
; break;
212 case 'c': op
.scheme
= &symbols_col1
; break;
213 case 'd': op
.scheme
= &symbols_doublewidth
; break;
216 fprintf (stderr
, "%s [OPTIONS] [FIELDSPEC]\n"
220 " -q(uestion marks)\n"
221 " -c(olored symbols)\n"
222 " -d(ec charset symbols)\n"
224 " WxH[xM] (width 'x' height 'x' mines)\n"
225 " defaults to 30x16x99; mines default to ~20%%\n"
228 " hjkl: move left/down/up/right\n"
229 " bduw: move to next boundary\n"
230 " ^Gg$: move to the left/bottom/top/right\n"
233 " space:modeful cursor (either open or flag)\n"
234 " a: toggle mode for space (open/flag)\n"
235 " mX: set a mark for letter X\n"
236 " `X: move to mark X (aliased to ')\n"
237 " r: start a new game\n"
238 " q: quit\n", argv
[0]);
242 /* end parse options*/
243 if (optind
< argc
) { /* parse Fieldspec */
244 int n
= sscanf (argv
[optind
], "%dx%dx%d", &f
.w
, &f
.h
, &f
.m
);
246 ioctl(STDOUT_FILENO
, TIOCGWINSZ
, &w
);
247 #define CW op.scheme->cell_width
248 #define WW w.ws_col /*window width */
249 #define WH w.ws_row /*window height */
250 #define FW f.w /* field width */
251 #define FH f.h /* field height */
252 #define MM 231 /* mouse maximum*/
253 #define LB 2 /* left border */
254 #define RB 2 /* right border */
255 #define TB 3 /* top border */
256 #define BB 2 /*bottom border */
258 if (LB
+ FW
*CW
+ RB
> WW
) FW
= WW
/CW
- (LB
+RB
);
259 if (TB
+ FH
+ BB
> WH
) FH
= WH
- (TB
+BB
);
260 if (LB
+ FW
*CW
> MM
) FW
= MM
/CW
- LB
;
261 if (TB
+ FH
> MM
) FH
= MM
- TB
;
264 fprintf (stderr
, "Provide at least width and height to "
265 "the fieldspec.\r\n");
268 if (f
.w
< 30) f
.m
= f
.w
*f
.h
*.15625;
269 else f
.m
= f
.w
*f
.h
*.20625;
281 } else { /* use defaults */
286 /* check boundaries */
287 if (f
.m
> (f
.w
-1) * (f
.h
-1)) {
288 f
.m
= (f
.w
-1) * (f
.h
-1);
289 fprintf (stderr
, "too many mines. reduced to %d.\r\n", f
.m
);
294 f
.c
= alloc_array (f
.h
, f
.w
);
304 struct line_col markers
[26];
305 for (int i
=26; i
; markers
[--i
].l
= -1);
307 /* switch to alternate screen */
308 printf ("\033[?47h");
310 /* reset cursor, clear screen */
311 printf ("\033[H\033[J");
313 /* swich charset, if necessary */
314 if (op
.scheme
->init_seq
!= NULL
) print (op
.scheme
->init_seq
);
316 show_minefield (NORMAL
);
318 /* enable mouse, hide cursor */
319 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 f
.p
[0] = screen2field_l (mouse
[2]);
347 f
.p
[1] = screen2field_c (mouse
[1]);
348 /* :D clicked: TODO: won't work in single-width mode! */
349 if (mouse
[2] == LINE_OFFSET
-1 &&
350 (mouse
[1] == f
.w
+COL_OFFSET
||
351 mouse
[1] == f
.w
+COL_OFFSET
+1)) {
355 if (f
.p
[1] < 0 || f
.p
[1] >= f
.w
||
356 f
.p
[0] < 0 || f
.p
[0] >= f
.h
) break; /*out of bound*/
359 switch (do_uncover(&is_newgame
)) {
360 case GAME_LOST
: goto lose
;
361 case GAME_WON
: goto win
;
364 case CTRSEQ_MOUSE_RIGHT
:
365 f
.p
[0] = screen2field_l (mouse
[2]);
366 f
.p
[1] = screen2field_c (mouse
[1]);
367 if (f
.p
[1] < 0 || f
.p
[1] >= f
.w
||
368 f
.p
[0] < 0 || f
.p
[0] >= f
.h
) break; /*out of bound*/
370 case 'i': flag_square (f
.p
[0], f
.p
[1]); break;
371 case '?':quesm_square (f
.p
[0], f
.p
[1]); break;
372 case 'h': cursor_move (f
.p
[0], f
.p
[1]-1 ); break;
373 case 'j': cursor_move (f
.p
[0]+1, f
.p
[1] ); break;
374 case 'k': cursor_move (f
.p
[0]-1, f
.p
[1] ); break;
375 case 'l': cursor_move (f
.p
[0], f
.p
[1]+1 ); break;
376 case 'w': to_next_boundary (f
.p
[0], f
.p
[1], '>'); break;
377 case 'b': to_next_boundary (f
.p
[0], f
.p
[1], '<'); break;
378 case 'u': to_next_boundary (f
.p
[0], f
.p
[1], '^'); break;
379 case 'd': to_next_boundary (f
.p
[0], f
.p
[1], 'v'); break;
380 case '0': /* fallthrough */
381 case '^': cursor_move (f
.p
[0], 0 ); break;
382 case '$': cursor_move (f
.p
[0], f
.w
-1 ); break;
383 case 'g': cursor_move (0, f
.p
[1] ); break;
384 case 'G': cursor_move (f
.h
-1, f
.p
[1] ); break;
386 action
= tolower(getch(mouse
));
387 if (action
< 'a' || action
> 'z') break;/*out of bound*/
388 markers
[action
-'a'].l
= f
.p
[0];
389 markers
[action
-'a'].c
= f
.p
[1];
391 case'\'': /* fallthrough */
393 action
= tolower(getch(mouse
));
394 if (action
< 'a' || action
> 'z' /* out of bound or */
395 || markers
[action
-'a'].l
== -1) break; /* unset */
396 cursor_move (markers
[action
-'a'].l
, markers
[action
-'a'].c
);
398 case 'r': /* start a new game */
403 case '\014': /* Ctrl-L -- redraw */
404 show_minefield (NORMAL
);
409 fill_minefield (-1, -1);
412 show_minefield (cheatmode
?NORMAL
:SHOWMINES
);
413 cheatmode
= !cheatmode
;
420 timer_setup(0); /* stop timer */
421 show_minefield (SHOWMINES
);
424 unsigned char mouse
[3];
425 gotaction
= getch(mouse
);
426 /* :D clicked: TODO: won't work in single-width mode! */
427 if (gotaction
==CTRSEQ_MOUSE_LEFT
&& mouse
[2]==LINE_OFFSET
-1 &&
428 (mouse
[1]==f
.w
+COL_OFFSET
|| mouse
[1]==f
.w
+COL_OFFSET
+1)) {
431 } else if (gotaction
== 'r') {
434 } else if (gotaction
== 'q') {
444 move(0,0); //move(f.h+LINE_OFFSET+2, 0);
445 /* disable mouse, show cursor */
446 printf ("\033[?9l\033[?25h");
447 /* reset charset, if necessary */
448 if (op
.scheme
&& op
.scheme
->reset_seq
) print (op
.scheme
->reset_seq
);
449 /* revert to primary screen */
450 if (alt_screen
) printf ("\033[?47l");
452 restore_term_mode(saved_term_mode
);
455 /* I haven't won as long as a cell exists, that
456 - I haven't opened, and
458 int everything_opened (void) {
459 for (int row
= 0; row
< f
.h
; row
++)
460 for (int col
= 0; col
< f
.w
; col
++)
461 if (f
.c
[row
][col
].o
== CLOSED
&&
462 f
.c
[row
][col
].m
== NO_MINE
) return 0;
466 int wait_mouse_up (int l
, int c
) {
467 unsigned char mouse2
[3];
472 move (1, field2screen_c (f
.w
/2)-1); print (EMOT(OHH
));
474 if (!(l
< 0 || l
>= f
.h
|| c
< 0 || c
>= f
.w
)) {
475 /* show a pushed-in button if cursor is on minefield */
476 move (l
+LINE_OFFSET
, field2screen_c(c
));
477 fputs (op
.scheme
->mouse_highlight
, stdout
);
481 if (getctrlseq (mouse2
) == CTRSEQ_MOUSE
) {
482 /* ignore mouse wheel events: */
483 if (mouse2
[0] & 0x40) continue;
485 else if (mouse2
[0]&3 == 3) level
--; /* release event */
486 else level
++; /* another button pressed */
490 move (1, field2screen_c (f
.w
/2)-1);
491 print (EMOT(SMILE
)); //TODO:doesn't respect SHOWMINES/GAME_WON/GAME_LOST
493 if (!(l
< 0 || l
>= f
.h
|| c
< 0 || c
>= f
.w
)) {
494 partial_show_minefield (l
, c
, NORMAL
);
496 c2
= screen2field_c(mouse2
[1]);
497 l2
= screen2field_l(mouse2
[2]);
498 return ((l2
== l
) && (c2
== c
));
501 int choord_square (int line
, int col
) {
502 for (int l
= MAX(line
-1, 0); l
<= MIN(line
+1, f
.h
-1); l
++) {
503 for (int c
= MAX(col
-1, 0); c
<= MIN(col
+1, f
.w
-1); c
++) {
504 if (f
.c
[l
][c
].f
!= FLAG
) {
505 if (uncover_square (l
, c
))
514 int uncover_square (int l
, int c
) {
515 f
.c
[l
][c
].o
= OPENED
;
516 f
.c
[l
][c
].f
= NOFLAG
; /*must not be QUESM, otherwise rendering issues*/
517 partial_show_minefield (l
, c
, NORMAL
);
520 f
.c
[l
][c
].m
= DEATH_MINE
;
524 /* check for chording */
525 if (f
.c
[l
][c
].n
== 0) {
526 for (int choord_l
= -1; choord_l
<= 1; choord_l
++) {
527 for (int choord_c
= -1; choord_c
<= 1; choord_c
++) {
528 int newl
= l
+ choord_l
;
529 int newc
= c
+ choord_c
;
530 if (newl
>= 0 && newl
< f
.h
&&
531 newc
>= 0 && newc
< f
.w
&&
532 f
.c
[newl
][newc
].o
== CLOSED
&&
533 uncover_square (newl
, newc
)) {
543 void flag_square (int l
, int c
) {
544 static char modechar
[] = {'*', '!', '?'};
546 if (f
.c
[l
][c
].o
!= CLOSED
) return;
547 /* cycles through flag/quesm/noflag (uses op.mode to detect which ones
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 f
.f
--; //WARN: breaks on `-q'!
552 partial_show_minefield (l
, c
, NORMAL
);
553 move (1, op
.scheme
->cell_width
);
554 printf ("[%03d%c]", f
.m
- f
.f
, modechar
[f
.s
]);
557 void quesm_square (int l
, int c
) {
558 /* toggle question mark / none. won't turn flags into question marks.
559 unlike flag_square, this function doesn't respect `-q'. */
560 if (f
.c
[l
][c
].o
!= CLOSED
) return;
561 else if (f
.c
[l
][c
].f
== NOFLAG
) f
.c
[l
][c
].f
= QUESM
;
562 else if (f
.c
[l
][c
].f
== QUESM
) f
.c
[l
][c
].f
= NOFLAG
;
563 partial_show_minefield (l
, c
, NORMAL
);
566 int do_uncover (int* is_newgame
) {
569 fill_minefield (f
.p
[0], f
.p
[1]);
573 if (f
.c
[f
.p
[0]][f
.p
[1]].f
== FLAG
) return GAME_INPROGRESS
;
574 if (f
.c
[f
.p
[0]][f
.p
[1]].o
== CLOSED
) {
575 if (uncover_square (f
.p
[0], f
.p
[1])) return GAME_LOST
;
576 } else if (get_neighbours (f
.p
[0], f
.p
[1], 1) == 0) {
577 if (choord_square (f
.p
[0], f
.p
[1])) return GAME_LOST
;
579 if (everything_opened()) return GAME_WON
;
581 return GAME_INPROGRESS
;
584 void fill_minefield (int l
, int c
) {
588 int line
= rand() % f
.h
;
589 int col
= rand() % f
.w
;
591 if (f
.c
[line
][col
].m
) {
592 /* skip if field already has a mine */
594 } else if ((line
== l
) && (col
== c
)) {
595 /* don't put a mine on the already opened (first click) field */
599 f
.c
[line
][col
].m
= STD_MINE
;
603 /* precalculate neighbours */
604 for (int l
=0; l
< f
.h
; l
++)
605 for (int c
=0; c
< f
.w
; c
++)
606 f
.c
[l
][c
].n
= get_neighbours (l
, c
, NORMAL
);
609 void move (int line
, int col
) {
610 printf ("\033[%d;%dH", line
+1, col
+1);
613 /* absolute coordinates! */
614 void cursor_move (int l
, int c
) {
615 partial_show_minefield (f
.p
[0], f
.p
[1], NORMAL
);
617 f
.p
[0] = CLAMP(l
, 0, f
.h
-1);
618 f
.p
[1] = CLAMP(c
, 0, f
.w
-1);
619 move (f
.p
[0]+LINE_OFFSET
, field2screen_c(f
.p
[1]));
620 //fputs (op.scheme->mouse_highlight, stdout);
622 /* invert unless ! or ? (and flag_offset is used in the scheme): */
623 if (!f
.c
[f
.p
[0]][f
.p
[1]].f
|| !op
.scheme
->flag_offset
)
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 cursor_move (new_l
, new_c
);
654 char* cell2schema (int l
, int c
, int mode
) {
655 struct minecell cell
= f
.c
[l
][c
];
656 /* move past invert-ctrlsequence when highlighting the cursor: */
657 int offset
= ((mode
==HIGHLIGHT
)*op
.scheme
->flag_offset
);
659 if (mode
== SHOWMINES
) return (
660 cell
.f
== FLAG
&& cell
.m
? op
.scheme
->field_flagged
+offset
:
661 cell
.f
== FLAG
&& !cell
.m
? op
.scheme
->mine_wrongf
:
662 cell
.m
== STD_MINE
? op
.scheme
->mine_normal
:
663 cell
.m
== DEATH_MINE
? op
.scheme
->mine_death
:
664 cell
.o
== CLOSED
? op
.scheme
->field_closed
:
665 /*.......................*/ op
.scheme
->number
[f
.c
[l
][c
].n
]);
667 cell
.f
== FLAG
? op
.scheme
->field_flagged
+offset
:
668 cell
.f
== QUESM
? op
.scheme
->field_question
+offset
:
669 cell
.o
== CLOSED
? op
.scheme
->field_closed
:
670 cell
.m
== STD_MINE
? op
.scheme
->mine_normal
:
671 cell
.m
== DEATH_MINE
? op
.scheme
->mine_death
:
672 /*.......................*/ op
.scheme
->number
[f
.c
[l
][c
].n
]);
675 void partial_show_minefield (int l
, int c
, int mode
) {
676 move (l
+LINE_OFFSET
, field2screen_c(c
));
678 print (cell2schema(l
, c
, mode
));
681 char* get_emoticon(int mode
) {
682 return mode
==SHOWMINES
689 /* https://zserge.com/blog/c-for-loop-tricks.html */
690 #define print_line(which) \
691 for (int _break = (printf(BORDER(which,LEFT)), 1); _break; \
692 _break = 0, printf("%s\r\n", BORDER(which,RIGHT)))
693 #define print_border(which, width) \
694 print_line(which) printm (width, BORDER(which,MIDDLE))
695 void show_minefield (int mode
) {
697 int half_spaces
= f
.w
*op
.scheme
->cell_width
/2;
698 int left_spaces
= MAX(0,half_spaces
-7-(f
.m
-f
.f
>999)+2);
699 int right_spaces
= MAX(0,half_spaces
-6-(dtime
>999));
700 static char modechar
[] = {'*', '!', '?'};
704 dtime
= (f
.t
== 0)?0: difftime (time(NULL
), f
.t
);
706 print_border(TOP
, f
.w
);
708 printf("[%03d%c]%*s%*s[%03d]",
709 /* [ */ f
.m
- f
.f
, modechar
[f
.s
], /* ] */
710 left_spaces
, get_emoticon(mode
), right_spaces
,"",
711 /* [ */ dtime
/* ] */);
713 print_border(DIVIDER
, f
.w
);
715 for (int l
= 0; l
< f
.h
; l
++) print_line(FIELD
)
716 printm (f
.w
, cell2schema(l
, _loop
, mode
));
717 print_border(BOTTOM
, f
.w
);
720 int get_neighbours (int line
, int col
, int reduced_mode
) {
721 /* counts mines surrounding a square
722 modes: 0=normal; 1=reduced */
726 for (int l
= MAX(line
-1, 0); l
<= MIN(line
+1, f
.h
-1); l
++) {
727 for (int c
= MAX(col
-1, 0); c
<= MIN(col
+1, f
.w
-1); c
++) {
728 if (!l
&& !c
) continue;
730 count
+= !!f
.c
[l
][c
].m
;
731 count
-= reduced_mode
* f
.c
[l
][c
].f
==FLAG
;
737 struct minecell
** alloc_array (int lines
, int cols
) {
738 struct minecell
** a
= malloc (lines
* sizeof(struct minecell
*));
739 if (a
== NULL
) return NULL
;
740 for (int l
= 0; l
< lines
; l
++) {
741 a
[l
] = calloc (cols
, sizeof(struct minecell
));
742 if (a
[l
] == NULL
) goto unalloc
;
747 for (int l
= 0; l
< lines
; l
++)
752 void free_field (void) {
753 if (f
.c
== NULL
) return;
754 for (int l
= 0; l
< f
.h
; l
++) {
762 int screen2field_l (int l
) {
763 return (l
-LINE_OFFSET
) - 1;
765 /* some trickery is required to extract the mouse position from the cell width,
766 depending on wheather we are using full width characters or double line width.
767 WARN: tested only with scheme.cell_width = 1 and scheme.cell_width = 2. */
768 #define CW op.scheme->cell_width
769 int screen2field_c (int c
) {
770 return (c
-COL_OFFSET
+1 - 2*(CW
%2))/2 - CW
/2;
773 int field2screen_l (int l
) {
774 return 0; //TODO: is never used, therefore not implemented
776 int field2screen_c (int c
) {
777 return (op
.scheme
->cell_width
*c
+COL_OFFSET
- (op
.scheme
->cell_width
%2));
786 int getctrlseq (unsigned char* buf
) {
789 int offset
= 0x20; /* never sends control chars as data */
790 while ((c
= getchar()) != EOF
) {
794 case '\033': state
=ESC_SENT
; break;
800 case '[': state
=CSI_SENT
; break;
801 default: return CTRSEQ_INVALID
;
806 case 'M': state
=MOUSE_EVENT
; break;
807 default: return CTRSEQ_INVALID
;
812 buf
[1] = getchar() - offset
;
813 buf
[2] = getchar() - offset
;
816 return CTRSEQ_INVALID
;
822 int getch(unsigned char* buf
) {
823 /* returns a character, EOF, or constant for an escape/control sequence - NOT
824 compatible with the ncurses implementation of same name */
825 int action
= getctrlseq(buf
);
829 l
= screen2field_l (buf
[2]);
830 c
= screen2field_c (buf
[1]);
832 if (buf
[0] > 3) break; /* ignore all but left/middle/right/up */
833 int success
= wait_mouse_up(l
, c
);
835 /* mouse moved while pressed: */
836 if (!success
) return CTRSEQ_INVALID
;
839 case 0: return CTRSEQ_MOUSE_LEFT
;
840 case 1: return CTRSEQ_MOUSE_MIDDLE
;
841 case 2: return CTRSEQ_MOUSE_RIGHT
;
848 void timer_setup (int enable
) {
849 static struct itimerval tbuf
;
850 tbuf
.it_interval
.tv_sec
= 1;
851 tbuf
.it_interval
.tv_usec
= 0;
855 tbuf
.it_value
.tv_sec
= 1;
856 tbuf
.it_value
.tv_usec
= 0;
857 if (setitimer(ITIMER_REAL
, &tbuf
, NULL
) == -1) {
862 tbuf
.it_value
.tv_sec
= 0;
863 tbuf
.it_value
.tv_usec
= 0;
864 if ( setitimer(ITIMER_REAL
, &tbuf
, NULL
) == -1 ) {