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

Subversion Repositories or1k

[/] [or1k/] [trunk/] [mw/] [src/] [demos/] [vnc/] [vncviewer/] [listen.c] - Blame information for rev 1765

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 673 markom
/*
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
 * listen.c - listen for incoming connections
22
 */
23
 
24
#include <unistd.h>
25
#include <sys/types.h>
26
#include <sys/wait.h>
27
#include <sys/time.h>
28
#include <vncviewer.h>
29
 
30
#define FLASHWIDTH 50   /* pixels */
31
#define FLASHDELAY 1    /* seconds */
32
 
33
static Font flashFont;
34
 
35
static void getFlashFont(Display *d);
36
static void flashDisplay(Display *d, char *user);
37
 
38
void
39
listenForIncomingConnections()
40
{
41
    Display *d;
42
    XEvent ev;
43
    int listenSocket, flashSocket, sock;
44
    fd_set fds;
45
    char flashUser[256];
46
    int n;
47
 
48
    if (!(d = XOpenDisplay(displayname))) {
49
        fprintf(stderr,"%s: unable to open display %s\n",
50
                programName, XDisplayName(displayname));
51
        exit(1);
52
    }
53
 
54
    getFlashFont(d);
55
 
56
    listenSocket = ListenAtTcpPort(listenPort);
57
    flashSocket = ListenAtTcpPort(flashPort);
58
 
59
    if ((listenSocket < 0) || (flashSocket < 0)) exit(1);
60
 
61
    fprintf(stderr,"%s: Listening on port %d (flash port %d)\n",
62
            programName,listenPort,flashPort);
63
 
64
    while (True) {
65
 
66
        /* reap any zombies */
67
        int status, pid;
68
        while ((pid= wait3(&status, WNOHANG, (struct rusage *)0))>0);
69
 
70
        /* discard any X events */
71
        while (XCheckIfEvent(d, &ev, AllXEventsPredicate, NULL))
72
            ;
73
 
74
        FD_ZERO(&fds);
75
 
76
        FD_SET(flashSocket, &fds);
77
        FD_SET(listenSocket, &fds);
78
        FD_SET(ConnectionNumber(d), &fds);
79
 
80
        select(FD_SETSIZE, &fds, NULL, NULL, NULL);
81
 
82
        if (FD_ISSET(flashSocket, &fds)) {
83
 
84
            sock = AcceptTcpConnection(flashSocket);
85
            if (sock < 0) exit(1);
86
            n = read(sock, flashUser, 255);
87
            if (n > 0) {
88
                flashUser[n] = 0;
89
                flashDisplay(d, flashUser);
90
            } else {
91
                flashDisplay(d, NULL);
92
            }
93
            close(sock);
94
        }
95
 
96
        if (FD_ISSET(listenSocket, &fds)) {
97
            rfbsock = AcceptTcpConnection(listenSocket);
98
            if (rfbsock < 0) exit(1);
99
 
100
            XCloseDisplay(d);
101
 
102
            /* Now fork off a new process to deal with it... */
103
 
104
            switch (fork()) {
105
 
106
            case -1:
107
                perror("fork");
108
                exit(1);
109
 
110
            case 0:
111
                /* child - return to caller */
112
                close(listenSocket);
113
                close(flashSocket);
114
                return;
115
 
116
            default:
117
                /* parent - go round and listen again */
118
                close(rfbsock);
119
                if (!(d = XOpenDisplay(displayname))) {
120
                    fprintf(stderr,"%s: unable to open display \"%s\"\r\n",
121
                            programName, XDisplayName (displayname));
122
                    exit(1);
123
                }
124
                getFlashFont(d);
125
                break;
126
            }
127
        }
128
    }
129
}
130
 
131
 
132
/*
133
 * getFlashFont
134
 */
135
 
136
static void
137
getFlashFont(Display *d)
138
{
139
    char fontName[256];
140
    char **fontNames;
141
    int nFontNames;
142
 
143
    sprintf(fontName,"-*-courier-bold-r-*-*-%d-*-*-*-*-*-iso8859-1",
144
            FLASHWIDTH);
145
    fontNames = XListFonts(d, fontName, 1, &nFontNames);
146
    if (nFontNames == 1) {
147
        XFreeFontNames(fontNames);
148
    } else {
149
        sprintf(fontName,"fixed");
150
    }
151
    flashFont = XLoadFont(d, fontName);
152
}
153
 
154
 
155
/*
156
 * flashDisplay
157
 */
158
 
159
static void
160
flashDisplay(Display *d, char *user)
161
{
162
    Window w1, w2, w3, w4;
163
    XSetWindowAttributes attr;
164
 
165
    XBell(d, 100);
166
 
167
    XForceScreenSaver(d, ScreenSaverReset);
168
 
169
    attr.background_pixel = BlackPixel(d, DefaultScreen(d));
170
    attr.override_redirect = 1;
171
    attr.save_under = True;
172
 
173
    w1 = XCreateWindow(d, DefaultRootWindow(d), 0, 0,
174
                       WidthOfScreen(DefaultScreenOfDisplay(d)),
175
                       FLASHWIDTH, 0,
176
                       CopyFromParent, CopyFromParent, CopyFromParent,
177
                       CWBackPixel|CWOverrideRedirect|CWSaveUnder,
178
                       &attr);
179
 
180
    w2 = XCreateWindow(d, DefaultRootWindow(d), 0, 0, FLASHWIDTH,
181
                       HeightOfScreen(DefaultScreenOfDisplay(d)), 0,
182
                       CopyFromParent, CopyFromParent, CopyFromParent,
183
                       CWBackPixel|CWOverrideRedirect|CWSaveUnder,
184
                       &attr);
185
 
186
    w3 = XCreateWindow(d, DefaultRootWindow(d),
187
                       WidthOfScreen(DefaultScreenOfDisplay(d))-FLASHWIDTH,
188
                       0, FLASHWIDTH,
189
                       HeightOfScreen(DefaultScreenOfDisplay(d)), 0,
190
                       CopyFromParent, CopyFromParent, CopyFromParent,
191
                       CWBackPixel|CWOverrideRedirect|CWSaveUnder,
192
                       &attr);
193
 
194
    w4 = XCreateWindow(d, DefaultRootWindow(d), 0,
195
                       HeightOfScreen(DefaultScreenOfDisplay(d))-FLASHWIDTH,
196
                       WidthOfScreen(DefaultScreenOfDisplay(d)),
197
                       FLASHWIDTH, 0,
198
                       CopyFromParent, CopyFromParent, CopyFromParent,
199
                       CWBackPixel|CWOverrideRedirect|CWSaveUnder,
200
                       &attr);
201
 
202
    XMapWindow(d, w1);
203
    XMapWindow(d, w2);
204
    XMapWindow(d, w3);
205
    XMapWindow(d, w4);
206
 
207
    if (user) {
208
        GC gc;
209
        XGCValues gcv;
210
 
211
        gcv.foreground = WhitePixel(d, DefaultScreen(d));
212
        gcv.font = flashFont;
213
        gc = XCreateGC(d, w1, GCForeground|GCFont, &gcv);
214
        XDrawString(d, w1, gc,
215
                    WidthOfScreen(DefaultScreenOfDisplay(d)) / 2 - FLASHWIDTH,
216
                    (FLASHWIDTH * 3 / 4), user, strlen(user));
217
    }
218
    XFlush(d);
219
 
220
    sleep(FLASHDELAY);
221
 
222
    XDestroyWindow(d, w1);
223
    XDestroyWindow(d, w2);
224
    XDestroyWindow(d, w3);
225
    XDestroyWindow(d, w4);
226
    XFlush(d);
227
}

powered by: WebSVN 2.1.0

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