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