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.
21 int nstates, statesize, statepos;
23 game_params **presets;
25 int npresets, presetsize;
29 game_drawstate *drawstate;
32 float anim_time, anim_pos;
33 float flash_time, flash_pos;
36 int pressed_mouse_button;
39 #define ensure(me) do { \
40 if ((me)->nstates >= (me)->statesize) { \
41 (me)->statesize = (me)->nstates + 128; \
42 (me)->states = sresize((me)->states, (me)->statesize, game_state *); \
46 midend_data *midend_new(frontend *fe, const game *ourgame)
48 midend_data *me = snew(midend_data);
52 get_random_seed(&randseed, &randseedsize);
55 me->ourgame = ourgame;
56 me->random = random_init(randseed, randseedsize);
57 me->nstates = me->statesize = me->statepos = 0;
59 me->params = ourgame->default_params();
61 me->fresh_seed = FALSE;
65 me->preset_names = NULL;
66 me->npresets = me->presetsize = 0;
67 me->anim_time = me->anim_pos = 0.0F;
68 me->flash_time = me->flash_pos = 0.0F;
71 me->pressed_mouse_button = 0;
78 void midend_free(midend_data *me)
82 me->ourgame->free_params(me->params);
86 void midend_size(midend_data *me, int *x, int *y)
88 me->ourgame->size(me->params, x, y);
91 void midend_set_params(midend_data *me, game_params *params)
93 me->ourgame->free_params(me->params);
94 me->params = me->ourgame->dup_params(params);
97 void midend_new_game(midend_data *me)
99 while (me->nstates > 0)
100 me->ourgame->free_game(me->states[--me->nstates]);
103 me->ourgame->free_drawstate(me->drawstate);
105 assert(me->nstates == 0);
107 if (!me->fresh_seed) {
109 me->seed = me->ourgame->new_seed(me->params, me->random);
111 me->fresh_seed = FALSE;
114 me->states[me->nstates++] = me->ourgame->new_game(me->params, me->seed);
116 me->drawstate = me->ourgame->new_drawstate(me->states[0]);
118 me->ourgame->free_ui(me->ui);
119 me->ui = me->ourgame->new_ui(me->states[0]);
120 me->pressed_mouse_button = 0;
123 void midend_restart_game(midend_data *me)
125 while (me->nstates > 1)
126 me->ourgame->free_game(me->states[--me->nstates]);
127 me->statepos = me->nstates;
128 me->ourgame->free_ui(me->ui);
129 me->ui = me->ourgame->new_ui(me->states[0]);
132 static int midend_undo(midend_data *me)
134 if (me->statepos > 1) {
142 static int midend_redo(midend_data *me)
144 if (me->statepos < me->nstates) {
152 static void midend_finish_move(midend_data *me)
156 if (me->oldstate || me->statepos > 1) {
157 flashtime = me->ourgame->flash_length(me->oldstate ? me->oldstate :
158 me->states[me->statepos-2],
159 me->states[me->statepos-1],
160 me->oldstate ? me->dir : +1);
162 me->flash_pos = 0.0F;
163 me->flash_time = flashtime;
168 me->ourgame->free_game(me->oldstate);
170 me->anim_pos = me->anim_time = 0;
173 if (me->flash_time == 0 && me->anim_time == 0)
174 deactivate_timer(me->frontend);
176 activate_timer(me->frontend);
179 static void midend_stop_anim(midend_data *me)
181 if (me->oldstate || me->anim_time) {
182 midend_finish_move(me);
187 static int midend_really_process_key(midend_data *me, int x, int y, int button)
189 game_state *oldstate = me->ourgame->dup_game(me->states[me->statepos - 1]);
192 if (button == 'n' || button == 'N' || button == '\x0E') {
193 midend_stop_anim(me);
196 return 1; /* never animate */
197 } else if (button == 'r' || button == 'R') {
198 midend_stop_anim(me);
199 midend_restart_game(me);
201 return 1; /* never animate */
202 } else if (button == 'u' || button == 'u' ||
203 button == '\x1A' || button == '\x1F') {
204 midend_stop_anim(me);
205 if (!midend_undo(me))
207 } else if (button == '\x12') {
208 midend_stop_anim(me);
209 if (!midend_redo(me))
211 } else if (button == 'q' || button == 'Q' || button == '\x11') {
212 me->ourgame->free_game(oldstate);
215 game_state *s = me->ourgame->make_move(me->states[me->statepos-1],
216 me->ui, x, y, button);
218 if (s == me->states[me->statepos-1]) {
220 * make_move() is allowed to return its input state to
221 * indicate that although no move has been made, the UI
222 * state has been updated and a redraw is called for.
227 midend_stop_anim(me);
228 while (me->nstates > me->statepos)
229 me->ourgame->free_game(me->states[--me->nstates]);
231 me->states[me->nstates] = s;
232 me->statepos = ++me->nstates;
235 me->ourgame->free_game(oldstate);
241 * See if this move requires an animation.
243 anim_time = me->ourgame->anim_length(oldstate, me->states[me->statepos-1],
246 me->oldstate = oldstate;
248 me->anim_time = anim_time;
251 midend_finish_move(me);
257 activate_timer(me->frontend);
262 int midend_process_key(midend_data *me, int x, int y, int button)
267 * Harmonise mouse drag and release messages.
269 * Some front ends might accidentally switch from sending, say,
270 * RIGHT_DRAG messages to sending LEFT_DRAG, half way through a
271 * drag. (This can happen on the Mac, for example, since
272 * RIGHT_DRAG is usually done using Command+drag, and if the
273 * user accidentally releases Command half way through the drag
274 * then there will be trouble.)
276 * It would be an O(number of front ends) annoyance to fix this
277 * in the front ends, but an O(number of back ends) annoyance
278 * to have each game capable of dealing with it. Therefore, we
279 * fix it _here_ in the common midend code so that it only has
282 * The possible ways in which things can go screwy in the front
285 * - in a system containing multiple physical buttons button
286 * presses can inadvertently overlap. We can see ABab (caps
287 * meaning button-down and lowercase meaning button-up) when
288 * the user had semantically intended AaBb.
290 * - in a system where one button is simulated by means of a
291 * modifier key and another button, buttons can mutate
292 * between press and release (possibly during drag). So we
293 * can see Ab instead of Aa.
295 * Definite requirements are:
297 * - button _presses_ must never be invented or destroyed. If
298 * the user presses two buttons in succession, the button
299 * presses must be transferred to the backend unchanged. So
300 * if we see AaBb , that's fine; if we see ABab (the button
301 * presses inadvertently overlapped) we must somehow
302 * `correct' it to AaBb.
304 * - every mouse action must end up looking like a press, zero
305 * or more drags, then a release. This allows back ends to
306 * make the _assumption_ that incoming mouse data will be
307 * sane in this regard, and not worry about the details.
309 * So my policy will be:
311 * - treat any button-up as a button-up for the currently
312 * pressed button, or ignore it if there is no currently
315 * - treat any drag as a drag for the currently pressed
316 * button, or ignore it if there is no currently pressed
319 * - if we see a button-down while another button is currently
320 * pressed, invent a button-up for the first one and then
321 * pass the button-down through as before.
324 if (IS_MOUSE_DRAG(button) || IS_MOUSE_RELEASE(button)) {
325 if (me->pressed_mouse_button) {
326 if (IS_MOUSE_DRAG(button)) {
327 button = me->pressed_mouse_button +
328 (LEFT_DRAG - LEFT_BUTTON);
330 button = me->pressed_mouse_button +
331 (LEFT_RELEASE - LEFT_BUTTON);
334 return ret; /* ignore it */
335 } else if (IS_MOUSE_DOWN(button) && me->pressed_mouse_button) {
337 * Fabricate a button-up for the previously pressed button.
339 ret = ret && midend_really_process_key
340 (me, x, y, (me->pressed_mouse_button +
341 (LEFT_RELEASE - LEFT_BUTTON)));
345 * Now send on the event we originally received.
347 ret = ret && midend_really_process_key(me, x, y, button);
350 * And update the currently pressed button.
352 if (IS_MOUSE_RELEASE(button))
353 me->pressed_mouse_button = 0;
354 else if (IS_MOUSE_DOWN(button))
355 me->pressed_mouse_button = button;
360 void midend_redraw(midend_data *me)
362 if (me->statepos > 0 && me->drawstate) {
363 start_draw(me->frontend);
364 if (me->oldstate && me->anim_time > 0 &&
365 me->anim_pos < me->anim_time) {
366 assert(me->dir != 0);
367 me->ourgame->redraw(me->frontend, me->drawstate, me->oldstate,
368 me->states[me->statepos-1], me->dir,
369 me->ui, me->anim_pos, me->flash_pos);
371 me->ourgame->redraw(me->frontend, me->drawstate, NULL,
372 me->states[me->statepos-1], +1 /*shrug*/,
373 me->ui, 0.0, me->flash_pos);
375 end_draw(me->frontend);
379 void midend_timer(midend_data *me, float tplus)
381 me->anim_pos += tplus;
382 if (me->anim_pos >= me->anim_time ||
383 me->anim_time == 0 || !me->oldstate) {
384 if (me->anim_time > 0)
385 midend_finish_move(me);
387 me->flash_pos += tplus;
388 if (me->flash_pos >= me->flash_time || me->flash_time == 0) {
389 me->flash_pos = me->flash_time = 0;
391 if (me->flash_time == 0 && me->anim_time == 0)
392 deactivate_timer(me->frontend);
396 float *midend_colours(midend_data *me, int *ncolours)
398 game_state *state = NULL;
401 if (me->nstates == 0) {
402 char *seed = me->ourgame->new_seed(me->params, me->random);
403 state = me->ourgame->new_game(me->params, seed);
406 state = me->states[0];
408 ret = me->ourgame->colours(me->frontend, state, ncolours);
410 if (me->nstates == 0)
411 me->ourgame->free_game(state);
416 int midend_num_presets(midend_data *me)
422 while (me->ourgame->fetch_preset(me->npresets, &name, &preset)) {
423 if (me->presetsize <= me->npresets) {
424 me->presetsize = me->npresets + 10;
425 me->presets = sresize(me->presets, me->presetsize,
427 me->preset_names = sresize(me->preset_names, me->presetsize,
431 me->presets[me->npresets] = preset;
432 me->preset_names[me->npresets] = name;
440 void midend_fetch_preset(midend_data *me, int n,
441 char **name, game_params **params)
443 assert(n >= 0 && n < me->npresets);
444 *name = me->preset_names[n];
445 *params = me->presets[n];
448 int midend_wants_statusbar(midend_data *me)
450 return me->ourgame->wants_statusbar();
453 config_item *midend_get_config(midend_data *me, int which, char **wintitle)
455 char *titlebuf, *parstr;
458 titlebuf = snewn(40 + strlen(me->ourgame->name), char);
462 sprintf(titlebuf, "%s configuration", me->ourgame->name);
463 *wintitle = dupstr(titlebuf);
464 return me->ourgame->configure(me->params);
466 sprintf(titlebuf, "%s game selection", me->ourgame->name);
467 *wintitle = dupstr(titlebuf);
469 ret = snewn(2, config_item);
471 ret[0].type = C_STRING;
472 ret[0].name = "Game ID";
475 * The text going in here will be a string encoding of the
476 * parameters, plus a colon, plus the game seed. This is a
479 parstr = me->ourgame->encode_params(me->params);
480 ret[0].sval = snewn(strlen(parstr) + strlen(me->seed) + 2, char);
481 sprintf(ret[0].sval, "%s:%s", parstr, me->seed);
485 ret[1].name = ret[1].sval = NULL;
491 assert(!"We shouldn't be here");
495 char *midend_game_id(midend_data *me, char *id, int def_seed)
497 char *error, *par, *seed;
500 seed = strchr(id, ':');
504 * We have a colon separating parameters from game seed. So
505 * `par' now points to the parameters string, and `seed' to
512 * We only have one string. Depending on `def_seed', we
513 * take it to be either parameters or seed.
525 params = me->ourgame->decode_params(par);
526 error = me->ourgame->validate_params(params);
528 me->ourgame->free_params(params);
531 me->ourgame->free_params(me->params);
536 error = me->ourgame->validate_seed(me->params, seed);
541 me->seed = dupstr(seed);
542 me->fresh_seed = TRUE;
548 char *midend_set_config(midend_data *me, int which, config_item *cfg)
555 params = me->ourgame->custom_params(cfg);
556 error = me->ourgame->validate_params(params);
559 me->ourgame->free_params(params);
563 me->ourgame->free_params(me->params);
568 error = midend_game_id(me, cfg[0].sval, TRUE);