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