]>
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*/
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 */
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 */
65 struct minescheme
* scheme
;
66 int mode
; /* allow flags? quesm? */
74 void fill_minefield (int, int);
76 void cursor_move (int, int);
77 void to_next_boundary (int l
, int c
, char direction
);
78 int getch (unsigned char*);
79 int getctrlseq (unsigned char*);
80 int everything_opened (void);
81 int wait_mouse_up (int, int);
82 void partial_show_minefield (int, int, int);
83 void show_minefield (int);
84 int get_neighbours (int, int, int);
85 int uncover_square (int, int);
86 void flag_square (int, int);
87 void quesm_square (int, int);
88 int choord_square (int, int);
89 int do_uncover (int*);
90 struct minecell
** alloc_array (int, int);
91 void free_field (void);
92 char* get_emoticon(void);
93 int screen2field_l (int);
94 int screen2field_c (int);
95 int field2screen_l (int);
96 int field2screen_c (int);
97 int clicked_emoticon (unsigned char*);
99 void signal_handler (int signum
);
100 void timer_setup (int);
113 enum fieldopenstates
{
129 /* for getctrlseq() */
135 CTRSEQ_MOUSE_LEFT
= -4,
136 CTRSEQ_MOUSE_MIDDLE
= -5,
137 CTRSEQ_MOUSE_RIGHT
= -6,
138 CTRSEQ_CURSOR_LEFT
= -7,
139 CTRSEQ_CURSOR_DOWN
= -8,
140 CTRSEQ_CURSOR_UP
= -9,
141 CTRSEQ_CURSOR_RIGHT
= -10,
149 #define CW op.scheme->cell_width
150 void signal_handler (int signum
) {
154 dtime
= difftime (time(NULL
), f
.t
);
155 move (1, f
.w
*CW
-(CW
%2)-3-(dtime
>999));
156 printf ("[%03d]", f
.t
?dtime
:0);
164 /* http://users.csc.calpoly.edu/~phatalsk/357/lectures/code/sigalrm.c */
165 struct termios saved_term_mode
;
166 struct termios
set_raw_term_mode() {
167 struct termios cur_term_mode
, raw_term_mode
;
169 tcgetattr(STDIN_FILENO
, &cur_term_mode
);
170 raw_term_mode
= cur_term_mode
;
171 raw_term_mode
.c_lflag
&= ~(ICANON
| ECHO
);
172 raw_term_mode
.c_cc
[VMIN
] = 1 ;
173 raw_term_mode
.c_cc
[VTIME
] = 0;
174 tcsetattr(STDIN_FILENO
, TCSAFLUSH
, &raw_term_mode
);
176 return cur_term_mode
;
178 void restore_term_mode(struct termios saved_term_mode
) {
179 tcsetattr(STDIN_FILENO
, TCSAFLUSH
, &saved_term_mode
);
183 int main (int argc
, char** argv
) {
184 struct sigaction saction
;
186 saction
.sa_handler
= signal_handler
;
187 sigemptyset(&saction
.sa_mask
);
188 saction
.sa_flags
= 0;
189 if (sigaction(SIGALRM
, &saction
, NULL
) < 0 ) {
194 if (sigaction(SIGINT
, &saction
, NULL
) < 0 ) {
198 /* end screen setup */
200 op
.scheme
= &symbols_mono
;
205 opterr
= 0; /* don't print message on unrecognized option */
206 while ((optget
= getopt (argc
, argv
, "+hnfqcdx")) != -1) {
208 case 'n': op
.mode
= NOFLAG
; break;
209 case 'f': op
.mode
= FLAG
; break; /*default*/
210 case 'q': op
.mode
= QUESM
; break;
211 case 'c': op
.scheme
= &symbols_col1
; break;
212 case 'd': op
.scheme
= &symbols_doublewidth
; break;
215 fprintf (stderr
, "%s [OPTIONS] [FIELDSPEC]\n"
219 " -q(uestion marks)\n"
220 " -c(olored symbols)\n"
221 " -d(ec charset symbols)\n"
223 " WxH[xM] (width 'x' height 'x' mines)\n"
224 " defaults to 30x16x99; mines default to ~20%%\n"
227 " hjkl: move left/down/up/right\n"
228 " bduw: move to next boundary\n"
229 " ^Gg$: move to the left/bottom/top/right\n"
232 " space:modeful cursor (either open or flag)\n"
233 " a: toggle mode for space (open/flag)\n"
234 " mX: set a mark for letter X\n"
235 " `X: move to mark X (aliased to ')\n"
236 " r: start a new game\n"
237 " q: quit\n", argv
[0]);
238 _exit(optget
=='h'?0:1);
241 /* end parse options*/
242 if (optind
< argc
) { /* parse Fieldspec */
243 int n
= sscanf (argv
[optind
], "%dx%dx%d", &f
.w
, &f
.h
, &f
.m
);
245 ioctl(STDOUT_FILENO
, TIOCGWINSZ
, &w
);
246 #define CW op.scheme->cell_width
247 #define WW w.ws_col /*window width */
248 #define WH w.ws_row /*window height */
249 #define FW f.w /* field width */
250 #define FH f.h /* field height */
251 #define MM 231 /* mouse maximum*/
252 #define LB 2 /* left border */
253 #define RB 2 /* right border */
254 #define TB 3 /* top border */
255 #define BB 2 /*bottom border */
257 if (LB
+ FW
*CW
+ RB
> WW
) FW
= WW
/CW
- (LB
+RB
);
258 if (TB
+ FH
+ BB
> WH
) FH
= WH
- (TB
+BB
);
259 if (LB
+ FW
*CW
> MM
) FW
= MM
/CW
- LB
;
260 if (TB
+ FH
> MM
) FH
= MM
- TB
;
263 fprintf (stderr
, "FIELDSPEC: WxH[xM]"
264 " (width 'x' height 'x' mines)\n");
267 if (f
.w
< 30) f
.m
= f
.w
*f
.h
*.15625;
268 else f
.m
= f
.w
*f
.h
*.20625;
280 } else { /* use defaults */
285 /* check boundaries */
286 if (f
.m
> (f
.w
-1) * (f
.h
-1)) {
287 f
.m
= (f
.w
-1) * (f
.h
-1);
288 fprintf (stderr
, "too many mines. reduced to %d.\r\n", f
.m
);
293 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 /* setup the screen: */
308 saved_term_mode
= set_raw_term_mode();
310 /* switch to alternate screen */
311 printf ("\033[?47h");
312 /* reset cursor, clear screen */
313 printf ("\033[H\033[J");
315 /* swich charset, if necessary */
316 if (op
.scheme
->init_seq
!= NULL
) print (op
.scheme
->init_seq
);
318 show_minefield (NORMAL
);
320 /* enable mouse, hide cursor */
321 printf ("\033[?1000h\033[?25l");
326 unsigned char mouse
[3];
328 action
= getch(mouse
);
331 if (f
.s
== MODE_OPEN
||
332 f
.c
[f
.p
[0]][f
.p
[1]].o
== OPENED
) {
333 switch (do_uncover(&is_newgame
)) {
334 case GAME_LOST
: goto lose
;
335 case GAME_WON
: goto win
;
337 } else if (f
.s
== MODE_FLAG
) {
338 flag_square (f
.p
[0], f
.p
[1]);
339 } else if (f
.s
== MODE_QUESM
) {
340 quesm_square (f
.p
[0], f
.p
[1]);
344 f
.s
= (f
.s
+1)%(op
.mode
+1);
345 show_minefield (cheatmode
?SHOWMINES
:NORMAL
);
347 case CTRSEQ_MOUSE_LEFT
:
348 if (clicked_emoticon(mouse
)) {
352 if (screen2field_c (mouse
[1]) < 0 ||
353 screen2field_c (mouse
[1]) >= f
.w
||
354 screen2field_l (mouse
[2]) < 0 ||
355 screen2field_l (mouse
[2]) >= f
.h
) break;
356 f
.p
[0] = screen2field_l (mouse
[2]);
357 f
.p
[1] = screen2field_c (mouse
[1]);
360 switch (do_uncover(&is_newgame
)) {
361 case GAME_LOST
: goto lose
;
362 case GAME_WON
: goto win
;
365 case CTRSEQ_MOUSE_RIGHT
:
366 if (screen2field_c (mouse
[1]) < 0 ||
367 screen2field_c (mouse
[1]) >= f
.w
||
368 screen2field_l (mouse
[2]) < 0 ||
369 screen2field_l (mouse
[2]) >= f
.h
) break;
370 f
.p
[0] = screen2field_l (mouse
[2]);
371 f
.p
[1] = screen2field_c (mouse
[1]);
373 case 'i': flag_square (f
.p
[0], f
.p
[1]); break;
374 case '?':quesm_square (f
.p
[0], f
.p
[1]); break;
375 case CTRSEQ_CURSOR_LEFT
:
376 case 'h': cursor_move (f
.p
[0], f
.p
[1]-1 ); break;
377 case CTRSEQ_CURSOR_DOWN
:
378 case 'j': cursor_move (f
.p
[0]+1, f
.p
[1] ); break;
379 case CTRSEQ_CURSOR_UP
:
380 case 'k': cursor_move (f
.p
[0]-1, f
.p
[1] ); break;
381 case CTRSEQ_CURSOR_RIGHT
:
382 case 'l': cursor_move (f
.p
[0], f
.p
[1]+1 ); break;
383 case 'w': to_next_boundary (f
.p
[0], f
.p
[1], '>'); break;
384 case 'b': to_next_boundary (f
.p
[0], f
.p
[1], '<'); break;
385 case 'u': to_next_boundary (f
.p
[0], f
.p
[1], '^'); break;
386 case 'd': to_next_boundary (f
.p
[0], f
.p
[1], 'v'); break;
387 case '0': /* fallthrough */
388 case '^': cursor_move (f
.p
[0], 0 ); break;
389 case '$': cursor_move (f
.p
[0], f
.w
-1 ); break;
390 case 'g': cursor_move (0, f
.p
[1] ); break;
391 case 'G': cursor_move (f
.h
-1, f
.p
[1] ); break;
393 action
= tolower(getch(mouse
));
394 if (action
< 'a' || action
> 'z') break;/*out of bound*/
395 markers
[action
-'a'].l
= f
.p
[0];
396 markers
[action
-'a'].c
= f
.p
[1];
398 case'\'': /* fallthrough */
400 action
= tolower(getch(mouse
));
401 if (action
< 'a' || action
> 'z' /* out of bound or */
402 || markers
[action
-'a'].l
== -1) break; /* unset */
403 cursor_move (markers
[action
-'a'].l
, markers
[action
-'a'].c
);
405 case 'r': /* start a new game */
410 case '\014': /* Ctrl-L -- redraw */
411 show_minefield (NORMAL
);
416 fill_minefield (-1, -1);
419 show_minefield (cheatmode
?NORMAL
:SHOWMINES
);
420 cheatmode
= !cheatmode
;
425 win
: f
.o
= GAME_WON
; goto endgame
;
426 lose
: f
.o
= GAME_LOST
; goto endgame
;
428 timer_setup(0); /* stop timer */
429 show_minefield (SHOWMINES
);
432 unsigned char mouse
[3];
433 gotaction
= getch(mouse
);
434 if (gotaction
==CTRSEQ_MOUSE_LEFT
&& clicked_emoticon(mouse
)) {
437 } else if (gotaction
== 'r') {
440 } else if (gotaction
== 'q') {
450 //move(0,0); //move(f.h+LINE_OFFSET+2, 0);
451 printf ("\033[?9l\033[?25h"); /* disable mouse, show cursor */
452 print (op
.scheme
->reset_seq
); /* reset charset, if necessary */
453 printf ("\033[?47l"); /* revert to primary screen */
455 restore_term_mode(saved_term_mode
);
458 /* I haven't won as long as a cell exists, that
459 - I haven't opened, and
461 int everything_opened (void) {
462 for (int row
= 0; row
< f
.h
; row
++)
463 for (int col
= 0; col
< f
.w
; col
++)
464 if (f
.c
[row
][col
].o
== CLOSED
&&
465 f
.c
[row
][col
].m
== NO_MINE
) return 0;
469 int wait_mouse_up (int l
, int c
) {
470 unsigned char mouse2
[3];
475 move (1, field2screen_c (f
.w
/2)-1); print (EMOT(OHH
));
477 if (!(l
< 0 || l
>= f
.h
|| c
< 0 || c
>= f
.w
)) {
478 /* show a pushed-in button if cursor is on minefield */
479 move (l
+LINE_OFFSET
, field2screen_c(c
));
480 fputs (op
.scheme
->mouse_highlight
, stdout
);
484 if (getctrlseq (mouse2
) == CTRSEQ_MOUSE
) {
485 /* ignore mouse wheel events: */
486 if (mouse2
[0] & 0x40) continue;
488 else if (mouse2
[0]&3 == 3) level
--; /* release event */
489 else level
++; /* another button pressed */
493 move (1, field2screen_c (f
.w
/2)-1); print (get_emoticon());
495 if (!(l
< 0 || l
>= f
.h
|| c
< 0 || c
>= f
.w
)) {
496 partial_show_minefield (l
, c
, NORMAL
);
498 c2
= screen2field_c(mouse2
[1]);
499 l2
= screen2field_l(mouse2
[2]);
500 return ((l2
== l
) && (c2
== c
));
503 int choord_square (int line
, int col
) {
504 for (int l
= MAX(line
-1, 0); l
<= MIN(line
+1, f
.h
-1); l
++) {
505 for (int c
= MAX(col
-1, 0); c
<= MIN(col
+1, f
.w
-1); c
++) {
506 if (f
.c
[l
][c
].f
!= FLAG
) {
507 if (uncover_square (l
, c
))
516 int uncover_square (int l
, int c
) {
517 f
.c
[l
][c
].o
= OPENED
;
518 f
.c
[l
][c
].f
= NOFLAG
; /*must not be QUESM, otherwise rendering issues*/
519 partial_show_minefield (l
, c
, NORMAL
);
522 f
.c
[l
][c
].m
= DEATH_MINE
;
526 /* check for chording */
527 if (f
.c
[l
][c
].n
== 0) {
528 for (int choord_l
= -1; choord_l
<= 1; choord_l
++) {
529 for (int choord_c
= -1; choord_c
<= 1; choord_c
++) {
530 int newl
= l
+ choord_l
;
531 int newc
= c
+ choord_c
;
532 if (newl
>= 0 && newl
< f
.h
&&
533 newc
>= 0 && newc
< f
.w
&&
534 f
.c
[newl
][newc
].o
== CLOSED
&&
535 uncover_square (newl
, newc
)) {
545 void flag_square (int l
, int c
) {
546 static char modechar
[] = {'*', '!', '?'};
548 if (f
.c
[l
][c
].o
!= CLOSED
) return;
549 /* cycles through flag/quesm/noflag (uses op.mode to detect which ones
551 f
.c
[l
][c
].f
= (f
.c
[l
][c
].f
+ 1) % (op
.mode
+ 1);
552 if (f
.c
[l
][c
].f
==FLAG
) f
.f
++;
553 else f
.f
--; //WARN: breaks on `-q'!
554 partial_show_minefield (l
, c
, NORMAL
);
555 move (1, op
.scheme
->cell_width
);
556 printf ("[%03d%c]", f
.m
- f
.f
, modechar
[f
.s
]);
559 void quesm_square (int l
, int c
) {
560 /* toggle question mark / none. won't turn flags into question marks.
561 unlike flag_square, this function doesn't respect `-q'. */
562 if (f
.c
[l
][c
].o
!= CLOSED
) return;
563 else if (f
.c
[l
][c
].f
== NOFLAG
) f
.c
[l
][c
].f
= QUESM
;
564 else if (f
.c
[l
][c
].f
== QUESM
) f
.c
[l
][c
].f
= NOFLAG
;
565 partial_show_minefield (l
, c
, NORMAL
);
568 int do_uncover (int* is_newgame
) {
571 fill_minefield (f
.p
[0], f
.p
[1]);
575 if (f
.c
[f
.p
[0]][f
.p
[1]].f
== FLAG
) return GAME_INPROGRESS
;
576 if (f
.c
[f
.p
[0]][f
.p
[1]].o
== CLOSED
) {
577 if (uncover_square (f
.p
[0], f
.p
[1])) return GAME_LOST
;
578 } else if (get_neighbours (f
.p
[0], f
.p
[1], 1) == 0) {
579 if (choord_square (f
.p
[0], f
.p
[1])) return GAME_LOST
;
581 if (everything_opened()) return GAME_WON
;
583 return GAME_INPROGRESS
;
586 void fill_minefield (int l
, int c
) {
590 int line
= rand() % f
.h
;
591 int col
= rand() % f
.w
;
593 if (f
.c
[line
][col
].m
) {
594 /* skip if field already has a mine */
596 } else if ((line
== l
) && (col
== c
)) {
597 /* don't put a mine on the already opened (first click) field */
601 f
.c
[line
][col
].m
= STD_MINE
;
605 /* precalculate neighbours */
606 for (int l
=0; l
< f
.h
; l
++)
607 for (int c
=0; c
< f
.w
; c
++)
608 f
.c
[l
][c
].n
= get_neighbours (l
, c
, NORMAL
);
611 void move (int line
, int col
) {
612 printf ("\033[%d;%dH", line
+1, col
+1);
615 /* absolute coordinates! */
616 void cursor_move (int l
, int c
) {
617 partial_show_minefield (f
.p
[0], f
.p
[1], NORMAL
);
619 f
.p
[0] = CLAMP(l
, 0, f
.h
-1);
620 f
.p
[1] = CLAMP(c
, 0, f
.w
-1);
621 move (f
.p
[0]+LINE_OFFSET
, field2screen_c(f
.p
[1]));
622 //fputs (op.scheme->mouse_highlight, stdout);
624 /* invert unless ! or ? (and flag_offset is used in the scheme): */
625 if (!f
.c
[f
.p
[0]][f
.p
[1]].f
|| !op
.scheme
->flag_offset
)
627 partial_show_minefield (f
.p
[0], f
.p
[1], HIGHLIGHT
);
628 print("\033[0m");//un-invert
631 /* to_next_boundary(): move into the supplied direction until a change in open-
632 state or flag-state is found and move there. falls back to BIG_MOVE. */
633 #define FIND_NEXT(X, L, C, L1, C1, MAX, OP) do {\
634 new_ ## X OP ## = BIG_MOVE;\
635 for (int i = X OP 1; i > 0 && i < f.MAX-1; i OP ## OP)\
636 if ((f.c[L ][C ].o<<2 + f.c[L ][C ].f) \
637 != (f.c[L1][C1].o<<2 + f.c[L1][C1].f)) {\
642 void to_next_boundary (int l
, int c
, char direction
) {
646 case '>': FIND_NEXT(c
, l
, i
, l
, i
+1, w
, +); break;
647 case '<': FIND_NEXT(c
, l
, i
, l
, i
-1, w
, -); break;
648 case '^': FIND_NEXT(l
, i
, c
, i
-1, c
, h
, -); break;
649 case 'v': FIND_NEXT(l
, i
, c
, i
+1, c
, h
, +); break;
652 cursor_move (new_l
, new_c
);
656 char* cell2schema (int l
, int c
, int mode
) {
657 struct minecell cell
= f
.c
[l
][c
];
658 /* move past invert-ctrlsequence when highlighting the cursor: */
659 int offset
= ((mode
==HIGHLIGHT
)*op
.scheme
->flag_offset
);
661 if (mode
== SHOWMINES
) return (
662 cell
.f
== FLAG
&& cell
.m
? op
.scheme
->field_flagged
+offset
:
663 cell
.f
== FLAG
&& !cell
.m
? op
.scheme
->mine_wrongf
:
664 cell
.m
== STD_MINE
? op
.scheme
->mine_normal
:
665 cell
.m
== DEATH_MINE
? op
.scheme
->mine_death
:
666 cell
.o
== CLOSED
? op
.scheme
->field_closed
:
667 /*.......................*/ op
.scheme
->number
[f
.c
[l
][c
].n
]);
669 cell
.f
== FLAG
? op
.scheme
->field_flagged
+offset
:
670 cell
.f
== QUESM
? op
.scheme
->field_question
+offset
:
671 cell
.o
== CLOSED
? op
.scheme
->field_closed
:
672 cell
.m
== STD_MINE
? op
.scheme
->mine_normal
:
673 cell
.m
== DEATH_MINE
? op
.scheme
->mine_death
:
674 /*.......................*/ op
.scheme
->number
[f
.c
[l
][c
].n
]);
677 void partial_show_minefield (int l
, int c
, int mode
) {
678 move (l
+LINE_OFFSET
, field2screen_c(c
));
680 print (cell2schema(l
, c
, mode
));
683 char* get_emoticon(void) {
684 return f
.o
==GAME_WON
? EMOT(WON
):
685 f
.o
==GAME_LOST
? EMOT(DEAD
):
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(), 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;
772 int field2screen_l (int l
) {
773 return 0; //TODO: is never used, therefore not implemented
775 int field2screen_c (int c
) {
776 return (CW
*c
+COL_OFFSET
- (CW
%2));
778 int clicked_emoticon (unsigned char* mouse
) {
779 move(30,0);printf("%d-%d", mouse
[2], mouse
[1]);
780 /* :D clicked: TODO: won't work in single-width mode! */
781 return (mouse
[2] == LINE_OFFSET
-1 && (
782 mouse
[1] == (f
.w
*CW
/2)+COL_OFFSET
||
783 mouse
[1] == (f
.w
*CW
/2)+COL_OFFSET
+1));
793 int getctrlseq (unsigned char* buf
) {
796 int offset
= 0x20; /* never sends control chars as data */
797 while ((c
= getchar()) != EOF
) {
801 case '\033': state
=ESC_SENT
; break;
807 case '[': state
=CSI_SENT
; break;
808 default: return CTRSEQ_INVALID
;
813 case 'A': return CTRSEQ_CURSOR_UP
;
814 case 'B': return CTRSEQ_CURSOR_DOWN
;
815 case 'C': return CTRSEQ_CURSOR_RIGHT
;
816 case 'D': return CTRSEQ_CURSOR_LEFT
;
817 case 'M': state
=MOUSE_EVENT
; break;
818 default: return CTRSEQ_INVALID
;
823 buf
[1] = getchar() - offset
;
824 buf
[2] = getchar() - offset
;
827 return CTRSEQ_INVALID
;
833 int getch(unsigned char* buf
) {
834 /* returns a character, EOF, or constant for an escape/control sequence - NOT
835 compatible with the ncurses implementation of same name */
836 int action
= getctrlseq(buf
);
840 l
= screen2field_l (buf
[2]);
841 c
= screen2field_c (buf
[1]);
843 if (buf
[0] > 3) break; /* ignore all but left/middle/right/up */
844 int success
= wait_mouse_up(l
, c
);
846 /* mouse moved while pressed: */
847 if (!success
) return CTRSEQ_INVALID
;
850 case 0: return CTRSEQ_MOUSE_LEFT
;
851 case 1: return CTRSEQ_MOUSE_MIDDLE
;
852 case 2: return CTRSEQ_MOUSE_RIGHT
;
859 void timer_setup (int enable
) {
860 static struct itimerval tbuf
;
861 tbuf
.it_interval
.tv_sec
= 1;
862 tbuf
.it_interval
.tv_usec
= 0;
866 tbuf
.it_value
.tv_sec
= 1;
867 tbuf
.it_value
.tv_usec
= 0;
868 if (setitimer(ITIMER_REAL
, &tbuf
, NULL
) == -1) {
873 tbuf
.it_value
.tv_sec
= 0;
874 tbuf
.it_value
.tv_usec
= 0;
875 if ( setitimer(ITIMER_REAL
, &tbuf
, NULL
) == -1 ) {