]> git.gir.st - minesVIiper.git/blob - mines.c
more event loop refactoring: mainly get/set marks
[minesVIiper.git] / mines.c
1 /*******************************************************************************
2 minesviiper 0.3.145926
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 - o/space to open and choord
12 - i to flag/unflag
13 - (see `./minesviiper -h' for all keybindings)
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/ioctl.h>
25 #include <sys/time.h>
26 #include <termios.h>
27 #include <time.h>
28 #include <unistd.h>
29
30 #include "mines.h"
31 #include "schemes.h"
32
33 #define LINE_OFFSET 3
34 #define LINES_AFTER 2
35 #define COL_OFFSET 2
36 #define BIG_MOVE 5
37 #define MOUSE_MAX 231
38
39 #define MIN(a,b) (a>b?b:a)
40 #define MAX(a,b) (a>b?a:b)
41 #define CLAMP(a,m,M) (a<m?m:(a>M?M:a))
42 #define printm(n, s) for (int _loop = 0; _loop < n; _loop++) fputs (s, stdout)
43 #define print(str) fputs (str?str:"", stdout)
44 #define EMOT(e) op.scheme->emoticons[EMOT_ ## e]
45 #define BORDER(l, c) op.scheme->border[B_ ## l][B_ ## c]
46 #define CW op.scheme->cell_width /* for brevity */
47 #define CTRL_ 0x1F &
48
49 struct minefield f;
50 struct game g;
51 struct opt op;
52
53 int main (int argc, char** argv) {
54 /* defaults: */
55 f.w = 30;
56 f.h = 16;
57 f.m = 99;
58 op.scheme = &symbols_mono;
59 op.mode = FLAG;
60
61 int optget;
62 opterr = 0; /* don't print message on unrecognized option */
63 while ((optget = getopt (argc, argv, "+hnfqcdx")) != -1) {
64 switch (optget) {
65 case 'n': op.mode = NOFLAG; break;
66 case 'f': op.mode = FLAG; break; /*default*/
67 case 'q': op.mode = QUESM; break;
68 case 'c': op.scheme = &symbols_col1; break;
69 case 'd': op.scheme = &symbols_doublewidth; break;
70 case 'h':
71 default:
72 fprintf (stderr, SHORTHELP LONGHELP, argv[0]);
73 return !(optget=='h');
74 }
75 } if (optind < argc) { /* parse Fieldspec */
76 if (parse_fieldspec (argv[optind])) {
77 fprintf (stderr, "FIELDSPEC: WxH[xM]"
78 " (width 'x' height 'x' mines)\n");
79 return 1;
80 }
81 }
82
83 signal_setup();
84 screen_setup(1);
85 atexit (*quit);
86
87 /* check boundaries */
88 if (f.m > (f.w-1) * (f.h-1)) {
89 move_ph (LINE_OFFSET+f.h+LINES_AFTER-1,0);
90 f.m = (f.w-1) * (f.h-1);
91 fprintf (stdout, "too many mines. reduced to %d.\r\n", f.m); //TODO: doesn't show up
92 }
93 /* end check */
94
95 newgame:
96 switch (minesviiper()) {
97 case GAME_NEW: goto newgame;
98 case GAME_WON: g.o = GAME_WON; break;
99 case GAME_LOST:g.o = GAME_LOST;break;
100 }
101
102 timer_setup(0); /* stop timer */
103 show_minefield (SHOWMINES);
104 for(;;) {
105 switch(getch_wrapper()) {
106 case WRAPPER_EMOTICON:
107 case 'r':
108 free_field ();
109 goto newgame;
110 case 'q': goto quit;
111 }
112 }
113
114 quit:
115 return 0;
116 }
117
118 int minesviiper(void) {
119 f.c = alloc_array (f.h, f.w); /* TODO: just memset it */
120 g = (const struct game){0}; /* reset all game-specific parameters */
121
122 show_minefield (NORMAL);
123
124 for(;;) {
125 switch (getch_wrapper()) {
126 case ' ':
127 if (g.s == MODE_OPEN || f.c[g.p[0]][g.p[1]].o == OPENED)
128 goto open_cell;
129 if (g.s == MODE_FLAG)
130 goto flag_cell;
131 if (g.s == MODE_QUESM)
132 goto quesm_cell;
133 break;
134 case 'a':
135 g.s = (g.s+1)%(op.mode+1);
136 show_minefield (g.c?SHOWMINES:NORMAL);
137 break;
138 open_cell:
139 case CTRSEQ_MOUSE_LEFT:
140 case 'o':
141 switch (do_uncover(&(g.n))) {
142 case GAME_LOST: return GAME_LOST;
143 case GAME_WON: return GAME_WON;
144 }
145 break;
146 flag_cell:
147 case CTRSEQ_MOUSE_RIGHT:
148 case 'i': flag_square (g.p[0], g.p[1]); break;
149 quesm_cell:
150 case '?':quesm_square (g.p[0], g.p[1]); break;
151 case CTRSEQ_CURSOR_LEFT:
152 case 'h': move_hi (g.p[0], g.p[1]-1); break;
153 case CTRSEQ_CURSOR_DOWN:
154 case 'j': move_hi (g.p[0]+1, g.p[1] ); break;
155 case CTRSEQ_CURSOR_UP:
156 case 'k': move_hi (g.p[0]-1, g.p[1] ); break;
157 case CTRSEQ_CURSOR_RIGHT:
158 case 'l': move_hi (g.p[0], g.p[1]+1); break;
159 case 'w': to_next_boundary (g.p[0], g.p[1], '>'); break;
160 case 'b': to_next_boundary (g.p[0], g.p[1], '<'); break;
161 case 'u': to_next_boundary (g.p[0], g.p[1], '^'); break;
162 case 'd': to_next_boundary (g.p[0], g.p[1], 'v'); break;
163 case '0': /* fallthrough */
164 case '^': move_hi (g.p[0], 0 ); break;
165 case '$': move_hi (g.p[0], f.w-1 ); break;
166 case 'g': move_hi (0, g.p[1] ); break;
167 case 'G': move_hi (f.h-1, g.p[1] ); break;
168 case 'z': move_hi (f.h/2, f.w/2); break;
169 case 'm': set_mark(); break;
170 case'\'': /* fallthrough */
171 case '`': jump_mark(); break;
172 case WRAPPER_EMOTICON:
173 case 'r': return GAME_NEW;
174 case 'q': return GAME_QUIT;
175 case CTRL_'L': show_minefield (NORMAL); break;
176 case '\\':
177 if (g.n == GAME_NEW) break; /* must open a cell first */
178 show_minefield (g.c?NORMAL:SHOWMINES);
179 g.c = !g.c;
180 break;
181 }
182 }
183
184 }
185
186 void quit (void) {
187 screen_setup(0);
188 free_field ();
189 }
190
191 /* I haven't won as long as a cell exists, that
192 - I haven't opened, and
193 - is not a mine */
194 int everything_opened (void) {
195 for (int row = 0; row < f.h; row++)
196 for (int col = 0; col < f.w; col++)
197 if (f.c[row][col].o == CLOSED &&
198 f.c[row][col].m == NO_MINE ) return 0;
199 return 1;
200 }
201
202 int wait_mouse_up (int l, int c) { /* TODO: should not take minefield-coords but absolute ones */
203 unsigned char mouse2[3];
204 int level = 1;
205 int l2, c2;
206
207 /* show :o face */
208 move_ph (1, field2screen_c (f.w/2)-1); print (EMOT(OHH));
209
210 if (!(l < 0 || l >= f.h || c < 0 || c >= f.w)) {
211 /* show a pushed-in button if cursor is on minefield */
212 move_ph (l+LINE_OFFSET, field2screen_c(c));
213 fputs (op.scheme->mouse_highlight, stdout);
214 }
215
216 while (level > 0) {
217 if (getctrlseq (mouse2) == CTRSEQ_MOUSE) {
218 /* ignore mouse wheel events: */
219 if (mouse2[0] & 0x40) continue;
220
221 else if (mouse2[0]&3 == 3) level--; /* release event */
222 else level++; /* another button pressed */
223 }
224 }
225
226 move_ph (1, field2screen_c (f.w/2)-1); print (get_emoticon());
227
228 if (!(l < 0 || l >= f.h || c < 0 || c >= f.w)) {
229 partial_show_minefield (l, c, NORMAL);
230 }
231 c2 = screen2field_c(mouse2[1]);
232 l2 = screen2field_l(mouse2[2]);
233 return ((l2 == l) && (c2 == c));
234 }
235
236 int choord_square (int line, int col) {
237 for (int l = MAX(line-1, 0); l <= MIN(line+1, f.h-1); l++) {
238 for (int c = MAX(col-1, 0); c <= MIN(col+1, f.w-1); c++) {
239 if (f.c[l][c].f != FLAG) {
240 if (uncover_square (l, c))
241 return 1;
242 }
243 }
244 }
245
246 return 0;
247 }
248
249 int uncover_square (int l, int c) {
250 f.c[l][c].o = OPENED;
251 f.c[l][c].f = NOFLAG; /*must not be QUESM, otherwise rendering issues*/
252 partial_show_minefield (l, c, NORMAL);
253
254 if (f.c[l][c].m) {
255 f.c[l][c].m = DEATH_MINE;
256 return 1;
257 }
258
259 /* check for chording */
260 if (f.c[l][c].n == 0) {
261 for (int choord_l = -1; choord_l <= 1; choord_l++) {
262 for (int choord_c = -1; choord_c <= 1; choord_c++) {
263 int newl = l + choord_l;
264 int newc = c + choord_c;
265 if (newl >= 0 && newl < f.h &&
266 newc >= 0 && newc < f.w &&
267 f.c[newl][newc].o == CLOSED &&
268 uncover_square (newl, newc)) {
269 return 1;
270 }
271 }
272 }
273 }
274
275 return 0;
276 }
277
278 void flag_square (int l, int c) {
279 static char modechar[] = {'*', '!', '?'};
280
281 if (f.c[l][c].o != CLOSED) return;
282 /* cycle through flag/quesm/noflag: */
283 f.c[l][c].f = (f.c[l][c].f + 1) % (op.mode + 1);
284 if (f.c[l][c].f==FLAG) g.f++;
285 else if ((op.mode==FLAG && f.c[l][c].f==NOFLAG) ||
286 (op.mode==QUESM && f.c[l][c].f==QUESM)) g.f--;
287 partial_show_minefield (l, c, NORMAL);
288 move_ph (1, op.scheme->cell_width);
289 printf ("[%03d%c]", f.m - g.f, modechar[g.s]);
290 }
291
292 void quesm_square (int l, int c) {
293 /* toggle question mark / none. won't turn flags into question marks.
294 unlike flag_square, this function doesn't respect `-q'. */
295 if (f.c[l][c].o != CLOSED) return;
296 else if (f.c[l][c].f == NOFLAG) f.c[l][c].f = QUESM;
297 else if (f.c[l][c].f == QUESM) f.c[l][c].f = NOFLAG;
298 partial_show_minefield (l, c, NORMAL);
299 }
300
301 int do_uncover (int* is_newgame) {
302 if (*is_newgame == GAME_NEW) {
303 *is_newgame = GAME_INPROGRESS;
304 fill_minefield (g.p[0], g.p[1]);
305 timer_setup(1);
306 }
307
308 if (f.c[g.p[0]][g.p[1]].f == FLAG ) return GAME_INPROGRESS;
309 if (f.c[g.p[0]][g.p[1]].o == CLOSED) {
310 if (uncover_square (g.p[0], g.p[1])) return GAME_LOST;
311 } else if (get_neighbours (g.p[0], g.p[1], 1) == 0) {
312 if (choord_square (g.p[0], g.p[1])) return GAME_LOST;
313 }
314 if (everything_opened()) return GAME_WON;
315
316 return GAME_INPROGRESS;
317 }
318
319 void set_mark(void) {
320 int mark = tolower(getch_wrapper());
321 if (mark < 'a' || mark > 'z') return; /*out of bound*/
322
323 g.m[mark-'a'].l = g.p[0];
324 g.m[mark-'a'].c = g.p[1];
325 g.m[mark-'a'].s = 1;
326 }
327
328 void jump_mark(void) {
329 int mark = tolower(getch_wrapper());
330 /* check bounds and if set: */
331 if (mark < 'a' || mark > 'z' || !g.m[mark-'a'].s) return;
332 move_hi (g.m[mark-'a'].l, g.m[mark-'a'].c);
333 }
334
335 void fill_minefield (int l, int c) {
336 srand (time(0));
337 int mines_set = f.m;
338 while (mines_set) {
339 int line = rand() % f.h;
340 int col = rand() % f.w;
341
342 if (f.c[line][col].m) {
343 /* skip if field already has a mine */
344 continue;
345 } else if ((line == l) && (col == c)) {
346 /* don't put a mine on the already opened (first click) field */
347 continue;
348 } else {
349 mines_set--;
350 f.c[line][col].m = STD_MINE;
351 }
352 }
353
354 /* precalculate neighbours */
355 for (int l=0; l < f.h; l++)
356 for (int c=0; c < f.w; c++)
357 f.c[l][c].n = get_neighbours (l, c, NORMAL);
358 }
359
360 void move_ph (int line, int col) {
361 /* move printhead to zero-indexed position */
362 printf ("\033[%d;%dH", line+1, col+1);
363 }
364
365 void move_hi (int l, int c) {
366 /* move cursor and highlight to absolute coordinates */
367
368 partial_show_minefield (g.p[0], g.p[1], NORMAL);
369 /* update g.p */
370 g.p[0] = CLAMP(l, 0, f.h-1);
371 g.p[1] = CLAMP(c, 0, f.w-1);
372 move_ph (g.p[0]+LINE_OFFSET, field2screen_c(g.p[1]));
373
374 print("\033[7m"); /* reverse video */
375 partial_show_minefield (g.p[0], g.p[1], HIGHLIGHT);
376 print("\033[0m"); /* un-invert */
377 }
378
379 /* to_next_boundary(): move into the supplied direction until a change in open-
380 state or flag-state is found and move there. falls back to BIG_MOVE. */
381 #define FIND_NEXT(X, L, C, L1, C1, MAX, OP) do {\
382 new_ ## X OP ## = BIG_MOVE;\
383 for (int i = X OP 1; i > 0 && i < f.MAX-1; i OP ## OP)\
384 if (((f.c[L ][C ].o<<2) + f.c[L ][C ].f) \
385 != ((f.c[L1][C1].o<<2) + f.c[L1][C1].f)) {\
386 new_ ## X = i OP 1;\
387 break;\
388 }\
389 } while(0)
390 void to_next_boundary (int l, int c, char direction) {
391 int new_l = l;
392 int new_c = c;
393 switch (direction) {
394 case '>': FIND_NEXT(c, l, i, l, i+1, w, +); break;
395 case '<': FIND_NEXT(c, l, i, l, i-1, w, -); break;
396 case '^': FIND_NEXT(l, i, c, i-1, c, h, -); break;
397 case 'v': FIND_NEXT(l, i, c, i+1, c, h, +); break;
398 }
399
400 move_hi (new_l, new_c);
401 }
402 #undef FIND_NEXT
403
404 char* cell2schema (int l, int c, int mode) {
405 struct minecell cell = f.c[l][c];
406
407 if (mode == SHOWMINES) return (
408 cell.f == FLAG && cell.m ? op.scheme->field_flagged:
409 cell.f == FLAG && !cell.m ? op.scheme->mine_wrongf:
410 cell.m == STD_MINE ? op.scheme->mine_normal:
411 cell.m == DEATH_MINE ? op.scheme->mine_death:
412 cell.o == CLOSED ? op.scheme->field_closed:
413 /*.......................*/ op.scheme->number[f.c[l][c].n]);
414 else return (
415 cell.f == FLAG ? op.scheme->field_flagged:
416 cell.f == QUESM ? op.scheme->field_question:
417 cell.o == CLOSED ? op.scheme->field_closed:
418 cell.m == STD_MINE ? op.scheme->mine_normal:
419 cell.m == DEATH_MINE ? op.scheme->mine_death:
420 /*.......................*/ op.scheme->number[f.c[l][c].n]);
421 }
422
423 void partial_show_minefield (int l, int c, int mode) {
424 move_ph (l+LINE_OFFSET, field2screen_c(c));
425
426 print (cell2schema(l, c, mode));
427 }
428
429 char* get_emoticon(void) {
430 return g.o==GAME_WON ? EMOT(WON):
431 g.o==GAME_LOST? EMOT(DEAD):
432 EMOT(SMILE);
433 }
434
435 /* https://zserge.com/blog/c-for-loop-tricks.html */
436 #define print_line(which) \
437 for (int _break = (printf("%s", BORDER(which,LEFT)), 1); _break; \
438 _break = 0, printf("%s\r\n", BORDER(which,RIGHT)))
439 #define print_border(which, width) \
440 print_line(which) printm (width, BORDER(which,MIDDLE))
441 void show_minefield (int mode) {
442 int dtime = difftime (time(NULL), g.t)*!!g.t;
443 int half_spaces = f.w*op.scheme->cell_width/2;
444 int left_spaces = MAX(0,half_spaces-7-(f.m-g.f>999));
445 int right_spaces = MAX(0,half_spaces-6-(dtime>999));
446 static char modechar[] = {'*', '!', '?'};
447
448 move_ph (0,0);
449
450 print_border(TOP, f.w);
451 print_line(STATUS) {
452 printf("[%03d%c]%*s%s%*s[%03d]",
453 /* [ */ f.m - g.f, modechar[g.s], /* ] */
454 left_spaces,"", get_emoticon(), right_spaces,"",
455 /* [ */ dtime /* ] */);
456 }
457 print_border(DIVIDER, f.w);
458 /* main body */
459 for (int l = 0; l < f.h; l++) print_line(FIELD)
460 printm (f.w, cell2schema(l, _loop, mode));
461 print_border(BOTTOM, f.w);
462 }
463
464 int get_neighbours (int line, int col, int reduced_mode) {
465 /* counts mines surrounding a square
466 modes: 0=normal; 1=reduced */
467
468 int count = 0;
469
470 for (int l = MAX(line-1, 0); l <= MIN(line+1, f.h-1); l++) {
471 for (int c = MAX(col-1, 0); c <= MIN(col+1, f.w-1); c++) {
472 if (!l && !c) continue;
473
474 count += !!f.c[l][c].m;
475 count -= reduced_mode * f.c[l][c].f==FLAG;
476 }
477 }
478 return count;
479 }
480
481 struct minecell** alloc_array (int lines, int cols) {
482 free_field (); /* TODO: just memset */
483 struct minecell** a = malloc (lines * sizeof(struct minecell*));
484 if (a == NULL) return NULL;
485 for (int l = 0; l < lines; l++) {
486 a[l] = calloc (cols, sizeof(struct minecell));
487 if (a[l] == NULL) goto unalloc;
488 }
489
490 return a;
491 unalloc:
492 for (int l = 0; l < lines; l++)
493 free (a[l]);
494 return NULL;
495 }
496
497 void free_field (void) {
498 if (f.c == NULL) return;
499 for (int l = 0; l < f.h; l++) {
500 free (f.c[l]);
501 }
502
503 free (f.c);
504 f.c = NULL;
505 }
506
507 int screen2field_l (int l) {
508 return (l-LINE_OFFSET) - 1;
509 }
510 int screen2field_c (int c) {
511 /* this depends on the cell width and only works when it is 1 or 2. */
512 return (c-COL_OFFSET+1 - 2*(CW%2))/2 - CW/2;
513 }
514 int field2screen_c (int c) {
515 return (CW*c+COL_OFFSET - (CW%2));
516 }
517 int clicked_emoticon (unsigned char* mouse) {
518 return (mouse[2] == LINE_OFFSET-1 && (
519 mouse[1] == f.w+COL_OFFSET ||
520 mouse[1] == f.w+COL_OFFSET+1));
521 }
522
523 enum esc_states {
524 START,
525 ESC_SENT,
526 CSI_SENT,
527 MOUSE_EVENT,
528 };
529 int getctrlseq (unsigned char* buf) {
530 int c;
531 int state = START;
532 int offset = 0x20; /* never sends control chars as data */
533 while ((c = getchar()) != EOF) {
534 switch (state) {
535 case START:
536 switch (c) {
537 case '\033': state=ESC_SENT; break;
538 default: return c;
539 }
540 break;
541 case ESC_SENT:
542 switch (c) {
543 case '[': state=CSI_SENT; break;
544 default: return CTRSEQ_INVALID;
545 }
546 break;
547 case CSI_SENT:
548 switch (c) {
549 case 'A': return CTRSEQ_CURSOR_UP;
550 case 'B': return CTRSEQ_CURSOR_DOWN;
551 case 'C': return CTRSEQ_CURSOR_RIGHT;
552 case 'D': return CTRSEQ_CURSOR_LEFT;
553 case 'M': state=MOUSE_EVENT; break;
554 default: return CTRSEQ_INVALID;
555 }
556 break;
557 case MOUSE_EVENT:
558 buf[0] = c - offset;
559 buf[1] = getchar() - offset;
560 buf[2] = getchar() - offset;
561 return CTRSEQ_MOUSE;
562 default:
563 return CTRSEQ_INVALID;
564 }
565 }
566 return 2;
567 }
568
569 int getch(unsigned char* buf) {
570 /* returns a character, EOF, or constant for an escape/control sequence - NOT
571 compatible with the ncurses implementation of same name */
572 int action = getctrlseq(buf);
573 int l, c;
574 switch (action) {
575 case CTRSEQ_MOUSE:
576 l = screen2field_l (buf[2]);
577 c = screen2field_c (buf[1]);
578
579 if (buf[0] > 3) break; /* ignore all but left/middle/right/up */
580 int success = wait_mouse_up(l, c);
581
582 /* mouse moved while pressed: */
583 if (!success) return CTRSEQ_INVALID;
584
585 switch (buf[0]) {
586 case 0: return CTRSEQ_MOUSE_LEFT;
587 case 1: return CTRSEQ_MOUSE_MIDDLE;
588 case 2: return CTRSEQ_MOUSE_RIGHT;
589 }
590 }
591
592 return action;
593 }
594
595 int getch_wrapper (void) {
596 unsigned char mouse[3];
597 int c = getch(mouse);
598
599 if (c == CTRSEQ_MOUSE_LEFT || c == CTRSEQ_MOUSE_RIGHT) {
600 if (clicked_emoticon(mouse))
601 return WRAPPER_EMOTICON;
602
603 if (screen2field_c (mouse[1]) < 0
604 || screen2field_c (mouse[1]) >= f.w
605 || screen2field_l (mouse[2]) < 0
606 || screen2field_l (mouse[2]) >= f.h)
607 return CTRSEQ_INVALID;
608
609 g.p[0] = screen2field_l (mouse[2]);
610 g.p[1] = screen2field_c (mouse[1]);
611
612 return c; /* CTRSEQ_MOUSE_LEFT || CTRSEQ_MOUSE_RIGHT */
613 }
614
615 return c;
616 }
617
618 int parse_fieldspec(char* str) {
619 /* parses the FIELDSPEC (WxHxM); returns 1 on error */
620 int n = sscanf (str, "%dx%dx%d", &f.w, &f.h, &f.m);
621 struct winsize w;
622 ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
623
624 /* clamp field size to terminal size and mouse maximum: */
625 if (COL_OFFSET + f.w*CW + COL_OFFSET > w.ws_col)
626 f.w = w.ws_col/CW - (COL_OFFSET+COL_OFFSET);
627 if (LINE_OFFSET + f.h + LINES_AFTER > w.ws_row)
628 f.h = w.ws_row - (LINE_OFFSET+LINES_AFTER);
629 if (COL_OFFSET + f.w*CW > MOUSE_MAX)
630 f.w = MOUSE_MAX/CW - COL_OFFSET;
631 if (LINE_OFFSET + f.h > MOUSE_MAX)
632 f.h = MOUSE_MAX - LINE_OFFSET;
633
634 if (n < 2) {
635 return 1; /* error */
636 } else if (n == 2) {
637 if (f.w < 30) f.m = f.w*f.h*.15625;
638 else f.m = f.w*f.h*.20625;
639 }
640
641 return 0;
642 }
643
644 void timer_setup (int enable) {
645 static struct itimerval tbuf;
646 tbuf.it_interval.tv_sec = 1;
647 tbuf.it_interval.tv_usec = 0;
648
649 if (enable) {
650 g.t = time(NULL);
651 tbuf.it_value.tv_sec = 1;
652 tbuf.it_value.tv_usec = 0;
653 if (setitimer(ITIMER_REAL, &tbuf, NULL) == -1) {
654 perror("setitimer");
655 exit(1);
656 }
657 } else {
658 tbuf.it_value.tv_sec = 0;
659 tbuf.it_value.tv_usec = 0;
660 if ( setitimer(ITIMER_REAL, &tbuf, NULL) == -1 ) {
661 perror("setitimer");
662 exit(1);
663 }
664 }
665
666 }
667
668 void signal_setup (void) {
669 struct sigaction saction;
670
671 saction.sa_handler = signal_handler;
672 sigemptyset(&saction.sa_mask);
673 saction.sa_flags = 0;
674 if (sigaction(SIGALRM, &saction, NULL) < 0 ) {
675 perror("SIGALRM");
676 exit(1);
677 }
678
679 if (sigaction(SIGINT, &saction, NULL) < 0 ) {
680 perror ("SIGINT");
681 exit (1);
682 }
683 }
684
685 void signal_handler (int signum) {
686 int dtime;
687 switch (signum) {
688 case SIGALRM:
689 dtime = difftime (time(NULL), g.t);
690 move_ph (1, f.w*CW-(CW%2)-3-(dtime>999));
691 printf ("[%03d]", g.t?dtime:0);
692 break;
693 case SIGINT:
694 exit(128+SIGINT);
695 }
696 }
697
698 void screen_setup (int enable) {
699 if (enable) {
700 raw_mode(1);
701 printf ("\033[s\033[?47h"); /* save cursor, alternate screen */
702 printf ("\033[H\033[J"); /* reset cursor, clear screen */
703 printf ("\033[?1000h\033[?25l"); /* enable mouse, hide cursor */
704 print (op.scheme->init_seq); /* swich charset, if necessary */
705 } else {
706 print (op.scheme->reset_seq); /* reset charset, if necessary */
707 printf ("\033[?9l\033[?25h"); /* disable mouse, show cursor */
708 printf ("\033[?47l\033[u"); /* primary screen, restore cursor */
709 raw_mode(0);
710 }
711 }
712
713 /* http://users.csc.calpoly.edu/~phatalsk/357/lectures/code/sigalrm.c */
714 void raw_mode(int enable) {
715 static struct termios saved_term_mode;
716 struct termios raw_term_mode;
717
718 if (enable) {
719 tcgetattr(STDIN_FILENO, &saved_term_mode);
720 raw_term_mode = saved_term_mode;
721 raw_term_mode.c_lflag &= ~(ICANON | ECHO);
722 raw_term_mode.c_cc[VMIN] = 1 ;
723 raw_term_mode.c_cc[VTIME] = 0;
724 tcsetattr(STDIN_FILENO, TCSAFLUSH, &raw_term_mode);
725 } else {
726 tcsetattr(STDIN_FILENO, TCSAFLUSH, &saved_term_mode);
727 }
728 }
Imprint / Impressum