OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [ecos-2.0/] [packages/] [services/] [gfx/] [mw/] [v2_0/] [src/] [demos/] [vnc/] [vncviewer/] [rfbproto.c] - Blame information for rev 174

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 27 unneback
/*
2
 *  Copyright (C) 1997, 1998 Olivetti & Oracle Research Laboratory
3
 *
4
 *  This is free software; you can redistribute it and/or modify
5
 *  it under the terms of the GNU General Public License as published by
6
 *  the Free Software Foundation; either version 2 of the License, or
7
 *  (at your option) any later version.
8
 *
9
 *  This software is distributed in the hope that it will be useful,
10
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 *  GNU General Public License for more details.
13
 *
14
 *  You should have received a copy of the GNU General Public License
15
 *  along with this software; if not, write to the Free Software
16
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,
17
 *  USA.
18
 */
19
 
20
/*
21
 * rfbproto.c - functions to deal with client side of RFB protocol.
22
 */
23
 
24
#include <unistd.h>
25
#include <errno.h>
26
#include <pwd.h>
27
#include <vncviewer.h>
28
#ifndef NANOX
29
#include <X11/Xatom.h>
30
#endif
31
#include <vncauth.h>
32
 
33
static Bool HandleHextileEncoding8(int x, int y, int w, int h);
34
static Bool HandleHextileEncoding16(int x, int y, int w, int h);
35
static Bool HandleHextileEncoding32(int x, int y, int w, int h);
36
 
37
int rfbsock;
38
char *desktopName;
39
rfbPixelFormat myFormat;
40
rfbServerInitMsg si;
41
struct timeval updateRequestTime;
42
Bool sendUpdateRequest;
43
 
44
int endianTest = 1;
45
 
46
 
47
/* note that the CoRRE encoding uses this buffer and assumes it is big enough
48
   to hold 255 * 255 * 32 bits -> 260100 bytes.  640*480 = 307200 bytes */
49
/* also hextile assumes it is big enough to hold 16 * 16 * 32 bits */
50
 
51
#define BUFFER_SIZE (640*480)
52
static char buffer[BUFFER_SIZE];
53
 
54
 
55
void PrintPixelFormat(rfbPixelFormat *format);
56
 
57
 
58
/*
59
 * ConnectToRFBServer.
60
 */
61
 
62
Bool
63
ConnectToRFBServer(const char *hostname, int port)
64
{
65
    unsigned int host;
66
 
67
    if (!StringToIPAddr(hostname, &host)) {
68
        fprintf(stderr,"%s: couldn't convert '%s' to host address\n",
69
                programName,hostname);
70
        return False;
71
    }
72
 
73
    rfbsock = ConnectToTcpAddr(host, port);
74
 
75
    if (rfbsock < 0) {
76
        fprintf(stderr,"%s: unable to connect to VNC server\n",
77
                programName);
78
        return False;
79
    }
80
 
81
    return True;
82
}
83
 
84
 
85
/*
86
 * InitialiseRFBConnection.
87
 */
88
 
89
Bool
90
InitialiseRFBConnection(int sock)
91
{
92
    rfbProtocolVersionMsg pv;
93
    int major,minor;
94
    Bool authWillWork = True;
95
    CARD32 authScheme, reasonLen, authResult;
96
    char *reason;
97
    CARD8 challenge[CHALLENGESIZE];
98
    char *passwd;
99
    int i;
100
    rfbClientInitMsg ci;
101
 
102
    /* if the connection is immediately closed, don't report anything, so
103
       that pmw's monitor can make test connections */
104
 
105
    if (listenSpecified)
106
        errorMessageFromReadExact = False;
107
 
108
    if (!ReadExact(sock, pv, sz_rfbProtocolVersionMsg)) return False;
109
 
110
    errorMessageFromReadExact = True;
111
 
112
    pv[sz_rfbProtocolVersionMsg] = 0;
113
 
114
    if (sscanf(pv,rfbProtocolVersionFormat,&major,&minor) != 2) {
115
        fprintf(stderr,"%s: Not a valid VNC server\n",programName);
116
        return False;
117
    }
118
 
119
    fprintf(stderr,"%s: VNC server supports protocol version %d.%d "
120
            "(viewer %d.%d)\n",
121
            programName,major,minor,rfbProtocolMajorVersion,
122
            rfbProtocolMinorVersion);
123
 
124
    if ((major == 3) && (minor < 3)) {
125
        /* if server is before 3.3 authentication won't work */
126
        authWillWork = False;
127
    } else {
128
        /* any other server version, just tell it what we want */
129
        major = rfbProtocolMajorVersion;
130
        minor = rfbProtocolMinorVersion;
131
    }
132
 
133
    sprintf(pv,rfbProtocolVersionFormat,major,minor);
134
 
135
    if (!WriteExact(sock, pv, sz_rfbProtocolVersionMsg)) return False;
136
 
137
    if (!ReadExact(sock, (char *)&authScheme, 4)) return False;
138
 
139
    authScheme = Swap32IfLE(authScheme);
140
 
141
    switch (authScheme) {
142
 
143
    case rfbConnFailed:
144
        if (!ReadExact(sock, (char *)&reasonLen, 4)) return False;
145
        reasonLen = Swap32IfLE(reasonLen);
146
 
147
        reason = malloc(reasonLen);
148
 
149
        if (!ReadExact(sock, reason, reasonLen)) return False;
150
 
151
        fprintf(stderr,"%s: VNC connection failed: %.*s\n",
152
                programName, (int)reasonLen, reason);
153
        return False;
154
 
155
    case rfbNoAuth:
156
        fprintf(stderr,"%s: No authentication needed\n",programName);
157
        break;
158
 
159
    case rfbVncAuth:
160
        if (!authWillWork) {
161
            fprintf(stderr,
162
                    "\n%s: VNC server uses the old authentication scheme.\n"
163
                    "You should kill your old desktop(s) and restart.\n"
164
                    "If you really need to connect to this desktop use "
165
                    "vncviewer3.2\n\n",
166
                    programName);
167
            return False;
168
        }
169
 
170
        if (!ReadExact(sock, (char *)challenge, CHALLENGESIZE)) return False;
171
 
172
        if (passwdFile) {
173
            passwd = vncDecryptPasswdFromFile(passwdFile);
174
        } else {
175
            static char pass[32] = {"foobar2"};
176
            passwd = pass;
177
            //passwd = getpass("Password: ");
178
            if (strlen(passwd) == 0) {
179
                fprintf(stderr,"%s: Reading password failed\n",programName);
180
                return False;
181
            }
182
            if (strlen(passwd) > 8) {
183
                passwd[8] = '\0';
184
            }
185
        }
186
 
187
        vncEncryptBytes(challenge, passwd);
188
 
189
        /* Lose the password from memory */
190
        for (i=0; i<strlen(passwd); i++) {
191
            passwd[i] = '\0';
192
        }
193
 
194
        if (!WriteExact(sock, challenge, CHALLENGESIZE)) return False;
195
 
196
        if (!ReadExact(sock, (char *)&authResult, 4)) return False;
197
 
198
        authResult = Swap32IfLE(authResult);
199
 
200
        switch (authResult) {
201
        case rfbVncAuthOK:
202
            fprintf(stderr,"%s: VNC authentication succeeded\n",programName);
203
            break;
204
        case rfbVncAuthFailed:
205
            fprintf(stderr,"%s: VNC authentication failed\n",programName);
206
            return False;
207
        case rfbVncAuthTooMany:
208
            fprintf(stderr,"%s: VNC authentication failed - too many tries\n",
209
                    programName);
210
            return False;
211
        default:
212
            fprintf(stderr,"%s: Unknown VNC authentication result: %d\n",
213
                    programName,(int)authResult);
214
            return False;
215
        }
216
        break;
217
 
218
    default:
219
        fprintf(stderr,
220
                "%s: Unknown authentication scheme from VNC server: %d\n",
221
                programName,(int)authScheme);
222
        return False;
223
    }
224
 
225
    ci.shared = (shareDesktop ? 1 : 0);
226
 
227
    if (!WriteExact(sock, (char *)&ci, sz_rfbClientInitMsg)) return False;
228
 
229
    if (!ReadExact(sock, (char *)&si, sz_rfbServerInitMsg)) return False;
230
 
231
    si.framebufferWidth = Swap16IfLE(si.framebufferWidth);
232
    si.framebufferHeight = Swap16IfLE(si.framebufferHeight);
233
    si.format.redMax = Swap16IfLE(si.format.redMax);
234
    si.format.greenMax = Swap16IfLE(si.format.greenMax);
235
    si.format.blueMax = Swap16IfLE(si.format.blueMax);
236
    si.nameLength = Swap32IfLE(si.nameLength);
237
 
238
    if (((updateRequestX + updateRequestW) > si.framebufferWidth) ||
239
        ((updateRequestY + updateRequestH) > si.framebufferHeight))
240
    {
241
        fprintf(stderr,
242
                "%s: region requested is outside server's framebuffer\n",
243
                programName);
244
        return False;
245
    }
246
    if (updateRequestW == 0)
247
        updateRequestW = si.framebufferWidth - updateRequestX;
248
    if (updateRequestH == 0)
249
        updateRequestH = si.framebufferHeight - updateRequestY;
250
 
251
    desktopName = malloc(si.nameLength + 1);
252
 
253
    if (!ReadExact(sock, desktopName, si.nameLength)) return False;
254
 
255
    desktopName[si.nameLength] = 0;
256
 
257
    fprintf(stderr,"%s: Desktop name \"%s\"\n",programName,desktopName);
258
 
259
    fprintf(stderr,
260
            "%s: Connected to VNC server, using protocol version %d.%d\n",
261
            programName, rfbProtocolMajorVersion, rfbProtocolMinorVersion);
262
 
263
    fprintf(stderr,"%s: VNC server default format:\n",programName);
264
    PrintPixelFormat(&si.format);
265
 
266
    return True;
267
}
268
 
269
 
270
/*
271
 * SetFormatAndEncodings.
272
 */
273
 
274
Bool
275
SetFormatAndEncodings()
276
{
277
    rfbSetPixelFormatMsg spf;
278
    char buf[sz_rfbSetEncodingsMsg + MAX_ENCODINGS * 4];
279
    rfbSetEncodingsMsg *se = (rfbSetEncodingsMsg *)buf;
280
    CARD32 *encs = (CARD32 *)(&buf[sz_rfbSetEncodingsMsg]);
281
    int len = 0;
282
    int i;
283
 
284
    spf.type = rfbSetPixelFormat;
285
    spf.format = myFormat;
286
    spf.format.redMax = Swap16IfLE(spf.format.redMax);
287
    spf.format.greenMax = Swap16IfLE(spf.format.greenMax);
288
    spf.format.blueMax = Swap16IfLE(spf.format.blueMax);
289
 
290
    if (!WriteExact(rfbsock, (char *)&spf, sz_rfbSetPixelFormatMsg))
291
        return False;
292
 
293
    se->type = rfbSetEncodings;
294
    se->nEncodings = 0;
295
 
296
    for (i = 0; i < nExplicitEncodings; i++) {
297
        encs[se->nEncodings++] = Swap32IfLE(explicitEncodings[i]);
298
    }
299
 
300
    if (SameMachine(rfbsock)) {
301
        encs[se->nEncodings++] = Swap32IfLE(rfbEncodingRaw);
302
    }
303
 
304
    if (addCopyRect)
305
        encs[se->nEncodings++] = Swap32IfLE(rfbEncodingCopyRect);
306
    if (addHextile)
307
        encs[se->nEncodings++] = Swap32IfLE(rfbEncodingHextile);
308
    if (addCoRRE)
309
        encs[se->nEncodings++] = Swap32IfLE(rfbEncodingCoRRE);
310
    if (addRRE)
311
        encs[se->nEncodings++] = Swap32IfLE(rfbEncodingRRE);
312
 
313
    len = sz_rfbSetEncodingsMsg + se->nEncodings * 4;
314
 
315
    se->nEncodings = Swap16IfLE(se->nEncodings);
316
 
317
    if (!WriteExact(rfbsock, buf, len)) return False;
318
 
319
    return True;
320
}
321
 
322
 
323
/*
324
 * SendIncrementalFramebufferUpdateRequest.
325
 */
326
 
327
Bool
328
SendIncrementalFramebufferUpdateRequest()
329
{
330
    return SendFramebufferUpdateRequest(updateRequestX, updateRequestY,
331
                                        updateRequestW, updateRequestH, True);
332
}
333
 
334
 
335
/*
336
 * SendFramebufferUpdateRequest.
337
 */
338
 
339
Bool
340
SendFramebufferUpdateRequest(int x, int y, int w, int h, Bool incremental)
341
{
342
    rfbFramebufferUpdateRequestMsg fur;
343
 
344
    fur.type = rfbFramebufferUpdateRequest;
345
    fur.incremental = incremental ? 1 : 0;
346
    fur.x = Swap16IfLE(x);
347
    fur.y = Swap16IfLE(y);
348
    fur.w = Swap16IfLE(w);
349
    fur.h = Swap16IfLE(h);
350
 
351
    if (!WriteExact(rfbsock, (char *)&fur, sz_rfbFramebufferUpdateRequestMsg))
352
        return False;
353
 
354
    gettimeofday(&updateRequestTime, NULL);
355
 
356
    sendUpdateRequest = False;
357
 
358
    return True;
359
}
360
 
361
 
362
/*
363
 * SendPointerEvent.
364
 */
365
 
366
Bool
367
SendPointerEvent(int x, int y, int buttonMask)
368
{
369
    rfbPointerEventMsg pe;
370
 
371
    pe.type = rfbPointerEvent;
372
    pe.buttonMask = buttonMask;
373
    if (x < 0) x = 0;
374
    if (y < 0) y = 0;
375
    pe.x = Swap16IfLE(x);
376
    pe.y = Swap16IfLE(y);
377
    return WriteExact(rfbsock, (char *)&pe, sz_rfbPointerEventMsg);
378
}
379
 
380
 
381
/*
382
 * SendKeyEvent.
383
 */
384
 
385
Bool
386
SendKeyEvent(CARD32 key, Bool down)
387
{
388
    rfbKeyEventMsg ke;
389
 
390
    ke.type = rfbKeyEvent;
391
    ke.down = down ? 1 : 0;
392
    ke.key = Swap32IfLE(key);
393
    return WriteExact(rfbsock, (char *)&ke, sz_rfbKeyEventMsg);
394
}
395
 
396
 
397
/*
398
 * SendClientCutText.
399
 */
400
 
401
Bool
402
SendClientCutText(char *str, int len)
403
{
404
    rfbClientCutTextMsg cct;
405
 
406
    cct.type = rfbClientCutText;
407
    cct.length = Swap32IfLE(len);
408
    return  (WriteExact(rfbsock, (char *)&cct, sz_rfbClientCutTextMsg) &&
409
             WriteExact(rfbsock, str, len));
410
}
411
 
412
 
413
 
414
/*
415
 * HandleRFBServerMessage.
416
 */
417
 
418
Bool
419
HandleRFBServerMessage()
420
{
421
    rfbServerToClientMsg msg;
422
 
423
    if (!ReadExact(rfbsock, (char *)&msg, 1))
424
        return False;
425
 
426
    switch (msg.type) {
427
 
428
    case rfbSetColourMapEntries:
429
    {
430
        int i;
431
        CARD16 rgb[3];
432
        XColor xc;
433
 
434
        if (!ReadExact(rfbsock, ((char *)&msg) + 1,
435
                         sz_rfbSetColourMapEntriesMsg - 1))
436
            return False;
437
 
438
        msg.scme.firstColour = Swap16IfLE(msg.scme.firstColour);
439
        msg.scme.nColours = Swap16IfLE(msg.scme.nColours);
440
 
441
        for (i = 0; i < msg.scme.nColours; i++) {
442
            if (!ReadExact(rfbsock, (char *)rgb, 6))
443
                return False;
444
            xc.pixel = msg.scme.firstColour + i;
445
            xc.red = Swap16IfLE(rgb[0]);
446
            xc.green = Swap16IfLE(rgb[1]);
447
            xc.blue = Swap16IfLE(rgb[2]);
448
            xc.flags = DoRed|DoGreen|DoBlue;
449
//printf("XStoreColor (%d,%d,%d) = %d\n", xc.red>>8, xc.green>>8, xc.blue>>8, xc.pixel);
450
            XStoreColor(dpy, cmap, &xc);
451
        }
452
 
453
        break;
454
    }
455
 
456
    case rfbFramebufferUpdate:
457
    {
458
        rfbFramebufferUpdateRectHeader rect;
459
        int linesToRead;
460
        int bytesPerLine;
461
        int i;
462
 
463
        if (!ReadExact(rfbsock, ((char *)&msg.fu) + 1,
464
                         sz_rfbFramebufferUpdateMsg - 1))
465
            return False;
466
 
467
        msg.fu.nRects = Swap16IfLE(msg.fu.nRects);
468
 
469
        for (i = 0; i < msg.fu.nRects; i++) {
470
            if (!ReadExact(rfbsock, (char *)&rect,
471
                             sz_rfbFramebufferUpdateRectHeader))
472
                return False;
473
 
474
            rect.r.x = Swap16IfLE(rect.r.x);
475
            rect.r.y = Swap16IfLE(rect.r.y);
476
            rect.r.w = Swap16IfLE(rect.r.w);
477
            rect.r.h = Swap16IfLE(rect.r.h);
478
 
479
            rect.encoding = Swap32IfLE(rect.encoding);
480
 
481
            if ((rect.r.x + rect.r.w > si.framebufferWidth) ||
482
                (rect.r.y + rect.r.h > si.framebufferHeight))
483
            {
484
                fprintf(stderr,"%s: rect too large: %dx%d at (%d, %d)\n",
485
                       programName, rect.r.w, rect.r.h, rect.r.x, rect.r.y);
486
                return False;
487
            }
488
 
489
            if ((rect.r.h * rect.r.w) == 0) {
490
                fprintf(stderr,"%s: zero size rect - ignoring\n",programName);
491
                continue;
492
            }
493
 
494
            switch (rect.encoding) {
495
 
496
            case rfbEncodingRaw:
497
 
498
                bytesPerLine = rect.r.w * myFormat.bitsPerPixel / 8;
499
                linesToRead = BUFFER_SIZE / bytesPerLine;
500
 
501
                while (rect.r.h > 0) {
502
                    if (linesToRead > rect.r.h)
503
                        linesToRead = rect.r.h;
504
 
505
                    if (!ReadExact(rfbsock, buffer,bytesPerLine * linesToRead))
506
                        return False;
507
 
508
                    CopyDataToScreen((CARD8 *)buffer, rect.r.x, rect.r.y,
509
                                     rect.r.w, linesToRead);
510
 
511
                    rect.r.h -= linesToRead;
512
                    rect.r.y += linesToRead;
513
 
514
                }
515
                break;
516
 
517
            case rfbEncodingCopyRect:
518
            {
519
                rfbCopyRect cr;
520
 
521
                if (!ReadExact(rfbsock, (char *)&cr, sz_rfbCopyRect))
522
                    return False;
523
 
524
                cr.srcX = Swap16IfLE(cr.srcX);
525
                cr.srcY = Swap16IfLE(cr.srcY);
526
 
527
                if (copyRectDelay != 0) {
528
                    XFillRectangle(dpy, canvas, srcGC, cr.srcX, cr.srcY,
529
                                   rect.r.w, rect.r.h);
530
                    XFillRectangle(dpy, canvas, dstGC, rect.r.x, rect.r.y,
531
                                   rect.r.w, rect.r.h);
532
                    XSync(dpy,False);
533
                    usleep(copyRectDelay * 1000);
534
                    XFillRectangle(dpy, canvas, dstGC, rect.r.x, rect.r.y,
535
                                   rect.r.w, rect.r.h);
536
                    XFillRectangle(dpy, canvas, srcGC, cr.srcX, cr.srcY,
537
                                   rect.r.w, rect.r.h);
538
                }
539
 
540
                XCopyArea(dpy, canvas, canvas, gc, cr.srcX, cr.srcY,
541
                          rect.r.w, rect.r.h, rect.r.x, rect.r.y);
542
 
543
                break;
544
            }
545
 
546
            case rfbEncodingRRE:
547
            {
548
                rfbRREHeader hdr;
549
                CARD8 pix8;
550
                CARD16 pix16;
551
                CARD32 pix32;
552
                XGCValues gcv;
553
                rfbRectangle subrect;
554
                int j;
555
 
556
                if (!ReadExact(rfbsock, (char *)&hdr, sz_rfbRREHeader))
557
                    return False;
558
 
559
                hdr.nSubrects = Swap32IfLE(hdr.nSubrects);
560
 
561
                switch (myFormat.bitsPerPixel) {
562
 
563
                case 8:
564
                    if (!ReadExact(rfbsock, (char *)&pix8, 1))
565
                        return False;
566
 
567
                    gcv.foreground = (useBGR233 ? BGR233ToPixel[pix8]
568
                                      : pix8);
569
                    XChangeGC(dpy, gc, GCForeground, &gcv);
570
                    XFillRectangle(dpy, canvas, gc, rect.r.x, rect.r.y,
571
                                   rect.r.w, rect.r.h);
572
 
573
                    for (j = 0; j < hdr.nSubrects; j++) {
574
                        if (!ReadExact(rfbsock, (char *)&pix8, 1))
575
                            return False;
576
 
577
                        if (!ReadExact(rfbsock, (char *)&subrect,
578
                                         sz_rfbRectangle))
579
                            return False;
580
 
581
                        subrect.x = Swap16IfLE(subrect.x);
582
                        subrect.y = Swap16IfLE(subrect.y);
583
                        subrect.w = Swap16IfLE(subrect.w);
584
                        subrect.h = Swap16IfLE(subrect.h);
585
 
586
                        gcv.foreground = (useBGR233 ? BGR233ToPixel[pix8]
587
                                          : pix8);
588
                        XChangeGC(dpy, gc, GCForeground, &gcv);
589
                        XFillRectangle(dpy, canvas, gc, rect.r.x + subrect.x,
590
                                       rect.r.y + subrect.y, subrect.w,
591
                                       subrect.h);
592
                    }
593
                    break;
594
 
595
                case 16:
596
                    if (!ReadExact(rfbsock, (char *)&pix16, 2))
597
                        return False;
598
 
599
                    gcv.foreground = pix16;
600
                    XChangeGC(dpy, gc, GCForeground, &gcv);
601
                    XFillRectangle(dpy, canvas, gc, rect.r.x, rect.r.y,
602
                                   rect.r.w, rect.r.h);
603
 
604
                    for (j = 0; j < hdr.nSubrects; j++) {
605
                        if (!ReadExact(rfbsock, (char *)&pix16, 2))
606
                            return False;
607
 
608
                        if (!ReadExact(rfbsock, (char *)&subrect,
609
                                         sz_rfbRectangle))
610
                            return False;
611
 
612
                        subrect.x = Swap16IfLE(subrect.x);
613
                        subrect.y = Swap16IfLE(subrect.y);
614
                        subrect.w = Swap16IfLE(subrect.w);
615
                        subrect.h = Swap16IfLE(subrect.h);
616
 
617
                        gcv.foreground = pix16;
618
                        XChangeGC(dpy, gc, GCForeground, &gcv);
619
                        XFillRectangle(dpy, canvas, gc, rect.r.x + subrect.x,
620
                                       rect.r.y + subrect.y, subrect.w,
621
                                       subrect.h);
622
                    }
623
                    break;
624
 
625
                case 32:
626
                    if (!ReadExact(rfbsock, (char *)&pix32, 4))
627
                        return False;
628
 
629
                    gcv.foreground = pix32;
630
                    XChangeGC(dpy, gc, GCForeground, &gcv);
631
                    XFillRectangle(dpy, canvas, gc, rect.r.x, rect.r.y,
632
                                   rect.r.w, rect.r.h);
633
 
634
                    for (j = 0; j < hdr.nSubrects; j++) {
635
                        if (!ReadExact(rfbsock, (char *)&pix32, 4))
636
                            return False;
637
 
638
                        if (!ReadExact(rfbsock, (char *)&subrect,
639
                                         sz_rfbRectangle))
640
                            return False;
641
 
642
                        subrect.x = Swap16IfLE(subrect.x);
643
                        subrect.y = Swap16IfLE(subrect.y);
644
                        subrect.w = Swap16IfLE(subrect.w);
645
                        subrect.h = Swap16IfLE(subrect.h);
646
 
647
                        gcv.foreground = pix32;
648
                        XChangeGC(dpy, gc, GCForeground, &gcv);
649
                        XFillRectangle(dpy, canvas, gc, rect.r.x + subrect.x,
650
                                       rect.r.y + subrect.y, subrect.w,
651
                                       subrect.h);
652
                    }
653
                    break;
654
                }
655
                break;
656
            }
657
 
658
            case rfbEncodingCoRRE:
659
            {
660
                rfbRREHeader hdr;
661
                CARD8 pix8;
662
                CARD16 pix16;
663
                CARD32 pix32;
664
                XGCValues gcv;
665
                int j;
666
                CARD8 *ptr;
667
                register int x, y, w, h;
668
 
669
                if (!ReadExact(rfbsock, (char *)&hdr, sz_rfbRREHeader))
670
                    return False;
671
 
672
                hdr.nSubrects = Swap32IfLE(hdr.nSubrects);
673
 
674
                switch (myFormat.bitsPerPixel) {
675
 
676
                case 8:
677
                    if (!ReadExact(rfbsock, (char *)&pix8, 1))
678
                        return False;
679
 
680
                    gcv.foreground = (useBGR233 ? BGR233ToPixel[pix8]
681
                                      : pix8);
682
                    XChangeGC(dpy, gc, GCForeground, &gcv);
683
                    XFillRectangle(dpy, canvas, gc, rect.r.x, rect.r.y,
684
                                   rect.r.w, rect.r.h);
685
 
686
                    if (!ReadExact(rfbsock, buffer, hdr.nSubrects * 5))
687
                        return False;
688
 
689
                    ptr = (CARD8 *)buffer;
690
 
691
                    for (j = 0; j < hdr.nSubrects; j++) {
692
                        pix8 = *ptr++;
693
                        x = *ptr++;
694
                        y = *ptr++;
695
                        w = *ptr++;
696
                        h = *ptr++;
697
                        gcv.foreground = (useBGR233 ? BGR233ToPixel[pix8]
698
                                          : pix8);
699
                        XChangeGC(dpy, gc, GCForeground, &gcv);
700
                        XFillRectangle(dpy, canvas, gc, rect.r.x + x,
701
                                       rect.r.y + y, w, h);
702
                    }
703
                    break;
704
 
705
                case 16:
706
                    if (!ReadExact(rfbsock, (char *)&pix16, 2))
707
                        return False;
708
 
709
                    gcv.foreground = pix16;
710
                    XChangeGC(dpy, gc, GCForeground, &gcv);
711
                    XFillRectangle(dpy, canvas, gc, rect.r.x, rect.r.y,
712
                                   rect.r.w, rect.r.h);
713
 
714
                    if (!ReadExact(rfbsock, buffer, hdr.nSubrects * 6))
715
                        return False;
716
 
717
                    ptr = (CARD8 *)buffer;
718
 
719
                    for (j = 0; j < hdr.nSubrects; j++) {
720
                        pix16 = *(CARD16 *)ptr;
721
                        ptr += 2;
722
                        x = *ptr++;
723
                        y = *ptr++;
724
                        w = *ptr++;
725
                        h = *ptr++;
726
                        gcv.foreground = pix16;
727
                        XChangeGC(dpy, gc, GCForeground, &gcv);
728
                        XFillRectangle(dpy, canvas, gc, rect.r.x + x,
729
                                       rect.r.y + y, w, h);
730
                    }
731
                    break;
732
 
733
                case 32:
734
                    if (!ReadExact(rfbsock, (char *)&pix32, 4))
735
                        return False;
736
 
737
                    gcv.foreground = pix32;
738
                    XChangeGC(dpy, gc, GCForeground, &gcv);
739
                    XFillRectangle(dpy, canvas, gc, rect.r.x, rect.r.y,
740
                                   rect.r.w, rect.r.h);
741
 
742
                    if (!ReadExact(rfbsock, buffer, hdr.nSubrects * 8))
743
                        return False;
744
 
745
                    ptr = (CARD8 *)buffer;
746
 
747
                    for (j = 0; j < hdr.nSubrects; j++) {
748
                        pix32 = *(CARD32 *)ptr;
749
                        ptr += 4;
750
                        x = *ptr++;
751
                        y = *ptr++;
752
                        w = *ptr++;
753
                        h = *ptr++;
754
                        gcv.foreground = pix32;
755
                        XChangeGC(dpy, gc, GCForeground, &gcv);
756
                        XFillRectangle(dpy, canvas, gc, rect.r.x + x,
757
                                       rect.r.y + y, w, h);
758
                    }
759
                    break;
760
                }
761
                break;
762
            }
763
 
764
            case rfbEncodingHextile:
765
            {
766
                switch (myFormat.bitsPerPixel) {
767
                case 8:
768
                    if (!HandleHextileEncoding8(rect.r.x, rect.r.y,
769
                                                rect.r.w, rect.r.h))
770
                        return False;
771
                    break;
772
                case 16:
773
                    if (!HandleHextileEncoding16(rect.r.x, rect.r.y,
774
                                                 rect.r.w, rect.r.h))
775
                        return False;
776
                    break;
777
                case 32:
778
                    if (!HandleHextileEncoding32(rect.r.x, rect.r.y,
779
                                                 rect.r.w, rect.r.h))
780
                        return False;
781
                    break;
782
                }
783
                break;
784
            }
785
 
786
            default:
787
                fprintf(stderr,"%s: unknown rect encoding %d\n",programName,
788
                       (int)rect.encoding);
789
                return False;
790
            }
791
        }
792
 
793
        sendUpdateRequest = True;
794
 
795
        break;
796
    }
797
 
798
    case rfbBell:
799
        XBell(dpy,100);
800
        break;
801
 
802
    case rfbServerCutText:
803
    {
804
#ifndef NANOX
805
        char *str;
806
 
807
        if (!ReadExact(rfbsock, ((char *)&msg) + 1,
808
                         sz_rfbServerCutTextMsg - 1))
809
            return False;
810
 
811
        msg.sct.length = Swap32IfLE(msg.sct.length);
812
 
813
        str = malloc(msg.sct.length);
814
 
815
        if (!ReadExact(rfbsock, str, msg.sct.length))
816
            return False;
817
 
818
        XSelectInput(dpy, DefaultRootWindow(dpy), 0);
819
        XStoreBytes(dpy, str, msg.sct.length);
820
        XSetSelectionOwner(dpy, XA_PRIMARY, None, CurrentTime);
821
        XSelectInput(dpy, DefaultRootWindow(dpy), PropertyChangeMask);
822
 
823
        free(str);
824
#endif /* !NANOX*/
825
        break;
826
    }
827
 
828
    default:
829
        fprintf(stderr,"%s: unknown message type %d from VNC server\n",
830
               programName,msg.type);
831
        return False;
832
    }
833
 
834
    return True;
835
}
836
 
837
 
838
#define GET_PIXEL8(pix, ptr) ((pix) = *(ptr)++)
839
 
840
#define GET_PIXEL16(pix, ptr) (((CARD8*)&(pix))[0] = *(ptr)++, \
841
                               ((CARD8*)&(pix))[1] = *(ptr)++)
842
 
843
#define GET_PIXEL32(pix, ptr) (((CARD8*)&(pix))[0] = *(ptr)++, \
844
                               ((CARD8*)&(pix))[1] = *(ptr)++, \
845
                               ((CARD8*)&(pix))[2] = *(ptr)++, \
846
                               ((CARD8*)&(pix))[3] = *(ptr)++)
847
 
848
#define DEFINE_HEXTILE(bpp)                                                   \
849
static Bool                                                                   \
850
HandleHextileEncoding##bpp(int rx, int ry, int rw, int rh)                    \
851
{                                                                             \
852
    CARD##bpp bg, fg;                                                         \
853
    XGCValues gcv;                                                            \
854
    int i;                                                                    \
855
    CARD8 *ptr;                                                               \
856
    int x, y, w, h;                                                           \
857
    int sx, sy, sw, sh;                                                       \
858
    CARD8 subencoding;                                                        \
859
    CARD8 nSubrects;                                                          \
860
                                                                              \
861
    for (y = ry; y < ry+rh; y += 16) {                                        \
862
        for (x = rx; x < rx+rw; x += 16) {                                    \
863
            w = h = 16;                                                       \
864
            if (rx+rw - x < 16)                                               \
865
                w = rx+rw - x;                                                \
866
            if (ry+rh - y < 16)                                               \
867
                h = ry+rh - y;                                                \
868
                                                                              \
869
            if (!ReadExact(rfbsock, (char *)&subencoding, 1))                 \
870
                return False;                                                 \
871
                                                                              \
872
            if (subencoding & rfbHextileRaw) {                                \
873
                if (!ReadExact(rfbsock, buffer, w * h * (bpp / 8)))           \
874
                    return False;                                             \
875
                                                                              \
876
                CopyDataToScreen((CARD8 *)buffer, x, y, w, h);                \
877
                continue;                                                     \
878
            }                                                                 \
879
                                                                              \
880
            if (subencoding & rfbHextileBackgroundSpecified)                  \
881
                if (!ReadExact(rfbsock, (char *)&bg, (bpp/8)))                \
882
                    return False;                                             \
883
                                                                              \
884
            if ((bpp == 8) && useBGR233)                                      \
885
                gcv.foreground = BGR233ToPixel[bg];                           \
886
            else                                                              \
887
                gcv.foreground = bg;                                          \
888
                                                                              \
889
            XChangeGC(dpy, gc, GCForeground, &gcv);                           \
890
            XFillRectangle(dpy, canvas, gc, x, y, w, h);                      \
891
                                                                              \
892
            if (subencoding & rfbHextileForegroundSpecified)                  \
893
                if (!ReadExact(rfbsock, (char *)&fg, (bpp/8)))                \
894
                    return False;                                             \
895
                                                                              \
896
            if (!(subencoding & rfbHextileAnySubrects)) {                     \
897
                continue;                                                     \
898
            }                                                                 \
899
                                                                              \
900
            if (!ReadExact(rfbsock, (char *)&nSubrects, 1))                   \
901
                return False;                                                 \
902
                                                                              \
903
            ptr = (CARD8 *)buffer;                                            \
904
                                                                              \
905
            if (subencoding & rfbHextileSubrectsColoured) {                   \
906
                if (!ReadExact(rfbsock, buffer, nSubrects * (2 + (bpp / 8)))) \
907
                    return False;                                             \
908
                                                                              \
909
                for (i = 0; i < nSubrects; i++) {                             \
910
                    GET_PIXEL##bpp(fg, ptr);                                  \
911
                    sx = rfbHextileExtractX(*ptr);                            \
912
                    sy = rfbHextileExtractY(*ptr);                            \
913
                    ptr++;                                                    \
914
                    sw = rfbHextileExtractW(*ptr);                            \
915
                    sh = rfbHextileExtractH(*ptr);                            \
916
                    ptr++;                                                    \
917
                    if ((bpp == 8) && useBGR233)                              \
918
                        gcv.foreground = BGR233ToPixel[fg];                   \
919
                    else                                                      \
920
                        gcv.foreground = fg;                                  \
921
                                                                              \
922
                    XChangeGC(dpy, gc, GCForeground, &gcv);                   \
923
                    XFillRectangle(dpy, canvas, gc, x+sx, y+sy, sw, sh);      \
924
                }                                                             \
925
                                                                              \
926
            } else {                                                          \
927
                if (!ReadExact(rfbsock, buffer, nSubrects * 2))               \
928
                    return False;                                             \
929
                                                                              \
930
                if ((bpp == 8) && useBGR233)                                  \
931
                    gcv.foreground = BGR233ToPixel[fg];                       \
932
                else                                                          \
933
                    gcv.foreground = fg;                                      \
934
                                                                              \
935
                XChangeGC(dpy, gc, GCForeground, &gcv);                       \
936
                                                                              \
937
                for (i = 0; i < nSubrects; i++) {                             \
938
                    sx = rfbHextileExtractX(*ptr);                            \
939
                    sy = rfbHextileExtractY(*ptr);                            \
940
                    ptr++;                                                    \
941
                    sw = rfbHextileExtractW(*ptr);                            \
942
                    sh = rfbHextileExtractH(*ptr);                            \
943
                    ptr++;                                                    \
944
                    XFillRectangle(dpy, canvas, gc, x+sx, y+sy, sw, sh);      \
945
                }                                                             \
946
            }                                                                 \
947
        }                                                                     \
948
    }                                                                         \
949
                                                                              \
950
    return True;                                                              \
951
}
952
 
953
DEFINE_HEXTILE(8)
954
DEFINE_HEXTILE(16)
955
DEFINE_HEXTILE(32)
956
 
957
 
958
/*
959
 * PrintPixelFormat.
960
 */
961
 
962
void
963
PrintPixelFormat(format)
964
    rfbPixelFormat *format;
965
{
966
    if (format->bitsPerPixel == 1) {
967
        fprintf(stderr,"Single bit per pixel.\n");
968
        fprintf(stderr,
969
                "%s significant bit in each byte is leftmost on the screen.\n",
970
                (format->bigEndian ? "Most" : "Least"));
971
    } else {
972
        fprintf(stderr,"%d bits per pixel.\n",format->bitsPerPixel);
973
        if (format->bitsPerPixel != 8) {
974
            fprintf(stderr,"%s significant byte first in each pixel.\n",
975
                    (format->bigEndian ? "Most" : "Least"));
976
        }
977
        if (format->trueColour) {
978
            fprintf(stderr,"True colour: max red %d green %d blue %d\n",
979
                    format->redMax, format->greenMax, format->blueMax);
980
            fprintf(stderr,"            shift red %d green %d blue %d\n",
981
                    format->redShift, format->greenShift, format->blueShift);
982
        } else {
983
            fprintf(stderr,"Uses a colour map (not true colour).\n");
984
        }
985
    }
986
}

powered by: WebSVN 2.1.0

© copyright 1999-2025 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.