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 static void midend_free_game(midend_data *me)
101 while (me->nstates > 0)
102 me->ourgame->free_game(me->states[--me->nstates].state);
105 me->ourgame->free_drawstate(me->drawstate);
108 void midend_free(midend_data *me)
112 midend_free_game(me);
114 random_free(me->random);
119 me->ourgame->free_aux_info(me->aux_info);
120 me->ourgame->free_params(me->params);
122 for (i = 0; i < me->npresets; i++) {
123 sfree(me->presets[i]);
124 sfree(me->preset_names[i]);
127 sfree(me->preset_names);
130 me->ourgame->free_ui(me->ui);
132 me->ourgame->free_params(me->curparams);
133 sfree(me->laststatus);
137 void midend_size(midend_data *me, int *x, int *y)
139 me->ourgame->size(me->params, x, y);
142 void midend_set_params(midend_data *me, game_params *params)
144 me->ourgame->free_params(me->params);
145 me->params = me->ourgame->dup_params(params);
148 static void midend_set_timer(midend_data *me)
150 me->timing = (me->ourgame->is_timed &&
151 me->ourgame->timing_state(me->states[me->statepos-1].state));
152 if (me->timing || me->flash_time || me->anim_time)
153 activate_timer(me->frontend);
155 deactivate_timer(me->frontend);
158 void midend_force_redraw(midend_data *me)
161 me->ourgame->free_drawstate(me->drawstate);
162 me->drawstate = me->ourgame->new_drawstate(me->states[0].state);
166 void midend_new_game(midend_data *me)
168 midend_free_game(me);
170 assert(me->nstates == 0);
172 if (me->genmode == GOT_DESC) {
173 me->genmode = GOT_NOTHING;
177 if (me->genmode == GOT_SEED) {
178 me->genmode = GOT_NOTHING;
181 * Generate a new random seed. 15 digits comes to about
182 * 48 bits, which should be more than enough.
184 * I'll avoid putting a leading zero on the number,
185 * just in case it confuses anybody who thinks it's
186 * processed as an integer rather than a string.
191 newseed[0] = '1' + random_upto(me->random, 9);
192 for (i = 1; i < 15; i++)
193 newseed[i] = '0' + random_upto(me->random, 10);
195 me->seedstr = dupstr(newseed);
198 me->ourgame->free_params(me->curparams);
199 me->curparams = me->ourgame->dup_params(me->params);
204 me->ourgame->free_aux_info(me->aux_info);
207 rs = random_init(me->seedstr, strlen(me->seedstr));
208 me->desc = me->ourgame->new_desc(me->curparams, rs,
209 &me->aux_info, TRUE);
214 me->states[me->nstates].state =
215 me->ourgame->new_game(me, me->params, me->desc);
216 me->states[me->nstates].special = TRUE;
219 me->drawstate = me->ourgame->new_drawstate(me->states[0].state);
221 midend_set_timer(me);
223 me->ourgame->free_ui(me->ui);
224 me->ui = me->ourgame->new_ui(me->states[0].state);
225 me->pressed_mouse_button = 0;
228 static int midend_undo(midend_data *me)
230 if (me->statepos > 1) {
232 me->ourgame->changed_state(me->ui,
233 me->states[me->statepos-1].state,
234 me->states[me->statepos-2].state);
242 static int midend_redo(midend_data *me)
244 if (me->statepos < me->nstates) {
246 me->ourgame->changed_state(me->ui,
247 me->states[me->statepos-1].state,
248 me->states[me->statepos].state);
256 static void midend_finish_move(midend_data *me)
261 * We do not flash if the later of the two states is special.
262 * This covers both forward Solve moves and backward (undone)
265 if ((me->oldstate || me->statepos > 1) &&
266 ((me->dir > 0 && !me->states[me->statepos-1].special) ||
267 (me->dir < 0 && me->statepos < me->nstates &&
268 !me->states[me->statepos].special))) {
269 flashtime = me->ourgame->flash_length(me->oldstate ? me->oldstate :
270 me->states[me->statepos-2].state,
271 me->states[me->statepos-1].state,
272 me->oldstate ? me->dir : +1,
275 me->flash_pos = 0.0F;
276 me->flash_time = flashtime;
281 me->ourgame->free_game(me->oldstate);
283 me->anim_pos = me->anim_time = 0;
286 midend_set_timer(me);
289 void midend_stop_anim(midend_data *me)
291 if (me->oldstate || me->anim_time) {
292 midend_finish_move(me);
297 void midend_restart_game(midend_data *me)
301 midend_stop_anim(me);
303 assert(me->statepos >= 1);
304 if (me->statepos == 1)
305 return; /* no point doing anything at all! */
307 s = me->ourgame->dup_game(me->states[0].state);
310 * Now enter the restarted state as the next move.
312 midend_stop_anim(me);
313 while (me->nstates > me->statepos)
314 me->ourgame->free_game(me->states[--me->nstates].state);
316 me->states[me->nstates].state = s;
317 me->states[me->nstates].special = TRUE; /* we just restarted */
318 me->statepos = ++me->nstates;
320 me->ourgame->changed_state(me->ui,
321 me->states[me->statepos-2].state,
322 me->states[me->statepos-1].state);
324 midend_finish_move(me);
326 midend_set_timer(me);
329 static int midend_really_process_key(midend_data *me, int x, int y, int button)
331 game_state *oldstate =
332 me->ourgame->dup_game(me->states[me->statepos - 1].state);
333 int special = FALSE, gotspecial = FALSE, ret = 1;
336 if (button == 'n' || button == 'N' || button == '\x0E') {
337 midend_stop_anim(me);
340 goto done; /* never animate */
341 } else if (button == 'u' || button == 'u' ||
342 button == '\x1A' || button == '\x1F') {
343 midend_stop_anim(me);
344 special = me->states[me->statepos-1].special;
346 if (!midend_undo(me))
348 } else if (button == 'r' || button == 'R' ||
349 button == '\x12' || button == '\x19') {
350 midend_stop_anim(me);
351 if (!midend_redo(me))
353 } else if (button == 'q' || button == 'Q' || button == '\x11') {
358 me->ourgame->make_move(me->states[me->statepos-1].state,
359 me->ui, me->drawstate, x, y, button);
361 if (s == me->states[me->statepos-1].state) {
363 * make_move() is allowed to return its input state to
364 * indicate that although no move has been made, the UI
365 * state has been updated and a redraw is called for.
370 midend_stop_anim(me);
371 while (me->nstates > me->statepos)
372 me->ourgame->free_game(me->states[--me->nstates].state);
374 me->states[me->nstates].state = s;
375 me->states[me->nstates].special = FALSE; /* normal move */
376 me->statepos = ++me->nstates;
384 special = me->states[me->statepos-1].special;
387 * See if this move requires an animation.
392 anim_time = me->ourgame->anim_length(oldstate,
393 me->states[me->statepos-1].state,
397 me->oldstate = oldstate; oldstate = NULL;
399 me->anim_time = anim_time;
402 midend_finish_move(me);
408 midend_set_timer(me);
411 if (oldstate) me->ourgame->free_game(oldstate);
415 int midend_process_key(midend_data *me, int x, int y, int button)
420 * Harmonise mouse drag and release messages.
422 * Some front ends might accidentally switch from sending, say,
423 * RIGHT_DRAG messages to sending LEFT_DRAG, half way through a
424 * drag. (This can happen on the Mac, for example, since
425 * RIGHT_DRAG is usually done using Command+drag, and if the
426 * user accidentally releases Command half way through the drag
427 * then there will be trouble.)
429 * It would be an O(number of front ends) annoyance to fix this
430 * in the front ends, but an O(number of back ends) annoyance
431 * to have each game capable of dealing with it. Therefore, we
432 * fix it _here_ in the common midend code so that it only has
435 * The possible ways in which things can go screwy in the front
438 * - in a system containing multiple physical buttons button
439 * presses can inadvertently overlap. We can see ABab (caps
440 * meaning button-down and lowercase meaning button-up) when
441 * the user had semantically intended AaBb.
443 * - in a system where one button is simulated by means of a
444 * modifier key and another button, buttons can mutate
445 * between press and release (possibly during drag). So we
446 * can see Ab instead of Aa.
448 * Definite requirements are:
450 * - button _presses_ must never be invented or destroyed. If
451 * the user presses two buttons in succession, the button
452 * presses must be transferred to the backend unchanged. So
453 * if we see AaBb , that's fine; if we see ABab (the button
454 * presses inadvertently overlapped) we must somehow
455 * `correct' it to AaBb.
457 * - every mouse action must end up looking like a press, zero
458 * or more drags, then a release. This allows back ends to
459 * make the _assumption_ that incoming mouse data will be
460 * sane in this regard, and not worry about the details.
462 * So my policy will be:
464 * - treat any button-up as a button-up for the currently
465 * pressed button, or ignore it if there is no currently
468 * - treat any drag as a drag for the currently pressed
469 * button, or ignore it if there is no currently pressed
472 * - if we see a button-down while another button is currently
473 * pressed, invent a button-up for the first one and then
474 * pass the button-down through as before.
476 * 2005-05-31: An addendum to the above. Some games might want
477 * a `priority order' among buttons, such that if one button is
478 * pressed while another is down then a fixed one of the
479 * buttons takes priority no matter what order they're pressed
480 * in. Mines, in particular, wants to treat a left+right click
481 * like a left click for the benefit of users of other
482 * implementations. So the last of the above points is modified
483 * in the presence of an (optional) button priority order.
485 if (IS_MOUSE_DRAG(button) || IS_MOUSE_RELEASE(button)) {
486 if (me->pressed_mouse_button) {
487 if (IS_MOUSE_DRAG(button)) {
488 button = me->pressed_mouse_button +
489 (LEFT_DRAG - LEFT_BUTTON);
491 button = me->pressed_mouse_button +
492 (LEFT_RELEASE - LEFT_BUTTON);
495 return ret; /* ignore it */
496 } else if (IS_MOUSE_DOWN(button) && me->pressed_mouse_button) {
498 * If the new button has lower priority than the old one,
499 * don't bother doing this.
501 if (me->ourgame->mouse_priorities &
502 BUTTON_BEATS(me->pressed_mouse_button, button))
503 return ret; /* just ignore it */
506 * Fabricate a button-up for the previously pressed button.
508 ret = ret && midend_really_process_key
509 (me, x, y, (me->pressed_mouse_button +
510 (LEFT_RELEASE - LEFT_BUTTON)));
514 * Now send on the event we originally received.
516 ret = ret && midend_really_process_key(me, x, y, button);
519 * And update the currently pressed button.
521 if (IS_MOUSE_RELEASE(button))
522 me->pressed_mouse_button = 0;
523 else if (IS_MOUSE_DOWN(button))
524 me->pressed_mouse_button = button;
529 void midend_redraw(midend_data *me)
531 if (me->statepos > 0 && me->drawstate) {
532 start_draw(me->frontend);
533 if (me->oldstate && me->anim_time > 0 &&
534 me->anim_pos < me->anim_time) {
535 assert(me->dir != 0);
536 me->ourgame->redraw(me->frontend, me->drawstate, me->oldstate,
537 me->states[me->statepos-1].state, me->dir,
538 me->ui, me->anim_pos, me->flash_pos);
540 me->ourgame->redraw(me->frontend, me->drawstate, NULL,
541 me->states[me->statepos-1].state, +1 /*shrug*/,
542 me->ui, 0.0, me->flash_pos);
544 end_draw(me->frontend);
548 void midend_timer(midend_data *me, float tplus)
550 me->anim_pos += tplus;
551 if (me->anim_pos >= me->anim_time ||
552 me->anim_time == 0 || !me->oldstate) {
553 if (me->anim_time > 0)
554 midend_finish_move(me);
557 me->flash_pos += tplus;
558 if (me->flash_pos >= me->flash_time || me->flash_time == 0) {
559 me->flash_pos = me->flash_time = 0;
565 float oldelapsed = me->elapsed;
566 me->elapsed += tplus;
567 if ((int)oldelapsed != (int)me->elapsed)
568 status_bar(me->frontend, me->laststatus ? me->laststatus : "");
571 midend_set_timer(me);
574 float *midend_colours(midend_data *me, int *ncolours)
576 game_state *state = NULL;
579 if (me->nstates == 0) {
580 game_aux_info *aux = NULL;
581 char *desc = me->ourgame->new_desc(me->params, me->random,
583 state = me->ourgame->new_game(me, me->params, desc);
586 me->ourgame->free_aux_info(aux);
588 state = me->states[0].state;
590 ret = me->ourgame->colours(me->frontend, state, ncolours);
596 * Allow environment-based overrides for the standard
597 * colours by defining variables along the lines of
598 * `NET_COLOUR_4=6000c0'.
601 for (i = 0; i < *ncolours; i++) {
603 unsigned int r, g, b;
606 sprintf(buf, "%s_COLOUR_%d", me->ourgame->name, i);
607 for (j = 0; buf[j]; j++)
608 buf[j] = toupper((unsigned char)buf[j]);
609 if ((e = getenv(buf)) != NULL &&
610 sscanf(e, "%2x%2x%2x", &r, &g, &b) == 3) {
611 ret[i*3 + 0] = r / 255.0;
612 ret[i*3 + 1] = g / 255.0;
613 ret[i*3 + 2] = b / 255.0;
618 if (me->nstates == 0)
619 me->ourgame->free_game(state);
624 int midend_num_presets(midend_data *me)
630 while (me->ourgame->fetch_preset(me->npresets, &name, &preset)) {
631 if (me->presetsize <= me->npresets) {
632 me->presetsize = me->npresets + 10;
633 me->presets = sresize(me->presets, me->presetsize,
635 me->preset_names = sresize(me->preset_names, me->presetsize,
639 me->presets[me->npresets] = preset;
640 me->preset_names[me->npresets] = name;
647 * Allow environment-based extensions to the preset list by
648 * defining a variable along the lines of `SOLO_PRESETS=2x3
649 * Advanced:2x3da'. Colon-separated list of items,
650 * alternating between textual titles in the menu and
651 * encoded parameter strings.
653 char buf[80], *e, *p;
656 sprintf(buf, "%s_PRESETS", me->ourgame->name);
657 for (j = 0; buf[j]; j++)
658 buf[j] = toupper((unsigned char)buf[j]);
660 if ((e = getenv(buf)) != NULL) {
668 while (*p && *p != ':') p++;
671 while (*p && *p != ':') p++;
674 preset = me->ourgame->default_params();
675 me->ourgame->decode_params(preset, val);
677 if (me->ourgame->validate_params(preset)) {
678 /* Drop this one from the list. */
679 me->ourgame->free_params(preset);
683 if (me->presetsize <= me->npresets) {
684 me->presetsize = me->npresets + 10;
685 me->presets = sresize(me->presets, me->presetsize,
687 me->preset_names = sresize(me->preset_names,
688 me->presetsize, char *);
691 me->presets[me->npresets] = preset;
692 me->preset_names[me->npresets] = name;
701 void midend_fetch_preset(midend_data *me, int n,
702 char **name, game_params **params)
704 assert(n >= 0 && n < me->npresets);
705 *name = me->preset_names[n];
706 *params = me->presets[n];
709 int midend_wants_statusbar(midend_data *me)
711 return me->ourgame->wants_statusbar();
714 void midend_supersede_game_desc(midend_data *me, char *desc)
717 me->desc = dupstr(desc);
720 config_item *midend_get_config(midend_data *me, int which, char **wintitle)
722 char *titlebuf, *parstr, *rest;
727 titlebuf = snewn(40 + strlen(me->ourgame->name), char);
731 sprintf(titlebuf, "%s configuration", me->ourgame->name);
732 *wintitle = titlebuf;
733 return me->ourgame->configure(me->params);
736 if (!me->curparams) {
740 sprintf(titlebuf, "%s %s selection", me->ourgame->name,
741 which == CFG_SEED ? "random" : "game");
742 *wintitle = titlebuf;
744 ret = snewn(2, config_item);
746 ret[0].type = C_STRING;
747 if (which == CFG_SEED)
748 ret[0].name = "Game random seed";
750 ret[0].name = "Game ID";
753 * For CFG_DESC the text going in here will be a string
754 * encoding of the restricted parameters, plus a colon,
755 * plus the game description. For CFG_SEED it will be the
756 * full parameters, plus a hash, plus the random seed data.
757 * Either of these is a valid full game ID (although only
758 * the former is likely to persist across many code
761 parstr = me->ourgame->encode_params(me->curparams, which == CFG_SEED);
763 if (which == CFG_DESC) {
764 rest = me->desc ? me->desc : "";
767 rest = me->seedstr ? me->seedstr : "";
770 ret[0].sval = snewn(strlen(parstr) + strlen(rest) + 2, char);
771 sprintf(ret[0].sval, "%s%c%s", parstr, sep, rest);
775 ret[1].name = ret[1].sval = NULL;
781 assert(!"We shouldn't be here");
785 static char *midend_game_id_int(midend_data *me, char *id, int defmode)
787 char *error, *par, *desc, *seed;
789 seed = strchr(id, '#');
790 desc = strchr(id, ':');
792 if (desc && (!seed || desc < seed)) {
794 * We have a colon separating parameters from game
795 * description. So `par' now points to the parameters
796 * string, and `desc' to the description string.
801 } else if (seed && (!desc || seed < desc)) {
803 * We have a hash separating parameters from random seed.
804 * So `par' now points to the parameters string, and `seed'
805 * to the seed string.
812 * We only have one string. Depending on `defmode', we take
813 * it to be either parameters, seed or description.
815 if (defmode == DEF_SEED) {
818 } else if (defmode == DEF_DESC) {
828 game_params *tmpparams;
829 tmpparams = me->ourgame->dup_params(me->params);
830 me->ourgame->decode_params(tmpparams, par);
831 error = me->ourgame->validate_params(tmpparams);
833 me->ourgame->free_params(tmpparams);
837 me->ourgame->free_params(me->curparams);
838 me->curparams = tmpparams;
841 * Now filter only the persistent parts of this state into
842 * the long-term params structure, unless we've _only_
843 * received a params string in which case the whole lot is
847 char *tmpstr = me->ourgame->encode_params(tmpparams, FALSE);
848 me->ourgame->decode_params(me->params, tmpstr);
851 me->ourgame->free_params(me->params);
852 me->params = me->ourgame->dup_params(tmpparams);
862 error = me->ourgame->validate_desc(me->params, desc);
866 me->desc = dupstr(desc);
867 me->genmode = GOT_DESC;
869 me->ourgame->free_aux_info(me->aux_info);
874 me->seedstr = dupstr(seed);
875 me->genmode = GOT_SEED;
881 char *midend_game_id(midend_data *me, char *id)
883 return midend_game_id_int(me, id, DEF_PARAMS);
886 char *midend_set_config(midend_data *me, int which, config_item *cfg)
893 params = me->ourgame->custom_params(cfg);
894 error = me->ourgame->validate_params(params);
897 me->ourgame->free_params(params);
901 me->ourgame->free_params(me->params);
907 error = midend_game_id_int(me, cfg[0].sval,
908 (which == CFG_SEED ? DEF_SEED : DEF_DESC));
917 char *midend_text_format(midend_data *me)
919 if (me->ourgame->can_format_as_text && me->statepos > 0)
920 return me->ourgame->text_format(me->states[me->statepos-1].state);
925 char *midend_solve(midend_data *me)
930 if (!me->ourgame->can_solve)
931 return "This game does not support the Solve operation";
933 if (me->statepos < 1)
934 return "No game set up to solve"; /* _shouldn't_ happen! */
936 msg = "Solve operation failed"; /* game _should_ overwrite on error */
937 s = me->ourgame->solve(me->states[0].state, me->aux_info, &msg);
942 * Now enter the solved state as the next move.
944 midend_stop_anim(me);
945 while (me->nstates > me->statepos)
946 me->ourgame->free_game(me->states[--me->nstates].state);
948 me->states[me->nstates].state = s;
949 me->states[me->nstates].special = TRUE; /* created using solve */
950 me->statepos = ++me->nstates;
952 me->ourgame->changed_state(me->ui,
953 me->states[me->statepos-2].state,
954 me->states[me->statepos-1].state);
956 midend_finish_move(me);
958 midend_set_timer(me);
962 char *midend_rewrite_statusbar(midend_data *me, char *text)
965 * An important special case is that we are occasionally called
966 * with our own laststatus, to update the timer.
968 if (me->laststatus != text) {
969 sfree(me->laststatus);
970 me->laststatus = dupstr(text);
973 if (me->ourgame->is_timed) {
974 char timebuf[100], *ret;
980 sprintf(timebuf, "[%d:%02d] ", min, sec);
982 ret = snewn(strlen(timebuf) + strlen(text) + 1, char);
983 strcpy(ret, timebuf);