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, *curparams;
38 struct midend_state_entry *states;
39 game_drawstate *drawstate;
42 float anim_time, anim_pos;
43 float flash_time, flash_pos;
50 int pressed_mouse_button;
53 #define ensure(me) do { \
54 if ((me)->nstates >= (me)->statesize) { \
55 (me)->statesize = (me)->nstates + 128; \
56 (me)->states = sresize((me)->states, (me)->statesize, \
57 struct midend_state_entry); \
61 midend_data *midend_new(frontend *fe, const game *ourgame)
63 midend_data *me = snew(midend_data);
67 get_random_seed(&randseed, &randseedsize);
70 me->ourgame = ourgame;
71 me->random = random_init(randseed, randseedsize);
72 me->nstates = me->statesize = me->statepos = 0;
74 me->params = ourgame->default_params();
79 me->genmode = GOT_NOTHING;
83 me->preset_names = NULL;
84 me->npresets = me->presetsize = 0;
85 me->anim_time = me->anim_pos = 0.0F;
86 me->flash_time = me->flash_pos = 0.0F;
89 me->pressed_mouse_button = 0;
90 me->laststatus = NULL;
99 void midend_free(midend_data *me)
104 random_free(me->random);
106 me->ourgame->free_aux_info(me->aux_info);
107 me->ourgame->free_params(me->params);
109 me->ourgame->free_params(me->curparams);
110 sfree(me->laststatus);
114 void midend_size(midend_data *me, int *x, int *y)
116 me->ourgame->size(me->params, x, y);
119 void midend_set_params(midend_data *me, game_params *params)
121 me->ourgame->free_params(me->params);
122 me->params = me->ourgame->dup_params(params);
125 static void midend_set_timer(midend_data *me)
127 me->timing = (me->ourgame->is_timed &&
128 me->ourgame->timing_state(me->states[me->statepos-1].state));
129 if (me->timing || me->flash_time || me->anim_time)
130 activate_timer(me->frontend);
132 deactivate_timer(me->frontend);
135 void midend_new_game(midend_data *me)
137 while (me->nstates > 0)
138 me->ourgame->free_game(me->states[--me->nstates].state);
141 me->ourgame->free_drawstate(me->drawstate);
143 assert(me->nstates == 0);
145 if (me->genmode == GOT_DESC) {
146 me->genmode = GOT_NOTHING;
150 if (me->genmode == GOT_SEED) {
151 me->genmode = GOT_NOTHING;
154 * Generate a new random seed. 15 digits comes to about
155 * 48 bits, which should be more than enough.
157 * I'll avoid putting a leading zero on the number,
158 * just in case it confuses anybody who thinks it's
159 * processed as an integer rather than a string.
164 newseed[0] = '1' + random_upto(me->random, 9);
165 for (i = 1; i < 15; i++)
166 newseed[i] = '0' + random_upto(me->random, 10);
168 me->seedstr = dupstr(newseed);
171 me->ourgame->free_params(me->curparams);
172 me->curparams = me->ourgame->dup_params(me->params);
177 me->ourgame->free_aux_info(me->aux_info);
180 rs = random_init(me->seedstr, strlen(me->seedstr));
181 me->desc = me->ourgame->new_desc(me->curparams, rs,
182 &me->aux_info, TRUE);
187 me->states[me->nstates].state =
188 me->ourgame->new_game(me, me->params, me->desc);
189 me->states[me->nstates].special = TRUE;
192 me->drawstate = me->ourgame->new_drawstate(me->states[0].state);
194 midend_set_timer(me);
196 me->ourgame->free_ui(me->ui);
197 me->ui = me->ourgame->new_ui(me->states[0].state);
198 me->pressed_mouse_button = 0;
201 static int midend_undo(midend_data *me)
203 if (me->statepos > 1) {
211 static int midend_redo(midend_data *me)
213 if (me->statepos < me->nstates) {
221 static void midend_finish_move(midend_data *me)
226 * We do not flash if the later of the two states is special.
227 * This covers both forward Solve moves and backward (undone)
230 if ((me->oldstate || me->statepos > 1) &&
231 ((me->dir > 0 && !me->states[me->statepos-1].special) ||
232 (me->dir < 0 && me->statepos < me->nstates &&
233 !me->states[me->statepos].special))) {
234 flashtime = me->ourgame->flash_length(me->oldstate ? me->oldstate :
235 me->states[me->statepos-2].state,
236 me->states[me->statepos-1].state,
237 me->oldstate ? me->dir : +1,
240 me->flash_pos = 0.0F;
241 me->flash_time = flashtime;
246 me->ourgame->free_game(me->oldstate);
248 me->anim_pos = me->anim_time = 0;
251 midend_set_timer(me);
254 static void midend_stop_anim(midend_data *me)
256 if (me->oldstate || me->anim_time) {
257 midend_finish_move(me);
262 void midend_restart_game(midend_data *me)
266 midend_stop_anim(me);
268 assert(me->statepos >= 1);
269 if (me->statepos == 1)
270 return; /* no point doing anything at all! */
272 s = me->ourgame->dup_game(me->states[0].state);
275 * Now enter the restarted state as the next move.
277 midend_stop_anim(me);
278 while (me->nstates > me->statepos)
279 me->ourgame->free_game(me->states[--me->nstates].state);
281 me->states[me->nstates].state = s;
282 me->states[me->nstates].special = TRUE; /* we just restarted */
283 me->statepos = ++me->nstates;
285 midend_finish_move(me);
287 midend_set_timer(me);
290 static int midend_really_process_key(midend_data *me, int x, int y, int button)
292 game_state *oldstate =
293 me->ourgame->dup_game(me->states[me->statepos - 1].state);
294 int special = FALSE, gotspecial = FALSE;
297 if (button == 'n' || button == 'N' || button == '\x0E') {
298 midend_stop_anim(me);
301 return 1; /* never animate */
302 } else if (button == 'u' || button == 'u' ||
303 button == '\x1A' || button == '\x1F') {
304 midend_stop_anim(me);
305 special = me->states[me->statepos-1].special;
307 if (!midend_undo(me))
309 } else if (button == 'r' || button == 'R' ||
311 midend_stop_anim(me);
312 if (!midend_redo(me))
314 } else if (button == 'q' || button == 'Q' || button == '\x11') {
315 me->ourgame->free_game(oldstate);
319 me->ourgame->make_move(me->states[me->statepos-1].state,
320 me->ui, x, y, button);
322 if (s == me->states[me->statepos-1].state) {
324 * make_move() is allowed to return its input state to
325 * indicate that although no move has been made, the UI
326 * state has been updated and a redraw is called for.
331 midend_stop_anim(me);
332 while (me->nstates > me->statepos)
333 me->ourgame->free_game(me->states[--me->nstates].state);
335 me->states[me->nstates].state = s;
336 me->states[me->nstates].special = FALSE; /* normal move */
337 me->statepos = ++me->nstates;
340 me->ourgame->free_game(oldstate);
346 special = me->states[me->statepos-1].special;
349 * See if this move requires an animation.
354 anim_time = me->ourgame->anim_length(oldstate,
355 me->states[me->statepos-1].state,
359 me->oldstate = oldstate;
361 me->anim_time = anim_time;
364 midend_finish_move(me);
370 midend_set_timer(me);
375 int midend_process_key(midend_data *me, int x, int y, int button)
380 * Harmonise mouse drag and release messages.
382 * Some front ends might accidentally switch from sending, say,
383 * RIGHT_DRAG messages to sending LEFT_DRAG, half way through a
384 * drag. (This can happen on the Mac, for example, since
385 * RIGHT_DRAG is usually done using Command+drag, and if the
386 * user accidentally releases Command half way through the drag
387 * then there will be trouble.)
389 * It would be an O(number of front ends) annoyance to fix this
390 * in the front ends, but an O(number of back ends) annoyance
391 * to have each game capable of dealing with it. Therefore, we
392 * fix it _here_ in the common midend code so that it only has
395 * The possible ways in which things can go screwy in the front
398 * - in a system containing multiple physical buttons button
399 * presses can inadvertently overlap. We can see ABab (caps
400 * meaning button-down and lowercase meaning button-up) when
401 * the user had semantically intended AaBb.
403 * - in a system where one button is simulated by means of a
404 * modifier key and another button, buttons can mutate
405 * between press and release (possibly during drag). So we
406 * can see Ab instead of Aa.
408 * Definite requirements are:
410 * - button _presses_ must never be invented or destroyed. If
411 * the user presses two buttons in succession, the button
412 * presses must be transferred to the backend unchanged. So
413 * if we see AaBb , that's fine; if we see ABab (the button
414 * presses inadvertently overlapped) we must somehow
415 * `correct' it to AaBb.
417 * - every mouse action must end up looking like a press, zero
418 * or more drags, then a release. This allows back ends to
419 * make the _assumption_ that incoming mouse data will be
420 * sane in this regard, and not worry about the details.
422 * So my policy will be:
424 * - treat any button-up as a button-up for the currently
425 * pressed button, or ignore it if there is no currently
428 * - treat any drag as a drag for the currently pressed
429 * button, or ignore it if there is no currently pressed
432 * - if we see a button-down while another button is currently
433 * pressed, invent a button-up for the first one and then
434 * pass the button-down through as before.
437 if (IS_MOUSE_DRAG(button) || IS_MOUSE_RELEASE(button)) {
438 if (me->pressed_mouse_button) {
439 if (IS_MOUSE_DRAG(button)) {
440 button = me->pressed_mouse_button +
441 (LEFT_DRAG - LEFT_BUTTON);
443 button = me->pressed_mouse_button +
444 (LEFT_RELEASE - LEFT_BUTTON);
447 return ret; /* ignore it */
448 } else if (IS_MOUSE_DOWN(button) && me->pressed_mouse_button) {
450 * Fabricate a button-up for the previously pressed button.
452 ret = ret && midend_really_process_key
453 (me, x, y, (me->pressed_mouse_button +
454 (LEFT_RELEASE - LEFT_BUTTON)));
458 * Now send on the event we originally received.
460 ret = ret && midend_really_process_key(me, x, y, button);
463 * And update the currently pressed button.
465 if (IS_MOUSE_RELEASE(button))
466 me->pressed_mouse_button = 0;
467 else if (IS_MOUSE_DOWN(button))
468 me->pressed_mouse_button = button;
473 void midend_redraw(midend_data *me)
475 if (me->statepos > 0 && me->drawstate) {
476 start_draw(me->frontend);
477 if (me->oldstate && me->anim_time > 0 &&
478 me->anim_pos < me->anim_time) {
479 assert(me->dir != 0);
480 me->ourgame->redraw(me->frontend, me->drawstate, me->oldstate,
481 me->states[me->statepos-1].state, me->dir,
482 me->ui, me->anim_pos, me->flash_pos);
484 me->ourgame->redraw(me->frontend, me->drawstate, NULL,
485 me->states[me->statepos-1].state, +1 /*shrug*/,
486 me->ui, 0.0, me->flash_pos);
488 end_draw(me->frontend);
492 void midend_timer(midend_data *me, float tplus)
494 me->anim_pos += tplus;
495 if (me->anim_pos >= me->anim_time ||
496 me->anim_time == 0 || !me->oldstate) {
497 if (me->anim_time > 0)
498 midend_finish_move(me);
501 me->flash_pos += tplus;
502 if (me->flash_pos >= me->flash_time || me->flash_time == 0) {
503 me->flash_pos = me->flash_time = 0;
509 float oldelapsed = me->elapsed;
510 me->elapsed += tplus;
511 if ((int)oldelapsed != (int)me->elapsed)
512 status_bar(me->frontend, me->laststatus ? me->laststatus : "");
515 midend_set_timer(me);
518 float *midend_colours(midend_data *me, int *ncolours)
520 game_state *state = NULL;
523 if (me->nstates == 0) {
524 game_aux_info *aux = NULL;
525 char *desc = me->ourgame->new_desc(me->params, me->random,
527 state = me->ourgame->new_game(me, me->params, desc);
530 me->ourgame->free_aux_info(aux);
532 state = me->states[0].state;
534 ret = me->ourgame->colours(me->frontend, state, ncolours);
540 * Allow environment-based overrides for the standard
541 * colours by defining variables along the lines of
542 * `NET_COLOUR_4=6000c0'.
545 for (i = 0; i < *ncolours; i++) {
547 unsigned int r, g, b;
550 sprintf(buf, "%s_COLOUR_%d", me->ourgame->name, i);
551 for (j = 0; buf[j]; j++)
552 buf[j] = toupper((unsigned char)buf[j]);
553 if ((e = getenv(buf)) != NULL &&
554 sscanf(e, "%2x%2x%2x", &r, &g, &b) == 3) {
555 ret[i*3 + 0] = r / 255.0;
556 ret[i*3 + 1] = g / 255.0;
557 ret[i*3 + 2] = b / 255.0;
562 if (me->nstates == 0)
563 me->ourgame->free_game(state);
568 int midend_num_presets(midend_data *me)
574 while (me->ourgame->fetch_preset(me->npresets, &name, &preset)) {
575 if (me->presetsize <= me->npresets) {
576 me->presetsize = me->npresets + 10;
577 me->presets = sresize(me->presets, me->presetsize,
579 me->preset_names = sresize(me->preset_names, me->presetsize,
583 me->presets[me->npresets] = preset;
584 me->preset_names[me->npresets] = name;
591 * Allow environment-based extensions to the preset list by
592 * defining a variable along the lines of `SOLO_PRESETS=2x3
593 * Advanced:2x3da'. Colon-separated list of items,
594 * alternating between textual titles in the menu and
595 * encoded parameter strings.
597 char buf[80], *e, *p;
600 sprintf(buf, "%s_PRESETS", me->ourgame->name);
601 for (j = 0; buf[j]; j++)
602 buf[j] = toupper((unsigned char)buf[j]);
604 if ((e = getenv(buf)) != NULL) {
612 while (*p && *p != ':') p++;
615 while (*p && *p != ':') p++;
618 preset = me->ourgame->default_params();
619 me->ourgame->decode_params(preset, val);
621 if (me->ourgame->validate_params(preset)) {
622 /* Drop this one from the list. */
623 me->ourgame->free_params(preset);
627 if (me->presetsize <= me->npresets) {
628 me->presetsize = me->npresets + 10;
629 me->presets = sresize(me->presets, me->presetsize,
631 me->preset_names = sresize(me->preset_names,
632 me->presetsize, char *);
635 me->presets[me->npresets] = preset;
636 me->preset_names[me->npresets] = name;
645 void midend_fetch_preset(midend_data *me, int n,
646 char **name, game_params **params)
648 assert(n >= 0 && n < me->npresets);
649 *name = me->preset_names[n];
650 *params = me->presets[n];
653 int midend_wants_statusbar(midend_data *me)
655 return me->ourgame->wants_statusbar();
658 void midend_supersede_game_desc(midend_data *me, char *desc)
661 me->desc = dupstr(desc);
664 config_item *midend_get_config(midend_data *me, int which, char **wintitle)
666 char *titlebuf, *parstr;
669 titlebuf = snewn(40 + strlen(me->ourgame->name), char);
673 sprintf(titlebuf, "%s configuration", me->ourgame->name);
674 *wintitle = dupstr(titlebuf);
675 return me->ourgame->configure(me->params);
678 sprintf(titlebuf, "%s %s selection", me->ourgame->name,
679 which == CFG_SEED ? "random" : "game");
680 *wintitle = dupstr(titlebuf);
682 ret = snewn(2, config_item);
684 ret[0].type = C_STRING;
685 if (which == CFG_SEED)
686 ret[0].name = "Game random seed";
688 ret[0].name = "Game ID";
691 * For CFG_DESC the text going in here will be a string
692 * encoding of the restricted parameters, plus a colon,
693 * plus the game description. For CFG_SEED it will be the
694 * full parameters, plus a hash, plus the random seed data.
695 * Either of these is a valid full game ID (although only
696 * the former is likely to persist across many code
699 parstr = me->ourgame->encode_params(me->curparams, which == CFG_SEED);
700 if (which == CFG_DESC) {
701 ret[0].sval = snewn(strlen(parstr) + strlen(me->desc) + 2, char);
702 sprintf(ret[0].sval, "%s:%s", parstr, me->desc);
703 } else if (me->seedstr) {
704 ret[0].sval = snewn(strlen(parstr) + strlen(me->seedstr) + 2, char);
705 sprintf(ret[0].sval, "%s#%s", parstr, me->seedstr);
708 * If the current game was not randomly generated, the
709 * best we can do is to give a template for typing a
712 ret[0].sval = snewn(strlen(parstr) + 2, char);
713 sprintf(ret[0].sval, "%s#", parstr);
718 ret[1].name = ret[1].sval = NULL;
724 assert(!"We shouldn't be here");
728 static char *midend_game_id_int(midend_data *me, char *id, int defmode)
730 char *error, *par, *desc, *seed;
732 seed = strchr(id, '#');
733 desc = strchr(id, ':');
735 if (desc && (!seed || desc < seed)) {
737 * We have a colon separating parameters from game
738 * description. So `par' now points to the parameters
739 * string, and `desc' to the description string.
744 } else if (seed && (!desc || seed < desc)) {
746 * We have a hash separating parameters from random seed.
747 * So `par' now points to the parameters string, and `seed'
748 * to the seed string.
755 * We only have one string. Depending on `defmode', we take
756 * it to be either parameters, seed or description.
758 if (defmode == DEF_SEED) {
761 } else if (defmode == DEF_DESC) {
771 game_params *tmpparams;
772 tmpparams = me->ourgame->dup_params(me->params);
773 me->ourgame->decode_params(tmpparams, par);
774 error = me->ourgame->validate_params(tmpparams);
776 me->ourgame->free_params(tmpparams);
780 me->ourgame->free_params(me->curparams);
781 me->curparams = tmpparams;
784 * Now filter only the persistent parts of this state into
785 * the long-term params structure, unless we've _only_
786 * received a params string in which case the whole lot is
790 char *tmpstr = me->ourgame->encode_params(tmpparams, FALSE);
791 me->ourgame->decode_params(me->params, tmpstr);
794 me->ourgame->free_params(me->params);
795 me->params = me->ourgame->dup_params(tmpparams);
805 error = me->ourgame->validate_desc(me->params, desc);
809 me->desc = dupstr(desc);
810 me->genmode = GOT_DESC;
812 me->ourgame->free_aux_info(me->aux_info);
817 me->seedstr = dupstr(seed);
818 me->genmode = GOT_SEED;
824 char *midend_game_id(midend_data *me, char *id)
826 return midend_game_id_int(me, id, DEF_PARAMS);
829 char *midend_set_config(midend_data *me, int which, config_item *cfg)
836 params = me->ourgame->custom_params(cfg);
837 error = me->ourgame->validate_params(params);
840 me->ourgame->free_params(params);
844 me->ourgame->free_params(me->params);
850 error = midend_game_id_int(me, cfg[0].sval,
851 (which == CFG_SEED ? DEF_SEED : DEF_DESC));
860 char *midend_text_format(midend_data *me)
862 if (me->ourgame->can_format_as_text && me->statepos > 0)
863 return me->ourgame->text_format(me->states[me->statepos-1].state);
868 char *midend_solve(midend_data *me)
873 if (!me->ourgame->can_solve)
874 return "This game does not support the Solve operation";
876 if (me->statepos < 1)
877 return "No game set up to solve"; /* _shouldn't_ happen! */
879 msg = "Solve operation failed"; /* game _should_ overwrite on error */
880 s = me->ourgame->solve(me->states[0].state, me->aux_info, &msg);
885 * Now enter the solved state as the next move.
887 midend_stop_anim(me);
888 while (me->nstates > me->statepos)
889 me->ourgame->free_game(me->states[--me->nstates].state);
891 me->states[me->nstates].state = s;
892 me->states[me->nstates].special = TRUE; /* created using solve */
893 me->statepos = ++me->nstates;
895 midend_finish_move(me);
897 midend_set_timer(me);
901 char *midend_rewrite_statusbar(midend_data *me, char *text)
904 * An important special case is that we are occasionally called
905 * with our own laststatus, to update the timer.
907 if (me->laststatus != text) {
908 sfree(me->laststatus);
909 me->laststatus = dupstr(text);
912 if (me->ourgame->is_timed) {
913 char timebuf[100], *ret;
919 sprintf(timebuf, "[%d:%02d] ", min, sec);
921 ret = snewn(strlen(timebuf) + strlen(text) + 1, char);
922 strcpy(ret, timebuf);