X-Git-Url: https://git.artsoft.org/?a=blobdiff_plain;f=src%2Fevents.c;h=dc398df7fe5870ab51767fb52292154ff615a649;hb=1146572677fd6858df5fc1fabfd7f7294bdefed7;hp=f1cd50dafaa7dca21d6b7c4583d94300c0540e78;hpb=b2a0ff1ddd4430110b331129469dabb8ea7b6ba7;p=rocksndiamonds.git diff --git a/src/events.c b/src/events.c index f1cd50da..dc398df7 100644 --- a/src/events.c +++ b/src/events.c @@ -20,6 +20,7 @@ #include "misc.h" #include "tape.h" #include "joystick.h" +#include "network.h" void EventLoop(void) { @@ -33,34 +34,40 @@ void EventLoop(void) switch(event.type) { - case Expose: - HandleExposeEvent((XExposeEvent *) &event); - break; - case UnmapNotify: - SleepWhileUnmapped(); - break; case ButtonPress: case ButtonRelease: HandleButtonEvent((XButtonEvent *) &event); break; + case MotionNotify: HandleMotionEvent((XMotionEvent *) &event); break; + case KeyPress: case KeyRelease: HandleKeyEvent((XKeyEvent *) &event); break; - case FocusIn: - case FocusOut: - HandleFocusEvent((XFocusChangeEvent *) &event); - break; - case ClientMessage: - HandleClientMessageEvent((XClientMessageEvent *) &event); - break; + default: + HandleOtherEvents(&event); break; } } + + HandleNoXEvent(); + + /* don't use all CPU time when idle; the main loop while playing + has its own synchronization and is CPU friendly, too */ + + if (game_status != PLAYING) + { + XSync(display, FALSE); + Delay(10); + } + + + +#if 0 else /* got no event, but don't be lazy... */ { HandleNoXEvent(); @@ -74,12 +81,41 @@ void EventLoop(void) Delay(10); } } +#endif + + if (game_status == EXITGAME) return; } } +void HandleOtherEvents(XEvent *event) +{ + switch(event->type) + { + case Expose: + HandleExposeEvent((XExposeEvent *) event); + break; + + case UnmapNotify: + SleepWhileUnmapped(); + break; + + case FocusIn: + case FocusOut: + HandleFocusEvent((XFocusChangeEvent *) event); + break; + + case ClientMessage: + HandleClientMessageEvent((XClientMessageEvent *) event); + break; + + default: + break; + } +} + void ClearEventQueue() { while(XPending(display)) @@ -90,26 +126,16 @@ void ClearEventQueue() switch(event.type) { - case Expose: - HandleExposeEvent((XExposeEvent *) &event); - break; - case UnmapNotify: - SleepWhileUnmapped(); - break; case ButtonRelease: button_status = MB_RELEASED; break; + case KeyRelease: key_joystick_mapping = 0; break; - case FocusIn: - case FocusOut: - HandleFocusEvent((XFocusChangeEvent *) &event); - break; - case ClientMessage: - HandleClientMessageEvent((XClientMessageEvent *) &event); - break; + default: + HandleOtherEvents(&event); break; } } @@ -117,7 +143,7 @@ void ClearEventQueue() void SleepWhileUnmapped() { - BOOL window_unmapped = TRUE; + boolean window_unmapped = TRUE; XAutoRepeatOn(display); @@ -129,22 +155,27 @@ void SleepWhileUnmapped() switch(event.type) { - case Expose: - HandleExposeEvent((XExposeEvent *) &event); - break; case ButtonRelease: button_status = MB_RELEASED; break; + case KeyRelease: key_joystick_mapping = 0; break; + case MapNotify: window_unmapped = FALSE; break; - case ClientMessage: - HandleClientMessageEvent((XClientMessageEvent *) &event); + + case UnmapNotify: + /* this is only to surely prevent the 'should not happen' case + * of recursively looping between 'SleepWhileUnmapped()' and + * 'HandleOtherEvents()' which usually calls this funtion. + */ break; + default: + HandleOtherEvents(&event); break; } } @@ -158,7 +189,7 @@ void HandleExposeEvent(XExposeEvent *event) int x = event->x, y = event->y; int width = event->width, height = event->height; - if (direct_draw_on && game_status==PLAYING) + if (setup.direct_draw_on && game_status==PLAYING) { int xx,yy; int x1 = (x-SX)/TILEX, y1 = (y-SY)/TILEY; @@ -175,7 +206,7 @@ void HandleExposeEvent(XExposeEvent *event) SetDrawtoField(DRAW_DIRECT); } - if (soft_scrolling_on && game_status == PLAYING) + if (setup.soft_scrolling_on && game_status == PLAYING) { int fx = FX, fy = FY; @@ -244,7 +275,7 @@ void HandleClientMessageEvent(XClientMessageEvent *event) { if ((event->window == window) && (event->data.l[0] == XInternAtom(display, "WM_DELETE_WINDOW", FALSE))) - CloseAll(); + CloseAllAndExit(0); } void HandleButton(int mx, int my, int button) @@ -294,27 +325,45 @@ void HandleButton(int mx, int my, int button) case MAINMENU: HandleMainMenu(mx,my,0,0,button); break; + case TYPENAME: HandleTypeName(0,XK_Return); break; + case CHOOSELEVEL: HandleChooseLevel(mx,my,0,0,button); break; + case HALLOFFAME: HandleHallOfFame(button); break; + case LEVELED: LevelEd(mx,my,button); break; + case HELPSCREEN: HandleHelpScreen(button); break; + case SETUP: HandleSetupScreen(mx,my,0,0,button); break; + + case SETUPINPUT: + HandleSetupInputScreen(mx,my,0,0,button); + break; + case PLAYING: + + /* --> NoXEvent() will follow */ + + /* HandleGameActions(0); + */ + break; + default: break; } @@ -323,6 +372,68 @@ void HandleButton(int mx, int my, int button) void HandleKey(KeySym key, int key_status) { int joy = 0; + static struct SetupKeyboardInfo custom_key; + static struct + { + KeySym *keysym_custom; + KeySym keysym_default; + byte action; + } key_info[] = + { + { &custom_key.left, DEFAULT_KEY_LEFT, JOY_LEFT }, + { &custom_key.right, DEFAULT_KEY_RIGHT, JOY_RIGHT }, + { &custom_key.up, DEFAULT_KEY_UP, JOY_UP }, + { &custom_key.down, DEFAULT_KEY_DOWN, JOY_DOWN }, + { &custom_key.snap, DEFAULT_KEY_SNAP, JOY_BUTTON_1 }, + { &custom_key.bomb, DEFAULT_KEY_BOMB, JOY_BUTTON_2 } + }; + + if (game_status == PLAYING) + { + int pnr; + + for (pnr=0; pnraction |= key_action; + else + stored_player[pnr].action |= key_action; + } + else + { + if (network_playing) + local_player->action &= ~key_action; + else + stored_player[pnr].action &= ~key_action; + } + } + } + else + { + int i; + + for (i=0; i<6; i++) + if (key == key_info[i].keysym_default) + joy |= key_info[i].action; + } + + +#if 0 + /* Map cursor keys to joystick directions */ @@ -339,6 +450,7 @@ void HandleKey(KeySym key, int key_status) case XK_j: joy |= JOY_LEFT; break; + case XK_Right: #ifdef XK_KP_Right case XK_KP_Right: @@ -350,6 +462,7 @@ void HandleKey(KeySym key, int key_status) case XK_k: joy |= JOY_RIGHT; break; + case XK_Up: #ifdef XK_KP_Up case XK_KP_Up: @@ -361,6 +474,7 @@ void HandleKey(KeySym key, int key_status) case XK_i: joy |= JOY_UP; break; + case XK_Down: #ifdef XK_KP_Down case XK_KP_Down: @@ -372,54 +486,63 @@ void HandleKey(KeySym key, int key_status) case XK_m: joy |= JOY_DOWN; break; + #ifdef XK_KP_Home case XK_KP_Home: /* Diagonalrichtungen */ #endif case XK_KP_7: joy |= JOY_UP | JOY_LEFT; break; + #ifdef XK_KP_Page_Up case XK_KP_Page_Up: #endif case XK_KP_9: joy = JOY_UP | JOY_RIGHT; break; + #ifdef XK_KP_End case XK_KP_End: #endif case XK_KP_1: joy |= JOY_DOWN | JOY_LEFT; break; + #ifdef XK_KP_Page_Down case XK_KP_Page_Down: #endif case XK_KP_3: joy |= JOY_DOWN | JOY_RIGHT; break; + #ifndef MSDOS case XK_S: /* Feld entfernen */ #endif case XK_s: joy |= JOY_BUTTON_1 | JOY_LEFT; break; + #ifndef MSDOS case XK_D: #endif case XK_d: joy |= JOY_BUTTON_1 | JOY_RIGHT; break; + #ifndef MSDOS case XK_E: #endif case XK_e: joy |= JOY_BUTTON_1 | JOY_UP; break; + #ifndef MSDOS case XK_X: #endif case XK_x: joy |= JOY_BUTTON_1 | JOY_DOWN; break; + case XK_Shift_L: /* Linker Feuerknopf */ #ifndef MSDOS case XK_Control_L: @@ -428,6 +551,7 @@ void HandleKey(KeySym key, int key_status) #endif joy |= JOY_BUTTON_1; break; + case XK_Shift_R: /* Rechter Feuerknopf */ #ifndef MSDOS case XK_Control_R: @@ -440,10 +564,15 @@ void HandleKey(KeySym key, int key_status) case XK_b: joy |= JOY_BUTTON_2; break; + default: break; } + +#endif + + if (joy) { if (key_status == KEY_PRESSED) @@ -484,10 +613,11 @@ void HandleKey(KeySym key, int key_status) case TYPENAME: HandleTypeName(0,key); break; + case MAINMENU: case CHOOSELEVEL: case SETUP: - { + case SETUPINPUT: switch(key) { case XK_Return: @@ -497,15 +627,19 @@ void HandleKey(KeySym key, int key_status) HandleChooseLevel(0,0,0,0,MB_MENU_CHOICE); else if (game_status==SETUP) HandleSetupScreen(0,0,0,0,MB_MENU_CHOICE); + else if (game_status==SETUPINPUT) + HandleSetupInputScreen(0,0,0,0,MB_MENU_CHOICE); break; + default: break; } break; - } + case HELPSCREEN: HandleHelpScreen(MB_RELEASED); break; + case HALLOFFAME: switch(key) { @@ -514,13 +648,16 @@ void HandleKey(KeySym key, int key_status) DrawMainMenu(); BackToFront(); break; + default: break; } break; + case LEVELED: LevelNameTyping(key); break; + case PLAYING: { switch(key) @@ -545,6 +682,8 @@ void HandleKey(KeySym key, int key_status) GAME_FRAME_DELAY * 100 / GameFrameDelay, GameFrameDelay); break; + +#if 0 case XK_a: if (ScrollStepSize == TILEX/8) ScrollStepSize = TILEX/4; @@ -552,19 +691,23 @@ void HandleKey(KeySym key, int key_status) ScrollStepSize = TILEX/8; printf("ScrollStepSize == %d\n", ScrollStepSize); break; +#endif case XK_f: ScrollStepSize = TILEX/8; printf("ScrollStepSize == %d (1/8)\n", ScrollStepSize); break; + case XK_g: ScrollStepSize = TILEX/4; printf("ScrollStepSize == %d (1/4)\n", ScrollStepSize); break; + case XK_h: ScrollStepSize = TILEX/2; printf("ScrollStepSize == %d (1/2)\n", ScrollStepSize); break; + case XK_l: ScrollStepSize = TILEX; printf("ScrollStepSize == %d (1/1)\n", ScrollStepSize); @@ -632,6 +775,8 @@ void HandleKey(KeySym key, int key_status) break; + +#if 0 case XK_y: /* { @@ -647,9 +792,11 @@ void HandleKey(KeySym key, int key_status) } */ - printf("direct_draw_on == %d\n", direct_draw_on); + printf("direct_draw_on == %d\n", setup.direct_draw_on); break; +#endif + case XK_z: { @@ -693,199 +840,6 @@ void HandleKey(KeySym key, int key_status) } } - - -/* TEST STUFF -------------------------------------------------------------- */ - -#define PROT_VERS_1 1 -#define PROT_VERS_2 0 -#define PROT_VERS_3 1 - -#define OP_NICK 1 -#define OP_PLAY 2 -#define OP_FALL 3 -#define OP_DRAW 4 -#define OP_LOST 5 -#define OP_GONE 6 -#define OP_CLEAR 7 -#define OP_NEW 8 -#define OP_LINES 9 -#define OP_GROW 10 -#define OP_MODE 11 -#define OP_LEVEL 12 -#define OP_BOT 13 -#define OP_KILL 14 -#define OP_PAUSE 15 -#define OP_CONT 16 -#define OP_VERSION 17 -#define OP_BADVERS 18 -#define OP_MSG 19 -#define OP_YOUARE 20 -#define OP_LINESTO 21 -#define OP_WON 22 -#define OP_ZERO 23 - -/* server stuff */ - -#define BUFLEN 4096 - -extern int sfd; -extern unsigned char realbuf[], readbuf[], writbuf[]; -extern unsigned char *buf; -extern int nread, nwrite; - -extern void fatal(char *); -extern void flushbuf(void); -extern void sysmsg(char *); - -/* like memcpy, but guaranteed to handle overlap when s <= t */ -void copydown(char *s, char *t, int n) -{ - for (; n; n--) - *(s++) = *(t++); -} - -void handlemessages() -{ - unsigned int len; - static char msgbuf[300]; - - while (nread >= 4 && nread >= 4 + readbuf[3]) - { - len = readbuf[3]; - if (readbuf[0] || readbuf[1] || readbuf[2]) - fatal("Wrong server line length"); - - memcpy(buf, &readbuf[4], len); - nread -= 4 + len; - copydown(readbuf, readbuf + 4 + len, nread); - - switch(buf[1]) - { - case OP_YOUARE: - printf("OP_YOUARE: %d\n", buf[0]); - break; - - case OP_NEW: - printf("OP_NEW: %d\n", buf[0]); - sprintf(msgbuf, "new client %d connected", buf[0]); - sysmsg(msgbuf); - break; - - case OP_GONE: - printf("OP_GONE: %d\n", buf[0]); - sprintf(msgbuf, "client %d disconnected", buf[0]); - sysmsg(msgbuf); - break; - - case OP_BADVERS: - { - static char tmpbuf[128]; - - sprintf(tmpbuf, "Protocol version mismatch: server expects %d.%d.x instead of %d.%d.%d\n", buf[2], buf[3], PROT_VERS_1, PROT_VERS_2, PROT_VERS_3); - fatal(tmpbuf); - } - break; - - case OP_PLAY: - printf("OP_PLAY: %d\n", buf[0]); - sprintf(msgbuf, "client %d starts game", buf[0]); - sysmsg(msgbuf); - break; - - case OP_PAUSE: - printf("OP_PAUSE: %d\n", buf[0]); - sprintf(msgbuf, "client %d pauses game", buf[0]); - sysmsg(msgbuf); - break; - - case OP_CONT: - printf("OP_CONT: %d\n", buf[0]); - sprintf(msgbuf, "client %d continues game", buf[0]); - sysmsg(msgbuf); - break; - - case OP_WON: - printf("OP_WON: %d\n", buf[0]); - sprintf(msgbuf, "client %d wins the game", buf[0]); - sysmsg(msgbuf); - break; - - case OP_ZERO: - printf("OP_ZERO: %d\n", buf[0]); - sprintf(msgbuf, "client %d resets game counters", buf[0]); - sysmsg(msgbuf); - break; - - case OP_NICK: - printf("OP_NICK: %d\n", buf[0]); - sprintf(msgbuf, "client %d calls itself %s", buf[0], &buf[2]); - sysmsg(msgbuf); - break; - - case OP_MSG: - printf("OP_MSG: %d\n", buf[0]); - sprintf(msgbuf, "client %d sends message", buf[0]); - break; - - case OP_LOST: - printf("OP_MSG: %d\n", buf[0]); - sprintf(msgbuf, "client %d has lost", buf[0]); - break; - - case OP_LEVEL: - printf("OP_MSG: %d\n", buf[0]); - sprintf(msgbuf, "client %d sets level to %d", buf[0], buf[2]); - break; - } - } - - fflush(stdout); -} - - - -static void HandleNetworking() -{ - static struct timeval tv = { 0, 0 }; - fd_set rfds; - int r = 0; - - if (standalone) - return; - - flushbuf(); - - FD_ZERO(&rfds); - FD_SET(sfd, &rfds); - - r = select(sfd + 1, &rfds, NULL, NULL, &tv); - - if (r < 0 && errno != EINTR) - { - perror("select"); - fatal("fatal: select() failed"); - } - - if (r < 0) - FD_ZERO(&rfds); - - if (FD_ISSET(sfd, &rfds)) - { - int r; - - r = read(sfd, readbuf + nread, BUFLEN - nread); - - if (r < 0) - fatal("Error reading from server"); - if (r == 0) - fatal("Connection to server lost"); - nread += r; - - handlemessages(); - } -} - void HandleNoXEvent() { if (button_status && game_status != PLAYING) @@ -894,7 +848,8 @@ void HandleNoXEvent() return; } - HandleNetworking(); + if (options.network) + HandleNetworking(); switch(game_status) { @@ -903,8 +858,10 @@ void HandleNoXEvent() case HALLOFFAME: case HELPSCREEN: case SETUP: + case SETUPINPUT: HandleJoystick(); break; + case PLAYING: HandleJoystick(); @@ -913,6 +870,7 @@ void HandleNoXEvent() */ break; + default: break; } @@ -922,11 +880,6 @@ void HandleJoystick() { int joystick = Joystick(); int keyboard = key_joystick_mapping; - - /* - int joy = (tape.playing ? TapePlayAction() : (joystick | keyboard)); - */ - int joy = (joystick | keyboard); int left = joy & JOY_LEFT; int right = joy & JOY_RIGHT; @@ -942,10 +895,11 @@ void HandleJoystick() case MAINMENU: case CHOOSELEVEL: case SETUP: + case SETUPINPUT: { static long joystickmove_delay = 0; - if (joystick && !button && !DelayReached(&joystickmove_delay,15)) + if (joystick && !button && !DelayReached(&joystickmove_delay,150)) newbutton = dx = dy = 0; if (game_status==MAINMENU) @@ -954,6 +908,9 @@ void HandleJoystick() HandleChooseLevel(0,0,dx,dy,newbutton ? MB_MENU_CHOICE : MB_MENU_MARK); else if (game_status==SETUP) HandleSetupScreen(0,0,dx,dy,newbutton ? MB_MENU_CHOICE : MB_MENU_MARK); + else if (game_status==SETUPINPUT) + HandleSetupInputScreen(0,0,dx,dy, + newbutton ? MB_MENU_CHOICE : MB_MENU_MARK); break; } @@ -977,10 +934,17 @@ void HandleJoystick() return; } + /* if (tape.pausing || AllPlayersGone) - joy = 0; + { + int i; + + for (i=0; i