summaryrefslogtreecommitdiff
path: root/ecos/packages/net/vnc_server/current/tests/vnc-test.c
blob: ed3726167fe40221c61e656d8947932076c829e0 (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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
//==========================================================================
//
//      vnc-test.c
//
//====================================================================
// Copyright (C) 2003 Sweeney Design Ltd
//
//  This software is provided 'as-is', without any express or implied
//  warranty. Permission is granted to anyone to use this software for
//  any purpose, including commercial applications, and to alter it and
//  redistribute it freely.
//
// http://www.sweeneydesign.co.uk
//
//====================================================================
//
//  Test app for eCos VNC-server
//
//==========================================================================


#include <vnc-server.h>
#include <cyg/kernel/kapi.h>  /* Kernel API */
#include <cyg/infra/diag.h>   /* diag_printf() */
#include <cyg/error/codes.h>  /* Cyg_ErrNo, ENOENT */
#include <cyg/io/io.h>        /* cyg_io_handle_t */
#include <sys/select.h>       /* select() functions */
#include <fcntl.h>            /* open() */
#include <unistd.h>           /* read() */
#include <stdlib.h>           /* rand() */

void DrawCursor(void);
void HideCursor(void);

int cursor_x, cursor_y;  /* Cursor position */
vnc_frame_format_t *display_info;  /* Display Info */

int main(int argc, char * argv[])
{
    int mouse_handle = -1;
    int kbd_handle = -1;
    cyg_uint8 mouse_data[8], kbd_data[4];
    cyg_uint8 last_mouse_button = 0;
    cyg_uint32 mouse_len = 0;
    cyg_uint32 kbd_len = 0;
    vnc_printf_return_t print_area;
    cyg_uint16 text_y_pos;
    int i, j;

    cyg_uint16 bell_text_x_pos, bell_text_y_pos;  /* Bell message text position */
    cyg_uint16 bell_text_width, bell_text_height; /* Bell text message size */

    fd_set  sock_desc;  /* Set of descriptors for select */
    int max_handle;

    int bell_text_state = 0;
    char bell_message[] = "***** Click on a yellow pixel to sound the bell *****";

    /* Get information about the display */
    display_info = VncGetInfo();

    /* Initialise the VNC server display */
    VncInit(VNC_WHITE);

    /* Open the mouse device */
    mouse_handle = open("/dev/vnc_mouse", O_RDONLY | O_NONBLOCK);
    if (mouse_handle < 0)
    {
        diag_printf("Could not open mouse device: /dev/vnc_mouse\n");
    }

    /* Open the keyboard device */
    kbd_handle = open("/dev/vnc_kbd", O_RDONLY | O_NONBLOCK);
    if (kbd_handle < 0)
    {
        diag_printf("Could not open kbd device: /dev/vnc_kbd\n");
    }

    /* Draw and label 16 rectangles using the 16 defined colours */
    VncFillRect(0, 0, 75, 50, VNC_BLACK);
    VncPrintf(&font_helvR10, 1, VNC_WHITE, 1, 1, "BLACK");
    VncFillRect(75, 0, 150, 50, VNC_BLUE);
    VncPrintf(&font_helvR10, 1, VNC_WHITE, 76, 1, "BLUE");
    VncFillRect(150, 0, 225, 50, VNC_GREEN);
    VncPrintf(&font_helvR10, 1, VNC_WHITE, 151, 1, "GREEN");
    VncFillRect(225, 0, 300, 50, VNC_CYAN);
    VncPrintf(&font_helvR10, 1, VNC_WHITE, 226, 1, "CYAN");

    VncFillRect(0, 50, 75, 100, VNC_RED);
    VncPrintf(&font_helvR10, 1, VNC_WHITE, 1, 51, "RED");
    VncFillRect(75, 50, 150, 100, VNC_MAGENTA);
    VncPrintf(&font_helvR10, 1, VNC_WHITE, 76, 51, "MAGENTA");
    VncFillRect(150, 50, 225, 100, VNC_BROWN);
    VncPrintf(&font_helvR10, 1, VNC_WHITE, 151, 51, "BROWN");
    VncFillRect(225, 50, 300, 100, VNC_GRAY);
    VncPrintf(&font_helvR10, 1, VNC_WHITE, 226, 51, "GRAY");

    VncFillRect(0, 100, 75, 150, VNC_LTGRAY);
    VncPrintf(&font_helvR10, 1, VNC_BLACK, 1, 101, "LTGRAY");
    VncFillRect(75, 100, 150, 150, VNC_LTBLUE);
    VncPrintf(&font_helvR10, 1, VNC_BLACK, 76, 101, "LTBLUE");
    VncFillRect(150, 100, 225, 150, VNC_LTGREEN);
    VncPrintf(&font_helvR10, 1, VNC_BLACK, 151, 101, "LTGREEN");
    VncFillRect(225, 100, 300, 150, VNC_LTCYAN);
    VncPrintf(&font_helvR10, 1, VNC_BLACK, 226, 101, "LTCYAN");

    VncFillRect(0, 150, 75, 200, VNC_LTRED);
    VncPrintf(&font_helvR10, 1, VNC_BLACK, 1, 151, "LTRED");
    VncFillRect(75, 150, 150, 200, VNC_LTMAGENTA);
    VncPrintf(&font_helvR10, 1, VNC_BLACK, 76, 151, "LTMAGENTA");
    VncFillRect(150, 150, 225, 200, VNC_YELLOW);
    VncPrintf(&font_helvR10, 1, VNC_BLACK, 151, 151, "YELLOW");
    VncFillRect(225, 150, 300, 200, VNC_WHITE);
    VncPrintf(&font_helvR10, 1, VNC_BLACK, 226, 151, "WHITE");

    /* Fade from black to while */
    for (i = 0; i < 256; i++)
    {
        VncDrawVertLine(i, 215, 265, VNC_RGB2COL(i, i, i));
    }

    VncPrintf(&font_helvR10, 1, VNC_WHITE, 1, 216, "GRAYSCALE");

    /* Draw a strip with random coloured pixels */
    for (i = 0; i < 256; i++)
    {
        for (j = 280; j < 330; j++)
        {
            VncDrawPixel( i, j, VNC_RGB2COL(rand()%256 , rand()%256, rand()%256) );
        }
    }

    /* Write a title for the bar of random colours */
    print_area = VncPrintf(&font_helvR10, 0, VNC_BLACK, 1, 281, "RANDOM");
    VncFillRect(1, 281, 1+print_area.width , 281+print_area.height, VNC_LTGRAY);
    VncPrintf(&font_helvR10, 1, VNC_BLACK, 1, 281, "RANDOM");

    /* Report the pixel format */
    if (display_info->rgb332)
    {
        VncPrintf(0, 1, VNC_BLACK, 1, 345, "Pixel format: RGB332");
    }
    else if (display_info->rgb555)
    {
        VncPrintf(0, 1, VNC_BLACK, 1, 345, "Pixel format: RGB555");
    }
    else if (display_info->rgb565)
    {
        VncPrintf(0, 1, VNC_BLACK, 1, 345, "Pixel format: RGB565");
    }
    else if (display_info->bgr233)
    {
        VncPrintf(0, 1, VNC_BLACK, 1, 345, "Pixel format: BGR233");
    }
    else if (display_info->truecolor0888)
    {
        VncPrintf(0, 1, VNC_BLACK, 1, 345, "Pixel format: TrueColor0888");
    }
    else
    {
        VncPrintf(0, 1, VNC_BLACK, 1, 345, "Pixel format: Unknown");
    }

    /* Report the frame dimensions */
    VncPrintf(0, 1, VNC_BLACK, 1, 360, "Frame size: %d x %d", display_info->frame_width, display_info->frame_height);

    /* Write text messages using each of the available fonts */
    text_y_pos = 5;
    print_area = VncPrintf(&font_rom8x8, 1, VNC_BLACK, 350, text_y_pos, "Hello World!\nUsing rom8x8 font");
    text_y_pos += (print_area.height * 1.5);
    print_area = VncPrintf(&font_rom8x16, 1, VNC_BLUE, 350, text_y_pos, "Hello World!\nUsing rom8x16 font");
    text_y_pos += (print_area.height * 1.5);
    print_area = VncPrintf(&font_winFreeSansSerif11x13, 1, VNC_CYAN, 350, text_y_pos, "Hello World!\nUsing winFreeSansSerif11x13 font");
    text_y_pos += (print_area.height * 1.5);
    print_area = VncPrintf(0, 1, VNC_RED, 350, text_y_pos, "Hello World!\nUsing default (winFreeSystem14x16) font");
    text_y_pos += (print_area.height * 1.5);
    print_area = VncPrintf(&font_helvB10, 1, VNC_MAGENTA, 350, text_y_pos, "Hello World!\nUsing helvB10 font");
    text_y_pos += (print_area.height * 1.5);
    print_area = VncPrintf(&font_helvB12, 1, VNC_BROWN, 350, text_y_pos, "Hello World!\nUsing helvB12 font");
    text_y_pos += (print_area.height * 1.5);
    print_area = VncPrintf(&font_helvR10, 1, VNC_GREEN, 350, text_y_pos, "Hello World!\nUsing helvR10 font");
    text_y_pos += (print_area.height * 1.5);
    print_area = VncPrintf(&font_X5x7, 1, VNC_BLACK, 350, text_y_pos, "Hello World!\nUsing X5x7 font");
    text_y_pos += (print_area.height * 1.5);
    print_area = VncPrintf(&font_X6x13, 1, VNC_LTRED, 350, text_y_pos, "Hello World!\nUsing X6x13 font");

    /* Write the message about ringing the bell */
    /* Get the details of the area the text will occupy */
    print_area = VncPrintf(0, 0, VNC_BLACK, 0, 0, "%s", bell_message);
    bell_text_width = print_area.width;
    bell_text_height = print_area.height;

    /* Calculate x position to center the bell text */
    bell_text_x_pos = (display_info->frame_width - bell_text_width) / 2;

    bell_text_y_pos = 400;  /* y position always the same */

    /* Draw a background for the bell text */
    VncFillRect(bell_text_x_pos,  /* x1 */
                bell_text_y_pos,  /* y1 */
                bell_text_x_pos + bell_text_width,  /* x2 */
                bell_text_y_pos + bell_text_height, /* y2 */
                VNC_BLUE);  /* Colour */
    /* Write the text on the background */
    VncPrintf(0, 1, VNC_YELLOW, bell_text_x_pos, bell_text_y_pos, "%s", bell_message);

    /* Initialise the cursor */
    cursor_x = display_info->frame_width / 2;
    cursor_y = display_info->frame_height / 2;
    DrawCursor();

    /* Initialse the max handle variable */
    max_handle = -1;
    if (kbd_handle > max_handle)
    {
        max_handle = kbd_handle;
    }

    if (mouse_handle > max_handle)
    {
        max_handle = mouse_handle;
    }


    while(1)
    {
        FD_ZERO(&sock_desc);  /* Zero the socket set descriptor */

        if (kbd_handle)
        {
            FD_SET(kbd_handle, &sock_desc);  /* Add the keyboard handle to the set */
        }

        if (mouse_handle)
        {
            FD_SET(mouse_handle, &sock_desc);  /* Add the mouse handle to the set */
        }

        /* Use select to wait until a keyboard or mouse event occurs*/
        select(max_handle+1, &sock_desc, NULL, NULL, NULL);

        /* Check for a keyboard event */
        if (FD_ISSET(kbd_handle, &sock_desc))
        {
            kbd_len = 4;

            /* Read keyboard data until there is none left */
            while (kbd_len == 4)
                {
                /* Read 4 bytes from keyboard */
                kbd_len = read(kbd_handle, kbd_data, 4);

                if (kbd_len == 4)
        {
            if (kbd_data[1])
            {
                diag_printf("Keyboard data: keysym value 0x%x is pressed\n",
                             kbd_data[2]*256 + kbd_data[3]);
            }
            else
            {
                diag_printf("Keyboard data: keysym value 0x%x is released\n",
                             kbd_data[2]*256 + kbd_data[3]);
            }
                }
            }
        }

        /* Check for a mouse event */
        if (FD_ISSET(mouse_handle, &sock_desc))
        {
            mouse_len = 8;

            /* Read mouse data until there is none left */
            while (mouse_len == 8)
            {
                /* Read 8 bytes from mouse */
                mouse_len = read(mouse_handle, mouse_data, 8);

                if (mouse_len == 8)
                {
                    HideCursor();  /* Hide the old cursor */
                    cursor_x = mouse_data[2]*256 + mouse_data[3];
                    cursor_y = mouse_data[4]*256 + mouse_data[5];

                    if (mouse_data[1] && !last_mouse_button)
                    {
                        /* Ring bell and change colours of bell message text if the */
                        /* mouse button is pressed on a yellow pixel                */
                        if (VncReadPixel(mouse_data[2]*256 + mouse_data[3],
                                         mouse_data[4]*256 + mouse_data[5]) == VNC_YELLOW)
                        {
                            VncSoundBell();  /* Ring bell on the client */

                            if (bell_text_state)
                            {
                                bell_text_state = 0;
                                /* Draw a background for the text */
                                VncFillRect(bell_text_x_pos,  /* x1 */
                                            bell_text_y_pos,  /* y1 */
                                            bell_text_x_pos + bell_text_width,  /* x2 */
                                            bell_text_y_pos + bell_text_height, /* y2 */
                                            VNC_BLUE);  /* Colour */
                                /* Write the text on the background */
                                print_area = VncPrintf(0, 1, VNC_YELLOW, bell_text_x_pos, bell_text_y_pos, "%s", bell_message);

                            }
                            else
                            {
                                bell_text_state = 1;
                                /* Draw a background for the text */
                                VncFillRect(bell_text_x_pos,  /* x1 */
                                            bell_text_y_pos,  /* y1 */
                                            bell_text_x_pos + bell_text_width,  /* x2 */
                                            bell_text_y_pos + bell_text_height, /* y2 */
                                            VNC_YELLOW);  /* Colour */
                                /* Write the text on the background */
                                print_area = VncPrintf(0, 1, VNC_BLUE, bell_text_x_pos, bell_text_y_pos, "%s", bell_message);
                            }
                        }
                    }

                    last_mouse_button = mouse_data[1];  /* Save mouse button data */
                    DrawCursor();  /* Draw the new cursor */
                }
            }

        }
    }

    return 1;
}


vnc_colour_t under_cursor[16][16];  /* Buffer to hold display area under cursor */

    /* Buffer holding cursor data */
#define BLK VNC_BLACK
#define TRN VNC_BLUE
#define WHT VNC_WHITE
#define GRY VNC_LTGRAY
vnc_colour_t cursor[16][16] = {{BLK, BLK, BLK, TRN, TRN, TRN, TRN, TRN, TRN, TRN, TRN, TRN, TRN, TRN, TRN, TRN},
                               {BLK, GRY, GRY, BLK, BLK, TRN, TRN, TRN, TRN, TRN, TRN, TRN, TRN, TRN, TRN, TRN},
                               {BLK, WHT, GRY, GRY, GRY, BLK, BLK, TRN, TRN, TRN, TRN, TRN, TRN, TRN, TRN, TRN},
                               {TRN, BLK, WHT, GRY, GRY, GRY, GRY, BLK, BLK, TRN, TRN, TRN, TRN, TRN, TRN, TRN},
                               {TRN, BLK, WHT, WHT, GRY, GRY, GRY, GRY, GRY, BLK, BLK, TRN, TRN, TRN, TRN, TRN},
                               {TRN, TRN, BLK, WHT, WHT, GRY, GRY, GRY, GRY, GRY, GRY, BLK, BLK, TRN, TRN, TRN},
                               {TRN, TRN, BLK, WHT, WHT, WHT, GRY, GRY, GRY, GRY, GRY, GRY, GRY, BLK, TRN, TRN},
                               {TRN, TRN, TRN, BLK, WHT, WHT, WHT, GRY, GRY, BLK, BLK, BLK, BLK, BLK, TRN, TRN},
                               {TRN, TRN, TRN, BLK, WHT, WHT, WHT, WHT, GRY, GRY, BLK, TRN, TRN, TRN, TRN, TRN},
                               {TRN, TRN, TRN, TRN, BLK, WHT, WHT, BLK, WHT, GRY, GRY, BLK, TRN, TRN, TRN, TRN},
                               {TRN, TRN, TRN, TRN, BLK, WHT, WHT, BLK, BLK, WHT, GRY, GRY, BLK, TRN, TRN, TRN},
                               {TRN, TRN, TRN, TRN, TRN, BLK, WHT, BLK, TRN, BLK, WHT, GRY, GRY, BLK, TRN, TRN},
                               {TRN, TRN, TRN, TRN, TRN, BLK, WHT, BLK, TRN, TRN, BLK, WHT, GRY, GRY, BLK, TRN},
                               {TRN, TRN, TRN, TRN, TRN, TRN, BLK, TRN, TRN, TRN, TRN, BLK, WHT, GRY, GRY, BLK},
                               {TRN, TRN, TRN, TRN, TRN, TRN, TRN, TRN, TRN, TRN, TRN, TRN, BLK, WHT, BLK, TRN},
                               {TRN, TRN, TRN, TRN, TRN, TRN, TRN, TRN, TRN, TRN, TRN, TRN, TRN, BLK, TRN, TRN}};
#undef BLK
#undef TRN
#undef WHT
#undef GRY

void DrawCursor(void)
{
    int rect_width, rect_height;

    if (cursor_x >= display_info->frame_width)
    {
        /* Cursor is off the screen */
        return;
    }
    else if ((cursor_x + 16) >= display_info->frame_width)
    {
        /* Cursor is partially off the screen */
        rect_width = display_info->frame_width - cursor_x;
    }
    else
    {
        /* Cursor is fully on the screen */
        rect_width = 16;
    }

    if (cursor_y >= display_info->frame_height)
    {
        /* Cursor is off the screen */
        return;
    }
    else if ((cursor_y + 16) >= display_info->frame_height)
    {
        /* Cursor is partially off the screen */
        rect_height = display_info->frame_height - cursor_y;
    }
    else
    {
        /* Cursor is fully on the screen */
        rect_height = 16;
    }

    /* Save the area under the cursor */
    VncCopyRect2Buffer(cursor_x, cursor_y, rect_width, rect_height, under_cursor, 16, 16, 0, 0);

    /* Draw the new cursor */
    VncCopyBuffer2RectMask(cursor, 16, 16, 0, 0, cursor_x, cursor_y, rect_width, rect_height, VNC_BLUE);
}


void HideCursor(void)
{
    int rect_width, rect_height;

    if (cursor_x >= display_info->frame_width)
    {
        /* Cursor is off the screen */
        return;
    }
    else if ((cursor_x + 16) >= display_info->frame_width)
    {
        /* Cursor is partially off the screen */
        rect_width = display_info->frame_width - cursor_x;
    }
    else
    {
        /* Cursor is fully on the screen */
        rect_width = 16;
    }

    if (cursor_y >= display_info->frame_height)
    {
        /* Cursor is off the screen */
        return;
    }
    else if ((cursor_y + 16) >= display_info->frame_height)
    {
        /* Cursor is partially off the screen */
        rect_height = display_info->frame_height - cursor_y;
    }
    else
    {
        /* Cursor is fully on the screen */
        rect_height = 16;
    }

    /* Restore the saved area under the cursor */
    VncCopyBuffer2Rect(under_cursor, 16, 16, 0, 0, cursor_x, cursor_y, rect_width, rect_height);
}