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;
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->curparams);
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);
148 me->ourgame->free_params(me->curparams);
149 me->curparams = me->ourgame->dup_params(me->params);
154 me->ourgame->free_aux_info(me->aux_info);
157 rs = random_init(me->seedstr, strlen(me->seedstr));
158 me->desc = me->ourgame->new_desc(me->curparams, rs, &me->aux_info);
163 me->states[me->nstates].state = me->ourgame->new_game(me->params, me->desc);
164 me->states[me->nstates].special = TRUE;
167 me->drawstate = me->ourgame->new_drawstate(me->states[0].state);
169 me->ourgame->free_ui(me->ui);
170 me->ui = me->ourgame->new_ui(me->states[0].state);
171 me->pressed_mouse_button = 0;
174 static int midend_undo(midend_data *me)
176 if (me->statepos > 1) {
184 static int midend_redo(midend_data *me)
186 if (me->statepos < me->nstates) {
194 static void midend_finish_move(midend_data *me)
199 * We do not flash if the later of the two states is special.
200 * This covers both forward Solve moves and backward (undone)
203 if ((me->oldstate || me->statepos > 1) &&
204 ((me->dir > 0 && !me->states[me->statepos-1].special) ||
205 (me->dir < 0 && me->statepos < me->nstates &&
206 !me->states[me->statepos].special))) {
207 flashtime = me->ourgame->flash_length(me->oldstate ? me->oldstate :
208 me->states[me->statepos-2].state,
209 me->states[me->statepos-1].state,
210 me->oldstate ? me->dir : +1);
212 me->flash_pos = 0.0F;
213 me->flash_time = flashtime;
218 me->ourgame->free_game(me->oldstate);
220 me->anim_pos = me->anim_time = 0;
223 if (me->flash_time == 0 && me->anim_time == 0)
224 deactivate_timer(me->frontend);
226 activate_timer(me->frontend);
229 static void midend_stop_anim(midend_data *me)
231 if (me->oldstate || me->anim_time) {
232 midend_finish_move(me);
237 void midend_restart_game(midend_data *me)
241 midend_stop_anim(me);
243 assert(me->statepos >= 1);
244 if (me->statepos == 1)
245 return; /* no point doing anything at all! */
247 s = me->ourgame->dup_game(me->states[0].state);
250 * Now enter the restarted state as the next move.
252 midend_stop_anim(me);
253 while (me->nstates > me->statepos)
254 me->ourgame->free_game(me->states[--me->nstates].state);
256 me->states[me->nstates].state = s;
257 me->states[me->nstates].special = TRUE; /* we just restarted */
258 me->statepos = ++me->nstates;
260 midend_finish_move(me);
262 activate_timer(me->frontend);
265 static int midend_really_process_key(midend_data *me, int x, int y, int button)
267 game_state *oldstate =
268 me->ourgame->dup_game(me->states[me->statepos - 1].state);
269 int special = FALSE, gotspecial = FALSE;
272 if (button == 'n' || button == 'N' || button == '\x0E') {
273 midend_stop_anim(me);
276 return 1; /* never animate */
277 } else if (button == 'u' || button == 'u' ||
278 button == '\x1A' || button == '\x1F') {
279 midend_stop_anim(me);
280 special = me->states[me->statepos-1].special;
282 if (!midend_undo(me))
284 } else if (button == 'r' || button == 'R' ||
286 midend_stop_anim(me);
287 if (!midend_redo(me))
289 } else if (button == 'q' || button == 'Q' || button == '\x11') {
290 me->ourgame->free_game(oldstate);
294 me->ourgame->make_move(me->states[me->statepos-1].state,
295 me->ui, x, y, button);
297 if (s == me->states[me->statepos-1].state) {
299 * make_move() is allowed to return its input state to
300 * indicate that although no move has been made, the UI
301 * state has been updated and a redraw is called for.
306 midend_stop_anim(me);
307 while (me->nstates > me->statepos)
308 me->ourgame->free_game(me->states[--me->nstates].state);
310 me->states[me->nstates].state = s;
311 me->states[me->nstates].special = FALSE; /* normal move */
312 me->statepos = ++me->nstates;
315 me->ourgame->free_game(oldstate);
321 special = me->states[me->statepos-1].special;
324 * See if this move requires an animation.
329 anim_time = me->ourgame->anim_length(oldstate,
330 me->states[me->statepos-1].state,
334 me->oldstate = oldstate;
336 me->anim_time = anim_time;
339 midend_finish_move(me);
345 activate_timer(me->frontend);
350 int midend_process_key(midend_data *me, int x, int y, int button)
355 * Harmonise mouse drag and release messages.
357 * Some front ends might accidentally switch from sending, say,
358 * RIGHT_DRAG messages to sending LEFT_DRAG, half way through a
359 * drag. (This can happen on the Mac, for example, since
360 * RIGHT_DRAG is usually done using Command+drag, and if the
361 * user accidentally releases Command half way through the drag
362 * then there will be trouble.)
364 * It would be an O(number of front ends) annoyance to fix this
365 * in the front ends, but an O(number of back ends) annoyance
366 * to have each game capable of dealing with it. Therefore, we
367 * fix it _here_ in the common midend code so that it only has
370 * The possible ways in which things can go screwy in the front
373 * - in a system containing multiple physical buttons button
374 * presses can inadvertently overlap. We can see ABab (caps
375 * meaning button-down and lowercase meaning button-up) when
376 * the user had semantically intended AaBb.
378 * - in a system where one button is simulated by means of a
379 * modifier key and another button, buttons can mutate
380 * between press and release (possibly during drag). So we
381 * can see Ab instead of Aa.
383 * Definite requirements are:
385 * - button _presses_ must never be invented or destroyed. If
386 * the user presses two buttons in succession, the button
387 * presses must be transferred to the backend unchanged. So
388 * if we see AaBb , that's fine; if we see ABab (the button
389 * presses inadvertently overlapped) we must somehow
390 * `correct' it to AaBb.
392 * - every mouse action must end up looking like a press, zero
393 * or more drags, then a release. This allows back ends to
394 * make the _assumption_ that incoming mouse data will be
395 * sane in this regard, and not worry about the details.
397 * So my policy will be:
399 * - treat any button-up as a button-up for the currently
400 * pressed button, or ignore it if there is no currently
403 * - treat any drag as a drag for the currently pressed
404 * button, or ignore it if there is no currently pressed
407 * - if we see a button-down while another button is currently
408 * pressed, invent a button-up for the first one and then
409 * pass the button-down through as before.
412 if (IS_MOUSE_DRAG(button) || IS_MOUSE_RELEASE(button)) {
413 if (me->pressed_mouse_button) {
414 if (IS_MOUSE_DRAG(button)) {
415 button = me->pressed_mouse_button +
416 (LEFT_DRAG - LEFT_BUTTON);
418 button = me->pressed_mouse_button +
419 (LEFT_RELEASE - LEFT_BUTTON);
422 return ret; /* ignore it */
423 } else if (IS_MOUSE_DOWN(button) && me->pressed_mouse_button) {
425 * Fabricate a button-up for the previously pressed button.
427 ret = ret && midend_really_process_key
428 (me, x, y, (me->pressed_mouse_button +
429 (LEFT_RELEASE - LEFT_BUTTON)));
433 * Now send on the event we originally received.
435 ret = ret && midend_really_process_key(me, x, y, button);
438 * And update the currently pressed button.
440 if (IS_MOUSE_RELEASE(button))
441 me->pressed_mouse_button = 0;
442 else if (IS_MOUSE_DOWN(button))
443 me->pressed_mouse_button = button;
448 void midend_redraw(midend_data *me)
450 if (me->statepos > 0 && me->drawstate) {
451 start_draw(me->frontend);
452 if (me->oldstate && me->anim_time > 0 &&
453 me->anim_pos < me->anim_time) {
454 assert(me->dir != 0);
455 me->ourgame->redraw(me->frontend, me->drawstate, me->oldstate,
456 me->states[me->statepos-1].state, me->dir,
457 me->ui, me->anim_pos, me->flash_pos);
459 me->ourgame->redraw(me->frontend, me->drawstate, NULL,
460 me->states[me->statepos-1].state, +1 /*shrug*/,
461 me->ui, 0.0, me->flash_pos);
463 end_draw(me->frontend);
467 void midend_timer(midend_data *me, float tplus)
469 me->anim_pos += tplus;
470 if (me->anim_pos >= me->anim_time ||
471 me->anim_time == 0 || !me->oldstate) {
472 if (me->anim_time > 0)
473 midend_finish_move(me);
475 me->flash_pos += tplus;
476 if (me->flash_pos >= me->flash_time || me->flash_time == 0) {
477 me->flash_pos = me->flash_time = 0;
479 if (me->flash_time == 0 && me->anim_time == 0)
480 deactivate_timer(me->frontend);
484 float *midend_colours(midend_data *me, int *ncolours)
486 game_state *state = NULL;
489 if (me->nstates == 0) {
490 game_aux_info *aux = NULL;
491 char *desc = me->ourgame->new_desc(me->params, me->random, &aux);
492 state = me->ourgame->new_game(me->params, desc);
495 me->ourgame->free_aux_info(aux);
497 state = me->states[0].state;
499 ret = me->ourgame->colours(me->frontend, state, ncolours);
505 * Allow environment-based overrides for the standard
506 * colours by defining variables along the lines of
507 * `NET_COLOUR_4=6000c0'.
510 for (i = 0; i < *ncolours; i++) {
512 unsigned int r, g, b;
515 sprintf(buf, "%s_COLOUR_%d", me->ourgame->name, i);
516 for (j = 0; buf[j]; j++)
517 buf[j] = toupper((unsigned char)buf[j]);
518 if ((e = getenv(buf)) != NULL &&
519 sscanf(e, "%2x%2x%2x", &r, &g, &b) == 3) {
520 ret[i*3 + 0] = r / 255.0;
521 ret[i*3 + 1] = g / 255.0;
522 ret[i*3 + 2] = b / 255.0;
527 if (me->nstates == 0)
528 me->ourgame->free_game(state);
533 int midend_num_presets(midend_data *me)
539 while (me->ourgame->fetch_preset(me->npresets, &name, &preset)) {
540 if (me->presetsize <= me->npresets) {
541 me->presetsize = me->npresets + 10;
542 me->presets = sresize(me->presets, me->presetsize,
544 me->preset_names = sresize(me->preset_names, me->presetsize,
548 me->presets[me->npresets] = preset;
549 me->preset_names[me->npresets] = name;
556 * Allow environment-based extensions to the preset list by
557 * defining a variable along the lines of `SOLO_PRESETS=2x3
558 * Advanced:2x3da'. Colon-separated list of items,
559 * alternating between textual titles in the menu and
560 * encoded parameter strings.
562 char buf[80], *e, *p;
565 sprintf(buf, "%s_PRESETS", me->ourgame->name);
566 for (j = 0; buf[j]; j++)
567 buf[j] = toupper((unsigned char)buf[j]);
569 if ((e = getenv(buf)) != NULL) {
577 while (*p && *p != ':') p++;
580 while (*p && *p != ':') p++;
583 preset = me->ourgame->default_params();
584 me->ourgame->decode_params(preset, val);
586 if (me->ourgame->validate_params(preset)) {
587 /* Drop this one from the list. */
588 me->ourgame->free_params(preset);
592 if (me->presetsize <= me->npresets) {
593 me->presetsize = me->npresets + 10;
594 me->presets = sresize(me->presets, me->presetsize,
596 me->preset_names = sresize(me->preset_names,
597 me->presetsize, char *);
600 me->presets[me->npresets] = preset;
601 me->preset_names[me->npresets] = name;
610 void midend_fetch_preset(midend_data *me, int n,
611 char **name, game_params **params)
613 assert(n >= 0 && n < me->npresets);
614 *name = me->preset_names[n];
615 *params = me->presets[n];
618 int midend_wants_statusbar(midend_data *me)
620 return me->ourgame->wants_statusbar();
623 config_item *midend_get_config(midend_data *me, int which, char **wintitle)
625 char *titlebuf, *parstr;
628 titlebuf = snewn(40 + strlen(me->ourgame->name), char);
632 sprintf(titlebuf, "%s configuration", me->ourgame->name);
633 *wintitle = dupstr(titlebuf);
634 return me->ourgame->configure(me->params);
637 sprintf(titlebuf, "%s %s selection", me->ourgame->name,
638 which == CFG_SEED ? "random" : "game");
639 *wintitle = dupstr(titlebuf);
641 ret = snewn(2, config_item);
643 ret[0].type = C_STRING;
644 if (which == CFG_SEED)
645 ret[0].name = "Game random seed";
647 ret[0].name = "Game ID";
650 * For CFG_DESC the text going in here will be a string
651 * encoding of the restricted parameters, plus a colon,
652 * plus the game description. For CFG_SEED it will be the
653 * full parameters, plus a hash, plus the random seed data.
654 * Either of these is a valid full game ID (although only
655 * the former is likely to persist across many code
658 parstr = me->ourgame->encode_params(me->curparams, which == CFG_SEED);
659 if (which == CFG_DESC) {
660 ret[0].sval = snewn(strlen(parstr) + strlen(me->desc) + 2, char);
661 sprintf(ret[0].sval, "%s:%s", parstr, me->desc);
662 } else if (me->seedstr) {
663 ret[0].sval = snewn(strlen(parstr) + strlen(me->seedstr) + 2, char);
664 sprintf(ret[0].sval, "%s#%s", parstr, me->seedstr);
667 * If the current game was not randomly generated, the
668 * best we can do is to give a template for typing a
671 ret[0].sval = snewn(strlen(parstr) + 2, char);
672 sprintf(ret[0].sval, "%s#", parstr);
677 ret[1].name = ret[1].sval = NULL;
683 assert(!"We shouldn't be here");
687 static char *midend_game_id_int(midend_data *me, char *id, int defmode)
689 char *error, *par, *desc, *seed;
691 seed = strchr(id, '#');
692 desc = strchr(id, ':');
694 if (desc && (!seed || desc < seed)) {
696 * We have a colon separating parameters from game
697 * description. So `par' now points to the parameters
698 * string, and `desc' to the description string.
703 } else if (seed && (!desc || seed < desc)) {
705 * We have a hash separating parameters from random seed.
706 * So `par' now points to the parameters string, and `seed'
707 * to the seed string.
714 * We only have one string. Depending on `defmode', we take
715 * it to be either parameters, seed or description.
717 if (defmode == DEF_SEED) {
720 } else if (defmode == DEF_DESC) {
730 game_params *tmpparams;
731 tmpparams = me->ourgame->dup_params(me->params);
732 me->ourgame->decode_params(tmpparams, par);
733 error = me->ourgame->validate_params(tmpparams);
735 me->ourgame->free_params(tmpparams);
739 me->ourgame->free_params(me->curparams);
740 me->curparams = tmpparams;
743 * Now filter only the persistent parts of this state into
744 * the long-term params structure, unless we've _only_
745 * received a params string in which case the whole lot is
749 char *tmpstr = me->ourgame->encode_params(tmpparams, FALSE);
750 me->ourgame->decode_params(me->params, tmpstr);
753 me->ourgame->free_params(me->params);
754 me->params = me->ourgame->dup_params(tmpparams);
764 error = me->ourgame->validate_desc(me->params, desc);
768 me->desc = dupstr(desc);
769 me->genmode = GOT_DESC;
771 me->ourgame->free_aux_info(me->aux_info);
776 me->seedstr = dupstr(seed);
777 me->genmode = GOT_SEED;
783 char *midend_game_id(midend_data *me, char *id)
785 return midend_game_id_int(me, id, DEF_PARAMS);
788 char *midend_set_config(midend_data *me, int which, config_item *cfg)
795 params = me->ourgame->custom_params(cfg);
796 error = me->ourgame->validate_params(params);
799 me->ourgame->free_params(params);
803 me->ourgame->free_params(me->params);
809 error = midend_game_id_int(me, cfg[0].sval,
810 (which == CFG_SEED ? DEF_SEED : DEF_DESC));
819 char *midend_text_format(midend_data *me)
821 if (me->ourgame->can_format_as_text && me->statepos > 0)
822 return me->ourgame->text_format(me->states[me->statepos-1].state);
827 char *midend_solve(midend_data *me)
832 if (!me->ourgame->can_solve)
833 return "This game does not support the Solve operation";
835 if (me->statepos < 1)
836 return "No game set up to solve"; /* _shouldn't_ happen! */
838 msg = "Solve operation failed"; /* game _should_ overwrite on error */
839 s = me->ourgame->solve(me->states[0].state, me->aux_info, &msg);
844 * Now enter the solved state as the next move.
846 midend_stop_anim(me);
847 while (me->nstates > me->statepos)
848 me->ourgame->free_game(me->states[--me->nstates].state);
850 me->states[me->nstates].state = s;
851 me->states[me->nstates].special = TRUE; /* created using solve */
852 me->statepos = ++me->nstates;
854 midend_finish_move(me);
856 activate_timer(me->frontend);