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