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 librararies and programs; if not, write
20 * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21 * Floor, Boston, MA 02110-1301 USA
23 /* $XConsortium: resource.c /main/4 1996/06/19 09:47:56 mustafa $ */
27 * (c) Copyright 1993, 1994 Hewlett-Packard Company *
28 * (c) Copyright 1993, 1994 International Business Machines Corp. *
29 * (c) Copyright 1993, 1994 Sun Microsystems, Inc. *
30 * (c) Copyright 1993, 1994 Novell, Inc. *
33 * resource.c - resource management for dtscreen, the X Window System lockscreen.
35 * Copyright (c) 1991 by Patrick J. Naughton.
37 * See dtscreen.c for copying information.
40 * 25-Sep-91: added worm mode.
41 * 06-Jun-91: Added flame mode.
42 * 16-May-91: Added random mode and pyro mode.
43 * 26-Mar-91: CheckResources: delay must be >= 0.
44 * 29-Oct-90: Added #include <ctype.h> for missing isupper() on some OS revs.
45 * moved -mode option, reordered Xrm database evaluation.
46 * 28-Oct-90: Added text strings.
47 * 26-Oct-90: Fix bug in mode specific options.
48 * 31-Jul-90: Fix ':' handling in parsefilepath
49 * 07-Jul-90: Created from resource work in dtscreen.c
60 #include <X11/Xresource.h>
62 /* include file for message texts */
65 #define MF_DTSCREEN "dtscreen.cat"
68 extern nl_catd scmc_catd; /* Cat descriptor for scmc conversion */
71 * Declare external interface routines for supported screen savers.
74 extern void inithop();
75 extern void drawhop();
77 extern void initlife();
78 extern void drawlife();
80 extern void initqix();
81 extern void drawqix();
83 extern void initimage();
84 extern void drawimage();
86 extern void initblank();
87 extern void drawblank();
89 extern void initswarm();
90 extern void drawswarm();
92 extern void initrotor();
93 extern void drawrotor();
95 extern void initpyro();
96 extern void drawpyro();
98 extern void initflame();
99 extern void drawflame();
101 extern void initworm();
102 extern void drawworm();
107 void (*lp_callback) ();
110 float def_saturation;
114 static char randomstring[] = "random";
116 static LockStruct LockProcs[] = {
117 {"hop", inithop, drawhop, 0, 1000, 1.0, "Hopalong iterated fractals"},
118 {"qix", initqix, drawqix, 30000, 64, 1.0, "Spinning lines a la Qix(tm)"},
119 {"image", initimage, drawimage, 2000000, 8, 0.3, "Random bouncing image"},
120 {"life", initlife, drawlife, 1000000, 100, 1.0, "Conway's game of Life"},
121 {"swarm", initswarm, drawswarm, 10000, 100, 1.0, "Swarm of bees"},
122 {"rotor", initrotor, drawrotor, 10000, 4, 0.4, "Rotor"},
123 {"pyro", initpyro, drawpyro, 15000, 40, 1.0, "Fireworks"},
124 {"flame", initflame, drawflame, 10000, 20, 1.0, "Cosmic Flame Fractals"},
125 {"worm", initworm, drawworm, 10000, 20, 1.0, "Wiggly Worms"},
126 {"blank", initblank, drawblank, 5000000, 1, 1.0, "Blank screen"},
127 {randomstring, NULL, NULL, 0, 0, 0.0, "Random mode"},
129 #define NUMPROCS (sizeof LockProcs / sizeof LockProcs[0])
132 extern char *getenv();
134 #ifndef DEF_FILESEARCHPATH
135 #define DEF_FILESEARCHPATH "/usr/lib/X11/%T/%N%S"
137 #define DEF_DISPLAY ":0"
138 #define DEF_MODE "swarm"
139 #define DEF_BG "White"
140 #define DEF_FG "Black"
141 #define DEF_BC "100" /* vectors (or whatever) per batch */
142 #define DEF_DELAY "200000"/* microseconds between batches */
143 #define DEF_NICE "10" /* dtscreen process nicelevel */
144 #define DEF_SAT "1.0" /* color ramp saturation 0->1 */
145 #define DEF_CLASSNAME "Dtscreen"
147 static char *classname;
148 static char modename[1024];
149 static char modeclass[1024];
151 static XrmOptionDescRec genTable[] = {
152 {"-mode", ".mode", XrmoptionSepArg, (caddr_t) NULL},
153 {"-mono", ".mono", XrmoptionNoArg, (caddr_t) "on"},
154 {"+mono", ".mono", XrmoptionNoArg, (caddr_t) "off"},
155 {"-nice", ".nice", XrmoptionSepArg, (caddr_t) NULL},
156 {"-create", ".create", XrmoptionNoArg, (caddr_t) "on"},
158 #define genEntries (sizeof genTable / sizeof genTable[0])
160 /*************************************************************/
161 /** This table was changed for AIX. In order to read these **/
162 /** command line options properly, the specifier field must **/
163 /** be built on the fly. **/
164 /*************************************************************/
165 static XrmOptionDescRec modeTable[] = {
166 {"-delay", NULL, XrmoptionSepArg, (caddr_t) NULL},
167 {"-batchcount", NULL, XrmoptionSepArg, (caddr_t) NULL},
168 {"-saturation", NULL, XrmoptionSepArg, (caddr_t) NULL},
170 #define modeEntries (sizeof modeTable / sizeof modeTable[0])
172 static XrmOptionDescRec cmdlineTable[] = {
173 {"-display", ".display", XrmoptionSepArg, (caddr_t) NULL},
174 {"-xrm", NULL, XrmoptionResArg, (caddr_t) NULL},
176 #define cmdlineEntries (sizeof cmdlineTable / sizeof cmdlineTable[0])
178 static XrmOptionDescRec nameTable[] = {
179 {"-name", ".name", XrmoptionSepArg, (caddr_t) NULL},
188 static OptionStruct opDesc[] = {
189 {"-help", "print out this message"},
190 {"-resources", "print default resource file to standard output"},
191 {"-display displayname", "X server to contact"},
192 {"-/+mono", "turn on/off monochrome override"},
193 {"-delay usecs", "microsecond delay between screen updates"},
194 {"-batchcount num", "number of things per batch"},
195 {"-nice level", "nice level for dtscreen process"},
196 {"-saturation value", "saturation of color ramp"},
197 {"-create", "create a window in which to draw"},
199 #define opDescEntries (sizeof opDesc / sizeof opDesc[0])
224 static argtype genvars[] = {
225 {(caddr_t *) &nicelevel, "nice", "Nice", DEF_NICE, t_Int},
226 {(caddr_t *) &mono, "mono", "Mono", "off", t_Bool},
227 {(caddr_t *) &create, "create", "Create", "off", t_Bool},
229 #define NGENARGS (sizeof genvars / sizeof genvars[0])
231 static argtype modevars[] = {
232 {(caddr_t *) &delay, "delay", "Delay", DEF_DELAY, t_Int},
233 {(caddr_t *) &batchcount, "batchcount", "BatchCount", DEF_BC, t_Int},
234 {(caddr_t *) &saturation, "saturation", "Saturation", DEF_SAT, t_Float},
236 #define NMODEARGS (sizeof modevars / sizeof modevars[0])
246 fprintf(stderr, "%s: bad command line option: %s.\n\n",
247 ProgramName, badOption);
249 fprintf(stderr, catgets(scmc_catd, 2, 1,
250 "%s: Bad command line option: %s.\n\n"),
251 ProgramName, badOption);
254 fprintf(stderr, "usage: %s", ProgramName);
255 col = 8 + strlen(ProgramName);
256 for (i = 0; i < opDescEntries; i++) {
257 len = 3 + strlen(opDesc[i].opt); /* space [ string ] */
258 if (col + len > 79) {
259 fprintf(stderr, "\n "); /* 3 spaces */
262 fprintf(stderr, " [%s]", opDesc[i].opt);
266 len = 8 + strlen(LockProcs[0].cmdline_arg);
267 if (col + len > 79) {
268 fprintf(stderr, "\n "); /* 3 spaces */
271 fprintf(stderr, " [-mode %s", LockProcs[0].cmdline_arg);
273 for (i = 1; i < NUMPROCS; i++) {
274 len = 3 + strlen(LockProcs[i].cmdline_arg);
275 if (col + len > 79) {
276 fprintf(stderr, "\n "); /* 3 spaces */
279 fprintf(stderr, " | %s", LockProcs[i].cmdline_arg);
282 fprintf(stderr, "]\n");
285 fprintf(stderr, "\nType %s -help for a full description.\n\n",
288 fprintf(stderr, catgets(scmc_catd, 2, 2,
289 "\nType %s -help for a full description.\n\n"),
301 fprintf(stderr, "usage:\n %s [-options ...]\n\n", ProgramName);
302 fprintf(stderr, "where options include:\n");
305 fprintf(stderr, catgets(scmc_catd, 2, 3,
306 "Usage:\n %s [-options ...]\n\n\
307 where options include:\n"), ProgramName);
310 for (i = 0; i < opDescEntries; i++) {
311 fprintf(stderr, " %-28s %s\n", opDesc[i].opt, opDesc[i].desc);
315 fprintf(stderr, " %-28s %s\n", "-mode mode", "animation mode");
316 fprintf(stderr, " where mode is one of:\n");
318 fprintf(stderr, catgets(scmc_catd, 2, 5,
319 " %-28s %s\n\t where mode is one of:\n"),
320 "-mode mode", "animation mode");
322 for (i = 0; i < NUMPROCS; i++) {
323 fprintf(stderr, " %-23s %s\n",
324 LockProcs[i].cmdline_arg, LockProcs[i].desc);
336 printf("%s.mode: %s\n", classname, DEF_MODE);
338 for (i = 0; i < NGENARGS; i++)
339 printf("%s.%s: %s\n",
340 classname, genvars[i].name, genvars[i].def);
342 for (i = 0; i < NUMPROCS - 1; i++) {
343 printf("%s.%s.%s: %d\n", classname, LockProcs[i].cmdline_arg,
344 "delay", LockProcs[i].def_delay);
345 printf("%s.%s.%s: %d\n", classname, LockProcs[i].cmdline_arg,
346 "batchcount", LockProcs[i].def_batchcount);
347 printf("%s.%s.%s: %g\n", classname, LockProcs[i].cmdline_arg,
348 "saturation", LockProcs[i].def_saturation);
367 GetResource(database, parentname, parentclass,
368 name, class, valueType, def, valuep)
369 XrmDatabase database;
376 caddr_t *valuep; /* RETURN */
383 char fullclass[1024];
386 sprintf(fullname, "%s.%s", parentname, name);
387 sprintf(fullclass, "%s.%s", parentclass, class);
388 if (XrmGetResource(database, fullname, fullclass, &type, &value)) {
393 len = strlen(string);
395 (void) strncpy(buffer, string, sizeof(buffer));
396 buffer[sizeof(buffer) - 1] = '\0';
402 s = (char *) malloc(len + 1);
403 if (s == (char *) NULL)
405 error("%s: GetResource - couldn't allocate memory");
408 fprintf(stderr, catgets(scmc_catd, 2, 18,
409 "%s: GetResource - couldn't allocate memory.\n"),ProgramName);
413 (void) strncpy(s, string, len);
415 *((char **) valuep) = s;
420 *((int *) valuep) = (!strcmp(buffer, "true") ||
421 !strcmp(buffer, "on") ||
422 !strcmp(buffer, "enabled") ||
423 !strcmp(buffer, "yes")) ? True : False;
426 *((int *) valuep) = atoi(buffer);
429 *((float *) valuep) = (float) atof(buffer);
436 parsefilepath(xfilesearchpath, TypeName, ClassName)
437 char *xfilesearchpath;
441 XrmDatabase database = NULL;
442 char appdefaults[1024];
446 src = xfilesearchpath;
447 appdefaults[0] = '\0';
459 (void) strcat(dst, TypeName);
461 dst += strlen(TypeName);
464 (void) strcat(dst, ClassName);
466 dst += strlen(ClassName);
475 } else if (*src == ':') {
476 database = XrmGetFileDatabase(appdefaults);
477 if (database == NULL) {
482 } else if (*src == '\0') {
483 database = XrmGetFileDatabase(appdefaults);
493 /*******************************************************************/
494 /** screenIOErrorHandler **/
496 /** this function will exit cleanly when the connection is broken **/
497 /*******************************************************************/
498 static int screenIOErrorHandler(dpy)
508 if (display != NULL) {
509 char *colon = strchr(display, ':');
510 int n = colon - display;
514 error("%s: Malformed -display argument, \"%s\"\n", display);
517 fprintf(stderr, catgets(scmc_catd, 2, 19,
518 "%s: Malformed -display argument: %s.\n"), ProgramName,display);
525 if (!(dsp = XOpenDisplay(display)))
527 error("%s: unable to open display %s.\n", display);
530 fprintf(stderr, catgets(scmc_catd, 2, 17,
531 "%s: Unable to open display %s.\n"),ProgramName, display);
536 XSetIOErrorHandler(screenIOErrorHandler);
546 fprintf(stderr, "%s.%s: %s\n",
547 class, var.name, *((char **) var.var));
550 fprintf(stderr, "%s.%s: %s\n",
551 class, var.name, *((int *) var.var)
555 fprintf(stderr, "%s.%s: %d\n",
556 class, var.name, *((int *) var.var));
559 fprintf(stderr, "%s.%s: %g\n",
560 class, var.name, *((float *) var.var));
567 GetResources(argc, argv)
571 XrmDatabase RDB = NULL;
572 XrmDatabase modeDB = NULL;
573 XrmDatabase nameDB = NULL;
574 XrmDatabase cmdlineDB = NULL;
575 XrmDatabase generalDB = NULL;
576 XrmDatabase homeDB = NULL;
577 XrmDatabase applicationDB = NULL;
578 XrmDatabase serverDB = NULL;
579 XrmDatabase userDB = NULL;
586 /***************************/
587 /** new variables for AIX **/
588 /***************************/
589 char delaySpecifier[64];
590 char batchcountSpecifier[64];
591 char saturationSpecifier[64];
595 for (i = 0; i < argc; i++) {
596 if (!strncmp(argv[i], "-help", strlen(argv[i])))
602 * get -name arg from command line so you can have different resource
603 * files for different configurations/machines etc...
605 XrmParseCommand(&nameDB, nameTable, 1, ProgramName,
607 GetResource(nameDB, ProgramName, "*", "name", "Name", t_String,
608 DEF_CLASSNAME, &classname);
610 homeenv = getenv("HOME");
614 env = getenv("XFILESEARCHPATH");
615 applicationDB = parsefilepath(env ? env : DEF_FILESEARCHPATH,
616 "app-defaults", classname);
618 XrmParseCommand(&cmdlineDB, cmdlineTable, cmdlineEntries, ProgramName,
621 userpath = getenv("XUSERFILESEARCHPATH");
623 env = getenv("XAPPLRESDIR");
625 sprintf(userfile, "%s/%%N:%s/%%N", env, homeenv);
627 sprintf(userfile, "%s/%%N", homeenv);
630 userDB = parsefilepath(userpath, "app-defaults", classname);
632 (void) XrmMergeDatabases(applicationDB, &RDB);
633 (void) XrmMergeDatabases(userDB, &RDB);
634 (void) XrmMergeDatabases(cmdlineDB, &RDB);
636 env = getenv("DISPLAY");
637 GetResource(RDB, ProgramName, classname, "display", "Display", t_String,
638 env ? env : DEF_DISPLAY, &display);
640 serverString = XResourceManagerString(dsp);
642 serverDB = XrmGetStringDatabase(serverString);
643 (void) XrmMergeDatabases(serverDB, &RDB);
646 sprintf(buf, "%s/.Xdefaults", homeenv);
647 homeDB = XrmGetFileDatabase(buf);
648 (void) XrmMergeDatabases(homeDB, &RDB);
651 XrmParseCommand(&generalDB, genTable, genEntries, ProgramName, &argc, argv);
652 (void) XrmMergeDatabases(generalDB, &RDB);
654 GetResource(RDB, ProgramName, classname, "mode", "Mode", t_String,
655 DEF_MODE, (caddr_t *) &mode);
658 * if random< mode, then just grab a random entry from the table
660 if (!strcmp(mode, randomstring))
661 mode = LockProcs[random() % (NUMPROCS - 2)].cmdline_arg;
663 sprintf(modename, "%s.%s", ProgramName, mode);
664 sprintf(modeclass, "%s.%s", classname, mode);
667 /*********************************************************************/
668 /** New code for AIX **/
669 /** We must build the specifier fields of the modeTable on the fly. **/
670 /*********************************************************************/
671 sprintf(delaySpecifier, ".%s.delay", mode);
672 sprintf(batchcountSpecifier, ".%s.batchcount", mode);
673 sprintf(saturationSpecifier, ".%s.saturation", mode);
674 modeTable[0].specifier = delaySpecifier;
675 modeTable[1].specifier = batchcountSpecifier;
676 modeTable[2].specifier = saturationSpecifier;
679 XrmParseCommand(&modeDB, modeTable, modeEntries, ProgramName, &argc, argv);
680 (void) XrmMergeDatabases(modeDB, &RDB);
682 /* Parse the rest of the command line */
683 for (argc--, argv++; argc > 0; argc--, argv++) {
686 switch (argv[0][1]) {
696 /* the RDB is set, now query load the variables from the database */
698 for (i = 0; i < NGENARGS; i++)
699 GetResource(RDB, ProgramName, classname,
700 genvars[i].name, genvars[i].class,
701 genvars[i].type, genvars[i].def, genvars[i].var);
703 for (i = 0; i < NMODEARGS; i++)
704 GetResource(RDB, modename, modeclass,
705 modevars[i].name, modevars[i].class,
706 modevars[i].type, modevars[i].def, modevars[i].var);
708 (void) XrmDestroyDatabase(RDB);
718 Syntax("-batchcount argument must be positive.");
719 if (saturation < 0.0 || saturation > 1.0)
720 Syntax("-saturation argument must be between 0.0 and 1.0.");
722 Syntax("-delay argument must be positive.");
724 for (i = 0; i < NUMPROCS; i++) {
725 if (!strncmp(LockProcs[i].cmdline_arg, mode, strlen(mode))) {
726 init = LockProcs[i].lp_init;
727 callback = LockProcs[i].lp_callback;
733 fprintf(stderr, "Unknown mode: ");
735 fprintf(stderr, "%s", catgets(scmc_catd, 2, 7,