3 * This is an example of how to use libvncserver.
6 * Copyright (C) 2001 Johannes E. Schindelin <Johannes.Schindelin@gmx.de>
8 * This is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This software is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this software; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
35 #include <rfb/keysym.h>
37 static const int bpp=4;
38 static int maxx=800, maxy=600;
39 /* TODO: odd maxx doesn't work (vncviewer bug) */
41 /* This initializes a nice (?) background */
43 static void initBuffer(unsigned char* buffer)
48 buffer[(j*maxx+i)*bpp+0]=(i+j)*128/(maxx+maxy); /* red */
49 buffer[(j*maxx+i)*bpp+1]=i*128/maxx; /* green */
50 buffer[(j*maxx+i)*bpp+2]=j*256/maxy; /* blue */
52 buffer[j*maxx*bpp+0]=0xff;
53 buffer[j*maxx*bpp+1]=0xff;
54 buffer[j*maxx*bpp+2]=0xff;
55 buffer[j*maxx*bpp+3]=0xff;
59 /* Here we create a structure so that every client has it's own pointer */
61 typedef struct ClientData {
66 static void clientgone(rfbClientPtr cl)
71 static enum rfbNewClientAction newclient(rfbClientPtr cl)
73 cl->clientData = (void*)calloc(sizeof(ClientData),1);
74 cl->clientGoneHook = clientgone;
75 return RFB_CLIENT_ACCEPT;
78 /* switch to new framebuffer contents */
80 static void newframebuffer(rfbScreenInfoPtr screen, int width, int height)
82 unsigned char *oldfb, *newfb;
86 oldfb = (unsigned char*)screen->frameBuffer;
87 newfb = (unsigned char*)malloc(maxx * maxy * bpp);
89 rfbNewFramebuffer(screen, (char*)newfb, maxx, maxy, 8, 3, bpp);
92 /*** FIXME: Re-install cursor. ***/
95 /* aux function to draw a line */
97 static void drawline(unsigned char* buffer,int rowstride,int bpp,int x1,int y1,int x2,int y2)
103 buffer[y1*rowstride+x1*bpp+i]=0xff;
109 if(y1>y2) { i=y2; y2=y1; y1=i; i=x2; x2=x1; x1=i; }
112 buffer[j*rowstride+(x1+(j-y1)*(x2-x1)/(y2-y1))*bpp+i]=0xff;
114 if(x1>x2) { i=y2; y2=y1; y1=i; i=x2; x2=x1; x1=i; }
117 buffer[(y1+(i-x1)*(y2-y1)/(x2-x1))*rowstride+i*bpp+j]=0xff;
121 /* Here the pointer events are handled */
123 static void doptr(int buttonMask,int x,int y,rfbClientPtr cl)
125 ClientData* cd=cl->clientData;
127 if(x>=0 && y>=0 && x<maxx && y<maxy) {
131 if(cd->oldButton==buttonMask) { /* draw a line */
132 drawline((unsigned char*)cl->screen->frameBuffer,cl->screen->paddedWidthInBytes,bpp,
133 x,y,cd->oldx,cd->oldy);
135 if(x1>cd->oldx) x1++; else cd->oldx++;
136 if(y1>cd->oldy) y1++; else cd->oldy++;
137 rfbMarkRectAsModified(cl->screen,x,y,cd->oldx,cd->oldy);
138 } else { /* draw a point (diameter depends on button) */
139 int w=cl->screen->paddedWidthInBytes;
140 x1=x-buttonMask; if(x1<0) x1=0;
141 x2=x+buttonMask; if(x2>maxx) x2=maxx;
142 y1=y-buttonMask; if(y1<0) y1=0;
143 y2=y+buttonMask; if(y2>maxy) y2=maxy;
145 for(i=x1*bpp;i<x2*bpp;i++)
147 cl->screen->frameBuffer[j*w+i]=(char)0xff;
148 rfbMarkRectAsModified(cl->screen,x1,y1,x2,y2);
151 /* we could get a selection like that:
152 rfbGotXCutText(cl->screen,"Hallo",5);
157 cd->oldx=x; cd->oldy=y; cd->oldButton=buttonMask;
159 rfbDefaultPtrAddEvent(buttonMask,x,y,cl);
162 /* aux function to draw a character to x, y */
166 /* Here the key events are handled */
168 static void dokey(rfbBool down,rfbKeySym key,rfbClientPtr cl)
174 /* close down server, disconnecting clients */
175 rfbShutdownServer(cl->screen,TRUE);
177 /* close down server, but wait for all clients to disconnect */
178 rfbShutdownServer(cl->screen,FALSE);
179 else if(key==XK_Page_Up) {
180 initBuffer((unsigned char*)cl->screen->frameBuffer);
181 rfbMarkRectAsModified(cl->screen,0,0,maxx,maxy);
182 } else if (key == XK_Up) {
185 newframebuffer(cl->screen, 800, 600);
187 newframebuffer(cl->screen, 1024, 768);
190 } else if(key==XK_Down) {
193 newframebuffer(cl->screen, 800, 600);
195 newframebuffer(cl->screen, 640, 480);
198 } else if(key>=' ' && key<0x100) {
199 ClientData* cd=cl->clientData;
200 int x1=cd->oldx,y1=cd->oldy,x2,y2;
201 cd->oldx+=rfbDrawCharWithClip(cl->screen,&radonFont,cd->oldx,cd->oldy,(char)key,0,0,cl->screen->width,cl->screen->height,0x00ffffff,0x00ffffff);
202 rfbFontBBox(&radonFont,(char)key,&x1,&y1,&x2,&y2);
203 rfbMarkRectAsModified(cl->screen,x1,y1,x2-1,y2-1);
208 /* Example for an XCursor (foreground/background only) */
210 #ifdef JUST_AN_EXAMPLE
212 static int exampleXCursorWidth=9,exampleXCursorHeight=7;
213 static char exampleXCursor[]=
224 /* Example for a rich cursor (full-colour) */
226 static void MakeRichCursor(rfbScreenInfoPtr rfbScreen)
229 rfbCursorPtr c = rfbScreen->cursor;
233 " xxxxxxxxxxxxxxxxx "
234 " xxxxxxxxxxxxxxxxxxxxxx "
235 " xxxxx xxxxxxxx xxxxxxxx "
236 " xxxxxxxxxxxxxxxxxxxxxxxxxxx "
237 " xxxxxxxxxxxxxxxxxxxxxxxxxxxxx "
238 " xxxxx xxxxxxxxxxx xxxxxxx "
239 " xxxx xxxxxxxxx xxxxxx "
240 " xxxxx xxxxxxxxxxx xxxxxxx "
241 " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx "
242 " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx "
243 " xxxxxxxxxxxx xxxxxxxxxxxxxxx "
244 " xxxxxxxxxxxxxxxxxxxxxxxxxxxx "
245 " xxxxxxxxxxxxxxxxxxxxxxxxxxxx "
246 " xxxxxxxxxxx xxxxxxxxxxxxxx "
247 " xxxxxxxxxx xxxxxxxxxxxx "
248 " xxxxxxxxx xxxxxxxxx "
249 " xxxxxxxxxx xxxxxxxxx "
250 " xxxxxxxxxxxxxxxxxxx "
251 " xxxxxxxxxxxxxxxxxxx "
252 " xxxxxxxxxxxxxxxxxxx "
253 " xxxxxxxxxxxxxxxxx "
255 " xxxx xxxxxxxxxxxxx "
259 " xxxxxx xxxxxxxxxxxx "
260 " xxxxxxxxxxxxxxxxxxxxxx "
263 c=rfbScreen->cursor = rfbMakeXCursor(w,h,bitmap,bitmap);
264 c->xhot = 16; c->yhot = 24;
266 c->richSource = (char*)malloc(w*h*bpp);
267 c->cleanupRichSource = TRUE;
270 c->richSource[j*w*bpp+i*bpp+0]=i*0xff/w;
271 c->richSource[j*w*bpp+i*bpp+1]=(i+j)*0xff/(w+h);
272 c->richSource[j*w*bpp+i*bpp+2]=j*0xff/h;
273 c->richSource[j*w*bpp+i*bpp+3]=0;
280 int main(int argc,char** argv)
282 rfbScreenInfoPtr rfbScreen =
283 rfbGetScreen(&argc,argv,maxx,maxy,8,3,bpp);
284 rfbScreen->desktopName = "LibVNCServer Example";
285 rfbScreen->frameBuffer = (char*)malloc(maxx*maxy*bpp);
286 rfbScreen->alwaysShared = TRUE;
287 rfbScreen->ptrAddEvent = doptr;
288 rfbScreen->kbdAddEvent = dokey;
289 rfbScreen->newClientHook = newclient;
290 rfbScreen->httpDir = "../classes";
291 rfbScreen->httpEnableProxyConnect = TRUE;
293 initBuffer((unsigned char*)rfbScreen->frameBuffer);
294 rfbDrawString(rfbScreen,&radonFont,20,100,"Hello, World!",0xffffff);
296 /* This call creates a mask and then a cursor: */
297 /* rfbScreen->defaultCursor =
298 rfbMakeXCursor(exampleCursorWidth,exampleCursorHeight,exampleCursor,0);
301 MakeRichCursor(rfbScreen);
303 /* initialize the server */
304 rfbInitServer(rfbScreen);
306 #ifndef BACKGROUND_LOOP_TEST
310 for(i=0;rfbIsActive(rfbScreen);i++) {
311 fprintf(stderr,"%d\r",i);
312 rfbProcessEvents(rfbScreen,100000);
316 /* this is the blocking event loop, i.e. it never returns */
317 /* 40000 are the microseconds to wait on select(), i.e. 0.04 seconds */
318 rfbRunEventLoop(rfbScreen,40000,FALSE);
319 #endif /* OWN LOOP */
321 #if !defined(LIBVNCSERVER_HAVE_LIBPTHREAD)
322 #error "I need pthreads for that."
325 /* this is the non-blocking event loop; a background thread is started */
326 rfbRunEventLoop(rfbScreen,-1,TRUE);
327 fprintf(stderr, "Running background loop...\n");
328 /* now we could do some cool things like rendering in idle time */
329 while(1) sleep(5); /* render(); */
330 #endif /* BACKGROUND_LOOP */
332 free(rfbScreen->frameBuffer);
333 rfbScreenCleanup(rfbScreen);