|
|
1.1 root 1: /* $Header: Conversion.c,v 1.1 87/09/11 08:16:01 toddb Exp $ */
2: #ifndef lint
3: static char *sccsid = "@(#)Conversion.c 1.11 3/19/87";
4: #endif lint
5:
6: /*
7: * Copyright 1987 by Digital Equipment Corporation, Maynard, Massachusetts.
8: *
9: * All Rights Reserved
10: *
11: * Permission to use, copy, modify, and distribute this software and its
12: * documentation for any purpose and without fee is hereby granted,
13: * provided that the above copyright notice appear in all copies and that
14: * both that copyright notice and this permission notice appear in
15: * supporting documentation, and that the name of Digital Equipment
16: * Corporation not be used in advertising or publicity pertaining to
17: * distribution of the software without specific, written prior permission.
18: *
19: *
20: * DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
21: * ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
22: * DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
23: * ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
24: * WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
25: * ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
26: * SOFTWARE.
27: */
28:
29:
30: /* Conversion.c - implementations of resource type conversion procs */
31:
32: #include "Xlib.h"
33: #include "Xutil.h"
34: #include "Xresource.h"
35: #include "XrmConvert.h"
36: #include "Conversion.h"
37: #include "Quarks.h"
38: #include <sys/file.h>
39: #include <stdio.h>
40:
41: #define done(address, type) \
42: { (*toVal).size = sizeof(type); (*toVal).addr = (caddr_t) address; }
43:
44: static void CvtXColorToPixel();
45:
46: static void CvtGeometryToDims();
47:
48: static void CvtIntToBoolean();
49: static void CvtIntToFont();
50: static void CvtIntOrPixelToXColor();
51: static void CvtIntToPixel();
52:
53: static void CvtStringToBoolean();
54: static void CvtStringToXColor();
55: static void CvtStringToDisplay();
56: extern void CvtStringToEventBindings();
57: static void CvtStringToFile();
58: static void CvtStringToFont();
59: static void CvtStringToFontStruct();
60: static void CvtStringToGeometry();
61: static void CvtStringToInt();
62: static void CvtStringToPixel();
63: static void CvtStringToPixmap();
64:
65: void _XLowerCase(source, dest)
66: register char *source, *dest;
67: {
68: register char ch;
69:
70: for (; (ch = *source) != 0; source++, dest++) {
71: if ('A' <= ch && ch <= 'Z')
72: *dest = ch - 'A' + 'a';
73: else
74: *dest = ch;
75: }
76: *dest = 0;
77: }
78:
79:
80: /*ARGSUSED*/
81: static void CvtIntToBoolean(dpy, fromVal, toVal)
82: Display *dpy;
83: XrmValue fromVal;
84: XrmValue *toVal;
85: {
86: static int b;
87:
88: b = (int) (*(int *)fromVal.addr != 0);
89: done(&b, int);
90: };
91:
92:
93: /*ARGSUSED*/
94: static void CvtStringToBoolean(dpy, fromVal, toVal)
95: Display *dpy;
96: XrmValue fromVal;
97: XrmValue *toVal;
98: {
99: static int b;
100: XrmQuark q;
101: char lowerName[1000];
102:
103: _XLowerCase((char *) fromVal.addr, lowerName);
104: q = XrmAtomToQuark(lowerName);
105:
106: if (q == XrmQEtrue) { b = 1; done(&b, int); return; }
107: if (q == XrmQEon) { b = 1; done(&b, int); return; }
108: if (q == XrmQEyes) { b = 1; done(&b, int); return; }
109:
110: if (q == XrmQEfalse) { b = 0; done(&b, int); return; }
111: if (q == XrmQEoff) { b = 0; done(&b, int); return; }
112: if (q == XrmQEno) { b = 0; done(&b, int); return; }
113: };
114:
115:
116: /*ARGSUSED*/
117: static void CvtIntOrPixelToXColor(dpy, fromVal, toVal)
118: Display *dpy;
119: XrmValue fromVal;
120: XrmValue *toVal;
121: {
122: static XColor c;
123:
124: if (DefaultVisual(dpy, DefaultScreen(dpy))->class == StaticGray)
125: return;
126:
127: c.pixel = *(int *)fromVal.addr;
128: XQueryColor(dpy, DefaultColormap(dpy, DefaultScreen(dpy)), &c);
129: /* !!! Need some error checking ... ||| */
130: done(&c, XColor);
131: };
132:
133:
134: /*ARGSUSED*/
135: static void CvtStringToXColor(dpy, fromVal, toVal)
136: Display *dpy;
137: XrmValue fromVal;
138: XrmValue *toVal;
139: {
140: static XColor c;
141: Status s;
142:
143: if (DefaultVisual(dpy, DefaultScreen(dpy))->class == StaticGray)
144: return;
145:
146: s = XParseColor(dpy, DefaultColormap(dpy, DefaultScreen(dpy)),
147: (char *)fromVal.addr, &c);
148: if (s == 0) return;
149: s = XAllocColor(dpy, DefaultColormap(dpy, DefaultScreen(dpy)), &c);
150: if (s == 0) return;
151: done(&c, XColor);
152: };
153:
154:
155: /*ARGSUSED*/
156: static void CvtGeometryToDims(dpy, fromVal, toVal)
157: Display *dpy;
158: XrmValue fromVal;
159: XrmValue *toVal;
160: {
161: done(&((Geometry *)fromVal.addr)->dims, Dims);
162: };
163:
164:
165: /*ARGSUSED*/
166: static void CvtStringToDisplay(dpy, fromVal, toVal)
167: Display *dpy;
168: XrmValue fromVal;
169: XrmValue *toVal;
170: {
171: static Display *d;
172:
173: d = XOpenDisplay((char *)fromVal.addr);
174: if (d != NULL) { done(d, Display); }
175: };
176:
177:
178: /*ARGSUSED*/
179: static void CvtStringToFile(dpy, fromVal, toVal)
180: Display *dpy;
181: XrmValue fromVal;
182: XrmValue *toVal;
183: {
184: static FILE *f;
185:
186: f = fopen((char *)fromVal.addr, "r");
187: if (f != NULL) { done(f, FILE); }
188: };
189:
190:
191: /*ARGSUSED*/
192: static void CvtStringToFont(dpy, fromVal, toVal)
193: Display *dpy;
194: XrmValue fromVal;
195: XrmValue *toVal;
196: {
197: static Font f;
198:
199: f = XLoadFont(dpy, (char *)fromVal.addr);
200: if (f != 0) { done(&f, Font); }
201: }
202:
203:
204: /*ARGSUSED*/
205: static void CvtIntToFont(dpy, fromVal, toVal)
206: Display *dpy;
207: XrmValue fromVal;
208: XrmValue *toVal;
209: {
210: done(fromVal.addr, int);
211: };
212:
213:
214: /*ARGSUSED*/
215: static void CvtStringToFontStruct(dpy, fromVal, toVal)
216: Display *dpy;
217: XrmValue fromVal;
218: XrmValue *toVal;
219: {
220: static XFontStruct *f;
221:
222: f = XLoadQueryFont(dpy, (char *)fromVal.addr);
223: if (f != NULL) { done(&f, XFontStruct *); }
224: }
225:
226: /*ARGSUSED*/
227: static void CvtStringToGeometry(dpy, fromVal, toVal)
228: Display *dpy;
229: XrmValue fromVal;
230: XrmValue *toVal;
231: {
232: static Geometry g;
233: int i;
234:
235: g.pos.xpos = g.pos.ypos = g.dims.width = g.dims.height = 0;
236: i = XParseGeometry((char *) fromVal.addr,
237: &g.pos.xpos, &g.pos.ypos, &g.dims.width, &g.dims.height);
238: if (i == NoValue) return;
239: if (i & XNegative)
240: g.pos.xpos = DisplayWidth(dpy, DefaultScreen(dpy))-1-g.pos.xpos;
241: if (i & YNegative)
242: g.pos.ypos = DisplayHeight(dpy, DefaultScreen(dpy))-1-g.pos.ypos;
243: done(&g, Geometry);
244: };
245:
246:
247: /*ARGSUSED*/
248: static void CvtStringToInt(dpy, fromVal, toVal)
249: Display *dpy;
250: XrmValue fromVal;
251: XrmValue *toVal;
252: {
253: static int i;
254:
255: if (sscanf((char *)fromVal.addr, "%d", &i) == 1) { done(&i, int); }
256: }
257:
258:
259: /*ARGSUSED*/
260: static void CvtStringToPixel(dpy, fromVal, toVal)
261: Display *dpy;
262: XrmValue fromVal;
263: XrmValue *toVal;
264: {
265: _XrmConvert(dpy, XrmQString, fromVal, XrmQColor, toVal);
266: if ((*toVal).addr == NULL) return;
267: done(&((XColor *)((*toVal).addr))->pixel, int)
268: };
269:
270:
271: /*ARGSUSED*/
272: static void CvtXColorToPixel(dpy, fromVal, toVal)
273: Display *dpy;
274: XrmValue fromVal;
275: XrmValue *toVal;
276: {
277: done(&((XColor *)fromVal.addr)->pixel, int);
278: };
279:
280:
281: /*ARGSUSED*/
282: static void CvtIntToPixel(dpy, fromVal, toVal)
283: Display *dpy;
284: XrmValue fromVal;
285: XrmValue *toVal;
286: {
287: done(fromVal.addr, int);
288: };
289:
290:
291: /*ARGSUSED*/
292: static void CvtStringToPixmap(dpy, fromVal, toVal)
293: Display *dpy;
294: XrmValue fromVal;
295: XrmValue *toVal;
296: {
297: XrmValue pixelVal;
298:
299: _XrmConvert(dpy, XrmQString, fromVal, XrmQPixel, &pixelVal);
300: if (pixelVal.addr == NULL) return;
301: _XrmConvert(dpy, XrmQPixel, pixelVal, XrmQPixmap, toVal);
302: }
303:
304:
305: static Bool initialized = False;
306:
307: void XrmInitialize()
308: {
309: if (initialized)
310: return;
311: initialized = True;
312:
313: _XrmRegisterTypeConverter(XrmQColor, XrmQPixel, CvtXColorToPixel);
314:
315: _XrmRegisterTypeConverter(XrmQGeometry, XrmQDims, CvtGeometryToDims);
316:
317: _XrmRegisterTypeConverter(XrmQInt, XrmQBoolean, CvtIntToBoolean);
318: _XrmRegisterTypeConverter(XrmQInt, XrmQPixel, CvtIntToPixel);
319: _XrmRegisterTypeConverter(XrmQInt, XrmQFont, CvtIntToFont);
320: _XrmRegisterTypeConverter(XrmQInt, XrmQColor, CvtIntOrPixelToXColor);
321:
322: _XrmRegisterTypeConverter(XrmQString, XrmQBoolean, CvtStringToBoolean);
323: _XrmRegisterTypeConverter(XrmQString, XrmQColor, CvtStringToXColor);
324: _XrmRegisterTypeConverter(XrmQString, XrmQDisplay, CvtStringToDisplay);
325: _XrmRegisterTypeConverter(XrmQString, XrmQFile, CvtStringToFile);
326: _XrmRegisterTypeConverter(XrmQString, XrmQFont, CvtStringToFont);
327: _XrmRegisterTypeConverter(XrmQString, XrmQFontStruct, CvtStringToFontStruct);
328: _XrmRegisterTypeConverter(XrmQString, XrmQGeometry, CvtStringToGeometry);
329: _XrmRegisterTypeConverter(XrmQString, XrmQInt, CvtStringToInt);
330: _XrmRegisterTypeConverter(XrmQString, XrmQPixel, CvtStringToPixel);
331: _XrmRegisterTypeConverter(XrmQString, XrmQPixmap, CvtStringToPixmap);
332:
333: _XrmRegisterTypeConverter(XrmQPixel, XrmQColor, CvtIntOrPixelToXColor);
334:
335: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.