]> git.gir.st - minesVIiper.git/blob - mines_2017.c
add indicator for space mode next to the time
[minesVIiper.git] / mines_2017.c
1 /*******************************************************************************
2 minesviiper 0.3.145
3 By Tobias Girstmair, 2015 - 2018
4
5 ./minesviiper 16x16x40
6 (see ./minesviiper -h for full list of options)
7
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
13 - i to flag/unflag
14
15 GNU GPL v3, see LICENSE or https://www.gnu.org/licenses/gpl-3.0.txt
16 *******************************************************************************/
17
18
19 #define _POSIX_C_SOURCE 2 /*for getopt, sigaction in c99*/
20 #include <ctype.h>
21 #include <signal.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <sys/time.h>
25 #include <termios.h>
26 #include <time.h>
27 #include <unistd.h>
28
29 #include "schemes.h"
30
31 #define LINE_OFFSET 3
32 #define COL_OFFSET 2
33 #define BIG_MOVE 5
34
35 #define MIN(a,b) (a>b?b:a)
36 #define MAX(a,b) (a>b?a:b)
37 #define CLAMP(a,m,M) (a<m?m:(a>M?M:a))
38 #define printm(num, str) for (int i = 0; i < num; i++) fputs (str, stdout)
39 #define print(str) fputs (str, stdout)
40
41 struct minecell {
42 unsigned m:2; /* mine?1:killmine?2:0 */
43 unsigned o:1; /* open?1:0 */
44 unsigned f:2; /* flagged?1:questioned?2:0 */
45 unsigned n:4; /* 0<= neighbours <=8 */
46 };
47 struct minefield {
48 struct minecell **c;
49 //TODO: rename w, h to L, C
50 int w; /* width */
51 int h; /* height */
52 int m; /* number of mines */
53
54 int f; /* flags counter */
55 int t; /* time of game start */
56 int p[2]; /* cursor position {line, col} */
57 int s; /* space mode */
58 } f;
59
60 struct opt {
61 struct minescheme* scheme;
62 int mode; /* allow flags? quesm? */
63 } op;
64
65 int alt_screen = 0;
66
67 struct line_col {
68 int l;
69 int c;
70 };
71
72 void fill_minefield (int, int);
73 void move (int, int);
74 void cursor_move (int, int);
75 int getch (unsigned char*);
76 int getctrlseq (unsigned char*);
77 int everything_opened ();
78 int wait_mouse_up (int, int);
79 void partial_show_minefield (int, int, int);
80 void show_minefield (int);
81 int get_neighbours (int, int, int);
82 int uncover_square (int, int);
83 void flag_square (int, int);
84 void quesm_square (int, int);
85 int choord_square (int, int);
86 int do_uncover (int*);
87 struct minecell** alloc_array (int, int);
88 void free_field ();
89 int screen2field_l (int);
90 int screen2field_c (int);
91 int field2screen_l (int);
92 int field2screen_c (int);
93 void quit();
94 void signal_handler (int signum);
95 void timer_setup (int);
96
97 enum modes {
98 NORMAL,
99 REDUCED,
100 SHOWMINES,
101 HIGHLIGHT,
102 };
103 enum flagtypes {
104 NOFLAG,
105 FLAG,
106 QUESM,
107 };
108 enum fieldopenstates {
109 CLOSED,
110 OPENED,
111 };
112 enum game_states {
113 GAME_INPROGRESS,
114 GAME_NEW,
115 GAME_WON,
116 GAME_LOST,
117 };
118 enum space_modes {
119 MODE_OPEN,
120 MODE_FLAG,
121 MODE_QUESM,
122 };
123 enum event {
124 /* for getctrlseq() */
125 CTRSEQ_NULL = 0,
126 CTRSEQ_EOF = -1,
127 CTRSEQ_INVALID = -2,
128 CTRSEQ_MOUSE = -3,
129 /* for getch() */
130 CTRSEQ_MOUSE_LEFT = -4,
131 CTRSEQ_MOUSE_MIDDLE = -5,
132 CTRSEQ_MOUSE_RIGHT = -6,
133 };
134 enum mine_types {
135 NO_MINE,
136 STD_MINE,
137 DEATH_MINE,
138 };
139
140 void signal_handler (int signum) {
141 switch (signum) {
142 case SIGALRM:
143 move (1, f.w*op.scheme->cell_width-(op.scheme->cell_width%2)-3);
144 printf ("[%03d]", f.t?(int)difftime (time(NULL), f.t):0);
145 break;
146 case SIGINT:
147 exit(128+SIGINT);
148 }
149 }
150
151 /* http://users.csc.calpoly.edu/~phatalsk/357/lectures/code/sigalrm.c */
152 struct termios saved_term_mode;
153 struct termios set_raw_term_mode() {
154 struct termios cur_term_mode, raw_term_mode;
155
156 tcgetattr(STDIN_FILENO, &cur_term_mode);
157 raw_term_mode = cur_term_mode;
158 raw_term_mode.c_lflag &= ~(ICANON | ECHO);
159 raw_term_mode.c_cc[VMIN] = 1 ;
160 raw_term_mode.c_cc[VTIME] = 0;
161 tcsetattr(STDIN_FILENO, TCSAFLUSH, &raw_term_mode);
162
163 return cur_term_mode;
164 }
165 void restore_term_mode(struct termios saved_term_mode) {
166 tcsetattr(STDIN_FILENO, TCSAFLUSH, &saved_term_mode);
167 }
168
169
170 int main (int argc, char** argv) {
171 struct sigaction saction;
172 saved_term_mode = set_raw_term_mode();
173
174 atexit (*quit);
175
176 saction.sa_handler = signal_handler;
177 sigemptyset(&saction.sa_mask);
178 saction.sa_flags = 0;
179 if (sigaction(SIGALRM, &saction, NULL) < 0 ) {
180 perror("SIGALRM");
181 exit(1);
182 }
183
184 if (sigaction(SIGINT, &saction, NULL) < 0 ) {
185 perror ("SIGINT");
186 exit (1);
187 }
188 /* end screen setup */
189
190 /* setup defaults */
191 f.w = 30;
192 f.h = 16;
193 f.m = 99;
194 f.c = NULL; /*to not free() array before it is allocated*/
195
196 op.scheme = &symbols_mono;
197 op.mode = FLAG;
198 /* end defaults */
199 /* parse options */
200 int optget;
201 opterr = 0; /* don't print message on unrecognized option */
202 while ((optget = getopt (argc, argv, "+hnfqcdx")) != -1) {
203 switch (optget) {
204 case 'n': op.mode = NOFLAG; break;
205 case 'f': op.mode = FLAG; break; /*default*/
206 case 'q': op.mode = QUESM; break;
207 case 'c': op.scheme = &symbols_col1; break;
208 case 'd': op.scheme = &symbols_doublewidth; break;
209 case 'h':
210 default:
211 fprintf (stderr, "%s [OPTIONS] [FIELDSPEC]\n"
212 "OPTIONS:\n"
213 " -n(o flagging)\n"
214 " -f(lagging)\n"
215 " -q(uestion marks)\n"
216 " -c(olored symbols)\n"
217 " -d(ec charset symbols)\n"
218 "FIELDSPEC:\n"
219 " WxHxM (width 'x' height 'x' mines)\n"
220 " defaults to 30x16x99\n"
221 "\n"
222 "hjkl: move 1 left/down/up/right\n"
223 "bduw: move 5 left/down/up/right\n"
224 "^Gg$: move to the left/bottom/top/right\n"
225 "left mouse/o: open/choord\n"
226 "right mouse/i: flag/unflag\n"
227 "space: modeful cursor (either open or flag)\n"
228 "a: toggle mode for space (open/flag)\n"
229 ":D / r: start a new game\n"
230 "q: quit\n", argv[0]);
231 _exit(0);
232 }
233 }
234 /* end parse options*/
235 if (optind < argc) {
236 sscanf (argv[optind], "%dx%dx%d", &(f.w), &(f.h), &(f.m));
237 }
238 /* check boundaries */
239 if (f.m > (f.w-1) * (f.h-1)) {
240 f.m = (f.w-1) * (f.h-1);
241 fprintf (stderr, "too many mines. reduced to %d.\r\n", f.m);
242 }
243 /* end check */
244
245 newgame:
246 f.c = alloc_array (f.h, f.w);
247
248 f.f = 0;
249 f.t = 0;
250 f.p[0] = 0;
251 f.p[1] = 0;
252
253 int is_newgame = 1;
254 int cheatmode = 0;
255 f.s = MODE_OPEN;
256 struct line_col markers[26];
257 for (int i=26; i; markers[--i].l = -1);
258
259 /* switch to alternate screen */
260 printf ("\033[?47h");
261 alt_screen = 1;
262 /* reset cursor, clear screen */
263 printf ("\033[H\033[J");
264
265 /* swich charset, if necessary */
266 if (op.scheme->init_seq != NULL) print (op.scheme->init_seq);
267
268 show_minefield (NORMAL);
269
270 /* enable mouse, hide cursor */
271 printf ("\033[?1000h\033[?25l");
272
273 while (1) {
274 int l, c;
275 int action;
276 unsigned char mouse[3];
277
278 action = getch(mouse);
279 switch (action) {
280 case ' ':
281 if (f.s == MODE_OPEN ||
282 f.c[f.p[0]][f.p[1]].o == OPENED) {
283 switch (do_uncover(&is_newgame)) {
284 case GAME_LOST: goto lose;
285 case GAME_WON: goto win;
286 }
287 } else if (f.s == MODE_FLAG) {
288 flag_square (f.p[0], f.p[1]);
289 } else if (f.s == MODE_QUESM) {
290 quesm_square (f.p[0], f.p[1]);
291 }
292 break;
293 case 'a':
294 f.s = (f.s+1)%(op.mode+1);
295 show_minefield (cheatmode?SHOWMINES:NORMAL);
296 break;
297 case CTRSEQ_MOUSE_LEFT:
298 f.p[0] = screen2field_l (mouse[2]);
299 f.p[1] = screen2field_c (mouse[1]);
300 /* :D clicked: TODO: won't work in single-width mode! */
301 if (mouse[2] == LINE_OFFSET-1 &&
302 (mouse[1] == f.w+COL_OFFSET ||
303 mouse[1] == f.w+COL_OFFSET+1)) {
304 free_field ();
305 goto newgame;
306 }
307 if (f.p[1] < 0 || f.p[1] >= f.w ||
308 f.p[0] < 0 || f.p[0] >= f.h) break; /*out of bound*/
309 /* fallthrough */
310 case 'o':
311 switch (do_uncover(&is_newgame)) {
312 case GAME_LOST: goto lose;
313 case GAME_WON: goto win;
314 }
315 break;
316 case CTRSEQ_MOUSE_RIGHT:
317 f.p[0] = screen2field_l (mouse[2]);
318 f.p[1] = screen2field_c (mouse[1]);
319 if (f.p[1] < 0 || f.p[1] >= f.w ||
320 f.p[0] < 0 || f.p[0] >= f.h) break; /*out of bound*/
321 /* fallthrough */
322 case 'i': flag_square (f.p[0], f.p[1]); break;
323 case '?':quesm_square (f.p[0], f.p[1]); break;
324 #define BM BIG_MOVE
325 case 'h': cursor_move (f.p[0], f.p[1]-1 ); break;
326 case 'j': cursor_move (f.p[0]+1, f.p[1] ); break;
327 case 'k': cursor_move (f.p[0]-1, f.p[1] ); break;
328 case 'l': cursor_move (f.p[0], f.p[1]+1 ); break;
329 case 'w': cursor_move (f.p[0], f.p[1]+BM); break;
330 case 'b': cursor_move (f.p[0], f.p[1]-BM); break;
331 case 'u': cursor_move (f.p[0]-BM, f.p[1] ); break;
332 case 'd': cursor_move (f.p[0]+BM, f.p[1] ); break;
333 case '0': /* fallthrough */
334 case '^': cursor_move (f.p[0], 0 ); break;
335 case '$': cursor_move (f.p[0], f.w-1 ); break;
336 case 'g': cursor_move (0, f.p[1] ); break;
337 case 'G': cursor_move (f.h-1, f.p[1] ); break;
338 #undef BM
339 case 'm':
340 action = tolower(getch(mouse));
341 if (action < 'a' || action > 'z') break;/*out of bound*/
342 markers[action-'a'].l = f.p[0];
343 markers[action-'a'].c = f.p[1];
344 break;
345 case'\'': /* fallthrough */
346 case '`':
347 action = tolower(getch(mouse));
348 if (action < 'a' || action > 'z' /* out of bound or */
349 || markers[action-'a'].l == -1) break; /* unset */
350 cursor_move (markers[action-'a'].l, markers[action-'a'].c);
351 break;
352 case 'r': /* start a new game */
353 free_field ();
354 goto newgame;
355 case 'q':
356 goto quit;
357 case '\014': /* Ctrl-L -- redraw */
358 show_minefield (NORMAL);
359 break;
360 case '\\':
361 if (is_newgame) {
362 is_newgame = 0;
363 fill_minefield (-1, -1);
364 timer_setup(1);
365 }
366 show_minefield (cheatmode?NORMAL:SHOWMINES);
367 cheatmode = !cheatmode;
368 break;
369 }
370 }
371
372 win:
373 lose:
374 timer_setup(0); /* stop timer */
375 show_minefield (SHOWMINES);
376 int gotaction;
377 do {
378 unsigned char mouse[3];
379 gotaction = getch(mouse);
380 /* :D clicked: TODO: won't work in single-width mode! */
381 if (gotaction==CTRSEQ_MOUSE_LEFT && mouse[2]==LINE_OFFSET-1 &&
382 (mouse[1]==f.w+COL_OFFSET || mouse[1]==f.w+COL_OFFSET+1)) {
383 free_field ();
384 goto newgame;
385 } else if (gotaction == 'r') {
386 free_field ();
387 goto newgame;
388 } else if (gotaction == 'q') {
389 goto quit;
390 }
391 } while (1);
392
393 quit:
394 return 0;
395 }
396
397 void quit () {
398 move(0,0); //move(f.h+LINE_OFFSET+2, 0);
399 /* disable mouse, show cursor */
400 printf ("\033[?9l\033[?25h");
401 /* reset charset, if necessary */
402 if (op.scheme && op.scheme->reset_seq) print (op.scheme->reset_seq);
403 /* revert to primary screen */
404 if (alt_screen) printf ("\033[?47l");
405 free_field ();
406 restore_term_mode(saved_term_mode);
407 }
408
409 /* I haven't won as long as a cell exists, that
410 - I haven't opened, and
411 - is not a mine */
412 int everything_opened () {
413 for (int row = 0; row < f.h; row++)
414 for (int col = 0; col < f.w; col++)
415 if (f.c[row][col].o == CLOSED &&
416 f.c[row][col].m == NO_MINE ) return 0;
417 return 1;
418 }
419
420 int wait_mouse_up (int l, int c) {
421 unsigned char mouse2[3];
422 int level = 1;
423 int l2, c2;
424
425 /* show :o face */
426 move (1, field2screen_c (f.w/2)-1); print (":o");
427
428 if (!(l < 0 || l >= f.h || c < 0 || c >= f.w)) {
429 /* show a pushed-in button if cursor is on minefield */
430 move (l+LINE_OFFSET, field2screen_c(c));
431 fputs (op.scheme->mouse_highlight, stdout);
432 }
433
434 while (level > 0) {
435 if (getctrlseq (mouse2) == CTRSEQ_MOUSE) {
436 /* ignore mouse wheel events: */
437 if (mouse2[0] & 0x40) continue;
438
439 else if (mouse2[0]&3 == 3) level--; /* release event */
440 else level++; /* another button pressed */
441 }
442 }
443
444 move (1, field2screen_c (f.w/2)-1); print (":D");
445 if (!(l < 0 || l >= f.h || c < 0 || c >= f.w)) {
446 partial_show_minefield (l, c, NORMAL);
447 }
448 c2 = screen2field_c(mouse2[1]);
449 l2 = screen2field_l(mouse2[2]);
450 return ((l2 == l) && (c2 == c));
451 }
452
453 int choord_square (int line, int col) {
454 for (int l = MAX(line-1, 0); l <= MIN(line+1, f.h-1); l++) {
455 for (int c = MAX(col-1, 0); c <= MIN(col+1, f.w-1); c++) {
456 if (f.c[l][c].f != FLAG) {
457 if (uncover_square (l, c))
458 return 1;
459 }
460 }
461 }
462
463 return 0;
464 }
465
466 int uncover_square (int l, int c) {
467 f.c[l][c].o = OPENED;
468 f.c[l][c].f = NOFLAG; /*must not be QUESM, otherwise rendering issues*/
469 partial_show_minefield (l, c, NORMAL);
470
471 if (f.c[l][c].m) {
472 f.c[l][c].m = DEATH_MINE;
473 return 1;
474 }
475
476 /* check for chording */
477 if (f.c[l][c].n == 0) {
478 for (int choord_l = -1; choord_l <= 1; choord_l++) {
479 for (int choord_c = -1; choord_c <= 1; choord_c++) {
480 int newl = l + choord_l;
481 int newc = c + choord_c;
482 if (newl >= 0 && newl < f.h &&
483 newc >= 0 && newc < f.w &&
484 f.c[newl][newc].o == CLOSED &&
485 uncover_square (newl, newc)) {
486 return 1;
487 }
488 }
489 }
490 }
491
492 return 0;
493 }
494
495 void flag_square (int l, int c) {
496 if (f.c[l][c].o != CLOSED) return;
497 /* cycles through flag/quesm/noflag (uses op.mode to detect which ones
498 are allowed) */
499 f.c[l][c].f = (f.c[l][c].f + 1) % (op.mode + 1);
500 if (f.c[l][c].f==FLAG) f.f++;
501 else f.f--; //WARN: breaks on `-q'!
502 partial_show_minefield (l, c, NORMAL);
503 move (1, op.scheme->cell_width);
504 printf ("[%03d]", f.f);
505 }
506
507 void quesm_square (int l, int c) {
508 /* toggle question mark / none. won't turn flags into question marks.
509 unlike flag_square, this function doesn't respect `-q'. */
510 if (f.c[l][c].o != CLOSED) return;
511 else if (f.c[l][c].f == NOFLAG) f.c[l][c].f = QUESM;
512 else if (f.c[l][c].f == QUESM) f.c[l][c].f = NOFLAG;
513 partial_show_minefield (l, c, NORMAL);
514 }
515
516 int do_uncover (int* is_newgame) {
517 if (*is_newgame) {
518 *is_newgame = 0;
519 fill_minefield (f.p[0], f.p[1]);
520 timer_setup(1);
521 }
522
523 if (f.c[f.p[0]][f.p[1]].f == FLAG ) return GAME_INPROGRESS;
524 if (f.c[f.p[0]][f.p[1]].o == CLOSED) {
525 if (uncover_square (f.p[0], f.p[1])) return GAME_LOST;
526 } else if (get_neighbours (f.p[0], f.p[1], 1) == 0) {
527 if (choord_square (f.p[0], f.p[1])) return GAME_LOST;
528 }
529 if (everything_opened()) return GAME_WON;
530
531 return GAME_INPROGRESS;
532 }
533
534 void fill_minefield (int l, int c) {
535 srand (time(0));
536 int mines_set = f.m;
537 while (mines_set) {
538 int line = rand() % f.h;
539 int col = rand() % f.w;
540
541 if (f.c[line][col].m) {
542 /* skip if field already has a mine */
543 continue;
544 } else if ((line == l) && (col == c)) {
545 /* don't put a mine on the already opened (first click) field */
546 continue;
547 } else {
548 mines_set--;
549 f.c[line][col].m = STD_MINE;
550 }
551 }
552
553 /* precalculate neighbours */
554 for (int l=0; l < f.h; l++)
555 for (int c=0; c < f.w; c++)
556 f.c[l][c].n = get_neighbours (l, c, NORMAL);
557 }
558
559 void move (int line, int col) {
560 printf ("\033[%d;%dH", line+1, col+1);
561 }
562
563 /* absolute coordinates! */
564 void cursor_move (int l, int c) {
565 partial_show_minefield (f.p[0], f.p[1], NORMAL);
566 /* update f.p */
567 f.p[0] = CLAMP(l, 0, f.h-1);
568 f.p[1] = CLAMP(c, 0, f.w-1);
569 move (f.p[0]+LINE_OFFSET, field2screen_c(f.p[1]));
570 //fputs (op.scheme->mouse_highlight, stdout);
571
572 if (!f.c[f.p[0]][f.p[1]].f) print("\033[7m");//invert unless ! or ?
573 partial_show_minefield (f.p[0], f.p[1], HIGHLIGHT);
574 print("\033[0m");//un-invert
575 }
576
577 char* cell2schema (int l, int c, int mode) {
578 struct minecell cell = f.c[l][c];
579 /* move past invert-ctrlsequence when highlighting the cursor: */
580 int offset = ((mode==HIGHLIGHT)*op.scheme->flag_offset);
581
582 if (mode == SHOWMINES) return (
583 cell.f == FLAG && cell.m ? op.scheme->field_flagged+offset:
584 cell.f == FLAG && !cell.m ? op.scheme->mine_wrongf+offset:
585 cell.m == STD_MINE ? op.scheme->mine_normal:
586 cell.m == DEATH_MINE ? op.scheme->mine_death:
587 cell.o == CLOSED ? op.scheme->field_closed:
588 /*.......................*/ op.scheme->number[f.c[l][c].n]);
589 else return (
590 cell.f == FLAG ? op.scheme->field_flagged+offset:
591 cell.f == QUESM ? op.scheme->field_question+offset:
592 cell.o == CLOSED ? op.scheme->field_closed:
593 cell.m == STD_MINE ? op.scheme->mine_normal:
594 cell.m == DEATH_MINE ? op.scheme->mine_death:
595 /*.......................*/ op.scheme->number[f.c[l][c].n]);
596 }
597
598 void partial_show_minefield (int l, int c, int mode) {
599 move (l+LINE_OFFSET, field2screen_c(c));
600
601 print (cell2schema(l, c, mode));
602 }
603
604 void show_minefield (int mode) {
605 int dtime;
606 static char modechar[] = {'*', '!', '?'};
607
608 move (0,0);
609
610 if (f.t == 0) {
611 dtime = 0;
612 } else {
613 dtime = difftime (time(NULL), f.t);
614 }
615
616 /* first line */
617 print (op.scheme->border_top_l);
618 printm (f.w*op.scheme->cell_width,op.scheme->border_top_m);
619 printf ("%s\r\n", op.scheme->border_top_r);
620 /* second line */
621 print (op.scheme->border_status_l);
622 printf("[%03d]", f.f);
623 printm (f.w*op.scheme->cell_width/2-6, " ");
624 printf ("%s", mode==SHOWMINES?":C":":D");
625 printm (f.w*op.scheme->cell_width/2-6-4, " ");
626 printf ("[%c] [%03d]", modechar[f.s], dtime);
627 print (op.scheme->border_status_r);
628 print ("\r\n");
629 /* third line */
630 print (op.scheme->border_spacer_l);
631 printm (f.w*op.scheme->cell_width,op.scheme->border_spacer_m);
632 print (op.scheme->border_spacer_r);
633 print ("\r\n");
634 /* main body */
635 for (int l = 0; l < f.h; l++) {
636 print (op.scheme->border_field_l);
637 for (int c = 0; c < f.w; c++) {
638 print (cell2schema(l, c, mode));
639 }
640 print (op.scheme->border_field_r); print ("\r\n");
641 }
642 /* last line */
643 print (op.scheme->border_bottom_l);
644 printm (f.w*op.scheme->cell_width,op.scheme->border_bottom_m);
645 print (op.scheme->border_bottom_r);
646 print ("\r\n");
647 }
648
649 int get_neighbours (int line, int col, int reduced_mode) {
650 /* counts mines surrounding a square
651 modes: 0=normal; 1=reduced */
652
653 int count = 0;
654
655 for (int l = MAX(line-1, 0); l <= MIN(line+1, f.h-1); l++) {
656 for (int c = MAX(col-1, 0); c <= MIN(col+1, f.w-1); c++) {
657 if (!l && !c) continue;
658
659 count += !!f.c[l][c].m;
660 count -= reduced_mode * f.c[l][c].f==FLAG;
661 }
662 }
663 return count;
664 }
665
666 struct minecell** alloc_array (int lines, int cols) {
667 struct minecell** a = malloc (lines * sizeof(struct minecell*));
668 if (a == NULL) return NULL;
669 for (int l = 0; l < lines; l++) {
670 a[l] = calloc (cols, sizeof(struct minecell));
671 if (a[l] == NULL) goto unalloc;
672 }
673
674 return a;
675 unalloc:
676 for (int l = 0; l < lines; l++)
677 free (a[l]);
678 return NULL;
679 }
680
681 void free_field () {
682 if (f.c == NULL) return;
683 for (int l = 0; l < f.h; l++) {
684 free (f.c[l]);
685 }
686 free (f.c);
687 }
688
689 int screen2field_l (int l) {
690 return (l-LINE_OFFSET) - 1;
691 }
692 /* some trickery is required to extract the mouse position from the cell width,
693 depending on wheather we are using full width characters or double line width.
694 WARN: tested only with scheme.cell_width = 1 and scheme.cell_width = 2. */
695 int screen2field_c (int c) {
696 return (c-COL_OFFSET+1 - 2*(op.scheme->cell_width%2))/2 - op.scheme->cell_width/2;
697 }
698 int field2screen_l (int l) {
699 return 0; //TODO: is never used, therefore not implemented
700 }
701 int field2screen_c (int c) {
702 return (op.scheme->cell_width*c+COL_OFFSET - (op.scheme->cell_width%2));
703 }
704
705 enum esc_states {
706 START,
707 ESC_SENT,
708 CSI_SENT,
709 MOUSE_EVENT,
710 };
711 int getctrlseq (unsigned char* buf) {
712 int c;
713 int state = START;
714 int offset = 0x20; /* never sends control chars as data */
715 while ((c = getchar()) != EOF) {
716 switch (state) {
717 case START:
718 switch (c) {
719 case '\033': state=ESC_SENT; break;
720 default: return c;
721 }
722 break;
723 case ESC_SENT:
724 switch (c) {
725 case '[': state=CSI_SENT; break;
726 default: return CTRSEQ_INVALID;
727 }
728 break;
729 case CSI_SENT:
730 switch (c) {
731 case 'M': state=MOUSE_EVENT; break;
732 default: return CTRSEQ_INVALID;
733 }
734 break;
735 case MOUSE_EVENT:
736 buf[0] = c - offset;
737 buf[1] = getchar() - offset;
738 buf[2] = getchar() - offset;
739 return CTRSEQ_MOUSE;
740 default:
741 return CTRSEQ_INVALID;
742 }
743 }
744 return 2;
745 }
746
747 int getch(unsigned char* buf) {
748 /* returns a character, EOF, or constant for an escape/control sequence - NOT
749 compatible with the ncurses implementation of same name */
750 int action = getctrlseq(buf);
751 int l, c;
752 switch (action) {
753 case CTRSEQ_MOUSE:
754 l = screen2field_l (buf[2]);
755 c = screen2field_c (buf[1]);
756
757 if (buf[0] > 3) break; /* ignore all but left/middle/right/up */
758 int success = wait_mouse_up(l, c);
759
760 /* mouse moved while pressed: */
761 if (!success) return CTRSEQ_INVALID;
762
763 switch (buf[0]) {
764 case 0: return CTRSEQ_MOUSE_LEFT;
765 case 1: return CTRSEQ_MOUSE_MIDDLE;
766 case 2: return CTRSEQ_MOUSE_RIGHT;
767 }
768 }
769
770 return action;
771 }
772
773 void timer_setup (int enable) {
774 static struct itimerval tbuf;
775 tbuf.it_interval.tv_sec = 1;
776 tbuf.it_interval.tv_usec = 0;
777
778 if (enable) {
779 f.t = time(NULL);
780 tbuf.it_value.tv_sec = 1;
781 tbuf.it_value.tv_usec = 0;
782 if (setitimer(ITIMER_REAL, &tbuf, NULL) == -1) {
783 perror("setitimer");
784 exit(1);
785 }
786 } else {
787 tbuf.it_value.tv_sec = 0;
788 tbuf.it_value.tv_usec = 0;
789 if ( setitimer(ITIMER_REAL, &tbuf, NULL) == -1 ) {
790 perror("setitimer");
791 exit(1);
792 }
793 }
794
795 }
Imprint / Impressum