SrvFile_io.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620
  1. /*
  2. * CDE - Common Desktop Environment
  3. *
  4. * Copyright (c) 1993-2012, The Open Group. All rights reserved.
  5. *
  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)
  10. * any later version.
  11. *
  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
  16. * details.
  17. *
  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
  22. */
  23. /*
  24. static char rcsid[] =
  25. "$XConsortium: SrvFile_io.c /main/8 1996/09/25 09:41:53 barstow $";
  26. */
  27. /* *
  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. *
  32. */
  33. /*****************************************************************************
  34. *****************************************************************************
  35. **
  36. ** File: SrvFile_io.c
  37. **
  38. ** Project: HP DT Style Manager (integrated into dtsession)
  39. **
  40. ** Description:
  41. ** -----------
  42. ** This file initializes the user specified ( or default) palette
  43. ** for this session.
  44. **
  45. *******************************************************************
  46. ** (c) Copyright Hewlett-Packard Company, 1990. All rights are
  47. ** reserved. Copying or other reproduction of this program
  48. ** except for archival purposes is prohibited without prior
  49. ** written consent of Hewlett-Packard Company.
  50. ********************************************************************
  51. **
  52. **
  53. **
  54. *****************************************************************************
  55. *************************************<+>*************************************/
  56. /* #includes */
  57. #include <fcntl.h>
  58. #include "Srv.h"
  59. #include "SrvFile_io.h"
  60. /* #defines */
  61. #define BUFSIZE 1024
  62. #define MSG2 ((char *)GETMESSAGE(28, 3, "Color Server Warning, the size of file is invalid: "))
  63. #define MSG3 ((char *)GETMESSAGE(28, 4, "Color Server Warning, invalid information in '"))
  64. #define MSG3a ((char *)GETMESSAGE(28, 5, "' removing file and starting again.\n"))
  65. /* Static Function Declarations */
  66. static Boolean FindPalette(
  67. char *palette,
  68. char *directory) ;
  69. static struct _palette* ReadPaletteFile(
  70. Display *dpy,
  71. int screen_number,
  72. char *palettePath,
  73. char *palette) ;
  74. static int ParsePaletteInfo(
  75. Display *dpy,
  76. int numDisplay,
  77. char *buf,
  78. int nbytes,
  79. struct _palette *new_palette) ;
  80. static void InitializeBW(
  81. unsigned long color,
  82. int num_of_colors,
  83. struct _palette *new_palette) ;
  84. /* static variables */
  85. static int error_value;
  86. #define HOME "HOME"
  87. #define USER_PALETTE_DIR "/.dt/palettes"
  88. #define SYSTEM_PALETTE_DIR CDE_INSTALLATION_TOP "/palettes"
  89. #define CONFIG_PALETTE_DIR CDE_CONFIGURATION_TOP "/palettes"
  90. #define DEFAULT_PALETTE "Default.dp"
  91. /************************************************************************
  92. **
  93. ** GetPaletteDefinition -
  94. ** Query the database for the Dtstyle.paletteDirectories resource
  95. ** Use Dtstyle default (Default) if not specified.
  96. ** Search the directories for the palette in reverse order
  97. **
  98. ************************************************************************/
  99. struct _palette *
  100. GetPaletteDefinition(
  101. Display *dpy,
  102. int screen_number,
  103. char *palette)
  104. {
  105. struct _palette *paletteDef;
  106. char *str_type_return;
  107. XrmValue value_return;
  108. XrmDatabase db;
  109. Boolean match = False;
  110. char *palettePath;
  111. char *path;
  112. char *p, *d;
  113. char *home;
  114. char dir[256];
  115. if (FindPalette (palette, SYSTEM_PALETTE_DIR))
  116. {
  117. /*
  118. * First look for palette in the system location
  119. */
  120. palettePath = (char *) SRV_MALLOC (strlen(SYSTEM_PALETTE_DIR) + 1 );
  121. strcpy(palettePath, SYSTEM_PALETTE_DIR);
  122. match = True;
  123. }
  124. else if (FindPalette (palette, CONFIG_PALETTE_DIR))
  125. {
  126. /*
  127. * Next check the local config location
  128. */
  129. palettePath = (char *) SRV_MALLOC (strlen(CONFIG_PALETTE_DIR) + 1 );
  130. strcpy(palettePath, CONFIG_PALETTE_DIR);
  131. match = True;
  132. }
  133. else
  134. {
  135. palettePath = NULL;
  136. }
  137. /* Get Dtstyle.paletteDirectories value */
  138. db = XtDatabase(dpy);
  139. if (XrmGetResource (db, "dtstyle.paletteDirectories",
  140. "Dtstyle.PaletteDirectories",
  141. &str_type_return, &value_return))
  142. {
  143. /* Make Local Copy of string */
  144. path = (char *) SRV_MALLOC( value_return.size + 1 );
  145. strcpy (path, value_return.addr);
  146. }
  147. else
  148. {
  149. path = NULL;
  150. }
  151. /* Look for palette in paletteDirectories */
  152. if (path != NULL)
  153. {
  154. /* Loop through paletteDirectories looking in each directory
  155. * till we find the palette file. Take first occurrence.
  156. * Copy directory name into dir. Look for NULL or space
  157. */
  158. p = path;
  159. while (*p != '\0')
  160. {
  161. d = dir;
  162. while (*p != ' ' && *p != '\0')
  163. *d++ = *p++;
  164. *d = '\0';
  165. if (FindPalette (palette, dir))
  166. {
  167. palettePath = (char *)SRV_REALLOC(palettePath,
  168. strlen(SYSTEM_PALETTE_DIR) + 1);
  169. strcpy(palettePath, dir);
  170. match = True;
  171. break;
  172. }
  173. }
  174. }
  175. /* Look for palette in $HOME/.dt/palettes */
  176. /* If there is a duplicate, take it */
  177. if ((home=getenv(HOME)) == NULL)
  178. home="";
  179. path = (char *) SRV_REALLOC (path,
  180. strlen(home) + strlen(USER_PALETTE_DIR) + 1);
  181. strcpy(path, home);
  182. strcat(path, USER_PALETTE_DIR);
  183. if (FindPalette (palette, path))
  184. {
  185. palettePath = (char *) SRV_REALLOC (palettePath, strlen(path) + 1 );
  186. strcpy(palettePath, path);
  187. match = True;
  188. }
  189. if (match)
  190. {
  191. /* Parse the data from the palette file */
  192. paletteDef = (struct _palette *) ReadPaletteFile(dpy,
  193. screen_number,
  194. palettePath,
  195. palette);
  196. }
  197. else /* default to system Default */
  198. {
  199. palettePath = (char *) SRV_REALLOC (palettePath,
  200. strlen(SYSTEM_PALETTE_DIR) + 1);
  201. strcpy(palettePath, SYSTEM_PALETTE_DIR);
  202. paletteDef = (struct _palette *)ReadPaletteFile(dpy,
  203. screen_number,
  204. palettePath,
  205. DEFAULT_PALETTE);
  206. }
  207. SRV_FREE(path);
  208. SRV_FREE(palettePath);
  209. return (paletteDef);
  210. }
  211. /************************************************************************
  212. **
  213. ** FindPalette -
  214. ** Open the directory and look for the palette file.
  215. ** If found, read in data and return true.
  216. ** If not found, return false
  217. **
  218. ************************************************************************/
  219. static Boolean
  220. FindPalette(
  221. char *palette,
  222. char *directory)
  223. {
  224. DIR *dirp;
  225. struct dirent *file_descpt;
  226. /* Open the directory */
  227. if( (dirp = opendir(directory)) == NULL)
  228. {
  229. return(False);
  230. }
  231. else
  232. {
  233. file_descpt = readdir(dirp);
  234. }
  235. /* cycle through the files in the directory until found a match */
  236. while( file_descpt != NULL)
  237. {
  238. /* check for a palette filename match */
  239. if (strcmp(palette, file_descpt->d_name) == 0)
  240. {
  241. closedir(dirp);
  242. return(True);
  243. }
  244. else
  245. {
  246. /* read the next file */
  247. file_descpt = readdir(dirp);
  248. }
  249. } /* while( file_descpt != NULL) */
  250. closedir(dirp);
  251. return (False);
  252. }
  253. /***************************************************************************
  254. *
  255. * ReadPaletteFile - this routines reads and parses the palette file.
  256. * It fills in the pCurrentPalette structure for the screen_number
  257. * that was passed in.
  258. *
  259. *************************************************************************/
  260. static struct _palette *
  261. ReadPaletteFile(
  262. Display *dpy,
  263. int screen_number,
  264. char *palettePath,
  265. char *palette )
  266. {
  267. struct _palette *new_palette;
  268. int fd, nbytes;
  269. char buf[BUFSIZE];
  270. char tmpPalette[BUFSIZE];
  271. char *token1;
  272. char *tmpStr2;
  273. char *tmpStr;
  274. char *fullPath;
  275. /*
  276. * create full path, don't forget to add 1 for the NULL byte
  277. * and 1 for the slash in the format string for sprintf.
  278. */
  279. fullPath = (char *) SRV_MALLOC (strlen(palettePath) + strlen(palette) + 2);
  280. sprintf(fullPath,"%s/%s", palettePath, palette);
  281. if( (fd = open( fullPath, O_RDONLY)) == -1)
  282. { /* open failed */
  283. printf("error opening %s\n",fullPath);
  284. SRV_FREE(fullPath);
  285. return((struct _palette *) NULL);
  286. }
  287. /*
  288. ** Read a buffer of data ... BUFSIZE == 1024, as long as
  289. ** screen < MAX_NUM_SCREENS (5) then we should never need more.
  290. */
  291. nbytes = read(fd, buf, BUFSIZE);
  292. if(nbytes == 0 || nbytes == BUFSIZE) /* A bogus number of bytes */
  293. { /* read failed */
  294. /*
  295. * Don't forget to add 1 for the NULL byte and 2 for the
  296. * period and the newline in the format string for sprintf
  297. */
  298. tmpStr = (char *)SRV_MALLOC(strlen(MSG2) + strlen(fullPath) + 3);
  299. sprintf(tmpStr,"%s%s.\n", MSG2, palettePath);
  300. _DtSimpleError(XmSCOLOR_SRV_NAME, DtWarning, NULL, tmpStr, NULL);
  301. SRV_FREE(tmpStr);
  302. SRV_FREE(fullPath);
  303. close(fd);
  304. return((struct _palette *) NULL);
  305. }
  306. else
  307. { /* read suceeded */
  308. /* Allocate space for this new palette. */
  309. new_palette = (palettes *)SRV_MALLOC( sizeof(struct _palette) + 1 );
  310. /* allocate enough space for the name */
  311. strcpy(tmpPalette, palette);
  312. for (token1=tmpPalette; *token1; token1++);
  313. while (token1!=tmpPalette && *token1!='.') token1--;
  314. if (!strcmp(token1,PALETTE_SUFFIX)) *token1 = '\0';
  315. new_palette->name = (char *)SRV_MALLOC(strlen(tmpPalette) + 1);
  316. strcpy(new_palette->name, (char *) tmpPalette);
  317. new_palette->converted = NULL;
  318. new_palette->converted_len = 0L;
  319. /* set the next pointer to NULL*/
  320. new_palette->next = NULL;
  321. if (ParsePaletteInfo(dpy, screen_number, buf, nbytes, new_palette) == -1)
  322. {
  323. /* palette file is bad */
  324. tmpStr = (char *)SRV_MALLOC(strlen(MSG3) + strlen(MSG3a) + strlen(palettePath) +1);
  325. tmpStr2 = (char *)SRV_MALLOC(strlen(MSG3)+1);
  326. sprintf(tmpStr2,"%s", MSG3);
  327. sprintf(tmpStr,"%s%s%s", tmpStr2, palettePath, MSG3a);
  328. _DtSimpleError(XmSCOLOR_SRV_NAME, DtWarning, NULL, tmpStr, NULL);
  329. SRV_FREE(tmpStr);
  330. SRV_FREE(tmpStr2);
  331. error_value = 1;
  332. unlink(palettePath);
  333. SRV_FREE(fullPath);
  334. close(fd);
  335. return((struct _palette *) NULL);
  336. }
  337. }
  338. /* close the file */
  339. close(fd);
  340. SRV_FREE(fullPath);
  341. return(new_palette);
  342. }
  343. /***********************************************************************
  344. *
  345. * ParsePaletteInfo - This routine reads from the buffer(buf) the
  346. * actual data into the new_palette. It reads in the bg colors
  347. * then uses the XmCalculateColorRGB to generate the ts, bs, and sc
  348. * colors. This routine doesn't allocate any pixel numbers but
  349. * does generate the RGB values for each color in a palette.
  350. *
  351. ***********************************************************************/
  352. static int
  353. ParsePaletteInfo(
  354. Display *dpy,
  355. int screen_num,
  356. char *buf,
  357. int nbytes,
  358. struct _palette *new_palette )
  359. {
  360. char tmpbuf[BUFSIZE];
  361. int count;
  362. int num_of_colors;
  363. int result;
  364. XColor tmp_color;
  365. int buf_count;
  366. static XmColorProc calcRGB = NULL;
  367. num_of_colors = new_palette->num_of_colors = 0;
  368. if(colorSrv.TypeOfMonitor[screen_num] != XmCO_BLACK_WHITE)
  369. if((strcmp(new_palette->name, W_O_B)) == 0 ||
  370. (strcmp(new_palette->name, B_O_W)) == 0 ||
  371. (strcmp(new_palette->name, W_ONLY)) == 0 ||
  372. (strcmp(new_palette->name, B_ONLY)) == 0)
  373. return(-1);
  374. buf_count = 0;
  375. /* read in background colors until end of file */
  376. while(buf_count < nbytes && buf[buf_count] != '!')
  377. {
  378. count = 0;
  379. /* read in a BG color */
  380. while(buf[buf_count] != '\012')
  381. tmpbuf[count++] = buf[buf_count++];
  382. tmpbuf[count++] = '\0';
  383. buf_count++;
  384. /* get the RGB value (XColor) of the background */
  385. result = XParseColor(dpy, DefaultColormap(dpy, screen_num),
  386. tmpbuf, &tmp_color);
  387. /* there was an error */
  388. if(result == 0)
  389. return(-1);
  390. new_palette->color[num_of_colors].bg.red = tmp_color.red;
  391. new_palette->color[num_of_colors].bg.blue = tmp_color.blue;
  392. new_palette->color[num_of_colors].bg.green = tmp_color.green;
  393. /*
  394. ** Now lets generate all the colors which go along with this bg i.e. ts,
  395. ** fg, bs, and sc.
  396. */
  397. if (calcRGB == NULL) calcRGB = XmGetColorCalculation();
  398. (*calcRGB)(&tmp_color,
  399. &(new_palette->color[num_of_colors].fg),
  400. &(new_palette->color[num_of_colors].sc),
  401. &(new_palette->color[num_of_colors].ts),
  402. &(new_palette->color[num_of_colors].bs));
  403. if(colorSrv.TypeOfMonitor[screen_num] == XmCO_BLACK_WHITE)
  404. InitializeBW(tmp_color.red, num_of_colors, new_palette);
  405. else /* Not Black and White */
  406. {
  407. if(colorSrv.UsePixmaps[screen_num] == TRUE)
  408. {
  409. /* the values generated by XmCalculateColorRBG are invalid */
  410. new_palette->color[num_of_colors].ts.red = 65535;
  411. new_palette->color[num_of_colors].ts.blue = 65535;
  412. new_palette->color[num_of_colors].ts.green = 65535;
  413. new_palette->color[num_of_colors].bs.red = 0;
  414. new_palette->color[num_of_colors].bs.blue = 0;
  415. new_palette->color[num_of_colors].bs.green = 0;
  416. }
  417. if(colorSrv.FgColor[screen_num] != DYNAMIC)
  418. {
  419. if(colorSrv.FgColor[screen_num] == BLACK)
  420. {
  421. new_palette->color[num_of_colors].fg.red = 0;
  422. new_palette->color[num_of_colors].fg.blue = 0;
  423. new_palette->color[num_of_colors].fg.green = 0;
  424. }
  425. else /* colorSrv.FgColor[screen_num] == WHITE */
  426. {
  427. new_palette->color[num_of_colors].fg.red = 65535;
  428. new_palette->color[num_of_colors].fg.blue = 65535;
  429. new_palette->color[num_of_colors].fg.green = 65535;
  430. }
  431. }
  432. } /* else Not Black and White */
  433. num_of_colors++;
  434. new_palette->num_of_colors++;
  435. if(colorSrv.TypeOfMonitor[screen_num] == XmCO_BLACK_WHITE)
  436. if( new_palette->num_of_colors == 2)
  437. break;
  438. if(colorSrv.TypeOfMonitor[screen_num] == XmCO_LOW_COLOR ||
  439. colorSrv.TypeOfMonitor[screen_num] == XmCO_MEDIUM_COLOR)
  440. if( new_palette->num_of_colors == 4)
  441. break;
  442. } /* while */
  443. if(colorSrv.TypeOfMonitor[screen_num] == XmCO_LOW_COLOR)
  444. {
  445. new_palette->num_of_colors = 2;
  446. SwitchAItoPS(new_palette);
  447. }
  448. return(0);
  449. }
  450. /***********************************************************************
  451. *
  452. * InitializeBW - the type of monitor is a black and white, so
  453. * initialize the colors to black and white. If the color passed
  454. * in is 0 (black) set everything to 65535 (white). Otherwise
  455. * set everything to 0 (black).
  456. *
  457. ***********************************************************************/
  458. static void
  459. InitializeBW(
  460. unsigned long color,
  461. int num_of_colors,
  462. struct _palette *new_palette )
  463. {
  464. if(color == 0L)
  465. {
  466. new_palette->color[num_of_colors].fg.red = 65535;
  467. new_palette->color[num_of_colors].fg.blue = 65535;
  468. new_palette->color[num_of_colors].fg.green = 65535;
  469. new_palette->color[num_of_colors].ts.red = 65535;
  470. new_palette->color[num_of_colors].ts.blue = 65535;
  471. new_palette->color[num_of_colors].ts.green = 65535;
  472. new_palette->color[num_of_colors].bs.red = 65535;
  473. new_palette->color[num_of_colors].bs.blue = 65535;
  474. new_palette->color[num_of_colors].bs.green = 65535;
  475. new_palette->color[num_of_colors].sc.red = 0;
  476. new_palette->color[num_of_colors].sc.blue = 0;
  477. new_palette->color[num_of_colors].sc.green = 0;
  478. }
  479. else
  480. {
  481. new_palette->color[num_of_colors].fg.red = 0;
  482. new_palette->color[num_of_colors].fg.blue = 0;
  483. new_palette->color[num_of_colors].fg.green = 0;
  484. new_palette->color[num_of_colors].ts.red = 0;
  485. new_palette->color[num_of_colors].ts.blue = 0;
  486. new_palette->color[num_of_colors].ts.green = 0;
  487. new_palette->color[num_of_colors].bs.red = 0;
  488. new_palette->color[num_of_colors].bs.blue = 0;
  489. new_palette->color[num_of_colors].bs.green = 0;
  490. new_palette->color[num_of_colors].sc.red = 65535;
  491. new_palette->color[num_of_colors].sc.blue = 65535;
  492. new_palette->color[num_of_colors].sc.green = 65535;
  493. }
  494. }
  495. void
  496. SwitchAItoPS(
  497. struct _palette *new_palette )
  498. {
  499. new_palette->color[0].bg.red = new_palette->color[3].bg.red;
  500. new_palette->color[0].bg.green = new_palette->color[3].bg.green;
  501. new_palette->color[0].bg.blue = new_palette->color[3].bg.blue;
  502. new_palette->color[0].fg.red = new_palette->color[3].fg.red;
  503. new_palette->color[0].fg.green = new_palette->color[3].fg.green;
  504. new_palette->color[0].fg.blue = new_palette->color[3].fg.blue;
  505. new_palette->color[0].ts.red = new_palette->color[3].ts.red;
  506. new_palette->color[0].ts.green = new_palette->color[3].ts.green;
  507. new_palette->color[0].ts.blue = new_palette->color[3].ts.blue;
  508. new_palette->color[0].bs.red = new_palette->color[3].bs.red;
  509. new_palette->color[0].bs.green = new_palette->color[3].bs.green;
  510. new_palette->color[0].bs.blue = new_palette->color[3].bs.blue;
  511. new_palette->color[0].sc.red = new_palette->color[3].sc.red;
  512. new_palette->color[0].sc.green = new_palette->color[3].sc.green;
  513. new_palette->color[0].sc.blue = new_palette->color[3].sc.blue;
  514. new_palette->color[1].bg.red = new_palette->color[2].bg.red;
  515. new_palette->color[1].bg.green = new_palette->color[2].bg.green;
  516. new_palette->color[1].bg.blue = new_palette->color[2].bg.blue;
  517. new_palette->color[1].fg.red = new_palette->color[2].fg.red;
  518. new_palette->color[1].fg.green = new_palette->color[2].fg.green;
  519. new_palette->color[1].fg.blue = new_palette->color[2].fg.blue;
  520. new_palette->color[1].ts.red = new_palette->color[2].ts.red;
  521. new_palette->color[1].ts.green = new_palette->color[2].ts.green;
  522. new_palette->color[1].ts.blue = new_palette->color[2].ts.blue;
  523. new_palette->color[1].bs.red = new_palette->color[2].bs.red;
  524. new_palette->color[1].bs.green = new_palette->color[2].bs.green;
  525. new_palette->color[1].bs.blue = new_palette->color[2].bs.blue;
  526. new_palette->color[1].sc.red = new_palette->color[2].sc.red;
  527. new_palette->color[1].sc.green = new_palette->color[2].sc.green;
  528. new_palette->color[1].sc.blue = new_palette->color[2].sc.blue;
  529. }
  530. /***********************************************************************
  531. *
  532. * SaveDefaultPalette - used to save the palette.dt file to
  533. * either $HOME/.dt/$DISPLAY/current or $HOME/.dt/$DISPLAY/home.
  534. * The parameter mode determines whether it is home or
  535. * current.
  536. *
  537. ***********************************************************************/
  538. void
  539. SaveDefaultPalette(
  540. Display *dpy,
  541. char *dtPath,
  542. int mode )
  543. {
  544. }