]> git.gir.st - solVItaire.git/blob - sol.c
remove freecell backspace
[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 #elif defined FREECELL
46 /* 1 2 3 4 5 6 7 8 cll fnd*/
47 /* 1 */ { t2f, t2t, t2t, t2t, t2t, t2t, t2t, t2t, t2c, t2f },
48 /* 2 */ { t2t, t2f, t2t, t2t, t2t, t2t, t2t, t2t, t2c, t2f },
49 /* 3 */ { t2t, t2t, t2f, t2t, t2t, t2t, t2t, t2t, t2c, t2f },
50 /* 4 */ { t2t, t2t, t2t, t2f, t2t, t2t, t2t, t2t, t2c, t2f },
51 /* 5 */ { t2t, t2t, t2t, t2t, t2f, t2t, t2t, t2t, t2c, t2f },
52 /* 6 */ { t2t, t2t, t2t, t2t, t2t, t2f, t2t, t2t, t2c, t2f },
53 /* 7 */ { t2t, t2t, t2t, t2t, t2t, t2t, t2f, t2t, t2c, t2f },
54 /* 8 */ { t2t, t2t, t2t, t2t, t2t, t2t, t2t, t2f, t2c, t2f },
55 /*cll*/ { c2t, c2t, c2t, c2t, c2t, c2t, c2t, c2t, c2f, c2f },
56 /*fnd*/ { f2t, f2t, f2t, f2t, f2t, f2t, f2t, f2t, f2c, nop },
57 #endif
58 };
59 // }}}
60
61 // argv parsing, game loops, cleanup {{{
62 int main(int argc, char** argv) {
63 /* opinionated defaults: */
64 op.s = &unicode_large_color;
65 op.v = 1; /* enable fake visbell by default */
66 #ifdef SPIDER
67 op.m = MEDIUM;
68 #endif
69
70 int optget;
71 opterr = 0; /* don't print message on unrecognized option */
72 while ((optget = getopt (argc, argv, "+:hs:vbcmMV")) != -1) {
73 switch (optget) {
74 #ifdef SPIDER
75 case 's': /* number of suits */
76 switch (optarg[0]) {
77 case '1': op.m = EASY; break;
78 case '2': op.m = MEDIUM; break;
79 case '4': op.m = NORMAL; break;
80 default: goto error;
81 } break;
82 #endif
83 case 'b': op.s = &unicode_large_mono; break;
84 case 'c': op.s = &unicode_large_color; break;
85 case 'm': op.s = &unicode_small_mono; break; /* "mini, monochrome" */
86 case 'M': op.s = &unicode_small_color; break; /* "mini, colorful" */
87 case 'V': op.v = 0; break; /* WARN: experimental; might change */
88 case 'h': default: goto error;
89 error:
90 fprintf (stderr, SHORTHELP LONGHELP KEYHELP, argv[0]);
91 return optget != 'h';
92 }
93 }
94
95 signal_setup();
96 atexit (*quit);
97
98 signal_handler(SIGWINCH); /* initialize window size */
99
100 newgame:
101 screen_setup(1);
102
103 switch(sol()) {
104 case GAME_NEW: goto newgame;
105 case GAME_WON:
106 print_table(NO_HI, NO_HI);
107 win_anim();
108 if (getch(NULL)=='q') return 0;
109 goto newgame;
110 case GAME_QUIT: return 0;
111 }
112 }
113
114 #define is_tableu(where) (where <= TAB_MAX) // "card games helper functions"
115
116 int sol(void) {
117 int ret;
118 long seed = time(NULL);
119 restart:
120 free_undo(f.u);
121 deal(seed);
122
123 int from, to, opt;
124 for(;;) {
125 switch (get_cmd(&from, &to, &opt)) {
126 case CMD_MOVE:
127 ret = action[from][to](from,to,opt);
128 #ifdef FREECELL
129 if (ret == ERR && is_tableu(from) && to == from) /*i.e. failed foundation move*/
130 ret = t2c(from, STOCK, 0);
131 else
132 #endif
133 if (ret == ERR && is_tableu(from) && is_tableu(to))
134 /* try again with from/to swapped: */
135 ret = action[to][from](to,from,opt);
136 switch (ret) {
137 case OK: break;
138 case ERR: visbell(); break;
139 case WON: return GAME_WON;
140 }
141 break;
142 case CMD_JOIN:
143 switch (join(to)) {
144 case OK: break;
145 case ERR: visbell(); break;
146 case WON: return GAME_WON;
147 }
148 break;
149 case CMD_HINT: break;//TODO: show a possible (and sensible) move. if possible, involve active cursor
150 case CMD_UNDO: undo_pop(f.u); break;
151 case CMD_INVAL: visbell(); break;
152 case CMD_NEW: return GAME_NEW;
153 case CMD_AGAIN: goto restart;
154 case CMD_QUIT: return GAME_QUIT;
155 case CMD_HELP:
156 printf (KEYHELP "\nPress any key to continue.");
157 getch(NULL);
158 break;
159 }
160 }
161 }
162
163 void quit(void) {
164 screen_setup(0);
165 free_undo(f.u);
166 }
167 //}}}
168
169 // card games helper functions {{{
170 #define get_suit(card) \
171 ((card-1) % NUM_SUITS)
172 #define get_rank(card) \
173 ((card-1) / NUM_SUITS)
174 #define get_color(card) \
175 ((get_suit(card) ^ get_suit(card)>>1) & 1)
176
177 int find_top(card_t* pile) {
178 int i;
179 for(i=PILE_SIZE-1; i>=0 && !pile[i]; i--);
180 return i;
181 }
182 int first_movable(card_t* pile) {
183 /* NOTE: in FREECELL this does not take max_move into account! */
184 int i = 0;
185 for (;pile[i] && !is_movable(pile, i); i++);
186 return i;
187 }
188 int turn_over(card_t* pile) {
189 int top = find_top(pile);
190 if (pile[top] < 0) {
191 pile[top] *= -1;
192 return 1;
193 } else return 0;
194 }
195 int check_won(void) {
196 for (int pile = 0; pile < NUM_DECKS*NUM_SUITS; pile++)
197 if (f.f[pile][NUM_RANKS-1] == NO_CARD) return 0;
198
199 return 1;
200 }
201 int rank_next (card_t a, card_t b) {
202 return get_rank(a) == get_rank(b)-1;
203 }
204 int is_consecutive (card_t* pile, int pos) {
205 if (pos+1 >= PILE_SIZE) return 1; /* card is last */
206 if (pile[pos+1] == NO_CARD) return 1; /* card is first */
207
208 #if defined KLONDIKE || defined FREECELL
209 /* ranks consecutive? */
210 if (!rank_next(pile[pos+1], pile[pos])) return 0;
211 /* color opposite? */
212 if (get_color(pile[pos+1]) == get_color(pile[pos])) return 0;
213 #elif defined SPIDER
214 /* ranks consecutive? */
215 if (!rank_next(pile[pos+1], pile[pos])) return 0;
216 /* same suit? */
217 if (get_suit(pile[pos+1]) != get_suit(pile[pos])) return 0;
218 #endif
219
220 return 1;
221 }
222
223 int is_movable(card_t* pile, int n) {
224 #ifdef KLONDIKE
225 return(pile[n] > NO_CARD); /*non-movable cards don't exist in klondike*/
226 #elif defined SPIDER || defined FREECELL
227 int top = find_top(pile);
228 for (int i = top; i >= 0; i--) {
229 if (pile[i] <= NO_CARD) return 0; /*no card or card face down?*/
230 if (!is_consecutive(pile, i)) return 0;
231 if (i == n) return 1; /* card reached, must be movable */
232 }
233 return 0;
234 #endif
235 }
236 //}}}
237
238 // takeable actions {{{
239 #ifdef KLONDIKE
240 card_t stack_take(void) { /*NOTE: assert(f.w >= 0) */
241 card_t card = f.s[f.w];
242 /* move stack one over, so there are no gaps in it: */
243 for (int i = f.w; i < f.z-1; i++)
244 f.s[i] = f.s[i+1];
245 f.z--;
246 f.w--; /* make previous card visible again */
247 return card;
248 }
249 int t2f(int from, int to, int opt) { /* tableu to foundation */
250 (void) to; (void) opt; /* don't need */
251 int top_from = find_top(f.t[from]);
252 to = get_suit(f.t[from][top_from]);
253 int top_to = find_top(f.f[to]);
254 if ((top_to < 0 && get_rank(f.t[from][top_from]) == RANK_A)
255 || (top_to >= 0 && rank_next(f.f[to][top_to],f.t[from][top_from]))) {
256 f.f[to][top_to+1] = f.t[from][top_from];
257 f.t[from][top_from] = NO_CARD;
258 undo_push(from, FOUNDATION, to,
259 turn_over(f.t[from]));
260 if (check_won()) return WON;
261 return OK;
262 } else return ERR;
263 }
264 int w2f(int from, int to, int opt) { /* waste to foundation */
265 (void) from; (void) to; (void) opt; /* don't need */
266 if (f.w < 0) return ERR;
267 to = get_suit(f.s[f.w]);
268 int top_to = find_top(f.f[to]);
269 if ((top_to < 0 && get_rank(f.s[f.w]) == RANK_A)
270 || (top_to >= 0 && rank_next(f.f[to][top_to], f.s[f.w]))) {
271 undo_push(WASTE, FOUNDATION, f.w | to<<16, 0);//ugly encoding :|
272 f.f[to][top_to+1] = stack_take();
273 if (check_won()) return WON;
274 return OK;
275 } else return ERR;
276
277 }
278 int s2w(int from, int to, int opt) { /* stock to waste */
279 (void) from; (void) to; (void) opt; /* don't need */
280 if (f.z == 0) return ERR;
281 f.w++;
282 if (f.w == f.z) f.w = -1;
283 return OK;
284 }
285 int w2s(int from, int to, int opt) { /* waste to stock (undo stock to waste) */
286 (void) from; (void) to; (void) opt; /* don't need */
287 if (f.z == 0) return ERR;
288 f.w--;
289 if (f.w < -1) f.w = f.z-1;
290 return OK;
291 }
292 int f2t(int from, int to, int opt) { /* foundation to tableu */
293 (void) from; /* don't need */
294 int top_to = find_top(f.t[to]);
295 from = opt;
296 int top_from = find_top(f.f[from]);
297
298 if ((get_color(f.t[to][top_to]) != get_color(f.f[from][top_from]))
299 && (rank_next(f.f[from][top_from], f.t[to][top_to]))) {
300 f.t[to][top_to+1] = f.f[from][top_from];
301 f.f[from][top_from] = NO_CARD;
302 undo_push(FOUNDATION, to, from, 0);
303 return OK;
304 } else return ERR;
305 }
306 int w2t(int from, int to, int opt) { /* waste to tableu */
307 (void) from; (void) opt; /* don't need */
308 if (f.w < 0) return ERR;
309 int top_to = find_top(f.t[to]);
310 if (((get_color(f.t[to][top_to]) != get_color(f.s[f.w]))
311 && (rank_next(f.s[f.w], f.t[to][top_to])))
312 || (top_to < 0 && get_rank(f.s[f.w]) == RANK_K)) {
313 undo_push(WASTE, to, f.w, 0);
314 f.t[to][top_to+1] = stack_take();
315 return OK;
316 } else return ERR;
317 }
318 int t2t(int from, int to, int opt) { /* tableu to tableu */
319 (void) opt; /* don't need */
320 int top_to = find_top(f.t[to]);
321 int top_from = find_top(f.t[from]);
322 int count = 0; //NOTE: could probably be factored out
323 for (int i = top_from; i >=0; i--) {
324 if (((get_color(f.t[to][top_to]) != get_color(f.t[from][i]))
325 && (rank_next(f.t[from][i], f.t[to][top_to]))
326 && f.t[from][i] > NO_CARD) /* card face up? */
327 || (top_to < 0 && get_rank(f.t[from][i]) == RANK_K)) {
328 /* move cards [i..top_from] to their destination */
329 for (;i <= top_from; i++) {
330 top_to++;
331 f.t[to][top_to] = f.t[from][i];
332 f.t[from][i] = NO_CARD;
333 count++;
334 }
335 undo_push(from, to, count,
336 turn_over(f.t[from]));
337 return OK;
338 }
339 }
340 return ERR; /* no such move possible */
341 }
342 #elif defined SPIDER
343 int remove_if_complete (int pileno) { //cleanup!
344 card_t* pile = f.t[pileno];
345 /* test if K...A complete; move to foundation if so */
346 int top_from = find_top(pile);
347 if (get_rank(pile[top_from]) != RANK_A) return 0;
348 for (int i = top_from; i>=0; i--) {
349 if (!is_consecutive (pile, i)) return 0;
350 if (i+RANK_K == top_from /* if ace to king: remove it */
351 && get_rank(pile[top_from-RANK_K]) == RANK_K) {
352 for(int i=top_from, j=0; i>top_from-NUM_RANKS; i--,j++){
353 f.f[f.w][j] = pile[i];
354 pile[i] = NO_CARD;
355 }
356 undo_push(pileno, FOUNDATION, f.w,
357 turn_over(pile));
358 f.w++;
359 return 1;
360 }
361 }
362
363 return 0;
364 }
365 int t2t(int from, int to, int opt) { //in dire need of cleanup
366 int top_from = find_top(f.t[from]);
367 int top_to = find_top(f.t[to]);
368 int empty_to = (top_to < 0)? opt: -1; /* empty pile? */
369 int count = 0; //NOTE: could probably be factored out
370
371 for (int i = top_from; i >= 0; i--) {
372 if (!is_consecutive(f.t[from], i)) break;
373
374 /* is consecutive OR to empty pile and rank ok? */
375 if (rank_next(f.t[from][i], f.t[to][top_to])
376 || (empty_to >= RANK_A && get_rank(f.t[from][i]) == empty_to)) {
377 for (;i <= top_from; i++) {
378 top_to++;
379 f.t[to][top_to] = f.t[from][i];
380 f.t[from][i] = NO_CARD;
381 count++;
382 }
383 undo_push(from, to, count,
384 turn_over(f.t[from]));
385 remove_if_complete(to);
386 if (check_won()) return WON;
387 return OK;
388 }
389 }
390
391 return ERR; /* no such move possible */
392 }
393 int s2t(int from, int to, int opt) {
394 (void) from; (void) to; (void) opt; /* don't need */
395 if (f.z <= 0) return ERR; /* stack out of cards */
396 for (int pile = 0; pile < NUM_PILES; pile++)
397 if (f.t[pile][0]==NO_CARD) return ERR; /*no piles may be empty*/
398 for (int pile = 0; pile < NUM_PILES; pile++) {
399 f.t[pile][find_top(f.t[pile])+1] = f.s[--f.z];
400 remove_if_complete(pile);
401 if (check_won()) return WON;
402 }
403 undo_push(STOCK, TABLEU, 1, 0);/*NOTE: puts 1 card on each tableu pile*/
404 return OK;
405 }
406 int t2f(int from, int to, int opt) {
407 (void) to; (void) opt; /* don't need */
408 /* manually retrigger remove_if_complete() (e.g. after undo_pop) */
409 return remove_if_complete(from)?OK:ERR;
410 }
411 #elif defined FREECELL
412 int max_move(int from, int to) {
413 /* returns the maximum number of cards that can be moved */
414 /* see also: https://boardgames.stackexchange.com/a/45157/26498 */
415 int free_tabs = 0, free_cells = 0;
416 for (int i = 0; i < NUM_PILES; i++) free_tabs += f.t[i][0] == NO_CARD;
417 for (int i = 0; i < NUM_CELLS; i++) free_cells += f.s[i] == NO_CARD;
418
419 /* don't count the tableau we are moving to: */
420 if (to >= 0 && f.t[to][0] == NO_CARD) free_tabs--;
421
422 /* theoretic maximum is limited by the number of cards on the pile */
423 int max_theory = (1<<free_tabs) * (free_cells + 1);
424 int max_effective = 1 + find_top(f.t[from]) - first_movable(f.t[from]);
425 return max_effective < max_theory? max_effective : max_theory;
426 }
427 //TODO FREECELL: auto move to tableu after each move (not all cards possible, only when it is the smallest rank still on the board)
428 int t2t(int from, int to, int opt) {
429 int top_to = find_top(f.t[to]);
430 int top_from = find_top(f.t[from]);
431 int count = 0; //NOTE: could probably be factored out
432 int cards = max_move(from, to);
433 if (top_to < 0) { /* moving to empty pile? */
434 if (opt > cards)
435 return ERR; /* cannot execute move */
436 cards = opt; /* user wants to move n cards*/
437 }
438
439 for (int i = top_from; i >=0; i--) {
440 if (cards-->0/*enough space and not more attempted than wanted*/
441 && ((top_to >= 0 /* if destn. not empty: check rank/color */
442 && ((get_color(f.t[to][top_to]) != get_color(f.t[from][i]))
443 && (rank_next(f.t[from][i], f.t[to][top_to]))))
444 || (top_to < 0 && !cards))) {/*if dest empty and right # cards*/
445 /* move cards [i..top_from] to their destination */
446 for (;i <= top_from; i++) {
447 top_to++;
448 f.t[to][top_to] = f.t[from][i];
449 f.t[from][i] = NO_CARD;
450 count++;
451 }
452 undo_push(from, to, count, 0);
453 return OK;
454 }
455 }
456 return ERR; /* no such move possible */
457 }
458 int t2f(int from, int to, int opt) { /* 1:1 copy from KLONDIKE */
459 (void) to; (void) opt; /* don't need */
460 int top_from = find_top(f.t[from]);
461 to = get_suit(f.t[from][top_from]);
462 int top_to = find_top(f.f[to]);
463 if ((top_to < 0 && get_rank(f.t[from][top_from]) == RANK_A)
464 || (top_to >= 0 && rank_next(f.f[to][top_to],f.t[from][top_from]))) {
465 f.f[to][top_to+1] = f.t[from][top_from];
466 f.t[from][top_from] = NO_CARD;
467 undo_push(from, FOUNDATION, to, 0);
468 if (check_won()) return WON;
469 return OK;
470 } else return ERR;
471 }
472 int f2t(int from, int to, int opt) {
473 (void) from; /* don't need */
474 int top_to = find_top(f.t[to]);
475 from = opt;
476 int top_from = find_top(f.f[from]);
477
478 if (top_to < 0 /* empty tableu? */
479 ||((get_color(f.t[to][top_to]) != get_color(f.f[from][top_from]))
480 && (rank_next(f.f[from][top_from], f.t[to][top_to])))) {
481 f.t[to][top_to+1] = f.f[from][top_from];
482 f.f[from][top_from] = NO_CARD;
483 undo_push(FOUNDATION, to, from, 0);
484 return OK;
485 } else return ERR;
486 }
487 int t2c(int from, int to, int opt) {
488 (void) to; (void) opt; /* don't need */
489 /* is a cell free? */
490 if (f.w == (1<<NUM_CELLS)-1)
491 return ERR;
492 for (to = 0; to < NUM_CELLS; to++)
493 if (!(f.w>>to&1)) break;
494 /* move 1 card */
495 int top_from = find_top(f.t[from]);
496 f.s[to] = f.t[from][top_from];
497 f.t[from][top_from] = NO_CARD;
498 f.w |= 1<<to; /* mark cell as occupied */
499 undo_push(from, STOCK, to, 0);
500
501 return OK;
502 }
503 int c2t(int from, int to, int opt) {
504 (void) from; /* don't need */
505 int top_to = find_top(f.t[to]);
506 from = opt;
507
508 if (top_to < 0 /* empty tableu? */
509 ||((get_color(f.t[to][top_to]) != get_color(f.s[from]))
510 && (rank_next(f.s[from], f.t[to][top_to])))) {
511 f.t[to][top_to+1] = f.s[from];
512 f.s[from] = NO_CARD;
513 f.w &= ~(1<<from); /* mark cell as free */
514 undo_push(STOCK, to, from, 0);
515 return OK;
516 } else return ERR;
517 return ERR;
518 }
519 int c2f(int from, int to, int opt) {
520 (void) from; (void) to; /* don't need */
521 from = opt;
522 to = get_suit(f.s[from]);
523 int top_to = find_top(f.f[to]);
524 if ((top_to < 0 && get_rank(f.s[from]) == RANK_A)
525 || (top_to >= 0 && rank_next(f.f[to][top_to],f.s[from]))) {
526 f.f[to][top_to+1] = f.s[from];
527 f.s[from] = NO_CARD;
528 f.w &= ~(1<<from); /* mark cell as free */
529 undo_push(STOCK, FOUNDATION, from | to<<16, 0);
530 if (check_won()) return WON;
531 return OK;
532 } else return ERR;
533 }
534 int f2c(int from, int to, int opt) {
535 (void) from; (void) to; /* don't need */
536 /* is a cell free? */
537 if (f.w == (1<<NUM_CELLS)-1)
538 return ERR;
539 for (to = 0; to < NUM_CELLS; to++)
540 if (!(f.w>>to&1)) break;
541 /* move 1 card */
542 from = opt;
543 int top_from = find_top(f.f[from]);
544 f.s[to] = f.f[from][top_from];
545 f.f[from][top_from] = NO_CARD;
546 f.w |= 1<<to; /* mark cell as occupied */
547 undo_push(FOUNDATION, STOCK, from | to<<16, 0);
548
549 return OK;
550 }
551 #endif
552
553 //TODO: generalize prediction engine for CMD_HINT
554 #ifdef KLONDIKE
555 #define would_complete(pile) 0
556 #elif defined SPIDER
557 #define would_complete(pile) \
558 (get_rank(f.t[pile][r[pile].top]) == RANK_A \
559 && get_rank(f.t[to][bottom_to]) == RANK_K)
560 #elif defined FREECELL
561 #define would_complete(pile) 0
562 #endif
563 #define would_turn(pile) \
564 (f.t[pile][r[pile].pos-1] < 0)
565 #define would_empty(pile) \
566 (r[pile].pos == 0)
567
568 int join(int to) {
569 int top_to = find_top(f.t[to]);
570 #ifdef SPIDER
571 int bottom_to = first_movable(f.t[to]);
572 #endif
573
574 #ifdef KLONDIKE
575 if (to == WASTE || to == STOCK) return ERR; /*why would you do that!?*/
576
577 if (to == FOUNDATION) {
578 int status = ERR;
579 for (int i = 0; i <= TAB_MAX; i++)
580 switch ((i?t2f:w2f)(i-1, FOUNDATION, 0)) {
581 case WON: return WON;
582 case OK: status = OK;
583 case ERR: /* nop */;
584 }
585 return status;
586 }
587
588 if (top_to < 0) { /* move a king to empty pile: */
589 for (int i = 0; i < TAB_MAX; i++) {
590 if (f.t[i][0] < 0) /* i.e. would turn? */
591 if (t2t(i, to, 0) == OK) return OK;
592 }
593 return w2t(WASTE, to, 0);
594 }
595 #endif
596
597 struct rating {
598 int ok:1; /* card to move in pile? */
599 int above; /* number of movable cards above */
600 int below; /* number of cards below ours */
601 int pos; /* where the card to move is in the pile */
602 int top; /* find_top() */
603 } r[NUM_PILES] = {{0}};
604 int complete = 0;/* SPIDER: true if any pile would complete a stack */
605 int turn = 0; /* SPIDER: true if any pile would turn_over */
606 int empty = 0; /* true if any pile would become empty */
607
608 /* 1. rate each pile: */
609 #ifdef SPIDER
610 if (top_to < 0) {
611 for (int pile = 0; pile < NUM_PILES; pile++) {
612 if (pile == to) continue;
613 int top = find_top(f.t[pile]);
614 int bottom = first_movable(f.t[pile]);
615 r[pile].pos = bottom; /* need for would_empty */
616
617 if (top < 0) continue; /* no cards to move */
618 if (would_empty(pile)) continue; /* doesn't help */
619
620 r[pile].ok++;
621 r[pile].above = 0; /* always take as many as possible */
622 r[pile].below = top - bottom;
623 r[pile].top = top;
624 complete |= would_complete(pile); /* never happens */
625 turn |= would_turn(pile);
626 empty |= would_empty(pile);
627 }
628 } else
629 #endif
630 for (int pile = 0; pile < NUM_PILES; pile++) {
631 r[pile].top = r[pile].pos = find_top(f.t[pile]);
632 /* backtrack until we find a compatible-to-'to'-pile card: */
633 while (r[pile].pos >= 0 && is_movable(f.t[pile], r[pile].pos)) {
634 int rankdiff = get_rank(f.t[pile][r[pile].pos])
635 - get_rank(f.t[to][top_to]);
636 if (rankdiff >= 0) break; /* past our card */
637 if (rankdiff == -1 /* rank matches */
638 #ifdef KLONDIKE
639 && get_color(f.t[pile][r[pile].pos]) /* color OK */
640 != get_color(f.t[to][top_to])
641 #elif defined SPIDER
642 && get_suit(f.t[pile][r[pile].pos]) /* color OK */
643 == get_suit(f.t[to][top_to])
644 #endif
645 ) {
646 r[pile].ok++;
647 complete |= would_complete(pile);
648 turn |= would_turn(pile);
649 empty |= would_empty(pile);
650 for (int i = r[pile].pos; i >= 0; i--)
651 if (is_movable(f.t[pile], i-1))
652 r[pile].above++;
653 else break;
654 break;
655 }
656 r[pile].pos--;
657 r[pile].below++;
658 }
659 }
660
661 /* 2. find optimal pile: (optimized for spider) */
662 //todo: in spider, prefer longest piles if above==0 (faster completions)
663 int from = -1;
664 for (int pile = 0, above = 99, below = 99; pile < NUM_PILES; pile++) {
665 if (!r[pile].ok) continue;
666 /* don't bother if another pile would be better: prefer ... */
667 /* ... to complete a stack: */
668 if (!would_complete(pile) && complete) continue;
669 /* ... emptying piles: */
670 if (!would_empty(pile) && empty && !complete) continue;
671 /* ... to turn_over: */
672 if (!would_turn(pile) && turn && !complete && !empty) continue;
673 /* ... not to rip apart too many cards: */
674 if (r[pile].above > above) continue;
675 /* if tied, prefer ... */
676 if (r[pile].above == above
677 /* ... larger pile if destination is empty */
678 && (top_to < 0? r[pile].below < below
679 /* ... shorter pile otherwise */
680 : r[pile].below > below))
681 continue;
682
683 from = pile;
684 above = r[pile].above;
685 below = r[pile].below;
686 }
687
688 /* 3. move cards over and return: */
689 #ifdef KLONDIKE
690 /* prefer waste if it wouldn't turn_over: */
691 /* NOTE: does not attempt to take from froundation */
692 if (!turn && w2t(WASTE, to, 0) == OK) //TODO: gives higher priority to waste than to empty!
693 return OK;
694 if (from < 0) /* nothing found */
695 return ERR;
696 return t2t(from, to, 0);
697 #elif defined SPIDER
698 if (from < 0) /* nothing found */
699 return ERR;
700 int bottom = first_movable(f.t[from]);
701 return t2t(from, to, get_rank(f.t[from][bottom]));
702 #elif defined FREECELL
703 (void)from;
704 return ERR; //TODO FREECELL: implement join
705 #endif
706 }
707 #undef would_empty
708 #undef would_turn
709 #undef would_complete
710 int nop(int from, int to, int opt) { (void)from;(void)to;(void)opt;return ERR; }
711 // }}}
712
713 // keyboard input handling {{{
714 // cursor functions{{{
715 #ifdef KLONDIKE
716 void cursor_left (struct cursor* cursor) {
717 op.h = 1;
718 if (is_tableu(cursor->pile)) {
719 if (cursor->pile > 0) cursor->pile--;
720 cursor->opt = 0;
721 } else { /* stock/waste/foundation*/
722 switch (cursor->pile) {
723 case WASTE: cursor->pile = STOCK; cursor->opt = 0; break;
724 case FOUNDATION:
725 if (cursor->opt <= 0)
726 cursor->pile = WASTE;
727 else
728 cursor->opt--;
729 }
730 }
731 }
732 void cursor_down (struct cursor* cursor) {
733 op.h = 1;
734 if (!is_tableu(cursor->pile)) {
735 switch (cursor->pile) {
736 case STOCK: cursor->pile = TAB_1; break;
737 case WASTE: cursor->pile = TAB_2; break;
738 case FOUNDATION:
739 cursor->pile = TAB_4 + cursor->opt;
740 }
741 cursor->opt = 0;
742 }
743 }
744 void cursor_up (struct cursor* cursor) {
745 op.h = 1;
746 if (is_tableu(cursor->pile)) {
747 switch (cursor->pile) { //ugly :|
748 case TAB_1: cursor->pile = STOCK; break;
749 case TAB_2: cursor->pile = WASTE; break;
750 case TAB_3: cursor->pile = WASTE; break;
751 case TAB_4: case TAB_5: case TAB_6: case TAB_7:
752 cursor->opt=cursor->pile-TAB_4;
753 cursor->pile = FOUNDATION;
754 break;
755 }
756 }
757 }
758 void cursor_right (struct cursor* cursor) {
759 op.h = 1;
760 if (is_tableu(cursor->pile)) {
761 if (cursor->pile < TAB_MAX) cursor->pile++;
762 cursor->opt = 0;
763 } else {
764 switch (cursor->pile) {
765 case STOCK: cursor->pile = WASTE; break;
766 case WASTE: cursor->pile = FOUNDATION;cursor->opt = 0; break;
767 case FOUNDATION:
768 if (cursor->opt < NUM_SUITS-1)
769 cursor->opt++;
770 }
771 }
772 }
773 #elif defined SPIDER
774 /*NOTE: one can't highlight the stock due to me being too lazy to implement it*/
775 void cursor_left (struct cursor* cursor) {
776 op.h = 1;
777 if (cursor->pile > 0) cursor->pile--;
778 cursor->opt = 0;
779 }
780 void cursor_down (struct cursor* cursor) {
781 op.h = 1;
782 int first = first_movable(f.t[cursor->pile]);
783 int top = find_top(f.t[cursor->pile]);
784 if (first + cursor->opt < top)
785 cursor->opt++;
786 }
787 void cursor_up (struct cursor* cursor) {
788 op.h = 1;
789 if (cursor->opt > 0) cursor->opt--;
790 }
791 void cursor_right (struct cursor* cursor) {
792 op.h = 1;
793 if (cursor->pile < TAB_MAX) cursor->pile++;
794 cursor->opt = 0;
795 }
796 #elif defined FREECELL
797 void cursor_left (struct cursor* cursor) {
798 op.h = 1;
799 if (is_tableu(cursor->pile)) {
800 if (cursor->pile > 0) cursor->pile--;
801 cursor->opt = 0;
802 } else { /* cells/foundation*/
803 switch (cursor->pile) {
804 case STOCK:
805 if (cursor->opt > 0)
806 cursor->opt--;
807 break;
808 case FOUNDATION:
809 if (cursor->opt <= 0) {
810 cursor->pile = STOCK;
811 cursor->opt = 3;
812 } else {
813 cursor->opt--;
814 }
815 }
816 }
817 }
818 void cursor_down (struct cursor* cursor) {
819 op.h = 1;
820 if (is_tableu(cursor->pile)) {
821 if (cursor->opt < max_move(cursor->pile, -1)-1)
822 cursor->opt++;
823 } else {
824 cursor->pile = cursor->opt+NUM_CELLS*(cursor->pile==FOUNDATION);
825 cursor->opt = 0;
826 }
827 }
828 void cursor_up (struct cursor* cursor) {
829 op.h = 1;
830 if (is_tableu(cursor->pile)) {
831 if (cursor->opt > 0) {
832 cursor->opt--;
833 } else {
834 switch (cursor->pile) {
835 case TAB_1: case TAB_2: case TAB_3: case TAB_4:
836 cursor->opt = cursor->pile; /*assumes TAB_1==0*/
837 cursor->pile = STOCK;
838 break;
839 case TAB_5: case TAB_6: case TAB_7: case TAB_8:
840 cursor->opt = cursor->pile - NUM_CELLS;
841 cursor->pile = FOUNDATION;
842 }
843 }
844 }
845 }
846 void cursor_right (struct cursor* cursor) {
847 op.h = 1;
848 if (is_tableu(cursor->pile)) {
849 if (cursor->pile < TAB_MAX) cursor->pile++;
850 cursor->opt = 0;
851 } else {
852 switch (cursor->pile) {
853 case STOCK:
854 if (cursor->opt < NUM_SUITS-1) {
855 cursor->opt++;
856 } else {
857 cursor->pile = FOUNDATION;
858 cursor->opt = 0;
859 } break;
860 case FOUNDATION:
861 if (cursor->opt < NUM_SUITS-1)
862 cursor->opt++;
863 }
864 }
865 }
866 #endif
867 void cursor_to (struct cursor* cursor, int pile) {
868 op.h = 1;
869 cursor->pile = pile;
870 cursor->opt = 0;
871 }
872 int set_mouse(int pile, int* main, int* opt) {
873 //TODO: this should set cursor.opt, so card selector choice dialog does not trigger!
874 op.h = 0;
875 if (pile < 0) return 1;
876 *main = pile;
877 #ifdef KLONDIKE
878 if (pile >= FOUNDATION)//TODO: check upper bound!
879 *main = FOUNDATION,
880 *opt = pile - FOUNDATION;
881 #elif defined SPIDER
882 (void)opt;
883 #elif defined FREECELL
884 if (pile > TAB_MAX) {
885 *main = pile-STOCK < NUM_CELLS? STOCK : FOUNDATION;
886 *opt = (pile-STOCK) % 4;
887 }
888 #endif
889 return 0;
890 }
891 //}}}
892 int get_cmd (int* from, int* to, int* opt) {
893 int _f, t;
894 unsigned char mouse[6] = {0}; /* must clear [3]! */
895 struct cursor inactive = {-1,-1};
896 static struct cursor active = {0,0};
897 if (is_tableu(active.pile))
898 active.opt = 0;
899
900 /***/
901 from_l: print_table(&active, &inactive);
902 _f = getch(mouse);
903
904 switch (_f) {
905 /* direct addressing: */
906 case '1': *from = TAB_1; break;
907 case '2': *from = TAB_2; break;
908 case '3': *from = TAB_3; break;
909 case '4': *from = TAB_4; break;
910 case '5': *from = TAB_5; break;
911 case '6': *from = TAB_6; break;
912 case '7': *from = TAB_7; break;
913 #ifdef SPIDER
914 case '8': *from = TAB_8; break;
915 case '9': *from = TAB_9; break;
916 case '0': *from = TAB_10;break;
917 #elif defined FREECELL
918 case '8': *from = TAB_8; break;
919 case '9': *from = STOCK; break;
920 case '0': *from = FOUNDATION; break;
921 #elif defined KLONDIKE
922 case '9': *from = WASTE; break;
923 case '0': *from = FOUNDATION; break;
924 case '8': /* fallthrough */
925 #endif
926 #ifndef FREECELL
927 case '\n': *from = STOCK; break;
928 #endif
929 /* cursor keys addressing: */
930 case KEY_LEFT:
931 case 'h': cursor_left (&active); goto from_l;
932 case KEY_DOWN:
933 case 'j': cursor_down (&active); goto from_l;
934 case KEY_UP:
935 case 'k': cursor_up (&active); goto from_l;
936 case KEY_RIGHT:
937 case 'l': cursor_right(&active); goto from_l;
938 case KEY_HOME:
939 case 'H': cursor_to(&active,TAB_1); goto from_l; /* leftmost tableu */
940 case KEY_END:
941 case 'L': cursor_to(&active,TAB_MAX);goto from_l; /* rigthmost tableu */
942 case KEY_INS:
943 case 'M': cursor_to(&active,TAB_MAX/2); goto from_l; /* center tableu */
944 case ' ': /* continue with second cursor */
945 *from = active.pile;
946 #ifdef KLONDIKE
947 *opt = active.opt; /* when FOUNDATION */
948 #endif
949 inactive = active;
950 break;
951 /* mouse addressing: */
952 case MOUSE_MIDDLE: return CMD_NONE;
953 case MOUSE_RIGHT:
954 if (set_mouse(term2pile(mouse), to, opt))
955 return CMD_INVAL;
956 goto join_l;
957 case MOUSE_LEFT:
958 if (set_mouse(term2pile(mouse), from, opt))
959 return CMD_INVAL;
960 if (!is_tableu(*from))
961 inactive.opt = *opt; /* prevents card selector dialog */
962 break;
963 /* misc keys: */
964 case ':':
965 {char buf[256];
966 fprintf (stderr, ":");
967 raw_mode(0); /* turn on echo */
968 fgets (buf, 256, stdin);
969 raw_mode(1);
970 switch(buf[0]) {
971 case 'q': return CMD_QUIT;
972 case 'n': return CMD_NEW;
973 case 'r': return CMD_AGAIN;
974 case 'h': return CMD_HELP;
975 default: return CMD_INVAL;
976 }}
977 case 'J':
978 *to = active.pile;
979 join_l:
980 #ifdef KLONDIKE
981 if (*to == FOUNDATION) return CMD_JOIN;
982 #endif
983 if (*to > TAB_MAX) return CMD_INVAL;
984 return CMD_JOIN;
985 case 'K': /* fallthrough */
986 case '?': return CMD_HINT;
987 case 'u': return CMD_UNDO;
988 case 002: return CMD_NONE; /* sent by SIGWINCH */
989 case EOF: return CMD_NONE; /* sent by SIGCONT */
990 default: return CMD_INVAL;
991 }
992 inactive.pile = *from; /* for direct addressing highlighting */
993 if (is_tableu(*from) && f.t[*from][0] == NO_CARD) return CMD_INVAL;
994
995 #ifndef FREECELL
996 if (*from == STOCK) {
997 *to = WASTE;
998 return CMD_MOVE;
999 }
1000 #endif
1001
1002 /***/
1003 to_l: print_table(&active, &inactive);
1004 t = getch(mouse);
1005
1006 switch (t) {
1007 case KEY_LEFT:
1008 case 'h': cursor_left (&active); goto to_l;
1009 case KEY_DOWN:
1010 case 'j': cursor_down (&active); goto to_l;
1011 case KEY_UP:
1012 case 'k': cursor_up (&active); goto to_l;
1013 case KEY_RIGHT:
1014 case 'l': cursor_right(&active); goto to_l;
1015 case KEY_HOME:
1016 case 'H': cursor_to(&active,TAB_1); goto to_l;
1017 case KEY_END:
1018 case 'L': cursor_to(&active,TAB_MAX); goto to_l;
1019 case KEY_INS:
1020 case 'M': cursor_to(&active,TAB_MAX/2); goto to_l;
1021 case 'J': /* fallthrough; just join selected pile */
1022 case ' ':
1023 *to = active.pile;
1024 break; /* continues with the foundation/empty tableu check */
1025 case MOUSE_MIDDLE:
1026 case MOUSE_RIGHT: return CMD_NONE;
1027 case MOUSE_LEFT:
1028 if (set_mouse(term2pile(mouse), to, opt))
1029 return CMD_INVAL;
1030 /*#ifdef SPIDER
1031 //TODO: set opt if to field is empty; suppress "up do" dialog from below
1032 if (is_tableu(*to) && f.t[*to][0] == NO_CARD) {
1033 int top = find_top(f.t[*from]);
1034 if (top < 0) return CMD_INVAL;
1035 if (top >= 0 && !is_movable(f.t[*from], top-1)) {
1036 *opt = get_rank(f.t[*from][top]);
1037 } else {
1038 // ask user
1039 }
1040 }
1041 #endif*/
1042 break;
1043 case 'K': /* fallthrough */
1044 case '?': return CMD_HINT;
1045 case 'u': return CMD_NONE; /* cancel selection */
1046 case EOF: return CMD_NONE; /* sent by SIGCONT */
1047 default:
1048 if (t < '0' || t > '9') return CMD_INVAL;
1049 if (t == '0')
1050 #ifdef KLONDIKE
1051 *to = FOUNDATION;
1052 #elif defined SPIDER
1053 *to = TAB_10;
1054 #elif defined FREECELL
1055 *to = FOUNDATION;
1056 else if (t == '9')
1057 *to = STOCK;
1058 #endif
1059 else
1060 *to = t-'1';
1061 }
1062
1063 /***/
1064 #ifdef KLONDIKE
1065 if (*from == FOUNDATION) {
1066 if (inactive.opt >= 0) {
1067 *opt = inactive.opt;
1068 return CMD_MOVE;
1069 }
1070 int top = find_top(f.t[*to]);
1071 if (top < 0) return CMD_INVAL;
1072 int color = get_color(f.t[*to][top]);
1073 int choice_1 = 1-color; /* selects piles of */
1074 int choice_2 = 2+color; /* the opposite color */
1075 int top_c1 = find_top(f.f[choice_1]);
1076 int top_c2 = find_top(f.f[choice_2]);
1077
1078 switch ((rank_next(f.f[choice_1][top_c1], f.t[*to][top])
1079 && top_c1 >= 0 ) << 0
1080 |(rank_next(f.f[choice_2][top_c2], f.t[*to][top])
1081 && top_c2 >= 0 ) << 1) {
1082 case ( 1<<0): *opt = choice_1; break; /* choice_1 only */
1083 case (1<<1 ): *opt = choice_2; break; /* choice_2 only */
1084 case (1<<1 | 1<<0): /* both, ask user which to pick from */
1085 printf ("take from (1-4): "); fflush (stdout);
1086 *opt = getch(NULL) - '1';
1087 if (*opt < 0 || *opt > 3) return CMD_INVAL;
1088 break;
1089 default: return CMD_INVAL; /* none matched */
1090 }
1091 /* `opt` is the foundation index (0..3) */
1092 }
1093 #elif defined SPIDER
1094 /* moving to empty tableu? */
1095 if (is_tableu(*to) && f.t[*to][0] == NO_CARD) {
1096 int bottom = first_movable(f.t[*from]);
1097 if (inactive.opt >= 0) { /*if from was cursor addressed: */
1098 *opt = get_rank(f.t[*from][bottom + inactive.opt]);
1099 return CMD_MOVE;
1100 }
1101 int top = find_top(f.t[*from]);
1102 if (top < 0) return CMD_INVAL;
1103 if (top >= 0 && !is_movable(f.t[*from], top-1)) {
1104 *opt = get_rank(f.t[*from][top]);
1105 } else { /* only ask the user if it's unclear: */
1106 printf ("\rup to ([a23456789xjqk] or space/return): ");
1107 *opt = getch(NULL);
1108 switch (*opt) {
1109 case ' ': *opt = get_rank(f.t[*from][top]); break;
1110 case'\n': *opt = get_rank(f.t[*from][bottom]); break;
1111 case 'a': case 'A': *opt = RANK_A; break;
1112 case '0': /* fallthrough */
1113 case 'x': case 'X': *opt = RANK_X; break;
1114 case 'j': case 'J': *opt = RANK_J; break;
1115 case 'q': case 'Q': *opt = RANK_Q; break;
1116 case 'k': case 'K': *opt = RANK_K; break;
1117 default: *opt -= '1';
1118 }
1119 if (*opt < RANK_A || *opt > RANK_K) return ERR;
1120 }
1121 /* `opt` is the rank of the highest card to move */
1122 }
1123 #elif defined FREECELL
1124 //TODO FREECELL: card selector choice dialog
1125
1126 /* if it was selected with a cursor, it's obvious: */
1127 if (inactive.opt >= 0) {
1128 if (is_tableu(*from)) {
1129 /* NOTE: max_move same as in cursor_down() */
1130 *opt = max_move(*from, -1) - inactive.opt;
1131 } else {
1132 *opt = inactive.opt;
1133 }
1134 /* moving from tableu to empty tableu? */
1135 } else if (is_tableu(*from) && is_tableu(*to) && f.t[*to][0] == NO_CARD) {
1136 // how many cards? (NOTE: spider asks "up to rank?"; do this then convert to number of cards?
1137 printf ("take how many (1-9): "); fflush (stdout);
1138 *opt = getch(NULL) - '0';
1139 if (*opt < 1 || *opt > 9) return CMD_INVAL;
1140 /* moving between stock/foundation? */
1141 } else if (*from == FOUNDATION && *to == STOCK) {
1142 //can take from all non-empty foundations
1143 printf ("take from (1-4): "); fflush (stdout);
1144 *opt = getch(NULL) - '1';
1145 if (*opt < 0 || *opt > 3) return CMD_INVAL;
1146 } else if (*from == STOCK && *to == FOUNDATION) {
1147 //check all non-empty cells
1148 printf ("take from (1-4): "); fflush (stdout);
1149 *opt = getch(NULL) - '1';
1150 if (*opt < 0 || *opt > 3) return CMD_INVAL;
1151 } else if (*from == FOUNDATION || *from == STOCK) { /* -> tableu */
1152 //foundation: 2 choices
1153 //stock: 4 choices
1154 printf ("take from (1-4): "); fflush (stdout);
1155 *opt = getch(NULL) - '1';
1156 if (*opt < 0 || *opt > 3) return CMD_INVAL;
1157 }
1158 #endif
1159 return CMD_MOVE;
1160 }
1161
1162 int getctrlseq(unsigned char* buf) {
1163 int c;
1164 enum esc_states {
1165 START,
1166 ESC_SENT,
1167 CSI_SENT,
1168 MOUSE_EVENT,
1169 } state = START;
1170 int offset = 0x20; /* never sends control chars as data */
1171 while ((c = getchar()) != EOF) {
1172 switch (state) {
1173 case START:
1174 switch (c) {
1175 case '\033': state=ESC_SENT; break;
1176 default: return c;
1177 }
1178 break;
1179 case ESC_SENT:
1180 switch (c) {
1181 case '[': state=CSI_SENT; break;
1182 default: return KEY_INVAL;
1183 }
1184 break;
1185 case CSI_SENT:
1186 switch (c) {
1187 case 'A': return KEY_UP;
1188 case 'B': return KEY_DOWN;
1189 case 'C': return KEY_RIGHT;
1190 case 'D': return KEY_LEFT;
1191 /*NOTE: home/end send ^[[x~ . no support for modifiers*/
1192 case 'H': return KEY_HOME;
1193 case 'F': return KEY_END;
1194 case '2': getchar(); return KEY_INS;
1195 case '5': getchar(); return KEY_PGUP;
1196 case '6': getchar(); return KEY_PGDN;
1197 case 'M': state=MOUSE_EVENT; break;
1198 default: return KEY_INVAL;
1199 }
1200 break;
1201 case MOUSE_EVENT:
1202 if (buf == NULL) return KEY_INVAL;
1203 buf[0] = c - offset;
1204 buf[1] = getchar() - offset;
1205 buf[2] = getchar() - offset;
1206 return MOUSE_ANY;
1207 default:
1208 return KEY_INVAL;
1209 }
1210 }
1211 return 2;
1212 }
1213 int term2pile(unsigned char *mouse) {
1214 int line = (mouse[2]-1);
1215 int column = (mouse[1]-1) / op.s->width;
1216
1217 if (line < op.s->height) { /* first line */
1218 #ifdef KLONDIKE
1219 switch (column) {
1220 case 0: return STOCK;
1221 case 1: return WASTE;
1222 case 2: return -1; /* spacer */
1223 case 3: return FOUNDATION+0;
1224 case 4: return FOUNDATION+1;
1225 case 5: return FOUNDATION+2;
1226 case 6: return FOUNDATION+3;
1227 }
1228 #elif defined SPIDER
1229 if (column < 3) return STOCK;
1230 return -1;
1231 #elif defined FREECELL
1232 if (column < NUM_SUITS + NUM_CELLS) return STOCK+column;
1233 return -1;
1234 #endif
1235 } else if (line > op.s->height) { /* tableu */
1236 if (column <= TAB_MAX) return column;
1237 }
1238 return -1;
1239 }
1240 int wait_mouse_up(unsigned char* mouse) {
1241 //TODO: mouse drag: start gets inactive, hovering gets active cursors
1242 struct cursor cur = {-1,-1};
1243 int level = 1;
1244 /* note: if dragged [3]==1 and second position is in mouse[0,4,5] */
1245
1246 /* display a cursor while mouse button is pushed: */
1247 int pile = term2pile(mouse);
1248 cur.pile = pile;
1249 #ifdef KLONDIKE
1250 if (pile >= FOUNDATION) {
1251 cur.pile = FOUNDATION;
1252 cur.opt = pile-FOUNDATION;
1253 }
1254 #elif defined FREECELL
1255 if (pile > TAB_MAX) {
1256 cur.pile = pile-STOCK < NUM_CELLS? STOCK : FOUNDATION;
1257 cur.opt = (pile-STOCK) % 4;
1258 }
1259 #endif
1260 /* need to temporarily show the cursor, then revert to last state: */
1261 int old_show_cursor_hi = op.h; //TODO: ARGH! that's awful!
1262 op.h = 1;
1263 print_table(&cur, NO_HI); //TODO: should not overwrite inactive cursor!
1264 op.h = old_show_cursor_hi;
1265
1266 while (level > 0) {
1267 if (getctrlseq (mouse+3) == MOUSE_ANY) {
1268 /* ignore mouse wheel events: */
1269 if (mouse[3] & 0x40) continue;
1270
1271 else if((mouse[3]&3) == 3) level--; /* release event */
1272 else level++; /* another button pressed */
1273 }
1274 }
1275
1276 int success = mouse[1] == mouse[4] && mouse[2] == mouse[5];
1277 if (success) {
1278 mouse[3] = 0;
1279 }
1280 return success;
1281 }
1282
1283 int getch(unsigned char* buf) {
1284 /* returns a character, EOF, or constant for an escape/control sequence - NOT
1285 compatible with the ncurses implementation of same name */
1286 int action;
1287 if (buf && buf[3]) {
1288 /* mouse was dragged; return 'ungetted' previous destination */
1289 action = MOUSE_DRAG;
1290 /* keep original [0], as [3] only contains release event */
1291 buf[1] = buf[4];
1292 buf[2] = buf[5];
1293 buf[3] = 0;
1294 } else {
1295 action = getctrlseq(buf);
1296 }
1297
1298 switch (action) {
1299 case MOUSE_ANY:
1300 if (buf[0] > 3) break; /* ignore wheel events */
1301 wait_mouse_up(buf);
1302 /* fallthrough */
1303 case MOUSE_DRAG:
1304 switch (buf[0]) {
1305 case 0: return MOUSE_LEFT;
1306 case 1: return MOUSE_MIDDLE;
1307 case 2: return MOUSE_RIGHT;
1308 }
1309 }
1310
1311 return action;
1312 }
1313 // }}}
1314
1315 // shuffling and dealing {{{
1316 void deal(long seed) {
1317 f = (const struct playfield){0}; /* clear playfield */
1318 card_t deck[DECK_SIZE*NUM_DECKS];
1319 int avail = DECK_SIZE*NUM_DECKS;
1320 for (int i = 0; i < DECK_SIZE*NUM_DECKS; i++) deck[i] = (i%DECK_SIZE)+1;
1321 #ifdef SPIDER
1322 if (op.m != NORMAL) for (int i = 0; i < DECK_SIZE*NUM_DECKS; i++) {
1323 if (op.m == MEDIUM) deck[i] = 1+((deck[i]-1) | 2);
1324 if (op.m == EASY) deck[i] = 1+((deck[i]-1) | 2 | 1);
1325 /* the 1+ -1 dance gets rid of the offset created by NO_CARD */
1326 }
1327 #endif
1328 srand (seed);
1329 for (int i = DECK_SIZE*NUM_DECKS-1; i > 0; i--) { /* fisher-yates */
1330 int j = rand() % (i+1);
1331 if (j-i) deck[i]^=deck[j],deck[j]^=deck[i],deck[i]^=deck[j];
1332 }
1333
1334 /* deal cards: */
1335 for (int i = 0; i < NUM_PILES; i++) {
1336 #ifdef KLONDIKE
1337 #define SIGN -
1338 int count = i; /* pile n has n closed cards, then 1 open */
1339 #elif defined SPIDER
1340 #define SIGN -
1341 int count = i<4?5:4; /* pile 1-4 have 5, 5-10 have 4 closed */
1342 #elif defined FREECELL
1343 #define SIGN +
1344 int count = i<4?6:5;/*like spider, but cards are dealt face-up*/
1345 #endif
1346 /* "SIGN": face down cards are negated */
1347 for (int j = 0; j < count; j++) f.t[i][j] = SIGN deck[--avail];
1348 f.t[i][count] = deck[--avail]; /* the face-up card */
1349 #undef SIGN
1350 }
1351 /* rest of the cards to the stock: */
1352 /* NOTE: assert(avail==50) for spider, assert(avail==0) for freecell */
1353 for (f.z = 0; avail; f.z++) f.s[f.z] = deck[--avail];
1354 #ifdef KLONDIKE
1355 f.w = -1; /* @start: nothing on waste */
1356 #elif defined SPIDER
1357 f.w = 0; /* number of used foundations */
1358 #elif defined FREECELL
1359 f.w = 0; /* bitmask of used free cells */
1360 #endif
1361
1362 f.u = &undo_sentinel;
1363 }
1364 //}}}
1365
1366 // screen drawing routines {{{
1367 void print_hi(int invert, int grey_bg, int bold, char* str) {
1368 if (!op.h) invert = 0; /* don't show invert if we used the mouse last */
1369 if (bold && op.s == &unicode_large_color){ //awful hack for bold + faint
1370 int offset = str[3]==017?16:str[4]==017?17:0;
1371 printf ("%s%s%s""%.*s%s%s""%s%s%s",
1372 "\033[1m", invert?"\033[7m":"", grey_bg?"\033[100m":"",
1373 offset, str, bold?"\033[1m":"", str+offset,
1374 grey_bg?"\033[49m":"", invert?"\033[27m":"","\033[22m");
1375 return;
1376 }
1377 printf ("%s%s%s%s%s%s%s",
1378 bold?"\033[1m":"", invert?"\033[7m":"", grey_bg?"\033[100m":"",
1379 str,
1380 grey_bg?"\033[49m":"", invert?"\033[27m":"",bold?"\033[22m":"");
1381 }
1382 void print_table(const struct cursor* active, const struct cursor* inactive) {
1383 printf("\033[2J\033[H"); /* clear screen, reset cursor */
1384 #ifdef KLONDIKE
1385 /* print stock, waste and foundation: */
1386 for (int line = 0; line < op.s->height; line++) {
1387 /* stock: */
1388 print_hi (active->pile == STOCK, inactive->pile == STOCK, 1, (
1389 (f.w < f.z-1)?op.s->facedown
1390 :op.s->placeholder)[line]);
1391 /* waste: */
1392 print_hi (active->pile == WASTE, inactive->pile == WASTE, 1, (
1393 /* NOTE: cast, because f.w sometimes is (short)-1 !? */
1394 ((short)f.w >= 0)?op.s->card[f.s[f.w]]
1395 :op.s->placeholder)[line]);
1396 printf ("%s", op.s->card[NO_CARD][line]); /* spacer */
1397 /* foundation: */
1398 for (int pile = 0; pile < NUM_SUITS; pile++) {
1399 int card = find_top(f.f[pile]);
1400 print_hi (active->pile==FOUNDATION && active->opt==pile,
1401 inactive->pile==FOUNDATION && (
1402 /* cursor addr. || direct addr. */
1403 inactive->opt==pile || inactive->opt < 0
1404 ), 1,
1405 (card < 0)?op.s->foundation[line]
1406 :op.s->card[f.f[pile][card]][line]);
1407 }
1408 printf("\n");
1409 }
1410 printf("\n");
1411 #elif defined SPIDER
1412 int fdone; for (fdone = NUM_DECKS*NUM_SUITS; fdone; fdone--)
1413 if (f.f[fdone-1][RANK_K]) break; /*number of completed stacks*/
1414 int spacer_from = f.z?(f.z/10-1) * op.s->halfwidth[0] + op.s->width:0;
1415 int spacer_to = NUM_PILES*op.s->width -
1416 ((fdone?(fdone-1) * op.s->halfwidth[1]:0)+op.s->width);
1417 for (int line = 0; line < op.s->height; line++) {
1418 /* available stock: */
1419 for (int i = f.z/10; i; i--) {
1420 if (i==1) printf ("%s", op.s->facedown[line]);
1421 else printf ("%s", op.s->halfstack[line]);
1422 }
1423 /* spacer: */
1424 for (int i = spacer_from; i < spacer_to; i++) printf (" ");
1425 /* foundation (overlapping): */
1426 for (int i = NUM_DECKS*NUM_SUITS-1, half = 0; i >= 0; i--) {
1427 int overlap = half? op.s->halfcard[line]: 0;
1428 if (f.f[i][RANK_K]) printf ("%.*s", op.s->halfwidth[2],
1429 op.s->card[f.f[i][RANK_K]][line]+overlap),
1430 half++;
1431 }
1432 printf("\n");
1433 }
1434 printf("\n");
1435 #elif defined FREECELL
1436 /* print open cells, foundation: */
1437 for (int line = 0; line < op.s->height; line++) {
1438 for (int pile = 0; pile < NUM_CELLS; pile++)
1439 print_hi (active->pile==STOCK && active->opt==pile,
1440 inactive->pile==STOCK && (
1441 /* cursor addr. || direct addr. */
1442 inactive->opt==pile || inactive->opt < 0
1443 ), 1,
1444 ((f.s[pile])?op.s->card[f.s[pile]]
1445 :op.s->placeholder)[line]);
1446 for (int pile = 0; pile < NUM_SUITS; pile++) {
1447 int card = find_top(f.f[pile]);
1448 print_hi (active->pile==FOUNDATION && active->opt==pile,
1449 inactive->pile==FOUNDATION && (
1450 /* cursor addr. || direct addr. */
1451 inactive->opt==pile || inactive->opt < 0
1452 ), 1,
1453 (card < 0)?op.s->foundation[line]
1454 :op.s->card[f.f[pile][card]][line]);
1455 }
1456 printf("\n");
1457 }
1458 printf("\n");
1459 #endif
1460 #ifdef KLONDIKE
1461 #define DO_HI(cursor) (cursor->pile == pile && (movable || empty))
1462 #define TOP_HI(c) 1 /* can't select partial stacks in KLONDIKE */
1463 #elif defined SPIDER || defined FREECELL
1464 int offset[NUM_PILES]={0}; /* first card to highlight */
1465 # ifdef FREECELL
1466 int bottom[NUM_PILES]; /* first movable card */
1467 for (int i=0; i<NUM_PILES; i++)
1468 bottom[i] = find_top(f.t[i]) - max_move(i,-1);
1469 # endif
1470 #define DO_HI(cursor) (cursor->pile == pile && (movable || empty) \
1471 && offset[pile] >= cursor->opt)
1472 #define TOP_HI(cursor) (cursor->pile == pile && movable \
1473 && offset[pile] == cursor->opt)
1474 #endif
1475 /* print tableu piles: */
1476 int row[NUM_PILES] = {0};
1477 int line[NUM_PILES]= {0};
1478 int label[NUM_PILES]={0};
1479 int line_had_card;
1480 int did_placeholders = 0;
1481 do {
1482 line_had_card = 0;
1483 for (int pile = 0; pile < NUM_PILES; pile++) {
1484 card_t card = f.t[pile][row[pile]];
1485 card_t next = f.t[pile][row[pile]+1];
1486 int movable = is_movable(f.t[pile], row[pile]);
1487 #ifdef FREECELL
1488 if(row[pile] <= bottom[pile]) movable = 0;
1489 #endif
1490 int empty = !card && row[pile] == 0;
1491
1492 print_hi (DO_HI(active), DO_HI(inactive), movable, (
1493 (!card && row[pile] == 0)?op.s->placeholder
1494 :(card<0)?op.s->facedown
1495 :op.s->card[card]
1496 )[line[pile]]);
1497
1498 int extreme_overlap = ( 3 /* spacer, labels, status */
1499 + 2 * op.s->height /* stock, top tableu card */
1500 + find_top(f.t[pile]) * op.s->overlap) >op.w[0];
1501 /* normal overlap: */
1502 if (++line[pile] >= (next?op.s->overlap:op.s->height)
1503 /* extreme overlap on closed cards: */
1504 || (extreme_overlap &&
1505 line[pile] >= 1 &&
1506 f.t[pile][row[pile]] < 0 &&
1507 f.t[pile][row[pile]+1] <0)
1508 /* extreme overlap on sequences: */
1509 || (extreme_overlap &&
1510 !TOP_HI(active) && /*always show top selected card*/
1511 line[pile] >= 1 && row[pile] > 0 &&
1512 f.t[pile][row[pile]-1] > NO_CARD &&
1513 is_consecutive (f.t[pile], row[pile]) &&
1514 is_consecutive (f.t[pile], row[pile]-1) &&
1515 f.t[pile][row[pile]+1] != NO_CARD)
1516 ) {
1517 line[pile]=0;
1518 row[pile]++;
1519 #if defined SPIDER || defined FREECELL
1520 if (movable) offset[pile]++;
1521 #endif
1522 }
1523 /* tableu labels: */
1524 if(!card && !label[pile] && row[pile]>0&&line[pile]>0) {
1525 label[pile] = 1;
1526 printf ("\b\b%d ", (pile+1) % 10); //XXX: hack
1527 }
1528 line_had_card |= !!card;
1529 did_placeholders |= row[pile] > 0;
1530 }
1531 printf ("\n");
1532 } while (line_had_card || !did_placeholders);
1533 }
1534
1535 void visbell (void) {
1536 if (!op.v) return;
1537 printf ("\033[?5h"); fflush (stdout);
1538 usleep (100000);
1539 printf ("\033[?5l"); fflush (stdout);
1540 }
1541 void win_anim(void) {
1542 printf ("\033[?25l"); /* hide cursor */
1543 for (;;) {
1544 /* set cursor to random location */
1545 int row = 1+rand()%(1+op.w[0]-op.s->height);
1546 int col = 1+rand()%(1+op.w[1]-op.s->width);
1547
1548 /* draw random card */
1549 int face = 1 + rand() % 52;
1550 for (int l = 0; l < op.s->height; l++) {
1551 printf ("\033[%d;%dH", row+l, col);
1552 printf ("%s", op.s->card[face][l]);
1553 }
1554 fflush (stdout);
1555
1556 /* exit on keypress */
1557 struct pollfd p = {STDIN_FILENO, POLLIN, 0};
1558 if (poll (&p, 1, 80)) goto fin;
1559 }
1560 fin:
1561 printf ("\033[?25h"); /* show cursor */
1562 return;
1563 }
1564 //}}}
1565
1566 // undo logic {{{
1567 void undo_push (int _f, int t, int n, int o) {
1568 struct undo* new = malloc(sizeof(struct undo));
1569 new->f = _f;
1570 new->t = t;
1571 new->n = n;
1572 new->o = o;
1573 new->prev = f.u;
1574 new->next = NULL;
1575 f.u->next = new;
1576 f.u = f.u->next;
1577 }
1578 void undo_pop (struct undo* u) {
1579 if (u == &undo_sentinel) return;
1580
1581 #ifdef KLONDIKE
1582 if (u->f == FOUNDATION) {
1583 /* foundation -> tableu */
1584 int top_f = find_top(f.f[u->n]);
1585 int top_t = find_top(f.t[u->t]);
1586 f.f[u->n][top_f+1] = f.t[u->t][top_t];
1587 f.t[u->t][top_t] = NO_CARD;
1588 } else if (u->f == WASTE && u->t == FOUNDATION) {
1589 /* waste -> foundation */
1590 /* split u->n into wst and fnd: */
1591 int wst = u->n & 0xffff;
1592 int fnd = u->n >> 16;
1593 /* move stock cards one position up to make room: */
1594 for (int i = f.z; i >= wst; i--) f.s[i+1] = f.s[i];
1595 /* move one card from foundation to waste: */
1596 int top = find_top(f.f[fnd]);
1597 f.s[wst] = f.f[fnd][top];
1598 f.f[fnd][top] = NO_CARD;
1599 f.z++;
1600 f.w++;
1601 } else if (u->f == WASTE) {
1602 /* waste -> tableu */
1603 /* move stock cards one position up to make room: */
1604 for (int i = f.z-1; i >= u->n; i--) f.s[i+1] = f.s[i];
1605 /* move one card from tableu to waste: */
1606 int top = find_top(f.t[u->t]);
1607 f.s[u->n] = f.t[u->t][top];
1608 f.t[u->t][top] = NO_CARD;
1609 f.z++;
1610 f.w++;
1611 } else if (u->t == FOUNDATION) {
1612 /* tableu -> foundation */
1613 int top_f = find_top(f.t[u->f]);
1614 int top_t = find_top(f.f[u->n]);
1615 /* close topcard if previous action caused turn_over(): */
1616 if (u->o) f.t[u->f][top_f] *= -1;
1617 /* move one card from foundation to tableu: */
1618 f.t[u->f][top_f+1] = f.f[u->n][top_t];
1619 f.f[u->n][top_t] = NO_CARD;
1620 } else {
1621 /* tableu -> tableu */
1622 int top_f = find_top(f.t[u->f]);
1623 int top_t = find_top(f.t[u->t]);
1624 /* close topcard if previous action caused turn_over(): */
1625 if (u->o) f.t[u->f][top_f] *= -1;
1626 /* move n cards from tableu[f] to tableu[t]: */
1627 for (int i = 0; i < u->n; i++) {
1628 f.t[u->f][top_f+u->n-i] = f.t[u->t][top_t-i];
1629 f.t[u->t][top_t-i] = NO_CARD;
1630 }
1631 }
1632 #elif defined SPIDER
1633 if (u->f == STOCK) {
1634 /* stock -> tableu */
1635 /*remove a card from each pile and put it back onto the stock:*/
1636 for (int pile = NUM_PILES-1; pile >= 0; pile--) {
1637 int top = find_top(f.t[pile]);
1638 f.s[f.z++] = f.t[pile][top];
1639 f.t[pile][top] = NO_CARD;
1640 }
1641 } else if (u->t == FOUNDATION) {
1642 /* tableu -> foundation */
1643 int top = find_top(f.t[u->f]);
1644 /* close topcard if previous action caused turn_over(): */
1645 if (u->o) f.t[u->f][top] *= -1;
1646 /* append cards from foundation to tableu */
1647 for (int i = RANK_K; i >= RANK_A; i--) {
1648 f.t[u->f][++top] = f.f[u->n][i];
1649 f.f[u->n][i] = NO_CARD;
1650 }
1651 f.w--; /* decrement complete-foundation-counter */
1652
1653 } else {
1654 /* tableu -> tableu */
1655 int top_f = find_top(f.t[u->f]);
1656 int top_t = find_top(f.t[u->t]);
1657 /* close topcard if previous action caused turn_over(): */
1658 if (u->o) f.t[u->f][top_f] *= -1;
1659 /* move n cards from tableu[f] to tableu[t]: */
1660 for (int i = 0; i < u->n; i++) {
1661 f.t[u->f][top_f+u->n-i] = f.t[u->t][top_t-i];
1662 f.t[u->t][top_t-i] = NO_CARD;
1663 }
1664 }
1665 #elif defined FREECELL
1666 /*NOTE: if from and to are both stock/foundation, opt = from | to<<16 */
1667 if (u->f == STOCK && u->t == FOUNDATION) {
1668 /* free cells -> foundation */
1669 /* split u->n into cll and fnd: */
1670 int cll = u->n & 0xffff;
1671 int fnd = u->n >> 16;
1672 /* move one card from foundation to free cell: */
1673 int top = find_top(f.f[fnd]);
1674 f.s[cll] = f.f[fnd][top];
1675 f.f[fnd][top] = NO_CARD;
1676 f.w |= 1<<cll; /* mark cell as occupied */
1677 } else if (u->f == STOCK) {
1678 /* free cells -> cascade */
1679 int top_t = find_top(f.t[u->t]);
1680 f.s[u->n] = f.t[u->t][top_t];
1681 f.t[u->t][top_t] = NO_CARD;
1682 f.w |= 1<<u->n; /* mark cell as occupied */
1683 } else if (u->f == FOUNDATION && u->t == STOCK) {
1684 /* foundation -> free cells */
1685 /* split u->n into cll and fnd: */
1686 int cll = u->n >> 16;
1687 int fnd = u->n & 0xffff;
1688 /* move 1 card from free cell to foundation: */
1689 int top_f = find_top(f.f[fnd]);
1690 f.f[fnd][top_f+1] = f.s[cll];
1691 f.s[cll] = NO_CARD;
1692 f.w &= ~(1<<cll); /* mark cell as free */
1693 } else if (u->f == FOUNDATION) {
1694 /* foundation -> cascade */
1695 int top_f = find_top(f.f[u->n]);
1696 int top_t = find_top(f.t[u->t]);
1697 f.f[u->n][top_f+1] = f.t[u->t][top_t];
1698 f.t[u->t][top_t] = NO_CARD;
1699 } else if (u->t == STOCK) {
1700 /* cascade -> free cells */
1701 int top_f = find_top(f.t[u->f]);
1702 f.t[u->f][top_f+1] = f.s[u->n];
1703 f.s[u->n] = NO_CARD;
1704 f.w &= ~(1<<u->n); /* mark cell as free */
1705 } else if (u->t == FOUNDATION) {
1706 /* cascade -> foundation */
1707 int top_f = find_top(f.t[u->f]);
1708 int top_t = find_top(f.f[u->n]);
1709 /* move one card from foundation to cascade: */
1710 f.t[u->f][top_f+1] = f.f[u->n][top_t];
1711 f.f[u->n][top_t] = NO_CARD;
1712 } else {
1713 /* cascade -> cascade */
1714 int top_f = find_top(f.t[u->f]);
1715 int top_t = find_top(f.t[u->t]);
1716 /* move n cards from tableu[f] to tableu[t]: */
1717 for (int i = 0; i < u->n; i++) {
1718 f.t[u->f][top_f+u->n-i] = f.t[u->t][top_t-i];
1719 f.t[u->t][top_t-i] = NO_CARD;
1720 }
1721 }
1722 #endif
1723
1724 void* old = f.u;
1725 f.u = f.u->prev;
1726 free(old);
1727 }
1728 void free_undo (struct undo* u) {
1729 while (u && u != &undo_sentinel) {
1730 void* old = u;
1731 u = u->prev;
1732 free (old);
1733 }
1734 }
1735 //}}}
1736
1737 // initialization stuff {{{
1738 void screen_setup (int enable) {
1739 if (enable) {
1740 raw_mode(1);
1741 printf ("\033[s\033[?47h"); /* save cursor, alternate screen */
1742 printf ("\033[H\033[J"); /* reset cursor, clear screen */
1743 printf ("\033[?1000h"); /* enable mouse */
1744 } else {
1745 printf ("\033[?1000l"); /* disable mouse */
1746 printf ("\033[?47l\033[u"); /* primary screen, restore cursor */
1747 raw_mode(0);
1748 }
1749 }
1750
1751 void raw_mode(int enable) {
1752 static struct termios saved_term_mode;
1753 struct termios raw_term_mode;
1754
1755 if (enable) {
1756 if (saved_term_mode.c_lflag == 0)/*don't overwrite stored mode*/
1757 tcgetattr(STDIN_FILENO, &saved_term_mode);
1758 raw_term_mode = saved_term_mode;
1759 raw_term_mode.c_lflag &= ~(ICANON | ECHO);
1760 raw_term_mode.c_cc[VMIN] = 1 ;
1761 raw_term_mode.c_cc[VTIME] = 0;
1762 tcsetattr(STDIN_FILENO, TCSAFLUSH, &raw_term_mode);
1763 } else {
1764 tcsetattr(STDIN_FILENO, TCSAFLUSH, &saved_term_mode);
1765 }
1766 }
1767
1768 void signal_handler (int signum) {
1769 struct winsize w;
1770 switch (signum) {
1771 case SIGTSTP:
1772 screen_setup(0);
1773 signal(SIGTSTP, SIG_DFL); /* NOTE: assumes SysV semantics! */
1774 raise(SIGTSTP);
1775 break;
1776 case SIGCONT:
1777 screen_setup(1);
1778 print_table(NO_HI, NO_HI);
1779 break;
1780 case SIGINT: //TODO: don't exit; just warn like vim does
1781 exit(128+SIGINT);
1782 case SIGWINCH:
1783 ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
1784 op.w[0] = w.ws_row;
1785 op.w[1] = w.ws_col;
1786 break;
1787 }
1788 }
1789 void signal_setup(void) {
1790 struct sigaction saction;
1791
1792 saction.sa_handler = signal_handler;
1793 sigemptyset(&saction.sa_mask);
1794 saction.sa_flags = 0;
1795 if (sigaction(SIGTSTP, &saction, NULL) < 0) {
1796 perror ("SIGTSTP");
1797 exit (1);
1798 }
1799 if (sigaction(SIGCONT, &saction, NULL) < 0) {
1800 perror ("SIGCONT");
1801 exit (1);
1802 }
1803 if (sigaction(SIGINT, &saction, NULL) < 0) {
1804 perror ("SIGINT");
1805 exit (1);
1806 }
1807 if (sigaction(SIGWINCH, &saction, NULL) < 0) {
1808 perror ("SIGWINCH");
1809 exit (1);
1810 }
1811 }
1812 //}}}
1813
1814 //vim: foldmethod=marker
Imprint / Impressum