]>
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? */
75 void fill_minefield (int, int);
77 void cursor_move (int, int);
78 void to_next_boundary (int l
, int c
, char direction
);
79 int getch (unsigned char*);
80 int getctrlseq (unsigned char*);
81 int everything_opened (void);
82 int wait_mouse_up (int, int);
83 void partial_show_minefield (int, int, int);
84 void show_minefield (int);
85 int get_neighbours (int, int, int);
86 int uncover_square (int, int);
87 void flag_square (int, int);
88 void quesm_square (int, int);
89 int choord_square (int, int);
90 int do_uncover (int*);
91 struct minecell
** alloc_array (int, int);
92 void free_field (void);
93 char* get_emoticon(void);
94 int screen2field_l (int);
95 int screen2field_c (int);
96 int field2screen_l (int);
97 int field2screen_c (int);
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
;
185 saved_term_mode
= set_raw_term_mode();
189 saction
.sa_handler
= signal_handler
;
190 sigemptyset(&saction
.sa_mask
);
191 saction
.sa_flags
= 0;
192 if (sigaction(SIGALRM
, &saction
, NULL
) < 0 ) {
197 if (sigaction(SIGINT
, &saction
, NULL
) < 0 ) {
201 /* end screen setup */
203 op
.scheme
= &symbols_mono
;
208 opterr
= 0; /* don't print message on unrecognized option */
209 while ((optget
= getopt (argc
, argv
, "+hnfqcdx")) != -1) {
211 case 'n': op
.mode
= NOFLAG
; break;
212 case 'f': op
.mode
= FLAG
; break; /*default*/
213 case 'q': op
.mode
= QUESM
; break;
214 case 'c': op
.scheme
= &symbols_col1
; break;
215 case 'd': op
.scheme
= &symbols_doublewidth
; break;
218 fprintf (stderr
, "%s [OPTIONS] [FIELDSPEC]\n"
222 " -q(uestion marks)\n"
223 " -c(olored symbols)\n"
224 " -d(ec charset symbols)\n"
226 " WxH[xM] (width 'x' height 'x' mines)\n"
227 " defaults to 30x16x99; mines default to ~20%%\n"
230 " hjkl: move left/down/up/right\n"
231 " bduw: move to next boundary\n"
232 " ^Gg$: move to the left/bottom/top/right\n"
235 " space:modeful cursor (either open or flag)\n"
236 " a: toggle mode for space (open/flag)\n"
237 " mX: set a mark for letter X\n"
238 " `X: move to mark X (aliased to ')\n"
239 " r: start a new game\n"
240 " q: quit\n", argv
[0]);
244 /* end parse options*/
245 if (optind
< argc
) { /* parse Fieldspec */
246 int n
= sscanf (argv
[optind
], "%dx%dx%d", &f
.w
, &f
.h
, &f
.m
);
248 ioctl(STDOUT_FILENO
, TIOCGWINSZ
, &w
);
249 #define CW op.scheme->cell_width
250 #define WW w.ws_col /*window width */
251 #define WH w.ws_row /*window height */
252 #define FW f.w /* field width */
253 #define FH f.h /* field height */
254 #define MM 231 /* mouse maximum*/
255 #define LB 2 /* left border */
256 #define RB 2 /* right border */
257 #define TB 3 /* top border */
258 #define BB 2 /*bottom border */
260 if (LB
+ FW
*CW
+ RB
> WW
) FW
= WW
/CW
- (LB
+RB
);
261 if (TB
+ FH
+ BB
> WH
) FH
= WH
- (TB
+BB
);
262 if (LB
+ FW
*CW
> MM
) FW
= MM
/CW
- LB
;
263 if (TB
+ FH
> MM
) FH
= MM
- TB
;
266 fprintf (stderr
, "FIELDSPEC: WxH[xM]"
267 " (width 'x' height 'x' mines)\n");
270 if (f
.w
< 30) f
.m
= f
.w
*f
.h
*.15625;
271 else f
.m
= f
.w
*f
.h
*.20625;
283 } else { /* use defaults */
288 /* check boundaries */
289 if (f
.m
> (f
.w
-1) * (f
.h
-1)) {
290 f
.m
= (f
.w
-1) * (f
.h
-1);
291 fprintf (stderr
, "too many mines. reduced to %d.\r\n", f
.m
);
296 f
.c
= alloc_array (f
.h
, f
.w
);
307 struct line_col markers
[26];
308 for (int i
=26; i
; markers
[--i
].l
= -1);
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 f
.p
[0] = screen2field_l (mouse
[2]);
349 f
.p
[1] = screen2field_c (mouse
[1]);
350 /* :D clicked: TODO: won't work in single-width mode! */
351 if (mouse
[2] == LINE_OFFSET
-1 &&
352 (mouse
[1] == f
.w
+COL_OFFSET
||
353 mouse
[1] == f
.w
+COL_OFFSET
+1)) {
357 if (f
.p
[1] < 0 || f
.p
[1] >= f
.w
||
358 f
.p
[0] < 0 || f
.p
[0] >= f
.h
) break; /*out of bound*/
361 switch (do_uncover(&is_newgame
)) {
362 case GAME_LOST
: goto lose
;
363 case GAME_WON
: goto win
;
366 case CTRSEQ_MOUSE_RIGHT
:
367 f
.p
[0] = screen2field_l (mouse
[2]);
368 f
.p
[1] = screen2field_c (mouse
[1]);
369 if (f
.p
[1] < 0 || f
.p
[1] >= f
.w
||
370 f
.p
[0] < 0 || f
.p
[0] >= f
.h
) break; /*out of bound*/
372 case 'i': flag_square (f
.p
[0], f
.p
[1]); break;
373 case '?':quesm_square (f
.p
[0], f
.p
[1]); break;
374 case CTRSEQ_CURSOR_LEFT
:
375 case 'h': cursor_move (f
.p
[0], f
.p
[1]-1 ); break;
376 case CTRSEQ_CURSOR_DOWN
:
377 case 'j': cursor_move (f
.p
[0]+1, f
.p
[1] ); break;
378 case CTRSEQ_CURSOR_UP
:
379 case 'k': cursor_move (f
.p
[0]-1, f
.p
[1] ); break;
380 case CTRSEQ_CURSOR_RIGHT
:
381 case 'l': cursor_move (f
.p
[0], f
.p
[1]+1 ); break;
382 case 'w': to_next_boundary (f
.p
[0], f
.p
[1], '>'); break;
383 case 'b': to_next_boundary (f
.p
[0], f
.p
[1], '<'); break;
384 case 'u': to_next_boundary (f
.p
[0], f
.p
[1], '^'); break;
385 case 'd': to_next_boundary (f
.p
[0], f
.p
[1], 'v'); break;
386 case '0': /* fallthrough */
387 case '^': cursor_move (f
.p
[0], 0 ); break;
388 case '$': cursor_move (f
.p
[0], f
.w
-1 ); break;
389 case 'g': cursor_move (0, f
.p
[1] ); break;
390 case 'G': cursor_move (f
.h
-1, f
.p
[1] ); 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 cursor_move (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
;
431 timer_setup(0); /* stop timer */
432 show_minefield (SHOWMINES
);
435 unsigned char mouse
[3];
436 gotaction
= getch(mouse
);
437 /* :D clicked: TODO: won't work in single-width mode! */
438 if (gotaction
==CTRSEQ_MOUSE_LEFT
&& mouse
[2]==LINE_OFFSET
-1 &&
439 (mouse
[1]==f
.w
+COL_OFFSET
|| mouse
[1]==f
.w
+COL_OFFSET
+1)) {
442 } else if (gotaction
== 'r') {
445 } else if (gotaction
== 'q') {
455 //move(0,0); //move(f.h+LINE_OFFSET+2, 0);
456 /* disable mouse, show cursor */
457 printf ("\033[?9l\033[?25h");
458 /* reset charset, if necessary */
459 if (op
.scheme
&& op
.scheme
->reset_seq
) print (op
.scheme
->reset_seq
);
460 /* revert to primary screen */
461 printf ("\033[?47l");
463 restore_term_mode(saved_term_mode
);
466 /* I haven't won as long as a cell exists, that
467 - I haven't opened, and
469 int everything_opened (void) {
470 for (int row
= 0; row
< f
.h
; row
++)
471 for (int col
= 0; col
< f
.w
; col
++)
472 if (f
.c
[row
][col
].o
== CLOSED
&&
473 f
.c
[row
][col
].m
== NO_MINE
) return 0;
477 int wait_mouse_up (int l
, int c
) {
478 unsigned char mouse2
[3];
483 move (1, field2screen_c (f
.w
/2)-1); print (EMOT(OHH
));
485 if (!(l
< 0 || l
>= f
.h
|| c
< 0 || c
>= f
.w
)) {
486 /* show a pushed-in button if cursor is on minefield */
487 move (l
+LINE_OFFSET
, field2screen_c(c
));
488 fputs (op
.scheme
->mouse_highlight
, stdout
);
492 if (getctrlseq (mouse2
) == CTRSEQ_MOUSE
) {
493 /* ignore mouse wheel events: */
494 if (mouse2
[0] & 0x40) continue;
496 else if (mouse2
[0]&3 == 3) level
--; /* release event */
497 else level
++; /* another button pressed */
501 move (1, field2screen_c (f
.w
/2)-1);
502 print (get_emoticon());
504 if (!(l
< 0 || l
>= f
.h
|| c
< 0 || c
>= f
.w
)) {
505 partial_show_minefield (l
, c
, NORMAL
);
507 c2
= screen2field_c(mouse2
[1]);
508 l2
= screen2field_l(mouse2
[2]);
509 return ((l2
== l
) && (c2
== c
));
512 int choord_square (int line
, int col
) {
513 for (int l
= MAX(line
-1, 0); l
<= MIN(line
+1, f
.h
-1); l
++) {
514 for (int c
= MAX(col
-1, 0); c
<= MIN(col
+1, f
.w
-1); c
++) {
515 if (f
.c
[l
][c
].f
!= FLAG
) {
516 if (uncover_square (l
, c
))
525 int uncover_square (int l
, int c
) {
526 f
.c
[l
][c
].o
= OPENED
;
527 f
.c
[l
][c
].f
= NOFLAG
; /*must not be QUESM, otherwise rendering issues*/
528 partial_show_minefield (l
, c
, NORMAL
);
531 f
.c
[l
][c
].m
= DEATH_MINE
;
535 /* check for chording */
536 if (f
.c
[l
][c
].n
== 0) {
537 for (int choord_l
= -1; choord_l
<= 1; choord_l
++) {
538 for (int choord_c
= -1; choord_c
<= 1; choord_c
++) {
539 int newl
= l
+ choord_l
;
540 int newc
= c
+ choord_c
;
541 if (newl
>= 0 && newl
< f
.h
&&
542 newc
>= 0 && newc
< f
.w
&&
543 f
.c
[newl
][newc
].o
== CLOSED
&&
544 uncover_square (newl
, newc
)) {
554 void flag_square (int l
, int c
) {
555 static char modechar
[] = {'*', '!', '?'};
557 if (f
.c
[l
][c
].o
!= CLOSED
) return;
558 /* cycles through flag/quesm/noflag (uses op.mode to detect which ones
560 f
.c
[l
][c
].f
= (f
.c
[l
][c
].f
+ 1) % (op
.mode
+ 1);
561 if (f
.c
[l
][c
].f
==FLAG
) f
.f
++;
562 else f
.f
--; //WARN: breaks on `-q'!
563 partial_show_minefield (l
, c
, NORMAL
);
564 move (1, op
.scheme
->cell_width
);
565 printf ("[%03d%c]", f
.m
- f
.f
, modechar
[f
.s
]);
568 void quesm_square (int l
, int c
) {
569 /* toggle question mark / none. won't turn flags into question marks.
570 unlike flag_square, this function doesn't respect `-q'. */
571 if (f
.c
[l
][c
].o
!= CLOSED
) return;
572 else if (f
.c
[l
][c
].f
== NOFLAG
) f
.c
[l
][c
].f
= QUESM
;
573 else if (f
.c
[l
][c
].f
== QUESM
) f
.c
[l
][c
].f
= NOFLAG
;
574 partial_show_minefield (l
, c
, NORMAL
);
577 int do_uncover (int* is_newgame
) {
580 fill_minefield (f
.p
[0], f
.p
[1]);
584 if (f
.c
[f
.p
[0]][f
.p
[1]].f
== FLAG
) return GAME_INPROGRESS
;
585 if (f
.c
[f
.p
[0]][f
.p
[1]].o
== CLOSED
) {
586 if (uncover_square (f
.p
[0], f
.p
[1])) return GAME_LOST
;
587 } else if (get_neighbours (f
.p
[0], f
.p
[1], 1) == 0) {
588 if (choord_square (f
.p
[0], f
.p
[1])) return GAME_LOST
;
590 if (everything_opened()) return GAME_WON
;
592 return GAME_INPROGRESS
;
595 void fill_minefield (int l
, int c
) {
599 int line
= rand() % f
.h
;
600 int col
= rand() % f
.w
;
602 if (f
.c
[line
][col
].m
) {
603 /* skip if field already has a mine */
605 } else if ((line
== l
) && (col
== c
)) {
606 /* don't put a mine on the already opened (first click) field */
610 f
.c
[line
][col
].m
= STD_MINE
;
614 /* precalculate neighbours */
615 for (int l
=0; l
< f
.h
; l
++)
616 for (int c
=0; c
< f
.w
; c
++)
617 f
.c
[l
][c
].n
= get_neighbours (l
, c
, NORMAL
);
620 void move (int line
, int col
) {
621 printf ("\033[%d;%dH", line
+1, col
+1);
624 /* absolute coordinates! */
625 void cursor_move (int l
, int c
) {
626 partial_show_minefield (f
.p
[0], f
.p
[1], NORMAL
);
628 f
.p
[0] = CLAMP(l
, 0, f
.h
-1);
629 f
.p
[1] = CLAMP(c
, 0, f
.w
-1);
630 move (f
.p
[0]+LINE_OFFSET
, field2screen_c(f
.p
[1]));
631 //fputs (op.scheme->mouse_highlight, stdout);
633 /* invert unless ! or ? (and flag_offset is used in the scheme): */
634 if (!f
.c
[f
.p
[0]][f
.p
[1]].f
|| !op
.scheme
->flag_offset
)
636 partial_show_minefield (f
.p
[0], f
.p
[1], HIGHLIGHT
);
637 print("\033[0m");//un-invert
640 /* to_next_boundary(): move into the supplied direction until a change in open-
641 state or flag-state is found and move there. falls back to BIG_MOVE. */
642 #define FIND_NEXT(X, L, C, L1, C1, MAX, OP) do {\
643 new_ ## X OP ## = BIG_MOVE;\
644 for (int i = X OP 1; i > 0 && i < f.MAX-1; i OP ## OP)\
645 if ((f.c[L ][C ].o<<2 + f.c[L ][C ].f) \
646 != (f.c[L1][C1].o<<2 + f.c[L1][C1].f)) {\
651 void to_next_boundary (int l
, int c
, char direction
) {
655 case '>': FIND_NEXT(c
, l
, i
, l
, i
+1, w
, +); break;
656 case '<': FIND_NEXT(c
, l
, i
, l
, i
-1, w
, -); break;
657 case '^': FIND_NEXT(l
, i
, c
, i
-1, c
, h
, -); break;
658 case 'v': FIND_NEXT(l
, i
, c
, i
+1, c
, h
, +); break;
661 cursor_move (new_l
, new_c
);
665 char* cell2schema (int l
, int c
, int mode
) {
666 struct minecell cell
= f
.c
[l
][c
];
667 /* move past invert-ctrlsequence when highlighting the cursor: */
668 int offset
= ((mode
==HIGHLIGHT
)*op
.scheme
->flag_offset
);
670 if (mode
== SHOWMINES
) return (
671 cell
.f
== FLAG
&& cell
.m
? op
.scheme
->field_flagged
+offset
:
672 cell
.f
== FLAG
&& !cell
.m
? op
.scheme
->mine_wrongf
:
673 cell
.m
== STD_MINE
? op
.scheme
->mine_normal
:
674 cell
.m
== DEATH_MINE
? op
.scheme
->mine_death
:
675 cell
.o
== CLOSED
? op
.scheme
->field_closed
:
676 /*.......................*/ op
.scheme
->number
[f
.c
[l
][c
].n
]);
678 cell
.f
== FLAG
? op
.scheme
->field_flagged
+offset
:
679 cell
.f
== QUESM
? op
.scheme
->field_question
+offset
:
680 cell
.o
== CLOSED
? op
.scheme
->field_closed
:
681 cell
.m
== STD_MINE
? op
.scheme
->mine_normal
:
682 cell
.m
== DEATH_MINE
? op
.scheme
->mine_death
:
683 /*.......................*/ op
.scheme
->number
[f
.c
[l
][c
].n
]);
686 void partial_show_minefield (int l
, int c
, int mode
) {
687 move (l
+LINE_OFFSET
, field2screen_c(c
));
689 print (cell2schema(l
, c
, mode
));
692 char* get_emoticon(void) {
693 return f
.o
==GAME_WON
? EMOT(WON
):
694 f
.o
==GAME_LOST
? EMOT(DEAD
):
698 /* https://zserge.com/blog/c-for-loop-tricks.html */
699 #define print_line(which) \
700 for (int _break = (printf(BORDER(which,LEFT)), 1); _break; \
701 _break = 0, printf("%s\r\n", BORDER(which,RIGHT)))
702 #define print_border(which, width) \
703 print_line(which) printm (width, BORDER(which,MIDDLE))
704 void show_minefield (int mode
) {
706 int half_spaces
= f
.w
*op
.scheme
->cell_width
/2;
707 int left_spaces
= MAX(0,half_spaces
-7-(f
.m
-f
.f
>999)+2);
708 int right_spaces
= MAX(0,half_spaces
-6-(dtime
>999));
709 static char modechar
[] = {'*', '!', '?'};
713 dtime
= (f
.t
== 0)?0: difftime (time(NULL
), f
.t
);
715 print_border(TOP
, f
.w
);
717 printf("[%03d%c]%*s%*s[%03d]",
718 /* [ */ f
.m
- f
.f
, modechar
[f
.s
], /* ] */
719 left_spaces
, get_emoticon(), right_spaces
,"",
720 /* [ */ dtime
/* ] */);
722 print_border(DIVIDER
, f
.w
);
724 for (int l
= 0; l
< f
.h
; l
++) print_line(FIELD
)
725 printm (f
.w
, cell2schema(l
, _loop
, mode
));
726 print_border(BOTTOM
, f
.w
);
729 int get_neighbours (int line
, int col
, int reduced_mode
) {
730 /* counts mines surrounding a square
731 modes: 0=normal; 1=reduced */
735 for (int l
= MAX(line
-1, 0); l
<= MIN(line
+1, f
.h
-1); l
++) {
736 for (int c
= MAX(col
-1, 0); c
<= MIN(col
+1, f
.w
-1); c
++) {
737 if (!l
&& !c
) continue;
739 count
+= !!f
.c
[l
][c
].m
;
740 count
-= reduced_mode
* f
.c
[l
][c
].f
==FLAG
;
746 struct minecell
** alloc_array (int lines
, int cols
) {
747 struct minecell
** a
= malloc (lines
* sizeof(struct minecell
*));
748 if (a
== NULL
) return NULL
;
749 for (int l
= 0; l
< lines
; l
++) {
750 a
[l
] = calloc (cols
, sizeof(struct minecell
));
751 if (a
[l
] == NULL
) goto unalloc
;
756 for (int l
= 0; l
< lines
; l
++)
761 void free_field (void) {
762 if (f
.c
== NULL
) return;
763 for (int l
= 0; l
< f
.h
; l
++) {
771 int screen2field_l (int l
) {
772 return (l
-LINE_OFFSET
) - 1;
774 /* some trickery is required to extract the mouse position from the cell width,
775 depending on wheather we are using full width characters or double line width.
776 WARN: tested only with scheme.cell_width = 1 and scheme.cell_width = 2. */
777 #define CW op.scheme->cell_width
778 int screen2field_c (int c
) {
779 return (c
-COL_OFFSET
+1 - 2*(CW
%2))/2 - CW
/2;
782 int field2screen_l (int l
) {
783 return 0; //TODO: is never used, therefore not implemented
785 int field2screen_c (int c
) {
786 return (op
.scheme
->cell_width
*c
+COL_OFFSET
- (op
.scheme
->cell_width
%2));
795 int getctrlseq (unsigned char* buf
) {
798 int offset
= 0x20; /* never sends control chars as data */
799 while ((c
= getchar()) != EOF
) {
803 case '\033': state
=ESC_SENT
; break;
809 case '[': state
=CSI_SENT
; break;
810 default: return CTRSEQ_INVALID
;
815 case 'A': return CTRSEQ_CURSOR_UP
;
816 case 'B': return CTRSEQ_CURSOR_DOWN
;
817 case 'C': return CTRSEQ_CURSOR_RIGHT
;
818 case 'D': return CTRSEQ_CURSOR_LEFT
;
819 case 'M': state
=MOUSE_EVENT
; break;
820 default: return CTRSEQ_INVALID
;
825 buf
[1] = getchar() - offset
;
826 buf
[2] = getchar() - offset
;
829 return CTRSEQ_INVALID
;
835 int getch(unsigned char* buf
) {
836 /* returns a character, EOF, or constant for an escape/control sequence - NOT
837 compatible with the ncurses implementation of same name */
838 int action
= getctrlseq(buf
);
842 l
= screen2field_l (buf
[2]);
843 c
= screen2field_c (buf
[1]);
845 if (buf
[0] > 3) break; /* ignore all but left/middle/right/up */
846 int success
= wait_mouse_up(l
, c
);
848 /* mouse moved while pressed: */
849 if (!success
) return CTRSEQ_INVALID
;
852 case 0: return CTRSEQ_MOUSE_LEFT
;
853 case 1: return CTRSEQ_MOUSE_MIDDLE
;
854 case 2: return CTRSEQ_MOUSE_RIGHT
;
861 void timer_setup (int enable
) {
862 static struct itimerval tbuf
;
863 tbuf
.it_interval
.tv_sec
= 1;
864 tbuf
.it_interval
.tv_usec
= 0;
868 tbuf
.it_value
.tv_sec
= 1;
869 tbuf
.it_value
.tv_usec
= 0;
870 if (setitimer(ITIMER_REAL
, &tbuf
, NULL
) == -1) {
875 tbuf
.it_value
.tv_sec
= 0;
876 tbuf
.it_value
.tv_usec
= 0;
877 if ( setitimer(ITIMER_REAL
, &tbuf
, NULL
) == -1 ) {