First import
[xorg_rtime.git] / xorg-server-1.4 / os / oscolor.c
blobe1756926d0d77e0c757bb027a33cd8582e13181b
1 /***********************************************************
3 Copyright 1987, 1998 The Open Group
5 Permission to use, copy, modify, distribute, and sell this software and its
6 documentation for any purpose is hereby granted without fee, provided that
7 the above copyright notice appear in all copies and that both that
8 copyright notice and this permission notice appear in supporting
9 documentation.
11 The above copyright notice and this permission notice shall be included in
12 all copies or substantial portions of the Software.
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
18 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
19 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21 Except as contained in this notice, the name of The Open Group shall not be
22 used in advertising or otherwise to promote the sale, use or other dealings
23 in this Software without prior written authorization from The Open Group.
26 Copyright 1987 by Digital Equipment Corporation, Maynard, Massachusetts.
28 All Rights Reserved
30 Permission to use, copy, modify, and distribute this software and its
31 documentation for any purpose and without fee is hereby granted,
32 provided that the above copyright notice appear in all copies and that
33 both that copyright notice and this permission notice appear in
34 supporting documentation, and that the name of Digital not be
35 used in advertising or publicity pertaining to distribution of the
36 software without specific, written prior permission.
38 DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
39 ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
40 DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
41 ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
42 WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
43 ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
44 SOFTWARE.
46 ******************************************************************/
48 #ifdef HAVE_DIX_CONFIG_H
49 #include <dix-config.h>
50 #endif
52 #define USE_RGB_BUILTIN 1
54 #if USE_RGB_BUILTIN
56 #include <X11/keysym.h>
57 #include "os.h"
59 static unsigned char
60 OsToLower (unsigned char a)
62 if ((a >= XK_A) && (a <= XK_Z))
63 return a + (XK_a - XK_A);
64 else if ((a >= XK_Agrave) && (a <= XK_Odiaeresis))
65 return a + (XK_agrave - XK_Agrave);
66 else if ((a >= XK_Ooblique) && (a <= XK_Thorn))
67 return a + (XK_oslash - XK_Ooblique);
68 else
69 return a;
72 static int
73 OsStrCaseCmp (const unsigned char *s1, const unsigned char *s2, int l2)
75 unsigned char c1, c2;
77 for (;;)
79 c1 = OsToLower (*s1++);
80 if (l2 == 0)
81 c2 = '\0';
82 else
83 c2 = OsToLower (*s2++);
84 if (!c1 || !c2)
85 break;
86 if (c1 != c2)
87 break;
88 l2--;
90 return c2 - c1;
93 typedef struct _builtinColor {
94 unsigned char red;
95 unsigned char green;
96 unsigned char blue;
97 unsigned short name;
98 } BuiltinColor;
100 #include "oscolor.h"
102 #define NUM_BUILTIN_COLORS (sizeof (BuiltinColors) / sizeof (BuiltinColors[0]))
104 Bool
105 OsInitColors(void)
107 return TRUE;
110 Bool
111 OsLookupColor(int screen,
112 char *s_name,
113 unsigned int len,
114 unsigned short *pred,
115 unsigned short *pgreen,
116 unsigned short *pblue)
118 const BuiltinColor *c;
119 unsigned char *name = (unsigned char *) s_name;
120 int low, mid, high;
121 int r;
123 low = 0;
124 high = NUM_BUILTIN_COLORS - 1;
125 while (high >= low)
127 mid = (low + high) / 2;
128 c = &BuiltinColors[mid];
129 r = OsStrCaseCmp (&BuiltinColorNames[c->name], name, len);
130 if (r == 0)
132 *pred = c->red * 0x101;
133 *pgreen = c->green * 0x101;
134 *pblue = c->blue * 0x101;
135 return TRUE;
137 if (r < 0)
138 high = mid - 1;
139 else
140 low = mid + 1;
142 return FALSE;
145 #else
148 * This file builds the server's internal database mapping color names to
149 * RGB tuples by reading in an rgb.txt file. This is still slightly foolish,
150 * rgb.txt hasn't changed in years, we should really include a precompiled
151 * version into the server.
154 #include <stdio.h>
155 #include "os.h"
156 #include "opaque.h"
158 #define HASHSIZE 63
160 typedef struct _dbEntry * dbEntryPtr;
161 typedef struct _dbEntry {
162 dbEntryPtr link;
163 unsigned short red;
164 unsigned short green;
165 unsigned short blue;
166 char name[1]; /* some compilers complain if [0] */
167 } dbEntry;
169 extern void CopyISOLatin1Lowered(
170 unsigned char * /*dest*/,
171 unsigned char * /*source*/,
172 int /*length*/);
174 static dbEntryPtr hashTab[HASHSIZE];
176 static dbEntryPtr
177 lookup(char *name, int len, Bool create)
179 unsigned int h = 0, g;
180 dbEntryPtr entry, *prev = NULL;
181 char *str = name;
183 if (!(name = (char*)ALLOCATE_LOCAL(len +1))) return NULL;
184 CopyISOLatin1Lowered((unsigned char *)name, (unsigned char *)str, len);
185 name[len] = '\0';
187 for(str = name; *str; str++) {
188 h = (h << 4) + *str;
189 if ((g = h) & 0xf0000000) h ^= (g >> 24);
190 h &= g;
192 h %= HASHSIZE;
194 if ( (entry = hashTab[h]) )
196 for( ; entry; prev = (dbEntryPtr*)entry, entry = entry->link )
197 if (! strcmp(name, entry->name) ) break;
199 else
200 prev = &(hashTab[h]);
202 if (!entry && create && (entry = (dbEntryPtr)xalloc(sizeof(dbEntry) +len)))
204 *prev = entry;
205 entry->link = NULL;
206 strcpy( entry->name, name );
209 DEALLOCATE_LOCAL(name);
211 return entry;
214 Bool
215 OsInitColors(void)
217 FILE *rgb;
218 char *path;
219 char line[BUFSIZ];
220 char name[BUFSIZ];
221 int red, green, blue, lineno = 0;
222 dbEntryPtr entry;
224 static Bool was_here = FALSE;
226 if (!was_here)
228 path = (char*)ALLOCATE_LOCAL(strlen(rgbPath) +5);
229 strcpy(path, rgbPath);
230 strcat(path, ".txt");
231 if (!(rgb = fopen(path, "r")))
233 ErrorF( "Couldn't open RGB_DB '%s'\n", rgbPath );
234 DEALLOCATE_LOCAL(path);
235 return FALSE;
238 while(fgets(line, sizeof(line), rgb))
240 lineno++;
241 if (sscanf(line,"%d %d %d %[^\n]\n", &red, &green, &blue, name) == 4)
243 if (red >= 0 && red <= 0xff &&
244 green >= 0 && green <= 0xff &&
245 blue >= 0 && blue <= 0xff)
247 if ((entry = lookup(name, strlen(name), TRUE)))
249 entry->red = (red * 65535) / 255;
250 entry->green = (green * 65535) / 255;
251 entry->blue = (blue * 65535) / 255;
254 else
255 ErrorF("Value out of range: %s:%d\n", path, lineno);
257 else if (*line && *line != '#' && *line != '!')
258 ErrorF("Syntax Error: %s:%d\n", path, lineno);
261 fclose(rgb);
262 DEALLOCATE_LOCAL(path);
264 was_here = TRUE;
266 return TRUE;
269 Bool
270 OsLookupColor(int screen, char *name, unsigned int len,
271 unsigned short *pred, unsigned short *pgreen, unsigned short *pblue)
273 dbEntryPtr entry;
275 if ((entry = lookup(name, len, FALSE)))
277 *pred = entry->red;
278 *pgreen = entry->green;
279 *pblue = entry->blue;
280 return TRUE;
283 return FALSE;
286 #endif /* USE_RGB_BUILTIN */