From e12c4ccf235e1760364c9a9930a5f1f56c4c7098 Mon Sep 17 00:00:00 2001 From: dscho Date: Sat, 7 Mar 2009 19:18:55 +0000 Subject: SDLvncviewer: refactor event handling Instead of having deep indent levels, put the code to handle events into its own function. That also helps readability. Signed-off-by: Johannes Schindelin --- client_examples/SDLvncviewer.c | 93 +++++++++++++++++++++++------------------- 1 file changed, 50 insertions(+), 43 deletions(-) (limited to 'client_examples/SDLvncviewer.c') diff --git a/client_examples/SDLvncviewer.c b/client_examples/SDLvncviewer.c index 2f011ee..738e9c6 100644 --- a/client_examples/SDLvncviewer.c +++ b/client_examples/SDLvncviewer.c @@ -8,7 +8,7 @@ struct { int sdl; int rfb; } buttonMapping[]={ {0,0} }; -static int enableResizable; +static int enableResizable, viewOnly, buttonMask; #ifdef SDL_ASYNCBLIT int sdlFlags = SDL_HWSURFACE | SDL_ASYNCBLIT | SDL_HWACCEL; #else @@ -343,13 +343,60 @@ log_to_file(const char *format, ...) } #endif +static void handleSDLEvent(rfbClient *cl, SDL_Event *e) +{ + switch(e->type) { +#if SDL_MAJOR_VERSION > 1 || SDL_MINOR_VERSION >= 2 + case SDL_VIDEOEXPOSE: + SendFramebufferUpdateRequest(cl, 0, 0, + cl->width, cl->height, FALSE); + break; +#endif + case SDL_MOUSEBUTTONUP: + case SDL_MOUSEBUTTONDOWN: + case SDL_MOUSEMOTION: { + int x, y, state, i; + if (viewOnly) + break; + + state = SDL_GetMouseState(&x, &y); + if (sdlPixels) { + x = x * cl->width / realWidth; + y = y * cl->height / realHeight; + } + for (buttonMask = 0, i = 0; buttonMapping[i].sdl; i++) + if (state & SDL_BUTTON(buttonMapping[i].sdl)) + buttonMask |= buttonMapping[i].rfb; + SendPointerEvent(cl, x, y, buttonMask); + break; + } + case SDL_KEYUP: + case SDL_KEYDOWN: + if (viewOnly) + break; + SendKeyEvent(cl, SDL_key2rfbKeySym(&e->key), + e->type == SDL_KEYDOWN ? TRUE : FALSE); + break; + case SDL_QUIT: + rfbClientCleanup(cl); + exit(0); + case SDL_ACTIVEEVENT: + break; + case SDL_VIDEORESIZE: + setRealDimension(cl, e->resize.w, e->resize.h); + break; + default: + rfbClientLog("ignore SDL event: 0x%x\n", e->type); + } +} + #ifdef mac #define main SDLmain #endif int main(int argc,char** argv) { rfbClient* cl; - int i, j, buttonMask = 0, viewOnly = 0; + int i, j; SDL_Event e; #ifdef LOG_TO_FILE @@ -385,47 +432,7 @@ int main(int argc,char** argv) { while(1) { if(SDL_PollEvent(&e)) - switch(e.type) { -#if SDL_MAJOR_VERSION>1 || SDL_MINOR_VERSION>=2 - case SDL_VIDEOEXPOSE: - SendFramebufferUpdateRequest(cl,0,0,cl->width,cl->height,FALSE); - break; -#endif - case SDL_MOUSEBUTTONUP: case SDL_MOUSEBUTTONDOWN: - case SDL_MOUSEMOTION: { - int x,y; - if (viewOnly) - break; - int state=SDL_GetMouseState(&x,&y); - int i; - - if (sdlPixels) { - x = x * cl->width / realWidth; - y = y * cl->height / realHeight; - } - for(buttonMask=0,i=0;buttonMapping[i].sdl;i++) - if(state&SDL_BUTTON(buttonMapping[i].sdl)) - buttonMask|=buttonMapping[i].rfb; - SendPointerEvent(cl,x,y,buttonMask); - } - break; - case SDL_KEYUP: case SDL_KEYDOWN: - if (viewOnly) - break; - SendKeyEvent(cl,SDL_key2rfbKeySym(&e.key),(e.type==SDL_KEYDOWN)?TRUE:FALSE); - break; - case SDL_QUIT: - rfbClientCleanup(cl); - return 0; - case SDL_ACTIVEEVENT: - break; - case SDL_VIDEORESIZE: - setRealDimension(cl, - e.resize.w, e.resize.h); - break; - default: - rfbClientLog("ignore SDL event: 0x%x\n",e.type); - } + handleSDLEvent(cl, &e); else { i=WaitForMessage(cl,500); if(i<0) -- cgit v1.1