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