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