not quite so much needs to be delayed to the init() function
[personal-kdebase.git] / workspace / kdm / backend / dpylist.c
blob50690e8dc4b41bf81e01c20c92b8077255a23c23
1 /*
3 Copyright 1988, 1998 The Open Group
4 Copyright 2000-2005 Oswald Buddenhagen <ossi@kde.org>
6 Permission to use, copy, modify, distribute, and sell this software and its
7 documentation for any purpose is hereby granted without fee, provided that
8 the above copyright notice appear in all copies and that both that
9 copyright notice and this permission notice appear in supporting
10 documentation.
12 The above copyright notice and this permission notice shall be included
13 in all copies or substantial portions of the Software.
15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
16 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18 IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
19 OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
21 OTHER DEALINGS IN THE SOFTWARE.
23 Except as contained in this notice, the name of a copyright holder shall
24 not be used in advertising or otherwise to promote the sale, use or
25 other dealings in this Software without prior written authorization
26 from the copyright holder.
31 * xdm - display manager daemon
32 * Author: Keith Packard, MIT X Consortium
34 * a simple linked list of known displays
37 #include "dm.h"
38 #include "dm_error.h"
40 struct display *displays;
41 static struct disphist *disphist;
43 int
44 anyDisplaysLeft( void )
46 return displays != (struct display *)0;
49 int
50 anyRunningDisplays( void )
52 struct display *d;
54 for (d = displays; d; d = d->next)
55 switch (d->status) {
56 case notRunning:
57 case textMode:
58 case reserve:
59 break;
60 default:
61 return True;
63 return False;
66 int
67 anyReserveDisplays( void )
69 struct display *d;
71 for (d = displays; d; d = d->next)
72 if ((d->displayType & d_lifetime) == dReserve)
73 return True;
74 return False;
77 int
78 idleReserveDisplays( void )
80 struct display *d;
81 int cnt = 0;
83 for (d = displays; d; d = d->next)
84 if (d->status == reserve)
85 cnt++;
86 return cnt;
89 int
90 startReserveDisplay( int lt )
92 struct display *d, *rd;
94 for (rd = 0, d = displays; d; d = d->next)
95 if (d->status == reserve)
96 rd = d;
97 if (rd) {
98 rd->idleTimeout = lt;
99 rd->status = notRunning;
100 return True;
102 return False;
105 void
106 forEachDisplay( void (*f)( struct display * ) )
108 struct display *d, *next;
110 for (d = displays; d; d = next) {
111 next = d->next;
112 (*f)( d );
116 #ifdef HAVE_VTS
117 static void
118 _forEachDisplayRev( struct display *d, void (*f)( struct display * ) )
120 if (d) {
121 if (d->next)
122 _forEachDisplayRev( d->next, f );
123 (*f)( d );
127 void
128 forEachDisplayRev( void (*f)( struct display * ) )
130 _forEachDisplayRev( displays, f );
132 #endif
134 struct display *
135 findDisplayByName( const char *name )
137 struct display *d;
139 for (d = displays; d; d = d->next)
140 if (!strcmp( name, d->name ))
141 return d;
142 return 0;
145 struct display *
146 findDisplayByPid( int pid )
148 struct display *d;
150 for (d = displays; d; d = d->next)
151 if (pid == d->pid)
152 return d;
153 return 0;
156 struct display *
157 findDisplayByServerPid( int serverPid )
159 struct display *d;
161 for (d = displays; d; d = d->next)
162 if (serverPid == d->serverPid)
163 return d;
164 return 0;
167 #ifdef XDMCP
169 struct display *
170 findDisplayBySessionID( CARD32 sessionID )
172 struct display *d;
174 for (d = displays; d; d = d->next)
175 if (sessionID == d->sessionID)
176 return d;
177 return 0;
180 struct display *
181 findDisplayByAddress( XdmcpNetaddr addr, int addrlen, CARD16 displayNumber )
183 struct display *d;
185 for (d = displays; d; d = d->next)
186 if ((d->displayType & d_origin) == dFromXDMCP &&
187 d->displayNumber == displayNumber &&
188 addressEqual( (XdmcpNetaddr)d->from.data, d->from.length,
189 addr, addrlen ))
190 return d;
191 return 0;
194 #endif /* XDMCP */
196 #define IfFree(x) if (x) free( (char *)x )
198 void
199 removeDisplay( struct display *old )
201 struct display *d, **dp;
202 int i;
204 for (dp = &displays; (d = *dp); dp = &(*dp)->next) {
205 if (d == old) {
206 debug( "Removing display %s\n", d->name );
207 *dp = d->next;
208 IfFree( d->class2 );
209 IfFree( d->cfg.data );
210 delStr( d->cfg.dep.name );
211 #ifdef XDMCP
212 IfFree( d->remoteHost );
213 #endif
214 if (d->authorizations) {
215 for (i = 0; i < d->authNum; i++)
216 XauDisposeAuth( d->authorizations[i] );
217 free( (char *)d->authorizations );
219 if (d->authFile) {
220 (void)unlink( d->authFile );
221 free( d->authFile );
223 IfFree( d->authNameLens );
224 #ifdef XDMCP
225 XdmcpDisposeARRAY8( &d->peer );
226 XdmcpDisposeARRAY8( &d->from );
227 XdmcpDisposeARRAY8( &d->clientAddr );
228 #endif
229 free( (char *)d );
230 break;
235 static struct disphist *
236 findHist( const char *name )
238 struct disphist *hstent;
240 for (hstent = disphist; hstent; hstent = hstent->next)
241 if (!strcmp( hstent->name, name ))
242 return hstent;
243 return 0;
246 struct display *
247 newDisplay( const char *name )
249 struct display *d;
250 struct disphist *hstent;
252 if (!(hstent = findHist( name ))) {
253 if (!(hstent = Calloc( 1, sizeof(*hstent) )))
254 return 0;
255 if (!strDup( &hstent->name, name )) {
256 free( hstent );
257 return 0;
259 hstent->next = disphist; disphist = hstent;
262 if (!(d = (struct display *)Calloc( 1, sizeof(*d) )))
263 return 0;
264 d->next = displays;
265 d->hstent = hstent;
266 d->name = hstent->name;
267 /* initialize fields (others are 0) */
268 d->pid = -1;
269 d->serverPid = -1;
270 d->ctrl.fd = -1;
271 d->pipe.fd.r = -1;
272 d->gpipe.fd.r = -1;
273 #ifndef SINGLE_PIPE
274 d->pipe.fd.w = -1;
275 d->gpipe.fd.w = -1;
276 #endif
277 d->userSess = -1;
278 #ifdef XDMCP
279 d->xdmcpFd = -1;
280 #endif
281 displays = d;
282 debug( "created new display %s\n", d->name );
283 return d;
286 const char *
287 displayName( struct display *d )
289 return memcmp( d->name, "localhost:", 10 ) ? d->name : d->name + 9;