]>
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(num, str) for (int i = 0; i < num; i++) fputs (str, stdout)
41 #define print(str) fputs (str, stdout)
42 #define EMOT(e) op.scheme->emoticons[EMOT_ ## e]
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 */
52 //TODO: rename w, h to L, C
55 int m
; /* number of mines */
57 int f
; /* flags counter */
58 int t
; /* time of game start */
59 int p
[2]; /* cursor position {line, col} */
60 int s
; /* space mode */
64 struct minescheme
* scheme
;
65 int mode
; /* allow flags? quesm? */
69 char* spaces
= /* for formatstring in show_minefield(); mouse-max < this */
80 void fill_minefield (int, int);
82 void cursor_move (int, int);
83 void to_next_boundary (int l
, int c
, char direction
);
84 int getch (unsigned char*);
85 int getctrlseq (unsigned char*);
86 int everything_opened ();
87 int wait_mouse_up (int, int);
88 void partial_show_minefield (int, int, int);
89 void show_minefield (int);
90 int get_neighbours (int, int, int);
91 int uncover_square (int, int);
92 void flag_square (int, int);
93 void quesm_square (int, int);
94 int choord_square (int, int);
95 int do_uncover (int*);
96 struct minecell
** alloc_array (int, int);
98 int screen2field_l (int);
99 int screen2field_c (int);
100 int field2screen_l (int);
101 int field2screen_c (int);
103 void signal_handler (int signum
);
104 void timer_setup (int);
117 enum fieldopenstates
{
133 /* for getctrlseq() */
139 CTRSEQ_MOUSE_LEFT
= -4,
140 CTRSEQ_MOUSE_MIDDLE
= -5,
141 CTRSEQ_MOUSE_RIGHT
= -6,
149 void signal_handler (int signum
) {
153 dtime
= difftime (time(NULL
), f
.t
);
154 move (1, f
.w
*op
.scheme
->cell_width
-(op
.scheme
->cell_width
%2)-3-(dtime
>999));
155 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 will be ~20%% if not given\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 the Fieldspec.\r\n");
267 if (f
.w
< 30) f
.m
= f
.w
*f
.h
*.15625;
268 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 () {
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); print (EMOT(SMILE
)); //TODO: does not respect mode (SHOWMINES, GAME_WON, GAME_LOST)
491 if (!(l
< 0 || l
>= f
.h
|| c
< 0 || c
>= f
.w
)) {
492 partial_show_minefield (l
, c
, NORMAL
);
494 c2
= screen2field_c(mouse2
[1]);
495 l2
= screen2field_l(mouse2
[2]);
496 return ((l2
== l
) && (c2
== c
));
499 int choord_square (int line
, int col
) {
500 for (int l
= MAX(line
-1, 0); l
<= MIN(line
+1, f
.h
-1); l
++) {
501 for (int c
= MAX(col
-1, 0); c
<= MIN(col
+1, f
.w
-1); c
++) {
502 if (f
.c
[l
][c
].f
!= FLAG
) {
503 if (uncover_square (l
, c
))
512 int uncover_square (int l
, int c
) {
513 f
.c
[l
][c
].o
= OPENED
;
514 f
.c
[l
][c
].f
= NOFLAG
; /*must not be QUESM, otherwise rendering issues*/
515 partial_show_minefield (l
, c
, NORMAL
);
518 f
.c
[l
][c
].m
= DEATH_MINE
;
522 /* check for chording */
523 if (f
.c
[l
][c
].n
== 0) {
524 for (int choord_l
= -1; choord_l
<= 1; choord_l
++) {
525 for (int choord_c
= -1; choord_c
<= 1; choord_c
++) {
526 int newl
= l
+ choord_l
;
527 int newc
= c
+ choord_c
;
528 if (newl
>= 0 && newl
< f
.h
&&
529 newc
>= 0 && newc
< f
.w
&&
530 f
.c
[newl
][newc
].o
== CLOSED
&&
531 uncover_square (newl
, newc
)) {
541 void flag_square (int l
, int c
) {
542 static char modechar
[] = {'*', '!', '?'};
544 if (f
.c
[l
][c
].o
!= CLOSED
) return;
545 /* cycles through flag/quesm/noflag (uses op.mode to detect which ones
547 f
.c
[l
][c
].f
= (f
.c
[l
][c
].f
+ 1) % (op
.mode
+ 1);
548 if (f
.c
[l
][c
].f
==FLAG
) f
.f
++;
549 else f
.f
--; //WARN: breaks on `-q'!
550 partial_show_minefield (l
, c
, NORMAL
);
551 move (1, op
.scheme
->cell_width
);
552 printf ("[%03d%c]", f
.m
- f
.f
, modechar
[f
.s
]);
555 void quesm_square (int l
, int c
) {
556 /* toggle question mark / none. won't turn flags into question marks.
557 unlike flag_square, this function doesn't respect `-q'. */
558 if (f
.c
[l
][c
].o
!= CLOSED
) return;
559 else if (f
.c
[l
][c
].f
== NOFLAG
) f
.c
[l
][c
].f
= QUESM
;
560 else if (f
.c
[l
][c
].f
== QUESM
) f
.c
[l
][c
].f
= NOFLAG
;
561 partial_show_minefield (l
, c
, NORMAL
);
564 int do_uncover (int* is_newgame
) {
567 fill_minefield (f
.p
[0], f
.p
[1]);
571 if (f
.c
[f
.p
[0]][f
.p
[1]].f
== FLAG
) return GAME_INPROGRESS
;
572 if (f
.c
[f
.p
[0]][f
.p
[1]].o
== CLOSED
) {
573 if (uncover_square (f
.p
[0], f
.p
[1])) return GAME_LOST
;
574 } else if (get_neighbours (f
.p
[0], f
.p
[1], 1) == 0) {
575 if (choord_square (f
.p
[0], f
.p
[1])) return GAME_LOST
;
577 if (everything_opened()) return GAME_WON
;
579 return GAME_INPROGRESS
;
582 void fill_minefield (int l
, int c
) {
586 int line
= rand() % f
.h
;
587 int col
= rand() % f
.w
;
589 if (f
.c
[line
][col
].m
) {
590 /* skip if field already has a mine */
592 } else if ((line
== l
) && (col
== c
)) {
593 /* don't put a mine on the already opened (first click) field */
597 f
.c
[line
][col
].m
= STD_MINE
;
601 /* precalculate neighbours */
602 for (int l
=0; l
< f
.h
; l
++)
603 for (int c
=0; c
< f
.w
; c
++)
604 f
.c
[l
][c
].n
= get_neighbours (l
, c
, NORMAL
);
607 void move (int line
, int col
) {
608 printf ("\033[%d;%dH", line
+1, col
+1);
611 /* absolute coordinates! */
612 void cursor_move (int l
, int c
) {
613 partial_show_minefield (f
.p
[0], f
.p
[1], NORMAL
);
615 f
.p
[0] = CLAMP(l
, 0, f
.h
-1);
616 f
.p
[1] = CLAMP(c
, 0, f
.w
-1);
617 move (f
.p
[0]+LINE_OFFSET
, field2screen_c(f
.p
[1]));
618 //fputs (op.scheme->mouse_highlight, stdout);
620 /*if (!f.c[f.p[0]][f.p[1]].f)*/ print("\033[7m");//invert unless ! or ?
621 partial_show_minefield (f
.p
[0], f
.p
[1], HIGHLIGHT
);
622 print("\033[0m");//un-invert
625 /* to_next_boundary(): move into the supplied direction until a change in open-
626 state or flag-state is found and move there. falls back to BIG_MOVE. */
627 #define FIND_NEXT(X, L, C, L1, C1, MAX, OP) do {\
628 new_ ## X OP ## = BIG_MOVE;\
629 for (int i = X OP 1; i > 0 && i < f.MAX-1; i OP ## OP)\
630 if ((f.c[L ][C ].o<<2 + f.c[L ][C ].f) \
631 != (f.c[L1][C1].o<<2 + f.c[L1][C1].f)) {\
636 void to_next_boundary (int l
, int c
, char direction
) {
640 case '>': FIND_NEXT(c
, l
, i
, l
, i
+1, w
, +); break;
641 case '<': FIND_NEXT(c
, l
, i
, l
, i
-1, w
, -); break;
642 case '^': FIND_NEXT(l
, i
, c
, i
-1, c
, h
, -); break;
643 case 'v': FIND_NEXT(l
, i
, c
, i
+1, c
, h
, +); break;
646 cursor_move (new_l
, new_c
);
650 char* cell2schema (int l
, int c
, int mode
) {
651 struct minecell cell
= f
.c
[l
][c
];
652 /* move past invert-ctrlsequence when highlighting the cursor: */
653 int offset
= ((mode
==HIGHLIGHT
)*op
.scheme
->flag_offset
);
655 if (mode
== SHOWMINES
) return (
656 cell
.f
== FLAG
&& cell
.m
? op
.scheme
->field_flagged
+offset
:
657 cell
.f
== FLAG
&& !cell
.m
? op
.scheme
->mine_wrongf
+offset
:
658 cell
.m
== STD_MINE
? op
.scheme
->mine_normal
:
659 cell
.m
== DEATH_MINE
? op
.scheme
->mine_death
:
660 cell
.o
== CLOSED
? op
.scheme
->field_closed
:
661 /*.......................*/ op
.scheme
->number
[f
.c
[l
][c
].n
]);
663 cell
.f
== FLAG
? op
.scheme
->field_flagged
+offset
:
664 cell
.f
== QUESM
? op
.scheme
->field_question
+offset
:
665 cell
.o
== CLOSED
? op
.scheme
->field_closed
:
666 cell
.m
== STD_MINE
? op
.scheme
->mine_normal
:
667 cell
.m
== DEATH_MINE
? op
.scheme
->mine_death
:
668 /*.......................*/ op
.scheme
->number
[f
.c
[l
][c
].n
]);
671 void partial_show_minefield (int l
, int c
, int mode
) {
672 move (l
+LINE_OFFSET
, field2screen_c(c
));
674 print (cell2schema(l
, c
, mode
));
677 void show_minefield (int mode
) {
679 int n1
, n2
; /* determine size of variable width fields */
680 int half_spaces
= f
.w
*op
.scheme
->cell_width
/2;
681 static char modechar
[] = {'*', '!', '?'};
688 dtime
= difftime (time(NULL
), f
.t
);
692 print (op
.scheme
->border_top_l
);
693 printm (f
.w
, op
.scheme
->border_top_m
);
694 printf ("%s\r\n", op
.scheme
->border_top_r
);
696 printf("%s[%03d%c]%.*s%s%.*s[%03d]%s\r\n",
697 op
.scheme
->border_status_l
,
700 half_spaces
-7-(f
.m
-f
.f
>999), spaces
,
701 mode
==SHOWMINES
?everything_opened()?EMOT(WON
):EMOT(DEAD
):EMOT(SMILE
),
702 half_spaces
-6-(dtime
>999), spaces
,
704 op
.scheme
->border_status_r
);
706 print (op
.scheme
->border_spacer_l
);
707 printm (f
.w
, op
.scheme
->border_spacer_m
);
708 print (op
.scheme
->border_spacer_r
);
711 for (int l
= 0; l
< f
.h
; l
++) {
712 print (op
.scheme
->border_field_l
);
713 for (int c
= 0; c
< f
.w
; c
++) {
714 print (cell2schema(l
, c
, mode
));
716 print (op
.scheme
->border_field_r
); print ("\r\n");
719 print (op
.scheme
->border_bottom_l
);
720 printm (f
.w
*op
.scheme
->cell_width
,op
.scheme
->border_bottom_m
);
721 print (op
.scheme
->border_bottom_r
);
725 int get_neighbours (int line
, int col
, int reduced_mode
) {
726 /* counts mines surrounding a square
727 modes: 0=normal; 1=reduced */
731 for (int l
= MAX(line
-1, 0); l
<= MIN(line
+1, f
.h
-1); l
++) {
732 for (int c
= MAX(col
-1, 0); c
<= MIN(col
+1, f
.w
-1); c
++) {
733 if (!l
&& !c
) continue;
735 count
+= !!f
.c
[l
][c
].m
;
736 count
-= reduced_mode
* f
.c
[l
][c
].f
==FLAG
;
742 struct minecell
** alloc_array (int lines
, int cols
) {
743 struct minecell
** a
= malloc (lines
* sizeof(struct minecell
*));
744 if (a
== NULL
) return NULL
;
745 for (int l
= 0; l
< lines
; l
++) {
746 a
[l
] = calloc (cols
, sizeof(struct minecell
));
747 if (a
[l
] == NULL
) goto unalloc
;
752 for (int l
= 0; l
< lines
; l
++)
758 if (f
.c
== NULL
) return;
759 for (int l
= 0; l
< f
.h
; l
++) {
767 int screen2field_l (int l
) {
768 return (l
-LINE_OFFSET
) - 1;
770 /* some trickery is required to extract the mouse position from the cell width,
771 depending on wheather we are using full width characters or double line width.
772 WARN: tested only with scheme.cell_width = 1 and scheme.cell_width = 2. */
773 int screen2field_c (int c
) {
774 return (c
-COL_OFFSET
+1 - 2*(op
.scheme
->cell_width
%2))/2 - op
.scheme
->cell_width
/2;
776 int field2screen_l (int l
) {
777 return 0; //TODO: is never used, therefore not implemented
779 int field2screen_c (int c
) {
780 return (op
.scheme
->cell_width
*c
+COL_OFFSET
- (op
.scheme
->cell_width
%2));
789 int getctrlseq (unsigned char* buf
) {
792 int offset
= 0x20; /* never sends control chars as data */
793 while ((c
= getchar()) != EOF
) {
797 case '\033': state
=ESC_SENT
; break;
803 case '[': state
=CSI_SENT
; break;
804 default: return CTRSEQ_INVALID
;
809 case 'M': state
=MOUSE_EVENT
; break;
810 default: return CTRSEQ_INVALID
;
815 buf
[1] = getchar() - offset
;
816 buf
[2] = getchar() - offset
;
819 return CTRSEQ_INVALID
;
825 int getch(unsigned char* buf
) {
826 /* returns a character, EOF, or constant for an escape/control sequence - NOT
827 compatible with the ncurses implementation of same name */
828 int action
= getctrlseq(buf
);
832 l
= screen2field_l (buf
[2]);
833 c
= screen2field_c (buf
[1]);
835 if (buf
[0] > 3) break; /* ignore all but left/middle/right/up */
836 int success
= wait_mouse_up(l
, c
);
838 /* mouse moved while pressed: */
839 if (!success
) return CTRSEQ_INVALID
;
842 case 0: return CTRSEQ_MOUSE_LEFT
;
843 case 1: return CTRSEQ_MOUSE_MIDDLE
;
844 case 2: return CTRSEQ_MOUSE_RIGHT
;
851 void timer_setup (int enable
) {
852 static struct itimerval tbuf
;
853 tbuf
.it_interval
.tv_sec
= 1;
854 tbuf
.it_interval
.tv_usec
= 0;
858 tbuf
.it_value
.tv_sec
= 1;
859 tbuf
.it_value
.tv_usec
= 0;
860 if (setitimer(ITIMER_REAL
, &tbuf
, NULL
) == -1) {
865 tbuf
.it_value
.tv_sec
= 0;
866 tbuf
.it_value
.tv_usec
= 0;
867 if ( setitimer(ITIMER_REAL
, &tbuf
, NULL
) == -1 ) {