2 * midend.c: general middle fragment sitting between the
3 * platform-specific front end and game-specific back end.
4 * Maintains a move list, takes care of Undo and Redo commands, and
5 * processes standard keystrokes for undo/redo/new/restart/quit.
20 game_aux_info *aux_info;
22 int nstates, statesize, statepos;
24 game_params **presets;
26 int npresets, presetsize;
30 game_drawstate *drawstate;
33 float anim_time, anim_pos;
34 float flash_time, flash_pos;
37 int pressed_mouse_button;
40 #define ensure(me) do { \
41 if ((me)->nstates >= (me)->statesize) { \
42 (me)->statesize = (me)->nstates + 128; \
43 (me)->states = sresize((me)->states, (me)->statesize, game_state *); \
47 midend_data *midend_new(frontend *fe, const game *ourgame)
49 midend_data *me = snew(midend_data);
53 get_random_seed(&randseed, &randseedsize);
56 me->ourgame = ourgame;
57 me->random = random_init(randseed, randseedsize);
58 me->nstates = me->statesize = me->statepos = 0;
60 me->params = ourgame->default_params();
63 me->fresh_seed = FALSE;
67 me->preset_names = NULL;
68 me->npresets = me->presetsize = 0;
69 me->anim_time = me->anim_pos = 0.0F;
70 me->flash_time = me->flash_pos = 0.0F;
73 me->pressed_mouse_button = 0;
80 void midend_free(midend_data *me)
85 me->ourgame->free_aux_info(me->aux_info);
86 me->ourgame->free_params(me->params);
90 void midend_size(midend_data *me, int *x, int *y)
92 me->ourgame->size(me->params, x, y);
95 void midend_set_params(midend_data *me, game_params *params)
97 me->ourgame->free_params(me->params);
98 me->params = me->ourgame->dup_params(params);
101 void midend_new_game(midend_data *me)
103 while (me->nstates > 0)
104 me->ourgame->free_game(me->states[--me->nstates]);
107 me->ourgame->free_drawstate(me->drawstate);
109 assert(me->nstates == 0);
111 if (!me->fresh_seed) {
114 me->ourgame->free_aux_info(me->aux_info);
116 me->seed = me->ourgame->new_seed(me->params, me->random,
119 me->fresh_seed = FALSE;
122 me->states[me->nstates++] = me->ourgame->new_game(me->params, me->seed);
124 me->drawstate = me->ourgame->new_drawstate(me->states[0]);
126 me->ourgame->free_ui(me->ui);
127 me->ui = me->ourgame->new_ui(me->states[0]);
128 me->pressed_mouse_button = 0;
131 void midend_restart_game(midend_data *me)
133 while (me->nstates > 1)
134 me->ourgame->free_game(me->states[--me->nstates]);
135 me->statepos = me->nstates;
136 me->ourgame->free_ui(me->ui);
137 me->ui = me->ourgame->new_ui(me->states[0]);
140 static int midend_undo(midend_data *me)
142 if (me->statepos > 1) {
150 static int midend_redo(midend_data *me)
152 if (me->statepos < me->nstates) {
160 static void midend_finish_move(midend_data *me)
164 if (me->oldstate || me->statepos > 1) {
165 flashtime = me->ourgame->flash_length(me->oldstate ? me->oldstate :
166 me->states[me->statepos-2],
167 me->states[me->statepos-1],
168 me->oldstate ? me->dir : +1);
170 me->flash_pos = 0.0F;
171 me->flash_time = flashtime;
176 me->ourgame->free_game(me->oldstate);
178 me->anim_pos = me->anim_time = 0;
181 if (me->flash_time == 0 && me->anim_time == 0)
182 deactivate_timer(me->frontend);
184 activate_timer(me->frontend);
187 static void midend_stop_anim(midend_data *me)
189 if (me->oldstate || me->anim_time) {
190 midend_finish_move(me);
195 static int midend_really_process_key(midend_data *me, int x, int y, int button)
197 game_state *oldstate = me->ourgame->dup_game(me->states[me->statepos - 1]);
200 if (button == 'n' || button == 'N' || button == '\x0E') {
201 midend_stop_anim(me);
204 return 1; /* never animate */
205 } else if (button == 'r' || button == 'R') {
206 midend_stop_anim(me);
207 midend_restart_game(me);
209 return 1; /* never animate */
210 } else if (button == 'u' || button == 'u' ||
211 button == '\x1A' || button == '\x1F') {
212 midend_stop_anim(me);
213 if (!midend_undo(me))
215 } else if (button == '\x12') {
216 midend_stop_anim(me);
217 if (!midend_redo(me))
219 } else if (button == 'q' || button == 'Q' || button == '\x11') {
220 me->ourgame->free_game(oldstate);
223 game_state *s = me->ourgame->make_move(me->states[me->statepos-1],
224 me->ui, x, y, button);
226 if (s == me->states[me->statepos-1]) {
228 * make_move() is allowed to return its input state to
229 * indicate that although no move has been made, the UI
230 * state has been updated and a redraw is called for.
235 midend_stop_anim(me);
236 while (me->nstates > me->statepos)
237 me->ourgame->free_game(me->states[--me->nstates]);
239 me->states[me->nstates] = s;
240 me->statepos = ++me->nstates;
243 me->ourgame->free_game(oldstate);
249 * See if this move requires an animation.
251 anim_time = me->ourgame->anim_length(oldstate, me->states[me->statepos-1],
254 me->oldstate = oldstate;
256 me->anim_time = anim_time;
259 midend_finish_move(me);
265 activate_timer(me->frontend);
270 int midend_process_key(midend_data *me, int x, int y, int button)
275 * Harmonise mouse drag and release messages.
277 * Some front ends might accidentally switch from sending, say,
278 * RIGHT_DRAG messages to sending LEFT_DRAG, half way through a
279 * drag. (This can happen on the Mac, for example, since
280 * RIGHT_DRAG is usually done using Command+drag, and if the
281 * user accidentally releases Command half way through the drag
282 * then there will be trouble.)
284 * It would be an O(number of front ends) annoyance to fix this
285 * in the front ends, but an O(number of back ends) annoyance
286 * to have each game capable of dealing with it. Therefore, we
287 * fix it _here_ in the common midend code so that it only has
290 * The possible ways in which things can go screwy in the front
293 * - in a system containing multiple physical buttons button
294 * presses can inadvertently overlap. We can see ABab (caps
295 * meaning button-down and lowercase meaning button-up) when
296 * the user had semantically intended AaBb.
298 * - in a system where one button is simulated by means of a
299 * modifier key and another button, buttons can mutate
300 * between press and release (possibly during drag). So we
301 * can see Ab instead of Aa.
303 * Definite requirements are:
305 * - button _presses_ must never be invented or destroyed. If
306 * the user presses two buttons in succession, the button
307 * presses must be transferred to the backend unchanged. So
308 * if we see AaBb , that's fine; if we see ABab (the button
309 * presses inadvertently overlapped) we must somehow
310 * `correct' it to AaBb.
312 * - every mouse action must end up looking like a press, zero
313 * or more drags, then a release. This allows back ends to
314 * make the _assumption_ that incoming mouse data will be
315 * sane in this regard, and not worry about the details.
317 * So my policy will be:
319 * - treat any button-up as a button-up for the currently
320 * pressed button, or ignore it if there is no currently
323 * - treat any drag as a drag for the currently pressed
324 * button, or ignore it if there is no currently pressed
327 * - if we see a button-down while another button is currently
328 * pressed, invent a button-up for the first one and then
329 * pass the button-down through as before.
332 if (IS_MOUSE_DRAG(button) || IS_MOUSE_RELEASE(button)) {
333 if (me->pressed_mouse_button) {
334 if (IS_MOUSE_DRAG(button)) {
335 button = me->pressed_mouse_button +
336 (LEFT_DRAG - LEFT_BUTTON);
338 button = me->pressed_mouse_button +
339 (LEFT_RELEASE - LEFT_BUTTON);
342 return ret; /* ignore it */
343 } else if (IS_MOUSE_DOWN(button) && me->pressed_mouse_button) {
345 * Fabricate a button-up for the previously pressed button.
347 ret = ret && midend_really_process_key
348 (me, x, y, (me->pressed_mouse_button +
349 (LEFT_RELEASE - LEFT_BUTTON)));
353 * Now send on the event we originally received.
355 ret = ret && midend_really_process_key(me, x, y, button);
358 * And update the currently pressed button.
360 if (IS_MOUSE_RELEASE(button))
361 me->pressed_mouse_button = 0;
362 else if (IS_MOUSE_DOWN(button))
363 me->pressed_mouse_button = button;
368 void midend_redraw(midend_data *me)
370 if (me->statepos > 0 && me->drawstate) {
371 start_draw(me->frontend);
372 if (me->oldstate && me->anim_time > 0 &&
373 me->anim_pos < me->anim_time) {
374 assert(me->dir != 0);
375 me->ourgame->redraw(me->frontend, me->drawstate, me->oldstate,
376 me->states[me->statepos-1], me->dir,
377 me->ui, me->anim_pos, me->flash_pos);
379 me->ourgame->redraw(me->frontend, me->drawstate, NULL,
380 me->states[me->statepos-1], +1 /*shrug*/,
381 me->ui, 0.0, me->flash_pos);
383 end_draw(me->frontend);
387 void midend_timer(midend_data *me, float tplus)
389 me->anim_pos += tplus;
390 if (me->anim_pos >= me->anim_time ||
391 me->anim_time == 0 || !me->oldstate) {
392 if (me->anim_time > 0)
393 midend_finish_move(me);
395 me->flash_pos += tplus;
396 if (me->flash_pos >= me->flash_time || me->flash_time == 0) {
397 me->flash_pos = me->flash_time = 0;
399 if (me->flash_time == 0 && me->anim_time == 0)
400 deactivate_timer(me->frontend);
404 float *midend_colours(midend_data *me, int *ncolours)
406 game_state *state = NULL;
409 if (me->nstates == 0) {
410 game_aux_info *aux = NULL;
411 char *seed = me->ourgame->new_seed(me->params, me->random, &aux);
412 state = me->ourgame->new_game(me->params, seed);
415 me->ourgame->free_aux_info(aux);
417 state = me->states[0];
419 ret = me->ourgame->colours(me->frontend, state, ncolours);
421 if (me->nstates == 0)
422 me->ourgame->free_game(state);
427 int midend_num_presets(midend_data *me)
433 while (me->ourgame->fetch_preset(me->npresets, &name, &preset)) {
434 if (me->presetsize <= me->npresets) {
435 me->presetsize = me->npresets + 10;
436 me->presets = sresize(me->presets, me->presetsize,
438 me->preset_names = sresize(me->preset_names, me->presetsize,
442 me->presets[me->npresets] = preset;
443 me->preset_names[me->npresets] = name;
451 void midend_fetch_preset(midend_data *me, int n,
452 char **name, game_params **params)
454 assert(n >= 0 && n < me->npresets);
455 *name = me->preset_names[n];
456 *params = me->presets[n];
459 int midend_wants_statusbar(midend_data *me)
461 return me->ourgame->wants_statusbar();
464 config_item *midend_get_config(midend_data *me, int which, char **wintitle)
466 char *titlebuf, *parstr;
469 titlebuf = snewn(40 + strlen(me->ourgame->name), char);
473 sprintf(titlebuf, "%s configuration", me->ourgame->name);
474 *wintitle = dupstr(titlebuf);
475 return me->ourgame->configure(me->params);
477 sprintf(titlebuf, "%s game selection", me->ourgame->name);
478 *wintitle = dupstr(titlebuf);
480 ret = snewn(2, config_item);
482 ret[0].type = C_STRING;
483 ret[0].name = "Game ID";
486 * The text going in here will be a string encoding of the
487 * parameters, plus a colon, plus the game seed. This is a
490 parstr = me->ourgame->encode_params(me->params);
491 ret[0].sval = snewn(strlen(parstr) + strlen(me->seed) + 2, char);
492 sprintf(ret[0].sval, "%s:%s", parstr, me->seed);
496 ret[1].name = ret[1].sval = NULL;
502 assert(!"We shouldn't be here");
506 char *midend_game_id(midend_data *me, char *id, int def_seed)
508 char *error, *par, *seed;
511 seed = strchr(id, ':');
515 * We have a colon separating parameters from game seed. So
516 * `par' now points to the parameters string, and `seed' to
523 * We only have one string. Depending on `def_seed', we
524 * take it to be either parameters or seed.
536 params = me->ourgame->decode_params(par);
537 error = me->ourgame->validate_params(params);
539 me->ourgame->free_params(params);
542 me->ourgame->free_params(me->params);
547 error = me->ourgame->validate_seed(me->params, seed);
552 me->seed = dupstr(seed);
553 me->fresh_seed = TRUE;
555 me->ourgame->free_aux_info(me->aux_info);
562 char *midend_set_config(midend_data *me, int which, config_item *cfg)
569 params = me->ourgame->custom_params(cfg);
570 error = me->ourgame->validate_params(params);
573 me->ourgame->free_params(params);
577 me->ourgame->free_params(me->params);
582 error = midend_game_id(me, cfg[0].sval, TRUE);
591 char *midend_text_format(midend_data *me)
593 if (me->ourgame->can_format_as_text && me->statepos > 0)
594 return me->ourgame->text_format(me->states[me->statepos-1]);