]> git.gir.st - solVItaire.git/blob - sol.c
better join for klondike
[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 //TODO: which pile to take from should form the basis of CMD_HINT
386 int join(int to) {
387 int top_to = find_top(f.t[to]);
388
389 #ifdef KLONDIKE
390 if (to == FOUNDATION) {
391 int status = ERR;
392 for (int i = 0; i <= TAB_MAX; i++)
393 switch ((i?t2f:w2f)(i-1, FOUNDATION, 0)) {
394 case WON: return WON;
395 case OK: status = OK;
396 case ERR: /* nop */;
397 }
398 return status;
399 }
400
401 if (top_to < 0) { /* move a king to empty pile: */
402 for (int i = 0; i < TAB_MAX; i++) {
403 if (f.t[i][0] < 0) /* i.e. would turn? */
404 if (t2t(i, to, 0) == OK) return OK;
405 }
406 return w2t(WASTE, to, 0);
407 }
408 #elif defined SPIDER
409 if (top_to < 0) { //TODO: would-turn or longest stack to empty pile
410 }
411 #endif
412
413 struct rating {
414 int ok:1; /* card to move in pile? */
415 int above; /* number of movable cards above */
416 int below; /* number of cards below ours */
417 int pos; /* where the card to move is in the pile */
418 } r[NUM_PILES] = {{0}};
419
420 /* 1. rate each pile: */
421 for (int pile = 0; pile < NUM_PILES; pile++) {
422 r[pile].pos = find_top(f.t[pile]);
423 /* backtrack until we find a compatible-to-'to'-pile card: */
424 while (r[pile].pos >= 0 && is_movable(f.t[pile], r[pile].pos)) {
425 int rankdiff = get_rank(f.t[pile][r[pile].pos])
426 - get_rank(f.t[to][top_to]);
427 if (rankdiff >= 0) break; /* past our card */
428 if (rankdiff == -1 /* rank matches */
429 #ifdef KLONDIKE
430 && get_color(f.t[pile][r[pile].pos]) /* color OK */
431 != get_color(f.t[to][top_to])
432 #elif defined SPIDER
433 && get_suit(f.t[pile][r[pile].pos]) /* color OK */
434 == get_suit(f.t[to][top_to])
435 #endif
436 ) {
437 r[pile].ok++;
438 for (int i = r[pile].pos; i >= 0; i--)
439 if (is_movable(f.t[pile], i-1))
440 r[pile].above++;
441 else break;
442 break;
443 }
444 r[pile].pos--;
445 r[pile].below++;
446 }
447 }
448
449 /* 2. find optimal pile: (optimized for spider) */
450 //TODO: maybe add 'would finish' (spider)?
451 int from = -1;
452 int turn = 0;
453 for (int pile = 0, above = 99, empty = 0, below = 99, e = 0, t = 0;
454 pile < NUM_PILES; pile++) {
455 if (!r[pile].ok) continue;
456
457 if ((e=(r[pile].pos == 0)) /* will become empty */
458 || ((t=(f.t[pile][r[pile].pos-1] < 0)) && !empty) /* will turn_over */
459 || (r[pile].above < above && !empty) /* less cards above */
460 || (r[pile].above ==above && r[pile].below < below && !empty && !turn)) { /* if tied, use shorter pile */
461 from = pile;
462 above = r[pile].above;
463 below = r[pile].below;
464 empty |= e;
465 turn |= t;
466 }
467 }
468
469 /* 3. move cards over and return: */
470 #ifdef KLONDIKE
471 /* prefer waste if it wouldn't turn_over: */
472 if (!turn && w2t(WASTE, to, 0) == OK)
473 return OK;
474 if (from < 0) /* nothing found */
475 return ERR;
476 return t2t(from, to, 0);
477 #elif defined SPIDER
478 if (from < 0) /* nothing found */
479 return ERR;
480 int bottom = first_movable(f.t[from]);
481 return t2t(from, to, get_rank(f.t[from][bottom]));
482 #endif
483 }
484 int nop(int from, int to, int opt) { (void)from;(void)to;(void)opt;return ERR; }
485 // }}}
486
487 // keyboard input handling {{{
488 // cursor functions{{{
489 #ifdef KLONDIKE
490 void cursor_left (struct cursor* cursor) {
491 if (is_tableu(cursor->pile)) {
492 if (cursor->pile > 0) cursor->pile--;
493 cursor->opt = 0;
494 } else { /* stock/waste/foundation*/
495 switch (cursor->pile) {
496 case WASTE: cursor->pile = STOCK; cursor->opt = 0; break;
497 case FOUNDATION:
498 if (cursor->opt <= 0)
499 cursor->pile = WASTE;
500 else
501 cursor->opt--;
502 }
503 }
504 }
505 void cursor_down (struct cursor* cursor) {
506 if (!is_tableu(cursor->pile)) {
507 switch (cursor->pile) {
508 case STOCK: cursor->pile = TAB_1; break;
509 case WASTE: cursor->pile = TAB_2; break;
510 case FOUNDATION:
511 cursor->pile = TAB_4 + cursor->opt;
512 }
513 cursor->opt = 0;
514 }
515 }
516 void cursor_up (struct cursor* cursor) {
517 if (is_tableu(cursor->pile)) {
518 switch (cursor->pile) { //ugly :|
519 case TAB_1: cursor->pile = STOCK; break;
520 case TAB_2: cursor->pile = WASTE; break;
521 case TAB_3: cursor->pile = WASTE; break;
522 case TAB_4: case TAB_5: case TAB_6: case TAB_7:
523 cursor->opt=cursor->pile-TAB_4;
524 cursor->pile = FOUNDATION;
525 break;
526 }
527 }
528 }
529 void cursor_right (struct cursor* cursor) {
530 if (is_tableu(cursor->pile)) {
531 if (cursor->pile < TAB_MAX) cursor->pile++;
532 } else {
533 switch (cursor->pile) {
534 case STOCK: cursor->pile = WASTE; break;
535 case WASTE: cursor->pile = FOUNDATION;cursor->opt = 0; break;
536 case FOUNDATION:
537 if (cursor->opt < NUM_SUITS-1)
538 cursor->opt++;
539 }
540 }
541 }
542 #elif defined SPIDER
543 /*NOTE: one can't highlight the stock due to me being too lazy to implement it*/
544 void cursor_left (struct cursor* cursor) {
545 if (cursor->pile > 0) cursor->pile--;
546 cursor->opt = 0;
547 }
548 void cursor_down (struct cursor* cursor) {
549 int first = first_movable(f.t[cursor->pile]);
550 int top = find_top(f.t[cursor->pile]);
551 if (first + cursor->opt < top)
552 cursor->opt++;
553 }
554 void cursor_up (struct cursor* cursor) {
555 if (cursor->opt > 0) cursor->opt--;
556 }
557 void cursor_right (struct cursor* cursor) {
558 if (cursor->pile < TAB_MAX) cursor->pile++;
559 cursor->opt = 0;
560 }
561 #endif
562 void cursor_to (struct cursor* cursor, int pile) {
563 cursor->pile = pile;
564 cursor->opt = 0;
565 }
566 //}}}
567 int get_cmd (int* from, int* to, int* opt) {
568 /*XXX*/unsigned char mouse[3];
569 //TODO: escape sequences (mouse, cursor keys)
570 int _f, t;
571 struct cursor inactive = {-1,-1};
572 static struct cursor active = {0,0};
573 active.opt = 0; /* always reset offset, but keep pile */
574
575 /***/
576 from_l: print_table(&active, &inactive);
577 _f = getch(mouse);
578
579 switch (_f) {
580 /* direct addressing: */
581 case '1': *from = TAB_1; break;
582 case '2': *from = TAB_2; break;
583 case '3': *from = TAB_3; break;
584 case '4': *from = TAB_4; break;
585 case '5': *from = TAB_5; break;
586 case '6': *from = TAB_6; break;
587 case '7': *from = TAB_7; break;
588 #ifdef SPIDER
589 case '8': *from = TAB_8; break;
590 case '9': *from = TAB_9; break;
591 case '0': *from = TAB_10;break;
592 #elif defined KLONDIKE
593 case '9': *from = WASTE; break;
594 case '0': *from = FOUNDATION; break;
595 case '8': /* fallthrough */
596 #endif
597 case '\n': /* shortcut for dealing from stock */
598 *from = STOCK;
599 *to = WASTE;
600 return CMD_MOVE;
601 /* cursor keys addressing: */
602 case KEY_LEFT:
603 case 'h': cursor_left (&active); goto from_l;
604 case KEY_DOWN:
605 case 'j': cursor_down (&active); goto from_l;
606 case KEY_UP:
607 case 'k': cursor_up (&active); goto from_l;
608 case KEY_RIGHT:
609 case 'l': cursor_right(&active); goto from_l;
610 case KEY_HOME:
611 case 'H': cursor_to(&active,TAB_1); goto from_l; /* leftmost tableu */
612 case KEY_END:
613 case 'L': cursor_to(&active,TAB_MAX);goto from_l; /* rigthmost tableu */
614 case KEY_INS:
615 case 'M': cursor_to(&active,TAB_MAX/2); goto from_l; /* center tableu */
616 //TODO: real cursor keys, home/end
617 case ' ': /* continue with second cursor */
618 *from = active.pile;
619 if (*from == STOCK) {
620 *to = WASTE;
621 return CMD_MOVE;
622 }
623 #ifdef KLONDIKE
624 *opt = active.opt; /* when FOUNDATION */
625 #endif
626 inactive = active;
627 break;
628 /* misc keys: */
629 case ':':
630 {char buf[256];
631 fprintf (stderr, ":");
632 raw_mode(0); /* turn on echo */
633 fgets (buf, 256, stdin);
634 raw_mode(1);
635 switch(buf[0]) {
636 case 'q': return CMD_QUIT;
637 case 'n': return CMD_NEW;
638 case 'r': return CMD_AGAIN;
639 default: return CMD_INVAL;
640 }}
641 case 'J':
642 *to = active.pile;
643 #ifdef KLONDIKE
644 if (*to == FOUNDATION) return CMD_JOIN;
645 #endif
646 if (*to > TAB_MAX) return CMD_INVAL;
647 return CMD_JOIN;
648 case 'K': /* fallthrough */
649 case '?': return CMD_HINT;
650 case 'u': return CMD_UNDO;
651 case EOF: return CMD_NONE; /* sent by SIGCONT */
652 default: return CMD_INVAL;
653 }
654 inactive.pile = *from; /* for direct addressing highlighting */
655 if (is_tableu(*from) && f.t[*from][0] == NO_CARD) return CMD_INVAL;
656
657 /***/
658 to_l: print_table(&active, &inactive);
659 t = getch(mouse);
660
661 switch (t) {
662 case KEY_LEFT:
663 case 'h': cursor_left (&active); goto to_l;
664 case KEY_DOWN:
665 case 'j': cursor_down (&active); goto to_l;
666 case KEY_UP:
667 case 'k': cursor_up (&active); goto to_l;
668 case KEY_RIGHT:
669 case 'l': cursor_right(&active); goto to_l;
670 case KEY_HOME:
671 case 'H': cursor_to(&active,TAB_1); goto to_l;
672 case KEY_END:
673 case 'L': cursor_to(&active,TAB_MAX); goto to_l;
674 case KEY_INS:
675 case 'M': cursor_to(&active,TAB_MAX/2); goto to_l;
676 case 'J': /* fallthrough; just join selected pile */
677 case ' ':
678 *to = active.pile;
679 break; /* continues with the foundation/empty tableu check */
680 case 'K': /* fallthrough */
681 case '?': return CMD_HINT;
682 case 'u': return CMD_NONE; /* cancel selection */
683 case EOF: return CMD_NONE; /* sent by SIGCONT */
684 default:
685 if (t < '0' || t > '9') return CMD_INVAL;
686 if (t == '0')
687 #ifdef KLONDIKE
688 *to = FOUNDATION;
689 #elif defined SPIDER
690 *to = TAB_10;
691 #endif
692 else
693 *to = t-'1';
694 }
695
696 /***/
697 #ifdef KLONDIKE
698 if (*from == FOUNDATION) {
699 int top = find_top(f.t[*to]);
700 if (top < 0) return CMD_INVAL;
701 int color = get_color(f.t[*to][top]);
702 int choice_1 = 1-color; /* selects piles of */
703 int choice_2 = 2+color; /* the opposite color */
704 int top_c1 = find_top(f.f[choice_1]);
705 int top_c2 = find_top(f.f[choice_2]);
706
707 switch ((rank_next(f.f[choice_1][top_c1], f.t[*to][top])
708 && top_c1 >= 0 ) << 0
709 |(rank_next(f.f[choice_2][top_c2], f.t[*to][top])
710 && top_c2 >= 0 ) << 1) {
711 case ( 1<<0): *opt = choice_1; break; /* choice_1 only */
712 case (1<<1 ): *opt = choice_2; break; /* choice_2 only */
713 case (1<<1 | 1<<0): /* both, ask user which to pick from */
714 printf ("take from (1-4): "); fflush (stdout);
715 *opt = getch(NULL) - '1';
716 if (*opt < 0 || *opt > 3) return CMD_INVAL;
717 break;
718 default: return CMD_INVAL; /* none matched */
719 }
720 /* `opt` is the foundation index (0..3) */
721 }
722 #elif defined SPIDER
723 /* moving to empty tableu? */
724 if (is_tableu(*to) && f.t[*to][0] == NO_CARD) {
725 int bottom = first_movable(f.t[*from]);
726 if (inactive.opt >= 0) { /*if from was cursor addressed: */
727 *opt = get_rank(f.t[*from][bottom + inactive.opt]);
728 return CMD_MOVE;
729 }
730 int top = find_top(f.t[*from]);
731 if (top < 0) return CMD_INVAL;
732 if (top >= 0 && !is_movable(f.t[*from], top-1)) {
733 *opt = get_rank(f.t[*from][top]);
734 } else { /* only ask the user if it's unclear: */
735 printf ("\rup to ([a23456789xjqk] or space/return): ");
736 *opt = getch(NULL);
737 switch (*opt) {
738 case ' ': *opt = get_rank(f.t[*from][top]); break;
739 case'\n': *opt = get_rank(f.t[*from][bottom]); break;
740 case 'a': case 'A': *opt = RANK_A; break;
741 case '0': /* fallthrough */
742 case 'x': case 'X': *opt = RANK_X; break;
743 case 'j': case 'J': *opt = RANK_J; break;
744 case 'q': case 'Q': *opt = RANK_Q; break;
745 case 'k': case 'K': *opt = RANK_K; break;
746 default: *opt -= '1';
747 }
748 if (*opt < RANK_A || *opt > RANK_K) return ERR;
749 }
750 /* `opt` is the rank of the highest card to move */
751 }
752 #endif
753 return CMD_MOVE;
754 }
755
756 int getctrlseq(unsigned char* buf) {
757 int c;
758 enum esc_states {
759 START,
760 ESC_SENT,
761 CSI_SENT,
762 MOUSE_EVENT,
763 } state = START;
764 int offset = 0x20; /* never sends control chars as data */
765 while ((c = getchar()) != EOF) {
766 switch (state) {
767 case START:
768 switch (c) {
769 case '\033': state=ESC_SENT; break;
770 default: return c;
771 }
772 break;
773 case ESC_SENT:
774 switch (c) {
775 case '[': state=CSI_SENT; break;
776 default: return KEY_INVAL;
777 }
778 break;
779 case CSI_SENT:
780 switch (c) {
781 case 'A': return KEY_UP;
782 case 'B': return KEY_DOWN;
783 case 'C': return KEY_RIGHT;
784 case 'D': return KEY_LEFT;
785 /*NOTE: home/end send ^[[x~ . no support for modifiers*/
786 case 'H': return KEY_HOME;
787 case 'F': return KEY_END;
788 case '2': getchar(); return KEY_INS;
789 case '5': getchar(); return KEY_PGUP;
790 case '6': getchar(); return KEY_PGDN;
791 case 'M': state=MOUSE_EVENT; break;
792 default: return KEY_INVAL;
793 }
794 break;
795 case MOUSE_EVENT:
796 if (buf == NULL) return KEY_INVAL;
797 buf[0] = c - offset;
798 buf[1] = getchar() - offset;
799 buf[2] = getchar() - offset;
800 return MOUSE_ANY;
801 default:
802 return KEY_INVAL;
803 }
804 }
805 return 2;
806 }
807 int wait_mouse_up(int l, int c) {
808 unsigned char mouse2[3];
809 int level = 1;
810 int l2, c2;
811
812 /* TODO: show a pushed-in button if cursor is on minefield */
813
814 while (level > 0) {
815 if (getctrlseq (mouse2) == MOUSE_ANY) {
816 /* ignore mouse wheel events: */
817 if (mouse2[0] & 0x40) continue;
818
819 else if((mouse2[0]&3) == 3) level--; /* release event */
820 else level++; /* another button pressed */
821 }
822 }
823
824 /* TODO: show normal button */
825
826 c2 = /*screen2field_c*/(mouse2[1]);
827 l2 = /*screen2field_l*/(mouse2[2]);
828 return ((l2 == l) && (c2 == c));
829 }
830
831 int getch(unsigned char* buf) {
832 /* returns a character, EOF, or constant for an escape/control sequence - NOT
833 compatible with the ncurses implementation of same name */
834 int action = getctrlseq(buf);
835 int l, c;
836 switch (action) {
837 case MOUSE_ANY:
838 l = /*screen2field_l*/ (buf[2]);
839 c = /*screen2field_c*/ (buf[1]);
840
841 if (buf[0] > 3) break; /* ignore all but left/middle/right/up */
842 int success = wait_mouse_up(l, c);
843
844 /* mouse moved while pressed: */
845 if (!success) return KEY_INVAL;
846
847 switch (buf[0]) {
848 case 0: return MOUSE_LEFT;
849 case 1: return MOUSE_MIDDLE;
850 case 2: return MOUSE_RIGHT;
851 }
852 }
853
854 return action;
855 }
856 // }}}
857
858 // shuffling and dealing {{{
859 void deal(long seed) {
860 f = (const struct playfield){0}; /* clear playfield */
861 card_t deck[DECK_SIZE*NUM_DECKS];
862 int avail = DECK_SIZE*NUM_DECKS;
863 for (int i = 0; i < DECK_SIZE*NUM_DECKS; i++) deck[i] = (i%DECK_SIZE)+1;
864 #ifdef SPIDER
865 if (op.m != NORMAL) for (int i = 0; i < DECK_SIZE*NUM_DECKS; i++) {
866 if (op.m == MEDIUM) deck[i] = 1+((deck[i]-1) | 2);
867 if (op.m == EASY) deck[i] = 1+((deck[i]-1) | 2 | 1);
868 /* the 1+ -1 dance gets rid of the offset created by NO_CARD */
869 }
870 #endif
871 srand (seed);
872 for (int i = DECK_SIZE*NUM_DECKS-1; i > 0; i--) { /* fisher-yates */
873 int j = rand() % (i+1);
874 if (j-i) deck[i]^=deck[j],deck[j]^=deck[i],deck[i]^=deck[j];
875 }
876
877 /* deal cards: */
878 for (int i = 0; i < NUM_PILES; i++) {
879 #ifdef KLONDIKE
880 int closed = i; /* pile n has n closed cards, then 1 open */
881 #elif defined SPIDER
882 int closed = i<4?5:4; /* pile 1-4 have 5, 5-10 have 4 closed */
883 #endif
884 /* face down cards are negated: */
885 for (int j = 0; j < closed; j++) f.t[i][j] = -deck[--avail];
886 f.t[i][closed] = deck[--avail]; /* the face-up card */
887 }
888 /* rest of the cards to the stock; NOTE: assert(avail==50) for spider */
889 for (f.z = 0; avail; f.z++) f.s[f.z] = deck[--avail];
890 #ifdef KLONDIKE
891 f.w = -1; /* @start: nothing on waste */
892 #elif defined SPIDER
893 f.w = 0; /* number of used foundations */
894 #endif
895
896 f.u = &undo_sentinel;
897 }
898 //}}}
899
900 // screen drawing routines {{{
901 void print_hi(int invert, int grey_bg, int bold, char* str) {
902 if (bold && op.s == &unicode_large_color){//ARGH! awful hack for bold with faint
903 int offset = str[3]==017?16:str[4]==017?17:0;
904 printf ("%s%s%s""%.*s%s%s""%s%s%s",
905 bold?"\033[1m":"", invert?"\033[7m":"", grey_bg?"\033[100m":"",
906 offset, str, bold?"\033[1m":"", str+offset,
907 grey_bg?"\033[49m":"", invert?"\033[27m":"",bold?"\033[22m":"");
908 return;
909 }
910 printf ("%s%s%s%s%s%s%s",
911 bold?"\033[1m":"", invert?"\033[7m":"", grey_bg?"\033[100m":"",
912 str,
913 grey_bg?"\033[49m":"", invert?"\033[27m":"",bold?"\033[22m":"");
914 }
915 void print_table(const struct cursor* active, const struct cursor* inactive) {
916 printf("\033[2J\033[H"); /* clear screen, reset cursor */
917 #ifdef KLONDIKE
918 /* print stock, waste and foundation: */
919 for (int line = 0; line < op.s->height; line++) {
920 /* stock: */
921 print_hi (active->pile == STOCK, inactive->pile == STOCK, 1, (
922 (f.w < f.z-1)?op.s->facedown
923 :op.s->placeholder)[line]);
924 /* waste: */
925 print_hi (active->pile == WASTE, inactive->pile == WASTE, 1, (
926 /* NOTE: cast, because f.w sometimes is (short)-1 !? */
927 ((short)f.w >= 0)?op.s->card[f.s[f.w]]
928 :op.s->placeholder)[line]);
929 printf ("%s", op.s->card[NO_CARD][line]); /* spacer */
930 /* foundation: */
931 for (int pile = 0; pile < NUM_SUITS; pile++) {
932 int card = find_top(f.f[pile]);
933 print_hi (active->pile==FOUNDATION && active->opt==pile,
934 inactive->pile==FOUNDATION && (
935 /* cursor addr. || direct addr. */
936 inactive->opt==pile || inactive->opt < 0
937 ), 1,
938 (card < 0)?op.s->placeholder[line]
939 :op.s->card[f.f[pile][card]][line]);
940 }
941 printf("\n");
942 }
943 printf("\n");
944 #elif defined SPIDER
945 int fdone; for (fdone = NUM_DECKS*NUM_SUITS; fdone; fdone--)
946 if (f.f[fdone-1][RANK_K]) break; /*number of completed stacks*/
947 int spacer_from = f.z?(f.z/10-1) * op.s->halfwidth[0] + op.s->width:0;
948 int spacer_to = NUM_PILES*op.s->width -
949 ((fdone?(fdone-1) * op.s->halfwidth[1]:0)+op.s->width);
950 for (int line = 0; line < op.s->height; line++) {
951 /* available stock: */
952 for (int i = f.z/10; i; i--) {
953 if (i==1) printf ("%s", op.s->facedown[line]);
954 else printf ("%s", op.s->halfstack[line]);
955 }
956 /* spacer: */
957 for (int i = spacer_from; i < spacer_to; i++) printf (" ");
958 /* foundation (overlapping): */
959 for (int i = 0; i < NUM_DECKS*NUM_SUITS; i++) { //TODO: print in revrse order (otherwise new piles get put 'below' older ones)
960 int overlap = i? op.s->halfcard[line]: 0;
961 if (f.f[i][RANK_K]) printf ("%.*s", op.s->halfwidth[2],
962 op.s->card[f.f[i][RANK_K]][line]+overlap);
963 }
964 printf("\n");
965 }
966 printf("\n");
967 #endif
968 #ifdef KLONDIKE
969 #define DO_HI(cursor) (cursor->pile == pile && (movable || empty))
970 #define TOP_HI(c) 1 /* can't select partial stacks in KLONDIKE */
971 #define INC_OFFSET
972 #elif defined SPIDER
973 int offset[NUM_PILES]={1,1,1,1,1,1,1,1,1,1}; // :|
974 #define DO_HI(cursor) (cursor->pile == pile && (movable || empty) \
975 && offset[pile] > cursor->opt)
976 #define TOP_HI(cursor) (cursor->pile == pile && movable \
977 && offset[pile]-1 == cursor->opt)
978 #define INC_OFFSET if (movable) offset[pile]++
979 #endif
980 /* print tableu piles: */
981 int row[NUM_PILES] = {0};
982 int line[NUM_PILES]= {0};
983 int label[NUM_PILES]={0};
984 int line_had_card;
985 int did_placeholders = 0;
986 do {
987 line_had_card = 0;
988 for (int pile = 0; pile < NUM_PILES; pile++) {
989 card_t card = f.t[pile][row[pile]];
990 card_t next = f.t[pile][row[pile]+1];
991 int movable = is_movable(f.t[pile], row[pile]);
992 int empty = !card && row[pile] == 0;
993
994 print_hi (DO_HI(active), DO_HI(inactive), movable, (
995 (!card && row[pile] == 0)?op.s->placeholder
996 :(card<0)?op.s->facedown
997 :op.s->card[card]
998 )[line[pile]]);
999
1000 int extreme_overlap = ( 3 /* spacer, labels, status */
1001 + 2 * op.s->height /* stock, top tableu card */
1002 + find_top(f.t[pile]) * op.s->overlap) >op.w[0];
1003 /* normal overlap: */
1004 if (++line[pile] >= (next?op.s->overlap:op.s->height)
1005 /* extreme overlap on closed cards: */
1006 || (extreme_overlap &&
1007 line[pile] >= 1 &&
1008 f.t[pile][row[pile]] < 0 &&
1009 f.t[pile][row[pile]+1] <0)
1010 /* extreme overlap on sequences: */
1011 || (extreme_overlap &&
1012 !TOP_HI(active) && /*always show top selected card*/
1013 line[pile] >= 1 && row[pile] > 0 &&
1014 f.t[pile][row[pile]-1] > NO_CARD &&
1015 is_consecutive (f.t[pile], row[pile]) &&
1016 is_consecutive (f.t[pile], row[pile]-1) &&
1017 f.t[pile][row[pile]+1] != NO_CARD)
1018 ) {
1019 line[pile]=0;
1020 row[pile]++;
1021 INC_OFFSET;
1022 }
1023 /* tableu labels: */
1024 if(!card && !label[pile] && row[pile]>0&&line[pile]>0) {
1025 label[pile] = 1;
1026 printf ("\b\b%d ", (pile+1) % 10); //XXX: hack
1027 }
1028 line_had_card |= !!card;
1029 did_placeholders |= row[pile] > 0;
1030 }
1031 printf ("\n");
1032 } while (line_had_card || !did_placeholders);
1033 }
1034
1035 void visbell (void) {
1036 printf ("\033[?5h"); fflush (stdout);
1037 usleep (100000);
1038 printf ("\033[?5l"); fflush (stdout);
1039 }
1040 void win_anim(void) {
1041 printf ("\033[?25l"); /* hide cursor */
1042 for (;;) {
1043 /* set cursor to random location */
1044 int row = 1+rand()%(24-op.s->width);
1045 int col = 1+rand()%(80-op.s->height);
1046
1047 /* draw random card */
1048 int face = 1 + rand() % 52;
1049 for (int l = 0; l < op.s->height; l++) {
1050 printf ("\033[%d;%dH", row+l, col);
1051 printf ("%s", op.s->card[face][l]);
1052 }
1053 fflush (stdout);
1054
1055 /* exit on keypress */
1056 struct pollfd p = {STDIN_FILENO, POLLIN, 0};
1057 if (poll (&p, 1, 80)) goto fin;
1058 }
1059 fin:
1060 printf ("\033[?25h"); /* show cursor */
1061 return;
1062 }
1063 //}}}
1064
1065 // undo logic {{{
1066 void undo_push (int _f, int t, int n, int o) {
1067 struct undo* new = malloc(sizeof(struct undo));
1068 new->f = _f;
1069 new->t = t;
1070 new->n = n;
1071 new->o = o;
1072 new->prev = f.u;
1073 new->next = NULL;
1074 f.u->next = new;
1075 f.u = f.u->next;
1076 }
1077 void undo_pop (struct undo* u) {
1078 if (u == &undo_sentinel) return;
1079
1080 #ifdef KLONDIKE
1081 if (u->f == FOUNDATION) {
1082 /* foundation -> tableu */
1083 int top_f = find_top(f.f[u->n]);
1084 int top_t = find_top(f.t[u->t]);
1085 f.f[u->n][top_f+1] = f.t[u->t][top_t];
1086 f.t[u->t][top_t] = NO_CARD;
1087 } else if (u->f == WASTE && u->t == FOUNDATION) {
1088 /* waste -> foundation */
1089 /* split u->n into wst and fnd: */
1090 int wst = u->n & 0xffff;
1091 int fnd = u->n >> 16;
1092 /* move stock cards one position up to make room: */
1093 for (int i = f.z; i >= wst; i--) f.s[i+1] = f.s[i];
1094 /* move one card from foundation to waste: */
1095 int top = find_top(f.f[fnd]);
1096 f.s[wst] = f.f[fnd][top];
1097 f.f[fnd][top] = NO_CARD;
1098 f.z++;
1099 f.w++;
1100 } else if (u->f == WASTE) {
1101 /* waste -> tableu */
1102 /* move stock cards one position up to make room: */
1103 for (int i = f.z; i >= u->n; i--) f.s[i+1] = f.s[i];
1104 /* move one card from tableu to waste: */
1105 int top = find_top(f.t[u->t]);
1106 f.s[u->n] = f.t[u->t][top];
1107 f.t[u->t][top] = NO_CARD;
1108 f.z++;
1109 f.w++;
1110 } else if (u->t == FOUNDATION) {
1111 /* tableu -> foundation */
1112 int top_f = find_top(f.t[u->f]);
1113 int top_t = find_top(f.f[u->n]);
1114 /* close topcard if previous action caused turn_over(): */
1115 if (u->o) f.t[u->f][top_f] *= -1;
1116 /* move one card from foundation to tableu: */
1117 f.t[u->f][top_f+1] = f.f[u->n][top_t];
1118 f.f[u->n][top_t] = NO_CARD;
1119 } else {
1120 /* tableu -> tableu */
1121 int top_f = find_top(f.t[u->f]);
1122 int top_t = find_top(f.t[u->t]);
1123 /* close topcard if previous action caused turn_over(): */
1124 if (u->o) f.t[u->f][top_f] *= -1;
1125 /* move n cards from tableu[f] to tableu[t]: */
1126 for (int i = 0; i < u->n; i++) {
1127 f.t[u->f][top_f+u->n-i] = f.t[u->t][top_t-i];
1128 f.t[u->t][top_t-i] = NO_CARD;
1129 }
1130 }
1131 #elif defined SPIDER
1132 if (u->f == STOCK) {
1133 /* stock -> tableu */
1134 /*remove a card from each pile and put it back onto the stock:*/
1135 for (int pile = NUM_PILES-1; pile >= 0; pile--) {
1136 int top = find_top(f.t[pile]);
1137 f.s[f.z++] = f.t[pile][top];
1138 f.t[pile][top] = NO_CARD;
1139 }
1140 } else if (u->t == FOUNDATION) {
1141 /* tableu -> foundation */
1142 int top = find_top(f.t[u->f]);
1143 /* close topcard if previous action caused turn_over(): */
1144 if (u->o) f.t[u->f][top] *= -1;
1145 /* append cards from foundation to tableu */
1146 for (int i = RANK_K; i >= RANK_A; i--) {
1147 f.t[u->f][++top] = f.f[u->n][i];
1148 f.f[u->n][i] = NO_CARD;
1149 }
1150 f.w--; /* decrement complete-foundation-counter */
1151
1152 } else {
1153 /* tableu -> tableu */
1154 int top_f = find_top(f.t[u->f]);
1155 int top_t = find_top(f.t[u->t]);
1156 /* close topcard if previous action caused turn_over(): */
1157 if (u->o) f.t[u->f][top_f] *= -1;
1158 /* move n cards from tableu[f] to tableu[t]: */
1159 for (int i = 0; i < u->n; i++) {
1160 f.t[u->f][top_f+u->n-i] = f.t[u->t][top_t-i];
1161 f.t[u->t][top_t-i] = NO_CARD;
1162 }
1163 }
1164 #endif
1165
1166 void* old = f.u;
1167 f.u = f.u->prev;
1168 free(old);
1169 }
1170 void free_undo (struct undo* u) {
1171 while (u && u != &undo_sentinel) {
1172 void* old = u;
1173 u = u->prev;
1174 free (old);
1175 }
1176 }
1177 //}}}
1178
1179 // initialization stuff {{{
1180 void screen_setup (int enable) {
1181 if (enable) {
1182 raw_mode(1);
1183 printf ("\033[s\033[?47h"); /* save cursor, alternate screen */
1184 printf ("\033[H\033[J"); /* reset cursor, clear screen */
1185 //TODO//printf ("\033[?1000h\033[?25l"); /* enable mouse, hide cursor */
1186 } else {
1187 //TODO//printf ("\033[?9l\033[?25h"); /* disable mouse, show cursor */
1188 printf ("\033[?47l\033[u"); /* primary screen, restore cursor */
1189 raw_mode(0);
1190 }
1191 }
1192
1193 void raw_mode(int enable) {
1194 static struct termios saved_term_mode;
1195 struct termios raw_term_mode;
1196
1197 if (enable) {
1198 if (saved_term_mode.c_lflag == 0)/*don't overwrite stored mode*/
1199 tcgetattr(STDIN_FILENO, &saved_term_mode);
1200 raw_term_mode = saved_term_mode;
1201 raw_term_mode.c_lflag &= ~(ICANON | ECHO);
1202 raw_term_mode.c_cc[VMIN] = 1 ;
1203 raw_term_mode.c_cc[VTIME] = 0;
1204 tcsetattr(STDIN_FILENO, TCSAFLUSH, &raw_term_mode);
1205 } else {
1206 tcsetattr(STDIN_FILENO, TCSAFLUSH, &saved_term_mode);
1207 }
1208 }
1209
1210 void signal_handler (int signum) {
1211 struct winsize w;
1212 switch (signum) {
1213 case SIGCONT:
1214 screen_setup(0);
1215 screen_setup(1);
1216 print_table(NO_HI, NO_HI);
1217 break;
1218 case SIGINT: //TODO: don't exit; just warn like vim does
1219 exit(128+SIGINT);
1220 case SIGWINCH:
1221 ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
1222 op.w[0] = w.ws_row;
1223 op.w[1] = w.ws_col;
1224 break;
1225 }
1226 }
1227 void signal_setup(void) {
1228 struct sigaction saction;
1229
1230 saction.sa_handler = signal_handler;
1231 sigemptyset(&saction.sa_mask);
1232 saction.sa_flags = 0;
1233 if (sigaction(SIGCONT, &saction, NULL) < 0) {
1234 perror ("SIGCONT");
1235 exit (1);
1236 }
1237 if (sigaction(SIGINT, &saction, NULL) < 0) {
1238 perror ("SIGINT");
1239 exit (1);
1240 }
1241 if (sigaction(SIGWINCH, &saction, NULL) < 0) {
1242 perror ("SIGWINCH");
1243 exit (1);
1244 }
1245 }
1246 //}}}
1247
1248 //vim: foldmethod=marker
Imprint / Impressum