rnd-20060314-1-src
[rocksndiamonds.git] / src / events.c
1 /***********************************************************
2 * Rocks'n'Diamonds -- McDuffin Strikes Back!               *
3 *----------------------------------------------------------*
4 * (c) 1995-2002 Artsoft Entertainment                      *
5 *               Holger Schemel                             *
6 *               Detmolder Strasse 189                      *
7 *               33604 Bielefeld                            *
8 *               Germany                                    *
9 *               e-mail: info@artsoft.org                   *
10 *----------------------------------------------------------*
11 * events.c                                                 *
12 ***********************************************************/
13
14 #include "libgame/libgame.h"
15
16 #include "events.h"
17 #include "init.h"
18 #include "screens.h"
19 #include "tools.h"
20 #include "game.h"
21 #include "editor.h"
22 #include "files.h"
23 #include "tape.h"
24 #include "network.h"
25
26
27 static boolean cursor_inside_playfield = FALSE;
28 static boolean playfield_cursor_set = FALSE;
29 static unsigned long playfield_cursor_delay = 0;
30
31
32 /* event filter especially needed for SDL event filtering due to
33    delay problems with lots of mouse motion events when mouse button
34    not pressed (X11 can handle this with 'PointerMotionHintMask') */
35
36 int FilterMouseMotionEvents(const Event *event)
37 {
38   MotionEvent *motion;
39
40   /* non-motion events are directly passed to event handler functions */
41   if (event->type != EVENT_MOTIONNOTIFY)
42     return 1;
43
44   motion = (MotionEvent *)event;
45   cursor_inside_playfield = (motion->x >= SX && motion->x < SX + SXSIZE &&
46                              motion->y >= SY && motion->y < SY + SYSIZE);
47
48   if (game_status == GAME_MODE_PLAYING && playfield_cursor_set)
49   {
50     SetMouseCursor(CURSOR_DEFAULT);
51     playfield_cursor_set = FALSE;
52     DelayReached(&playfield_cursor_delay, 0);
53   }
54
55   /* skip mouse motion events without pressed button outside level editor */
56   if (button_status == MB_RELEASED &&
57       game_status != GAME_MODE_EDITOR && game_status != GAME_MODE_PLAYING)
58     return 0;
59   else
60     return 1;
61 }
62
63 /* to prevent delay problems, skip mouse motion events if the very next
64    event is also a mouse motion event (and therefore effectively only
65    handling the last of a row of mouse motion events in the event queue) */
66
67 boolean SkipPressedMouseMotionEvent(const Event *event)
68 {
69   /* nothing to do if the current event is not a mouse motion event */
70   if (event->type != EVENT_MOTIONNOTIFY)
71     return FALSE;
72
73   /* only skip motion events with pressed button outside level editor */
74   if (button_status == MB_RELEASED ||
75       game_status == GAME_MODE_EDITOR || game_status == GAME_MODE_PLAYING)
76     return FALSE;
77
78   if (PendingEvent())
79   {
80     Event next_event;
81
82     PeekEvent(&next_event);
83
84     /* if next event is also a mouse motion event, skip the current one */
85     if (next_event.type == EVENT_MOTIONNOTIFY)
86       return TRUE;
87   }
88
89   return FALSE;
90 }
91
92 /* this is only really needed for non-SDL targets to filter unwanted events;
93    when using SDL with properly installed event filter, this function can be
94    replaced with a simple "NextEvent()" call, but it doesn't hurt either */
95
96 static boolean NextValidEvent(Event *event)
97 {
98   while (PendingEvent())
99   {
100     boolean handle_this_event = FALSE;
101
102     NextEvent(event);
103
104     if (FilterMouseMotionEvents(event))
105       handle_this_event = TRUE;
106
107     if (SkipPressedMouseMotionEvent(event))
108       handle_this_event = FALSE;
109
110     if (handle_this_event)
111       return TRUE;
112   }
113
114   return FALSE;
115 }
116
117 void EventLoop(void)
118 {
119   while (1)
120   {
121     if (PendingEvent())         /* got event */
122     {
123       Event event;
124
125       while (NextValidEvent(&event))
126       {
127         switch(event.type)
128         {
129           case EVENT_BUTTONPRESS:
130           case EVENT_BUTTONRELEASE:
131             HandleButtonEvent((ButtonEvent *) &event);
132             break;
133   
134           case EVENT_MOTIONNOTIFY:
135             HandleMotionEvent((MotionEvent *) &event);
136             break;
137   
138           case EVENT_KEYPRESS:
139           case EVENT_KEYRELEASE:
140             HandleKeyEvent((KeyEvent *) &event);
141             break;
142   
143           default:
144             HandleOtherEvents(&event);
145             break;
146         }
147       }
148     }
149     else
150     {
151       /* when playing, display a special mouse pointer inside the playfield */
152       if (game_status == GAME_MODE_PLAYING && !tape.pausing)
153       {
154         if (!playfield_cursor_set && cursor_inside_playfield &&
155             DelayReached(&playfield_cursor_delay, 1000))
156         {
157           SetMouseCursor(CURSOR_PLAYFIELD);
158           playfield_cursor_set = TRUE;
159         }
160       }
161       else if (playfield_cursor_set)
162       {
163         SetMouseCursor(CURSOR_DEFAULT);
164         playfield_cursor_set = FALSE;
165       }
166
167       HandleNoEvent();
168     }
169
170     /* don't use all CPU time when idle; the main loop while playing
171        has its own synchronization and is CPU friendly, too */
172
173     if (game_status == GAME_MODE_PLAYING)
174       HandleGameActions();
175     else
176     {
177       SyncDisplay();
178       if (!PendingEvent())      /* delay only if no pending events */
179         Delay(10);
180     }
181
182     /* refresh window contents from drawing buffer, if needed */
183     BackToFront();
184
185     if (game_status == GAME_MODE_QUIT)
186       return;
187   }
188 }
189
190 void HandleOtherEvents(Event *event)
191 {
192   switch(event->type)
193   {
194     case EVENT_EXPOSE:
195       HandleExposeEvent((ExposeEvent *) event);
196       break;
197
198     case EVENT_UNMAPNOTIFY:
199 #if 0
200       /* This causes the game to stop not only when iconified, but also
201          when on another virtual desktop, which might be not desired. */
202       SleepWhileUnmapped();
203 #endif
204       break;
205
206     case EVENT_FOCUSIN:
207     case EVENT_FOCUSOUT:
208       HandleFocusEvent((FocusChangeEvent *) event);
209       break;
210
211     case EVENT_CLIENTMESSAGE:
212       HandleClientMessageEvent((ClientMessageEvent *) event);
213       break;
214
215 #if defined(TARGET_SDL)
216     case SDL_JOYAXISMOTION:
217     case SDL_JOYBUTTONDOWN:
218     case SDL_JOYBUTTONUP:
219       HandleJoystickEvent(event);
220       break;
221 #endif
222
223     default:
224       break;
225   }
226 }
227
228 void ClearEventQueue()
229 {
230   while (PendingEvent())
231   {
232     Event event;
233
234     NextEvent(&event);
235
236     switch(event.type)
237     {
238       case EVENT_BUTTONRELEASE:
239         button_status = MB_RELEASED;
240         break;
241
242       case EVENT_KEYRELEASE:
243         key_joystick_mapping = 0;
244         break;
245
246       default:
247         HandleOtherEvents(&event);
248         break;
249     }
250   }
251 }
252
253 void ClearPlayerAction()
254 {
255   int i;
256
257   /* simulate key release events for still pressed keys */
258   key_joystick_mapping = 0;
259   for (i = 0; i < MAX_PLAYERS; i++)
260     stored_player[i].action = 0;
261 }
262
263 void SleepWhileUnmapped()
264 {
265   boolean window_unmapped = TRUE;
266
267   KeyboardAutoRepeatOn();
268
269   while (window_unmapped)
270   {
271     Event event;
272
273     NextEvent(&event);
274
275     switch(event.type)
276     {
277       case EVENT_BUTTONRELEASE:
278         button_status = MB_RELEASED;
279         break;
280
281       case EVENT_KEYRELEASE:
282         key_joystick_mapping = 0;
283         break;
284
285       case EVENT_MAPNOTIFY:
286         window_unmapped = FALSE;
287         break;
288
289       case EVENT_UNMAPNOTIFY:
290         /* this is only to surely prevent the 'should not happen' case
291          * of recursively looping between 'SleepWhileUnmapped()' and
292          * 'HandleOtherEvents()' which usually calls this funtion.
293          */
294         break;
295
296       default:
297         HandleOtherEvents(&event);
298         break;
299     }
300   }
301
302   if (game_status == GAME_MODE_PLAYING)
303     KeyboardAutoRepeatOffUnlessAutoplay();
304 }
305
306 void HandleExposeEvent(ExposeEvent *event)
307 {
308 #ifndef TARGET_SDL
309   RedrawPlayfield(FALSE, event->x, event->y, event->width, event->height);
310   FlushDisplay();
311 #endif
312 }
313
314 void HandleButtonEvent(ButtonEvent *event)
315 {
316   motion_status = FALSE;
317
318   if (event->type == EVENT_BUTTONPRESS)
319     button_status = event->button;
320   else
321     button_status = MB_RELEASED;
322
323   HandleButton(event->x, event->y, button_status);
324 }
325
326 void HandleMotionEvent(MotionEvent *event)
327 {
328   if (!PointerInWindow(window))
329     return;     /* window and pointer are on different screens */
330
331   if (button_status == MB_RELEASED && game_status != GAME_MODE_EDITOR)
332     return;
333
334   motion_status = TRUE;
335
336   HandleButton(event->x, event->y, button_status);
337 }
338
339 void HandleKeyEvent(KeyEvent *event)
340 {
341   int key_status = (event->type==EVENT_KEYPRESS ? KEY_PRESSED : KEY_RELEASED);
342   boolean with_modifiers = (game_status == GAME_MODE_PLAYING ? FALSE : TRUE);
343   Key key = GetEventKey(event, with_modifiers);
344   Key keymod = (with_modifiers ? GetEventKey(event, FALSE) : key);
345
346   HandleKeyModState(keymod, key_status);
347   HandleKey(key, key_status);
348 }
349
350 void HandleFocusEvent(FocusChangeEvent *event)
351 {
352   static int old_joystick_status = -1;
353
354   if (event->type == EVENT_FOCUSOUT)
355   {
356     KeyboardAutoRepeatOn();
357     old_joystick_status = joystick.status;
358     joystick.status = JOYSTICK_NOT_AVAILABLE;
359
360     ClearPlayerAction();
361   }
362   else if (event->type == EVENT_FOCUSIN)
363   {
364     /* When there are two Rocks'n'Diamonds windows which overlap and
365        the player moves the pointer from one game window to the other,
366        a 'FocusOut' event is generated for the window the pointer is
367        leaving and a 'FocusIn' event is generated for the window the
368        pointer is entering. In some cases, it can happen that the
369        'FocusIn' event is handled by the one game process before the
370        'FocusOut' event by the other game process. In this case the
371        X11 environment would end up with activated keyboard auto repeat,
372        because unfortunately this is a global setting and not (which
373        would be far better) set for each X11 window individually.
374        The effect would be keyboard auto repeat while playing the game
375        (game_status == GAME_MODE_PLAYING), which is not desired.
376        To avoid this special case, we just wait 1/10 second before
377        processing the 'FocusIn' event.
378     */
379
380     if (game_status == GAME_MODE_PLAYING)
381     {
382       Delay(100);
383       KeyboardAutoRepeatOffUnlessAutoplay();
384     }
385
386     if (old_joystick_status != -1)
387       joystick.status = old_joystick_status;
388   }
389 }
390
391 void HandleClientMessageEvent(ClientMessageEvent *event)
392 {
393   if (CheckCloseWindowEvent(event))
394     CloseAllAndExit(0);
395 }
396
397 void HandleButton(int mx, int my, int button)
398 {
399   static int old_mx = 0, old_my = 0;
400
401   if (button < 0)
402   {
403     mx = old_mx;
404     my = old_my;
405     button = -button;
406   }
407   else
408   {
409     old_mx = mx;
410     old_my = my;
411   }
412
413   if (HandleGadgets(mx, my, button))
414   {
415     /* do not handle this button event anymore */
416     mx = my = -32;      /* force mouse event to be outside screen tiles */
417   }
418
419   switch(game_status)
420   {
421     case GAME_MODE_MAIN:
422       HandleMainMenu(mx,my, 0,0, button);
423       break;
424
425     case GAME_MODE_PSEUDO_TYPENAME:
426       HandleTypeName(0, KSYM_Return);
427       break;
428
429     case GAME_MODE_LEVELS:
430       HandleChooseLevel(mx,my, 0,0, button);
431       break;
432
433     case GAME_MODE_SCORES:
434       HandleHallOfFame(0,0, 0,0, button);
435       break;
436
437     case GAME_MODE_EDITOR:
438       HandleLevelEditorIdle();
439       break;
440
441     case GAME_MODE_INFO:
442       HandleInfoScreen(mx,my, 0,0, button);
443       break;
444
445     case GAME_MODE_SETUP:
446       HandleSetupScreen(mx,my, 0,0, button);
447       break;
448
449     case GAME_MODE_PLAYING:
450 #ifdef DEBUG
451       if (button == MB_PRESSED && !motion_status && IN_GFX_SCREEN(mx, my))
452         DumpTile(LEVELX((mx - SX) / TILEX), LEVELY((my - SY) / TILEY));
453 #endif
454       break;
455
456     default:
457       break;
458   }
459 }
460
461 static boolean is_string_suffix(char *string, char *suffix)
462 {
463   int string_len = strlen(string);
464   int suffix_len = strlen(suffix);
465
466   if (suffix_len > string_len)
467     return FALSE;
468
469   return (strcmp(&string[string_len - suffix_len], suffix) == 0);
470 }
471
472 #define MAX_CHEAT_INPUT_LEN     32
473
474 static void HandleKeysSpecial(Key key)
475 {
476   static char cheat_input[2 * MAX_CHEAT_INPUT_LEN + 1] = "";
477   char letter = getCharFromKey(key);
478   int cheat_input_len = strlen(cheat_input);
479   int i;
480
481   if (letter == 0)
482     return;
483
484   if (cheat_input_len >= 2 * MAX_CHEAT_INPUT_LEN)
485   {
486     for (i = 0; i < MAX_CHEAT_INPUT_LEN + 1; i++)
487       cheat_input[i] = cheat_input[MAX_CHEAT_INPUT_LEN + i];
488
489     cheat_input_len = MAX_CHEAT_INPUT_LEN;
490   }
491
492   cheat_input[cheat_input_len++] = letter;
493   cheat_input[cheat_input_len] = '\0';
494
495 #if 0
496   printf("::: '%s' [%d]\n", cheat_input, cheat_input_len);
497 #endif
498
499   if (game_status == GAME_MODE_MAIN)
500   {
501     if (is_string_suffix(cheat_input, ":insert-solution-tape") ||
502         is_string_suffix(cheat_input, ":ist"))
503     {
504       InsertSolutionTape();
505     }
506     else if (is_string_suffix(cheat_input, ":reload-graphics") ||
507              is_string_suffix(cheat_input, ":rg"))
508     {
509       ReloadCustomArtwork(1 << ARTWORK_TYPE_GRAPHICS);
510       DrawMainMenu();
511     }
512     else if (is_string_suffix(cheat_input, ":reload-sounds") ||
513              is_string_suffix(cheat_input, ":rs"))
514     {
515       ReloadCustomArtwork(1 << ARTWORK_TYPE_SOUNDS);
516       DrawMainMenu();
517     }
518     else if (is_string_suffix(cheat_input, ":reload-music") ||
519              is_string_suffix(cheat_input, ":rm"))
520     {
521       ReloadCustomArtwork(1 << ARTWORK_TYPE_MUSIC);
522       DrawMainMenu();
523     }
524     else if (is_string_suffix(cheat_input, ":reload-artwork") ||
525              is_string_suffix(cheat_input, ":ra"))
526     {
527       ReloadCustomArtwork(1 << ARTWORK_TYPE_GRAPHICS |
528                           1 << ARTWORK_TYPE_SOUNDS |
529                           1 << ARTWORK_TYPE_MUSIC);
530       DrawMainMenu();
531     }
532     else if (is_string_suffix(cheat_input, ":dump-level") ||
533              is_string_suffix(cheat_input, ":dl"))
534     {
535       DumpLevel(&level);
536     }
537     else if (is_string_suffix(cheat_input, ":dump-tape") ||
538              is_string_suffix(cheat_input, ":dt"))
539     {
540       DumpTape(&tape);
541     }
542   }
543   else if (game_status == GAME_MODE_PLAYING)
544   {
545 #ifdef DEBUG
546     if (is_string_suffix(cheat_input, ".q"))
547       DEBUG_SetMaximumDynamite();
548 #endif
549   }
550   else if (game_status == GAME_MODE_EDITOR)
551   {
552     if (is_string_suffix(cheat_input, ":dump-brush") ||
553         is_string_suffix(cheat_input, ":DB"))
554     {
555       DumpBrush();
556     }
557     else if (is_string_suffix(cheat_input, ":DDB"))
558     {
559       DumpBrush_Small();
560     }
561   }
562 }
563
564 void HandleKey(Key key, int key_status)
565 {
566   boolean anyTextGadgetActiveOrJustFinished = anyTextGadgetActive();
567   static struct SetupKeyboardInfo custom_key;
568   static struct
569   {
570     Key *key_custom;
571     Key key_default;
572     byte action;
573   } key_info[] =
574   {
575     { &custom_key.left,  DEFAULT_KEY_LEFT,  JOY_LEFT     },
576     { &custom_key.right, DEFAULT_KEY_RIGHT, JOY_RIGHT    },
577     { &custom_key.up,    DEFAULT_KEY_UP,    JOY_UP       },
578     { &custom_key.down,  DEFAULT_KEY_DOWN,  JOY_DOWN     },
579     { &custom_key.snap,  DEFAULT_KEY_SNAP,  JOY_BUTTON_1 },
580     { &custom_key.drop,  DEFAULT_KEY_DROP,  JOY_BUTTON_2 }
581   };
582   int joy = 0;
583   int i;
584
585   if (game_status == GAME_MODE_PLAYING)
586   {
587     /* only needed for single-step tape recording mode */
588     static boolean clear_button_2[MAX_PLAYERS] = { FALSE,FALSE,FALSE,FALSE };
589     static boolean element_dropped[MAX_PLAYERS] = { FALSE,FALSE,FALSE,FALSE };
590     int pnr;
591
592     for (pnr = 0; pnr < MAX_PLAYERS; pnr++)
593     {
594       byte key_action = 0;
595
596       if (setup.input[pnr].use_joystick)
597         continue;
598
599       custom_key = setup.input[pnr].key;
600
601       for (i = 0; i < 6; i++)
602         if (key == *key_info[i].key_custom)
603           key_action |= key_info[i].action;
604
605       if (tape.single_step && clear_button_2[pnr])
606       {
607         stored_player[pnr].action &= ~KEY_BUTTON_2;
608         clear_button_2[pnr] = FALSE;
609       }
610
611       if (key_status == KEY_PRESSED)
612         stored_player[pnr].action |= key_action;
613       else
614         stored_player[pnr].action &= ~key_action;
615
616       if (tape.single_step && tape.recording && tape.pausing)
617       {
618         if (key_status == KEY_PRESSED &&
619             (key_action & (KEY_MOTION | KEY_BUTTON_1)))
620         {
621           TapeTogglePause(TAPE_TOGGLE_AUTOMATIC);
622
623           if (key_action & KEY_MOTION)
624           {
625             if (stored_player[pnr].action & KEY_BUTTON_2)
626               element_dropped[pnr] = TRUE;
627           }
628         }
629         else if (key_status == KEY_RELEASED &&
630                  (key_action & KEY_BUTTON_2))
631         {
632           if (!element_dropped[pnr])
633           {
634             TapeTogglePause(TAPE_TOGGLE_AUTOMATIC);
635
636             stored_player[pnr].action |= KEY_BUTTON_2;
637             clear_button_2[pnr] = TRUE;
638           }
639
640           element_dropped[pnr] = FALSE;
641         }
642       }
643       else if (tape.recording && tape.pausing && (key_action & KEY_ACTION))
644         TapeTogglePause(TAPE_TOGGLE_MANUAL);
645     }
646   }
647   else
648   {
649     for (i = 0; i < 6; i++)
650       if (key == key_info[i].key_default)
651         joy |= key_info[i].action;
652   }
653
654   if (joy)
655   {
656     if (key_status == KEY_PRESSED)
657       key_joystick_mapping |= joy;
658     else
659       key_joystick_mapping &= ~joy;
660
661     HandleJoystick();
662   }
663
664   if (game_status != GAME_MODE_PLAYING)
665     key_joystick_mapping = 0;
666
667   if (key_status == KEY_RELEASED)
668     return;
669
670   if (game_status == GAME_MODE_PLAYING && AllPlayersGone &&
671       (key == KSYM_Return || key == setup.shortcut.toggle_pause))
672   {
673     CloseDoor(DOOR_CLOSE_1);
674     game_status = GAME_MODE_MAIN;
675     DrawMainMenu();
676
677     return;
678   }
679
680   if (game_status == GAME_MODE_MAIN &&
681       (key == setup.shortcut.toggle_pause || key == KSYM_space))
682   {
683     StartGameActions(options.network, setup.autorecord, NEW_RANDOMIZE);
684
685     return;
686   }
687
688   if (game_status == GAME_MODE_MAIN || game_status == GAME_MODE_PLAYING)
689   {
690     if (key == setup.shortcut.save_game)
691       TapeQuickSave();
692     else if (key == setup.shortcut.load_game)
693       TapeQuickLoad();
694     else if (key == setup.shortcut.toggle_pause)
695       TapeTogglePause(TAPE_TOGGLE_MANUAL);
696   }
697
698   if (game_status == GAME_MODE_PLAYING)
699   {
700     int centered_player_nr_next = -999;
701
702     if (key == setup.shortcut.focus_player_all)
703       centered_player_nr_next = -1;
704     else
705       for (i = 0; i < MAX_PLAYERS; i++)
706         if (key == setup.shortcut.focus_player[i])
707           centered_player_nr_next = i;
708
709     if (centered_player_nr_next != -999)
710     {
711       game.centered_player_nr_next = centered_player_nr_next;
712       game.set_centered_player = TRUE;
713
714       if (tape.recording)
715       {
716         tape.centered_player_nr_next = game.centered_player_nr_next;
717         tape.set_centered_player = TRUE;
718       }
719     }
720   }
721
722   HandleKeysSpecial(key);
723
724   if (HandleGadgetsKeyInput(key))
725   {
726     if (key != KSYM_Escape)     /* always allow ESC key to be handled */
727       key = KSYM_UNDEFINED;
728   }
729
730   switch(game_status)
731   {
732     case GAME_MODE_PSEUDO_TYPENAME:
733       HandleTypeName(0, key);
734       break;
735
736     case GAME_MODE_MAIN:
737     case GAME_MODE_LEVELS:
738     case GAME_MODE_SETUP:
739     case GAME_MODE_INFO:
740       switch(key)
741       {
742 #if 1
743         case KSYM_space:
744 #else
745         /* !!! only use "space" key to start game from main menu !!! */
746         case KSYM_space:
747 #endif
748         case KSYM_Return:
749           if (game_status == GAME_MODE_MAIN)
750             HandleMainMenu(0,0, 0,0, MB_MENU_CHOICE);
751           else if (game_status == GAME_MODE_LEVELS)
752             HandleChooseLevel(0,0, 0,0, MB_MENU_CHOICE);
753           else if (game_status == GAME_MODE_SETUP)
754             HandleSetupScreen(0,0, 0,0, MB_MENU_CHOICE);
755           else if (game_status == GAME_MODE_INFO)
756             HandleInfoScreen(0,0, 0,0, MB_MENU_CHOICE);
757           break;
758
759         case KSYM_Escape:
760           if (game_status == GAME_MODE_LEVELS)
761             HandleChooseLevel(0,0, 0,0, MB_MENU_LEAVE);
762           else if (game_status == GAME_MODE_SETUP)
763             HandleSetupScreen(0,0, 0,0, MB_MENU_LEAVE);
764           else if (game_status == GAME_MODE_INFO)
765             HandleInfoScreen(0,0, 0,0, MB_MENU_LEAVE);
766           break;
767
768         case KSYM_Page_Up:
769           if (game_status == GAME_MODE_LEVELS)
770             HandleChooseLevel(0,0, 0, -1 * SCROLL_PAGE, MB_MENU_MARK);
771           else if (game_status == GAME_MODE_SETUP)
772             HandleSetupScreen(0,0, 0, -1 * SCROLL_PAGE, MB_MENU_MARK);
773           else if (game_status == GAME_MODE_INFO)
774             HandleInfoScreen(0,0, 0, -1 * SCROLL_PAGE, MB_MENU_MARK);
775           break;
776
777         case KSYM_Page_Down:
778           if (game_status == GAME_MODE_LEVELS)
779             HandleChooseLevel(0,0, 0, +1 * SCROLL_PAGE, MB_MENU_MARK);
780           else if (game_status == GAME_MODE_SETUP)
781             HandleSetupScreen(0,0, 0, +1 * SCROLL_PAGE, MB_MENU_MARK);
782           else if (game_status == GAME_MODE_INFO)
783             HandleInfoScreen(0,0, 0, +1 * SCROLL_PAGE, MB_MENU_MARK);
784           break;
785
786 #ifdef DEBUG
787         case KSYM_0:
788           GameFrameDelay = (GameFrameDelay == 500 ? GAME_FRAME_DELAY : 500);
789           break;
790 #endif
791
792         default:
793           break;
794       }
795       break;
796
797     case GAME_MODE_SCORES:
798       switch(key)
799       {
800         case KSYM_space:
801         case KSYM_Return:
802         case KSYM_Escape:
803           game_status = GAME_MODE_MAIN;
804           DrawMainMenu();
805           break;
806
807         case KSYM_Page_Up:
808           HandleHallOfFame(0,0, 0, -1 * SCROLL_PAGE, MB_MENU_MARK);
809           break;
810
811         case KSYM_Page_Down:
812           HandleHallOfFame(0,0, 0, +1 * SCROLL_PAGE, MB_MENU_MARK);
813           break;
814
815         default:
816           break;
817       }
818       break;
819
820     case GAME_MODE_EDITOR:
821       if (!anyTextGadgetActiveOrJustFinished || key == KSYM_Escape)
822         HandleLevelEditorKeyInput(key);
823       break;
824
825     case GAME_MODE_PLAYING:
826     {
827       switch(key)
828       {
829         case KSYM_Escape:
830           RequestQuitGame(setup.ask_on_escape);
831           break;
832
833 #ifdef DEBUG
834         case KSYM_0:
835 #if 0
836         case KSYM_1:
837         case KSYM_2:
838         case KSYM_3:
839         case KSYM_4:
840         case KSYM_5:
841         case KSYM_6:
842         case KSYM_7:
843         case KSYM_8:
844         case KSYM_9:
845 #endif
846           if (key == KSYM_0)
847           {
848             if (GameFrameDelay == 500)
849               GameFrameDelay = GAME_FRAME_DELAY;
850             else
851               GameFrameDelay = 500;
852           }
853           else
854             GameFrameDelay = (key - KSYM_0) * 10;
855           printf("Game speed == %d%% (%d ms delay between two frames)\n",
856                  GAME_FRAME_DELAY * 100 / GameFrameDelay, GameFrameDelay);
857           break;
858
859         case KSYM_d:
860           if (options.debug)
861           {
862             options.debug = FALSE;
863             printf("debug mode disabled\n");
864           }
865           else
866           {
867             options.debug = TRUE;
868             printf("debug mode enabled\n");
869           }
870           break;
871
872         case KSYM_S:
873           if (!global.fps_slowdown)
874           {
875             global.fps_slowdown = TRUE;
876             global.fps_slowdown_factor = 2;
877             printf("fps slowdown enabled -- display only every 2nd frame\n");
878           }
879           else if (global.fps_slowdown_factor == 2)
880           {
881             global.fps_slowdown_factor = 4;
882             printf("fps slowdown enabled -- display only every 4th frame\n");
883           }
884           else
885           {
886             global.fps_slowdown = FALSE;
887             global.fps_slowdown_factor = 1;
888             printf("fps slowdown disabled\n");
889           }
890           break;
891
892         case KSYM_f:
893           ScrollStepSize = TILEX/8;
894           printf("ScrollStepSize == %d (1/8)\n", ScrollStepSize);
895           break;
896
897         case KSYM_g:
898           ScrollStepSize = TILEX/4;
899           printf("ScrollStepSize == %d (1/4)\n", ScrollStepSize);
900           break;
901
902         case KSYM_h:
903           ScrollStepSize = TILEX/2;
904           printf("ScrollStepSize == %d (1/2)\n", ScrollStepSize);
905           break;
906
907         case KSYM_l:
908           ScrollStepSize = TILEX;
909           printf("ScrollStepSize == %d (1/1)\n", ScrollStepSize);
910           break;
911
912         case KSYM_v:
913           printf("::: currently using game engine version %d\n",
914                  game.engine_version);
915           break;
916 #endif
917
918         default:
919           break;
920       }
921       break;
922     }
923
924     default:
925       if (key == KSYM_Escape)
926       {
927         game_status = GAME_MODE_MAIN;
928         DrawMainMenu();
929
930         return;
931       }
932   }
933 }
934
935 void HandleNoEvent()
936 {
937   if (button_status && game_status != GAME_MODE_PLAYING)
938   {
939     HandleButton(0, 0, -button_status);
940     return;
941   }
942
943 #if defined(NETWORK_AVALIABLE)
944   if (options.network)
945     HandleNetworking();
946 #endif
947
948   HandleJoystick();
949 }
950
951 static int HandleJoystickForAllPlayers()
952 {
953   int i;
954   int result = 0;
955
956   for (i = 0; i < MAX_PLAYERS; i++)
957   {
958     byte joy_action = 0;
959
960     /*
961     if (!setup.input[i].use_joystick)
962       continue;
963       */
964
965     joy_action = Joystick(i);
966     result |= joy_action;
967
968     if (!setup.input[i].use_joystick)
969       continue;
970
971     stored_player[i].action = joy_action;
972   }
973
974   return result;
975 }
976
977 void HandleJoystick()
978 {
979   int joystick  = HandleJoystickForAllPlayers();
980   int keyboard  = key_joystick_mapping;
981   int joy       = (joystick | keyboard);
982   int left      = joy & JOY_LEFT;
983   int right     = joy & JOY_RIGHT;
984   int up        = joy & JOY_UP;
985   int down      = joy & JOY_DOWN;
986   int button    = joy & JOY_BUTTON;
987   int newbutton = (AnyJoystickButton() == JOY_BUTTON_NEW_PRESSED);
988   int dx        = (left ? -1    : right ? 1     : 0);
989   int dy        = (up   ? -1    : down  ? 1     : 0);
990
991   switch(game_status)
992   {
993     case GAME_MODE_MAIN:
994     case GAME_MODE_LEVELS:
995     case GAME_MODE_SETUP:
996     case GAME_MODE_INFO:
997     {
998       static unsigned long joystickmove_delay = 0;
999
1000       if (joystick && !button &&
1001           !DelayReached(&joystickmove_delay, GADGET_FRAME_DELAY))
1002         newbutton = dx = dy = 0;
1003
1004       if (game_status == GAME_MODE_MAIN)
1005         HandleMainMenu(0,0,dx,dy,newbutton ? MB_MENU_CHOICE : MB_MENU_MARK);
1006       else if (game_status == GAME_MODE_LEVELS)
1007         HandleChooseLevel(0,0,dx,dy,newbutton ? MB_MENU_CHOICE : MB_MENU_MARK);
1008       else if (game_status == GAME_MODE_SETUP)
1009         HandleSetupScreen(0,0,dx,dy,newbutton ? MB_MENU_CHOICE : MB_MENU_MARK);
1010       else if (game_status == GAME_MODE_INFO)
1011         HandleInfoScreen(0,0,dx,dy,newbutton ? MB_MENU_CHOICE : MB_MENU_MARK);
1012       break;
1013     }
1014
1015     case GAME_MODE_SCORES:
1016       HandleHallOfFame(0,0, dx,dy, !newbutton);
1017       break;
1018
1019     case GAME_MODE_EDITOR:
1020       HandleLevelEditorIdle();
1021       break;
1022
1023     case GAME_MODE_PLAYING:
1024       if (tape.playing || keyboard)
1025         newbutton = ((joy & JOY_BUTTON) != 0);
1026
1027       if (AllPlayersGone && newbutton)
1028       {
1029         CloseDoor(DOOR_CLOSE_1);
1030         game_status = GAME_MODE_MAIN;
1031         DrawMainMenu();
1032         return;
1033       }
1034
1035       break;
1036
1037     default:
1038       break;
1039   }
1040 }