chiark / gitweb /
Include code in midend.c to sanitise streams of mouse events so that
[sgt-puzzles.git] / midend.c
1 /*
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/restart/quit.
6  */
7
8 #include <stdio.h>
9 #include <string.h>
10 #include <assert.h>
11
12 #include "puzzles.h"
13
14 struct midend_data {
15     frontend *frontend;
16     random_state *random;
17     const game *ourgame;
18
19     char *seed;
20     int fresh_seed;
21     int nstates, statesize, statepos;
22
23     game_params **presets;
24     char **preset_names;
25     int npresets, presetsize;
26
27     game_params *params;
28     game_state **states;
29     game_drawstate *drawstate;
30     game_state *oldstate;
31     game_ui *ui;
32     float anim_time, anim_pos;
33     float flash_time, flash_pos;
34     int dir;
35
36     int pressed_mouse_button;
37 };
38
39 #define ensure(me) do { \
40     if ((me)->nstates >= (me)->statesize) { \
41         (me)->statesize = (me)->nstates + 128; \
42         (me)->states = sresize((me)->states, (me)->statesize, game_state *); \
43     } \
44 } while (0)
45
46 midend_data *midend_new(frontend *fe, const game *ourgame)
47 {
48     midend_data *me = snew(midend_data);
49     void *randseed;
50     int randseedsize;
51
52     get_random_seed(&randseed, &randseedsize);
53
54     me->frontend = fe;
55     me->ourgame = ourgame;
56     me->random = random_init(randseed, randseedsize);
57     me->nstates = me->statesize = me->statepos = 0;
58     me->states = NULL;
59     me->params = ourgame->default_params();
60     me->seed = NULL;
61     me->fresh_seed = FALSE;
62     me->drawstate = NULL;
63     me->oldstate = NULL;
64     me->presets = NULL;
65     me->preset_names = NULL;
66     me->npresets = me->presetsize = 0;
67     me->anim_time = me->anim_pos = 0.0F;
68     me->flash_time = me->flash_pos = 0.0F;
69     me->dir = 0;
70     me->ui = NULL;
71     me->pressed_mouse_button = 0;
72
73     sfree(randseed);
74
75     return me;
76 }
77
78 void midend_free(midend_data *me)
79 {
80     sfree(me->states);
81     sfree(me->seed);
82     me->ourgame->free_params(me->params);
83     sfree(me);
84 }
85
86 void midend_size(midend_data *me, int *x, int *y)
87 {
88     me->ourgame->size(me->params, x, y);
89 }
90
91 void midend_set_params(midend_data *me, game_params *params)
92 {
93     me->ourgame->free_params(me->params);
94     me->params = me->ourgame->dup_params(params);
95 }
96
97 void midend_new_game(midend_data *me)
98 {
99     while (me->nstates > 0)
100         me->ourgame->free_game(me->states[--me->nstates]);
101
102     if (me->drawstate)
103         me->ourgame->free_drawstate(me->drawstate);
104
105     assert(me->nstates == 0);
106
107     if (!me->fresh_seed) {
108         sfree(me->seed);
109         me->seed = me->ourgame->new_seed(me->params, me->random);
110     } else
111         me->fresh_seed = FALSE;
112
113     ensure(me);
114     me->states[me->nstates++] = me->ourgame->new_game(me->params, me->seed);
115     me->statepos = 1;
116     me->drawstate = me->ourgame->new_drawstate(me->states[0]);
117     if (me->ui)
118         me->ourgame->free_ui(me->ui);
119     me->ui = me->ourgame->new_ui(me->states[0]);
120     me->pressed_mouse_button = 0;
121 }
122
123 void midend_restart_game(midend_data *me)
124 {
125     while (me->nstates > 1)
126         me->ourgame->free_game(me->states[--me->nstates]);
127     me->statepos = me->nstates;
128     me->ourgame->free_ui(me->ui);
129     me->ui = me->ourgame->new_ui(me->states[0]);
130 }
131
132 static int midend_undo(midend_data *me)
133 {
134     if (me->statepos > 1) {
135         me->statepos--;
136         me->dir = -1;
137         return 1;
138     } else
139         return 0;
140 }
141
142 static int midend_redo(midend_data *me)
143 {
144     if (me->statepos < me->nstates) {
145         me->statepos++;
146         me->dir = +1;
147         return 1;
148     } else
149         return 0;
150 }
151
152 static void midend_finish_move(midend_data *me)
153 {
154     float flashtime;
155
156     if (me->oldstate || me->statepos > 1) {
157         flashtime = me->ourgame->flash_length(me->oldstate ? me->oldstate :
158                                               me->states[me->statepos-2],
159                                               me->states[me->statepos-1],
160                                               me->oldstate ? me->dir : +1);
161         if (flashtime > 0) {
162             me->flash_pos = 0.0F;
163             me->flash_time = flashtime;
164         }
165     }
166
167     if (me->oldstate)
168         me->ourgame->free_game(me->oldstate);
169     me->oldstate = NULL;
170     me->anim_pos = me->anim_time = 0;
171     me->dir = 0;
172
173     if (me->flash_time == 0 && me->anim_time == 0)
174         deactivate_timer(me->frontend);
175     else
176         activate_timer(me->frontend);
177 }
178
179 static void midend_stop_anim(midend_data *me)
180 {
181     if (me->oldstate || me->anim_time) {
182         midend_finish_move(me);
183         midend_redraw(me);
184     }
185 }
186
187 static int midend_really_process_key(midend_data *me, int x, int y, int button)
188 {
189     game_state *oldstate = me->ourgame->dup_game(me->states[me->statepos - 1]);
190     float anim_time;
191
192     if (button == 'n' || button == 'N' || button == '\x0E') {
193         midend_stop_anim(me);
194         midend_new_game(me);
195         midend_redraw(me);
196         return 1;                      /* never animate */
197     } else if (button == 'r' || button == 'R') {
198         midend_stop_anim(me);
199         midend_restart_game(me);
200         midend_redraw(me);
201         return 1;                      /* never animate */
202     } else if (button == 'u' || button == 'u' ||
203                button == '\x1A' || button == '\x1F') {
204         midend_stop_anim(me);
205         if (!midend_undo(me))
206             return 1;
207     } else if (button == '\x12') {
208         midend_stop_anim(me);
209         if (!midend_redo(me))
210             return 1;
211     } else if (button == 'q' || button == 'Q' || button == '\x11') {
212         me->ourgame->free_game(oldstate);
213         return 0;
214     } else {
215         game_state *s = me->ourgame->make_move(me->states[me->statepos-1],
216                                                me->ui, x, y, button);
217
218         if (s == me->states[me->statepos-1]) {
219             /*
220              * make_move() is allowed to return its input state to
221              * indicate that although no move has been made, the UI
222              * state has been updated and a redraw is called for.
223              */
224             midend_redraw(me);
225             return 1;
226         } else if (s) {
227             midend_stop_anim(me);
228             while (me->nstates > me->statepos)
229                 me->ourgame->free_game(me->states[--me->nstates]);
230             ensure(me);
231             me->states[me->nstates] = s;
232             me->statepos = ++me->nstates;
233             me->dir = +1;
234         } else {
235             me->ourgame->free_game(oldstate);
236             return 1;
237         }
238     }
239
240     /*
241      * See if this move requires an animation.
242      */
243     anim_time = me->ourgame->anim_length(oldstate, me->states[me->statepos-1],
244                                          me->dir);
245
246     me->oldstate = oldstate;
247     if (anim_time > 0) {
248         me->anim_time = anim_time;
249     } else {
250         me->anim_time = 0.0;
251         midend_finish_move(me);
252     }
253     me->anim_pos = 0.0;
254
255     midend_redraw(me);
256
257     activate_timer(me->frontend);
258
259     return 1;
260 }
261
262 int midend_process_key(midend_data *me, int x, int y, int button)
263 {
264     int ret = 1;
265
266     /*
267      * Harmonise mouse drag and release messages.
268      * 
269      * Some front ends might accidentally switch from sending, say,
270      * RIGHT_DRAG messages to sending LEFT_DRAG, half way through a
271      * drag. (This can happen on the Mac, for example, since
272      * RIGHT_DRAG is usually done using Command+drag, and if the
273      * user accidentally releases Command half way through the drag
274      * then there will be trouble.)
275      * 
276      * It would be an O(number of front ends) annoyance to fix this
277      * in the front ends, but an O(number of back ends) annoyance
278      * to have each game capable of dealing with it. Therefore, we
279      * fix it _here_ in the common midend code so that it only has
280      * to be done once.
281      * 
282      * Semantics:
283      * 
284      *  - when we receive a button down message, we remember which
285      *    button went down.
286      * 
287      *  - if we receive a drag or release message for a button we
288      *    don't currently think is pressed, we fabricate a
289      *    button-down for it before sending it.
290      * 
291      *  - if we receive (or fabricate) a button down message
292      *    without having seen a button up for the previously
293      *    pressed button, we invent the button up before sending
294      *    the button down.
295      * 
296      * Therefore, front ends can just send whatever data they
297      * happen to be conveniently able to get, and back ends can be
298      * guaranteed of always receiving a down, zero or more drags
299      * and an up for a single button at a time.
300      */
301     if (IS_MOUSE_DRAG(button) || IS_MOUSE_RELEASE(button)) {
302         int which;
303
304         if (IS_MOUSE_DRAG(button))
305             which = button + (LEFT_BUTTON - LEFT_DRAG);
306         else
307             which = button + (LEFT_BUTTON - LEFT_RELEASE);
308
309         if (which != me->pressed_mouse_button) {
310             /*
311              * Fabricate a button-up for the currently pressed
312              * button, if any.
313              */
314             if (me->pressed_mouse_button) {
315                 ret = ret && midend_really_process_key
316                     (me, x, y, (me->pressed_mouse_button +
317                                 (LEFT_RELEASE - LEFT_BUTTON)));
318             }
319
320             /*
321              * Now fabricate a button-down for this one.
322              */
323             ret = ret && midend_really_process_key(me, x, y, which);
324
325             /*
326              * And set the currently pressed button to this one.
327              */
328             me->pressed_mouse_button = which;
329         }
330     } else if (IS_MOUSE_DOWN(button) && me->pressed_mouse_button) {
331         /*
332          * Fabricate a button-up for the previously pressed button.
333          */
334         ret = ret && midend_really_process_key
335             (me, x, y, (me->pressed_mouse_button +
336                         (LEFT_RELEASE - LEFT_BUTTON)));
337     }
338
339     /*
340      * Now send on the event we originally received.
341      */
342     ret = ret && midend_really_process_key(me, x, y, button);
343
344     /*
345      * And update the currently pressed button.
346      */
347     if (IS_MOUSE_RELEASE(button))
348         me->pressed_mouse_button = 0;
349     else if (IS_MOUSE_DOWN(button))
350         me->pressed_mouse_button = button;
351
352     return ret;
353 }
354
355 void midend_redraw(midend_data *me)
356 {
357     if (me->statepos > 0 && me->drawstate) {
358         start_draw(me->frontend);
359         if (me->oldstate && me->anim_time > 0 &&
360             me->anim_pos < me->anim_time) {
361             assert(me->dir != 0);
362             me->ourgame->redraw(me->frontend, me->drawstate, me->oldstate,
363                                 me->states[me->statepos-1], me->dir,
364                                 me->ui, me->anim_pos, me->flash_pos);
365         } else {
366             me->ourgame->redraw(me->frontend, me->drawstate, NULL,
367                                 me->states[me->statepos-1], +1 /*shrug*/,
368                                 me->ui, 0.0, me->flash_pos);
369         }
370         end_draw(me->frontend);
371     }
372 }
373
374 void midend_timer(midend_data *me, float tplus)
375 {
376     me->anim_pos += tplus;
377     if (me->anim_pos >= me->anim_time ||
378         me->anim_time == 0 || !me->oldstate) {
379         if (me->anim_time > 0)
380             midend_finish_move(me);
381     }
382     me->flash_pos += tplus;
383     if (me->flash_pos >= me->flash_time || me->flash_time == 0) {
384         me->flash_pos = me->flash_time = 0;
385     }
386     if (me->flash_time == 0 && me->anim_time == 0)
387         deactivate_timer(me->frontend);
388     midend_redraw(me);
389 }
390
391 float *midend_colours(midend_data *me, int *ncolours)
392 {
393     game_state *state = NULL;
394     float *ret;
395
396     if (me->nstates == 0) {
397         char *seed = me->ourgame->new_seed(me->params, me->random);
398         state = me->ourgame->new_game(me->params, seed);
399         sfree(seed);
400     } else
401         state = me->states[0];
402
403     ret = me->ourgame->colours(me->frontend, state, ncolours);
404
405     if (me->nstates == 0)
406         me->ourgame->free_game(state);
407
408     return ret;
409 }
410
411 int midend_num_presets(midend_data *me)
412 {
413     if (!me->npresets) {
414         char *name;
415         game_params *preset;
416
417         while (me->ourgame->fetch_preset(me->npresets, &name, &preset)) {
418             if (me->presetsize <= me->npresets) {
419                 me->presetsize = me->npresets + 10;
420                 me->presets = sresize(me->presets, me->presetsize,
421                                       game_params *);
422                 me->preset_names = sresize(me->preset_names, me->presetsize,
423                                            char *);
424             }
425
426             me->presets[me->npresets] = preset;
427             me->preset_names[me->npresets] = name;
428             me->npresets++;
429         }
430     }
431
432     return me->npresets;
433 }
434
435 void midend_fetch_preset(midend_data *me, int n,
436                          char **name, game_params **params)
437 {
438     assert(n >= 0 && n < me->npresets);
439     *name = me->preset_names[n];
440     *params = me->presets[n];
441 }
442
443 int midend_wants_statusbar(midend_data *me)
444 {
445     return me->ourgame->wants_statusbar();
446 }
447
448 config_item *midend_get_config(midend_data *me, int which, char **wintitle)
449 {
450     char *titlebuf, *parstr;
451     config_item *ret;
452
453     titlebuf = snewn(40 + strlen(me->ourgame->name), char);
454
455     switch (which) {
456       case CFG_SETTINGS:
457         sprintf(titlebuf, "%s configuration", me->ourgame->name);
458         *wintitle = dupstr(titlebuf);
459         return me->ourgame->configure(me->params);
460       case CFG_SEED:
461         sprintf(titlebuf, "%s game selection", me->ourgame->name);
462         *wintitle = dupstr(titlebuf);
463
464         ret = snewn(2, config_item);
465
466         ret[0].type = C_STRING;
467         ret[0].name = "Game ID";
468         ret[0].ival = 0;
469         /*
470          * The text going in here will be a string encoding of the
471          * parameters, plus a colon, plus the game seed. This is a
472          * full game ID.
473          */
474         parstr = me->ourgame->encode_params(me->params);
475         ret[0].sval = snewn(strlen(parstr) + strlen(me->seed) + 2, char);
476         sprintf(ret[0].sval, "%s:%s", parstr, me->seed);
477         sfree(parstr);
478
479         ret[1].type = C_END;
480         ret[1].name = ret[1].sval = NULL;
481         ret[1].ival = 0;
482
483         return ret;
484     }
485
486     assert(!"We shouldn't be here");
487     return NULL;
488 }
489
490 char *midend_game_id(midend_data *me, char *id, int def_seed)
491 {
492     char *error, *par, *seed;
493     game_params *params;
494
495     seed = strchr(id, ':');
496
497     if (seed) {
498         /*
499          * We have a colon separating parameters from game seed. So
500          * `par' now points to the parameters string, and `seed' to
501          * the seed string.
502          */
503         *seed++ = '\0';
504         par = id;
505     } else {
506         /*
507          * We only have one string. Depending on `def_seed', we
508          * take it to be either parameters or seed.
509          */
510         if (def_seed) {
511             seed = id;
512             par = NULL;
513         } else {
514             seed = NULL;
515             par = id;
516         }
517     }
518
519     if (par) {
520         params = me->ourgame->decode_params(par);
521         error = me->ourgame->validate_params(params);
522         if (error) {
523             me->ourgame->free_params(params);
524             return error;
525         }
526         me->ourgame->free_params(me->params);
527         me->params = params;
528     }
529
530     if (seed) {
531         error = me->ourgame->validate_seed(me->params, seed);
532         if (error)
533             return error;
534
535         sfree(me->seed);
536         me->seed = dupstr(seed);
537         me->fresh_seed = TRUE;
538     }
539
540     return NULL;
541 }
542
543 char *midend_set_config(midend_data *me, int which, config_item *cfg)
544 {
545     char *error;
546     game_params *params;
547
548     switch (which) {
549       case CFG_SETTINGS:
550         params = me->ourgame->custom_params(cfg);
551         error = me->ourgame->validate_params(params);
552
553         if (error) {
554             me->ourgame->free_params(params);
555             return error;
556         }
557
558         me->ourgame->free_params(me->params);
559         me->params = params;
560         break;
561
562       case CFG_SEED:
563         error = midend_game_id(me, cfg[0].sval, TRUE);
564         if (error)
565             return error;
566         break;
567     }
568
569     return NULL;
570 }