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, &me->aux_info);
186 me->states[me->nstates].state =
187 me->ourgame->new_game(me, me->params, me->desc);
188 me->states[me->nstates].special = TRUE;
191 me->drawstate = me->ourgame->new_drawstate(me->states[0].state);
193 midend_set_timer(me);
195 me->ourgame->free_ui(me->ui);
196 me->ui = me->ourgame->new_ui(me->states[0].state);
197 me->pressed_mouse_button = 0;
200 static int midend_undo(midend_data *me)
202 if (me->statepos > 1) {
210 static int midend_redo(midend_data *me)
212 if (me->statepos < me->nstates) {
220 static void midend_finish_move(midend_data *me)
225 * We do not flash if the later of the two states is special.
226 * This covers both forward Solve moves and backward (undone)
229 if ((me->oldstate || me->statepos > 1) &&
230 ((me->dir > 0 && !me->states[me->statepos-1].special) ||
231 (me->dir < 0 && me->statepos < me->nstates &&
232 !me->states[me->statepos].special))) {
233 flashtime = me->ourgame->flash_length(me->oldstate ? me->oldstate :
234 me->states[me->statepos-2].state,
235 me->states[me->statepos-1].state,
236 me->oldstate ? me->dir : +1,
239 me->flash_pos = 0.0F;
240 me->flash_time = flashtime;
245 me->ourgame->free_game(me->oldstate);
247 me->anim_pos = me->anim_time = 0;
250 midend_set_timer(me);
253 static void midend_stop_anim(midend_data *me)
255 if (me->oldstate || me->anim_time) {
256 midend_finish_move(me);
261 void midend_restart_game(midend_data *me)
265 midend_stop_anim(me);
267 assert(me->statepos >= 1);
268 if (me->statepos == 1)
269 return; /* no point doing anything at all! */
271 s = me->ourgame->dup_game(me->states[0].state);
274 * Now enter the restarted state as the next move.
276 midend_stop_anim(me);
277 while (me->nstates > me->statepos)
278 me->ourgame->free_game(me->states[--me->nstates].state);
280 me->states[me->nstates].state = s;
281 me->states[me->nstates].special = TRUE; /* we just restarted */
282 me->statepos = ++me->nstates;
284 midend_finish_move(me);
286 midend_set_timer(me);
289 static int midend_really_process_key(midend_data *me, int x, int y, int button)
291 game_state *oldstate =
292 me->ourgame->dup_game(me->states[me->statepos - 1].state);
293 int special = FALSE, gotspecial = FALSE;
296 if (button == 'n' || button == 'N' || button == '\x0E') {
297 midend_stop_anim(me);
300 return 1; /* never animate */
301 } else if (button == 'u' || button == 'u' ||
302 button == '\x1A' || button == '\x1F') {
303 midend_stop_anim(me);
304 special = me->states[me->statepos-1].special;
306 if (!midend_undo(me))
308 } else if (button == 'r' || button == 'R' ||
310 midend_stop_anim(me);
311 if (!midend_redo(me))
313 } else if (button == 'q' || button == 'Q' || button == '\x11') {
314 me->ourgame->free_game(oldstate);
318 me->ourgame->make_move(me->states[me->statepos-1].state,
319 me->ui, x, y, button);
321 if (s == me->states[me->statepos-1].state) {
323 * make_move() is allowed to return its input state to
324 * indicate that although no move has been made, the UI
325 * state has been updated and a redraw is called for.
330 midend_stop_anim(me);
331 while (me->nstates > me->statepos)
332 me->ourgame->free_game(me->states[--me->nstates].state);
334 me->states[me->nstates].state = s;
335 me->states[me->nstates].special = FALSE; /* normal move */
336 me->statepos = ++me->nstates;
339 me->ourgame->free_game(oldstate);
345 special = me->states[me->statepos-1].special;
348 * See if this move requires an animation.
353 anim_time = me->ourgame->anim_length(oldstate,
354 me->states[me->statepos-1].state,
358 me->oldstate = oldstate;
360 me->anim_time = anim_time;
363 midend_finish_move(me);
369 midend_set_timer(me);
374 int midend_process_key(midend_data *me, int x, int y, int button)
379 * Harmonise mouse drag and release messages.
381 * Some front ends might accidentally switch from sending, say,
382 * RIGHT_DRAG messages to sending LEFT_DRAG, half way through a
383 * drag. (This can happen on the Mac, for example, since
384 * RIGHT_DRAG is usually done using Command+drag, and if the
385 * user accidentally releases Command half way through the drag
386 * then there will be trouble.)
388 * It would be an O(number of front ends) annoyance to fix this
389 * in the front ends, but an O(number of back ends) annoyance
390 * to have each game capable of dealing with it. Therefore, we
391 * fix it _here_ in the common midend code so that it only has
394 * The possible ways in which things can go screwy in the front
397 * - in a system containing multiple physical buttons button
398 * presses can inadvertently overlap. We can see ABab (caps
399 * meaning button-down and lowercase meaning button-up) when
400 * the user had semantically intended AaBb.
402 * - in a system where one button is simulated by means of a
403 * modifier key and another button, buttons can mutate
404 * between press and release (possibly during drag). So we
405 * can see Ab instead of Aa.
407 * Definite requirements are:
409 * - button _presses_ must never be invented or destroyed. If
410 * the user presses two buttons in succession, the button
411 * presses must be transferred to the backend unchanged. So
412 * if we see AaBb , that's fine; if we see ABab (the button
413 * presses inadvertently overlapped) we must somehow
414 * `correct' it to AaBb.
416 * - every mouse action must end up looking like a press, zero
417 * or more drags, then a release. This allows back ends to
418 * make the _assumption_ that incoming mouse data will be
419 * sane in this regard, and not worry about the details.
421 * So my policy will be:
423 * - treat any button-up as a button-up for the currently
424 * pressed button, or ignore it if there is no currently
427 * - treat any drag as a drag for the currently pressed
428 * button, or ignore it if there is no currently pressed
431 * - if we see a button-down while another button is currently
432 * pressed, invent a button-up for the first one and then
433 * pass the button-down through as before.
436 if (IS_MOUSE_DRAG(button) || IS_MOUSE_RELEASE(button)) {
437 if (me->pressed_mouse_button) {
438 if (IS_MOUSE_DRAG(button)) {
439 button = me->pressed_mouse_button +
440 (LEFT_DRAG - LEFT_BUTTON);
442 button = me->pressed_mouse_button +
443 (LEFT_RELEASE - LEFT_BUTTON);
446 return ret; /* ignore it */
447 } else if (IS_MOUSE_DOWN(button) && me->pressed_mouse_button) {
449 * Fabricate a button-up for the previously pressed button.
451 ret = ret && midend_really_process_key
452 (me, x, y, (me->pressed_mouse_button +
453 (LEFT_RELEASE - LEFT_BUTTON)));
457 * Now send on the event we originally received.
459 ret = ret && midend_really_process_key(me, x, y, button);
462 * And update the currently pressed button.
464 if (IS_MOUSE_RELEASE(button))
465 me->pressed_mouse_button = 0;
466 else if (IS_MOUSE_DOWN(button))
467 me->pressed_mouse_button = button;
472 void midend_redraw(midend_data *me)
474 if (me->statepos > 0 && me->drawstate) {
475 start_draw(me->frontend);
476 if (me->oldstate && me->anim_time > 0 &&
477 me->anim_pos < me->anim_time) {
478 assert(me->dir != 0);
479 me->ourgame->redraw(me->frontend, me->drawstate, me->oldstate,
480 me->states[me->statepos-1].state, me->dir,
481 me->ui, me->anim_pos, me->flash_pos);
483 me->ourgame->redraw(me->frontend, me->drawstate, NULL,
484 me->states[me->statepos-1].state, +1 /*shrug*/,
485 me->ui, 0.0, me->flash_pos);
487 end_draw(me->frontend);
491 void midend_timer(midend_data *me, float tplus)
493 me->anim_pos += tplus;
494 if (me->anim_pos >= me->anim_time ||
495 me->anim_time == 0 || !me->oldstate) {
496 if (me->anim_time > 0)
497 midend_finish_move(me);
500 me->flash_pos += tplus;
501 if (me->flash_pos >= me->flash_time || me->flash_time == 0) {
502 me->flash_pos = me->flash_time = 0;
508 float oldelapsed = me->elapsed;
509 me->elapsed += tplus;
510 if ((int)oldelapsed != (int)me->elapsed)
511 status_bar(me->frontend, me->laststatus ? me->laststatus : "");
514 midend_set_timer(me);
517 float *midend_colours(midend_data *me, int *ncolours)
519 game_state *state = NULL;
522 if (me->nstates == 0) {
523 game_aux_info *aux = NULL;
524 char *desc = me->ourgame->new_desc(me->params, me->random, &aux);
525 state = me->ourgame->new_game(me, me->params, desc);
528 me->ourgame->free_aux_info(aux);
530 state = me->states[0].state;
532 ret = me->ourgame->colours(me->frontend, state, ncolours);
538 * Allow environment-based overrides for the standard
539 * colours by defining variables along the lines of
540 * `NET_COLOUR_4=6000c0'.
543 for (i = 0; i < *ncolours; i++) {
545 unsigned int r, g, b;
548 sprintf(buf, "%s_COLOUR_%d", me->ourgame->name, i);
549 for (j = 0; buf[j]; j++)
550 buf[j] = toupper((unsigned char)buf[j]);
551 if ((e = getenv(buf)) != NULL &&
552 sscanf(e, "%2x%2x%2x", &r, &g, &b) == 3) {
553 ret[i*3 + 0] = r / 255.0;
554 ret[i*3 + 1] = g / 255.0;
555 ret[i*3 + 2] = b / 255.0;
560 if (me->nstates == 0)
561 me->ourgame->free_game(state);
566 int midend_num_presets(midend_data *me)
572 while (me->ourgame->fetch_preset(me->npresets, &name, &preset)) {
573 if (me->presetsize <= me->npresets) {
574 me->presetsize = me->npresets + 10;
575 me->presets = sresize(me->presets, me->presetsize,
577 me->preset_names = sresize(me->preset_names, me->presetsize,
581 me->presets[me->npresets] = preset;
582 me->preset_names[me->npresets] = name;
589 * Allow environment-based extensions to the preset list by
590 * defining a variable along the lines of `SOLO_PRESETS=2x3
591 * Advanced:2x3da'. Colon-separated list of items,
592 * alternating between textual titles in the menu and
593 * encoded parameter strings.
595 char buf[80], *e, *p;
598 sprintf(buf, "%s_PRESETS", me->ourgame->name);
599 for (j = 0; buf[j]; j++)
600 buf[j] = toupper((unsigned char)buf[j]);
602 if ((e = getenv(buf)) != NULL) {
610 while (*p && *p != ':') p++;
613 while (*p && *p != ':') p++;
616 preset = me->ourgame->default_params();
617 me->ourgame->decode_params(preset, val);
619 if (me->ourgame->validate_params(preset)) {
620 /* Drop this one from the list. */
621 me->ourgame->free_params(preset);
625 if (me->presetsize <= me->npresets) {
626 me->presetsize = me->npresets + 10;
627 me->presets = sresize(me->presets, me->presetsize,
629 me->preset_names = sresize(me->preset_names,
630 me->presetsize, char *);
633 me->presets[me->npresets] = preset;
634 me->preset_names[me->npresets] = name;
643 void midend_fetch_preset(midend_data *me, int n,
644 char **name, game_params **params)
646 assert(n >= 0 && n < me->npresets);
647 *name = me->preset_names[n];
648 *params = me->presets[n];
651 int midend_wants_statusbar(midend_data *me)
653 return me->ourgame->wants_statusbar();
656 void midend_supersede_game_desc(midend_data *me, char *desc)
659 me->desc = dupstr(desc);
662 config_item *midend_get_config(midend_data *me, int which, char **wintitle)
664 char *titlebuf, *parstr;
667 titlebuf = snewn(40 + strlen(me->ourgame->name), char);
671 sprintf(titlebuf, "%s configuration", me->ourgame->name);
672 *wintitle = dupstr(titlebuf);
673 return me->ourgame->configure(me->params);
676 sprintf(titlebuf, "%s %s selection", me->ourgame->name,
677 which == CFG_SEED ? "random" : "game");
678 *wintitle = dupstr(titlebuf);
680 ret = snewn(2, config_item);
682 ret[0].type = C_STRING;
683 if (which == CFG_SEED)
684 ret[0].name = "Game random seed";
686 ret[0].name = "Game ID";
689 * For CFG_DESC the text going in here will be a string
690 * encoding of the restricted parameters, plus a colon,
691 * plus the game description. For CFG_SEED it will be the
692 * full parameters, plus a hash, plus the random seed data.
693 * Either of these is a valid full game ID (although only
694 * the former is likely to persist across many code
697 parstr = me->ourgame->encode_params(me->curparams, which == CFG_SEED);
698 if (which == CFG_DESC) {
699 ret[0].sval = snewn(strlen(parstr) + strlen(me->desc) + 2, char);
700 sprintf(ret[0].sval, "%s:%s", parstr, me->desc);
701 } else if (me->seedstr) {
702 ret[0].sval = snewn(strlen(parstr) + strlen(me->seedstr) + 2, char);
703 sprintf(ret[0].sval, "%s#%s", parstr, me->seedstr);
706 * If the current game was not randomly generated, the
707 * best we can do is to give a template for typing a
710 ret[0].sval = snewn(strlen(parstr) + 2, char);
711 sprintf(ret[0].sval, "%s#", parstr);
716 ret[1].name = ret[1].sval = NULL;
722 assert(!"We shouldn't be here");
726 static char *midend_game_id_int(midend_data *me, char *id, int defmode)
728 char *error, *par, *desc, *seed;
730 seed = strchr(id, '#');
731 desc = strchr(id, ':');
733 if (desc && (!seed || desc < seed)) {
735 * We have a colon separating parameters from game
736 * description. So `par' now points to the parameters
737 * string, and `desc' to the description string.
742 } else if (seed && (!desc || seed < desc)) {
744 * We have a hash separating parameters from random seed.
745 * So `par' now points to the parameters string, and `seed'
746 * to the seed string.
753 * We only have one string. Depending on `defmode', we take
754 * it to be either parameters, seed or description.
756 if (defmode == DEF_SEED) {
759 } else if (defmode == DEF_DESC) {
769 game_params *tmpparams;
770 tmpparams = me->ourgame->dup_params(me->params);
771 me->ourgame->decode_params(tmpparams, par);
772 error = me->ourgame->validate_params(tmpparams);
774 me->ourgame->free_params(tmpparams);
778 me->ourgame->free_params(me->curparams);
779 me->curparams = tmpparams;
782 * Now filter only the persistent parts of this state into
783 * the long-term params structure, unless we've _only_
784 * received a params string in which case the whole lot is
788 char *tmpstr = me->ourgame->encode_params(tmpparams, FALSE);
789 me->ourgame->decode_params(me->params, tmpstr);
792 me->ourgame->free_params(me->params);
793 me->params = me->ourgame->dup_params(tmpparams);
803 error = me->ourgame->validate_desc(me->params, desc);
807 me->desc = dupstr(desc);
808 me->genmode = GOT_DESC;
810 me->ourgame->free_aux_info(me->aux_info);
815 me->seedstr = dupstr(seed);
816 me->genmode = GOT_SEED;
822 char *midend_game_id(midend_data *me, char *id)
824 return midend_game_id_int(me, id, DEF_PARAMS);
827 char *midend_set_config(midend_data *me, int which, config_item *cfg)
834 params = me->ourgame->custom_params(cfg);
835 error = me->ourgame->validate_params(params);
838 me->ourgame->free_params(params);
842 me->ourgame->free_params(me->params);
848 error = midend_game_id_int(me, cfg[0].sval,
849 (which == CFG_SEED ? DEF_SEED : DEF_DESC));
858 char *midend_text_format(midend_data *me)
860 if (me->ourgame->can_format_as_text && me->statepos > 0)
861 return me->ourgame->text_format(me->states[me->statepos-1].state);
866 char *midend_solve(midend_data *me)
871 if (!me->ourgame->can_solve)
872 return "This game does not support the Solve operation";
874 if (me->statepos < 1)
875 return "No game set up to solve"; /* _shouldn't_ happen! */
877 msg = "Solve operation failed"; /* game _should_ overwrite on error */
878 s = me->ourgame->solve(me->states[0].state, me->aux_info, &msg);
883 * Now enter the solved state as the next move.
885 midend_stop_anim(me);
886 while (me->nstates > me->statepos)
887 me->ourgame->free_game(me->states[--me->nstates].state);
889 me->states[me->nstates].state = s;
890 me->states[me->nstates].special = TRUE; /* created using solve */
891 me->statepos = ++me->nstates;
893 midend_finish_move(me);
895 midend_set_timer(me);
899 char *midend_rewrite_statusbar(midend_data *me, char *text)
902 * An important special case is that we are occasionally called
903 * with our own laststatus, to update the timer.
905 if (me->laststatus != text) {
906 sfree(me->laststatus);
907 me->laststatus = dupstr(text);
910 if (me->ourgame->is_timed) {
911 char timebuf[100], *ret;
917 sprintf(timebuf, "[%d:%02d] ", min, sec);
919 ret = snewn(strlen(timebuf) + strlen(text) + 1, char);
920 strcpy(ret, timebuf);