fix includes
[presencevnc] / libvnc / client_examples / SDLvncviewer.c
1 #include <SDL.h>
2 #include <rfb/rfbclient.h>
3
4 struct { int sdl; int rfb; } buttonMapping[]={
5         {1, rfbButton1Mask},
6         {2, rfbButton2Mask},
7         {3, rfbButton3Mask},
8         {0,0}
9 };
10
11 static rfbBool resize(rfbClient* client) {
12         static char first=TRUE;
13 #ifdef SDL_ASYNCBLIT
14         int flags=SDL_HWSURFACE|SDL_ASYNCBLIT|SDL_HWACCEL;
15 #else
16         int flags=SDL_HWSURFACE|SDL_HWACCEL;
17 #endif
18         int width=client->width,height=client->height,
19                 depth=client->format.bitsPerPixel;
20         client->updateRect.x = client->updateRect.y = 0;
21         client->updateRect.w = width; client->updateRect.h = height;
22         rfbBool okay=SDL_VideoModeOK(width,height,depth,flags);
23         if(!okay)
24                 for(depth=24;!okay && depth>4;depth/=2)
25                         okay=SDL_VideoModeOK(width,height,depth,flags);
26         if(okay) {
27                 SDL_Surface* sdl=SDL_SetVideoMode(width,height,depth,flags);
28                 rfbClientSetClientData(client, SDL_Init, sdl);
29                 client->width = sdl->pitch / (depth / 8);
30                 client->frameBuffer=sdl->pixels;
31                 if(first || depth!=client->format.bitsPerPixel) {
32                         first=FALSE;
33                         client->format.bitsPerPixel=depth;
34                         client->format.redShift=sdl->format->Rshift;
35                         client->format.greenShift=sdl->format->Gshift;
36                         client->format.blueShift=sdl->format->Bshift;
37                         client->format.redMax=sdl->format->Rmask>>client->format.redShift;
38                         client->format.greenMax=sdl->format->Gmask>>client->format.greenShift;
39                         client->format.blueMax=sdl->format->Bmask>>client->format.blueShift;
40                         SetFormatAndEncodings(client);
41                 }
42         } else {
43                 SDL_Surface* sdl=rfbClientGetClientData(client, SDL_Init);
44                 rfbClientLog("Could not set resolution %dx%d!\n",
45                                 client->width,client->height);
46                 if(sdl) {
47                         client->width=sdl->pitch / (depth / 8);
48                         client->height=sdl->h;
49                 } else {
50                         client->width=0;
51                         client->height=0;
52                 }
53                 return FALSE;
54         }
55         SDL_WM_SetCaption(client->desktopName, "SDL");
56         return TRUE;
57 }
58
59 static rfbKeySym SDL_key2rfbKeySym(SDL_KeyboardEvent* e) {
60         rfbKeySym k = 0;
61         switch(e->keysym.sym) {
62         case SDLK_BACKSPACE: k = XK_BackSpace; break;
63         case SDLK_TAB: k = XK_Tab; break;
64         case SDLK_CLEAR: k = XK_Clear; break;
65         case SDLK_RETURN: k = XK_Return; break;
66         case SDLK_PAUSE: k = XK_Pause; break;
67         case SDLK_ESCAPE: k = XK_Escape; break;
68         case SDLK_SPACE: k = XK_space; break;
69         case SDLK_DELETE: k = XK_Delete; break;
70         case SDLK_KP0: k = XK_KP_0; break;
71         case SDLK_KP1: k = XK_KP_1; break;
72         case SDLK_KP2: k = XK_KP_2; break;
73         case SDLK_KP3: k = XK_KP_3; break;
74         case SDLK_KP4: k = XK_KP_4; break;
75         case SDLK_KP5: k = XK_KP_5; break;
76         case SDLK_KP6: k = XK_KP_6; break;
77         case SDLK_KP7: k = XK_KP_7; break;
78         case SDLK_KP8: k = XK_KP_8; break;
79         case SDLK_KP9: k = XK_KP_9; break;
80         case SDLK_KP_PERIOD: k = XK_KP_Decimal; break;
81         case SDLK_KP_DIVIDE: k = XK_KP_Divide; break;
82         case SDLK_KP_MULTIPLY: k = XK_KP_Multiply; break;
83         case SDLK_KP_MINUS: k = XK_KP_Subtract; break;
84         case SDLK_KP_PLUS: k = XK_KP_Add; break;
85         case SDLK_KP_ENTER: k = XK_KP_Enter; break;
86         case SDLK_KP_EQUALS: k = XK_KP_Equal; break;
87         case SDLK_UP: k = XK_Up; break;
88         case SDLK_DOWN: k = XK_Down; break;
89         case SDLK_RIGHT: k = XK_Right; break;
90         case SDLK_LEFT: k = XK_Left; break;
91         case SDLK_INSERT: k = XK_Insert; break;
92         case SDLK_HOME: k = XK_Home; break;
93         case SDLK_END: k = XK_End; break;
94         case SDLK_PAGEUP: k = XK_Page_Up; break;
95         case SDLK_PAGEDOWN: k = XK_Page_Down; break;
96         case SDLK_F1: k = XK_F1; break;
97         case SDLK_F2: k = XK_F2; break;
98         case SDLK_F3: k = XK_F3; break;
99         case SDLK_F4: k = XK_F4; break;
100         case SDLK_F5: k = XK_F5; break;
101         case SDLK_F6: k = XK_F6; break;
102         case SDLK_F7: k = XK_F7; break;
103         case SDLK_F8: k = XK_F8; break;
104         case SDLK_F9: k = XK_F9; break;
105         case SDLK_F10: k = XK_F10; break;
106         case SDLK_F11: k = XK_F11; break;
107         case SDLK_F12: k = XK_F12; break;
108         case SDLK_F13: k = XK_F13; break;
109         case SDLK_F14: k = XK_F14; break;
110         case SDLK_F15: k = XK_F15; break;
111         case SDLK_NUMLOCK: k = XK_Num_Lock; break;
112         case SDLK_CAPSLOCK: k = XK_Caps_Lock; break;
113         case SDLK_SCROLLOCK: k = XK_Scroll_Lock; break;
114         case SDLK_RSHIFT: k = XK_Shift_R; break;
115         case SDLK_LSHIFT: k = XK_Shift_L; break;
116         case SDLK_RCTRL: k = XK_Control_R; break;
117         case SDLK_LCTRL: k = XK_Control_L; break;
118         case SDLK_RALT: k = XK_Alt_R; break;
119         case SDLK_LALT: k = XK_Alt_L; break;
120         case SDLK_RMETA: k = XK_Meta_R; break;
121         case SDLK_LMETA: k = XK_Meta_L; break;
122 #if 0
123         /* TODO: find out keysyms */
124         case SDLK_LSUPER: k = XK_LSuper; break;         /* left "windows" key */
125         case SDLK_RSUPER: k = XK_RSuper; break;         /* right "windows" key */
126         case SDLK_COMPOSE: k = XK_Compose; break;
127 #endif
128         case SDLK_MODE: k = XK_Mode_switch; break;
129         case SDLK_HELP: k = XK_Help; break;
130         case SDLK_PRINT: k = XK_Print; break;
131         case SDLK_SYSREQ: k = XK_Sys_Req; break;
132         case SDLK_BREAK: k = XK_Break; break;
133         default: break;
134         }
135         if (k == 0 && e->keysym.sym >= SDLK_a && e->keysym.sym <= SDLK_z) {
136                 k = XK_a + e->keysym.sym - SDLK_a;
137                 if (e->keysym.mod & (KMOD_LSHIFT | KMOD_RSHIFT))
138                         k &= ~0x20;
139         }
140         if (k == 0) {
141                 if (e->keysym.unicode < 0x100)
142                         k = e->keysym.unicode;
143                 else
144                         rfbClientLog("Unknown keysym: %d\n",e->keysym.sym);
145         }
146
147         return k;
148 }
149
150 static void update(rfbClient* cl,int x,int y,int w,int h) {
151         SDL_UpdateRect(rfbClientGetClientData(cl, SDL_Init), x, y, w, h);
152 }
153
154 static void kbd_leds(rfbClient* cl, int value, int pad) {
155         /* note: pad is for future expansion 0=unused */
156         fprintf(stderr,"Led State= 0x%02X\n", value);
157         fflush(stderr);
158 }
159
160 /* trivial support for textchat */
161 static void text_chat(rfbClient* cl, int value, char *text) {
162         switch(value) {
163         case rfbTextChatOpen:
164                 fprintf(stderr,"TextChat: We should open a textchat window!\n");
165                 TextChatOpen(cl);
166                 break;
167         case rfbTextChatClose:
168                 fprintf(stderr,"TextChat: We should close our window!\n");
169                 break;
170         case rfbTextChatFinished:
171                 fprintf(stderr,"TextChat: We should close our window!\n");
172                 break;
173         default:
174                 fprintf(stderr,"TextChat: Received \"%s\"\n", text);
175                 break;
176         }
177         fflush(stderr);
178 }
179
180 #ifdef __MINGW32__
181 #define LOG_TO_FILE
182 #endif
183
184 #ifdef LOG_TO_FILE
185 #include <stdarg.h>
186 static void
187 log_to_file(const char *format, ...)
188 {
189     FILE* logfile;
190     static char* logfile_str=0;
191     va_list args;
192     char buf[256];
193     time_t log_clock;
194
195     if(!rfbEnableClientLogging)
196       return;
197
198     if(logfile_str==0) {
199         logfile_str=getenv("VNCLOG");
200         if(logfile_str==0)
201             logfile_str="vnc.log";
202     }
203
204     logfile=fopen(logfile_str,"a");
205
206     va_start(args, format);
207
208     time(&log_clock);
209     strftime(buf, 255, "%d/%m/%Y %X ", localtime(&log_clock));
210     fprintf(logfile,buf);
211
212     vfprintf(logfile, format, args);
213     fflush(logfile);
214
215     va_end(args);
216     fclose(logfile);
217 }
218 #endif
219
220 #ifdef mac
221 #define main SDLmain
222 #endif
223
224 int main(int argc,char** argv) {
225         rfbClient* cl;
226         int i, j, buttonMask = 0, viewOnly = 0;
227         SDL_Event e;
228
229 #ifdef LOG_TO_FILE
230         rfbClientLog=rfbClientErr=log_to_file;
231 #endif
232
233         for (i = 1, j = 1; i < argc; i++)
234                 if (!strcmp(argv[1], "-viewonly"))
235                         viewOnly = 1;
236                 else {
237                         if (i != j)
238                                 argv[j] = argv[i];
239                         j++;
240                 }
241         argc = j;
242
243         SDL_Init(SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE);
244         SDL_EnableUNICODE(1);
245
246         /* 16-bit: cl=rfbGetClient(5,3,2); */
247         cl=rfbGetClient(8,3,4);
248         cl->MallocFrameBuffer=resize;
249         cl->canHandleNewFBSize = TRUE;
250         cl->GotFrameBufferUpdate=update;
251         cl->HandleKeyboardLedState=kbd_leds;
252         cl->HandleTextChat=text_chat;
253         if(!rfbInitClient(cl,&argc,argv))
254                 return 1;
255
256         while(1) {
257                 if(SDL_PollEvent(&e))
258                         switch(e.type) {
259 #if SDL_MAJOR_VERSION>1 || SDL_MINOR_VERSION>=2
260                                 case SDL_VIDEOEXPOSE:
261                                         SendFramebufferUpdateRequest(cl,0,0,cl->width,cl->height,FALSE);
262                                         break;
263 #endif
264                                 case SDL_MOUSEBUTTONUP: case SDL_MOUSEBUTTONDOWN:
265                                 case SDL_MOUSEMOTION: {
266                                                 int x,y;
267                                                 if (viewOnly)
268                                                         break;
269                                                 int state=SDL_GetMouseState(&x,&y);
270                                                 int i;
271                                                 for(buttonMask=0,i=0;buttonMapping[i].sdl;i++)
272                                                         if(state&SDL_BUTTON(buttonMapping[i].sdl))
273                                                                 buttonMask|=buttonMapping[i].rfb;
274                                                 SendPointerEvent(cl,x,y,buttonMask);
275                                         }
276                                         break;
277                                 case SDL_KEYUP: case SDL_KEYDOWN:
278                                         if (viewOnly)
279                                                 break;
280                                         SendKeyEvent(cl,SDL_key2rfbKeySym(&e.key),(e.type==SDL_KEYDOWN)?TRUE:FALSE);
281                                         break;
282                                 case SDL_QUIT:
283                                         rfbClientCleanup(cl);
284                                         return 0;
285                                 case SDL_ACTIVEEVENT:
286                                         break;
287                                 default:
288                                         rfbClientLog("ignore SDL event: 0x%x\n",e.type);
289                         }
290                 else {
291                         i=WaitForMessage(cl,500);
292                         if(i<0)
293                                 return 0;
294                         if(i)
295                                 if(!HandleRFBServerMessage(cl))
296                                         return 0;
297                 }
298         }
299
300         return 0;
301 }
302