summaryrefslogtreecommitdiffstats
path: root/examples/zippy.c
blob: 5a5961a09c9171df1a2922ad69268901ca7cdee6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#include <stdio.h>
#include <stdlib.h>
#include <rfb/rfb.h>
#include <rfb/keysym.h>
#include <rfb/default8x16.h>

static int maxx=400, maxy=400, bpp=4;
/* odd maxx doesn't work (vncviewer bug) */

/* Here we create a structure so that every client has it's own pointer */

/* turns the framebuffer black */
void blank_framebuffer(char* frame_buffer, int x1, int y1, int x2, int y2);
/* displays a red bar, a green bar, and a blue bar */
void draw_primary_colors (char* frame_buffer, int x1, int y1, int x2, int y2);
void draw_primary_colours_generic(rfbScreenInfoPtr s,int x1,int y1,int x2,int y2);
void draw_primary_colours_generic_fast(rfbScreenInfoPtr s,int x1,int y1,int x2,int y2);
void linecount (char* frame_buffer);
/* handles mouse events */
void on_mouse_event (int buttonMask,int x,int y,rfbClientPtr cl);
/* handles keyboard events */
void on_key_press (rfbBool down,rfbKeySym key,rfbClientPtr cl);

int main (int argc, char **argv)
{
	rfbScreenInfoPtr server;

	if(!rfbProcessSizeArguments(&maxx,&maxy,&bpp,&argc,argv))
	  return 1;
	  
        server = rfbGetScreen (&argc, argv, maxx, maxy, 8, 3, bpp);
        if(!server)
          return 0;
	server->desktopName = "Zippy das wundersquirrel\'s VNC server";
	server->frameBuffer = (char*)malloc(maxx*maxy*bpp);
	server->alwaysShared = TRUE;
        server->kbdAddEvent = on_key_press;
	server->ptrAddEvent = on_mouse_event;

	rfbInitServer (server);

	blank_framebuffer(server->frameBuffer, 0, 0, maxx, maxy);
	rfbRunEventLoop (server, -1, FALSE);
	free(server->frameBuffer);
	rfbScreenCleanup (server);
	return 0;
}

void blank_framebuffer(char* frame_buffer, int x1, int y1, int x2, int y2)
{
	int i;
        for (i=0; i < maxx * maxy * bpp; i++) frame_buffer[i]=(char) 0;
}

void draw_primary_colors (char* frame_buffer, int x1, int y1, int x2, int y2)
{
        int i, j, current_pixel;
        for (i=y1; i < y2; i++){
                for (j=x1; j < x2; j++) {
                        current_pixel = (i*x2 + j) * bpp;
                        if (i < y2 ) {
                                frame_buffer[current_pixel+0] = (char) 128;
                                frame_buffer[current_pixel+1] = (char) 0;
                                frame_buffer[current_pixel+2] = (char) 0;
                        }
                        if (i < y2/3*2) {
                                frame_buffer[current_pixel+0] = (char) 0;
                                frame_buffer[current_pixel+1] = (char) 128;
                                frame_buffer[current_pixel+2] = (char) 0;
                        }
                        if (i < y2/3) {
                                frame_buffer[current_pixel+0] = (char) 0;
                                frame_buffer[current_pixel+1] = (char) 0;
                                frame_buffer[current_pixel+2] = (char) 128;
                        }
                }
        }
 }

/* Dscho's versions (slower, but works for bpp != 3 or 4) */
void draw_primary_colours_generic(rfbScreenInfoPtr s,int x1,int y1,int x2,int y2)
{
  rfbPixelFormat f=s->serverFormat;
  int i,j;
  for(j=y1;j<y2;j++)
    for(i=x1;i<x2;i++)
      if(j<y1*2/3+y2/3)
	rfbDrawPixel(s,i,j,f.redMax<<f.redShift);
      else if(j<y1/3+y2*2/3)
	rfbDrawPixel(s,i,j,f.greenMax<<f.greenShift);
      else
	rfbDrawPixel(s,i,j,f.blueMax<<f.blueShift);
}

void draw_primary_colours_generic_fast(rfbScreenInfoPtr s,int x1,int y1,int x2,int y2)
{
  rfbPixelFormat f=s->serverFormat;
  int i,j,y3=(y1*2+y2)/3,y4=(y1+y2*2)/3;
  /* draw first pixel */
  rfbDrawPixel(s,x1,y1,f.redMax<<f.redShift);
  rfbDrawPixel(s,x1,y3,f.greenMax<<f.greenShift);
  rfbDrawPixel(s,x1,y4,f.blueMax<<f.blueShift);
  /* then copy stripes */
  for(j=0;j<y2-y4;j++)
    for(i=x1;i<x2;i++) {
#define ADDR(x,y) s->frameBuffer+(x)*bpp+(y)*s->paddedWidthInBytes
      memcpy(ADDR(i,j+y1),ADDR(x1,y1),bpp);
      memcpy(ADDR(i,j+y3),ADDR(x1,y3),bpp);
      memcpy(ADDR(i,j+y4),ADDR(x1,y4),bpp);
    }
}

static void draw_primary_colours_generic_ultrafast(rfbScreenInfoPtr s,int x1,int y1,int x2,int y2)
{
  rfbPixelFormat f=s->serverFormat;
  int y3=(y1*2+y2)/3,y4=(y1+y2*2)/3;
  /* fill rectangles */
  rfbFillRect(s,x1,y1,x2,y3,f.redMax<<f.redShift);
  rfbFillRect(s,x1,y3,x2,y4,f.greenMax<<f.greenShift);
  rfbFillRect(s,x1,y4,x2,y2,f.blueMax<<f.blueShift);
}

void linecount (char* frame_buffer)
{
        int i,j,k, current_pixel;
        for (i=maxy-4; i>maxy-20; i-=4)
                for (j=0; j<4; j++) for (k=0; k < maxx; k++) {
                        current_pixel = (i*j*maxx + k) * bpp;
                        if (i%2 == 0) {
                                frame_buffer[current_pixel+0] = (char) 0;
                                frame_buffer[current_pixel+1] = (char) 0;
                                frame_buffer[current_pixel+2] = (char) 128;
                        }

                        if (i%2 == 1) {
                                frame_buffer[current_pixel+0] = (char) 128;
                                frame_buffer[current_pixel+1] = (char) 0;
                                frame_buffer[current_pixel+2] = (char) 0;
                        }
                }

}


void on_key_press (rfbBool down,rfbKeySym key,rfbClientPtr cl)
{
        if (down)		/* or else the action occurs on both the press and depress */
	switch (key) {

        case XK_b:
        case XK_B:
                blank_framebuffer(cl->screen->frameBuffer, 0, 0, maxx, maxy);
                rfbDrawString(cl->screen,&default8x16Font,20,maxy-20,"Hello, World!",0xffffff);
                rfbMarkRectAsModified(cl->screen,0, 0,maxx,maxy);
                rfbLog("Framebuffer blanked\n");
                break;
        case XK_p:
        case XK_P:
                /* draw_primary_colors (cl->screen->frameBuffer, 0, 0, maxx, maxy); */
		draw_primary_colours_generic_ultrafast (cl->screen, 0, 0, maxx, maxy);
                rfbMarkRectAsModified(cl->screen,0, 0,maxx,maxy);
                rfbLog("Primary colors displayed\n");
                break;
        case XK_Q:
        case XK_q:
                rfbLog("Exiting now\n");
                exit(0);
        case XK_C:
        case XK_c:
		rfbDrawString(cl->screen,&default8x16Font,20,100,"Hello, World!",0xffffff);
                rfbMarkRectAsModified(cl->screen,0, 0,maxx,maxy);
                break;
        default:
                rfbLog("The %c key was pressed\n", (char) key);
        }
}


void on_mouse_event (int buttonMask,int x,int y,rfbClientPtr cl)
{
	printf("buttonMask: %i\n"
		"x: %i\n" "y: %i\n", buttonMask, x, y);
}