10 #define E_BAD_SCREEN_DIMS 2
11 #define E_ENGINE_INITIALIZE 3
12 #define E_ENGINE_ERROR 4
17 int inp_cell(int height
, int width
, int s_height
, int s_width
, int yoff
,
21 static int x
= -1, y
= -1;
25 const char * const *titles
= NULL
;
33 if (con_input_titles(&n
, &titles
))
36 values
= calloc(n
, sizeof(*values
));
39 for (j
= 0; j
< s_width
; ++j
)
42 mvaddstr(s_height
, 0, "Select:");
58 move(yoff
+ y
, xoff
+ x
);
62 case 'h': case KEY_LEFT
: x
--; break;
63 case 'l': case KEY_RIGHT
: x
++; break;
64 case 'j': case KEY_DOWN
: y
++; break;
65 case 'k': case KEY_UP
: y
--; break;
66 case 'y': case KEY_A1
: x
--; y
--; break;
67 case 'u': case KEY_A3
: x
++; y
--; break;
68 case 'b': case KEY_C1
: x
--; y
++; break;
69 case 'n': case KEY_C3
: x
++; y
++; break;
70 case 'H': x
-=8; break;
71 case 'L': x
+=8; break;
72 case 'J': y
+=8; break;
73 case 'K': y
-=8; break;
74 case 'Y': x
-=8; y
-=8; break;
75 case 'U': x
+=8; y
-=8; break;
76 case 'B': x
-=8; y
+=8; break;
77 case 'N': x
+=8; y
+=8; break;
80 } while(c
!= ' ' && c
!= '\n' && c
!= '\r' && c
!= KEY_ENTER
);
82 for (i
= 0; i
< n
; ++i
) {
83 values
[i
] = calloc(128, sizeof(*values
[i
]));
85 for (j
= 0; j
< s_width
; ++j
)
87 mvaddstr(s_height
, 0, titles
[i
]);
96 timeout(1000 / FPS_CAP
);
99 err
= con_set_val(x
, y
, (const char **)values
);
101 for (i
= 0; i
< n
; ++i
)
109 void bottom_bar(int height
, int width
, char run
)
114 for (i
= 0; i
< width
; ++i
)
121 addch('P' | A_STANDOUT
);
125 addch('R' | A_STANDOUT
);
127 addch('S' | A_STANDOUT
);
131 addch('V' | A_STANDOUT
);
133 addch('Q' | A_STANDOUT
);
138 int displayloop(void)
140 int s_width
= -1, s_height
= -1, o_width
= -1, o_height
= -1;
141 int sx
= -1, sy
= -1;
143 int g_width
= con_width(), g_height
= con_height();
144 char run
= 0, should_quit
= 0, onestep
= 0, skip_this_calc
= 0;
146 const char *cell
= NULL
;
150 getmaxyx(stdscr
, s_height
, s_width
);
153 if (s_height
!= o_height
|| s_width
!= o_width
) {
159 sy
= (s_height
- g_height
- 1) / 2;
160 sx
= (s_width
- g_width
- 1) / 2;
162 if (sy
< 0 || sx
< 0)
163 return E_BAD_SCREEN_DIMS
;
167 mvaddch((sy
- 1), (sx
- 1), ACS_ULCORNER
);
169 move((sy
- 1), sx
); for (i
= 0; i
< g_width
; ++i
) addch(ACS_HLINE
);
171 if (sx
+ g_width
< s_width
)
172 mvaddch((sy
- 1), (sx
+ g_width
), ACS_URCORNER
);
176 for (i
= sy
; i
< sy
+ g_height
; ++i
)
177 mvaddch(i
, (sx
- 1), ACS_VLINE
);
179 if (sx
+ g_width
< s_width
)
180 for (i
= sy
; i
< sy
+ g_height
; ++i
)
181 mvaddch(i
, (sx
+ g_width
), ACS_VLINE
);
183 if (sy
+ g_height
< s_height
) {
185 mvaddch((sy
+ g_height
), (sx
- 1), ACS_LLCORNER
);
187 move((sy
+ g_height
), sx
);
188 for (i
= 0; i
< g_width
; ++i
) addch(ACS_HLINE
);
190 if (sx
+ g_width
< s_width
)
191 mvaddch((sy
+ g_height
), (sx
+ g_width
), ACS_LRCORNER
);
195 for (j
= 0; j
< g_height
; ++j
) {
197 for (i
= 0; i
< g_width
; ++i
) {
198 if ((err
= con_rep_at(i
, j
, &cell
)))
205 bottom_bar(s_height
, s_width
, run
);
211 case 'q': should_quit
= 1; break;
213 case 'r': run
= 1; break;
215 case 'p': run
= 0; onestep
= 0; break;
218 case 's': run
= 0; onestep
= 1; break;
224 inp_cell(g_height
, g_width
, s_height
, s_width
, sy
, sx
);
227 run
= 0; onestep
= 1; skip_this_calc
= 1; break;
229 } while (!(run
|| onestep
) && !should_quit
&& !err
);
231 if (should_quit
|| err
)
236 else if ((err
= con_step()))
239 } while (!should_quit
);
241 return (!err
) ? 0 : E_ENGINE_ERROR
;
244 int main(int argc
, const char **argv
)
246 int width
= -1, height
= -1, ret
= 0;
247 FILE *template = NULL
;
249 setlocale(LC_ALL
, "");
252 if (!(template = fopen(argv
[1], "r"))) {
253 perror("Could not open template file");
256 err
= con_initialize_f(template);
258 } else if (argc
!= 1) {
259 fprintf(stderr
, "Usage: %s [path-to-input-file]\n", argv
[0]);
265 getmaxyx(stdscr
, height
, width
);
269 timeout(1000 / FPS_CAP
);
272 intrflush(stdscr
, FALSE
);
273 keypad(stdscr
, TRUE
);
276 err
= con_initialize_wh(width
, height
- 1);
280 fprintf(stderr
, "Could not initialize engine: %s\n", con_errstring(err
));
281 return E_ENGINE_INITIALIZE
;
288 case E_BAD_SCREEN_DIMS
:
289 fprintf(stderr
, "Term dimensions too small.\n");
292 fprintf(stderr
, "Engine error: %s\n", con_errstring(err
));