1 /*
2 * Copyright (C) 1989-95 GROUPE BULL
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to
6 * deal in the Software without restriction, including without limitation the
7 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
8 * sell copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * GROUPE BULL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
18 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
19 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20 *
21 * Except as contained in this notice, the name of GROUPE BULL shall not be
22 * used in advertising or otherwise to promote the sale, use or other dealings
23 * in this Software without prior written authorization from GROUPE BULL.
24 */
25
26 /*****************************************************************************\
27 * simx.c: 0.1a *
28 * *
29 * This emulates some Xlib functionality for MSW. It's not a general solution, *
30 * it is close related to XPM-lib. It is only intended to satisfy what is need *
31 * there. Thus allowing to read XPM files under MS windows. *
32 * *
33 * Developed by HeDu 3/94 (hedu@cul-ipn.uni-kiel.de) *
34 \*****************************************************************************/
35
36 #ifdef FOR_MSW
37
38 #ifdef HAVE_CONFIG_H
39 #include <config.h>
40 #endif
41 #include "xpm.h"
42 #include "xpmi.h" /* for XpmMalloc */
43
44 /*
45 * On DOS size_t is only 2 bytes, thus malloc(size_t s) can only malloc
46 * 64K. BUT an expression data=malloc(width*height) may result in an
47 * overflow. So this function takes a long as input, and returns NULL if the
48 * request is larger than 64K, is size_t is only 2 bytes.
49 *
50 * This requires casts like XpmMalloc( (long)width*(long(height)), else it
51 * might have no effect at all.
52 */
53
54 void *
boundCheckingMalloc(long s)55 boundCheckingMalloc(long s)
56 {
57 if (sizeof(size_t) == sizeof(long)) { /* same size, just do it */
58 return (malloc((size_t) s));
59 } else {
60 if (sizeof(size_t) == 2) {
61 if (s > 0xFFFF)
62 return (NULL); /* to large, size_t with 2 bytes
63 * only allows 16 bits */
64 else
65 return (malloc((size_t) s));
66 } else { /* it's not a long, not 2 bytes,
67 * what is it ??? */
68 return (malloc((size_t) s));
69 }
70 }
71 }
72 void *
boundCheckingCalloc(long num,long s)73 boundCheckingCalloc(long num, long s)
74 {
75 if (sizeof(size_t) == sizeof(long)) { /* same size, just do it */
76 return (calloc((size_t) num, (size_t) s));
77 } else {
78 if (sizeof(size_t) == 2) {
79 if (s > 0xFFFF || num * s > 0xFFFF)
80 return (NULL); /* to large, size_t with 2 bytes
81 * only allows 16 bits */
82 else
83 return (calloc((size_t) num, (size_t) s));
84 } else { /* it's not a long, not 2 bytes,
85 * what is it ??? */
86 return (calloc((size_t) num, (size_t) s));
87 }
88 }
89 }
90 void *
boundCheckingRealloc(void * p,long s)91 boundCheckingRealloc(void *p, long s)
92 {
93 if (sizeof(size_t) == sizeof(long)) { /* same size, just do it */
94 return (realloc(p, (size_t) s));
95 } else {
96 if (sizeof(size_t) == 2) {
97 if (s > 0xFFFF)
98 return (NULL); /* to large, size_t with 2 bytes
99 * only allows 16 bits */
100 else
101 return (realloc(p, (size_t) s));
102 } else { /* it's not a long, not 2 bytes,
103 * what is it ??? */
104 return (realloc(p, (size_t) s));
105 }
106 }
107 }
108
109 /* static Visual theVisual = { 0 }; */
110 Visual *
XDefaultVisual(Display * display,Screen * screen)111 XDefaultVisual(Display *display, Screen *screen)
112 {
113 return (NULL); /* struct could contain info about
114 * MONO, GRAY, COLOR */
115 }
116
117 Screen *
XDefaultScreen(Display * d)118 XDefaultScreen(Display *d)
119 {
120 return (NULL);
121 }
122
123 /* I get only 1 plane but 8 bits per pixel,
124 so I think BITSPIXEL should be depth */
125 int
XDefaultDepth(Display * display,Screen * screen)126 XDefaultDepth(Display *display, Screen *screen)
127 {
128 int d, b;
129
130 b = GetDeviceCaps(*display, BITSPIXEL);
131 d = GetDeviceCaps(*display, PLANES);
132 return (b);
133 }
134
135 Colormap *
XDefaultColormap(Display * display,Screen * screen)136 XDefaultColormap(Display *display, Screen *screen)
137 {
138 return (NULL);
139 }
140
141 /* convert hex color names,
142 wrong digits (not a-f,A-F,0-9) are treated as zero */
143 static int
hexCharToInt(c)144 hexCharToInt(c)
145 {
146 int r;
147
148 if (c >= '0' && c <= '9')
149 r = c - '0';
150 else if (c >= 'a' && c <= 'f')
151 r = c - 'a' + 10;
152 else if (c >= 'A' && c <= 'F')
153 r = c - 'A' + 10;
154 else
155 r = 0;
156
157 return (r);
158 }
159
160 static int
rgbFromHex(char * hex,int * r,int * g,int * b)161 rgbFromHex(char *hex, int *r, int *g, int *b)
162 {
163 int len;
164
165 if (hex == NULL || hex[0] != '#')
166 return (0);
167
168 len = strlen(hex);
169 if (len == 3 + 1) {
170 *r = hexCharToInt(hex[1]);
171 *g = hexCharToInt(hex[2]);
172 *b = hexCharToInt(hex[3]);
173 } else if (len == 6 + 1) {
174 *r = hexCharToInt(hex[1]) * 16 + hexCharToInt(hex[2]);
175 *g = hexCharToInt(hex[3]) * 16 + hexCharToInt(hex[4]);
176 *b = hexCharToInt(hex[5]) * 16 + hexCharToInt(hex[6]);
177 } else if (len == 12 + 1) {
178 /* it's like c #32329999CCCC */
179 /* so for now only take two digits */
180 *r = hexCharToInt(hex[1]) * 16 + hexCharToInt(hex[2]);
181 *g = hexCharToInt(hex[5]) * 16 + hexCharToInt(hex[6]);
182 *b = hexCharToInt(hex[9]) * 16 + hexCharToInt(hex[10]);
183 } else
184 return (0);
185
186 return (1);
187 }
188
189 /* Color related functions */
190 int
XParseColor(Display * d,Colormap * cmap,char * name,XColor * color)191 XParseColor(Display *d, Colormap *cmap, char *name, XColor *color)
192 {
193 int r, g, b; /* only 8 bit values used */
194 int okay;
195
196 /* TODO: use colormap via PALETTE */
197 /* parse name either in table or #RRGGBB #RGB */
198 if (name == NULL)
199 return (0);
200
201 if (name[0] == '#') { /* a hex string */
202 okay = rgbFromHex(name, &r, &g, &b);
203 } else {
204 okay = xpmGetRGBfromName(name, &r, &g, &b);
205 }
206
207 if (okay) {
208 color->pixel = RGB(r, g, b);
209 color->red = (BYTE) r;
210 color->green = (BYTE) g;
211 color->blue = (BYTE) b;
212 return (1);
213 } else
214 return (0); /* --> ColorError */
215 }
216
217
218 int
XAllocColor(Display * d,Colormap cmap,XColor * color)219 XAllocColor(Display *d, Colormap cmap, XColor *color)
220 {
221 /* colormap not used yet so color->pixel is the real COLORREF (RBG) and not an
222 index in some colormap as in X */
223 return (1);
224 }
225 void
XQueryColors(Display * display,Colormap * colormap,XColor * xcolors,int ncolors)226 XQueryColors(Display *display, Colormap *colormap,
227 XColor *xcolors, int ncolors)
228 {
229 /* under X this fills the rgb values to given .pixel */
230 /* since there no colormap use FOR_MSW (not yet!!), rgb is plain encoded */
231 XColor *xc = xcolors;
232 int i;
233
234 for (i = 0; i < ncolors; i++, xc++) {
235 xc->red = GetRValue(xc->pixel);
236 xc->green = GetGValue(xc->pixel);
237 xc->blue = GetBValue(xc->pixel);
238 }
239 return;
240 }
241 int
XFreeColors(Display * d,Colormap cmap,unsigned long pixels[],int npixels,unsigned long planes)242 XFreeColors(Display *d, Colormap cmap,
243 unsigned long pixels[], int npixels, unsigned long planes)
244 {
245 /* no colormap yet */
246 return (0); /* correct ??? */
247 }
248
249 /* XImage functions */
250 XImage *
XCreateImage(Display * d,Visual * v,int depth,int format,int x,int y,int width,int height,int pad,int foo)251 XCreateImage(Display *d, Visual *v,
252 int depth, int format,
253 int x, int y, int width, int height,
254 int pad, int foo)
255 {
256 XImage *img = (XImage *) XpmMalloc(sizeof(XImage));
257
258 if (img) {
259 /*JW: This is what it should be, but the picture comes out
260 just black!? It appears to be doing monochrome reduction,
261 but I've got no clue why. Using CreateBitmap() is supposed
262 to be slower, but otherwise ok
263 if ( depth == GetDeviceCaps(*d, BITSPIXEL) ) {
264 img->bitmap = CreateCompatibleBitmap(*d, width, height);
265 } else*/ {
266 img->bitmap = CreateBitmap(width, height, 1 /* plane */ ,
267 depth /* bits per pixel */ , NULL);
268 }
269 img->width = width;
270 img->height = height;
271 img->depth = depth;
272 }
273 return (img);
274
275 }
276
277 void
XImageFree(XImage * img)278 XImageFree(XImage *img)
279 {
280 if (img) {
281 XpmFree(img);
282 }
283 }
284 void
XDestroyImage(XImage * img)285 XDestroyImage(XImage *img)
286 {
287 if (img) {
288 DeleteObject(img->bitmap); /* check return ??? */
289 XImageFree(img);
290 }
291 }
292
293 #endif
294