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