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