2 * CDE - Common Desktop Environment
4 * Copyright (c) 1993-2012, The Open Group. All rights reserved.
6 * These libraries and programs are free software; you can
7 * redistribute them and/or modify them under the terms of the GNU
8 * Lesser General Public License as published by the Free Software
9 * Foundation; either version 2 of the License, or (at your option)
12 * These libraries and programs are distributed in the hope that
13 * they will be useful, but WITHOUT ANY WARRANTY; without even the
14 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU Lesser General Public License for more
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with these libraries and programs; if not, write
20 * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21 * Floor, Boston, MA 02110-1301 USA
25 "$XConsortium: SrvFile_io.c /main/8 1996/09/25 09:41:53 barstow $";
28 * (c) Copyright 1993, 1994 Hewlett-Packard Company *
29 * (c) Copyright 1993, 1994 International Business Machines Corp. *
30 * (c) Copyright 1993, 1994 Sun Microsystems, Inc. *
31 * (c) Copyright 1993, 1994 Novell, Inc. *
34 /*****************************************************************************
35 *****************************************************************************
39 ** Project: HP DT Style Manager (integrated into dtsession)
43 ** This file initializes the user specified ( or default) palette
46 *******************************************************************
47 ** (c) Copyright Hewlett-Packard Company, 1990. All rights are
48 ** reserved. Copying or other reproduction of this program
49 ** except for archival purposes is prohibited without prior
50 ** written consent of Hewlett-Packard Company.
51 ********************************************************************
55 *****************************************************************************
56 *************************************<+>*************************************/
61 #include "SrvFile_io.h"
67 #define MSG2 ((char *)GETMESSAGE(28, 3, "Color Server Warning, the size of file is invalid: "))
68 #define MSG3 ((char *)GETMESSAGE(28, 4, "Color Server Warning, invalid information in '"))
69 #define MSG3a ((char *)GETMESSAGE(28, 5, "' removing file and starting again.\n"))
71 /* Static Function Declarations */
74 static Boolean FindPalette(
77 static struct _palette* ReadPaletteFile(
82 static int ParsePaletteInfo(
87 struct _palette *new_palette) ;
88 static void InitializeBW(
91 struct _palette *new_palette) ;
93 /* static variables */
95 static int error_value;
98 #define USER_PALETTE_DIR "/.dt/palettes"
99 #define SYSTEM_PALETTE_DIR CDE_INSTALLATION_TOP "/palettes"
100 #define CONFIG_PALETTE_DIR CDE_CONFIGURATION_TOP "/palettes"
101 #define DEFAULT_PALETTE "Default.dp"
104 /************************************************************************
106 ** GetPaletteDefinition -
107 ** Query the database for the Dtstyle.paletteDirectories resource
108 ** Use Dtstyle default (Default) if not specified.
109 ** Search the directories for the palette in reverse order
111 ************************************************************************/
113 GetPaletteDefinition(
119 struct _palette *paletteDef;
120 char *str_type_return;
121 XrmValue value_return;
123 Boolean match = False;
131 if (FindPalette (palette, SYSTEM_PALETTE_DIR))
134 * First look for palette in the system location
136 palettePath = (char *) SRV_MALLOC (strlen(SYSTEM_PALETTE_DIR) + 1 );
137 strcpy(palettePath, SYSTEM_PALETTE_DIR);
140 else if (FindPalette (palette, CONFIG_PALETTE_DIR))
143 * Next check the local config location
145 palettePath = (char *) SRV_MALLOC (strlen(CONFIG_PALETTE_DIR) + 1 );
146 strcpy(palettePath, CONFIG_PALETTE_DIR);
154 /* Get Dtstyle.paletteDirectories value */
155 db = XtDatabase(dpy);
156 if (XrmGetResource (db, "dtstyle.paletteDirectories",
157 "Dtstyle.PaletteDirectories",
158 &str_type_return, &value_return))
160 /* Make Local Copy of string */
161 path = (char *) SRV_MALLOC( value_return.size + 1 );
162 strcpy (path, value_return.addr);
169 /* Look for palette in paletteDirectories */
172 /* Loop through paletteDirectories looking in each directory
173 * till we find the palette file. Take first occurrence.
174 * Copy directory name into dir. Look for NULL or space
181 while (*p != ' ' && *p != '\0')
184 if (FindPalette (palette, dir))
186 palettePath = (char *)SRV_REALLOC(palettePath,
187 strlen(SYSTEM_PALETTE_DIR) + 1);
188 strcpy(palettePath, dir);
195 /* Look for palette in $HOME/.dt/palettes */
196 /* If there is a duplicate, take it */
198 if ((home=getenv(HOME)) == NULL)
200 path = (char *) SRV_REALLOC (path,
201 strlen(home) + strlen(USER_PALETTE_DIR) + 1);
203 strcat(path, USER_PALETTE_DIR);
205 if (FindPalette (palette, path))
207 palettePath = (char *) SRV_REALLOC (palettePath, strlen(path) + 1 );
208 strcpy(palettePath, path);
214 /* Parse the data from the palette file */
215 paletteDef = (struct _palette *) ReadPaletteFile(dpy,
220 else /* default to system Default */
222 palettePath = (char *) SRV_REALLOC (palettePath,
223 strlen(SYSTEM_PALETTE_DIR) + 1);
224 strcpy(palettePath, SYSTEM_PALETTE_DIR);
225 paletteDef = (struct _palette *)ReadPaletteFile(dpy,
232 SRV_FREE(palettePath);
238 /************************************************************************
241 ** Open the directory and look for the palette file.
242 ** If found, read in data and return true.
243 ** If not found, return false
245 ************************************************************************/
253 struct dirent *file_descpt;
255 /* Open the directory */
256 if( (dirp = opendir(directory)) == NULL)
262 file_descpt = readdir(dirp);
265 /* cycle through the files in the directory until found a match */
266 while( file_descpt != NULL)
268 /* check for a palette filename match */
269 if (strcmp(palette, file_descpt->d_name) == 0)
276 /* read the next file */
277 file_descpt = readdir(dirp);
279 } /* while( file_descpt != NULL) */
285 /***************************************************************************
287 * ReadPaletteFile - this routines reads and parses the palette file.
288 * It fills in the pCurrentPalette structure for the screen_number
289 * that was passed in.
291 *************************************************************************/
292 static struct _palette *
299 struct _palette *new_palette;
302 char tmpPalette[BUFSIZE];
309 * create full path, don't forget to add 1 for the NULL byte
310 * and 1 for the slash in the format string for sprintf.
312 fullPath = (char *) SRV_MALLOC (strlen(palettePath) + strlen(palette) + 2);
313 sprintf(fullPath,"%s/%s", palettePath, palette);
315 if( (fd = open( fullPath, O_RDONLY)) == -1)
317 printf("error opening %s\n",fullPath);
319 return((struct _palette *) NULL);
323 ** Read a buffer of data ... BUFSIZE == 1024, as long as
324 ** screen < MAX_NUM_SCREENS (5) then we should never need more.
326 nbytes = read(fd, buf, BUFSIZE);
327 if(nbytes == 0 || nbytes == BUFSIZE) /* A bogus number of bytes */
330 * Don't forget to add 1 for the NULL byte and 2 for the
331 * period and the newline in the format string for sprintf
333 tmpStr = (char *)SRV_MALLOC(strlen(MSG2) + strlen(fullPath) + 3);
334 sprintf(tmpStr,"%s%s.\n", MSG2, palettePath);
335 _DtSimpleError(XmSCOLOR_SRV_NAME, DtWarning, NULL, tmpStr, NULL);
339 return((struct _palette *) NULL);
342 { /* read suceeded */
343 /* Allocate space for this new palette. */
344 new_palette = (palettes *)SRV_MALLOC( sizeof(struct _palette) + 1 );
346 /* allocate enough space for the name */
347 strcpy(tmpPalette, palette);
348 for (token1=tmpPalette; *token1; token1++);
349 while (token1!=tmpPalette && *token1!='.') token1--;
350 if (!strcmp(token1,PALETTE_SUFFIX)) *token1 = '\0';
351 new_palette->name = (char *)SRV_MALLOC(strlen(tmpPalette) + 1);
352 strcpy(new_palette->name, (char *) tmpPalette);
354 new_palette->converted = NULL;
355 new_palette->converted_len = 0L;
357 /* set the next pointer to NULL*/
358 new_palette->next = NULL;
360 if (ParsePaletteInfo(dpy, screen_number, buf, nbytes, new_palette) == -1)
362 /* palette file is bad */
363 tmpStr = (char *)SRV_MALLOC(strlen(MSG3) + strlen(MSG3a) + strlen(palettePath) +1);
364 tmpStr2 = (char *)SRV_MALLOC(strlen(MSG3)+1);
365 sprintf(tmpStr2,"%s", MSG3);
366 sprintf(tmpStr,"%s%s%s", tmpStr2, palettePath, MSG3a);
367 _DtSimpleError(XmSCOLOR_SRV_NAME, DtWarning, NULL, tmpStr, NULL);
374 return((struct _palette *) NULL);
385 /***********************************************************************
387 * ParsePaletteInfo - This routine reads from the buffer(buf) the
388 * actual data into the new_palette. It reads in the bg colors
389 * then uses the XmCalculateColorRGB to generate the ts, bs, and sc
390 * colors. This routine doesn't allocate any pixel numbers but
391 * does generate the RGB values for each color in a palette.
393 ***********************************************************************/
400 struct _palette *new_palette )
402 char tmpbuf[BUFSIZE];
408 static XmColorProc calcRGB = NULL;
410 num_of_colors = new_palette->num_of_colors = 0;
412 if(colorSrv.TypeOfMonitor[screen_num] != XmCO_BLACK_WHITE)
413 if((strcmp(new_palette->name, W_O_B)) == 0 ||
414 (strcmp(new_palette->name, B_O_W)) == 0 ||
415 (strcmp(new_palette->name, W_ONLY)) == 0 ||
416 (strcmp(new_palette->name, B_ONLY)) == 0)
420 /* read in background colors until end of file */
421 while(buf_count < nbytes && buf[buf_count] != '!')
425 /* read in a BG color */
426 while(buf[buf_count] != '\012')
427 tmpbuf[count++] = buf[buf_count++];
428 tmpbuf[count++] = '\0';
431 /* get the RGB value (XColor) of the background */
432 result = XParseColor(dpy, DefaultColormap(dpy, screen_num),
434 /* there was an error */
438 new_palette->color[num_of_colors].bg.red = tmp_color.red;
439 new_palette->color[num_of_colors].bg.blue = tmp_color.blue;
440 new_palette->color[num_of_colors].bg.green = tmp_color.green;
443 ** Now lets generate all the colors which go along with this bg i.e. ts,
446 if (calcRGB == NULL) calcRGB = XmGetColorCalculation();
447 (*calcRGB)(&tmp_color,
448 &(new_palette->color[num_of_colors].fg),
449 &(new_palette->color[num_of_colors].sc),
450 &(new_palette->color[num_of_colors].ts),
451 &(new_palette->color[num_of_colors].bs));
453 if(colorSrv.TypeOfMonitor[screen_num] == XmCO_BLACK_WHITE)
454 InitializeBW(tmp_color.red, num_of_colors, new_palette);
456 else /* Not Black and White */
458 if(colorSrv.UsePixmaps[screen_num] == TRUE)
460 /* the values generated by XmCalculateColorRBG are invalid */
461 new_palette->color[num_of_colors].ts.red = 65535;
462 new_palette->color[num_of_colors].ts.blue = 65535;
463 new_palette->color[num_of_colors].ts.green = 65535;
465 new_palette->color[num_of_colors].bs.red = 0;
466 new_palette->color[num_of_colors].bs.blue = 0;
467 new_palette->color[num_of_colors].bs.green = 0;
470 if(colorSrv.FgColor[screen_num] != DYNAMIC)
472 if(colorSrv.FgColor[screen_num] == BLACK)
474 new_palette->color[num_of_colors].fg.red = 0;
475 new_palette->color[num_of_colors].fg.blue = 0;
476 new_palette->color[num_of_colors].fg.green = 0;
478 else /* colorSrv.FgColor[screen_num] == WHITE */
480 new_palette->color[num_of_colors].fg.red = 65535;
481 new_palette->color[num_of_colors].fg.blue = 65535;
482 new_palette->color[num_of_colors].fg.green = 65535;
485 } /* else Not Black and White */
488 new_palette->num_of_colors++;
490 if(colorSrv.TypeOfMonitor[screen_num] == XmCO_BLACK_WHITE)
491 if( new_palette->num_of_colors == 2)
494 if(colorSrv.TypeOfMonitor[screen_num] == XmCO_LOW_COLOR ||
495 colorSrv.TypeOfMonitor[screen_num] == XmCO_MEDIUM_COLOR)
496 if( new_palette->num_of_colors == 4)
500 if(colorSrv.TypeOfMonitor[screen_num] == XmCO_LOW_COLOR)
502 new_palette->num_of_colors = 2;
503 SwitchAItoPS(new_palette);
508 /***********************************************************************
510 * InitializeBW - the type of monitor is a black and white, so
511 * initialize the colors to black and white. If the color passed
512 * in is 0 (black) set everything to 65535 (white). Otherwise
513 * set everything to 0 (black).
515 ***********************************************************************/
520 struct _palette *new_palette )
524 new_palette->color[num_of_colors].fg.red = 65535;
525 new_palette->color[num_of_colors].fg.blue = 65535;
526 new_palette->color[num_of_colors].fg.green = 65535;
528 new_palette->color[num_of_colors].ts.red = 65535;
529 new_palette->color[num_of_colors].ts.blue = 65535;
530 new_palette->color[num_of_colors].ts.green = 65535;
532 new_palette->color[num_of_colors].bs.red = 65535;
533 new_palette->color[num_of_colors].bs.blue = 65535;
534 new_palette->color[num_of_colors].bs.green = 65535;
536 new_palette->color[num_of_colors].sc.red = 0;
537 new_palette->color[num_of_colors].sc.blue = 0;
538 new_palette->color[num_of_colors].sc.green = 0;
542 new_palette->color[num_of_colors].fg.red = 0;
543 new_palette->color[num_of_colors].fg.blue = 0;
544 new_palette->color[num_of_colors].fg.green = 0;
546 new_palette->color[num_of_colors].ts.red = 0;
547 new_palette->color[num_of_colors].ts.blue = 0;
548 new_palette->color[num_of_colors].ts.green = 0;
550 new_palette->color[num_of_colors].bs.red = 0;
551 new_palette->color[num_of_colors].bs.blue = 0;
552 new_palette->color[num_of_colors].bs.green = 0;
554 new_palette->color[num_of_colors].sc.red = 65535;
555 new_palette->color[num_of_colors].sc.blue = 65535;
556 new_palette->color[num_of_colors].sc.green = 65535;
562 struct _palette *new_palette )
564 new_palette->color[0].bg.red = new_palette->color[3].bg.red;
565 new_palette->color[0].bg.green = new_palette->color[3].bg.green;
566 new_palette->color[0].bg.blue = new_palette->color[3].bg.blue;
568 new_palette->color[0].fg.red = new_palette->color[3].fg.red;
569 new_palette->color[0].fg.green = new_palette->color[3].fg.green;
570 new_palette->color[0].fg.blue = new_palette->color[3].fg.blue;
572 new_palette->color[0].ts.red = new_palette->color[3].ts.red;
573 new_palette->color[0].ts.green = new_palette->color[3].ts.green;
574 new_palette->color[0].ts.blue = new_palette->color[3].ts.blue;
576 new_palette->color[0].bs.red = new_palette->color[3].bs.red;
577 new_palette->color[0].bs.green = new_palette->color[3].bs.green;
578 new_palette->color[0].bs.blue = new_palette->color[3].bs.blue;
580 new_palette->color[0].sc.red = new_palette->color[3].sc.red;
581 new_palette->color[0].sc.green = new_palette->color[3].sc.green;
582 new_palette->color[0].sc.blue = new_palette->color[3].sc.blue;
584 new_palette->color[1].bg.red = new_palette->color[2].bg.red;
585 new_palette->color[1].bg.green = new_palette->color[2].bg.green;
586 new_palette->color[1].bg.blue = new_palette->color[2].bg.blue;
588 new_palette->color[1].fg.red = new_palette->color[2].fg.red;
589 new_palette->color[1].fg.green = new_palette->color[2].fg.green;
590 new_palette->color[1].fg.blue = new_palette->color[2].fg.blue;
592 new_palette->color[1].ts.red = new_palette->color[2].ts.red;
593 new_palette->color[1].ts.green = new_palette->color[2].ts.green;
594 new_palette->color[1].ts.blue = new_palette->color[2].ts.blue;
596 new_palette->color[1].bs.red = new_palette->color[2].bs.red;
597 new_palette->color[1].bs.green = new_palette->color[2].bs.green;
598 new_palette->color[1].bs.blue = new_palette->color[2].bs.blue;
600 new_palette->color[1].sc.red = new_palette->color[2].sc.red;
601 new_palette->color[1].sc.green = new_palette->color[2].sc.green;
602 new_palette->color[1].sc.blue = new_palette->color[2].sc.blue;
606 /***********************************************************************
608 * SaveDefaultPalette - used to save the palette.dt file to
609 * either $HOME/.dt/$DISPLAY/current or $HOME/.dt/$DISPLAY/home.
610 * The parameter mode determines whether it is home or
613 ***********************************************************************/