]>
git.gir.st - minesVIiper.git/blob - mines.c
1 /*******************************************************************************
3 By Tobias Girstmair, 2015 - 2018
6 (see ./minesviiper -h for full list of options)
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
13 - (see `./minesviiper -h' for all keybindings)
15 GNU GPL v3, see LICENSE or https://www.gnu.org/licenses/gpl-3.0.txt
16 *******************************************************************************/
19 #define _POSIX_C_SOURCE 2 /*for getopt, sigaction in c99*/
24 #include <sys/ioctl.h>
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 */
49 #define CELL f.c[ROW][COL] /* helper for AROUND() */
50 #define AROUND(l, c) /* gives us loop-variables ROW, COL, CELL */ \
51 for (int ROW = MAX(l-1, 0); ROW <= MIN(l+1, f.h-1); ROW++) \
52 for (int COL = MAX(c-1, 0); COL <= MIN(c+1, f.w-1); COL++) \
53 if (!(ROW == l && COL == c)) /* skip itself */
59 int main ( int argc
, char ** argv
) {
64 op
. scheme
= & symbols_mono
;
68 opterr
= 0 ; /* don't print message on unrecognized option */
69 while (( optget
= getopt ( argc
, argv
, "+hnfqcdx" )) != - 1 ) {
71 case 'n' : op
. mode
= NOFLAG
; break ;
72 case 'f' : op
. mode
= FLAG
; break ; /*default*/
73 case 'q' : op
. mode
= QUESM
; break ;
74 case 'c' : op
. scheme
= & symbols_col1
; break ;
75 case 'd' : op
. scheme
= & symbols_doublewidth
; break ;
78 fprintf ( stderr
, SHORTHELP LONGHELP
, argv
[ 0 ]);
79 return !( optget
== 'h' );
81 } if ( optind
< argc
) { /* parse Fieldspec */
82 if ( parse_fieldspec ( argv
[ optind
])) {
83 fprintf ( stderr
, "FIELDSPEC: WxH[xM]"
84 " (width 'x' height 'x' mines) \n " );
93 /* check boundaries */
94 if ( f
. m
> ( f
. w
- 1 ) * ( f
. h
- 1 )) {
95 move_ph ( LINE_OFFSET
+ f
. h
+ LINES_AFTER
- 1 , 0 );
96 f
. m
= ( f
. w
- 1 ) * ( f
. h
- 1 );
97 fprintf ( stdout
, "too many mines. reduced to %d. \r\n " , f
. m
); //TODO: should disappear after a timeout (stomp-hi?)
102 switch ( minesviiper ()) {
103 case GAME_NEW
: goto newgame
;
104 case GAME_WON
: g
. o
= GAME_WON
; break ;
105 case GAME_LOST
: g
. o
= GAME_LOST
; break ;
106 case GAME_QUIT
: goto quit
;
109 timer_setup ( 0 ); /* stop timer */
110 show_minefield ( SHOWMINES
);
112 switch ( getch_wrapper ()) {
113 case WRAPPER_EMOTICON
:
125 int minesviiper ( void ) {
126 f
. c
= alloc_array ( f
. h
, f
. w
); /* TODO: just memset it */
127 g
= ( const struct game
){ 0 }; /* reset all game-specific parameters */
129 show_minefield ( NORMAL
);
132 switch ( getch_wrapper ()) {
134 if ( f
. c
[ g
. p
[ 0 ]][ g
. p
[ 1 ]]. o
== OPENED
) goto open_cell
;
135 if ( g
. s
== MODE_OPEN
) goto open_cell
;
136 if ( g
. s
== MODE_FLAG
) goto flag_cell
;
137 if ( g
. s
== MODE_QUESM
) goto quesm_cell
;
140 g
. s
= ( g
. s
+ 1 )%( op
. mode
+ 1 );
141 show_minefield ( g
. c
? SHOWMINES
: NORMAL
);
144 case CTRSEQ_MOUSE_LEFT
:
146 switch ( do_uncover (&( g
. n
))) {
147 case GAME_LOST
: return GAME_LOST
;
148 case GAME_WON
: return GAME_WON
;
152 case CTRSEQ_MOUSE_RIGHT
:
153 case 'i' : flag_square ( g
. p
[ 0 ], g
. p
[ 1 ]); break ;
155 case '?' : quesm_square ( g
. p
[ 0 ], g
. p
[ 1 ]); break ;
156 case CTRSEQ_CURSOR_LEFT
:
157 case 'h' : move_hi ( g
. p
[ 0 ], g
. p
[ 1 ]- 1 ); break ;
158 case CTRSEQ_CURSOR_DOWN
:
159 case 'j' : move_hi ( g
. p
[ 0 ]+ 1 , g
. p
[ 1 ] ); break ;
160 case CTRSEQ_CURSOR_UP
:
161 case 'k' : move_hi ( g
. p
[ 0 ]- 1 , g
. p
[ 1 ] ); break ;
162 case CTRSEQ_CURSOR_RIGHT
:
163 case 'l' : move_hi ( g
. p
[ 0 ], g
. p
[ 1 ]+ 1 ); break ;
164 case 'w' : to_next_boundary ( g
. p
[ 0 ], g
. p
[ 1 ], '>' ); break ;
165 case 'b' : to_next_boundary ( g
. p
[ 0 ], g
. p
[ 1 ], '<' ); break ;
166 case 'u' : to_next_boundary ( g
. p
[ 0 ], g
. p
[ 1 ], '^' ); break ;
167 case 'd' : to_next_boundary ( g
. p
[ 0 ], g
. p
[ 1 ], 'v' ); break ;
168 case '0' : /* fallthrough */
169 case '^' : move_hi ( g
. p
[ 0 ], 0 ); break ;
170 case '$' : move_hi ( g
. p
[ 0 ], f
. w
- 1 ); break ;
171 case 'g' : move_hi ( 0 , g
. p
[ 1 ] ); break ;
172 case 'G' : move_hi ( f
. h
- 1 , g
. p
[ 1 ] ); break ;
173 case 'z' : move_hi ( f
. h
/ 2 , f
. w
/ 2 ); break ;
174 case 'm' : set_mark (); break ;
175 case ' \' ' : /* fallthrough */
176 case '`' : jump_mark (); break ;
177 case WRAPPER_EMOTICON
:
178 case 'r' : return GAME_NEW
;
179 case 'q' : return GAME_QUIT
;
182 show_minefield ( NORMAL
);
185 /* resize minefield mode */
188 switch ( getch_wrapper ()) { //TODO: segfault on mouse up
189 case 'q' : return GAME_NEW
;
190 case 'h' : f
. w
--; break ;
191 case 'j' : f
. h
++; break ;
192 case 'k' : f
. h
--; break ;
193 case 'l' : f
. w
++; break ;
195 // TODO: clamp size to termsize/mousemax
196 /* recalculate mines: */
197 f
. m
= f
. w
* f
. h
*( f
. w
* f
. h
< 30 * 16 ? .15625 : .20625 ); //TODO: should be function
199 f
. c
= alloc_array ( f
. h
, f
. w
);
200 screen_setup ( 1 ); /* clears the screen */
201 show_minefield ( RESIZEMODE
);
204 if ( g
. n
== GAME_NEW
) break ; /* must open a cell first */
206 show_minefield ( g
. c
? SHOWMINES
: NORMAL
);
218 /* I haven't won as long as a cell exists, that
219 - I haven't opened, and
221 int everything_opened ( void ) {
222 for ( int row
= 0 ; row
< f
. h
; row
++)
223 for ( int col
= 0 ; col
< f
. w
; col
++)
224 if ( f
. c
[ row
][ col
]. o
== CLOSED
&&
225 f
. c
[ row
][ col
]. m
== NO_MINE
) return 0 ;
229 int wait_mouse_up ( int l
, int c
) { /* TODO: should not take minefield-coords but absolute ones */
230 unsigned char mouse2
[ 3 ];
235 move_ph ( 1 , field2screen_c ( f
. w
/ 2 )- 1 ); print ( EMOT ( OHH
));
237 if (!( l
< 0 || l
>= f
. h
|| c
< 0 || c
>= f
. w
)) {
238 /* show a pushed-in button if cursor is on minefield */
239 partial_show_minefield ( l
, c
, HIGHLIGHT
);
243 if ( getctrlseq ( mouse2
) == CTRSEQ_MOUSE
) {
244 /* ignore mouse wheel events: */
245 if ( mouse2
[ 0 ] & 0x40 ) continue ;
247 else if ( mouse2
[ 0 ]& 3 == 3 ) level
--; /* release event */
248 else level
++; /* another button pressed */
252 move_ph ( 1 , field2screen_c ( f
. w
/ 2 )- 1 ); print ( get_emoticon ());
254 if (!( l
< 0 || l
>= f
. h
|| c
< 0 || c
>= f
. w
)) {
255 partial_show_minefield ( l
, c
, NORMAL
);
257 c2
= screen2field_c ( mouse2
[ 1 ]);
258 l2
= screen2field_l ( mouse2
[ 2 ]);
259 return (( l2
== l
) && ( c2
== c
));
262 int choord_square ( int line
, int col
) {
263 if ( uncover_square ( line
, col
)) return 1 ;
265 if ( CELL
. f
!= FLAG
) {
266 if ( uncover_square ( ROW
, COL
))
273 int uncover_square ( int l
, int c
) {
274 if ( f
. c
[ l
][ c
]. o
== OPENED
)
275 return 0 ; /* nothing to do */
277 f
. c
[ l
][ c
]. o
= OPENED
;
278 f
. c
[ l
][ c
]. f
= NOFLAG
; /*must not be QUESM, otherwise rendering issues*/
279 partial_show_minefield ( l
, c
, NORMAL
);
282 f
. c
[ l
][ c
]. m
= DEATH_MINE
;
286 /* check for chording */
287 if ( f
. c
[ l
][ c
]. n
== 0 ) {
289 if ( uncover_square ( ROW
, COL
)) return 1 ;
295 void flag_square ( int l
, int c
) {
296 static char modechar
[] = { '*' , '!' , '?' };
298 if ( f
. c
[ l
][ c
]. o
!= CLOSED
) return ;
299 /* cycle through flag/quesm/noflag: */
300 f
. c
[ l
][ c
]. f
= ( f
. c
[ l
][ c
]. f
+ 1 ) % ( op
. mode
+ 1 );
301 if ( f
. c
[ l
][ c
]. f
== FLAG
) g
. f
++;
302 else if (( op
. mode
== FLAG
&& f
. c
[ l
][ c
]. f
== NOFLAG
) ||
303 ( op
. mode
== QUESM
&& f
. c
[ l
][ c
]. f
== QUESM
)) g
. f
--;
304 partial_show_minefield ( l
, c
, NORMAL
);
305 move_ph ( 1 , op
. scheme
-> cell_width
);
306 printf ( "[%03d%c]" , f
. m
- g
. f
, modechar
[ g
. s
]);
309 void quesm_square ( int l
, int c
) {
310 /* toggle question mark / none. won't turn flags into question marks.
311 unlike flag_square, this function doesn't respect `-q'. */
312 if ( f
. c
[ l
][ c
]. o
!= CLOSED
) return ;
313 else if ( f
. c
[ l
][ c
]. f
== NOFLAG
) f
. c
[ l
][ c
]. f
= QUESM
;
314 else if ( f
. c
[ l
][ c
]. f
== QUESM
) f
. c
[ l
][ c
]. f
= NOFLAG
;
315 partial_show_minefield ( l
, c
, NORMAL
);
318 int do_uncover ( int * is_newgame
) {
319 if (* is_newgame
== GAME_NEW
) {
320 * is_newgame
= GAME_INPROGRESS
;
321 fill_minefield ( g
. p
[ 0 ], g
. p
[ 1 ]);
325 if ( f
. c
[ g
. p
[ 0 ]][ g
. p
[ 1 ]]. f
== FLAG
) return GAME_INPROGRESS
;
326 if ( f
. c
[ g
. p
[ 0 ]][ g
. p
[ 1 ]]. o
== CLOSED
) {
327 if ( uncover_square ( g
. p
[ 0 ], g
. p
[ 1 ])) return GAME_LOST
;
328 } else if ( get_neighbours ( g
. p
[ 0 ], g
. p
[ 1 ], 1 ) == 0 ) {
329 if ( choord_square ( g
. p
[ 0 ], g
. p
[ 1 ])) return GAME_LOST
;
331 /* show the stomp size if we aren't fully flagged */
332 AROUND ( g
. p
[ 0 ], g
. p
[ 1 ])
333 if ( CELL
. o
== CLOSED
&& CELL
. f
!= FLAG
)
334 partial_show_minefield ( ROW
, COL
, HIGHLIGHT
); //TODO: hide after timeout
335 /* save the highlight position, timeout-end, and setup a timer. if another tiemout comes in before the timeout is over, the old one shall be removed and the timer cancelled before drawing the new one. if we hit any key it shall remove itself aswell */
337 if ( everything_opened ()) return GAME_WON
;
339 return GAME_INPROGRESS
;
342 void set_mark ( void ) {
343 int mark
= tolower ( getch_wrapper ());
344 if ( mark
< 'a' || mark
> 'z' ) return ; /*out of bound*/
346 g
. m
[ mark
- 'a' ]. l
= g
. p
[ 0 ];
347 g
. m
[ mark
- 'a' ]. c
= g
. p
[ 1 ];
351 void jump_mark ( void ) {
352 int mark
= tolower ( getch_wrapper ());
353 /* check bounds and if set: */
354 if ( mark
< 'a' || mark
> 'z' || ! g
. m
[ mark
- 'a' ]. s
) return ;
355 move_hi ( g
. m
[ mark
- 'a' ]. l
, g
. m
[ mark
- 'a' ]. c
);
358 void fill_minefield ( int l
, int c
) {
362 int line
= rand () % f
. h
;
363 int col
= rand () % f
. w
;
365 if ( f
. c
[ line
][ col
]. m
) {
366 /* skip if field already has a mine */
368 } else if (( line
== l
) && ( col
== c
)) {
369 /* don't put a mine on the already opened (first click) field */
373 f
. c
[ line
][ col
]. m
= STD_MINE
;
377 /* precalculate neighbours */
378 for ( int l
= 0 ; l
< f
. h
; l
++)
379 for ( int c
= 0 ; c
< f
. w
; c
++)
380 f
. c
[ l
][ c
]. n
= get_neighbours ( l
, c
, NORMAL
);
383 void move_ph ( int line
, int col
) {
384 /* move printhead to zero-indexed position */
385 printf ( " \033 [%d;%dH" , line
+ 1 , col
+ 1 );
388 void move_hi ( int l
, int c
) {
389 /* move cursor and highlight to absolute coordinates */
391 partial_show_minefield ( g
. p
[ 0 ], g
. p
[ 1 ], NORMAL
);
393 g
. p
[ 0 ] = CLAMP ( l
, 0 , f
. h
- 1 );
394 g
. p
[ 1 ] = CLAMP ( c
, 0 , f
. w
- 1 );
395 move_ph ( g
. p
[ 0 ]+ LINE_OFFSET
, field2screen_c ( g
. p
[ 1 ]));
397 partial_show_minefield ( g
. p
[ 0 ], g
. p
[ 1 ], HIGHLIGHT
);
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)) {\
411 void to_next_boundary ( int l
, int c
, char 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 ;
421 move_hi ( new_l
, new_c
);
425 char * cell2schema ( int l
, int c
, int mode
) {
426 struct minecell cell
= f
. c
[ l
][ c
];
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
]);
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
]);
444 void partial_show_minefield ( int l
, int c
, int mode
) {
445 move_ph ( l
+ LINE_OFFSET
, field2screen_c ( c
));
446 if ( mode
== HIGHLIGHT
) printf ( " \033 [7m" ); /* reverse video */
447 print ( cell2schema ( l
, c
, mode
));
448 if ( mode
== HIGHLIGHT
) printf ( " \033 [0m" ); /* reset all */
451 char * get_emoticon ( void ) {
452 return g
. o
== GAME_WON
? EMOT ( WON
):
453 g
. o
== GAME_LOST
? EMOT ( DEAD
):
457 /* https://zserge.com/blog/c-for-loop-tricks.html */
458 #define print_line(which) \
459 for (int _break = (printf( "%s" , BORDER(which,LEFT)), 1); _break; \
460 _break = 0, printf( "%s \r\n " , BORDER(which,RIGHT)))
461 #define print_border(which, width) \
462 print_line(which) printm (width, BORDER(which,MIDDLE))
463 void show_minefield ( int mode
) {
464 int dtime
= difftime ( time ( NULL
), g
. t
)*!! g
. t
;
465 int half_spaces
= f
. w
* op
. scheme
-> cell_width
/ 2 ;
466 int left_spaces
= MAX ( 0 , half_spaces
- 7 -( f
. m
- g
. f
> 999 ));
467 int right_spaces
= MAX ( 0 , half_spaces
- 6 -( dtime
> 999 ));
468 static char modechar
[] = { '*' , '!' , '?' };
472 print_border ( TOP
, f
. w
);
474 if ( mode
== RESIZEMODE
) printf ( "%-*s" , 2 * half_spaces
,
475 "Resize Mode: hjkl to resize, q to exit" );
476 else printf ( "[%03d%c]%*s%s%*s[%03d]" ,
477 /* [ */ f
. m
- g
. f
, modechar
[ g
. s
], /* ] */
478 left_spaces
, "" , get_emoticon (), right_spaces
, "" ,
479 /* [ */ dtime
/* ] */ );
481 print_border ( DIVIDER
, f
. w
);
483 for ( int l
= 0 ; l
< f
. h
; l
++) print_line ( FIELD
)
484 printm ( f
. w
, cell2schema ( l
, _loop
, mode
));
485 print_border ( BOTTOM
, f
. w
);
488 int get_neighbours ( int line
, int col
, int reduced_mode
) {
489 /* counts mines surrounding a square
490 modes: 0=normal; 1=reduced */
495 count
-= reduced_mode
* CELL
. f
== FLAG
;
500 struct minecell
** alloc_array ( int lines
, int cols
) {
501 free_field (); /* TODO: just memset */
502 struct minecell
** a
= malloc ( lines
* sizeof ( struct minecell
*));
503 if ( a
== NULL
) return NULL
;
504 for ( int l
= 0 ; l
< lines
; l
++) {
505 a
[ l
] = calloc ( cols
, sizeof ( struct minecell
));
506 if ( a
[ l
] == NULL
) goto unalloc
;
511 for ( int l
= 0 ; l
< lines
; l
++)
516 void free_field ( void ) {
517 if ( f
. c
== NULL
) return ;
518 for ( int l
= 0 ; l
< f
. h
; l
++) {
526 int screen2field_l ( int l
) {
527 return ( l
- LINE_OFFSET
) - 1 ;
529 int screen2field_c ( int c
) {
530 /* this depends on the cell width and only works when it is 1 or 2. */
531 return ( c
- COL_OFFSET
+ 1 - 2 *( CW
% 2 ))/ 2 - CW
/ 2 ;
533 int field2screen_c ( int c
) {
534 return ( CW
* c
+ COL_OFFSET
- ( CW
% 2 ));
536 int clicked_emoticon ( unsigned char * mouse
) {
537 return ( mouse
[ 2 ] == LINE_OFFSET
- 1 && (
538 mouse
[ 1 ] == f
. w
+ COL_OFFSET
||
539 mouse
[ 1 ] == f
. w
+ COL_OFFSET
+ 1 ));
548 int getctrlseq ( unsigned char * buf
) {
551 int offset
= 0x20 ; /* never sends control chars as data */
552 while (( c
= getchar ()) != EOF
) {
556 case ' \033 ' : state
= ESC_SENT
; break ;
562 case '[' : state
= CSI_SENT
; break ;
563 default : return CTRSEQ_INVALID
;
568 case 'A' : return CTRSEQ_CURSOR_UP
;
569 case 'B' : return CTRSEQ_CURSOR_DOWN
;
570 case 'C' : return CTRSEQ_CURSOR_RIGHT
;
571 case 'D' : return CTRSEQ_CURSOR_LEFT
;
572 case 'M' : state
= MOUSE_EVENT
; break ;
573 default : return CTRSEQ_INVALID
;
578 buf
[ 1 ] = getchar () - offset
;
579 buf
[ 2 ] = getchar () - offset
;
582 return CTRSEQ_INVALID
;
588 int getch ( unsigned char * buf
) {
589 /* returns a character, EOF, or constant for an escape/control sequence - NOT
590 compatible with the ncurses implementation of same name */
591 int action
= getctrlseq ( buf
);
595 l
= screen2field_l ( buf
[ 2 ]);
596 c
= screen2field_c ( buf
[ 1 ]);
598 if ( buf
[ 0 ] > 3 ) break ; /* ignore all but left/middle/right/up */
599 int success
= wait_mouse_up ( l
, c
);
601 /* mouse moved while pressed: */
602 if (! success
) return CTRSEQ_INVALID
;
605 case 0 : return CTRSEQ_MOUSE_LEFT
;
606 case 1 : return CTRSEQ_MOUSE_MIDDLE
;
607 case 2 : return CTRSEQ_MOUSE_RIGHT
;
614 int getch_wrapper ( void ) {
615 unsigned char mouse
[ 3 ];
616 int c
= getch ( mouse
);
618 if ( c
== CTRSEQ_MOUSE_LEFT
|| c
== CTRSEQ_MOUSE_RIGHT
) {
619 if ( clicked_emoticon ( mouse
))
620 return WRAPPER_EMOTICON
;
622 if ( screen2field_c ( mouse
[ 1 ]) < 0
623 || screen2field_c ( mouse
[ 1 ]) >= f
. w
624 || screen2field_l ( mouse
[ 2 ]) < 0
625 || screen2field_l ( mouse
[ 2 ]) >= f
. h
)
626 return CTRSEQ_INVALID
;
628 g
. p
[ 0 ] = screen2field_l ( mouse
[ 2 ]);
629 g
. p
[ 1 ] = screen2field_c ( mouse
[ 1 ]);
631 return c
; /* CTRSEQ_MOUSE_LEFT || CTRSEQ_MOUSE_RIGHT */
637 int parse_fieldspec ( char * str
) {
638 /* parses the FIELDSPEC (WxHxM); returns 1 on error */
639 int n
= sscanf ( str
, "%dx%dx%d" , & f
. w
, & f
. h
, & f
. m
);
641 ioctl ( STDOUT_FILENO
, TIOCGWINSZ
, & w
);
643 /* clamp field size to terminal size and mouse maximum: */
644 if ( COL_OFFSET
+ f
. w
* CW
+ COL_OFFSET
> w
. ws_col
)
645 f
. w
= w
. ws_col
/ CW
- ( COL_OFFSET
+ COL_OFFSET
);
646 if ( LINE_OFFSET
+ f
. h
+ LINES_AFTER
> w
. ws_row
)
647 f
. h
= w
. ws_row
- ( LINE_OFFSET
+ LINES_AFTER
);
648 if ( COL_OFFSET
+ f
. w
* CW
> MOUSE_MAX
)
649 f
. w
= MOUSE_MAX
/ CW
- COL_OFFSET
;
650 if ( LINE_OFFSET
+ f
. h
> MOUSE_MAX
)
651 f
. h
= MOUSE_MAX
- LINE_OFFSET
;
654 return 1 ; /* error */
656 if ( f
. w
< 30 ) f
. m
= f
. w
* f
. h
* .15625 ; //TODO: used twice; make function
657 else f
. m
= f
. w
* f
. h
* .20625 ;
663 void timer_setup ( int enable
) {
664 static struct itimerval tbuf
;
665 tbuf
. it_interval
. tv_sec
= 1 ;
666 tbuf
. it_interval
. tv_usec
= 0 ;
670 tbuf
. it_value
. tv_sec
= 1 ;
671 tbuf
. it_value
. tv_usec
= 0 ;
672 if ( setitimer ( ITIMER_REAL
, & tbuf
, NULL
) == - 1 ) {
677 tbuf
. it_value
. tv_sec
= 0 ;
678 tbuf
. it_value
. tv_usec
= 0 ;
679 if ( setitimer ( ITIMER_REAL
, & tbuf
, NULL
) == - 1 ) {
687 void signal_setup ( void ) {
688 struct sigaction saction
;
690 saction
. sa_handler
= signal_handler
;
691 sigemptyset (& saction
. sa_mask
);
692 saction
. sa_flags
= 0 ;
693 if ( sigaction ( SIGALRM
, & saction
, NULL
) < 0 ) {
698 if ( sigaction ( SIGINT
, & saction
, NULL
) < 0 ) {
704 void signal_handler ( int signum
) {
708 dtime
= difftime ( time ( NULL
), g
. t
);
709 move_ph ( 1 , f
. w
* CW
-( CW
% 2 )- 3 -( dtime
> 999 ));
710 printf ( "[%03d]" , g
. t
? dtime
: 0 );
717 void screen_setup ( int enable
) {
720 printf ( " \033 [s \033 [?47h" ); /* save cursor, alternate screen */
721 printf ( " \033 [H \033 [J" ); /* reset cursor, clear screen */
722 printf ( " \033 [?1000h \033 [?25l" ); /* enable mouse, hide cursor */
723 print ( op
. scheme
-> init_seq
); /* swich charset, if necessary */
725 print ( op
. scheme
-> reset_seq
); /* reset charset, if necessary */
726 printf ( " \033 [?9l \033 [?25h" ); /* disable mouse, show cursor */
727 printf ( " \033 [?47l \033 [u" ); /* primary screen, restore cursor */
732 /* http://users.csc.calpoly.edu/~phatalsk/357/lectures/code/sigalrm.c */
733 void raw_mode ( int enable
) {
734 static struct termios saved_term_mode
;
735 struct termios raw_term_mode
;
738 tcgetattr ( STDIN_FILENO
, & saved_term_mode
);
739 raw_term_mode
= saved_term_mode
;
740 raw_term_mode
. c_lflag
&= ~( ICANON
| ECHO
);
741 raw_term_mode
. c_cc
[ VMIN
] = 1 ;
742 raw_term_mode
. c_cc
[ VTIME
] = 0 ;
743 tcsetattr ( STDIN_FILENO
, TCSAFLUSH
, & raw_term_mode
);
745 tcsetattr ( STDIN_FILENO
, TCSAFLUSH
, & saved_term_mode
);