Back to index

plt-scheme  4.2.1
simx.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 1989-95 GROUPE BULL
00003  *
00004  * Permission is hereby granted, free of charge, to any person obtaining a copy
00005  * of this software and associated documentation files (the "Software"), to
00006  * deal in the Software without restriction, including without limitation the
00007  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
00008  * sell copies of the Software, and to permit persons to whom the Software is
00009  * furnished to do so, subject to the following conditions:
00010  *
00011  * The above copyright notice and this permission notice shall be included in
00012  * all copies or substantial portions of the Software.
00013  *
00014  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00015  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00016  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
00017  * GROUPE BULL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
00018  * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
00019  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00020  *
00021  * Except as contained in this notice, the name of GROUPE BULL shall not be
00022  * used in advertising or otherwise to promote the sale, use or other dealings
00023  * in this Software without prior written authorization from GROUPE BULL.
00024  */
00025 
00026 /*****************************************************************************\
00027 * simx.c: 0.1a                                                                *
00028 *                                                                             *
00029 * This emulates some Xlib functionality for MSW. It's not a general solution, *
00030 * it is close related to XPM-lib. It is only intended to satisfy what is need *
00031 * there. Thus allowing to read XPM files under MS windows.                    *
00032 *                                                                             *
00033 * Developed by HeDu 3/94 (hedu@cul-ipn.uni-kiel.de)                           *
00034 \*****************************************************************************/
00035 
00036 #ifdef FOR_MSW
00037 
00038 #include "xpm.h"
00039 #include "xpmp.h"                  /* for XpmMalloc */
00040 
00041 /*
00042  * On DOS size_t is only 2 bytes, thus malloc(size_t s) can only malloc
00043  * 64K. BUT an expression data=malloc(width*height) may result in an
00044  * overflow. So this function takes a long as input, and returns NULL if the
00045  * request is larger than 64K, is size_t is only 2 bytes.
00046  *
00047  * This requires casts like XpmMalloc( (long)width*(long(height)), else it
00048  * might have no effect at all.
00049  */
00050 
00051 void *
00052 boundCheckingMalloc(long s)
00053 {
00054     if (sizeof(size_t) == sizeof(long)) { /* same size, just do it */
00055        return (malloc((size_t) s));
00056     } else {
00057        if (sizeof(size_t) == 2) {
00058            if (s > 0xFFFF)
00059               return (NULL);              /* to large, size_t with 2 bytes
00060                                     * only allows 16 bits */
00061            else
00062               return (malloc((size_t) s));
00063        } else {                    /* it's not a long, not 2 bytes,
00064                                     * what is it ??? */
00065            return (malloc((size_t) s));
00066        }
00067     }
00068 }
00069 void *
00070 boundCheckingCalloc(long num, long s)
00071 {
00072     if (sizeof(size_t) == sizeof(long)) { /* same size, just do it */
00073        return (calloc((size_t) num, (size_t) s));
00074     } else {
00075        if (sizeof(size_t) == 2) {
00076            if (s > 0xFFFF || num * s > 0xFFFF)
00077               return (NULL);              /* to large, size_t with 2 bytes
00078                                     * only allows 16 bits */
00079            else
00080               return (calloc((size_t) num, (size_t) s));
00081        } else {                    /* it's not a long, not 2 bytes,
00082                                     * what is it ??? */
00083            return (calloc((size_t) num, (size_t) s));
00084        }
00085     }
00086 }
00087 void *
00088 boundCheckingRealloc(void *p, long s)
00089 {
00090     if (sizeof(size_t) == sizeof(long)) { /* same size, just do it */
00091        return (realloc(p, (size_t) s));
00092     } else {
00093        if (sizeof(size_t) == 2) {
00094            if (s > 0xFFFF)
00095               return (NULL);              /* to large, size_t with 2 bytes
00096                                     * only allows 16 bits */
00097            else
00098               return (realloc(p, (size_t) s));
00099        } else {                    /* it's not a long, not 2 bytes,
00100                                     * what is it ??? */
00101            return (realloc(p, (size_t) s));
00102        }
00103     }
00104 }
00105 
00106 /* static Visual theVisual = { 0 }; */
00107 Visual *
00108 XDefaultVisual(Display *display, Screen *screen)
00109 {
00110     return (NULL);                 /* struct could contain info about
00111                                     * MONO, GRAY, COLOR */
00112 }
00113 
00114 Screen *
00115 XDefaultScreen(Display *d)
00116 {
00117     return (NULL);
00118 }
00119 
00120 /* I get only 1 plane but 8 bits per pixel,
00121    so I think BITSPIXEL should be depth */
00122 int 
00123 XDefaultDepth(Display *display, Screen *screen)
00124 {
00125     int d, b;
00126 
00127     b = GetDeviceCaps(*display, BITSPIXEL);
00128     d = GetDeviceCaps(*display, PLANES);
00129     return (b);
00130 }
00131 
00132 Colormap *
00133 XDefaultColormap(Display *display, Screen *screen)
00134 {
00135     return (NULL);
00136 }
00137 
00138 /* convert hex color names,
00139    wrong digits (not a-f,A-F,0-9) are treated as zero */
00140 static int 
00141 hexCharToInt(c)
00142 {
00143     int r;
00144 
00145     if (c >= '0' && c <= '9')
00146        r = c - '0';
00147     else if (c >= 'a' && c <= 'f')
00148        r = c - 'a' + 10;
00149     else if (c >= 'A' && c <= 'F')
00150        r = c - 'A' + 10;
00151     else
00152        r = 0;
00153 
00154     return (r);
00155 }
00156 
00157 static int 
00158 rgbFromHex(char *hex, int *r, int *g, int *b)
00159 {
00160     int len;
00161 
00162     if (hex == NULL || hex[0] != '#')
00163        return (0);
00164 
00165     len = strlen(hex);
00166     if (len == 3 + 1) {
00167        *r = hexCharToInt(hex[1]);
00168        *g = hexCharToInt(hex[2]);
00169        *b = hexCharToInt(hex[3]);
00170     } else if (len == 6 + 1) {
00171        *r = hexCharToInt(hex[1]) * 16 + hexCharToInt(hex[2]);
00172        *g = hexCharToInt(hex[3]) * 16 + hexCharToInt(hex[4]);
00173        *b = hexCharToInt(hex[5]) * 16 + hexCharToInt(hex[6]);
00174     } else if (len == 12 + 1) {
00175        /* it's like c #32329999CCCC */
00176        /* so for now only take two digits */
00177        *r = hexCharToInt(hex[1]) * 16 + hexCharToInt(hex[2]);
00178        *g = hexCharToInt(hex[5]) * 16 + hexCharToInt(hex[6]);
00179        *b = hexCharToInt(hex[9]) * 16 + hexCharToInt(hex[10]);
00180     } else
00181        return (0);
00182 
00183     return (1);
00184 }
00185 
00186 /* Color related functions */
00187 int 
00188 XParseColor(Display *d, Colormap *cmap, char *name, XColor *color)
00189 {
00190     int r, g, b;                   /* only 8 bit values used */
00191     int okay;
00192 
00193 /* TODO: use colormap via PALETTE */
00194     /* parse name either in table or #RRGGBB #RGB */
00195     if (name == NULL)
00196        return (0);
00197 
00198     if (name[0] == '#') {          /* a hex string */
00199        okay = rgbFromHex(name, &r, &g, &b);
00200     } else {
00201        okay = xpmGetRGBfromName(name, &r, &g, &b);
00202     }
00203 
00204     if (okay) {
00205        color->pixel = RGB(r, g, b);
00206        color->red = (BYTE) r;
00207        color->green = (BYTE) g;
00208        color->blue = (BYTE) b;
00209        return (1);
00210     } else
00211        return (0);                 /* --> ColorError */
00212 }
00213 
00214 
00215 int 
00216 XAllocColor(Display *d, Colormap cmap, XColor *color)
00217 {
00218 /* colormap not used yet so color->pixel is the real COLORREF (RBG) and not an
00219    index in some colormap as in X */
00220     return (1);
00221 }
00222 void 
00223 XQueryColors(Display *display, Colormap *colormap,
00224             XColor *xcolors, int ncolors)
00225 {
00226 /* under X this fills the rgb values to given .pixel */
00227 /* since there no colormap use FOR_MSW (not yet!!), rgb is plain encoded */
00228     XColor *xc = xcolors;
00229     int i;
00230 
00231     for (i = 0; i < ncolors; i++, xc++) {
00232        xc->red = GetRValue(xc->pixel);
00233        xc->green = GetGValue(xc->pixel);
00234        xc->blue = GetBValue(xc->pixel);
00235     }
00236     return;
00237 }
00238 int 
00239 XFreeColors(Display *d, Colormap cmap,
00240            unsigned long pixels[], int npixels, unsigned long planes)
00241 {
00242     /* no colormap yet */
00243     return (0);                           /* correct ??? */
00244 }
00245 
00246 /* XImage functions */
00247 XImage *
00248 XCreateImage(Display *d, Visual *v,
00249             int depth, int format,
00250             int x, int y, int width, int height,
00251             int pad, int foo)
00252 {
00253     XImage *img = (XImage *) XpmMalloc(sizeof(XImage));
00254 
00255     if (img) {
00256        /* *img = CreateCompatibleBitmap(*d, width, height); */
00257        img->bitmap = CreateBitmap(width, height, 1 /* plane */ ,
00258                                depth /* bits per pixel */ , NULL);
00259        img->width = width;
00260        img->height = height;
00261        img->depth = depth;
00262     }
00263     return (img);
00264 
00265 }
00266 
00267 void 
00268 XImageFree(XImage *img)
00269 {
00270     if (img) {
00271        XpmFree(img);
00272     }
00273 }
00274 void 
00275 XDestroyImage(XImage *img)
00276 {
00277     if (img) {
00278        DeleteObject(img->bitmap);  /* check return ??? */
00279        XImageFree(img);
00280     }
00281 }
00282 
00283 #endif