]> git.sesse.net Git - rdpsrv/blob - Xserver/programs/Xserver/os/oscolor.c
Import X server from vnc-3.3.7.
[rdpsrv] / Xserver / programs / Xserver / os / oscolor.c
1 /***********************************************************
2
3 Copyright (c) 1987  X Consortium
4
5 Permission is hereby granted, free of charge, to any person obtaining a copy
6 of this software and associated documentation files (the "Software"), to deal
7 in the Software without restriction, including without limitation the rights
8 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 copies of the Software, and to permit persons to whom the Software is
10 furnished to do so, subject to the following conditions:
11
12 The above copyright notice and this permission notice shall be included in
13 all copies or substantial portions of the Software.
14
15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
18 X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
19 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21
22 Except as contained in this notice, the name of the X Consortium shall not be
23 used in advertising or otherwise to promote the sale, use or other dealings
24 in this Software without prior written authorization from the X Consortium.
25
26
27 Copyright 1987 by Digital Equipment Corporation, Maynard, Massachusetts.
28
29                         All Rights Reserved
30
31 Permission to use, copy, modify, and distribute this software and its 
32 documentation for any purpose and without fee is hereby granted, 
33 provided that the above copyright notice appear in all copies and that
34 both that copyright notice and this permission notice appear in 
35 supporting documentation, and that the name of Digital not be
36 used in advertising or publicity pertaining to distribution of the
37 software without specific, written prior permission.  
38
39 DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
40 ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
41 DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
42 ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
43 WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
44 ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
45 SOFTWARE.
46
47 ******************************************************************/
48 /* $XConsortium: oscolor.c,v 1.23 94/04/17 20:27:04 dpw Exp $ */
49 /* $XFree86: xc/programs/Xserver/os/oscolor.c,v 3.2.4.1 1998/01/22 10:47:14 dawes Exp $ */
50
51 #ifndef USE_RGB_TXT
52
53 #ifdef NDBM
54 #include <ndbm.h>
55 #else
56 #ifdef SVR4
57 #include <rpcsvc/dbm.h>
58 #else
59 #include <dbm.h>
60 #endif
61 #endif
62 #include "rgb.h"
63 #include "os.h"
64 #include "opaque.h"
65
66 /* Note that we are assuming there is only one database for all the screens. */
67
68 #ifdef NDBM
69 DBM *rgb_dbm = (DBM *)NULL;
70 #else
71 int rgb_dbm = 0;
72 #endif
73
74 extern void CopyISOLatin1Lowered();
75
76 int
77 OsInitColors()
78 {
79     if (!rgb_dbm)
80     {
81 #ifdef NDBM
82         rgb_dbm = dbm_open(rgbPath, 0, 0);
83 #else
84         if (dbminit(rgbPath) == 0)
85             rgb_dbm = 1;
86 #endif
87         if (!rgb_dbm) {
88             ErrorF( "Couldn't open RGB_DB '%s'\n", rgbPath );
89             return FALSE;
90         }
91     }
92     return TRUE;
93 }
94
95 /*ARGSUSED*/
96 int
97 OsLookupColor(screen, name, len, pred, pgreen, pblue)
98     int         screen;
99     char        *name;
100     unsigned    len;
101     unsigned short      *pred, *pgreen, *pblue;
102
103 {
104     datum               dbent;
105     RGB                 rgb;
106     char                buf[64];
107     char                *lowername;
108
109     if(!rgb_dbm)
110         return(0);
111
112     /* we use xalloc here so that we can compile with cc without alloca
113      * when otherwise using gcc */
114     if (len < sizeof(buf))
115         lowername = buf;
116     else if (!(lowername = (char *)xalloc(len + 1)))
117         return(0);
118     CopyISOLatin1Lowered ((unsigned char *) lowername, (unsigned char *) name,
119                           (int)len);
120
121     dbent.dptr = lowername;
122     dbent.dsize = len;
123 #ifdef NDBM
124     dbent = dbm_fetch(rgb_dbm, dbent);
125 #else
126     dbent = fetch (dbent);
127 #endif
128
129     if (len >= sizeof(buf))
130         xfree(lowername);
131
132     if(dbent.dptr)
133     {
134         memmove((char *) &rgb, dbent.dptr, sizeof (RGB));
135         *pred = rgb.red;
136         *pgreen = rgb.green;
137         *pblue = rgb.blue;
138         return (1);
139     }
140     return(0);
141 }
142
143 #else /* USE_RGB_TXT */
144
145
146 /*
147  * The dbm routines are a porting hassle. This implementation will do
148  * the same thing by reading the rgb.txt file directly, which is much
149  * more portable.
150  */
151
152 #include <stdio.h>
153 #include "os.h"
154 #include "opaque.h"
155
156 #define HASHSIZE 511
157
158 typedef struct _dbEntry * dbEntryPtr;
159 typedef struct _dbEntry {
160   dbEntryPtr     link;
161   unsigned short red;
162   unsigned short green;
163   unsigned short blue;
164   char           name[1];       /* some compilers complain if [0] */
165 } dbEntry;
166
167
168 extern void CopyISOLatin1Lowered();
169
170 static dbEntryPtr hashTab[HASHSIZE];
171
172
173 static dbEntryPtr
174 lookup(name, len, create)
175      char *name;
176      int  len;
177      Bool create;
178 {
179   unsigned int h = 0, g;
180   dbEntryPtr   entry, *prev;
181   char         *str = name;
182
183   if (!(name = (char*)ALLOCATE_LOCAL(len +1))) return NULL;
184   CopyISOLatin1Lowered(name, str, len);
185   name[len] = '\0';
186
187   for(str = name; *str; str++) {
188     h = (h << 4) + *str;
189     if ((g = h) & 0xf0000000) h ^= (g >> 24);
190     h &= g;
191   }
192   h %= HASHSIZE;
193
194   if ( entry = hashTab[h] )
195     {
196       for( ; entry; prev = (dbEntryPtr*)entry, entry = entry->link )
197         if (! strcmp(name, entry->name) ) break;
198     }
199   else
200     prev = &(hashTab[h]);
201
202   if (!entry && create && (entry = (dbEntryPtr)xalloc(sizeof(dbEntry) +len)))
203     {
204       *prev = entry;
205       entry->link = NULL;
206       strcpy( entry->name, name );
207     }
208
209   DEALLOCATE_LOCAL(name);
210
211   return entry;
212 }
213
214
215 Bool
216 OsInitColors()
217 {
218   FILE       *rgb;
219   char       *path;
220   char       line[BUFSIZ];
221   char       name[BUFSIZ];
222   int        red, green, blue, lineno = 0;
223   dbEntryPtr entry;
224
225   static Bool was_here = FALSE;
226
227   if (!was_here)
228     {
229 #ifndef __EMX__
230       path = (char*)ALLOCATE_LOCAL(strlen(rgbPath) +5);
231       strcpy(path, rgbPath);
232       strcat(path, ".txt");
233 #else
234       char *tmp = (char*)__XOS2RedirRoot(rgbPath);
235       path = (char*)ALLOCATE_LOCAL(strlen(tmp) +5);
236       strcpy(path, tmp);
237       strcat(path, ".txt");
238 #endif
239       if (!(rgb = fopen(path, "r")))
240         {
241            ErrorF( "Couldn't open RGB_DB '%s'\n", rgbPath );
242            DEALLOCATE_LOCAL(path);
243            return FALSE;
244         }
245
246       while(fgets(line, sizeof(line), rgb))
247         {
248           lineno++;
249 #ifndef __EMX__
250           if (sscanf(line,"%d %d %d %[^\n]\n", &red, &green, &blue, name) == 4)
251 #else
252           if (sscanf(line,"%d %d %d %[^\n\r]\n", &red, &green, &blue, name) == 4)
253 #endif
254             {
255               if (red >= 0   && red <= 0xff &&
256                   green >= 0 && green <= 0xff &&
257                   blue >= 0  && blue <= 0xff)
258                 {
259                   if (entry = lookup(name, strlen(name), TRUE))
260                     {
261                       entry->red   = (red * 65535)   / 255;
262                       entry->green = (green * 65535) / 255;
263                       entry->blue  = (blue  * 65535) / 255;
264                     }
265                 }
266               else
267                 ErrorF("Value out of range: %s:%d\n", path, lineno);
268             }
269           else if (*line && *line != '#' && *line != '!')
270             ErrorF("Syntax Error: %s:%d\n", path, lineno);
271         }
272       
273       fclose(rgb);
274       DEALLOCATE_LOCAL(path);
275
276       was_here = TRUE;
277     }
278
279   return TRUE;
280 }
281
282
283
284 Bool
285 OsLookupColor(screen, name, len, pred, pgreen, pblue)
286     int            screen;
287     char           *name;
288     unsigned       len;
289     unsigned short *pred, *pgreen, *pblue;
290
291 {
292   dbEntryPtr entry;
293
294   if (entry = lookup(name, len, FALSE))
295     {
296       *pred   = entry->red;
297       *pgreen = entry->green;
298       *pblue  = entry->blue;
299       return TRUE;
300     }
301
302   return FALSE;
303 }
304
305 #endif /* USE_RGB_TXT */