Annotation of researchv9/X11/src/X.V11R1/lib/Xtk/Conversion.c, revision 1.1

1.1     ! root        1: #ifndef lint
        !             2: static char *sccsid = "@(#)Conversion.c        1.11    3/19/87";
        !             3: #endif lint
        !             4: 
        !             5: /*
        !             6:  * Copyright 1987 by Digital Equipment Corporation, Maynard, Massachusetts.
        !             7:  * 
        !             8:  *                         All Rights Reserved
        !             9:  * 
        !            10:  * Permission to use, copy, modify, and distribute this software and its 
        !            11:  * documentation for any purpose and without fee is hereby granted, 
        !            12:  * provided that the above copyright notice appear in all copies and that
        !            13:  * both that copyright notice and this permission notice appear in 
        !            14:  * supporting documentation, and that the name of Digital Equipment
        !            15:  * Corporation not be used in advertising or publicity pertaining to
        !            16:  * distribution of the software without specific, written prior permission.  
        !            17:  * 
        !            18:  * 
        !            19:  * DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
        !            20:  * ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
        !            21:  * DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
        !            22:  * ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
        !            23:  * WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
        !            24:  * ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
        !            25:  * SOFTWARE.
        !            26:  */
        !            27: 
        !            28: 
        !            29: /* Conversion.c - implementations of resource type conversion procs */
        !            30: 
        !            31: #include       "Xlib.h"
        !            32: #include       "Xutil.h"
        !            33: #include       "Xresource.h"
        !            34: #include       "XrmConvert.h"
        !            35: #include       "Conversion.h"
        !            36: #include       "Quarks.h"
        !            37: #include       "cursorfont.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 CvtStringToCursor();
        !            56: static void CvtStringToDisplay();
        !            57: extern void CvtStringToEventBindings();
        !            58: static void CvtStringToFile();
        !            59: static void CvtStringToFont();
        !            60: static void CvtStringToFontStruct();
        !            61: static void CvtStringToGeometry();
        !            62: static void CvtStringToInt();
        !            63: static void CvtStringToPixel();
        !            64: static void CvtStringToPixmap();
        !            65: 
        !            66: void _XLowerCase(source, dest)
        !            67:     register char  *source, *dest;
        !            68: {
        !            69:     register char ch;
        !            70: 
        !            71:     for (; (ch = *source) != 0; source++, dest++) {
        !            72:        if ('A' <= ch && ch <= 'Z')
        !            73:            *dest = ch - 'A' + 'a';
        !            74:        else
        !            75:            *dest = ch;
        !            76:     }
        !            77:     *dest = 0;
        !            78: }
        !            79: 
        !            80: 
        !            81: /*ARGSUSED*/
        !            82: static void CvtIntToBoolean(dpy, fromVal, toVal)
        !            83:     Display    *dpy;
        !            84:     XrmValue   fromVal;
        !            85:     XrmValue   *toVal;
        !            86: {
        !            87:     static int b;
        !            88: 
        !            89:     b = (int) (*(int *)fromVal.addr != 0);
        !            90:     done(&b, int);
        !            91: };
        !            92: 
        !            93: 
        !            94: /*ARGSUSED*/
        !            95: static void CvtStringToBoolean(dpy, fromVal, toVal)
        !            96:     Display    *dpy;
        !            97:     XrmValue   fromVal;
        !            98:     XrmValue   *toVal;
        !            99: {
        !           100:     static int b;
        !           101:     XrmQuark   q;
        !           102:     char       lowerName[1000];
        !           103: 
        !           104:     _XLowerCase((char *) fromVal.addr, lowerName);
        !           105:     q = XrmAtomToQuark(lowerName);
        !           106: 
        !           107:     if (q == XrmQEtrue)        { b = 1; done(&b, int); return; }
        !           108:     if (q == XrmQEon)  { b = 1; done(&b, int); return; }
        !           109:     if (q == XrmQEyes) { b = 1; done(&b, int); return; }
        !           110: 
        !           111:     if (q == XrmQEfalse)       { b = 0; done(&b, int); return; }
        !           112:     if (q == XrmQEoff) { b = 0; done(&b, int); return; }
        !           113:     if (q == XrmQEno)  { b = 0; done(&b, int); return; }
        !           114: };
        !           115: 
        !           116: 
        !           117: /*ARGSUSED*/
        !           118: static void CvtIntOrPixelToXColor(dpy, fromVal, toVal)
        !           119:     Display    *dpy;
        !           120:     XrmValue   fromVal;
        !           121:     XrmValue   *toVal;
        !           122: {    
        !           123:     static XColor      c;
        !           124: 
        !           125: /*
        !           126:     if (DefaultVisual(dpy, DefaultScreen(dpy))->class == StaticGray)
        !           127:        return;
        !           128: */
        !           129: 
        !           130:     c.pixel = *(int *)fromVal.addr;
        !           131:     XQueryColor(dpy, DefaultColormap(dpy, DefaultScreen(dpy)), &c);
        !           132:                                /* !!! Need some error checking ... ||| */
        !           133:     done(&c, XColor);
        !           134: };
        !           135: 
        !           136: 
        !           137: /*ARGSUSED*/
        !           138: static void CvtStringToXColor(dpy, fromVal, toVal)
        !           139:     Display    *dpy;
        !           140:     XrmValue   fromVal;
        !           141:     XrmValue   *toVal;
        !           142: {
        !           143:     static XColor      c;
        !           144:     Status             s;
        !           145: 
        !           146: /*
        !           147:     if (DefaultVisual(dpy, DefaultScreen(dpy))->class == StaticGray)
        !           148:        return;
        !           149: */
        !           150: 
        !           151:     s = XParseColor(dpy, DefaultColormap(dpy, DefaultScreen(dpy)),
        !           152:                        (char *)fromVal.addr, &c);
        !           153:     if (s == 0) return;
        !           154:     s = XAllocColor(dpy, DefaultColormap(dpy, DefaultScreen(dpy)), &c);
        !           155:     if (s == 0) return;
        !           156:     done(&c, XColor);
        !           157: };
        !           158: 
        !           159: 
        !           160: /*ARGSUSED*/
        !           161: static void CvtStringToCursor(dpy, fromVal, toVal)
        !           162:     Display    *dpy;
        !           163:     XrmValue   fromVal;
        !           164:     XrmValue   *toVal;
        !           165: {
        !           166: #define count(array)   (sizeof(array)/sizeof(array[0]))
        !           167: 
        !           168:     static struct _CursorName {
        !           169:        char            *name;
        !           170:        unsigned int    shape;
        !           171:     } cursor_names[] = {
        !           172:                        {"X_cursor",            XC_X_cursor},
        !           173:                        {"arrow",               XC_arrow},
        !           174:                        {"based_arrow_down",    XC_based_arrow_down},
        !           175:                        {"based_arrow_up",      XC_based_arrow_up},
        !           176:                        {"boat",                XC_boat},
        !           177:                        {"bogosity",            XC_bogosity},
        !           178:                        {"bottom_left_corner",  XC_bottom_left_corner},
        !           179:                        {"bottom_right_corner", XC_bottom_right_corner},
        !           180:                        {"bottom_side",         XC_bottom_side},
        !           181:                        {"bottom_tee",          XC_bottom_tee},
        !           182:                        {"box_spiral",          XC_box_spiral},
        !           183:                        {"center_ptr",          XC_center_ptr},
        !           184:                        {"circle",              XC_circle},
        !           185:                        {"clock",               XC_clock},
        !           186:                        {"coffee_mug",          XC_coffee_mug},
        !           187:                        {"cross",               XC_cross},
        !           188:                        {"cross_reverse",       XC_cross_reverse},
        !           189:                        {"crosshair",           XC_crosshair},
        !           190:                        {"diamond_cross",       XC_diamond_cross},
        !           191:                        {"dot",                 XC_dot},
        !           192:                        {"dotbox",              XC_dotbox},
        !           193:                        {"double_arrow",        XC_double_arrow},
        !           194:                        {"draft_large",         XC_draft_large},
        !           195:                        {"draft_small",         XC_draft_small},
        !           196:                        {"draped_box",          XC_draped_box},
        !           197:                        {"exchange",            XC_exchange},
        !           198:                        {"fleur",               XC_fleur},
        !           199:                        {"gobbler",             XC_gobbler},
        !           200:                        {"gumby",               XC_gumby},
        !           201:                        {"hand1",               XC_hand1},
        !           202:                        {"hand2",               XC_hand2},
        !           203:                        {"heart",               XC_heart},
        !           204:                        {"icon",                XC_icon},
        !           205:                        {"iron_cross",          XC_iron_cross},
        !           206:                        {"left_ptr",            XC_left_ptr},
        !           207:                        {"left_side",           XC_left_side},
        !           208:                        {"left_tee",            XC_left_tee},
        !           209:                        {"leftbutton",          XC_leftbutton},
        !           210:                        {"ll_angle",            XC_ll_angle},
        !           211:                        {"lr_angle",            XC_lr_angle},
        !           212:                        {"man",                 XC_man},
        !           213:                        {"middlebutton",        XC_middlebutton},
        !           214:                        {"mouse",               XC_mouse},
        !           215:                        {"pencil",              XC_pencil},
        !           216:                        {"pirate",              XC_pirate},
        !           217:                        {"plus",                XC_plus},
        !           218:                        {"question_arrow",      XC_question_arrow},
        !           219:                        {"right_ptr",           XC_right_ptr},
        !           220:                        {"right_side",          XC_right_side},
        !           221:                        {"right_tee",           XC_right_tee},
        !           222:                        {"rightbutton",         XC_rightbutton},
        !           223:                        {"rtl_logo",            XC_rtl_logo},
        !           224:                        {"sailboat",            XC_sailboat},
        !           225:                        {"sb_down_arrow",       XC_sb_down_arrow},
        !           226:                        {"sb_h_double_arrow",   XC_sb_h_double_arrow},
        !           227:                        {"sb_left_arrow",       XC_sb_left_arrow},
        !           228:                        {"sb_right_arrow",      XC_sb_right_arrow},
        !           229:                        {"sb_up_arrow",         XC_sb_up_arrow},
        !           230:                        {"sb_v_double_arrow",   XC_sb_v_double_arrow},
        !           231:                        {"shuttle",             XC_shuttle},
        !           232:                        {"sizing",              XC_sizing},
        !           233:                        {"spider",              XC_spider},
        !           234:                        {"spraycan",            XC_spraycan},
        !           235:                        {"star",                XC_star},
        !           236:                        {"target",              XC_target},
        !           237:                        {"tcross",              XC_tcross},
        !           238:                        {"top_left_arrow",      XC_top_left_arrow},
        !           239:                        {"top_left_corner",     XC_top_left_corner},
        !           240:                        {"top_right_corner",    XC_top_right_corner},
        !           241:                        {"top_side",            XC_top_side},
        !           242:                        {"top_tee",             XC_top_tee},
        !           243:                        {"trek",                XC_trek},
        !           244:                        {"ul_angle",            XC_ul_angle},
        !           245:                        {"umbrella",            XC_umbrella},
        !           246:                        {"ur_angle",            XC_ur_angle},
        !           247:                        {"watch",               XC_watch},
        !           248:                        {"xterm",               XC_xterm},
        !           249:     };
        !           250:     struct _CursorName *cache;
        !           251:     Cursor cursor;
        !           252:     char *name = (char *)fromVal.addr;
        !           253:     int i, found = False;
        !           254: 
        !           255:     /* linear search, for now; can improve on this later */
        !           256:     for( i=0,cache=cursor_names; i < count(cursor_names); i++,cache++ ) {
        !           257:        if (strcmp(name, cache->name) == 0) {
        !           258:            found = True;
        !           259:            break;
        !           260:        }
        !           261:     }
        !           262: 
        !           263:     if (found) {
        !           264:         /* cacheing is actually done by higher layers of Xrm */
        !           265:        cursor = XCreateFontCursor( dpy, cache->shape );
        !           266:        done(&cursor, Cursor);
        !           267:     }
        !           268: };
        !           269: 
        !           270: 
        !           271: /*ARGSUSED*/
        !           272: static void CvtGeometryToDims(dpy, fromVal, toVal)
        !           273:     Display    *dpy;
        !           274:     XrmValue   fromVal;
        !           275:     XrmValue   *toVal;
        !           276: {
        !           277:     done(&((Geometry *)fromVal.addr)->dims, Dims);
        !           278: };
        !           279: 
        !           280: 
        !           281: /*ARGSUSED*/
        !           282: static void CvtStringToDisplay(dpy, fromVal, toVal)
        !           283:     Display    *dpy;
        !           284:     XrmValue   fromVal;
        !           285:     XrmValue   *toVal;
        !           286: {
        !           287:     static Display     *d;
        !           288: 
        !           289:     d = XOpenDisplay((char *)fromVal.addr);
        !           290:     if (d != NULL) { done(d, Display); }
        !           291: };
        !           292: 
        !           293: 
        !           294: /*ARGSUSED*/
        !           295: static void CvtStringToFile(dpy, fromVal, toVal)
        !           296:     Display    *dpy;
        !           297:     XrmValue   fromVal;
        !           298:     XrmValue   *toVal;
        !           299: {
        !           300:     static FILE        *f;
        !           301: 
        !           302:     f = fopen((char *)fromVal.addr, "r");
        !           303:     if (f != NULL) { done(f, FILE); }
        !           304: };
        !           305: 
        !           306: 
        !           307: /*ARGSUSED*/
        !           308: static void CvtStringToFont(dpy, fromVal, toVal)
        !           309:     Display    *dpy;
        !           310:     XrmValue   fromVal;
        !           311:     XrmValue   *toVal;
        !           312: {
        !           313:     static Font        f;
        !           314: 
        !           315:     f = XLoadFont(dpy, (char *)fromVal.addr);
        !           316:     if (f != 0) { done(&f, Font); }
        !           317: }
        !           318: 
        !           319: 
        !           320: /*ARGSUSED*/
        !           321: static void CvtIntToFont(dpy, fromVal, toVal)
        !           322:     Display    *dpy;
        !           323:     XrmValue   fromVal;
        !           324:     XrmValue   *toVal;
        !           325: {
        !           326:     done(fromVal.addr, int);
        !           327: };
        !           328: 
        !           329: 
        !           330: /*ARGSUSED*/
        !           331: static void CvtStringToFontStruct(dpy, fromVal, toVal)
        !           332:     Display    *dpy;
        !           333:     XrmValue   fromVal;
        !           334:     XrmValue   *toVal;
        !           335: {
        !           336:     static XFontStruct *f;
        !           337: 
        !           338:     f = XLoadQueryFont(dpy, (char *)fromVal.addr);
        !           339:     if (f != NULL) { done(&f, XFontStruct *); }
        !           340: }
        !           341: 
        !           342: /*ARGSUSED*/
        !           343: static void CvtStringToGeometry(dpy, fromVal, toVal)
        !           344:     Display    *dpy;
        !           345:     XrmValue   fromVal;
        !           346:     XrmValue   *toVal;
        !           347: {
        !           348:     static Geometry    g;
        !           349:     int                        i;
        !           350: 
        !           351:     g.pos.xpos = g.pos.ypos = g.dims.width = g.dims.height = 0;
        !           352:     i = XParseGeometry((char *) fromVal.addr,
        !           353:            &g.pos.xpos, &g.pos.ypos, &g.dims.width, &g.dims.height);
        !           354:     if (i == NoValue) return;
        !           355:     if (i & XNegative)
        !           356:        g.pos.xpos = DisplayWidth(dpy, DefaultScreen(dpy))-1-g.pos.xpos;
        !           357:     if (i & YNegative)
        !           358:        g.pos.ypos = DisplayHeight(dpy, DefaultScreen(dpy))-1-g.pos.ypos;
        !           359:     done(&g, Geometry);
        !           360: };
        !           361: 
        !           362: 
        !           363: /*ARGSUSED*/
        !           364: static void CvtStringToInt(dpy, fromVal, toVal)
        !           365:     Display    *dpy;
        !           366:     XrmValue   fromVal;
        !           367:     XrmValue   *toVal;
        !           368: {
        !           369:     static int i;
        !           370: 
        !           371:     if (sscanf((char *)fromVal.addr, "%d", &i) == 1) { done(&i, int); }
        !           372: }
        !           373: 
        !           374: 
        !           375: /*ARGSUSED*/
        !           376: static void CvtStringToPixel(dpy, fromVal, toVal)
        !           377:     Display    *dpy;
        !           378:     XrmValue   fromVal;
        !           379:     XrmValue   *toVal;
        !           380: {
        !           381:     _XrmConvert(dpy, XrmQString, fromVal, XrmQColor, toVal);
        !           382:     if ((*toVal).addr == NULL) return;
        !           383:     done(&((XColor *)((*toVal).addr))->pixel, int)
        !           384: };
        !           385: 
        !           386: 
        !           387: /*ARGSUSED*/
        !           388: static void CvtXColorToPixel(dpy, fromVal, toVal)
        !           389:     Display    *dpy;
        !           390:     XrmValue   fromVal;
        !           391:     XrmValue   *toVal;
        !           392: {
        !           393:     done(&((XColor *)fromVal.addr)->pixel, int);
        !           394: };
        !           395: 
        !           396: 
        !           397: /*ARGSUSED*/
        !           398: static void CvtIntToPixel(dpy, fromVal, toVal)
        !           399:     Display    *dpy;
        !           400:     XrmValue   fromVal;
        !           401:     XrmValue   *toVal;
        !           402: {
        !           403:        done(fromVal.addr, int);
        !           404: };
        !           405: 
        !           406: 
        !           407: /*ARGSUSED*/
        !           408: static void CvtStringToPixmap(dpy, fromVal, toVal)
        !           409:     Display    *dpy;
        !           410:     XrmValue   fromVal;
        !           411:     XrmValue   *toVal;
        !           412: {
        !           413:     XrmValue   pixelVal;
        !           414: 
        !           415:     _XrmConvert(dpy, XrmQString, fromVal, XrmQPixel, &pixelVal);
        !           416:     if (pixelVal.addr == NULL) return;
        !           417:     _XrmConvert(dpy, XrmQPixel, pixelVal, XrmQPixmap, toVal);
        !           418: }
        !           419: 
        !           420: 
        !           421: static Bool initialized = False;
        !           422: 
        !           423: void XrmInitialize()
        !           424: {
        !           425:     if (initialized)
        !           426:        return;
        !           427:     initialized = True;
        !           428: 
        !           429:     _XrmRegisterTypeConverter(XrmQColor, XrmQPixel,    CvtXColorToPixel);
        !           430: 
        !           431:     _XrmRegisterTypeConverter(XrmQGeometry, XrmQDims,  CvtGeometryToDims);
        !           432: 
        !           433:     _XrmRegisterTypeConverter(XrmQInt,         XrmQBoolean,    CvtIntToBoolean);
        !           434:     _XrmRegisterTypeConverter(XrmQInt,         XrmQPixel,      CvtIntToPixel);
        !           435:     _XrmRegisterTypeConverter(XrmQInt,         XrmQFont,       CvtIntToFont);
        !           436:     _XrmRegisterTypeConverter(XrmQInt,         XrmQColor,      CvtIntOrPixelToXColor);
        !           437: 
        !           438:     _XrmRegisterTypeConverter(XrmQString, XrmQBoolean, CvtStringToBoolean);
        !           439:     _XrmRegisterTypeConverter(XrmQString, XrmQColor,   CvtStringToXColor);
        !           440:     _XrmRegisterTypeConverter(XrmQString, XrmQCursor,  CvtStringToCursor);
        !           441:     _XrmRegisterTypeConverter(XrmQString, XrmQDisplay, CvtStringToDisplay);
        !           442:     _XrmRegisterTypeConverter(XrmQString, XrmQFile,    CvtStringToFile);
        !           443:     _XrmRegisterTypeConverter(XrmQString, XrmQFont,    CvtStringToFont);
        !           444:     _XrmRegisterTypeConverter(XrmQString, XrmQFontStruct,      CvtStringToFontStruct);
        !           445:     _XrmRegisterTypeConverter(XrmQString, XrmQGeometry,        CvtStringToGeometry);
        !           446:     _XrmRegisterTypeConverter(XrmQString, XrmQInt,     CvtStringToInt);
        !           447:     _XrmRegisterTypeConverter(XrmQString, XrmQPixel,   CvtStringToPixel);
        !           448:     _XrmRegisterTypeConverter(XrmQString, XrmQPixmap,  CvtStringToPixmap);
        !           449: 
        !           450:     _XrmRegisterTypeConverter(XrmQPixel, XrmQColor,    CvtIntOrPixelToXColor);
        !           451: 
        !           452: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.