]> git.gir.st - solVItaire.git/blob - sol.c
allow joining to foundation
[solVItaire.git] / sol.c
1 #define _DEFAULT_SOURCE /* for getopt, sigaction, usleep */
2 #include <poll.h>
3 #include <signal.h>
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <sys/ioctl.h>
7 #include <time.h>
8 #include <termios.h>
9 #include <unistd.h>
10
11 #include "sol.h"
12 #include "schemes.h"
13
14 struct playfield f;
15 struct opts op;
16
17 // action table {{{
18 /* stores a function pointer for every takeable action; called by game loop */
19 int (*action[NUM_PLACES][10])(int,int,int) = {
20 #ifdef KLONDIKE
21 /* 1 2 3 4 5 6 7 stk wst fnd*/
22 /* 1 */ { t2f, t2t, t2t, t2t, t2t, t2t, t2t, nop, nop, t2f },
23 /* 2 */ { t2t, t2f, t2t, t2t, t2t, t2t, t2t, nop, nop, t2f },
24 /* 3 */ { t2t, t2t, t2f, t2t, t2t, t2t, t2t, nop, nop, t2f },
25 /* 4 */ { t2t, t2t, t2t, t2f, t2t, t2t, t2t, nop, nop, t2f },
26 /* 5 */ { t2t, t2t, t2t, t2t, t2f, t2t, t2t, nop, nop, t2f },
27 /* 6 */ { t2t, t2t, t2t, t2t, t2t, t2f, t2t, nop, nop, t2f },
28 /* 7 */ { t2t, t2t, t2t, t2t, t2t, t2t, t2f, nop, nop, t2f },
29 /*stk*/ { nop, nop, nop, nop, nop, nop, nop, nop, s2w, nop },
30 /*wst*/ { w2t, w2t, w2t, w2t, w2t, w2t, w2t, w2s, w2f, w2f },
31 /*fnd*/ { f2t, f2t, f2t, f2t, f2t, f2t, f2t, nop, nop, nop },
32 #elif defined SPIDER
33 /* 1 2 3 4 5 6 7 8 9 10*/
34 /* 1 */ { t2f, t2t, t2t, t2t, t2t, t2t, t2t, t2t, t2t, t2t },
35 /* 2 */ { t2t, t2f, t2t, t2t, t2t, t2t, t2t, t2t, t2t, t2t },
36 /* 3 */ { t2t, t2t, t2f, t2t, t2t, t2t, t2t, t2t, t2t, t2t },
37 /* 4 */ { t2t, t2t, t2t, t2f, t2t, t2t, t2t, t2t, t2t, t2t },
38 /* 5 */ { t2t, t2t, t2t, t2t, t2f, t2t, t2t, t2t, t2t, t2t },
39 /* 6 */ { t2t, t2t, t2t, t2t, t2t, t2f, t2t, t2t, t2t, t2t },
40 /* 7 */ { t2t, t2t, t2t, t2t, t2t, t2t, t2f, t2t, t2t, t2t },
41 /* 8 */ { t2t, t2t, t2t, t2t, t2t, t2t, t2t, t2f, t2t, t2t },
42 /* 9 */ { t2t, t2t, t2t, t2t, t2t, t2t, t2t, t2t, t2f, t2t },
43 /*10 */ { t2t, t2t, t2t, t2t, t2t, t2t, t2t, t2t, t2t, t2f },
44 /*stk*/ { s2t, s2t, s2t, s2t, s2t, s2t, s2t, s2t, s2t, s2t },
45 #endif
46 };
47 // }}}
48
49 // argv parsing, game loops, cleanup {{{
50 int main(int argc, char** argv) {
51 /* opinionated defaults: */
52 op.s = &unicode_large_color;
53 #ifdef SPIDER
54 op.m = MEDIUM;
55 #endif
56
57 int optget;
58 opterr = 0; /* don't print message on unrecognized option */
59 while ((optget = getopt (argc, argv, "+:hs:vbcm")) != -1) {
60 switch (optget) {
61 #ifdef SPIDER
62 case 's': /* number of suits */
63 switch (optarg[0]) {
64 case '1': op.m = EASY; break;
65 case '2': op.m = MEDIUM; break;
66 case '4': op.m = NORMAL; break;
67 default: goto error;
68 } break;
69 #endif
70 case 'b': op.s = &unicode_large_mono; break;
71 case 'c': op.s = &unicode_large_color; break;
72 case 'm': op.s = &unicode_small_mono; break; /* "mini" */
73 case 'h': default: goto error;
74 error:
75 fprintf (stderr, SHORTHELP LONGHELP KEYHELP, argv[0]);
76 return optget != 'h';
77 }
78 }
79
80 signal_setup();
81 atexit (*quit);
82
83 signal_handler(SIGWINCH); /* initialize window size */
84
85 newgame:
86 screen_setup(1);
87
88 switch(sol()) {
89 case GAME_NEW: goto newgame;
90 case GAME_WON:
91 print_table(NO_HI, NO_HI);
92 win_anim();
93 if (getch(NULL)=='q') return 0;
94 goto newgame;
95 case GAME_QUIT: return 0;
96 }
97 }
98
99 int sol(void) {
100 int ret;
101 long seed = time(NULL);
102 restart:
103 free_undo(f.u);
104 deal(seed);
105
106 int from, to, opt;
107 for(;;) {
108 switch (get_cmd(&from, &to, &opt)) {
109 case CMD_MOVE:
110 ret = action[from][to](from,to,opt);
111 if (ret == ERR) /* try again with from/to swapped: */
112 ret = action[to][from](to,from,opt);
113 switch (ret) {
114 case OK: break;
115 case ERR: visbell(); break;
116 case WON: return GAME_WON;
117 }
118 break;
119 case CMD_JOIN:
120 switch (join(to)) {
121 case OK: break;
122 case ERR: visbell(); break;
123 case WON: return GAME_WON;
124 }
125 break;
126 case CMD_HINT: break;//TODO: show a possible (and sensible) move. if possible, involve active cursor
127 case CMD_UNDO: undo_pop(f.u); break;
128 case CMD_INVAL: visbell(); break;
129 case CMD_NEW: return GAME_NEW;
130 case CMD_AGAIN: goto restart;
131 case CMD_QUIT: return GAME_QUIT;
132 }
133 }
134 }
135
136 void quit(void) {
137 screen_setup(0);
138 free_undo(f.u);
139 }
140 //}}}
141
142 // card games helper functions {{{
143 #define get_suit(card) \
144 ((card-1) % NUM_SUITS)
145 #define get_rank(card) \
146 ((card-1) / NUM_SUITS)
147 #define get_color(card) \
148 ((get_suit(card) ^ get_suit(card)>>1) & 1)
149
150 #define is_tableu(where) (where <= TAB_MAX)
151
152 int find_top(card_t* pile) {
153 int i;
154 for(i=PILE_SIZE-1; i>=0 && !pile[i]; i--);
155 return i;
156 }
157 int first_movable(card_t* pile) {
158 int i = 0;
159 for (;pile[i] && !is_movable(pile, i); i++);
160 return i;
161 }
162 int turn_over(card_t* pile) {
163 int top = find_top(pile);
164 if (pile[top] < 0) {
165 pile[top] *= -1;
166 return 1;
167 } else return 0;
168 }
169 int check_won(void) {
170 for (int pile = 0; pile < NUM_DECKS*NUM_SUITS; pile++)
171 if (f.f[pile][NUM_RANKS-1] == NO_CARD) return 0;
172
173 return 1;
174 }
175 int rank_next (card_t a, card_t b) {
176 return get_rank(a) == get_rank(b)-1;
177 }
178 int is_consecutive (card_t* pile, int pos) {
179 if (pos+1 >= PILE_SIZE) return 1; /* card is last */
180 if (pile[pos+1] == NO_CARD) return 1; /* card is first */
181
182 #ifdef KLONDIKE
183 /* ranks consecutive? */
184 if (!rank_next(pile[pos+1], pile[pos])) return 0;
185 /* color opposite? */
186 if (get_color(pile[pos+1]) == get_color(pile[pos])) return 0;
187 #elif defined SPIDER
188 /* ranks consecutive? */
189 if (!rank_next(pile[pos+1], pile[pos])) return 0;
190 /* same suit? */
191 if (get_suit(pile[pos+1]) != get_suit(pile[pos])) return 0;
192 #endif
193
194 return 1;
195 }
196
197 int is_movable(card_t* pile, int n) {
198 #ifdef KLONDIKE
199 return(pile[n] > NO_CARD); /*non-movable cards don't exist in klondike*/
200 #elif defined SPIDER
201 int top = find_top(pile);
202 for (int i = top; i >= 0; i--) {
203 if (pile[i] <= NO_CARD) return 0; /*no card or card face down?*/
204 if (!is_consecutive(pile, i)) return 0;
205 if (i == n) return 1; /* card reached, must be movable */
206 }
207 return 0;
208 #endif
209 }
210 //}}}
211
212 // takeable actions {{{
213 #ifdef KLONDIKE
214 card_t stack_take(void) { /*NOTE: assert(f.w >= 0) */
215 card_t card = f.s[f.w];
216 /* move stack one over, so there are no gaps in it: */
217 for (int i = f.w; i < f.z-1; i++)
218 f.s[i] = f.s[i+1];
219 f.z--;
220 f.w--; /* make previous card visible again */
221 return card;
222 }
223 int t2f(int from, int to, int opt) { /* tableu to foundation */
224 (void) to; (void) opt; /* don't need */
225 int top_from = find_top(f.t[from]);
226 to = get_suit(f.t[from][top_from]);
227 int top_to = find_top(f.f[to]);
228 if ((top_to < 0 && get_rank(f.t[from][top_from]) == RANK_A)
229 || (top_to >= 0 && rank_next(f.f[to][top_to],f.t[from][top_from]))) {
230 f.f[to][top_to+1] = f.t[from][top_from];
231 f.t[from][top_from] = NO_CARD;
232 undo_push(from, FOUNDATION, to,
233 turn_over(f.t[from]));
234 if (check_won()) return WON;
235 return OK;
236 } else return ERR;
237 }
238 int w2f(int from, int to, int opt) { /* waste to foundation */
239 (void) from; (void) to; (void) opt; /* don't need */
240 if (f.w < 0) return ERR;
241 to = get_suit(f.s[f.w]);
242 int top_to = find_top(f.f[to]);
243 if ((top_to < 0 && get_rank(f.s[f.w]) == RANK_A)
244 || (top_to >= 0 && rank_next(f.f[to][top_to], f.s[f.w]))) {
245 undo_push(WASTE, FOUNDATION, f.w | to<<16, 0);//ugly encoding :|
246 f.f[to][top_to+1] = stack_take();
247 if (check_won()) return WON;
248 return OK;
249 } else return ERR;
250
251 }
252 int s2w(int from, int to, int opt) { /* stock to waste */
253 (void) from; (void) to; (void) opt; /* don't need */
254 if (f.z == 0) return ERR;
255 f.w++;
256 if (f.w == f.z) f.w = -1;
257 return OK;
258 }
259 int w2s(int from, int to, int opt) { /* waste to stock (undo stock to waste) */
260 (void) from; (void) to; (void) opt; /* don't need */
261 if (f.z == 0) return ERR;
262 f.w--;
263 if (f.w < -1) f.w = f.z-1;
264 return OK;
265 }
266 int f2t(int from, int to, int opt) { /* foundation to tableu */
267 (void) from; /* don't need */
268 int top_to = find_top(f.t[to]);
269 from = opt;
270 int top_from = find_top(f.f[from]);
271
272 if ((get_color(f.t[to][top_to]) != get_color(f.f[from][top_from]))
273 && (rank_next(f.f[from][top_from], f.t[to][top_to]))) {
274 f.t[to][top_to+1] = f.f[from][top_from];
275 f.f[from][top_from] = NO_CARD;
276 undo_push(FOUNDATION, to, from, 0);
277 return OK;
278 } else return ERR;
279 }
280 int w2t(int from, int to, int opt) { /* waste to tableu */
281 (void) from; (void) opt; /* don't need */
282 int top_to = find_top(f.t[to]);
283 if (((get_color(f.t[to][top_to]) != get_color(f.s[f.w]))
284 && (rank_next(f.s[f.w], f.t[to][top_to])))
285 || (top_to < 0 && get_rank(f.s[f.w]) == RANK_K)) {
286 undo_push(WASTE, to, f.w, 0);
287 f.t[to][top_to+1] = stack_take();
288 return OK;
289 } else return ERR;
290 }
291 int t2t(int from, int to, int opt) { /* tableu to tableu */
292 (void) opt; /* don't need */
293 int top_to = find_top(f.t[to]);
294 int top_from = find_top(f.t[from]);
295 int count = 0; //NOTE: could probably be factored out
296 for (int i = top_from; i >=0; i--) {
297 if (((get_color(f.t[to][top_to]) != get_color(f.t[from][i]))
298 && (rank_next(f.t[from][i], f.t[to][top_to]))
299 && f.t[from][i] > NO_CARD) /* card face up? */
300 || (top_to < 0 && get_rank(f.t[from][i]) == RANK_K)) {
301 /* move cards [i..top_from] to their destination */
302 for (;i <= top_from; i++) {
303 top_to++;
304 f.t[to][top_to] = f.t[from][i];
305 f.t[from][i] = NO_CARD;
306 count++;
307 }
308 undo_push(from, to, count,
309 turn_over(f.t[from]));
310 return OK;
311 }
312 }
313 return ERR; /* no such move possible */
314 }
315 #elif defined SPIDER
316 int remove_if_complete (int pileno) { //cleanup!
317 card_t* pile = f.t[pileno];
318 /* test if K...A complete; move to foundation if so */
319 int top_from = find_top(pile);
320 if (get_rank(pile[top_from]) != RANK_A) return 0;
321 for (int i = top_from; i>=0; i--) {
322 if (!is_consecutive (pile, i)) return 0;
323 if (i+RANK_K == top_from /* if ace to king: remove it */
324 && get_rank(pile[top_from-RANK_K]) == RANK_K) {
325 for(int i=top_from, j=0; i>top_from-NUM_RANKS; i--,j++){
326 f.f[f.w][j] = pile[i];
327 pile[i] = NO_CARD;
328 }
329 undo_push(pileno, FOUNDATION, f.w,
330 turn_over(pile));
331 f.w++;
332 return 1;
333 }
334 }
335
336 return 0;
337 }
338 int t2t(int from, int to, int opt) { //in dire need of cleanup
339 int top_from = find_top(f.t[from]);
340 int top_to = find_top(f.t[to]);
341 int empty_to = (top_to < 0)? opt: -1; /* empty pile? */
342 int count = 0; //NOTE: could probably be factored out
343
344 for (int i = top_from; i >= 0; i--) {
345 if (!is_consecutive(f.t[from], i)) break;
346
347 /* is consecutive OR to empty pile and rank ok? */
348 if (rank_next(f.t[from][i], f.t[to][top_to])
349 || (empty_to >= RANK_A && get_rank(f.t[from][i]) == empty_to)) {
350 for (;i <= top_from; i++) {
351 top_to++;
352 f.t[to][top_to] = f.t[from][i];
353 f.t[from][i] = NO_CARD;
354 count++;
355 }
356 undo_push(from, to, count,
357 turn_over(f.t[from]));
358 remove_if_complete(to);
359 if (check_won()) return WON;
360 return OK;
361 }
362 }
363
364 return ERR; /* no such move possible */
365 }
366 int s2t(int from, int to, int opt) {
367 (void) from; (void) to; (void) opt; /* don't need */
368 if (f.z <= 0) return ERR; /* stack out of cards */
369 for (int pile = 0; pile < NUM_PILES; pile++)
370 if (f.t[pile][0]==NO_CARD) return ERR; /*no piles may be empty*/
371 for (int pile = 0; pile < NUM_PILES; pile++) {
372 f.t[pile][find_top(f.t[pile])+1] = f.s[--f.z];
373 remove_if_complete(pile);
374 if (check_won()) return WON;
375 }
376 undo_push(STOCK, TABLEU, 1, 0); /*NOTE: puts 1 card on each tableu pile*/
377 return OK;
378 }
379 int t2f(int from, int to, int opt) {
380 (void) to; (void) opt; /* don't need */
381 /* manually retrigger remove_if_complete() (e.g. after undo_pop) */
382 return remove_if_complete(from)?OK:ERR;
383 }
384 #endif
385 int join(int to) {
386 #ifdef KLONDIKE
387 if (to == FOUNDATION) {
388 int status = ERR;
389 for (int i = 0; i <= TAB_MAX; i++)
390 switch ((i?t2f:w2f)(i-1, FOUNDATION, 0)) {
391 case WON: return WON;
392 case OK: status = OK;
393 case ERR: /* nop */;
394 }
395 return status;
396 }
397 #endif
398 //TODO: which pile to take from should form the basis of CMD_HINT
399
400 int top_to = find_top(f.t[to]); //TODO: handle empty: join would-empty, would-turn, longest stack (currently just ERRors)
401 int from = -1;
402
403 struct rating {
404 int ok:1; /* card to move in pile? */
405 int above; /* number of movable cards above */
406 int below; /* number of cards below ours */
407 int pos; /* where the card to move is in the pile */
408 } r[NUM_PILES] = {{0}};
409
410 /* 1. rate each pile: */
411 for (int pile = 0; pile < NUM_PILES; pile++) {
412 r[pile].pos = find_top(f.t[pile]);
413 /* backtrack until we find a compatible-to-'to'-pile card: */
414 while (r[pile].pos >= 0 && is_movable(f.t[pile], r[pile].pos)) {
415 int rankdiff = get_rank(f.t[pile][r[pile].pos])
416 - get_rank(f.t[to][top_to]);
417 if (rankdiff >= 0) break; /* past our card */
418 if (rankdiff == -1 /* rank matches */
419 #ifdef KLONDIKE
420 && get_color(f.t[pile][r[pile].pos]) /* color OK */
421 != get_color(f.t[to][top_to])
422 #elif defined SPIDER
423 && get_suit(f.t[pile][r[pile].pos]) /* color OK */
424 == get_suit(f.t[to][top_to])
425 #endif
426 ) {
427 r[pile].ok++;
428 for (int i = r[pile].pos; i >= 0; i--)
429 if (is_movable(f.t[pile], i-1))
430 r[pile].above++;
431 else break;
432 break;
433 }
434 r[pile].pos--;
435 r[pile].below++;
436 }
437 }
438
439 /* 2. find optimal pile: (optimized for spider) */
440 //TODO: maybe add 'would finish' (spider)?
441 for (int pile = 0, above = 99, turn = 0, empty = 0, below = 99, e=0,t=0;
442 pile < NUM_PILES; pile++) {
443 if (!r[pile].ok) continue;
444
445 if ((e=(r[pile].pos == 0)) /* will become empty */
446 || ((t=(f.t[pile][r[pile].pos-1] < 0)) && !empty) /* will turn_over */
447 || (r[pile].above < above && !empty) /* less cards above */
448 || (r[pile].above ==above && r[pile].below < below && !empty && !turn)) { /* if tied, use shorter pile */
449 from = pile;
450 above = r[pile].above;
451 below = r[pile].below;
452 empty |= e;
453 turn |= t;
454 }
455 }
456
457 /* 3a. give up if nothing found: */
458 if (from < 0) {
459 #ifdef KLONDIKE /* check if we can take from waste before giving up */
460 return w2t(WASTE, to, 0);
461 #elif defined SPIDER
462 return ERR;
463 #endif
464 }
465
466 /* 3b. move cards over and return: */
467 #ifdef KLONDIKE
468 return t2t(from, to, 0);
469 #elif defined SPIDER
470 int bottom = first_movable(f.t[from]);
471 return t2t(from, to, get_rank(f.t[from][bottom]));
472 #endif
473 }
474 int nop(int from, int to, int opt) { (void)from;(void)to;(void)opt;return ERR; }
475 // }}}
476
477 // keyboard input handling {{{
478 // cursor functions{{{
479 #ifdef KLONDIKE
480 void cursor_left (struct cursor* cursor) {
481 if (is_tableu(cursor->pile)) {
482 if (cursor->pile > 0) cursor->pile--;
483 cursor->opt = 0;
484 } else { /* stock/waste/foundation*/
485 switch (cursor->pile) {
486 case WASTE: cursor->pile = STOCK; cursor->opt = 0; break;
487 case FOUNDATION:
488 if (cursor->opt <= 0)
489 cursor->pile = WASTE;
490 else
491 cursor->opt--;
492 }
493 }
494 }
495 void cursor_down (struct cursor* cursor) {
496 if (!is_tableu(cursor->pile)) {
497 switch (cursor->pile) {
498 case STOCK: cursor->pile = TAB_1; break;
499 case WASTE: cursor->pile = TAB_2; break;
500 case FOUNDATION:
501 cursor->pile = TAB_4 + cursor->opt;
502 }
503 cursor->opt = 0;
504 }
505 }
506 void cursor_up (struct cursor* cursor) {
507 if (is_tableu(cursor->pile)) {
508 switch (cursor->pile) { //ugly :|
509 case TAB_1: cursor->pile = STOCK; break;
510 case TAB_2: cursor->pile = WASTE; break;
511 case TAB_3: cursor->pile = WASTE; break;
512 case TAB_4: case TAB_5: case TAB_6: case TAB_7:
513 cursor->opt=cursor->pile-TAB_4;
514 cursor->pile = FOUNDATION;
515 break;
516 }
517 }
518 }
519 void cursor_right (struct cursor* cursor) {
520 if (is_tableu(cursor->pile)) {
521 if (cursor->pile < TAB_MAX) cursor->pile++;
522 } else {
523 switch (cursor->pile) {
524 case STOCK: cursor->pile = WASTE; break;
525 case WASTE: cursor->pile = FOUNDATION;cursor->opt = 0; break;
526 case FOUNDATION:
527 if (cursor->opt < NUM_SUITS-1)
528 cursor->opt++;
529 }
530 }
531 }
532 #elif defined SPIDER
533 /*NOTE: one can't highlight the stock due to me being too lazy to implement it*/
534 void cursor_left (struct cursor* cursor) {
535 if (cursor->pile > 0) cursor->pile--;
536 cursor->opt = 0;
537 }
538 void cursor_down (struct cursor* cursor) {
539 int first = first_movable(f.t[cursor->pile]);
540 int top = find_top(f.t[cursor->pile]);
541 if (first + cursor->opt < top)
542 cursor->opt++;
543 }
544 void cursor_up (struct cursor* cursor) {
545 if (cursor->opt > 0) cursor->opt--;
546 }
547 void cursor_right (struct cursor* cursor) {
548 if (cursor->pile < TAB_MAX) cursor->pile++;
549 cursor->opt = 0;
550 }
551 #endif
552 void cursor_to (struct cursor* cursor, int pile) {
553 cursor->pile = pile;
554 cursor->opt = 0;
555 }
556 //}}}
557 int get_cmd (int* from, int* to, int* opt) {
558 /*XXX*/unsigned char mouse[3];
559 //TODO: escape sequences (mouse, cursor keys)
560 int _f, t;
561 struct cursor inactive = {-1,-1};
562 static struct cursor active = {0,0};
563 active.opt = 0; /* always reset offset, but keep pile */
564
565 /***/
566 from_l: print_table(&active, &inactive);
567 _f = getch(mouse);
568
569 switch (_f) {
570 /* direct addressing: */
571 case '1': *from = TAB_1; break;
572 case '2': *from = TAB_2; break;
573 case '3': *from = TAB_3; break;
574 case '4': *from = TAB_4; break;
575 case '5': *from = TAB_5; break;
576 case '6': *from = TAB_6; break;
577 case '7': *from = TAB_7; break;
578 #ifdef SPIDER
579 case '8': *from = TAB_8; break;
580 case '9': *from = TAB_9; break;
581 case '0': *from = TAB_10;break;
582 #elif defined KLONDIKE
583 case '9': *from = WASTE; break;
584 case '0': *from = FOUNDATION; break;
585 case '8': /* fallthrough */
586 #endif
587 case '\n': /* shortcut for dealing from stock */
588 *from = STOCK;
589 *to = WASTE;
590 return CMD_MOVE;
591 /* cursor keys addressing: */
592 case KEY_LEFT:
593 case 'h': cursor_left (&active); goto from_l;
594 case KEY_DOWN:
595 case 'j': cursor_down (&active); goto from_l;
596 case KEY_UP:
597 case 'k': cursor_up (&active); goto from_l;
598 case KEY_RIGHT:
599 case 'l': cursor_right(&active); goto from_l;
600 case KEY_HOME:
601 case 'H': cursor_to(&active,TAB_1); goto from_l; /* leftmost tableu */
602 case KEY_END:
603 case 'L': cursor_to(&active,TAB_MAX);goto from_l; /* rigthmost tableu */
604 case KEY_INS:
605 case 'M': cursor_to(&active,TAB_MAX/2); goto from_l; /* center tableu */
606 //TODO: real cursor keys, home/end
607 case ' ': /* continue with second cursor */
608 *from = active.pile;
609 if (*from == STOCK) {
610 *to = WASTE;
611 return CMD_MOVE;
612 }
613 #ifdef KLONDIKE
614 *opt = active.opt; /* when FOUNDATION */
615 #endif
616 inactive = active;
617 break;
618 /* misc keys: */
619 case ':':
620 {char buf[256];
621 fprintf (stderr, ":");
622 raw_mode(0); /* turn on echo */
623 fgets (buf, 256, stdin);
624 raw_mode(1);
625 switch(buf[0]) {
626 case 'q': return CMD_QUIT;
627 case 'n': return CMD_NEW;
628 case 'r': return CMD_AGAIN;
629 default: return CMD_INVAL;
630 }}
631 case 'J':
632 *to = active.pile;
633 #ifdef KLONDIKE
634 if (*to == FOUNDATION) return CMD_JOIN;
635 #endif
636 if (*to > TAB_MAX) return CMD_INVAL;
637 return CMD_JOIN;
638 case 'K': /* fallthrough */
639 case '?': return CMD_HINT;
640 case 'u': return CMD_UNDO;
641 case EOF: return CMD_NONE; /* sent by SIGCONT */
642 default: return CMD_INVAL;
643 }
644 inactive.pile = *from; /* for direct addressing highlighting */
645 if (is_tableu(*from) && f.t[*from][0] == NO_CARD) return CMD_INVAL;
646
647 /***/
648 to_l: print_table(&active, &inactive);
649 t = getch(mouse);
650
651 switch (t) {
652 case KEY_LEFT:
653 case 'h': cursor_left (&active); goto to_l;
654 case KEY_DOWN:
655 case 'j': cursor_down (&active); goto to_l;
656 case KEY_UP:
657 case 'k': cursor_up (&active); goto to_l;
658 case KEY_RIGHT:
659 case 'l': cursor_right(&active); goto to_l;
660 case KEY_HOME:
661 case 'H': cursor_to(&active,TAB_1); goto to_l;
662 case KEY_END:
663 case 'L': cursor_to(&active,TAB_MAX); goto to_l;
664 case KEY_INS:
665 case 'M': cursor_to(&active,TAB_MAX/2); goto to_l;
666 case 'J': /* fallthrough; just join selected pile */
667 case ' ':
668 *to = active.pile;
669 break; /* continues with the foundation/empty tableu check */
670 case 'K': /* fallthrough */
671 case '?': return CMD_HINT;
672 case 'u': return CMD_NONE; /* cancel selection */
673 case EOF: return CMD_NONE; /* sent by SIGCONT */
674 default:
675 if (t < '0' || t > '9') return CMD_INVAL;
676 if (t == '0')
677 #ifdef KLONDIKE
678 *to = FOUNDATION;
679 #elif defined SPIDER
680 *to = TAB_10;
681 #endif
682 else
683 *to = t-'1';
684 }
685
686 /***/
687 #ifdef KLONDIKE
688 if (*from == FOUNDATION) {
689 int top = find_top(f.t[*to]);
690 if (top < 0) return CMD_INVAL;
691 int color = get_color(f.t[*to][top]);
692 int choice_1 = 1-color; /* selects piles of */
693 int choice_2 = 2+color; /* the opposite color */
694 int top_c1 = find_top(f.f[choice_1]);
695 int top_c2 = find_top(f.f[choice_2]);
696
697 switch ((rank_next(f.f[choice_1][top_c1], f.t[*to][top])
698 && top_c1 >= 0 ) << 0
699 |(rank_next(f.f[choice_2][top_c2], f.t[*to][top])
700 && top_c2 >= 0 ) << 1) {
701 case ( 1<<0): *opt = choice_1; break; /* choice_1 only */
702 case (1<<1 ): *opt = choice_2; break; /* choice_2 only */
703 case (1<<1 | 1<<0): /* both, ask user which to pick from */
704 printf ("take from (1-4): "); fflush (stdout);
705 *opt = getch(NULL) - '1';
706 if (*opt < 0 || *opt > 3) return CMD_INVAL;
707 break;
708 default: return CMD_INVAL; /* none matched */
709 }
710 /* `opt` is the foundation index (0..3) */
711 }
712 #elif defined SPIDER
713 /* moving to empty tableu? */
714 if (is_tableu(*to) && f.t[*to][0] == NO_CARD) {
715 int bottom = first_movable(f.t[*from]);
716 if (inactive.opt >= 0) { /*if from was cursor addressed: */
717 *opt = get_rank(f.t[*from][bottom + inactive.opt]);
718 return CMD_MOVE;
719 }
720 int top = find_top(f.t[*from]);
721 if (top < 0) return CMD_INVAL;
722 if (top >= 0 && !is_movable(f.t[*from], top-1)) {
723 *opt = get_rank(f.t[*from][top]);
724 } else { /* only ask the user if it's unclear: */
725 printf ("\rup to ([a23456789xjqk] or space/return): ");
726 *opt = getch(NULL);
727 switch (*opt) {
728 case ' ': *opt = get_rank(f.t[*from][top]); break;
729 case'\n': *opt = get_rank(f.t[*from][bottom]); break;
730 case 'a': case 'A': *opt = RANK_A; break;
731 case '0': /* fallthrough */
732 case 'x': case 'X': *opt = RANK_X; break;
733 case 'j': case 'J': *opt = RANK_J; break;
734 case 'q': case 'Q': *opt = RANK_Q; break;
735 case 'k': case 'K': *opt = RANK_K; break;
736 default: *opt -= '1';
737 }
738 if (*opt < RANK_A || *opt > RANK_K) return ERR;
739 }
740 /* `opt` is the rank of the highest card to move */
741 }
742 #endif
743 return CMD_MOVE;
744 }
745
746 int getctrlseq(unsigned char* buf) {
747 int c;
748 enum esc_states {
749 START,
750 ESC_SENT,
751 CSI_SENT,
752 MOUSE_EVENT,
753 } state = START;
754 int offset = 0x20; /* never sends control chars as data */
755 while ((c = getchar()) != EOF) {
756 switch (state) {
757 case START:
758 switch (c) {
759 case '\033': state=ESC_SENT; break;
760 default: return c;
761 }
762 break;
763 case ESC_SENT:
764 switch (c) {
765 case '[': state=CSI_SENT; break;
766 default: return KEY_INVAL;
767 }
768 break;
769 case CSI_SENT:
770 switch (c) {
771 case 'A': return KEY_UP;
772 case 'B': return KEY_DOWN;
773 case 'C': return KEY_RIGHT;
774 case 'D': return KEY_LEFT;
775 /*NOTE: home/end send ^[[x~ . no support for modifiers*/
776 case 'H': return KEY_HOME;
777 case 'F': return KEY_END;
778 case '2': getchar(); return KEY_INS;
779 case '5': getchar(); return KEY_PGUP;
780 case '6': getchar(); return KEY_PGDN;
781 case 'M': state=MOUSE_EVENT; break;
782 default: return KEY_INVAL;
783 }
784 break;
785 case MOUSE_EVENT:
786 if (buf == NULL) return KEY_INVAL;
787 buf[0] = c - offset;
788 buf[1] = getchar() - offset;
789 buf[2] = getchar() - offset;
790 return MOUSE_ANY;
791 default:
792 return KEY_INVAL;
793 }
794 }
795 return 2;
796 }
797 int wait_mouse_up(int l, int c) {
798 unsigned char mouse2[3];
799 int level = 1;
800 int l2, c2;
801
802 /* TODO: show a pushed-in button if cursor is on minefield */
803
804 while (level > 0) {
805 if (getctrlseq (mouse2) == MOUSE_ANY) {
806 /* ignore mouse wheel events: */
807 if (mouse2[0] & 0x40) continue;
808
809 else if((mouse2[0]&3) == 3) level--; /* release event */
810 else level++; /* another button pressed */
811 }
812 }
813
814 /* TODO: show normal button */
815
816 c2 = /*screen2field_c*/(mouse2[1]);
817 l2 = /*screen2field_l*/(mouse2[2]);
818 return ((l2 == l) && (c2 == c));
819 }
820
821 int getch(unsigned char* buf) {
822 /* returns a character, EOF, or constant for an escape/control sequence - NOT
823 compatible with the ncurses implementation of same name */
824 int action = getctrlseq(buf);
825 int l, c;
826 switch (action) {
827 case MOUSE_ANY:
828 l = /*screen2field_l*/ (buf[2]);
829 c = /*screen2field_c*/ (buf[1]);
830
831 if (buf[0] > 3) break; /* ignore all but left/middle/right/up */
832 int success = wait_mouse_up(l, c);
833
834 /* mouse moved while pressed: */
835 if (!success) return KEY_INVAL;
836
837 switch (buf[0]) {
838 case 0: return MOUSE_LEFT;
839 case 1: return MOUSE_MIDDLE;
840 case 2: return MOUSE_RIGHT;
841 }
842 }
843
844 return action;
845 }
846 // }}}
847
848 // shuffling and dealing {{{
849 void deal(long seed) {
850 f = (const struct playfield){0}; /* clear playfield */
851 card_t deck[DECK_SIZE*NUM_DECKS];
852 int avail = DECK_SIZE*NUM_DECKS;
853 for (int i = 0; i < DECK_SIZE*NUM_DECKS; i++) deck[i] = (i%DECK_SIZE)+1;
854 #ifdef SPIDER
855 if (op.m != NORMAL) for (int i = 0; i < DECK_SIZE*NUM_DECKS; i++) {
856 if (op.m == MEDIUM) deck[i] = 1+((deck[i]-1) | 2);
857 if (op.m == EASY) deck[i] = 1+((deck[i]-1) | 2 | 1);
858 /* the 1+ -1 dance gets rid of the offset created by NO_CARD */
859 }
860 #endif
861 srand (seed);
862 for (int i = DECK_SIZE*NUM_DECKS-1; i > 0; i--) { /* fisher-yates */
863 int j = rand() % (i+1);
864 if (j-i) deck[i]^=deck[j],deck[j]^=deck[i],deck[i]^=deck[j];
865 }
866
867 /* deal cards: */
868 for (int i = 0; i < NUM_PILES; i++) {
869 #ifdef KLONDIKE
870 int closed = i; /* pile n has n closed cards, then 1 open */
871 #elif defined SPIDER
872 int closed = i<4?5:4; /* pile 1-4 have 5, 5-10 have 4 closed */
873 #endif
874 /* face down cards are negated: */
875 for (int j = 0; j < closed; j++) f.t[i][j] = -deck[--avail];
876 f.t[i][closed] = deck[--avail]; /* the face-up card */
877 }
878 /* rest of the cards to the stock; NOTE: assert(avail==50) for spider */
879 for (f.z = 0; avail; f.z++) f.s[f.z] = deck[--avail];
880 #ifdef KLONDIKE
881 f.w = -1; /* @start: nothing on waste */
882 #elif defined SPIDER
883 f.w = 0; /* number of used foundations */
884 #endif
885
886 f.u = &undo_sentinel;
887 }
888 //}}}
889
890 // screen drawing routines {{{
891 void print_hi(int invert, int grey_bg, int bold, char* str) {
892 if (bold && op.s == &unicode_large_color){//ARGH! awful hack for bold with faint
893 int offset = str[3]==017?16:str[4]==017?17:0;
894 printf ("%s%s%s""%.*s%s%s""%s%s%s",
895 bold?"\033[1m":"", invert?"\033[7m":"", grey_bg?"\033[100m":"",
896 offset, str, bold?"\033[1m":"", str+offset,
897 grey_bg?"\033[49m":"", invert?"\033[27m":"",bold?"\033[22m":"");
898 return;
899 }
900 printf ("%s%s%s%s%s%s%s",
901 bold?"\033[1m":"", invert?"\033[7m":"", grey_bg?"\033[100m":"",
902 str,
903 grey_bg?"\033[49m":"", invert?"\033[27m":"",bold?"\033[22m":"");
904 }
905 void print_table(const struct cursor* active, const struct cursor* inactive) {
906 printf("\033[2J\033[H"); /* clear screen, reset cursor */
907 #ifdef KLONDIKE
908 /* print stock, waste and foundation: */
909 for (int line = 0; line < op.s->height; line++) {
910 /* stock: */
911 print_hi (active->pile == STOCK, inactive->pile == STOCK, 1, (
912 (f.w < f.z-1)?op.s->facedown
913 :op.s->placeholder)[line]);
914 /* waste: */
915 print_hi (active->pile == WASTE, inactive->pile == WASTE, 1, (
916 /* NOTE: cast, because f.w sometimes is (short)-1 !? */
917 ((short)f.w >= 0)?op.s->card[f.s[f.w]]
918 :op.s->placeholder)[line]);
919 printf ("%s", op.s->card[NO_CARD][line]); /* spacer */
920 /* foundation: */
921 for (int pile = 0; pile < NUM_SUITS; pile++) {
922 int card = find_top(f.f[pile]);
923 print_hi (active->pile==FOUNDATION && active->opt==pile,
924 inactive->pile==FOUNDATION && (
925 /* cursor addr. || direct addr. */
926 inactive->opt==pile || inactive->opt < 0
927 ), 1,
928 (card < 0)?op.s->placeholder[line]
929 :op.s->card[f.f[pile][card]][line]);
930 }
931 printf("\n");
932 }
933 printf("\n");
934 #elif defined SPIDER
935 int fdone; for (fdone = NUM_DECKS*NUM_SUITS; fdone; fdone--)
936 if (f.f[fdone-1][RANK_K]) break; /*number of completed stacks*/
937 int spacer_from = f.z?(f.z/10-1) * op.s->halfwidth[0] + op.s->width:0;
938 int spacer_to = NUM_PILES*op.s->width -
939 ((fdone?(fdone-1) * op.s->halfwidth[1]:0)+op.s->width);
940 for (int line = 0; line < op.s->height; line++) {
941 /* available stock: */
942 for (int i = f.z/10; i; i--) {
943 if (i==1) printf ("%s", op.s->facedown[line]);
944 else printf ("%s", op.s->halfstack[line]);
945 }
946 /* spacer: */
947 for (int i = spacer_from; i < spacer_to; i++) printf (" ");
948 /* foundation (overlapping): */
949 for (int i = 0; i < NUM_DECKS*NUM_SUITS; i++) { //TODO: print in revrse order (otherwise new piles get put 'below' older ones)
950 int overlap = i? op.s->halfcard[line]: 0;
951 if (f.f[i][RANK_K]) printf ("%.*s", op.s->halfwidth[2],
952 op.s->card[f.f[i][RANK_K]][line]+overlap);
953 }
954 printf("\n");
955 }
956 printf("\n");
957 #endif
958 #ifdef KLONDIKE
959 #define DO_HI(cursor) (cursor->pile == pile && (movable || empty))
960 #define TOP_HI(c) 1 /* can't select partial stacks in KLONDIKE */
961 #define INC_OFFSET
962 #elif defined SPIDER
963 int offset[NUM_PILES]={1,1,1,1,1,1,1,1,1,1}; // :|
964 #define DO_HI(cursor) (cursor->pile == pile && (movable || empty) \
965 && offset[pile] > cursor->opt)
966 #define TOP_HI(cursor) (cursor->pile == pile && movable \
967 && offset[pile]-1 == cursor->opt)
968 #define INC_OFFSET if (movable) offset[pile]++
969 #endif
970 /* print tableu piles: */
971 int row[NUM_PILES] = {0};
972 int line[NUM_PILES]= {0};
973 int label[NUM_PILES]={0};
974 int line_had_card;
975 int did_placeholders = 0;
976 do {
977 line_had_card = 0;
978 for (int pile = 0; pile < NUM_PILES; pile++) {
979 card_t card = f.t[pile][row[pile]];
980 card_t next = f.t[pile][row[pile]+1];
981 int movable = is_movable(f.t[pile], row[pile]);
982 int empty = !card && row[pile] == 0;
983
984 print_hi (DO_HI(active), DO_HI(inactive), movable, (
985 (!card && row[pile] == 0)?op.s->placeholder
986 :(card<0)?op.s->facedown
987 :op.s->card[card]
988 )[line[pile]]);
989
990 int extreme_overlap = ( 3 /* spacer, labels, status */
991 + 2 * op.s->height /* stock, top tableu card */
992 + find_top(f.t[pile]) * op.s->overlap) >op.w[0];
993 /* normal overlap: */
994 if (++line[pile] >= (next?op.s->overlap:op.s->height)
995 /* extreme overlap on closed cards: */
996 || (extreme_overlap &&
997 line[pile] >= 1 &&
998 f.t[pile][row[pile]] < 0 &&
999 f.t[pile][row[pile]+1] <0)
1000 /* extreme overlap on sequences: */
1001 || (extreme_overlap &&
1002 !TOP_HI(active) && /*always show top selected card*/
1003 line[pile] >= 1 && row[pile] > 0 &&
1004 f.t[pile][row[pile]-1] > NO_CARD &&
1005 is_consecutive (f.t[pile], row[pile]) &&
1006 is_consecutive (f.t[pile], row[pile]-1) &&
1007 f.t[pile][row[pile]+1] != NO_CARD)
1008 ) {
1009 line[pile]=0;
1010 row[pile]++;
1011 INC_OFFSET;
1012 }
1013 /* tableu labels: */
1014 if(!card && !label[pile] && row[pile]>0&&line[pile]>0) {
1015 label[pile] = 1;
1016 printf ("\b\b%d ", (pile+1) % 10); //XXX: hack
1017 }
1018 line_had_card |= !!card;
1019 did_placeholders |= row[pile] > 0;
1020 }
1021 printf ("\n");
1022 } while (line_had_card || !did_placeholders);
1023 }
1024
1025 void visbell (void) {
1026 printf ("\033[?5h"); fflush (stdout);
1027 usleep (100000);
1028 printf ("\033[?5l"); fflush (stdout);
1029 }
1030 void win_anim(void) {
1031 printf ("\033[?25l"); /* hide cursor */
1032 for (;;) {
1033 /* set cursor to random location */
1034 int row = 1+rand()%(24-op.s->width);
1035 int col = 1+rand()%(80-op.s->height);
1036
1037 /* draw random card */
1038 int face = 1 + rand() % 52;
1039 for (int l = 0; l < op.s->height; l++) {
1040 printf ("\033[%d;%dH", row+l, col);
1041 printf ("%s", op.s->card[face][l]);
1042 }
1043 fflush (stdout);
1044
1045 /* exit on keypress */
1046 struct pollfd p = {STDIN_FILENO, POLLIN, 0};
1047 if (poll (&p, 1, 80)) goto fin;
1048 }
1049 fin:
1050 printf ("\033[?25h"); /* show cursor */
1051 return;
1052 }
1053 //}}}
1054
1055 // undo logic {{{
1056 void undo_push (int _f, int t, int n, int o) {
1057 struct undo* new = malloc(sizeof(struct undo));
1058 new->f = _f;
1059 new->t = t;
1060 new->n = n;
1061 new->o = o;
1062 new->prev = f.u;
1063 new->next = NULL;
1064 f.u->next = new;
1065 f.u = f.u->next;
1066 }
1067 void undo_pop (struct undo* u) {
1068 if (u == &undo_sentinel) return;
1069
1070 #ifdef KLONDIKE
1071 if (u->f == FOUNDATION) {
1072 /* foundation -> tableu */
1073 int top_f = find_top(f.f[u->n]);
1074 int top_t = find_top(f.t[u->t]);
1075 f.f[u->n][top_f+1] = f.t[u->t][top_t];
1076 f.t[u->t][top_t] = NO_CARD;
1077 } else if (u->f == WASTE && u->t == FOUNDATION) {
1078 /* waste -> foundation */
1079 /* split u->n into wst and fnd: */
1080 int wst = u->n & 0xffff;
1081 int fnd = u->n >> 16;
1082 /* move stock cards one position up to make room: */
1083 for (int i = f.z; i >= wst; i--) f.s[i+1] = f.s[i];
1084 /* move one card from foundation to waste: */
1085 int top = find_top(f.f[fnd]);
1086 f.s[wst] = f.f[fnd][top];
1087 f.f[fnd][top] = NO_CARD;
1088 f.z++;
1089 f.w++;
1090 } else if (u->f == WASTE) {
1091 /* waste -> tableu */
1092 /* move stock cards one position up to make room: */
1093 for (int i = f.z; i >= u->n; i--) f.s[i+1] = f.s[i];
1094 /* move one card from tableu to waste: */
1095 int top = find_top(f.t[u->t]);
1096 f.s[u->n] = f.t[u->t][top];
1097 f.t[u->t][top] = NO_CARD;
1098 f.z++;
1099 f.w++;
1100 } else if (u->t == FOUNDATION) {
1101 /* tableu -> foundation */
1102 int top_f = find_top(f.t[u->f]);
1103 int top_t = find_top(f.f[u->n]);
1104 /* close topcard if previous action caused turn_over(): */
1105 if (u->o) f.t[u->f][top_f] *= -1;
1106 /* move one card from foundation to tableu: */
1107 f.t[u->f][top_f+1] = f.f[u->n][top_t];
1108 f.f[u->n][top_t] = NO_CARD;
1109 } else {
1110 /* tableu -> tableu */
1111 int top_f = find_top(f.t[u->f]);
1112 int top_t = find_top(f.t[u->t]);
1113 /* close topcard if previous action caused turn_over(): */
1114 if (u->o) f.t[u->f][top_f] *= -1;
1115 /* move n cards from tableu[f] to tableu[t]: */
1116 for (int i = 0; i < u->n; i++) {
1117 f.t[u->f][top_f+u->n-i] = f.t[u->t][top_t-i];
1118 f.t[u->t][top_t-i] = NO_CARD;
1119 }
1120 }
1121 #elif defined SPIDER
1122 if (u->f == STOCK) {
1123 /* stock -> tableu */
1124 /*remove a card from each pile and put it back onto the stock:*/
1125 for (int pile = NUM_PILES-1; pile >= 0; pile--) {
1126 int top = find_top(f.t[pile]);
1127 f.s[f.z++] = f.t[pile][top];
1128 f.t[pile][top] = NO_CARD;
1129 }
1130 } else if (u->t == FOUNDATION) {
1131 /* tableu -> foundation */
1132 int top = find_top(f.t[u->f]);
1133 /* close topcard if previous action caused turn_over(): */
1134 if (u->o) f.t[u->f][top] *= -1;
1135 /* append cards from foundation to tableu */
1136 for (int i = RANK_K; i >= RANK_A; i--) {
1137 f.t[u->f][++top] = f.f[u->n][i];
1138 f.f[u->n][i] = NO_CARD;
1139 }
1140 f.w--; /* decrement complete-foundation-counter */
1141
1142 } else {
1143 /* tableu -> tableu */
1144 int top_f = find_top(f.t[u->f]);
1145 int top_t = find_top(f.t[u->t]);
1146 /* close topcard if previous action caused turn_over(): */
1147 if (u->o) f.t[u->f][top_f] *= -1;
1148 /* move n cards from tableu[f] to tableu[t]: */
1149 for (int i = 0; i < u->n; i++) {
1150 f.t[u->f][top_f+u->n-i] = f.t[u->t][top_t-i];
1151 f.t[u->t][top_t-i] = NO_CARD;
1152 }
1153 }
1154 #endif
1155
1156 void* old = f.u;
1157 f.u = f.u->prev;
1158 free(old);
1159 }
1160 void free_undo (struct undo* u) {
1161 while (u && u != &undo_sentinel) {
1162 void* old = u;
1163 u = u->prev;
1164 free (old);
1165 }
1166 }
1167 //}}}
1168
1169 // initialization stuff {{{
1170 void screen_setup (int enable) {
1171 if (enable) {
1172 raw_mode(1);
1173 printf ("\033[s\033[?47h"); /* save cursor, alternate screen */
1174 printf ("\033[H\033[J"); /* reset cursor, clear screen */
1175 //TODO//printf ("\033[?1000h\033[?25l"); /* enable mouse, hide cursor */
1176 } else {
1177 //TODO//printf ("\033[?9l\033[?25h"); /* disable mouse, show cursor */
1178 printf ("\033[?47l\033[u"); /* primary screen, restore cursor */
1179 raw_mode(0);
1180 }
1181 }
1182
1183 void raw_mode(int enable) {
1184 static struct termios saved_term_mode;
1185 struct termios raw_term_mode;
1186
1187 if (enable) {
1188 if (saved_term_mode.c_lflag == 0)/*don't overwrite stored mode*/
1189 tcgetattr(STDIN_FILENO, &saved_term_mode);
1190 raw_term_mode = saved_term_mode;
1191 raw_term_mode.c_lflag &= ~(ICANON | ECHO);
1192 raw_term_mode.c_cc[VMIN] = 1 ;
1193 raw_term_mode.c_cc[VTIME] = 0;
1194 tcsetattr(STDIN_FILENO, TCSAFLUSH, &raw_term_mode);
1195 } else {
1196 tcsetattr(STDIN_FILENO, TCSAFLUSH, &saved_term_mode);
1197 }
1198 }
1199
1200 void signal_handler (int signum) {
1201 struct winsize w;
1202 switch (signum) {
1203 case SIGCONT:
1204 screen_setup(0);
1205 screen_setup(1);
1206 print_table(NO_HI, NO_HI);
1207 break;
1208 case SIGINT: //TODO: don't exit; just warn like vim does
1209 exit(128+SIGINT);
1210 case SIGWINCH:
1211 ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
1212 op.w[0] = w.ws_row;
1213 op.w[1] = w.ws_col;
1214 break;
1215 }
1216 }
1217 void signal_setup(void) {
1218 struct sigaction saction;
1219
1220 saction.sa_handler = signal_handler;
1221 sigemptyset(&saction.sa_mask);
1222 saction.sa_flags = 0;
1223 if (sigaction(SIGCONT, &saction, NULL) < 0) {
1224 perror ("SIGCONT");
1225 exit (1);
1226 }
1227 if (sigaction(SIGINT, &saction, NULL) < 0) {
1228 perror ("SIGINT");
1229 exit (1);
1230 }
1231 if (sigaction(SIGWINCH, &saction, NULL) < 0) {
1232 perror ("SIGWINCH");
1233 exit (1);
1234 }
1235 }
1236 //}}}
1237
1238 //vim: foldmethod=marker
Imprint / Impressum