1#include <stdio.h>
2#include <stdlib.h>
3#include <rfb/rfb.h>
4#include <rfb/keysym.h>
5#include <rfb/default8x16.h>
6
7static int maxx=400, maxy=400, bpp=4;
8/* odd maxx doesn't work (vncviewer bug) */
9
10/* Here we create a structure so that every client has it's own pointer */
11
12/* turns the framebuffer black */
13void blank_framebuffer(char* frame_buffer, int x1, int y1, int x2, int y2);
14/* displays a red bar, a green bar, and a blue bar */
15void draw_primary_colors (char* frame_buffer, int x1, int y1, int x2, int y2);
16void draw_primary_colours_generic(rfbScreenInfoPtr s,int x1,int y1,int x2,int y2);
17void draw_primary_colours_generic_fast(rfbScreenInfoPtr s,int x1,int y1,int x2,int y2);
18void linecount (char* frame_buffer);
19/* handles mouse events */
20void on_mouse_event (int buttonMask,int x,int y,rfbClientPtr cl);
21/* handles keyboard events */
22void on_key_press (rfbBool down,rfbKeySym key,rfbClientPtr cl);
23
24int main (int argc, char **argv)
25{
26	rfbScreenInfoPtr server;
27
28	if(!rfbProcessSizeArguments(&maxx,&maxy,&bpp,&argc,argv))
29	  return 1;
30
31        server = rfbGetScreen (&argc, argv, maxx, maxy, 8, 3, bpp);
32        if(!server)
33          return 0;
34	server->desktopName = "Zippy das wundersquirrel\'s VNC server";
35	server->frameBuffer = (char*)malloc(maxx*maxy*bpp);
36	server->alwaysShared = TRUE;
37        server->kbdAddEvent = on_key_press;
38	server->ptrAddEvent = on_mouse_event;
39
40	rfbInitServer (server);
41
42	blank_framebuffer(server->frameBuffer, 0, 0, maxx, maxy);
43	rfbRunEventLoop (server, -1, FALSE);
44	free(server->frameBuffer);
45	rfbScreenCleanup (server);
46	return 0;
47}
48
49void blank_framebuffer(char* frame_buffer, int x1, int y1, int x2, int y2)
50{
51	int i;
52        for (i=0; i < maxx * maxy * bpp; i++) frame_buffer[i]=(char) 0;
53}
54
55void draw_primary_colors (char* frame_buffer, int x1, int y1, int x2, int y2)
56{
57        int i, j, current_pixel;
58        for (i=y1; i < y2; i++){
59                for (j=x1; j < x2; j++) {
60                        current_pixel = (i*x2 + j) * bpp;
61                        if (i < y2 ) {
62                                frame_buffer[current_pixel+0] = (char) 128;
63                                frame_buffer[current_pixel+1] = (char) 0;
64                                frame_buffer[current_pixel+2] = (char) 0;
65                        }
66                        if (i < y2/3*2) {
67                                frame_buffer[current_pixel+0] = (char) 0;
68                                frame_buffer[current_pixel+1] = (char) 128;
69                                frame_buffer[current_pixel+2] = (char) 0;
70                        }
71                        if (i < y2/3) {
72                                frame_buffer[current_pixel+0] = (char) 0;
73                                frame_buffer[current_pixel+1] = (char) 0;
74                                frame_buffer[current_pixel+2] = (char) 128;
75                        }
76                }
77        }
78 }
79
80/* Dscho's versions (slower, but works for bpp != 3 or 4) */
81void draw_primary_colours_generic(rfbScreenInfoPtr s,int x1,int y1,int x2,int y2)
82{
83  rfbPixelFormat f=s->serverFormat;
84  int i,j;
85  for(j=y1;j<y2;j++)
86    for(i=x1;i<x2;i++)
87      if(j<y1*2/3+y2/3)
88	rfbDrawPixel(s,i,j,f.redMax<<f.redShift);
89      else if(j<y1/3+y2*2/3)
90	rfbDrawPixel(s,i,j,f.greenMax<<f.greenShift);
91      else
92	rfbDrawPixel(s,i,j,f.blueMax<<f.blueShift);
93}
94
95void draw_primary_colours_generic_fast(rfbScreenInfoPtr s,int x1,int y1,int x2,int y2)
96{
97  rfbPixelFormat f=s->serverFormat;
98  int i,j,y3=(y1*2+y2)/3,y4=(y1+y2*2)/3;
99  /* draw first pixel */
100  rfbDrawPixel(s,x1,y1,f.redMax<<f.redShift);
101  rfbDrawPixel(s,x1,y3,f.greenMax<<f.greenShift);
102  rfbDrawPixel(s,x1,y4,f.blueMax<<f.blueShift);
103  /* then copy stripes */
104  for(j=0;j<y2-y4;j++)
105    for(i=x1;i<x2;i++) {
106#define ADDR(x,y) s->frameBuffer+(x)*bpp+(y)*s->paddedWidthInBytes
107      memcpy(ADDR(i,j+y1),ADDR(x1,y1),bpp);
108      memcpy(ADDR(i,j+y3),ADDR(x1,y3),bpp);
109      memcpy(ADDR(i,j+y4),ADDR(x1,y4),bpp);
110    }
111}
112
113static void draw_primary_colours_generic_ultrafast(rfbScreenInfoPtr s,int x1,int y1,int x2,int y2)
114{
115  rfbPixelFormat f=s->serverFormat;
116  int y3=(y1*2+y2)/3,y4=(y1+y2*2)/3;
117  /* fill rectangles */
118  rfbFillRect(s,x1,y1,x2,y3,f.redMax<<f.redShift);
119  rfbFillRect(s,x1,y3,x2,y4,f.greenMax<<f.greenShift);
120  rfbFillRect(s,x1,y4,x2,y2,f.blueMax<<f.blueShift);
121}
122
123void linecount (char* frame_buffer)
124{
125        int i,j,k, current_pixel;
126        for (i=maxy-4; i>maxy-20; i-=4)
127                for (j=0; j<4; j++) for (k=0; k < maxx; k++) {
128                        current_pixel = (i*j*maxx + k) * bpp;
129                        if (i%2 == 0) {
130                                frame_buffer[current_pixel+0] = (char) 0;
131                                frame_buffer[current_pixel+1] = (char) 0;
132                                frame_buffer[current_pixel+2] = (char) 128;
133                        }
134
135                        if (i%2 == 1) {
136                                frame_buffer[current_pixel+0] = (char) 128;
137                                frame_buffer[current_pixel+1] = (char) 0;
138                                frame_buffer[current_pixel+2] = (char) 0;
139                        }
140                }
141
142}
143
144
145void on_key_press (rfbBool down,rfbKeySym key,rfbClientPtr cl)
146{
147        if (down)		/* or else the action occurs on both the press and depress */
148	switch (key) {
149
150        case XK_b:
151        case XK_B:
152                blank_framebuffer(cl->screen->frameBuffer, 0, 0, maxx, maxy);
153                rfbDrawString(cl->screen,&default8x16Font,20,maxy-20,"Hello, World!",0xffffff);
154                rfbMarkRectAsModified(cl->screen,0, 0,maxx,maxy);
155                rfbLog("Framebuffer blanked\n");
156                break;
157        case XK_p:
158        case XK_P:
159                /* draw_primary_colors (cl->screen->frameBuffer, 0, 0, maxx, maxy); */
160		draw_primary_colours_generic_ultrafast (cl->screen, 0, 0, maxx, maxy);
161                rfbMarkRectAsModified(cl->screen,0, 0,maxx,maxy);
162                rfbLog("Primary colors displayed\n");
163                break;
164        case XK_Q:
165        case XK_q:
166                rfbLog("Exiting now\n");
167                exit(0);
168        case XK_C:
169        case XK_c:
170		rfbDrawString(cl->screen,&default8x16Font,20,100,"Hello, World!",0xffffff);
171                rfbMarkRectAsModified(cl->screen,0, 0,maxx,maxy);
172                break;
173        default:
174                rfbLog("The %c key was pressed\n", (char) key);
175        }
176}
177
178
179void on_mouse_event (int buttonMask,int x,int y,rfbClientPtr cl)
180{
181	printf("buttonMask: %i\n"
182		"x: %i\n" "y: %i\n", buttonMask, x, y);
183}
184