X-Git-Url: https://git.distorted.org.uk/~mdw/sgt/puzzles/blobdiff_plain/6776a950ab780244e06e9eca5b0f2f59053cbe6f..1cdfcdbdd6bb3f14838be9ff1ef78699bffba9c1:/midend.c diff --git a/midend.c b/midend.c index f8e922d..dbbaad4 100644 --- a/midend.c +++ b/midend.c @@ -17,6 +17,7 @@ struct midend_data { const game *ourgame; char *seed; + game_aux_info *aux_info; int fresh_seed; int nstates, statesize, statepos; @@ -58,6 +59,7 @@ midend_data *midend_new(frontend *fe, const game *ourgame) me->states = NULL; me->params = ourgame->default_params(); me->seed = NULL; + me->aux_info = NULL; me->fresh_seed = FALSE; me->drawstate = NULL; me->oldstate = NULL; @@ -79,6 +81,8 @@ void midend_free(midend_data *me) { sfree(me->states); sfree(me->seed); + if (me->aux_info) + me->ourgame->free_aux_info(me->aux_info); me->ourgame->free_params(me->params); sfree(me); } @@ -106,7 +110,11 @@ void midend_new_game(midend_data *me) if (!me->fresh_seed) { sfree(me->seed); - me->seed = me->ourgame->new_seed(me->params, me->random); + if (me->aux_info) + me->ourgame->free_aux_info(me->aux_info); + me->aux_info = NULL; + me->seed = me->ourgame->new_seed(me->params, me->random, + &me->aux_info); } else me->fresh_seed = FALSE; @@ -279,54 +287,59 @@ int midend_process_key(midend_data *me, int x, int y, int button) * fix it _here_ in the common midend code so that it only has * to be done once. * - * Semantics: + * The possible ways in which things can go screwy in the front + * end are: * - * - when we receive a button down message, we remember which - * button went down. + * - in a system containing multiple physical buttons button + * presses can inadvertently overlap. We can see ABab (caps + * meaning button-down and lowercase meaning button-up) when + * the user had semantically intended AaBb. * - * - if we receive a drag or release message for a button we - * don't currently think is pressed, we fabricate a - * button-down for it before sending it. + * - in a system where one button is simulated by means of a + * modifier key and another button, buttons can mutate + * between press and release (possibly during drag). So we + * can see Ab instead of Aa. * - * - if we receive (or fabricate) a button down message - * without having seen a button up for the previously - * pressed button, we invent the button up before sending - * the button down. + * Definite requirements are: + * + * - button _presses_ must never be invented or destroyed. If + * the user presses two buttons in succession, the button + * presses must be transferred to the backend unchanged. So + * if we see AaBb , that's fine; if we see ABab (the button + * presses inadvertently overlapped) we must somehow + * `correct' it to AaBb. + * + * - every mouse action must end up looking like a press, zero + * or more drags, then a release. This allows back ends to + * make the _assumption_ that incoming mouse data will be + * sane in this regard, and not worry about the details. + * + * So my policy will be: + * + * - treat any button-up as a button-up for the currently + * pressed button, or ignore it if there is no currently + * pressed button. + * + * - treat any drag as a drag for the currently pressed + * button, or ignore it if there is no currently pressed + * button. + * + * - if we see a button-down while another button is currently + * pressed, invent a button-up for the first one and then + * pass the button-down through as before. * - * Therefore, front ends can just send whatever data they - * happen to be conveniently able to get, and back ends can be - * guaranteed of always receiving a down, zero or more drags - * and an up for a single button at a time. */ if (IS_MOUSE_DRAG(button) || IS_MOUSE_RELEASE(button)) { - int which; - - if (IS_MOUSE_DRAG(button)) - which = button + (LEFT_BUTTON - LEFT_DRAG); - else - which = button + (LEFT_BUTTON - LEFT_RELEASE); - - if (which != me->pressed_mouse_button) { - /* - * Fabricate a button-up for the currently pressed - * button, if any. - */ - if (me->pressed_mouse_button) { - ret = ret && midend_really_process_key - (me, x, y, (me->pressed_mouse_button + - (LEFT_RELEASE - LEFT_BUTTON))); + if (me->pressed_mouse_button) { + if (IS_MOUSE_DRAG(button)) { + button = me->pressed_mouse_button + + (LEFT_DRAG - LEFT_BUTTON); + } else { + button = me->pressed_mouse_button + + (LEFT_RELEASE - LEFT_BUTTON); } - - /* - * Now fabricate a button-down for this one. - */ - ret = ret && midend_really_process_key(me, x, y, which); - - /* - * And set the currently pressed button to this one. - */ - me->pressed_mouse_button = which; - } + } else + return ret; /* ignore it */ } else if (IS_MOUSE_DOWN(button) && me->pressed_mouse_button) { /* * Fabricate a button-up for the previously pressed button. @@ -394,9 +407,12 @@ float *midend_colours(midend_data *me, int *ncolours) float *ret; if (me->nstates == 0) { - char *seed = me->ourgame->new_seed(me->params, me->random); + game_aux_info *aux = NULL; + char *seed = me->ourgame->new_seed(me->params, me->random, &aux); state = me->ourgame->new_game(me->params, seed); sfree(seed); + if (aux) + me->ourgame->free_aux_info(aux); } else state = me->states[0]; @@ -535,6 +551,9 @@ char *midend_game_id(midend_data *me, char *id, int def_seed) sfree(me->seed); me->seed = dupstr(seed); me->fresh_seed = TRUE; + if (me->aux_info) + me->ourgame->free_aux_info(me->aux_info); + me->aux_info = NULL; } return NULL; @@ -568,3 +587,11 @@ char *midend_set_config(midend_data *me, int which, config_item *cfg) return NULL; } + +char *midend_text_format(midend_data *me) +{ + if (me->ourgame->can_format_as_text && me->statepos > 0) + return me->ourgame->text_format(me->states[me->statepos-1]); + else + return NULL; +}