1 /***********************************************************
3 Copyright (c) 1987 X Consortium
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:
12 The above copyright notice and this permission notice shall be included in
13 all copies or substantial portions of the Software.
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.
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.
27 Copyright 1987 by Digital Equipment Corporation, Maynard, Massachusetts.
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.
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
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 $ */
57 #include <rpcsvc/dbm.h>
66 /* Note that we are assuming there is only one database for all the screens. */
69 DBM *rgb_dbm = (DBM *)NULL;
74 extern void CopyISOLatin1Lowered();
82 rgb_dbm = dbm_open(rgbPath, 0, 0);
84 if (dbminit(rgbPath) == 0)
88 ErrorF( "Couldn't open RGB_DB '%s'\n", rgbPath );
97 OsLookupColor(screen, name, len, pred, pgreen, pblue)
101 unsigned short *pred, *pgreen, *pblue;
112 /* we use xalloc here so that we can compile with cc without alloca
113 * when otherwise using gcc */
114 if (len < sizeof(buf))
116 else if (!(lowername = (char *)xalloc(len + 1)))
118 CopyISOLatin1Lowered ((unsigned char *) lowername, (unsigned char *) name,
121 dbent.dptr = lowername;
124 dbent = dbm_fetch(rgb_dbm, dbent);
126 dbent = fetch (dbent);
129 if (len >= sizeof(buf))
134 memmove((char *) &rgb, dbent.dptr, sizeof (RGB));
143 #else /* USE_RGB_TXT */
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
158 typedef struct _dbEntry * dbEntryPtr;
159 typedef struct _dbEntry {
162 unsigned short green;
164 char name[1]; /* some compilers complain if [0] */
168 extern void CopyISOLatin1Lowered();
170 static dbEntryPtr hashTab[HASHSIZE];
174 lookup(name, len, create)
179 unsigned int h = 0, g;
180 dbEntryPtr entry, *prev;
183 if (!(name = (char*)ALLOCATE_LOCAL(len +1))) return NULL;
184 CopyISOLatin1Lowered(name, str, len);
187 for(str = name; *str; str++) {
189 if ((g = h) & 0xf0000000) h ^= (g >> 24);
194 if ( entry = hashTab[h] )
196 for( ; entry; prev = (dbEntryPtr*)entry, entry = entry->link )
197 if (! strcmp(name, entry->name) ) break;
200 prev = &(hashTab[h]);
202 if (!entry && create && (entry = (dbEntryPtr)xalloc(sizeof(dbEntry) +len)))
206 strcpy( entry->name, name );
209 DEALLOCATE_LOCAL(name);
222 int red, green, blue, lineno = 0;
225 static Bool was_here = FALSE;
230 path = (char*)ALLOCATE_LOCAL(strlen(rgbPath) +5);
231 strcpy(path, rgbPath);
232 strcat(path, ".txt");
234 char *tmp = (char*)__XOS2RedirRoot(rgbPath);
235 path = (char*)ALLOCATE_LOCAL(strlen(tmp) +5);
237 strcat(path, ".txt");
239 if (!(rgb = fopen(path, "r")))
241 ErrorF( "Couldn't open RGB_DB '%s'\n", rgbPath );
242 DEALLOCATE_LOCAL(path);
246 while(fgets(line, sizeof(line), rgb))
250 if (sscanf(line,"%d %d %d %[^\n]\n", &red, &green, &blue, name) == 4)
252 if (sscanf(line,"%d %d %d %[^\n\r]\n", &red, &green, &blue, name) == 4)
255 if (red >= 0 && red <= 0xff &&
256 green >= 0 && green <= 0xff &&
257 blue >= 0 && blue <= 0xff)
259 if (entry = lookup(name, strlen(name), TRUE))
261 entry->red = (red * 65535) / 255;
262 entry->green = (green * 65535) / 255;
263 entry->blue = (blue * 65535) / 255;
267 ErrorF("Value out of range: %s:%d\n", path, lineno);
269 else if (*line && *line != '#' && *line != '!')
270 ErrorF("Syntax Error: %s:%d\n", path, lineno);
274 DEALLOCATE_LOCAL(path);
285 OsLookupColor(screen, name, len, pred, pgreen, pblue)
289 unsigned short *pred, *pgreen, *pblue;
294 if (entry = lookup(name, len, FALSE))
297 *pgreen = entry->green;
298 *pblue = entry->blue;
305 #endif /* USE_RGB_TXT */