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/quit.
16 enum { DEF_PARAMS, DEF_SEED, DEF_DESC }; /* for midend_game_id_int */
18 struct midend_state_entry {
20 int special; /* created by solve or restart */
29 game_aux_info *aux_info;
30 enum { GOT_SEED, GOT_DESC, GOT_NOTHING } genmode;
31 int nstates, statesize, statepos;
33 game_params **presets;
35 int npresets, presetsize;
37 game_params *params, *tmpparams;
38 struct midend_state_entry *states;
39 game_drawstate *drawstate;
42 float anim_time, anim_pos;
43 float flash_time, flash_pos;
46 int pressed_mouse_button;
49 #define ensure(me) do { \
50 if ((me)->nstates >= (me)->statesize) { \
51 (me)->statesize = (me)->nstates + 128; \
52 (me)->states = sresize((me)->states, (me)->statesize, \
53 struct midend_state_entry); \
57 midend_data *midend_new(frontend *fe, const game *ourgame)
59 midend_data *me = snew(midend_data);
63 get_random_seed(&randseed, &randseedsize);
66 me->ourgame = ourgame;
67 me->random = random_init(randseed, randseedsize);
68 me->nstates = me->statesize = me->statepos = 0;
70 me->params = ourgame->default_params();
75 me->genmode = GOT_NOTHING;
79 me->preset_names = NULL;
80 me->npresets = me->presetsize = 0;
81 me->anim_time = me->anim_pos = 0.0F;
82 me->flash_time = me->flash_pos = 0.0F;
85 me->pressed_mouse_button = 0;
92 void midend_free(midend_data *me)
97 random_free(me->random);
99 me->ourgame->free_aux_info(me->aux_info);
100 me->ourgame->free_params(me->params);
102 me->ourgame->free_params(me->tmpparams);
106 void midend_size(midend_data *me, int *x, int *y)
108 me->ourgame->size(me->params, x, y);
111 void midend_set_params(midend_data *me, game_params *params)
113 me->ourgame->free_params(me->params);
114 me->params = me->ourgame->dup_params(params);
117 void midend_new_game(midend_data *me)
119 while (me->nstates > 0)
120 me->ourgame->free_game(me->states[--me->nstates].state);
123 me->ourgame->free_drawstate(me->drawstate);
125 assert(me->nstates == 0);
127 if (me->genmode == GOT_DESC) {
128 me->genmode = GOT_NOTHING;
132 if (me->genmode == GOT_SEED) {
133 me->genmode = GOT_NOTHING;
136 * Generate a new random seed. 15 digits comes to about
137 * 48 bits, which should be more than enough.
142 for (i = 0; i < 15; i++)
143 newseed[i] = '0' + random_upto(me->random, 10);
145 me->seedstr = dupstr(newseed);
150 me->ourgame->free_aux_info(me->aux_info);
153 rs = random_init(me->seedstr, strlen(me->seedstr));
154 me->desc = me->ourgame->new_desc
155 (me->tmpparams ? me->tmpparams : me->params, rs, &me->aux_info);
159 me->ourgame->free_params(me->tmpparams);
160 me->tmpparams = NULL;
165 me->states[me->nstates].state = me->ourgame->new_game(me->params, me->desc);
166 me->states[me->nstates].special = TRUE;
169 me->drawstate = me->ourgame->new_drawstate(me->states[0].state);
171 me->ourgame->free_ui(me->ui);
172 me->ui = me->ourgame->new_ui(me->states[0].state);
173 me->pressed_mouse_button = 0;
176 static int midend_undo(midend_data *me)
178 if (me->statepos > 1) {
186 static int midend_redo(midend_data *me)
188 if (me->statepos < me->nstates) {
196 static void midend_finish_move(midend_data *me)
201 * We do not flash if the later of the two states is special.
202 * This covers both forward Solve moves and backward (undone)
205 if ((me->oldstate || me->statepos > 1) &&
206 ((me->dir > 0 && !me->states[me->statepos-1].special) ||
207 (me->dir < 0 && me->statepos < me->nstates &&
208 !me->states[me->statepos].special))) {
209 flashtime = me->ourgame->flash_length(me->oldstate ? me->oldstate :
210 me->states[me->statepos-2].state,
211 me->states[me->statepos-1].state,
212 me->oldstate ? me->dir : +1);
214 me->flash_pos = 0.0F;
215 me->flash_time = flashtime;
220 me->ourgame->free_game(me->oldstate);
222 me->anim_pos = me->anim_time = 0;
225 if (me->flash_time == 0 && me->anim_time == 0)
226 deactivate_timer(me->frontend);
228 activate_timer(me->frontend);
231 static void midend_stop_anim(midend_data *me)
233 if (me->oldstate || me->anim_time) {
234 midend_finish_move(me);
239 void midend_restart_game(midend_data *me)
243 midend_stop_anim(me);
245 assert(me->statepos >= 1);
246 if (me->statepos == 1)
247 return; /* no point doing anything at all! */
249 s = me->ourgame->dup_game(me->states[0].state);
252 * Now enter the restarted state as the next move.
254 midend_stop_anim(me);
255 while (me->nstates > me->statepos)
256 me->ourgame->free_game(me->states[--me->nstates].state);
258 me->states[me->nstates].state = s;
259 me->states[me->nstates].special = TRUE; /* we just restarted */
260 me->statepos = ++me->nstates;
262 midend_finish_move(me);
264 activate_timer(me->frontend);
267 static int midend_really_process_key(midend_data *me, int x, int y, int button)
269 game_state *oldstate =
270 me->ourgame->dup_game(me->states[me->statepos - 1].state);
271 int special = FALSE, gotspecial = FALSE;
274 if (button == 'n' || button == 'N' || button == '\x0E') {
275 midend_stop_anim(me);
278 return 1; /* never animate */
279 } else if (button == 'u' || button == 'u' ||
280 button == '\x1A' || button == '\x1F') {
281 midend_stop_anim(me);
282 special = me->states[me->statepos-1].special;
284 if (!midend_undo(me))
286 } else if (button == 'r' || button == 'R' ||
288 midend_stop_anim(me);
289 if (!midend_redo(me))
291 } else if (button == 'q' || button == 'Q' || button == '\x11') {
292 me->ourgame->free_game(oldstate);
296 me->ourgame->make_move(me->states[me->statepos-1].state,
297 me->ui, x, y, button);
299 if (s == me->states[me->statepos-1].state) {
301 * make_move() is allowed to return its input state to
302 * indicate that although no move has been made, the UI
303 * state has been updated and a redraw is called for.
308 midend_stop_anim(me);
309 while (me->nstates > me->statepos)
310 me->ourgame->free_game(me->states[--me->nstates].state);
312 me->states[me->nstates].state = s;
313 me->states[me->nstates].special = FALSE; /* normal move */
314 me->statepos = ++me->nstates;
317 me->ourgame->free_game(oldstate);
323 special = me->states[me->statepos-1].special;
326 * See if this move requires an animation.
331 anim_time = me->ourgame->anim_length(oldstate,
332 me->states[me->statepos-1].state,
336 me->oldstate = oldstate;
338 me->anim_time = anim_time;
341 midend_finish_move(me);
347 activate_timer(me->frontend);
352 int midend_process_key(midend_data *me, int x, int y, int button)
357 * Harmonise mouse drag and release messages.
359 * Some front ends might accidentally switch from sending, say,
360 * RIGHT_DRAG messages to sending LEFT_DRAG, half way through a
361 * drag. (This can happen on the Mac, for example, since
362 * RIGHT_DRAG is usually done using Command+drag, and if the
363 * user accidentally releases Command half way through the drag
364 * then there will be trouble.)
366 * It would be an O(number of front ends) annoyance to fix this
367 * in the front ends, but an O(number of back ends) annoyance
368 * to have each game capable of dealing with it. Therefore, we
369 * fix it _here_ in the common midend code so that it only has
372 * The possible ways in which things can go screwy in the front
375 * - in a system containing multiple physical buttons button
376 * presses can inadvertently overlap. We can see ABab (caps
377 * meaning button-down and lowercase meaning button-up) when
378 * the user had semantically intended AaBb.
380 * - in a system where one button is simulated by means of a
381 * modifier key and another button, buttons can mutate
382 * between press and release (possibly during drag). So we
383 * can see Ab instead of Aa.
385 * Definite requirements are:
387 * - button _presses_ must never be invented or destroyed. If
388 * the user presses two buttons in succession, the button
389 * presses must be transferred to the backend unchanged. So
390 * if we see AaBb , that's fine; if we see ABab (the button
391 * presses inadvertently overlapped) we must somehow
392 * `correct' it to AaBb.
394 * - every mouse action must end up looking like a press, zero
395 * or more drags, then a release. This allows back ends to
396 * make the _assumption_ that incoming mouse data will be
397 * sane in this regard, and not worry about the details.
399 * So my policy will be:
401 * - treat any button-up as a button-up for the currently
402 * pressed button, or ignore it if there is no currently
405 * - treat any drag as a drag for the currently pressed
406 * button, or ignore it if there is no currently pressed
409 * - if we see a button-down while another button is currently
410 * pressed, invent a button-up for the first one and then
411 * pass the button-down through as before.
414 if (IS_MOUSE_DRAG(button) || IS_MOUSE_RELEASE(button)) {
415 if (me->pressed_mouse_button) {
416 if (IS_MOUSE_DRAG(button)) {
417 button = me->pressed_mouse_button +
418 (LEFT_DRAG - LEFT_BUTTON);
420 button = me->pressed_mouse_button +
421 (LEFT_RELEASE - LEFT_BUTTON);
424 return ret; /* ignore it */
425 } else if (IS_MOUSE_DOWN(button) && me->pressed_mouse_button) {
427 * Fabricate a button-up for the previously pressed button.
429 ret = ret && midend_really_process_key
430 (me, x, y, (me->pressed_mouse_button +
431 (LEFT_RELEASE - LEFT_BUTTON)));
435 * Now send on the event we originally received.
437 ret = ret && midend_really_process_key(me, x, y, button);
440 * And update the currently pressed button.
442 if (IS_MOUSE_RELEASE(button))
443 me->pressed_mouse_button = 0;
444 else if (IS_MOUSE_DOWN(button))
445 me->pressed_mouse_button = button;
450 void midend_redraw(midend_data *me)
452 if (me->statepos > 0 && me->drawstate) {
453 start_draw(me->frontend);
454 if (me->oldstate && me->anim_time > 0 &&
455 me->anim_pos < me->anim_time) {
456 assert(me->dir != 0);
457 me->ourgame->redraw(me->frontend, me->drawstate, me->oldstate,
458 me->states[me->statepos-1].state, me->dir,
459 me->ui, me->anim_pos, me->flash_pos);
461 me->ourgame->redraw(me->frontend, me->drawstate, NULL,
462 me->states[me->statepos-1].state, +1 /*shrug*/,
463 me->ui, 0.0, me->flash_pos);
465 end_draw(me->frontend);
469 void midend_timer(midend_data *me, float tplus)
471 me->anim_pos += tplus;
472 if (me->anim_pos >= me->anim_time ||
473 me->anim_time == 0 || !me->oldstate) {
474 if (me->anim_time > 0)
475 midend_finish_move(me);
477 me->flash_pos += tplus;
478 if (me->flash_pos >= me->flash_time || me->flash_time == 0) {
479 me->flash_pos = me->flash_time = 0;
481 if (me->flash_time == 0 && me->anim_time == 0)
482 deactivate_timer(me->frontend);
486 float *midend_colours(midend_data *me, int *ncolours)
488 game_state *state = NULL;
491 if (me->nstates == 0) {
492 game_aux_info *aux = NULL;
493 char *desc = me->ourgame->new_desc(me->params, me->random, &aux);
494 state = me->ourgame->new_game(me->params, desc);
497 me->ourgame->free_aux_info(aux);
499 state = me->states[0].state;
501 ret = me->ourgame->colours(me->frontend, state, ncolours);
507 * Allow environment-based overrides for the standard
508 * colours by defining variables along the lines of
509 * `NET_COLOUR_4=6000c0'.
512 for (i = 0; i < *ncolours; i++) {
514 unsigned int r, g, b;
517 sprintf(buf, "%s_COLOUR_%d", me->ourgame->name, i);
518 for (j = 0; buf[j]; j++)
519 buf[j] = toupper((unsigned char)buf[j]);
520 if ((e = getenv(buf)) != NULL &&
521 sscanf(e, "%2x%2x%2x", &r, &g, &b) == 3) {
522 ret[i*3 + 0] = r / 255.0;
523 ret[i*3 + 1] = g / 255.0;
524 ret[i*3 + 2] = b / 255.0;
529 if (me->nstates == 0)
530 me->ourgame->free_game(state);
535 int midend_num_presets(midend_data *me)
541 while (me->ourgame->fetch_preset(me->npresets, &name, &preset)) {
542 if (me->presetsize <= me->npresets) {
543 me->presetsize = me->npresets + 10;
544 me->presets = sresize(me->presets, me->presetsize,
546 me->preset_names = sresize(me->preset_names, me->presetsize,
550 me->presets[me->npresets] = preset;
551 me->preset_names[me->npresets] = name;
558 * Allow environment-based extensions to the preset list by
559 * defining a variable along the lines of `SOLO_PRESETS=2x3
560 * Advanced:2x3da'. Colon-separated list of items,
561 * alternating between textual titles in the menu and
562 * encoded parameter strings.
564 char buf[80], *e, *p;
567 sprintf(buf, "%s_PRESETS", me->ourgame->name);
568 for (j = 0; buf[j]; j++)
569 buf[j] = toupper((unsigned char)buf[j]);
571 if ((e = getenv(buf)) != NULL) {
579 while (*p && *p != ':') p++;
582 while (*p && *p != ':') p++;
585 preset = me->ourgame->default_params();
586 me->ourgame->decode_params(preset, val);
588 if (me->ourgame->validate_params(preset)) {
589 /* Drop this one from the list. */
590 me->ourgame->free_params(preset);
594 if (me->presetsize <= me->npresets) {
595 me->presetsize = me->npresets + 10;
596 me->presets = sresize(me->presets, me->presetsize,
598 me->preset_names = sresize(me->preset_names,
599 me->presetsize, char *);
602 me->presets[me->npresets] = preset;
603 me->preset_names[me->npresets] = name;
612 void midend_fetch_preset(midend_data *me, int n,
613 char **name, game_params **params)
615 assert(n >= 0 && n < me->npresets);
616 *name = me->preset_names[n];
617 *params = me->presets[n];
620 int midend_wants_statusbar(midend_data *me)
622 return me->ourgame->wants_statusbar();
625 config_item *midend_get_config(midend_data *me, int which, char **wintitle)
627 char *titlebuf, *parstr;
630 titlebuf = snewn(40 + strlen(me->ourgame->name), char);
634 sprintf(titlebuf, "%s configuration", me->ourgame->name);
635 *wintitle = dupstr(titlebuf);
636 return me->ourgame->configure(me->params);
639 sprintf(titlebuf, "%s %s selection", me->ourgame->name,
640 which == CFG_SEED ? "random" : "game");
641 *wintitle = dupstr(titlebuf);
643 ret = snewn(2, config_item);
645 ret[0].type = C_STRING;
646 if (which == CFG_SEED)
647 ret[0].name = "Game random seed";
649 ret[0].name = "Game ID";
652 * For CFG_DESC the text going in here will be a string
653 * encoding of the restricted parameters, plus a colon,
654 * plus the game description. For CFG_SEED it will be the
655 * full parameters, plus a hash, plus the random seed data.
656 * Either of these is a valid full game ID (although only
657 * the former is likely to persist across many code
660 parstr = me->ourgame->encode_params(me->params, which == CFG_SEED);
661 if (which == CFG_DESC) {
662 ret[0].sval = snewn(strlen(parstr) + strlen(me->desc) + 2, char);
663 sprintf(ret[0].sval, "%s:%s", parstr, me->desc);
664 } else if (me->seedstr) {
665 ret[0].sval = snewn(strlen(parstr) + strlen(me->seedstr) + 2, char);
666 sprintf(ret[0].sval, "%s#%s", parstr, me->seedstr);
669 * If the current game was not randomly generated, the
670 * best we can do is to give a template for typing a
673 ret[0].sval = snewn(strlen(parstr) + 2, char);
674 sprintf(ret[0].sval, "%s#", parstr);
679 ret[1].name = ret[1].sval = NULL;
685 assert(!"We shouldn't be here");
689 static char *midend_game_id_int(midend_data *me, char *id, int defmode)
691 char *error, *par, *desc, *seed;
693 seed = strchr(id, '#');
694 desc = strchr(id, ':');
696 if (desc && (!seed || desc < seed)) {
698 * We have a colon separating parameters from game
699 * description. So `par' now points to the parameters
700 * string, and `desc' to the description string.
705 } else if (seed && (!desc || seed < desc)) {
707 * We have a hash separating parameters from random seed.
708 * So `par' now points to the parameters string, and `seed'
709 * to the seed string.
716 * We only have one string. Depending on `defmode', we take
717 * it to be either parameters, seed or description.
719 if (defmode == DEF_SEED) {
722 } else if (defmode == DEF_DESC) {
732 game_params *tmpparams;
733 tmpparams = me->ourgame->dup_params(me->params);
734 me->ourgame->decode_params(tmpparams, par);
735 error = me->ourgame->validate_params(tmpparams);
737 me->ourgame->free_params(tmpparams);
741 me->ourgame->free_params(me->tmpparams);
742 me->tmpparams = tmpparams;
745 * Now filter only the persistent parts of this state into
746 * the long-term params structure, unless we've _only_
747 * received a params string in which case the whole lot is
751 char *tmpstr = me->ourgame->encode_params(tmpparams, FALSE);
752 me->ourgame->decode_params(me->params, tmpstr);
754 me->ourgame->free_params(me->params);
755 me->params = me->ourgame->dup_params(tmpparams);
760 error = me->ourgame->validate_desc(me->params, desc);
765 me->desc = dupstr(desc);
766 me->genmode = GOT_DESC;
768 me->ourgame->free_aux_info(me->aux_info);
774 me->seedstr = dupstr(seed);
775 me->genmode = GOT_SEED;
781 char *midend_game_id(midend_data *me, char *id)
783 return midend_game_id_int(me, id, DEF_PARAMS);
786 char *midend_set_config(midend_data *me, int which, config_item *cfg)
793 params = me->ourgame->custom_params(cfg);
794 error = me->ourgame->validate_params(params);
797 me->ourgame->free_params(params);
801 me->ourgame->free_params(me->params);
807 error = midend_game_id_int(me, cfg[0].sval,
808 (which == CFG_SEED ? DEF_SEED : DEF_DESC));
817 char *midend_text_format(midend_data *me)
819 if (me->ourgame->can_format_as_text && me->statepos > 0)
820 return me->ourgame->text_format(me->states[me->statepos-1].state);
825 char *midend_solve(midend_data *me)
830 if (!me->ourgame->can_solve)
831 return "This game does not support the Solve operation";
833 if (me->statepos < 1)
834 return "No game set up to solve"; /* _shouldn't_ happen! */
836 msg = "Solve operation failed"; /* game _should_ overwrite on error */
837 s = me->ourgame->solve(me->states[0].state, me->aux_info, &msg);
842 * Now enter the solved state as the next move.
844 midend_stop_anim(me);
845 while (me->nstates > me->statepos)
846 me->ourgame->free_game(me->states[--me->nstates].state);
848 me->states[me->nstates].state = s;
849 me->states[me->nstates].special = TRUE; /* created using solve */
850 me->statepos = ++me->nstates;
852 midend_finish_move(me);
854 activate_timer(me->frontend);