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