|
|
1.1 root 1: /*
2: * $header: xset.c,v 1.18 87/07/11 08:47:46 dkk Locked $
3: * $Locker: $
4: */
5: #include <X11/copyright.h>
6:
7: /* Copyright Massachusetts Institute of Technology 1985 */
8:
9: #ifndef lint
10: static char *rcsid_xset_c = "$Header: xset.c,v 1.22 87/09/12 23:14:10 toddb Exp $";
11: #endif
12:
13: #include <X11/X.h> /* Should be transplanted to X11/Xlibwm.h %*/
14: #include <X11/Xlib.h>
15: /* #include <X11/Xlibwm.h> [Doesn't exist yet 5-14-87] %*/
16: #include <X11/keysym.h>
17: #include <stdio.h>
18: #include <netdb.h>
19: #include <netinet/in.h>
20: #include <strings.h>
21: #include <ctype.h>
22:
23: #define ON 1
24: #define OFF 0
25:
26: #define DONT_CHANGE -2
27:
28: #define ALL -1
29: #define TIMEOUT 1
30: #define INTERVAL 2
31: #define PREFER_BLANK 3
32: #define ALLOW_EXP 4
33:
34: #define nextarg(i, argv) \
35: argv[i]; \
36: if (i >= argc) \
37: break; \
38:
39: char *progName;
40:
41: main(argc, argv)
42: int argc;
43: char **argv;
44: {
45: register char *arg;
46: register int i;
47: int percent;
48: int acc_num, acc_denom, threshold;
49: XKeyboardControl values;
50: unsigned long pixels[512];
51: caddr_t colors[512];
52: XColor def;
53: int numpixels = 0;
54: char *disp = '\0';
55: Display *dpy;
56: if (argc == 1) usage(argv[0]); /* To be replaced by window-interface */
57: progName = argv[0];
58: for (i = 1; i < argc; ) {
59: arg = argv[i++];
60: if (index(arg, ':')) { /* Set display name if given by user. */
61: disp = arg;
62: }
63: }
64: dpy = XOpenDisplay(disp); /* Open display and check for success */
65: if (dpy == NULL) {
66: fprintf(stderr, "%s: Can't open display '%s'\n",
67: argv[0], XDisplayName(disp ? disp : NULL ));
68: exit(1);
69: }
70: for (i = 1; i < argc; ) {
71: arg = argv[i++];
72: if (index(arg, ':')) { /* Set display name if given by user. */
73: ; /* forget this */
74: } else if (*arg == '-' && *(arg + 1) == 'c'){ /* Does arg start with "-c"? */
75: set_click(dpy, 0); /* If so, turn click off and */
76: }
77: else if (*arg == 'c') { /* Well, does it start with "c", then? */
78: percent = -1; /* Default click volume. */
79: arg = nextarg(i, argv);
80: if (strcmp(arg, "on") == 0) { /* Let click be default. */
81: i++;
82: }
83: else if (strcmp(arg, "off") == 0) {
84: percent = 0; /* Turn it off. */
85: i++;
86: }
87: else if (isnumber(arg, 100)) {
88: percent = atoi(arg); /* Set to spec. volume */
89: i++;
90: }
91: set_click(dpy, percent);
92: }
93: else if (*arg == '-' && *(arg + 1) == 'b') { /* Does arg start w/ "-b" */
94: set_bell_vol(dpy, 0); /* Then turn off bell. */
95: }
96: else if (*arg == 'b') { /* Does it start w/ "b". */
97: percent = -1; /* Set bell to default. */
98: arg = nextarg(i, argv);
99: if (strcmp(arg, "on") == 0) { /* Let it stay that way. */
100: set_bell_vol(dpy, percent);
101: i++;
102: }
103: else if (strcmp(arg, "off") == 0) {
104: percent = 0; /* Turn the bell off. */
105: set_bell_vol(dpy, percent);
106: i++;
107: }
108: else if (isnumber(arg, 100)) { /* If volume is given: */
109: percent = atoi(arg); /* set bell appropriately.*/
110: set_bell_vol(dpy, percent);
111: i++;
112: arg = nextarg(i, argv);
113:
114: if (isnumber(arg, 20000)) { /* If pitch is given: */
115: set_bell_pitch(dpy, atoi(arg)); /* set the bell. */
116: i++;
117:
118: arg = nextarg(i, argv);
119: if (isnumber(arg, 1000)) { /* If duration is given: */
120: set_bell_dur(dpy, atoi(arg)); /* set the bell. */
121: i++;
122: }
123: }
124: }
125: }
126: else if (strcmp(arg, "fp") == 0) { /* set font path */
127: arg = nextarg(i, argv);
128: set_font_path(dpy, arg);
129: i++;
130: }
131: else if (strcmp(arg, "-led") == 0) { /* Turn off one or all LEDs */
132: values.led_mode = OFF;
133: values.led = ALL; /* None specified */
134: arg = nextarg(i, argv);
135: if (isnumber(arg, 32) && atoi(arg) > 0) {
136: values.led = atoi(arg);
137: i++;
138: }
139: set_led(dpy, values.led, values.led_mode);
140: }
141: else if (strcmp(arg, "led") == 0) { /* Turn on one or all LEDs */
142: values.led_mode = ON;
143: values.led = ALL;
144: arg = nextarg(i, argv);
145: if (strcmp(arg, "on") == 0) {
146: i++;
147: }
148: else if (strcmp(arg, "off") == 0) { /* ...except in this case. */
149: values.led_mode = OFF;
150: i++;
151: }
152: else if (isnumber(arg, 32) && atoi(arg) > 0) {
153: values.led = atoi(arg);
154: i++;
155: }
156: set_led(dpy, values.led, values.led_mode);
157: }
158: /* Set pointer (mouse) settings: Acceleration and Threshold. */
159: else if (strcmp(arg, "m") == 0 || strcmp(arg, "mouse") == 0) {
160: acc_num = -1;
161: acc_denom = -1; /* Defaults */
162: threshold = -1;
163: if (i >= argc){
164: set_mouse(dpy, acc_num, acc_denom, threshold);
165: break;
166: }
167: arg = argv[i];
168: if (strcmp(arg, "default") == 0) {
169: i++;
170: }
171: else if (*arg >= '0' && *arg <= '9') {
172: acc_num = atoi(arg); /* Set acceleration to user's tastes. */
173: i++;
174: if (i >= argc) {
175: set_mouse(dpy, acc_num, acc_denom, threshold);
176: break;
177: }
178: arg = argv[i];
179: if (*arg >= '0' && *arg <= '9') {
180: threshold = atoi(arg); /* Set threshold as user specified. */
181: i++;
182: }
183: }
184: set_mouse(dpy, acc_num, acc_denom, threshold);
185: }
186: else if (*arg == 's') { /* If arg starts with "s". */
187: if (i >= argc) {
188: set_saver(dpy, ALL, 0); /* Set everything to default */
189: break;
190: }
191: arg = argv[i];
192: if (strcmp(arg, "blank") == 0) { /* Alter blanking preference. */
193: set_saver(dpy, PREFER_BLANK, PreferBlanking);
194: i++;
195: }
196: else if (strcmp(arg, "noblank") == 0) { /* Ditto. */
197: set_saver(dpy, PREFER_BLANK, DontPreferBlanking);
198: i++;
199: }
200: else if (strcmp(arg, "off") == 0) {
201: set_saver(dpy, TIMEOUT, 0); /* Turn off screen saver. */
202: i++;
203: if (i >= argc)
204: break;
205: arg = argv[i];
206: if (strcmp(arg, "off") == 0) {
207: set_saver(dpy, INTERVAL, 0);
208: i++;
209: }
210: }
211: else if (strcmp(arg, "default") == 0) { /* Leave as default. */
212: set_saver(dpy, ALL, 0);
213: i++;
214: }
215: else if (*arg >= '0' && *arg <= '9') { /* Set as user wishes. */
216: set_saver(dpy, TIMEOUT, atoi(arg));
217: i++;
218: if (i >= argc)
219: break;
220: arg = argv[i];
221: if (*arg >= '0' && *arg <= '9') {
222: set_saver(dpy, INTERVAL, atoi(arg));
223: i++;
224: }
225: }
226: }
227: else if(*arg == '-' && *(arg + 1) == 'r'){ /* If arg starts w/ "-r" */
228: set_repeat(dpy, ALL, OFF);
229: }
230: else if (*arg == 'r') { /* If it starts with "r" */
231: if (i >= argc) {
232: set_repeat(dpy, ALL, ON);
233: break;
234: }
235: arg = argv[i]; /* Check next argument. */
236: if (strcmp(arg, "on") == 0) {
237: set_repeat(dpy, ALL, ON);
238: i++;
239: }
240: else if (strcmp(arg, "off") == 0) {
241: set_repeat(dpy, ALL, OFF);
242: i++;
243: }
244: }
245: else if (*arg == 'p') { /* If arg starts with "p" */
246: if (i + 1 >= argc)
247: usage(argv[0]);
248: arg = argv[i];
249: if (*arg >= '0' && *arg <= '9')
250: pixels[numpixels] = atoi(arg);
251: else
252: usage(argv[0]);
253: i++;
254: colors[numpixels] = argv[i];
255: i++;
256: numpixels++;
257: set_pixels(dpy, pixels, colors, numpixels);
258: }
259: else if (*arg == '-' && *(arg + 1) == 'k') {
260: set_lock(dpy, OFF);
261: }
262: else if (*arg == 'k') { /* Set modifier keys. */
263: set_lock(dpy, ON);
264: }
265: else if (*arg == 'q') { /* Give status to user. */
266: query(dpy);
267: }
268: else
269: usage(argv[0]);
270: }
271:
272: XFlush(dpy);
273:
274: exit(0); /* Done. We can go home now. */
275: }
276:
277:
278: isnumber(arg, maximum)
279: char *arg;
280: int maximum;
281: {
282: register char *p;
283:
284: if (arg[0] == '-' && arg[1] == '1' && arg[2] == '\0')
285: return(1);
286: for (p=arg; isdigit(*p); p++);
287: if (*p || atoi(arg) > maximum)
288: return(0);
289: return(1);
290: }
291:
292: /* These next few functions do the real work (xsetting things).
293: */
294: set_click(dpy, percent)
295: Display *dpy;
296: int percent;
297: {
298: XKeyboardControl values;
299: values.key_click_percent = percent;
300: XChangeKeyboardControl(dpy, KBKeyClickPercent, &values);
301: return;
302: }
303:
304: set_bell_vol(dpy, percent)
305: Display *dpy;
306: int percent;
307: {
308: XKeyboardControl values;
309: values.bell_percent = percent;
310: XChangeKeyboardControl(dpy, KBBellPercent, &values);
311: return;
312: }
313:
314: set_bell_pitch(dpy, pitch)
315: Display *dpy;
316: int pitch;
317: {
318: XKeyboardControl values;
319: values.bell_pitch = pitch;
320: XChangeKeyboardControl(dpy, KBBellPitch, &values);
321: return;
322: }
323:
324: set_bell_dur(dpy, duration)
325: Display *dpy;
326: int duration;
327: {
328: XKeyboardControl values;
329: values.bell_duration = duration;
330: XChangeKeyboardControl(dpy, KBBellDuration, &values);
331: return;
332: }
333:
334: set_font_path(dpy, path)
335: Display *dpy;
336: char *path;
337: {
338: char **directoryList = NULL; int ndirs = 0;
339: char *directories;
340: char *pDir;
341:
342: if (strcmp(path, "default")!=0) {
343: if (((directories = (char *)malloc( strlen(path) )) == NULL) ||
344: ((directoryList = (char **)malloc(sizeof(char *))) == NULL))
345: error( "out of memory" );
346:
347: strcpy( directories, path );
348: *directoryList = pDir = directories;
349: ndirs++;
350: while( (pDir = index(pDir, ',')) != NULL) {
351: *pDir++ = '\0';
352: directoryList = (char **)realloc(directoryList,
353: (ndirs+1)*sizeof(char *));
354: if (directoryList == NULL) error( "out of memory" );
355: directoryList[ndirs++] = pDir;
356: }
357: }
358:
359: XSetFontPath( dpy, directoryList, ndirs );
360: }
361:
362: set_led(dpy, led, led_mode)
363: Display *dpy;
364: int led, led_mode;
365: {
366: XKeyboardControl values;
367: values.led_mode = led_mode;
368: if (led != ALL) {
369: values.led = led;
370: XChangeKeyboardControl(dpy, KBLed | KBLedMode, &values);
371: }
372: else {
373: XChangeKeyboardControl(dpy, KBLedMode, &values);
374: }
375: return;
376: }
377:
378: set_mouse(dpy, acc_num, acc_denom, threshold)
379: Display *dpy;
380: int acc_num, acc_denom, threshold;
381: {
382: int do_accel = True, do_threshold = True;
383: if (acc_num == DONT_CHANGE)
384: do_accel = False;
385: if (threshold == DONT_CHANGE)
386: do_threshold = False;
387: XChangePointerControl(dpy, do_accel, do_threshold, acc_num,
388: acc_denom, threshold);
389: return;
390: }
391:
392: set_saver(dpy, mask, value)
393: Display *dpy;
394: int mask, value;
395: {
396: int timeout, interval, prefer_blank, allow_exp;
397: XGetScreenSaver(dpy, &timeout, &interval, &prefer_blank,
398: &allow_exp);
399: if (mask == TIMEOUT) timeout = value;
400: if (mask == INTERVAL) interval = value;
401: if (mask == PREFER_BLANK) prefer_blank = value;
402: if (mask == ALLOW_EXP) allow_exp = value;
403: if (mask == ALL) { /* "value" is ignored in this case. (defaults) */
404: timeout = -1;
405: interval = -1;
406: prefer_blank = DefaultBlanking;
407: allow_exp = DefaultExposures;
408: }
409: XSetScreenSaver(dpy, timeout, interval, prefer_blank,
410: allow_exp);
411: return;
412: }
413:
414: set_repeat(dpy, key, auto_repeat_mode)
415: Display *dpy;
416: int key, auto_repeat_mode;
417: {
418: XKeyboardControl values;
419: values.auto_repeat_mode = auto_repeat_mode;
420: if (key != ALL) {
421: values.key = key;
422: XChangeKeyboardControl(dpy, KBKey | KBAutoRepeatMode, &values);
423: }
424: else {
425: XChangeKeyboardControl(dpy, KBAutoRepeatMode, &values);
426: }
427: return;
428: }
429:
430: set_pixels(dpy, pixels, colors, numpixels)
431: Display *dpy;
432: unsigned long pixels[512];
433: caddr_t colors[512];
434: int numpixels;
435: {
436: char *spec; /*%%%%%*/
437: XColor def;
438: if(DisplayCells(dpy, DefaultScreen(dpy)) >= 2) {
439: while (--numpixels >= 0) {
440: def.pixel = pixels[numpixels];
441: if (XParseColor(dpy, colors[numpixels], spec, &def))
442: XStoreColor(&def);
443: else
444: fprintf(stderr, "%s: No such color\n", colors[numpixels]);
445: }
446: }
447: return;
448: }
449:
450: set_lock(dpy, onoff)
451: Display *dpy;
452: Bool onoff;
453: {
454: XModifierKeymap *mods;
455: mods = XGetModifierMapping(dpy);
456:
457: if (onoff)
458: mods = XInsertModifiermapEntry(mods, XK_Caps_Lock, LockMapIndex);
459: else
460: mods = XDeleteModifiermapEntry(mods, XK_Caps_Lock, LockMapIndex);
461: XSetModifierMapping(dpy, mods);
462: return;
463: }
464:
465: /* This is the information-getting function for telling the user what the
466: * current "xsettings" are.
467: */
468: query(dpy)
469: Display *dpy;
470: {
471: XKeyboardState values;
472: int acc_num, acc_denom, threshold;
473: int timeout, interval, prefer_blank, allow_exp;
474: char **font_path; int npaths;
475:
476: XGetKeyboardControl(dpy, &values);
477: XGetPointerControl(dpy, &acc_num, &acc_denom, &threshold);
478: XGetScreenSaver(dpy, &timeout, &interval, &prefer_blank, &allow_exp);
479: font_path = XGetFontPath(dpy, &npaths);
480:
481: printf ("Keyboard Control Values:\n");
482: /*printf ("Auto Repeat: %d \t\t", values.auto_repeat_mode); %%*/
483: /*printf ("Key: %d \n\n", values.key); %%*/
484: printf ("Key Click Volume (%%): %d \n", values.key_click_percent);
485: printf ("Bell Volume (%%): %d \t", values.bell_percent);
486: printf ("Bell Pitch (Hz): %d \t", values.bell_pitch);
487: printf ("Bell Duration (msec): %d \n", values.bell_duration);
488: /*printf ("LED: %d \t\t\t", values.led);
489: printf ("LED Mode: %o \t\t", values.led_mode); %%*/
490:
491: printf ("Pointer (Mouse) Control Values:\n");
492: printf ("Acceleration: %d \t", acc_num / acc_denom);
493: printf ("Threshold: %d \n", threshold);
494: printf ("Screen Saver: (yes = %d, no = %d, default = %d)\n",
495: PreferBlanking, DontPreferBlanking, DefaultBlanking);
496: printf ("Prefer Blanking: %d \t", prefer_blank);
497: printf ("Time-out: %d \t Cycle: %d\n", timeout, interval);
498: if (npaths) {
499: printf( "Font Path: %s", *font_path++ );
500: for( --npaths; npaths; npaths-- )
501: printf( ",%s", *font_path++ );
502: printf( "\n" );
503: }
504: return;
505: }
506:
507:
508: /* This is the usage function */
509:
510: usage(prog)
511: char *prog;
512: {
513: printf("usage: %s option [option ...] [host:vs]\n", prog);
514: printf(" To turn bell off:\n");
515: printf("\t-b b off b 0\n");
516: printf(" To set bell volume, pitch and duration:\n");
517: printf("\t b [vol [pitch [dur]]] b on\n");
518: printf(" To turn keyclick off:\n");
519: printf("\t-c c off c 0\n");
520: printf(" To set keyclick volume:\n");
521: printf("\t c [0-100] c on\n");
522: printf(" To set the font path:\n" );
523: printf("\t fp path[,path...]\n" );
524: printf(" To restore the default font path:\n" );
525: printf("\t fp default\n" );
526: printf(" To set LED states off or on:\n");
527: printf("\t-led [1-32] led off\n");
528: printf("\t led [1-32] led on\n");
529: printf(" To set mouse acceleration and threshold:\n");
530: printf("\t m [acc [thr]] m default\n");
531: printf(" To set pixel colors:\n");
532: printf("\t p pixel_value color_name\n");
533: printf(" To turn auto-repeat off or on:\n");
534: printf("\t-r r off r r on\n");
535: printf(" For screen-saver control:\n");
536: printf("\t s [timeout [cycle]] s default\n");
537: printf("\t s blank s noblank\n");
538: printf(" For status information: q or query\n");
539: exit(0);
540: }
541:
542: error( message )
543: char *message;
544: {
545: fprintf( stderr, "%s: %s\n", progName, message );
546: exit( 1 );
547: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.