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