SrvPalette.c 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557
  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. "$TOG: SrvPalette.c /main/13 1998/07/23 18:08:39 mgreess $";
  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: SrvPalette.c
  37. **
  38. ** Project: HP DT Style Manager , integrated into dtsession
  39. **
  40. ** Description:
  41. ** -----------
  42. ** This is the main program for the color server portion of the dt session
  43. ** manager. It:
  44. ** 1. Determines the number of color cells for each screen
  45. ** attached to the server this session manager is running on.
  46. ** 2. Reads in resouces for the colorserver on a per screen
  47. ** basis.
  48. ** 3. Allocates pixels either Read/Write or Read Only depending
  49. ** on the resource DynamicColor.
  50. ** 4. Handles query's about those allocated pixels through
  51. ** Selections.
  52. **
  53. *******************************************************************
  54. ** (c) Copyright Hewlett-Packard Company, 1990. All rights are
  55. ** reserved. Copying or other reproduction of this program
  56. ** except for archival purposes is prohibited without prior
  57. ** written consent of Hewlett-Packard Company.
  58. ********************************************************************
  59. **
  60. **
  61. **************************************************************************
  62. **********************************<+>*************************************/
  63. #include <ctype.h>
  64. #include <X11/Xatom.h>
  65. #include "Srv.h"
  66. #include "SrvFile_io.h"
  67. #include "SrvPalette.h"
  68. #ifdef sun
  69. #include "OWsync.h"
  70. #else
  71. #include <Dt/DtP.h>
  72. #endif
  73. #define DEFAULT 4
  74. #define SRVBUFSIZE 1024
  75. #define DEFAULT_COLOR_PALETTE "Default.dp"
  76. #define DEFAULT_GRAYSCALE_PALETTE "GrayScale.dp"
  77. #define DtRColorUse "ColorUse"
  78. #define DtRForegroundColor "ForegroundColor"
  79. #define DtRShadowPixmaps "ShadowPixmaps"
  80. #define TYPE_OF_MONITOR "Type Of Monitor" /* also in dtstyle/ColorMain.c */
  81. /* global color server struct */
  82. ColorSrv colorSrv;
  83. /*************************
  84. * Color Server Resources
  85. *************************/
  86. typedef struct {
  87. int ColorUse;
  88. int ShadowPixmaps;
  89. int ForegroundColor;
  90. Boolean DynamicColor;
  91. Boolean WriteXrdbColors;
  92. char *ColorPalette;
  93. char *MonochromePalette;
  94. } Appdata, *AppdataPtr;
  95. static XtResource resources[] = {
  96. { "colorUse",
  97. DtRColorUse,
  98. DtRColorUse,
  99. sizeof(int),
  100. XtOffset(AppdataPtr, ColorUse),
  101. XmRString,
  102. "DEFAULT"},
  103. { "shadowPixmaps",
  104. DtRShadowPixmaps,
  105. DtRShadowPixmaps,
  106. sizeof(int),
  107. XtOffset(AppdataPtr, ShadowPixmaps),
  108. XmRString,
  109. "DEFAULT"},
  110. { "foregroundColor",
  111. DtRForegroundColor,
  112. DtRForegroundColor,
  113. sizeof(int),
  114. XtOffset(AppdataPtr, ForegroundColor),
  115. XmRString,
  116. "DYNAMIC"},
  117. { "dynamicColor",
  118. "DynamicColor",
  119. XmRBoolean,
  120. sizeof(Boolean),
  121. XtOffset(AppdataPtr, DynamicColor),
  122. XmRImmediate,
  123. (XtPointer) True},
  124. { "writeXrdbColors",
  125. "WriteXrdbColors",
  126. XmRBoolean,
  127. sizeof(Boolean),
  128. XtOffset(AppdataPtr, WriteXrdbColors),
  129. XmRImmediate,
  130. (XtPointer) True},
  131. { "colorPalette",
  132. "ColorPalette",
  133. XmRString,
  134. sizeof(char *),
  135. XtOffset(AppdataPtr, ColorPalette),
  136. XmRImmediate,
  137. (XtPointer)"DEFAULT"},
  138. { "monochromePalette",
  139. "MonochromePalette",
  140. XmRString,
  141. sizeof(char *),
  142. XtOffset(AppdataPtr, MonochromePalette),
  143. XmRImmediate,
  144. (XtPointer)"Black.dp"},
  145. };
  146. Appdata pColorSrvRsrc;
  147. /************************************
  148. * External Interface
  149. ***********************************/
  150. /* variables */
  151. Widget shell[MAX_NUM_SCREENS];
  152. /******** Static Function Declarations ********/
  153. static Boolean AllocateColors(
  154. Display *dpy) ;
  155. static char *convert_pixel_set(
  156. int typeOfMonitor,
  157. ColorSet *color );
  158. static Boolean convert_selection(
  159. Widget w,
  160. Atom *selection,
  161. Atom *target,
  162. Atom *type,
  163. XtPointer *value,
  164. unsigned long *length,
  165. int *format) ;
  166. static void lose_selection(
  167. Widget w,
  168. Atom *selection) ;
  169. static int FindMaximumDefault(
  170. Display *dpy,
  171. int screen_number) ;
  172. static int FindNumOfPixels(
  173. Display *dpy,
  174. int screen_number) ;
  175. static int GetNumOfPixels(
  176. int screen_number) ;
  177. static void MatchAndStore(
  178. Display *dpy,
  179. int screen_number,
  180. unsigned long *pixels) ;
  181. static Boolean AllocReadWrite(
  182. Display *dpy,
  183. int screen_number,
  184. int numOfPixels) ;
  185. static void AllocReadOnly(
  186. Display *dpy,
  187. int screen_number) ;
  188. static void CvtStringToColorUse(
  189. XrmValue *args,
  190. Cardinal *num_args,
  191. XrmValue *from_val,
  192. XrmValue *to_val) ;
  193. static void CvtStringToForegroundColor(
  194. XrmValue *args,
  195. Cardinal *num_args,
  196. XrmValue *from_val,
  197. XrmValue *to_val) ;
  198. static void CvtStringToShadowPixmaps(
  199. XrmValue *args,
  200. Cardinal *num_args,
  201. XrmValue *from_val,
  202. XrmValue *to_val) ;
  203. static Boolean _DtWmStringsAreEqual(
  204. register char *in_str,
  205. register char *test_str) ;
  206. static void SetDefaults(
  207. Display *dpy,
  208. int screen_number) ;
  209. static void _DtCacheProperties(
  210. Display *dpy,
  211. Window Win) ;
  212. /******** End Static Function Declarations ********/
  213. #define MSG1 ((char *)GETMESSAGE(26, 1, "Color Server Warning: couldn't get ownership of the selection: "))
  214. #define MSG2 ((char *)GETMESSAGE(26, 2, "Color Server Warning, losing ownership of the selection: "))
  215. #define MSG2a ((char *)GETMESSAGE(26, 3, "Should never lose the selection."))
  216. #define MSG3 ((char *)GETMESSAGE(26, 4, "Warning, found more pixels then are available."))
  217. /***********************************************************************
  218. *
  219. * InitializeDtcolor - calls all the routines which do the initialization
  220. * for the color server.
  221. *
  222. **********************************************************************/
  223. int
  224. InitializeDtcolor(
  225. Display *dpy,
  226. short sessionType )
  227. {
  228. int status, screen_number;
  229. char xrdb_string[100];
  230. /* find out what type of monitor(s?) is being used */
  231. status = CheckMonitor(dpy);
  232. if(status == 0)
  233. {
  234. /* Allocate colors for the default palette */
  235. AllocateColors(dpy);
  236. }
  237. else {
  238. for(screen_number=0;
  239. screen_number != colorSrv.NumOfScreens;
  240. screen_number++)
  241. {
  242. /* Set disown selections of the pixel set atoms */
  243. XtDisownSelection(shell[screen_number],
  244. colorSrv.XA_CUSTOMIZE[screen_number],
  245. CurrentTime);
  246. }
  247. return(-1);
  248. }
  249. /* don't set resources if writeXrdbColors == false */
  250. if (!pColorSrvRsrc.WriteXrdbColors)
  251. return(0);
  252. #ifdef sun
  253. OWsyncColorResources(dpy, colorSrv.TypeOfMonitor[0],
  254. colorSrv.pCurrentPalette[0]->color);
  255. OWsyncLocaleResources(dpy);
  256. #else
  257. /* Set the *background: resource
  258. What gets set depends on what type of monitor */
  259. /* For XmCO_HIGH_COLOR the default primary colorset is 4, else 1 */
  260. if(colorSrv.TypeOfMonitor[0] != XmCO_BLACK_WHITE)
  261. {
  262. int chipnum = colorSrv.TypeOfMonitor[0] == XmCO_HIGH_COLOR ? 4 : 1;
  263. sprintf(xrdb_string,
  264. "*background: #%04X%04X%04X\n*foreground: #%04X%04X%04X\n",
  265. colorSrv.pCurrentPalette[0]->color[chipnum].bg.red,
  266. colorSrv.pCurrentPalette[0]->color[chipnum].bg.green,
  267. colorSrv.pCurrentPalette[0]->color[chipnum].bg.blue,
  268. colorSrv.pCurrentPalette[0]->color[chipnum].fg.red,
  269. colorSrv.pCurrentPalette[0]->color[chipnum].fg.green,
  270. colorSrv.pCurrentPalette[0]->color[chipnum].fg.blue);
  271. }
  272. /* For XmCO_BLACK_WHITE the resources depended on whether the default
  273. palette is White on Black or Black on White */
  274. else /* XmCO_BLACK_WHITE */
  275. {
  276. if(strcmp(colorSrv.pCurrentPalette[0]->name, W_O_B) == 0 ||
  277. strcmp(colorSrv.pCurrentPalette[0]->name, B_ONLY) == 0)
  278. {
  279. sprintf(xrdb_string,
  280. "*background: #000000000000\n*foreground: #FFFFFFFFFFFF\n");
  281. }
  282. else
  283. {
  284. sprintf(xrdb_string,
  285. "*background: #FFFFFFFFFFFF\n*foreground: #000000000000\n");
  286. }
  287. }
  288. /* go merge the xrdb_string into the xrdb */
  289. _DtAddToResource(dpy, xrdb_string);
  290. #endif
  291. _DtCacheProperties(dpy, XtWindow(shell[0])) ;
  292. return(0);
  293. }
  294. static void
  295. _DtCacheProperties(
  296. Display *dpy,
  297. Window win)
  298. {
  299. Atom pixel_set_atom ;
  300. struct _palette *palette ;
  301. int typeOfMonitor;
  302. int screen_number = 0 ; /*assuming simple case i.e. screen 0 */
  303. pixel_set_atom = XInternAtom(dpy, XmSPIXEL_SET_PROP, FALSE) ;
  304. palette = colorSrv.pCurrentPalette[screen_number];
  305. typeOfMonitor = colorSrv.TypeOfMonitor[screen_number];
  306. if (palette->converted == NULL)
  307. {
  308. palette->converted = convert_pixel_set(typeOfMonitor, palette->color);
  309. palette->converted_len = strlen(palette->converted);
  310. }
  311. *(palette->converted + palette->converted_len) = XmPIXEL_SET_PROP_VERSION ;
  312. palette->converted_len++ ;
  313. palette->converted[palette->converted_len] = 0 ;
  314. XChangeProperty(dpy, win, pixel_set_atom, XA_STRING, 8, PropModeAppend,
  315. (unsigned char *) XtNewString(palette->converted),
  316. palette->converted_len) ;
  317. }
  318. /*****************************************************************************
  319. **
  320. ** Allocates color cells to be used by clients. The global varible
  321. ** DynamicColor[screen_number] determines if the cells are to be allocated
  322. ** read/write or read only. Right now this routine allocates
  323. ** all cells needed for a palette up front. For performance tuning we will
  324. ** want to look at allocating on demand. The allocation scheme looks like
  325. ** the following: (AT - Alway True)
  326. ** # TypeOfMonior UsePixmaps FgColor # of Cells allocated per palette
  327. ** - ------------ ---------- ------ -------------------------------
  328. ** 1 HIGH_COLOR FALSE DYNAMIC (fg,bg,ts,bs,sc) 5*8 = 40
  329. ** 2 HIGH_COLOR FALSE BLACK or WHITE (bg,ts,bs,sc) 4*8 = 32
  330. ** 3 HIGH_COLOR TRUE DYNAMIC (fg,bg,sc) 3*8 = 24
  331. ** 4 HIGH_COLOR TRUE BLACK or WHITE (bg,sc) 2*8 = 16
  332. **
  333. ** 5 MEDIUM_COLOR FALSE DYNAMIC (fg,bg,ts,bs,sc) 5*4 = 20
  334. ** 6 MEDIUM_COLOR FALSE BLACK or WHITE (bg,ts,bs,sc) 4*4 = 16
  335. ** 7 MEDIUM_COLOR TRUE DYNAMIC (fg,bg,sc) 3*4 = 12
  336. ** 8 MEDIUM_COLOR TRUE BLACK or WHITE (bg,sc) 2*4 = 8
  337. **
  338. ** 9 LOW_COLOR FALSE DYNAMIC (fg,bg,ts,bs,sc) 5*2 = 10
  339. ** 10 LOW_COLOR FALSE BLACK or WHITE (bg,ts,bs,sc) 4*2 = 8
  340. ** 11 LOW_COLOR TRUE DYNAMIC (fg,bg,sc) 3*2 = 6
  341. ** 12 LOW_COLOR TRUE BLACK or WHITE (bg,sc) 2*2 = 4
  342. **
  343. ** 13 BLACK_WHITE AT Aways opposite 0
  344. ** of Bg
  345. **
  346. ***************************************************************************/
  347. static Boolean
  348. AllocateColors(
  349. Display *dpy )
  350. {
  351. int screen_number;
  352. int numOfPixels;
  353. /* Determine how many pixels to allocate (numOfPixels) */
  354. for(screen_number=0;screen_number != colorSrv.NumOfScreens;screen_number++)
  355. {
  356. numOfPixels = GetNumOfPixels(screen_number);
  357. /* Now allocate the correct number of pixels using numOfPixels */
  358. if(numOfPixels != 0) /* Not XmCO_BLACK_WHITE */
  359. {
  360. if(colorSrv.DynamicColor[screen_number] == True)
  361. {
  362. /* go allocate Read/Write cells for the color server */
  363. if(!AllocReadWrite(dpy, screen_number, numOfPixels))
  364. return(False);
  365. }
  366. else
  367. /* go allocate Read Only cells for the color server */
  368. AllocReadOnly(dpy, screen_number);
  369. }
  370. if(colorSrv.TypeOfMonitor[screen_number] == XmCO_BLACK_WHITE)
  371. {
  372. #define BlackColorSet(dpy,scr,xcolor) \
  373. (xcolor).pixel = BlackPixel((dpy),(scr)); \
  374. (xcolor).red = (xcolor).green = (xcolor).blue = 0;
  375. #define WhiteColorSet(dpy,scr,xcolor) \
  376. (xcolor).pixel = WhitePixel((dpy),(scr)); \
  377. (xcolor).red = (xcolor).green = (xcolor).blue = 65535;
  378. palettes *palette = colorSrv.pCurrentPalette[screen_number];
  379. /* Check to see what black and white palette it is */
  380. /* note: color[0] = secondary, color[1] = primary (as of 8/8/90) */
  381. if(!(strcmp(colorSrv.pCurrentPalette[screen_number]->name, W_O_B)))
  382. {
  383. WhiteColorSet(dpy,screen_number,palette->color[0].bg);
  384. BlackColorSet(dpy,screen_number,palette->color[0].fg);
  385. BlackColorSet(dpy,screen_number,palette->color[0].ts);
  386. BlackColorSet(dpy,screen_number,palette->color[0].bs);
  387. WhiteColorSet(dpy,screen_number,palette->color[0].sc);
  388. BlackColorSet(dpy,screen_number,palette->color[1].bg);
  389. WhiteColorSet(dpy,screen_number,palette->color[1].fg);
  390. WhiteColorSet(dpy,screen_number,palette->color[1].ts);
  391. WhiteColorSet(dpy,screen_number,palette->color[1].bs);
  392. BlackColorSet(dpy,screen_number,palette->color[1].sc);
  393. }
  394. else
  395. if(!(strcmp(colorSrv.pCurrentPalette[screen_number]->name, B_O_W)))
  396. {
  397. BlackColorSet(dpy,screen_number,palette->color[0].bg);
  398. WhiteColorSet(dpy,screen_number,palette->color[0].fg);
  399. WhiteColorSet(dpy,screen_number,palette->color[0].ts);
  400. WhiteColorSet(dpy,screen_number,palette->color[0].bs);
  401. BlackColorSet(dpy,screen_number,palette->color[0].sc);
  402. WhiteColorSet(dpy,screen_number,palette->color[1].bg);
  403. BlackColorSet(dpy,screen_number,palette->color[1].fg);
  404. BlackColorSet(dpy,screen_number,palette->color[1].ts);
  405. BlackColorSet(dpy,screen_number,palette->color[1].bs);
  406. WhiteColorSet(dpy,screen_number,palette->color[1].sc);
  407. }
  408. else
  409. if(!(strcmp(colorSrv.pCurrentPalette[screen_number]->name, W_ONLY)))
  410. {
  411. WhiteColorSet(dpy,screen_number,palette->color[0].bg);
  412. BlackColorSet(dpy,screen_number,palette->color[0].fg);
  413. BlackColorSet(dpy,screen_number,palette->color[0].ts);
  414. BlackColorSet(dpy,screen_number,palette->color[0].bs);
  415. WhiteColorSet(dpy,screen_number,palette->color[0].sc);
  416. WhiteColorSet(dpy,screen_number,palette->color[1].bg);
  417. BlackColorSet(dpy,screen_number,palette->color[1].fg);
  418. BlackColorSet(dpy,screen_number,palette->color[1].ts);
  419. BlackColorSet(dpy,screen_number,palette->color[1].bs);
  420. WhiteColorSet(dpy,screen_number,palette->color[1].sc);
  421. }
  422. else /* black only */
  423. {
  424. BlackColorSet(dpy,screen_number,palette->color[0].bg);
  425. WhiteColorSet(dpy,screen_number,palette->color[0].fg);
  426. WhiteColorSet(dpy,screen_number,palette->color[0].ts);
  427. WhiteColorSet(dpy,screen_number,palette->color[0].bs);
  428. BlackColorSet(dpy,screen_number,palette->color[0].sc);
  429. BlackColorSet(dpy,screen_number,palette->color[1].bg);
  430. WhiteColorSet(dpy,screen_number,palette->color[1].fg);
  431. WhiteColorSet(dpy,screen_number,palette->color[1].ts);
  432. WhiteColorSet(dpy,screen_number,palette->color[1].bs);
  433. BlackColorSet(dpy,screen_number,palette->color[1].sc);
  434. }
  435. #undef BlackColorSet
  436. #undef WhiteColorSet
  437. }
  438. XSync(dpy, 0);
  439. } /* for screen_number=0 ; screen_number < NumOfScreens; screen_number++ */
  440. return(True);
  441. }
  442. /************************************************************************
  443. **
  444. ** CheckMonitor - check to determine which type of monitor each of the
  445. ** screens on the server is running on.
  446. **
  447. ************************************************************************/
  448. int
  449. CheckMonitor(
  450. Display *dpy )
  451. {
  452. int n, screen_number, result;
  453. Arg args[4];
  454. char screenStr[5], cust_msg[24];
  455. char *tmpStr = NULL;
  456. char tmpPalette[SRVBUFSIZE];
  457. char *token1 = NULL;
  458. char *xrdb_string = NULL;
  459. Widget mainShell;
  460. XtAppContext app_context;
  461. /* Determine the number of screens attached to this server */
  462. colorSrv.NumOfScreens = ScreenCount(dpy);
  463. /* Initialize the Atoms used to pass information */
  464. colorSrv.XA_PIXEL_SET = XInternAtom(dpy, XmSPIXEL_SET, FALSE);
  465. colorSrv.XA_TYPE_MONITOR = XInternAtom(dpy, TYPE_OF_MONITOR, FALSE);
  466. /* create a top level shell to retrieve subresources from */
  467. n = 0;
  468. XtSetArg(args[n], XmNbackground,
  469. BlackPixelOfScreen(DefaultScreenOfDisplay(dpy))); n++;
  470. XtSetArg(args[n], XmNmappedWhenManaged, False); n++;
  471. XtSetArg (args[n], XmNwidth, 1); n++;
  472. XtSetArg (args[n], XmNheight, 1); n++;
  473. mainShell = XtAppCreateShell("dtsession", XmSCOLOR_SRV_NAME,
  474. applicationShellWidgetClass,
  475. dpy, args, n);
  476. /* create an application context */
  477. app_context = XtWidgetToApplicationContext(mainShell);
  478. /* Register the resource converters */
  479. XtAppAddConverter(app_context, XmRString, "ColorUse",
  480. CvtStringToColorUse, NULL, 0);
  481. XtAppAddConverter(app_context, XmRString, "ForegroundColor",
  482. CvtStringToForegroundColor, NULL, 0);
  483. XtAppAddConverter(app_context, XmRString, "ShadowPixmaps",
  484. CvtStringToShadowPixmaps, NULL, 0);
  485. /* cycle through each screen */
  486. for(screen_number=0;screen_number != colorSrv.NumOfScreens;screen_number++)
  487. {
  488. snprintf(screenStr, sizeof(screenStr), "%d", screen_number);
  489. n = 0;
  490. XtSetArg(args[n], XmNbackground,
  491. BlackPixelOfScreen(DefaultScreenOfDisplay(dpy))); n++;
  492. XtSetArg(args[n], XmNmappedWhenManaged, False); n++;
  493. XtSetArg (args[n], XmNwidth, 1); n++;
  494. XtSetArg (args[n], XmNheight, 1); n++;
  495. shell[screen_number] = XtAppCreateShell(screenStr, XmSCOLOR_SRV_NAME,
  496. applicationShellWidgetClass,
  497. dpy, args, n);
  498. /*
  499. * widget needs to be realized for the window ID for
  500. * selections to work
  501. */
  502. XtRealizeWidget(shell[screen_number]);
  503. snprintf(cust_msg, sizeof(cust_msg), "%s%d",
  504. XmSCUSTOMIZE_DATA, screen_number);
  505. colorSrv.XA_CUSTOMIZE[screen_number] =
  506. XInternAtom(dpy, cust_msg, FALSE);
  507. /* go set ownership of the pixel set atoms */
  508. result = XtOwnSelection(shell[screen_number],
  509. colorSrv.XA_CUSTOMIZE[screen_number],
  510. CurrentTime, convert_selection,
  511. lose_selection, NULL);
  512. if(result == False)
  513. {
  514. /*
  515. * Don't forget to add length for the extra characters.
  516. */
  517. int len = strlen(MSG1) + 25 + 5 + 1 + 1;
  518. tmpStr = (char *)SRV_MALLOC(len);
  519. if (tmpStr)
  520. {
  521. snprintf(tmpStr, len, "%s colorSrv.XA_CUSTOMIZE[%d].\n",
  522. MSG1, screen_number);
  523. _DtSimpleError(XmSCOLOR_SRV_NAME, DtWarning, NULL, tmpStr, NULL);
  524. SRV_FREE(tmpStr);
  525. tmpStr = NULL;
  526. }
  527. return(-1);
  528. }
  529. /* Get the colorserver resources for this screen */
  530. XtGetSubresources(mainShell, &pColorSrvRsrc, screenStr, screenStr,
  531. resources, XtNumber(resources), NULL, 0);
  532. /*
  533. * Set TypeOfMonitor, UsePixmaps FgColor and
  534. * DynamicColor for this screen
  535. */
  536. SetDefaults(dpy, screen_number);
  537. if (colorSrv.TypeOfMonitor[screen_number] != XmCO_BLACK_WHITE)
  538. {
  539. colorSrv.pCurrentPalette[screen_number] =
  540. (struct _palette *) GetPaletteDefinition(dpy,
  541. screen_number,
  542. pColorSrvRsrc.ColorPalette);
  543. }
  544. else
  545. {
  546. /* Allocate space for new palette. */
  547. colorSrv.pCurrentPalette[screen_number] =
  548. (struct _palette *) SRV_MALLOC( sizeof(struct _palette) + 1 );
  549. /* allocate enough space for the name */
  550. snprintf(tmpPalette, SRVBUFSIZE, "%s",
  551. pColorSrvRsrc.MonochromePalette);
  552. for (token1=tmpPalette; *token1; token1++)
  553. ;
  554. while (token1 != tmpPalette && *token1 != '.')
  555. token1--;
  556. if (!strcmp(token1, PALETTE_SUFFIX))
  557. *token1 = '\0';
  558. colorSrv.pCurrentPalette[screen_number]->name =
  559. (char *)SRV_MALLOC(strlen(tmpPalette) + 1);
  560. strcpy(colorSrv.pCurrentPalette[screen_number]->name,
  561. (char *) tmpPalette);
  562. colorSrv.pCurrentPalette[screen_number]->converted = NULL;
  563. }
  564. if (colorSrv.pCurrentPalette[screen_number] == (struct _palette *) NULL)
  565. {
  566. return(-1);
  567. }
  568. /* write out the color or monochrome palette resource for the screen */
  569. xrdb_string = XtMalloc(BUFSIZ);
  570. if (!xrdb_string)
  571. return -1;
  572. if (colorSrv.TypeOfMonitor[0] == XmCO_HIGH_COLOR ||
  573. colorSrv.TypeOfMonitor[0] == XmCO_MEDIUM_COLOR ||
  574. colorSrv.TypeOfMonitor[0] == XmCO_LOW_COLOR)
  575. {
  576. snprintf(xrdb_string, BUFSIZ, "*%d*ColorPalette: %s%s\n",
  577. screen_number,
  578. colorSrv.pCurrentPalette[screen_number]->name,
  579. PALETTE_SUFFIX);
  580. }
  581. else /* XmCO_BLACK_WHITE */
  582. {
  583. snprintf(xrdb_string, BUFSIZ, "*%d*MonochromePalette: %s%s\n",
  584. screen_number,
  585. colorSrv.pCurrentPalette[screen_number]->name,
  586. PALETTE_SUFFIX);
  587. }
  588. _DtAddToResource(dpy, xrdb_string);
  589. XtFree(xrdb_string);
  590. } /* for each screen */
  591. return(0);
  592. }
  593. /************************************************************************
  594. **
  595. ** convert_pixel_set - converts palette pixel set to selection format
  596. **
  597. ************************************************************************/
  598. static char *
  599. convert_pixel_set(
  600. int typeOfMonitor,
  601. ColorSet *color )
  602. {
  603. int i;
  604. char *converted;
  605. char *p;
  606. int colormappingindex = 0;
  607. const int colormapping [4][XmCO_MAX_NUM_COLORS] = {
  608. {0, 1, 2, 3, 4, 5, 6, 7}, /* XmCO_HIGH_COLOR */
  609. {0, 1, 2, 3, 1, 1, 2, 1}, /* XmCO_MEDIUM_COLOR */
  610. {0, 1, 1, 1, 1, 1, 1, 1}, /* XmCO_LOW_COLOR */
  611. {0, 1, 1, 1, 1, 1, 1, 1} /* XmCO_BLACK_WHITE */
  612. };
  613. #if 0 /* map when hi-color was the default */
  614. const int colormapping [4][XmCO_MAX_NUM_COLORS] = {
  615. {0, 1, 2, 3, 4, 5, 6, 7}, /* XmCO_HIGH_COLOR */
  616. {0, 1, 2, 3, 3, 3, 3, 3}, /* XmCO_MEDIUM_COLOR */
  617. {0, 1, 1, 0, 0, 0, 0, 0}, /* XmCO_LOW_COLOR */
  618. {0, 1, 1, 0, 0, 0, 0, 0} /* XmCO_BLACK_WHITE */
  619. };
  620. #endif
  621. switch(typeOfMonitor)
  622. {
  623. case XmCO_HIGH_COLOR: colormappingindex = 0; break;
  624. case XmCO_MEDIUM_COLOR: colormappingindex = 1; break;
  625. case XmCO_LOW_COLOR: colormappingindex = 2; break;
  626. case XmCO_BLACK_WHITE: colormappingindex = 3; break;
  627. }
  628. p = converted = (char *)SRV_MALLOC(400);
  629. /* lead the string with the type of monitor */
  630. p += sprintf(p, "%x_", typeOfMonitor);
  631. for (i = 0; i < NUM_OF_COLORS; i++)
  632. {
  633. p += sprintf (p, "%lx_%lx_%lx_%lx_%lx_",
  634. color[colormapping[colormappingindex][i]].bg.pixel,
  635. color[colormapping[colormappingindex][i]].fg.pixel,
  636. color[colormapping[colormappingindex][i]].ts.pixel,
  637. color[colormapping[colormappingindex][i]].bs.pixel,
  638. color[colormapping[colormappingindex][i]].sc.pixel);
  639. }
  640. return(converted);
  641. }
  642. /************************************************************************
  643. **
  644. ** convert_selection - Callback, called when some other client wishes
  645. ** to get information from the dtcolor (color server)
  646. **
  647. ************************************************************************/
  648. static Boolean
  649. convert_selection(
  650. Widget w,
  651. Atom *selection,
  652. Atom *target,
  653. Atom *type,
  654. XtPointer *value,
  655. unsigned long *length,
  656. int *format )
  657. {
  658. char pixels[50];
  659. int i, screen_number = 0;
  660. char *temp;
  661. char *str_type_return;
  662. XrmValue value_return;
  663. XrmValue cvt_value;
  664. XrmDatabase db;
  665. char instanceName[30], instanceClass[30];
  666. Boolean status;
  667. struct _palette *palette;
  668. int typeOfMonitor;
  669. /* Determine for which screen the selection came from */
  670. for(i=0; i < MAX_NUM_SCREENS; i++)
  671. {
  672. if(colorSrv.XA_CUSTOMIZE[i] == *selection)
  673. {
  674. screen_number = i;
  675. break;
  676. }
  677. }
  678. palette = colorSrv.pCurrentPalette[screen_number];
  679. typeOfMonitor = colorSrv.TypeOfMonitor[screen_number];
  680. if(*target == colorSrv.XA_PIXEL_SET)
  681. {
  682. /* wants to know the pixels allocated for the palette */
  683. if (palette->converted == NULL)
  684. {
  685. palette->converted = convert_pixel_set(typeOfMonitor, palette->color);
  686. palette->converted_len = strlen(palette->converted);
  687. }
  688. *type = XA_STRING;
  689. *length = palette->converted_len;
  690. *value = XtNewString(palette->converted);
  691. *format = 8;
  692. return TRUE;
  693. }
  694. else if(*target == colorSrv.XA_TYPE_MONITOR)
  695. {
  696. /* wants to know ColorUse, ShadowPixmaps, ForegroundColor,
  697. and DynamicColor */
  698. *type = XA_STRING;
  699. temp = (char *)SRV_MALLOC(20);
  700. sprintf (temp, "%x_%x_%x_%x", colorSrv.TypeOfMonitor[screen_number],
  701. colorSrv.UsePixmaps[screen_number],
  702. colorSrv.FgColor[screen_number],
  703. colorSrv.DynamicColor[screen_number]);
  704. *length = strlen(temp);
  705. *value = temp;
  706. *format = 8;
  707. return TRUE;
  708. }
  709. else
  710. return FALSE;
  711. }
  712. /************************************************************************
  713. **
  714. ** lose_selection - Callback, called when some other client wishes
  715. ** to take ownership of one of the servers selections ...
  716. ** should never happen.
  717. **
  718. ************************************************************************/
  719. static void
  720. lose_selection(
  721. Widget w,
  722. Atom *selection )
  723. {
  724. char *tmpStr, *tmpStr2, *tmpStr3;
  725. Atom pixel_set_atom ;
  726. pixel_set_atom = XInternAtom(XtDisplay(w), XmSPIXEL_SET_PROP, FALSE) ;
  727. XDeleteProperty(XtDisplay(w), XtWindow(shell[0]), pixel_set_atom) ;
  728. tmpStr = (char *)SRV_MALLOC(strlen(MSG2) + strlen(MSG2a) + 6);
  729. tmpStr2 = (char *)SRV_MALLOC(strlen(MSG2) + 1);
  730. if (selection != NULL) {
  731. tmpStr3 = XGetAtomName(XtDisplay(w), *selection);
  732. } else {
  733. tmpStr3 = NULL;
  734. }
  735. printf(tmpStr2,"%s", MSG2);
  736. sprintf(tmpStr,"%s%s%s", tmpStr2, (tmpStr3 == NULL) ? "(null)" : tmpStr3, MSG2a);
  737. _DtSimpleError(XmSCOLOR_SRV_NAME, DtWarning, NULL, tmpStr, NULL);
  738. SRV_FREE(tmpStr);
  739. SRV_FREE(tmpStr2);
  740. }
  741. /*****************************************************************************
  742. **
  743. ** FindMaximumDefault .. used when the actual or user defaults for
  744. ** TypeOfMonitor, UsePixmaps, and FgColor try to allocate more cells than
  745. ** are available .. this finds and allocates the maximum that are available
  746. ** It also adjusts TypeOfMonitor, UsePixmaps, and FgColor accordingly.
  747. **
  748. ******************************************************************************/
  749. static int
  750. FindMaximumDefault(
  751. Display *dpy,
  752. int screen_number )
  753. {
  754. int numOfPixelsLeft;
  755. /* go find the Number of pixels left to allocate */
  756. numOfPixelsLeft = FindNumOfPixels(dpy, screen_number);
  757. if(numOfPixelsLeft < 4)
  758. {
  759. /* Use Black and White */
  760. colorSrv.TypeOfMonitor[screen_number] = XmCO_BLACK_WHITE;
  761. return(1);
  762. }
  763. if((colorSrv.TypeOfMonitor[screen_number] ==
  764. XmCO_HIGH_COLOR && numOfPixelsLeft >= 40) ||
  765. (colorSrv.TypeOfMonitor[screen_number] ==
  766. XmCO_MEDIUM_COLOR && numOfPixelsLeft >= 20) ||
  767. (colorSrv.TypeOfMonitor[screen_number] ==
  768. XmCO_LOW_COLOR && numOfPixelsLeft >= 10))
  769. {
  770. /* should never get here */
  771. return(0);
  772. }
  773. else if(colorSrv.TypeOfMonitor[screen_number] == XmCO_HIGH_COLOR)
  774. {
  775. if(numOfPixelsLeft >= 32) /* was asking for 40 */
  776. {
  777. colorSrv.UsePixmaps[screen_number] = FALSE;
  778. colorSrv.FgColor[screen_number] = WHITE;
  779. return(32);
  780. }
  781. else if(numOfPixelsLeft >= 24)
  782. {
  783. colorSrv.UsePixmaps[screen_number] = TRUE;
  784. colorSrv.FgColor[screen_number] = DYNAMIC;
  785. return(24);
  786. }
  787. else if(numOfPixelsLeft >= 16)
  788. {
  789. colorSrv.UsePixmaps[screen_number] = TRUE;
  790. colorSrv.FgColor[screen_number] = WHITE;
  791. return(16);
  792. }
  793. else /* can't use XmCO_HIGH_COLOR anymore so set to
  794. next highest XmCO_MEDIUM_COLOR */
  795. {
  796. colorSrv.TypeOfMonitor[screen_number] = XmCO_MEDIUM_COLOR;
  797. colorSrv.pCurrentPalette[screen_number]->num_of_colors = 4;
  798. }
  799. }
  800. /* need to do an if instead of an else because TypeOfMonitor can be reset
  801. in the else if above */
  802. if(colorSrv.TypeOfMonitor[screen_number] == XmCO_MEDIUM_COLOR)
  803. {
  804. if(numOfPixelsLeft >= 16)
  805. {
  806. colorSrv.UsePixmaps[screen_number] = FALSE;
  807. colorSrv.FgColor[screen_number] = WHITE;
  808. return(16);
  809. }
  810. if(numOfPixelsLeft >= 12)
  811. {
  812. colorSrv.UsePixmaps[screen_number] = TRUE;
  813. colorSrv.FgColor[screen_number] = DYNAMIC;
  814. return(12);
  815. }
  816. else if(numOfPixelsLeft >= 8)
  817. {
  818. colorSrv.UsePixmaps[screen_number] = TRUE;
  819. colorSrv.FgColor[screen_number] = WHITE;
  820. return(8);
  821. }
  822. else /* can't use XmCO_MEDIUM_COLOR anymore so set to next */
  823. /* highest XmCO_LOW_COLOR*/
  824. {
  825. colorSrv.TypeOfMonitor[screen_number] = XmCO_LOW_COLOR;
  826. colorSrv.pCurrentPalette[screen_number]->num_of_colors = 2;
  827. SwitchAItoPS(colorSrv.pCurrentPalette[screen_number]);
  828. }
  829. }
  830. /* need to do an if instead of an else because TypeOfMonitor can be reset
  831. in the else if above */
  832. if(colorSrv.TypeOfMonitor[screen_number] == XmCO_LOW_COLOR)
  833. {
  834. if(numOfPixelsLeft >= 10)
  835. {
  836. colorSrv.UsePixmaps[screen_number] = FALSE;
  837. colorSrv.FgColor[screen_number] = DYNAMIC;
  838. return(10);
  839. }
  840. else if(numOfPixelsLeft >= 8)
  841. {
  842. colorSrv.UsePixmaps[screen_number] = FALSE;
  843. colorSrv.FgColor[screen_number] = WHITE;
  844. return(8);
  845. }
  846. else if(numOfPixelsLeft >= 6)
  847. {
  848. colorSrv.UsePixmaps[screen_number] = TRUE;
  849. colorSrv.FgColor[screen_number] = DYNAMIC;
  850. return(6);
  851. }
  852. else if(numOfPixelsLeft >= 4)
  853. {
  854. colorSrv.UsePixmaps[screen_number] = TRUE;
  855. colorSrv.FgColor[screen_number] = WHITE;
  856. return(4);
  857. }
  858. }
  859. /* should never get here */
  860. return(0);
  861. }
  862. /****************************************************************************
  863. **
  864. ** FindNumOfPixels ... routine used to determine the num of allocable cells
  865. ** left in the default colormap. With this number we can determine the
  866. ** Maximum default for the user
  867. **
  868. ******************************************************************************/
  869. static int
  870. FindNumOfPixels(
  871. Display *dpy,
  872. int screen_number )
  873. {
  874. unsigned long *pixels;
  875. unsigned long plane_mask;
  876. int i, iterations, status;
  877. int num_of_pixels, count, base, countdown;
  878. Colormap colormap;
  879. colormap = DefaultColormap(dpy, screen_number);
  880. /* get the total number of cells in this screen */
  881. num_of_pixels = XDisplayCells(dpy, screen_number);
  882. /* get the number of iterations to be used .. the number of plane in this
  883. screen */
  884. iterations = XDisplayPlanes(dpy, screen_number);
  885. /* Allocate enough space to store the pixels. */
  886. pixels = (unsigned long *)SRV_MALLOC (num_of_pixels * sizeof (unsigned long));
  887. /* now iterate through until the we know how many cells are available */
  888. count = num_of_pixels;
  889. countdown = count;
  890. base = 0;
  891. for(i = 0; i < iterations; i++)
  892. {
  893. status = XAllocColorCells (dpy, colormap, (Boolean)0, &plane_mask,
  894. 0, pixels, count);
  895. countdown = countdown / 2;
  896. if(status == False)
  897. {
  898. count = base + countdown;
  899. }
  900. else
  901. {
  902. XFreeColors(dpy, colormap, pixels, count, (unsigned long)0);
  903. if(count != num_of_pixels)
  904. {
  905. base = count;
  906. count = base + countdown;
  907. }
  908. }
  909. }
  910. status = XAllocColorCells (dpy, colormap, (Boolean)0, &plane_mask,
  911. 0, pixels, count);
  912. if(status == False)
  913. count--;
  914. else
  915. XFreeColors(dpy, colormap, pixels, count, (unsigned long)0);
  916. SRV_FREE((char *) pixels);
  917. return(count);
  918. }
  919. /************************************************************************
  920. **
  921. ** GetNumOfPixels - returns the number of pixels to allocate based on the
  922. ** resources ColorUse(TypeOfMonitor), ShadowPixmaps(UsePixmaps),
  923. ** and ForegroundColor(FgColor).
  924. **
  925. ************************************************************************/
  926. static int
  927. GetNumOfPixels(
  928. int screen_number )
  929. {
  930. if(colorSrv.TypeOfMonitor[screen_number] == XmCO_BLACK_WHITE)
  931. {
  932. return(0);
  933. }
  934. else /* non Black and White monitor */
  935. {
  936. if(colorSrv.UsePixmaps[screen_number] == FALSE)
  937. {
  938. if(colorSrv.FgColor[screen_number] == DYNAMIC)
  939. {
  940. return(colorSrv.pCurrentPalette[screen_number]->num_of_colors * 5);
  941. }
  942. else /*FgColor == BLACK or WHITE ... bg, ts, bs, & sc used */
  943. {
  944. return(colorSrv.pCurrentPalette[screen_number]->num_of_colors * 4);
  945. }
  946. }
  947. else /* UsePixmaps == True */
  948. {
  949. if(colorSrv.FgColor[screen_number] == DYNAMIC) /* fg, bg, & sc used */
  950. {
  951. return(colorSrv.pCurrentPalette[screen_number]->num_of_colors * 3);
  952. }
  953. else /*FgColor == BLACK or WHITE ... bg & sc used */
  954. {
  955. return(colorSrv.pCurrentPalette[screen_number]->num_of_colors * 2);
  956. }
  957. }
  958. }
  959. }
  960. /************************************************************************
  961. **
  962. ** MatchAndStore - match the pixels already allocated with the current
  963. ** palettes storage .. then do a Store Colors to set the colors
  964. ** correctly at the X server.
  965. **
  966. ************************************************************************/
  967. static void
  968. MatchAndStore(
  969. Display *dpy,
  970. int screen_number,
  971. unsigned long *pixels )
  972. {
  973. int i, count = 0;
  974. struct _palette *p;
  975. XColor *xcolor;
  976. p = colorSrv.pCurrentPalette[screen_number];
  977. xcolor = (XColor *)SRV_MALLOC (p->num_of_colors * 5 * sizeof (XColor));
  978. for(i = 0; i < p->num_of_colors; i++)
  979. {
  980. /* Background Pixel */
  981. p->color[i].bg.pixel = pixels[count];
  982. p->color[i].bg.flags = DoRed | DoGreen | DoBlue;
  983. xcolor[count] = p->color[i].bg;
  984. count++;
  985. /* SelectColor (ArmColor) Pixel */
  986. p->color[i].sc.pixel = pixels[count];
  987. p->color[i].sc.flags = DoRed | DoGreen | DoBlue;
  988. xcolor[count] = p->color[i].sc;
  989. count++;
  990. if(colorSrv.UsePixmaps[screen_number] == FALSE)
  991. {
  992. /* TopShadow Pixel */
  993. p->color[i].ts.pixel = pixels[count];
  994. p->color[i].ts.flags = DoRed | DoGreen | DoBlue;
  995. xcolor[count] = p->color[i].ts;
  996. count++;
  997. /* BottomShadow Pixel */
  998. p->color[i].bs.pixel = pixels[count];
  999. p->color[i].bs.flags = DoRed | DoGreen | DoBlue;
  1000. xcolor[count] = p->color[i].bs;
  1001. count++;
  1002. }
  1003. else /* colorSrv.UsePixmaps = True */
  1004. {
  1005. /* TopShadow Pixel set to white */
  1006. p->color[i].ts.pixel = WhitePixel(dpy,screen_number);
  1007. p->color[i].ts.flags = 0;
  1008. /* BottomShadow Pixel set to black */
  1009. p->color[i].bs.pixel = BlackPixel(dpy,screen_number);
  1010. p->color[i].bs.flags = 0;
  1011. }
  1012. if(colorSrv.FgColor[screen_number] == DYNAMIC)
  1013. {
  1014. /* Foreground Pixel */
  1015. p->color[i].fg.pixel = pixels[count];
  1016. p->color[i].fg.flags = DoRed | DoGreen | DoBlue;
  1017. xcolor[count] = p->color[i].fg;
  1018. count++;
  1019. }
  1020. else if(colorSrv.FgColor[screen_number] == BLACK)
  1021. {
  1022. /* Foreground Pixel set to BLACK */
  1023. p->color[i].fg.pixel = BlackPixel(dpy,screen_number);
  1024. p->color[i].fg.flags = 0;
  1025. }
  1026. else
  1027. {
  1028. /* Foreground Pixel set to WHITE */
  1029. p->color[i].fg.pixel = WhitePixel(dpy,screen_number);
  1030. p->color[i].fg.flags = 0;
  1031. }
  1032. } /* for */
  1033. if (count > 0)
  1034. {
  1035. XStoreColors(dpy, DefaultColormap(dpy, screen_number), xcolor, count);
  1036. }
  1037. SRV_FREE((char *) xcolor);
  1038. }
  1039. /************************************************************************
  1040. **
  1041. ** AllocReadWrite - Allocates Read/Write cells for use by the color
  1042. ** server. If the X server can't allocate enough cells (numOfPixels)
  1043. ** this routine finds the number of pixels available, and sets
  1044. ** the varibles TypeOfMonitor, UsePixmaps, and FgColor accordingly.
  1045. **
  1046. ************************************************************************/
  1047. static Boolean
  1048. AllocReadWrite(
  1049. Display *dpy,
  1050. int screen_number,
  1051. int numOfPixels )
  1052. {
  1053. char *tmpStr;
  1054. unsigned long *pixels;
  1055. unsigned long plane_mask;
  1056. int status;
  1057. /* Allocate enough space to store the pixels. */
  1058. pixels = (unsigned long *)SRV_MALLOC (numOfPixels * sizeof (unsigned long));
  1059. /* Now actually allocate R/W pixels */
  1060. status = XAllocColorCells (dpy, DefaultColormap(dpy, screen_number),
  1061. (Boolean)0, &plane_mask, 0, pixels, numOfPixels);
  1062. /* When status is false means the alloc couldn't get all the pixels
  1063. the user wanted or what the default is .. so lets go find the
  1064. minumum and set up and use that */
  1065. if(status == False)
  1066. {
  1067. SRV_FREE((char *) pixels);
  1068. numOfPixels = FindMaximumDefault(dpy, screen_number);
  1069. if(numOfPixels == 0)
  1070. {
  1071. tmpStr = (char *)SRV_MALLOC(strlen(MSG3) + 6);
  1072. sprintf(tmpStr,"%s%d", MSG3, screen_number);
  1073. _DtSimpleError(XmSCOLOR_SRV_NAME, DtWarning, NULL, tmpStr, NULL);
  1074. SRV_FREE(tmpStr);
  1075. return(False);
  1076. }
  1077. else
  1078. {
  1079. if(colorSrv.TypeOfMonitor[screen_number] != XmCO_BLACK_WHITE)
  1080. {
  1081. /* Allocate enough space to store the pixels. */
  1082. pixels = (unsigned long *)SRV_MALLOC (numOfPixels *
  1083. sizeof (unsigned long));
  1084. /* Now actually allocate R/W pixels */
  1085. status = XAllocColorCells(dpy, DefaultColormap(dpy,screen_number),
  1086. (Boolean)0, &plane_mask, 0, pixels, numOfPixels);
  1087. if(status == False)
  1088. {
  1089. SRV_FREE((char *) pixels);
  1090. _DtSimpleError(XmSCOLOR_SRV_NAME, DtWarning, NULL, ((char *)GETMESSAGE(26, 5, "Warning, can't allocate enough pixels.\n")), NULL);
  1091. return(False);
  1092. }
  1093. }
  1094. }
  1095. }
  1096. if(colorSrv.TypeOfMonitor[screen_number] != XmCO_BLACK_WHITE)
  1097. {
  1098. /* Go match pixels allocated with the colorsets then use store
  1099. XStoreColors to set the RGB values of them */
  1100. MatchAndStore(dpy, screen_number, pixels);
  1101. }
  1102. /* free the allocated space for pixels */
  1103. SRV_FREE((char *) pixels);
  1104. return(True);
  1105. }
  1106. /************************************************************************
  1107. **
  1108. ** AllocReadOnly - Allocates Read Only cells for use by the color
  1109. ** server. If the X server can't allocate the cell it finds the
  1110. ** closest approximation to the color of a cell already allocated.
  1111. ** Therefore there is no error recorded.
  1112. **
  1113. ************************************************************************/
  1114. static void
  1115. AllocReadOnly(
  1116. Display *dpy,
  1117. int screen_number )
  1118. {
  1119. int i;
  1120. for(i=0; i < colorSrv.pCurrentPalette[screen_number]->num_of_colors; i++)
  1121. {
  1122. XAllocColor(dpy, DefaultColormap(dpy, screen_number),
  1123. &(colorSrv.pCurrentPalette[screen_number]->color[i].bg));
  1124. XAllocColor(dpy, DefaultColormap(dpy, screen_number),
  1125. &(colorSrv.pCurrentPalette[screen_number]->color[i].sc));
  1126. /* Check UsePixmaps varible */
  1127. if(colorSrv.UsePixmaps[screen_number] == FALSE)
  1128. {
  1129. XAllocColor(dpy, DefaultColormap(dpy, screen_number),
  1130. &(colorSrv.pCurrentPalette[screen_number]->color[i].ts));
  1131. XAllocColor(dpy, DefaultColormap(dpy, screen_number),
  1132. &(colorSrv.pCurrentPalette[screen_number]->color[i].bs));
  1133. }
  1134. else /* colorSrv.UsePixmaps[screen_number] == True */
  1135. {
  1136. colorSrv.pCurrentPalette[screen_number]->color[i].ts.pixel =
  1137. WhitePixel(dpy,screen_number);
  1138. colorSrv.pCurrentPalette[screen_number]->color[i].bs.pixel =
  1139. BlackPixel(dpy,screen_number);
  1140. }
  1141. /* Check FgColor varible */
  1142. if(colorSrv.FgColor[screen_number] == DYNAMIC)
  1143. XAllocColor(dpy, DefaultColormap(dpy, screen_number),
  1144. &(colorSrv.pCurrentPalette[screen_number]->color[i].fg));
  1145. else if(colorSrv.FgColor[screen_number] == BLACK)
  1146. colorSrv.pCurrentPalette[screen_number]->color[i].fg.pixel =
  1147. BlackPixel(dpy,screen_number);
  1148. else
  1149. colorSrv.pCurrentPalette[screen_number]->color[i].fg.pixel =
  1150. WhitePixel(dpy,screen_number);
  1151. }
  1152. }
  1153. /*********************************************************************
  1154. **
  1155. ** Converter which converts a string to the ColorUse value
  1156. **
  1157. **********************************************************************/
  1158. static void
  1159. CvtStringToColorUse(
  1160. XrmValue *args,
  1161. Cardinal *num_args,
  1162. XrmValue *from_val,
  1163. XrmValue *to_val )
  1164. {
  1165. char * in_str = (char *) (from_val->addr);
  1166. static int i;
  1167. to_val->size = sizeof (int);
  1168. to_val->addr = (XtPointer) &i;
  1169. if (_DtWmStringsAreEqual (in_str, "high_color"))
  1170. i = XmCO_HIGH_COLOR;
  1171. else if (_DtWmStringsAreEqual (in_str, "medium_color"))
  1172. i = XmCO_MEDIUM_COLOR;
  1173. else if (_DtWmStringsAreEqual (in_str, "low_color"))
  1174. i = XmCO_LOW_COLOR;
  1175. else if (_DtWmStringsAreEqual (in_str, "b_w"))
  1176. i = XmCO_BLACK_WHITE;
  1177. else if (_DtWmStringsAreEqual (in_str, "default"))
  1178. i = DEFAULT;
  1179. else
  1180. {
  1181. to_val->size = 0;
  1182. to_val->addr = NULL;
  1183. XtStringConversionWarning ((char *)from_val->addr, DtRColorUse);
  1184. }
  1185. }
  1186. /**********************************************************************
  1187. **
  1188. ** Converter which converts a string to the ForegroundColor value
  1189. **
  1190. **********************************************************************/
  1191. static void
  1192. CvtStringToForegroundColor(
  1193. XrmValue *args,
  1194. Cardinal *num_args,
  1195. XrmValue *from_val,
  1196. XrmValue *to_val )
  1197. {
  1198. char * in_str = (char *) (from_val->addr);
  1199. static int i;
  1200. to_val->size = sizeof (int);
  1201. to_val->addr = (XtPointer) &i;
  1202. if (_DtWmStringsAreEqual (in_str, "dynamic"))
  1203. i = DYNAMIC;
  1204. else if (_DtWmStringsAreEqual (in_str, "black"))
  1205. i = BLACK;
  1206. else if (_DtWmStringsAreEqual (in_str, "white"))
  1207. i = WHITE;
  1208. else
  1209. {
  1210. to_val->size = 0;
  1211. to_val->addr = NULL;
  1212. XtStringConversionWarning ((char *)from_val->addr, DtRForegroundColor);
  1213. }
  1214. }
  1215. /***********************************************************************
  1216. **
  1217. ** Converter which converts a string to the ShadowPixmaps value
  1218. **
  1219. ***********************************************************************/
  1220. static void
  1221. CvtStringToShadowPixmaps(
  1222. XrmValue *args,
  1223. Cardinal *num_args,
  1224. XrmValue *from_val,
  1225. XrmValue *to_val )
  1226. {
  1227. char * in_str = (char *) (from_val->addr);
  1228. static int i;
  1229. to_val->size = sizeof (int);
  1230. to_val->addr = (XtPointer) &i;
  1231. if (_DtWmStringsAreEqual (in_str, "true"))
  1232. i = 1;
  1233. else if (_DtWmStringsAreEqual (in_str, "false"))
  1234. i = 0;
  1235. else if (_DtWmStringsAreEqual (in_str, "default"))
  1236. i = -1;
  1237. else
  1238. {
  1239. to_val->size = 0;
  1240. to_val->addr = NULL;
  1241. XtStringConversionWarning ((char *)from_val->addr, DtRShadowPixmaps);
  1242. }
  1243. }
  1244. /************************************************************************
  1245. *
  1246. * _DtWmStringsAreEqual
  1247. * Compare two strings and return true if equal.
  1248. * The comparison is on lower cased strings. It is the callers
  1249. * responsibility to ensure that test_str is already lower cased.
  1250. *
  1251. ************************************************************************/
  1252. static Boolean
  1253. _DtWmStringsAreEqual(
  1254. register char *in_str,
  1255. register char *test_str )
  1256. {
  1257. register int i;
  1258. register int j;
  1259. i = *in_str;
  1260. for (;;)
  1261. {
  1262. i = *in_str;
  1263. j = *test_str;
  1264. #ifdef MULTIBYTE
  1265. if ((mblen(in_str, MB_CUR_MAX) == 1))
  1266. if (isupper (i)) i = tolower (i);
  1267. #else
  1268. if (isupper (i)) i = tolower (i);
  1269. #endif
  1270. if (i != j) return (False);
  1271. if (i == 0) return (True);
  1272. in_str++;
  1273. test_str++;
  1274. }
  1275. }
  1276. /************************************************************************
  1277. *
  1278. * SetDefaults - set the TypeOfMonitor, UsePixmaps, FgColor, and DynamicColor
  1279. * for the screen passed in. Use the resource values, the number of
  1280. * colors for this screen, and the visual type of the screen to
  1281. * determine which values best fit.
  1282. *
  1283. *************************************************************************/
  1284. static void
  1285. SetDefaults(
  1286. Display *dpy,
  1287. int screen_number )
  1288. {
  1289. int numPlanes;
  1290. Visual *visual;
  1291. /* Initialize colorSrv data for this screen with specified resource values */
  1292. colorSrv.UsePixmaps[screen_number] = pColorSrvRsrc.ShadowPixmaps;
  1293. colorSrv.DynamicColor[screen_number] = pColorSrvRsrc.DynamicColor;
  1294. /* If this is a static color visual class, set DynamicColor to False. */
  1295. visual = XDefaultVisual(dpy, screen_number);
  1296. /* GrayScale and PseudoColor are the only visual types that make */
  1297. /* sense to change dynamically with the current implementation */
  1298. if ((visual->class != GrayScale) && (visual->class != PseudoColor))
  1299. colorSrv.DynamicColor[screen_number] = False;
  1300. /* if not specified, set ColorPalette default */
  1301. if (strcmp(pColorSrvRsrc.ColorPalette,"DEFAULT") == 0)
  1302. {
  1303. if ((visual->class == GrayScale) || (visual->class == StaticGray))
  1304. {
  1305. pColorSrvRsrc.ColorPalette =
  1306. XtMalloc(strlen(DEFAULT_GRAYSCALE_PALETTE)+1);
  1307. strcpy(pColorSrvRsrc.ColorPalette, DEFAULT_GRAYSCALE_PALETTE);
  1308. }
  1309. else
  1310. {
  1311. pColorSrvRsrc.ColorPalette =
  1312. XtMalloc(strlen(DEFAULT_COLOR_PALETTE)+1);
  1313. strcpy(pColorSrvRsrc.ColorPalette, DEFAULT_COLOR_PALETTE);
  1314. }
  1315. }
  1316. numPlanes = XDisplayPlanes(dpy, screen_number);
  1317. if( numPlanes < 3) /* 1 or 2 planes */
  1318. {
  1319. colorSrv.TypeOfMonitor[screen_number] = XmCO_BLACK_WHITE;
  1320. colorSrv.DynamicColor[screen_number] = False;
  1321. }
  1322. else if( numPlanes == 3 ) /* 3 planes */
  1323. {
  1324. switch(pColorSrvRsrc.ColorUse) {
  1325. case XmCO_LOW_COLOR:
  1326. colorSrv.TypeOfMonitor[screen_number] = XmCO_LOW_COLOR;
  1327. break;
  1328. default:
  1329. colorSrv.TypeOfMonitor[screen_number] = XmCO_BLACK_WHITE;
  1330. colorSrv.DynamicColor[screen_number] = False;
  1331. }
  1332. /* for 3 planes UsePixmaps (ShadowPixmaps) have to be on (TRUE) */
  1333. colorSrv.UsePixmaps[screen_number] = 1;
  1334. }
  1335. else if( numPlanes == 4 ) /* 4 planes */
  1336. {
  1337. switch(pColorSrvRsrc.ColorUse) {
  1338. case XmCO_MEDIUM_COLOR:
  1339. /* for 4 planes ColorUse = Med_color shadowPixmaps have to be True */
  1340. pColorSrvRsrc.ShadowPixmaps = -1;
  1341. colorSrv.TypeOfMonitor[screen_number] = XmCO_MEDIUM_COLOR;
  1342. break;
  1343. case XmCO_BLACK_WHITE:
  1344. colorSrv.TypeOfMonitor[screen_number] = XmCO_BLACK_WHITE;
  1345. colorSrv.DynamicColor[screen_number] = False;
  1346. break;
  1347. default:
  1348. colorSrv.TypeOfMonitor[screen_number] = XmCO_LOW_COLOR;
  1349. }
  1350. /* check to see what type of shadow pixmap to use */
  1351. if(pColorSrvRsrc.ShadowPixmaps == -1)
  1352. colorSrv.UsePixmaps[screen_number] = 1;
  1353. }
  1354. else if( numPlanes == 5 ) /* 5 planes */
  1355. {
  1356. switch(pColorSrvRsrc.ColorUse) {
  1357. case XmCO_HIGH_COLOR:
  1358. /* for 5 planes ColorUse = hi_color shadowPixmaps have to be True */
  1359. pColorSrvRsrc.ShadowPixmaps = -1;
  1360. colorSrv.TypeOfMonitor[screen_number] = XmCO_HIGH_COLOR;
  1361. break;
  1362. case XmCO_MEDIUM_COLOR:
  1363. colorSrv.TypeOfMonitor[screen_number] = XmCO_MEDIUM_COLOR;
  1364. break;
  1365. case XmCO_BLACK_WHITE:
  1366. colorSrv.TypeOfMonitor[screen_number] = XmCO_BLACK_WHITE;
  1367. colorSrv.DynamicColor[screen_number] = False;
  1368. break;
  1369. default:
  1370. colorSrv.TypeOfMonitor[screen_number] = XmCO_LOW_COLOR;
  1371. }
  1372. }
  1373. else /* 6 and above planes */
  1374. {
  1375. switch(pColorSrvRsrc.ColorUse) {
  1376. case XmCO_HIGH_COLOR:
  1377. colorSrv.TypeOfMonitor[screen_number] = XmCO_HIGH_COLOR;
  1378. break;
  1379. case XmCO_LOW_COLOR:
  1380. colorSrv.TypeOfMonitor[screen_number] = XmCO_LOW_COLOR;
  1381. break;
  1382. case XmCO_BLACK_WHITE:
  1383. colorSrv.TypeOfMonitor[screen_number] = XmCO_BLACK_WHITE;
  1384. colorSrv.DynamicColor[screen_number] = False;
  1385. break;
  1386. default:
  1387. colorSrv.TypeOfMonitor[screen_number] = XmCO_MEDIUM_COLOR;
  1388. }
  1389. /* check to see what type of shadow pixmap to use */
  1390. if(pColorSrvRsrc.ShadowPixmaps == -1)
  1391. colorSrv.UsePixmaps[screen_number] = 0;
  1392. }
  1393. /* Determine the correct foreground color useage */
  1394. switch(pColorSrvRsrc.ForegroundColor) {
  1395. case BLACK:
  1396. colorSrv.FgColor[screen_number] = BLACK;
  1397. break;
  1398. case WHITE:
  1399. colorSrv.FgColor[screen_number] = WHITE;
  1400. break;
  1401. default:
  1402. colorSrv.FgColor[screen_number] = DYNAMIC;
  1403. }
  1404. }