]> git.gir.st - solVItaire.git/blob - sol.c
fix check_won
[solVItaire.git] / sol.c
1 #define _DEFAULT_SOURCE
2 #include <poll.h>
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <time.h>
6 #include <termios.h>
7 #include <unistd.h>
8
9 #include "sol.h"
10 #include "schemes.h"
11
12 #ifdef KLONDIKE
13 #define NUM_PILES 7
14 #define MAX_HIDDEN 6 /*how many cards are turned over at most in a tableu pile*/
15 #define MAX_STOCK 24 /*how many cards can be in the stock at most (=@start)*/
16 #define NUM_DECKS 1
17 #define PILE_SIZE MAX_HIDDEN+NUM_RANKS
18 #elif defined SPIDER
19 #define MAX_HIDDEN 5
20 #define NUM_PILES 10
21 #define MAX_STOCK 50 /*how many cards can be dealt onto the piles*/
22 #define NUM_DECKS 2
23 #define PILE_SIZE DECK_SIZE*NUM_DECKS /* no maximum stack size in spider :/ */
24 #endif
25
26 #define get_suit(card) \
27 ((card-1) % NUM_SUITS)
28 #define get_rank(card) \
29 ((card-1) / NUM_SUITS)
30 #define get_color(card) \
31 ((get_suit(card) ^ get_suit(card)>>1) & 1)
32
33 struct playfield {
34 card_t s[MAX_STOCK]; /* stock */
35 int z; /* stock size */
36 int w; /* waste; index into stock (const -1 in spider) */
37 card_t f[NUM_DECKS*NUM_SUITS][PILE_SIZE]; /* foundation */
38 card_t t[NUM_PILES][PILE_SIZE]; /* tableu piles */
39 struct undo {
40 int f; /* pile cards were taken from (overloaded:128+n=stock) */
41 int t; /* pile cards were moved to */
42 int n; /* number of cards moved */
43 struct undo* prev;
44 struct undo* next;
45 }* u;
46 } f;
47 struct opts {
48 #ifdef SPIDER
49 int m; /* difficulty mode */
50 #endif
51 const struct scheme* s;
52 } op;
53
54 // action table {{{
55 /* stores a function pointer for every takeable action; called by game loop */
56 int (*action[NUM_PLACES][10])(int,int) = {
57 #ifdef KLONDIKE
58 /* 1 2 3 4 5 6 7 stk wst fnd*/
59 /* 1 */ { t2f, t2t, t2t, t2t, t2t, t2t, t2t, nop, nop, t2f },
60 /* 2 */ { t2t, t2f, t2t, t2t, t2t, t2t, t2t, nop, nop, t2f },
61 /* 3 */ { t2t, t2t, t2f, t2t, t2t, t2t, t2t, nop, nop, t2f },
62 /* 4 */ { t2t, t2t, t2t, t2f, t2t, t2t, t2t, nop, nop, t2f },
63 /* 5 */ { t2t, t2t, t2t, t2t, t2f, t2t, t2t, nop, nop, t2f },
64 /* 6 */ { t2t, t2t, t2t, t2t, t2t, t2f, t2t, nop, nop, t2f },
65 /* 7 */ { t2t, t2t, t2t, t2t, t2t, t2t, t2f, nop, nop, t2f },
66 /*stk*/ { nop, nop, nop, nop, nop, nop, nop, nop, s2w, nop },
67 /*wst*/ { w2t, w2t, w2t, w2t, w2t, w2t, w2t, w2s, w2f, w2f },
68 /*fnd*/ { f2t, f2t, f2t, f2t, f2t, f2t, f2t, nop, nop, nop },
69 #elif defined SPIDER
70 /* 1 2 3 4 5 6 7 8 9 10*/
71 /* 1 */ { nop, t2t, t2t, t2t, t2t, t2t, t2t, t2t, t2t, t2t },
72 /* 2 */ { t2t, nop, t2t, t2t, t2t, t2t, t2t, t2t, t2t, t2t },
73 /* 3 */ { t2t, t2t, nop, t2t, t2t, t2t, t2t, t2t, t2t, t2t },
74 /* 4 */ { t2t, t2t, t2t, nop, t2t, t2t, t2t, t2t, t2t, t2t },
75 /* 5 */ { t2t, t2t, t2t, t2t, nop, t2t, t2t, t2t, t2t, t2t },
76 /* 6 */ { t2t, t2t, t2t, t2t, t2t, nop, t2t, t2t, t2t, t2t },
77 /* 7 */ { t2t, t2t, t2t, t2t, t2t, t2t, nop, t2t, t2t, t2t },
78 /* 8 */ { t2t, t2t, t2t, t2t, t2t, t2t, t2t, nop, t2t, t2t },
79 /* 9 */ { t2t, t2t, t2t, t2t, t2t, t2t, t2t, t2t, nop, t2t },
80 /*10 */ { t2t, t2t, t2t, t2t, t2t, t2t, t2t, t2t, t2t, nop },
81 /*stk*/ { s2t, s2t, s2t, s2t, s2t, s2t, s2t, s2t, s2t, s2t },
82 #endif
83 };
84 // }}}
85
86 int main(int argc, char** argv) {
87 (void) argc;(void) argv;
88 op.s = &unicode_large_color;
89 #ifdef SPIDER
90 op.m = MEDIUM; //TODO: make configurable
91 op.m = EASY;
92 #endif
93 screen_setup(1);
94 sol(); //TODO: restart, etc.
95 screen_setup(0);
96 }
97
98 void sol(void) {
99 deal();
100
101 int from, to;
102 print_table(NO_HI);
103 for(;;) {
104 switch (get_cmd(&from, &to)) {
105 case CMD_MOVE:
106 switch (action[from][to](from,to)) {
107 case OK: break;
108 case ERR: visbell(); break;
109 case WON:
110 print_table(NO_HI);
111 win_anim();
112 getchar(); /* consume char left by win_anim() */
113 return;
114 }
115 break;
116 case CMD_QUIT: return;
117 }
118 print_table(NO_HI);
119 }
120 }
121
122 int find_top(card_t* pile) {
123 int i;
124 for(i=PILE_SIZE-1; i>=0 && !pile[i]; i--);
125 return i;
126 }
127 void turn_over(card_t* pile) {
128 int top = find_top(pile);
129 if (pile[top] < 0) pile[top] *= -1;
130 }
131 int check_won(void) {
132 for (int pile = 0; pile < NUM_DECKS*NUM_SUITS; pile++)
133 if (f.f[pile][NUM_RANKS-1] == NO_CARD) return 0;
134
135 return 1;
136 }
137 void win_anim(void) {
138 printf ("\033[?25l"); /* hide cursor */
139 for (;;) {
140 /* set cursor to random location */
141 int row = 1+random()%(24-op.s->width);
142 int col = 1+random()%(80-op.s->height);
143
144 /* draw random card */
145 int face = 1 + random() % 52;
146 for (int l = 0; l < op.s->height; l++) {
147 printf ("\033[%d;%dH", row+l, col);
148 printf ("%s", op.s->card[face][l]);
149 }
150 fflush (stdout);
151
152 /* exit on keypress */
153 struct pollfd p = {STDIN_FILENO, POLLIN, 0};
154 if (poll (&p, 1, 80)) return;
155 }
156 }
157 // takeable actions {{{
158 #ifdef KLONDIKE
159 card_t stack_take(void) { /*NOTE: assert(f.w >= 0) */
160 card_t card = f.s[f.w];
161 /* move stack one over, so there are no gaps in it: */
162 for (int i = f.w; i < f.z-1; i++)
163 f.s[i] = f.s[i+1];
164 f.z--;
165 f.w--; /* make previous card visible again */
166 return card;
167 }
168 int t2f(int from, int to) { /* tableu to foundation */
169 (void) to; //don't need
170 int top_from = find_top(f.t[from]);
171 to = get_suit(f.t[from][top_from]);
172 int top_to = find_top(f.f[to]);
173 if ((top_to < 0 && get_rank(f.t[from][top_from]) == RANK_A)
174 || (top_to >= 0 && get_rank(f.f[to][top_to]) == get_rank(f.t[from][top_from])-1)) {
175 f.f[to][top_to+1] = f.t[from][top_from];
176 f.t[from][top_from] = NO_CARD;
177 turn_over(f.t[from]);
178 if (check_won()) return WON;
179 return OK;
180 } else return ERR;
181 }
182 int w2f(int from, int to) { /* waste to foundation */
183 (void) from; (void) to; //don't need
184 if (f.w < 0) return ERR;
185 to = get_suit(f.s[f.w]);
186 int top_to = find_top(f.f[to]);
187 if ((top_to < 0 && get_rank(f.s[f.w]) == RANK_A)
188 || (top_to >= 0 && get_rank(f.f[to][top_to]) == get_rank(f.s[f.w])-1)) {
189 f.f[to][top_to+1] = stack_take();
190 if (check_won()) return WON;
191 return OK;
192 } else return ERR;
193
194 }
195 int s2w(int from, int to) { /* stock to waste */
196 (void) from; (void) to; //don't need
197 if (f.z == 0) return ERR;
198 f.w++;
199 if (f.w == f.z) f.w = -1;
200 return OK;
201 }
202 int w2s(int from, int to) { /* waste to stock (undoes stock to waste) */
203 (void) from; (void) to; //don't need
204 if (f.z == 0) return ERR;
205 f.w--;
206 if (f.w < -1) f.w = f.z-1;
207 return OK;
208 }
209 int f2t(int from, int to) { /* foundation to tableu */
210 int top_to = find_top(f.t[to]);
211 printf ("take from (1-4): "); fflush (stdout);
212 from = getchar() - '1';
213 if (from < 0 || from > 3) return ERR;
214 int top_from = find_top(f.f[from]);
215
216 if ((get_color(f.t[to][top_to]) != get_color(f.f[from][top_from]))
217 && (get_rank(f.t[to][top_to]) == get_rank(f.f[from][top_from])+1)) {
218 f.t[to][top_to+1] = f.f[from][top_from];
219 f.f[from][top_from] = NO_CARD;
220 return OK;
221 } else return ERR;
222 }
223 int w2t(int from, int to) { /* waste to tableu */
224 (void) from; //don't need
225 int top_to = find_top(f.t[to]);
226 if (((get_color(f.t[to][top_to]) != get_color(f.s[f.w]))
227 && (get_rank(f.t[to][top_to]) == get_rank(f.s[f.w])+1))
228 || (top_to < 0 && get_rank(f.s[f.w]) == RANK_K)) {
229 f.t[to][top_to+1] = stack_take();
230 return OK;
231 } else return ERR;
232 }
233 int t2t(int from, int to) { /* tableu to tableu */
234 int top_to = find_top(f.t[to]);
235 int top_from = find_top(f.t[from]);
236 for (int i = top_from; i >=0; i--) {
237 if (((get_color(f.t[to][top_to]) != get_color(f.t[from][i]))
238 && (get_rank(f.t[to][top_to]) == get_rank(f.t[from][i])+1)
239 && f.t[from][i] > NO_CARD) /* card face up? */
240 || (top_to < 0 && get_rank(f.t[from][i]) == RANK_K)) {
241 /* move cards [i..top_from] to their destination */
242 for (;i <= top_from; i++) {
243 top_to++;
244 f.t[to][top_to] = f.t[from][i];
245 f.t[from][i] = NO_CARD;
246 }
247 turn_over(f.t[from]);
248 return OK;
249 }
250 }
251 return ERR; /* no such move possible */
252 }
253 #elif defined SPIDER
254 void remove_if_complete (card_t* pile) { //TODO: cleanup
255 static int foundation = 0; /* where to put pile onto (1 set per stack)*/
256 /* test if K...A complete; move to foundation if so */
257 int top_from = find_top(pile);
258 if (get_rank(pile[top_from]) != RANK_A) return;
259 for (int i = top_from; i>=0; i--) {
260 if ((i+1 < PILE_SIZE && pile[i+1] != NO_CARD) // card below or last? XXX: copied from t2t()--make function
261 && (get_rank(pile[i+1]) != get_rank(pile[i])-1) //cards not consecutive?
262 ) {
263 return;
264 }
265 if ((i+1 < PILE_SIZE && pile[i+1] != NO_CARD) // card below or last?
266 && (get_suit(pile[i+1]) != get_suit(pile[i])) //cards not same suit?
267 ) {
268 return;
269 }
270 if (i+RANK_K == top_from
271 && get_rank(pile[top_from-RANK_K]) == RANK_K) { //ace to king ok, remove it
272 for (int i = top_from, j = 0; i >= top_from-NUM_RANKS; i--, j++) {
273 f.f[foundation][j] = pile[i];
274 pile[i] = NO_CARD;
275 }
276 foundation++;
277 turn_over(pile);
278 return;
279 }
280 }
281 }
282 int t2t(int from, int to) { //TODO: in dire need of cleanup
283 //TODO: segfaulted once on large column
284 //TODO: sometimes moving doesn't work (ERR when it should be OK) XXX
285
286 int top_from = find_top(f.t[from]);
287 int top_to = find_top(f.t[to]);
288 int empty_to = -1; //awful, nondescriptive name :/
289 if (top_to < 0) { /* empty pile? */
290 printf ("\rup to (a23456789xjqk): "); //TODO: automatically do it if only 1 card movable
291 empty_to = getchar();
292 switch (empty_to) {
293 case 'a': case 'A': empty_to = RANK_A; break;
294 case '0': /* fallthrough */
295 case 'x': case 'X': empty_to = RANK_X; break;
296 case 'j': case 'J': empty_to = RANK_J; break;
297 case 'q': case 'Q': empty_to = RANK_Q; break;
298 case 'k': case 'K': empty_to = RANK_K; break;
299 default: empty_to -= '1';
300 }
301 if (empty_to < RANK_A || empty_to > RANK_K) return ERR;
302 }
303 for (int i = top_from; i >= 0; i--) {
304 if ((i+1 < PILE_SIZE && f.t[from][i+1] != NO_CARD) // card below or last?
305 && (get_rank(f.t[from][i+1]) != get_rank(f.t[from][i])-1) //cards not consecutive?
306 ) {
307 break;
308 }
309 if ((i+1 < PILE_SIZE && f.t[from][i+1] != NO_CARD) // card below or last?
310 && (get_suit(f.t[from][i+1]) != get_suit(f.t[from][i])) //cards not same suit?
311 ) {
312 break;
313 }
314
315 if ((get_rank(f.t[from][i]) == get_rank(f.t[to][top_to])-1) // consecutive?
316 || (empty_to >= RANK_A && get_rank(f.t[from][i]) == empty_to)) { //to empty pile and rank ok?
317 for (;i <= top_from; i++) {
318 top_to++;
319 f.t[to][top_to] = f.t[from][i];
320 f.t[from][i] = NO_CARD;
321 }
322 turn_over(f.t[from]);
323 remove_if_complete (f.t[to]);
324 if (check_won()) return WON;
325 return OK;
326 }
327 }
328
329 return ERR; /* no such move possible */
330 }
331 int s2t(int from, int to) { //TODO: check remove, won
332 (void) from; (void) to; //don't need
333 if (f.z <= 0) return ERR; /* stack out of cards */
334 for (int pile = 0; pile < NUM_PILES; pile++)
335 if (f.t[pile][0]==NO_CARD) return ERR; /*no piles may be empty*/
336 for (int pile = 0; pile < NUM_PILES; pile++) {
337 f.t[pile][find_top(f.t[pile])+1] = f.s[--f.z];
338 }
339 return OK;
340 }
341 #endif
342 int nop(int from, int to) { (void)from;(void)to; return ERR; }
343 // }}}
344
345 int get_cmd (int* from, int* to) {
346 //returns 0 on success or an error code indicating game quit, new game,...
347 //TODO: escape sequences (mouse, cursor keys)
348 int f, t;
349 f = getchar();
350
351 switch (f) {
352 case '1': *from = TAB_1; break;
353 case '2': *from = TAB_2; break;
354 case '3': *from = TAB_3; break;
355 case '4': *from = TAB_4; break;
356 case '5': *from = TAB_5; break;
357 case '6': *from = TAB_6; break;
358 case '7': *from = TAB_7; break;
359 #ifdef SPIDER
360 case '8': *from = TAB_8; break;
361 case '9': *from = TAB_9; break;
362 case '0': *from = TAB_10; break;
363 #elif defined KLONDIKE
364 case '9': *from = WASTE; break;
365 case '0': *from = FOUNDATION; break;
366 case '8': /* fallthrough */
367 #endif
368 case '\n': /* shortcut for dealing from stock */
369 *from = STOCK;
370 *to = WASTE;
371 return CMD_MOVE;
372 case 'q': return CMD_QUIT;
373 case 'r': return CMD_NEW; //TODO
374 case 'h': return CMD_HINT; //TODO
375 case '?': return CMD_HELP; //TODO
376 case '/': return CMD_FIND; //TODO: highlight card of given rank (even non-movable)
377 case '\033': return CMD_INVAL; //TODO: cntlseq
378 default: return CMD_INVAL;
379 }
380 print_table(*from);
381
382 t = getchar();
383 if (t < '0' || t > '9') return CMD_INVAL;
384 if (t == '0')
385 #ifdef KLONDIKE
386 *to = FOUNDATION;
387 #elif defined SPIDER
388 *to = TAB_10;
389 #endif
390 else
391 *to = t-'1';
392 return CMD_MOVE;
393 }
394
395 void deal(void) {
396 f = (const struct playfield){0}; /* clear playfield */
397 card_t deck[DECK_SIZE*NUM_DECKS];
398 int avail = DECK_SIZE*NUM_DECKS;
399 for (int i = 0; i < DECK_SIZE*NUM_DECKS; i++) deck[i] = (i%DECK_SIZE)+1;
400 #ifdef SPIDER
401 if (op.m != NORMAL) for (int i = 0; i < DECK_SIZE*NUM_DECKS; i++) {
402 if (op.m == MEDIUM) deck[i] = 1+((deck[i]-1) | 2);
403 if (op.m == EASY) deck[i] = 1+((deck[i]-1) | 2 | 1);
404 /* the 1+ -1 dance gets rid of the offset created by NO_CARD */
405 }
406 #endif
407 srandom (time(NULL));
408 /*XXX*/ long seed = time(NULL);
409 /*XXX*/ srandom (seed);
410 for (int i = DECK_SIZE*NUM_DECKS-1; i > 0; i--) { //fisher-yates
411 int j = random() % (i+1);
412 if (j-i) deck[i]^=deck[j],deck[j]^=deck[i],deck[i]^=deck[j];
413 }
414 ///////////////////////////////////////////////XXX
415 //sometimes we see duplicate cards. this tries to catch that
416 int count[_NUM_CARDS_internal] = {0};
417 for (int i = 0; i < DECK_SIZE*NUM_DECKS; i++)
418 count[deck[i]]++;
419 for (int i = 0; i < _NUM_CARDS_internal; i++){ //0 is NO_CARD
420 #ifdef SPIDER
421 int x = op.m==MEDIUM?2:op.m==EASY?4:1;
422 #else
423 int x = 1;
424 #endif
425 if (deck[i] == NO_CARD) continue;
426 if (count[deck[i]] != NUM_DECKS*x) {
427 screen_setup(0);
428 printf ("found duplicate card with seed %lx!\n", seed);
429 for (int i = 1; i < _NUM_CARDS_internal; i++)
430 printf ("%3d of %2d\n", count[deck[i]], deck[i]);
431 exit(1);
432 }
433 }
434 ///////////////////////////////////////////////XXX
435
436 /* deal cards: */
437 for (int i = 0; i < NUM_PILES; i++) {
438 #ifdef KLONDIKE
439 int closed = i; /* pile n has n closed cards, then 1 open */
440 #elif defined SPIDER
441 int closed = i<4?5:4; /* pile 1-4 have 5, 5-10 have 4 closed */
442 #endif
443 /* face down cards are negated: */
444 for (int j = 0; j < closed; j++) f.t[i][j] = -deck[--avail];
445 f.t[i][closed] = deck[--avail]; /* the face-up card */
446 }
447 /* rest of the cards to the stock; NOTE: assert(avail==50) for spider */
448 for (f.z = 0; avail; f.z++) f.s[f.z] = deck[--avail];
449 f.w = -1; /* @start: nothing on waste (no waste in spider -> const) */
450 }
451
452 int is_movable(card_t* pile, int n) { //TODO cleanup, code deduplication, needs entry in sol.h
453 #ifdef KLONDIKE
454 return(pile[n] > NO_CARD); /*non-movable cards don't exist in klondike*/
455 #elif defined SPIDER
456 int top = find_top(pile);
457 for (int i = top; i; i--) {
458 if (pile[i] <= NO_CARD) return 0; //card face down?
459 if ((i+1 < PILE_SIZE && pile[i+1] != NO_CARD) // card below or last? //COPIED FROM t2t
460 && (get_rank(pile[i+1]) != get_rank(pile[i])-1) //cards not consecutive?
461 ) {
462 return 0;
463 }
464 if ((i+1 < PILE_SIZE && pile[i+1] != NO_CARD) // card below or last?
465 && (get_suit(pile[i+1]) != get_suit(pile[i])) //cards not same suit?
466 ) {
467 return 0;
468 }
469 if (i == n) return 1; //card reached, must be movable
470 }
471 return 0;
472 #endif
473 }
474 #define print_hi(test, str) /* for highlighting during get_cmd() */ \
475 printf ("%s%s%s", (test)?"\033[7m":"", str, (test)?"\033[27m":"")
476 void print_table(int highlight) { //{{{
477 printf("\033[2J\033[H"); /* clear screen, reset cursor */
478 #ifdef KLONDIKE
479 /* print stock, waste and foundation: */
480 for (int line = 0; line < op.s->height; line++) {
481 /* stock: */
482 print_hi (highlight == STOCK, (
483 (f.w < f.z-1)?op.s->facedown
484 :op.s->placeholder)[line]);
485 /* waste: */
486 print_hi (highlight == WASTE, (
487 /* NOTE: cast, because f.w sometimes is (short)-1 !? */
488 ((short)f.w >= 0)?op.s->card[f.s[f.w]]
489 :op.s->placeholder)[line]);
490 printf ("%s", op.s->card[NO_CARD][line]); /* spacer */
491 /* foundation: */
492 for (int pile = 0; pile < NUM_SUITS; pile++) {
493 int card = find_top(f.f[pile]);
494 print_hi (highlight == FOUNDATION,
495 (card < 0)?op.s->placeholder[line]
496 :op.s->card[f.f[pile][card]][line]);
497 }
498 printf("\n");
499 }
500 printf("\n");
501 #endif
502 /* print tableu piles: */
503 int row[NUM_PILES] = {0};
504 int line[NUM_PILES]= {0};
505 int label[NUM_PILES]={0};// :|
506 int line_had_card; // :|
507 do {
508 line_had_card = 0;
509 for (int pile = 0; pile < NUM_PILES; pile++) {
510 card_t card = f.t[pile][row[pile]];
511 card_t next = f.t[pile][row[pile]+1];
512
513 print_hi (highlight == pile && is_movable(f.t[pile],row[pile]), (
514 (card<0)?op.s->facedown
515 :op.s->card[card]
516 )[line[pile]]);
517
518 if (++line[pile] >= (next?op.s->overlap:op.s->height) //normal overlap
519 #if 0 //XXX
520 || (line[pile] >= 1 &&
521 f.t[pile][row[pile]] < 0 &&
522 f.t[pile][row[pile]+1] <0) //extreme overlap on closed
523 || (0) //extreme overlap on sequence TODO
524 #endif
525 ) {
526 line[pile]=0;
527 row[pile]++;
528 }
529 if(!card && !label[pile]) { /* tableu labels: */
530 label[pile] = 1;
531 printf ("\b\b%d ", (pile+1) % 10); //XXX: hack
532 }
533 line_had_card |= !!card;
534 }
535 printf ("\n");
536 } while (line_had_card);
537 }//}}}
538
539 void visbell (void) {
540 printf ("\033[?5h"); fflush (stdout);
541 usleep (100000);
542 printf ("\033[?5l"); fflush (stdout);
543 }
544
545 void append_undo (int n, int f, int t) {
546 (void)n;(void)f;(void)t;
547 //check if we have to free redo buffer (.next)
548 //malloc
549 //update pointers
550 //TODO: undo; needs operations to be written by x2y()
551 }
552
553 void screen_setup (int enable) {
554 if (enable) {
555 raw_mode(1);
556 printf ("\033[s\033[?47h"); /* save cursor, alternate screen */
557 printf ("\033[H\033[J"); /* reset cursor, clear screen */
558 //TODO//printf ("\033[?1000h\033[?25l"); /* enable mouse, hide cursor */
559 } else {
560 //TODO//printf ("\033[?9l\033[?25h"); /* disable mouse, show cursor */
561 printf ("\033[?47l\033[u"); /* primary screen, restore cursor */
562 raw_mode(0);
563 }
564 }
565
566 void raw_mode(int enable) { //{{{
567 static struct termios saved_term_mode;
568 struct termios raw_term_mode;
569
570 if (enable) {
571 tcgetattr(STDIN_FILENO, &saved_term_mode);
572 raw_term_mode = saved_term_mode;
573 raw_term_mode.c_lflag &= ~(ICANON | ECHO);
574 raw_term_mode.c_cc[VMIN] = 1 ;
575 raw_term_mode.c_cc[VTIME] = 0;
576 tcsetattr(STDIN_FILENO, TCSAFLUSH, &raw_term_mode);
577 } else {
578 tcsetattr(STDIN_FILENO, TCSAFLUSH, &saved_term_mode);
579 }
580 } //}}}
581
582 //vim: foldmethod=marker
Imprint / Impressum