1 | /* $Id: win32_x11.c,v 1.3 2000-03-11 15:07:48 sandervl Exp $ */
|
---|
2 | /* Copyright (c) Nate Robins, 1997. */
|
---|
3 | /* portions Copyright (c) Mark Kilgard, 1998. */
|
---|
4 |
|
---|
5 | /* This program is freely distributable without licensing fees
|
---|
6 | and is provided without guarantee or warrantee expressed or
|
---|
7 | implied. This program is -not- in the public domain. */
|
---|
8 |
|
---|
9 | #include <stdio.h>
|
---|
10 | #if defined(__WIN32OS2__)
|
---|
11 | #include "pfddefs.h"
|
---|
12 | #include "os2_x11.h"
|
---|
13 | #include <memory.h>
|
---|
14 | #else
|
---|
15 | #include "win32_x11.h"
|
---|
16 | #endif
|
---|
17 |
|
---|
18 | /* global variable that must be set for some functions to operate
|
---|
19 | correctly. */
|
---|
20 | HDC XHDC;
|
---|
21 |
|
---|
22 | XVisualInfo*
|
---|
23 | XGetVisualInfo(Display* display, long mask, XVisualInfo* template_, int* nitems)
|
---|
24 | {
|
---|
25 | /* KLUDGE: this function needs XHDC to be set to the HDC currently
|
---|
26 | being operated on before it is invoked! */
|
---|
27 |
|
---|
28 | PIXELFORMATDESCRIPTOR* pfds;
|
---|
29 | int i, n;
|
---|
30 |
|
---|
31 | n = DescribePixelFormat(XHDC, 0, 0, NULL);
|
---|
32 | pfds = (PIXELFORMATDESCRIPTOR*)malloc(sizeof(PIXELFORMATDESCRIPTOR) * n);
|
---|
33 | memset(pfds, 0, sizeof(PIXELFORMATDESCRIPTOR) * n);
|
---|
34 |
|
---|
35 | for (i = 0; i < n; i++) {
|
---|
36 | DescribePixelFormat(XHDC, i, sizeof(PIXELFORMATDESCRIPTOR), &pfds[i]);
|
---|
37 | }
|
---|
38 |
|
---|
39 | *nitems = n;
|
---|
40 | return pfds;
|
---|
41 | }
|
---|
42 |
|
---|
43 | Colormap
|
---|
44 | XCreateColormap(Display* display, Window root, Visual* visual, int alloc)
|
---|
45 | {
|
---|
46 | /* KLUDGE: this function needs XHDC to be set to the HDC currently
|
---|
47 | being operated on before it is invoked! */
|
---|
48 |
|
---|
49 | PIXELFORMATDESCRIPTOR pfd;
|
---|
50 | LOGPALETTE *logical;
|
---|
51 | HPALETTE palette;
|
---|
52 | int n;
|
---|
53 |
|
---|
54 | /* grab the pixel format */
|
---|
55 | memset(&pfd, 0, sizeof(PIXELFORMATDESCRIPTOR));
|
---|
56 | DescribePixelFormat(XHDC, GetPixelFormat(XHDC),
|
---|
57 | sizeof(PIXELFORMATDESCRIPTOR), &pfd);
|
---|
58 |
|
---|
59 | if (!(pfd.dwFlags & PFD_NEED_PALETTE ||
|
---|
60 | pfd.iPixelType == PFD_TYPE_COLORINDEX))
|
---|
61 | {
|
---|
62 | return 0;
|
---|
63 | }
|
---|
64 |
|
---|
65 | n = 1 << pfd.cColorBits;
|
---|
66 |
|
---|
67 | /* allocate a bunch of memory for the logical palette (assume 256
|
---|
68 | colors in a Win32 palette */
|
---|
69 | logical = (LOGPALETTE*)malloc(sizeof(LOGPALETTE) +
|
---|
70 | sizeof(PALETTEENTRY) * n);
|
---|
71 | memset(logical, 0, sizeof(LOGPALETTE) + sizeof(PALETTEENTRY) * n);
|
---|
72 |
|
---|
73 | /* set the entries in the logical palette */
|
---|
74 | logical->palVersion = 0x300;
|
---|
75 | logical->palNumEntries = n;
|
---|
76 |
|
---|
77 | /* start with a copy of the current system palette */
|
---|
78 | GetSystemPaletteEntries(XHDC, 0, 256, &logical->palPalEntry[0]);
|
---|
79 |
|
---|
80 | if (pfd.iPixelType == PFD_TYPE_RGBA) {
|
---|
81 | int redMask = (1 << pfd.cRedBits) - 1;
|
---|
82 | int greenMask = (1 << pfd.cGreenBits) - 1;
|
---|
83 | int blueMask = (1 << pfd.cBlueBits) - 1;
|
---|
84 | int i;
|
---|
85 |
|
---|
86 | /* fill in an RGBA color palette */
|
---|
87 | for (i = 0; i < n; ++i) {
|
---|
88 | logical->palPalEntry[i].peRed =
|
---|
89 | (((i >> pfd.cRedShift) & redMask) * 255) / redMask;
|
---|
90 | logical->palPalEntry[i].peGreen =
|
---|
91 | (((i >> pfd.cGreenShift) & greenMask) * 255) / greenMask;
|
---|
92 | logical->palPalEntry[i].peBlue =
|
---|
93 | (((i >> pfd.cBlueShift) & blueMask) * 255) / blueMask;
|
---|
94 | logical->palPalEntry[i].peFlags = 0;
|
---|
95 | }
|
---|
96 | }
|
---|
97 |
|
---|
98 | palette = CreatePalette(logical);
|
---|
99 | free(logical);
|
---|
100 |
|
---|
101 | SelectPalette(XHDC, palette, FALSE);
|
---|
102 | RealizePalette(XHDC);
|
---|
103 |
|
---|
104 | return palette;
|
---|
105 | }
|
---|
106 |
|
---|
107 | void
|
---|
108 | XAllocColorCells(Display* display, Colormap colormap, Bool contig,
|
---|
109 | unsigned long plane_masks_return[], unsigned int nplanes,
|
---|
110 | unsigned long pixels_return[], unsigned int npixels)
|
---|
111 | {
|
---|
112 | /* NOP -- we did all the allocating in XCreateColormap! */
|
---|
113 | }
|
---|
114 |
|
---|
115 | void
|
---|
116 | XStoreColor(Display* display, Colormap colormap, XColor* color)
|
---|
117 | {
|
---|
118 | /* KLUDGE: set XHDC to 0 if the palette should NOT be realized after
|
---|
119 | setting the color. set XHDC to the correct HDC if it should. */
|
---|
120 |
|
---|
121 | PALETTEENTRY pe;
|
---|
122 |
|
---|
123 | /* X11 stores color from 0-65535, Win32 expects them to be 0-256, so
|
---|
124 | twiddle the bits ( / 256). */
|
---|
125 | pe.peRed = color->red / 256;
|
---|
126 | pe.peGreen = color->green / 256;
|
---|
127 | pe.peBlue = color->blue / 256;
|
---|
128 |
|
---|
129 | /* make sure we use this flag, otherwise the colors might get mapped
|
---|
130 | to another place in the colormap, and when we glIndex() that
|
---|
131 | color, it may have moved (argh!!) */
|
---|
132 | pe.peFlags = PC_NOCOLLAPSE;
|
---|
133 |
|
---|
134 | /* the pixel field of the XColor structure is the index into the
|
---|
135 | colormap */
|
---|
136 | SetPaletteEntries(colormap, color->pixel, 1, &pe);
|
---|
137 |
|
---|
138 | if (XHDC) {
|
---|
139 | UnrealizeObject(colormap);
|
---|
140 | SelectPalette(XHDC, colormap, FALSE);
|
---|
141 | RealizePalette(XHDC);
|
---|
142 | }
|
---|
143 | }
|
---|
144 |
|
---|
145 | void
|
---|
146 | XSetWindowColormap(Display* display, Window window, Colormap colormap)
|
---|
147 | {
|
---|
148 | HDC hdc = GetDC(window);
|
---|
149 |
|
---|
150 | /* if the third parameter is FALSE, the logical colormap is copied
|
---|
151 | into the device palette when the application is in the
|
---|
152 | foreground, if it is TRUE, the colors are mapped into the current
|
---|
153 | palette in the best possible way. */
|
---|
154 | SelectPalette(hdc, colormap, FALSE);
|
---|
155 | RealizePalette(hdc);
|
---|
156 |
|
---|
157 | /* note that we don't have to release the DC, since our window class
|
---|
158 | uses the WC_OWNDC flag! */
|
---|
159 | }
|
---|
160 |
|
---|
161 | Bool
|
---|
162 | XTranslateCoordinates(Display *display, Window src, Window dst,
|
---|
163 | int src_x, int src_y,
|
---|
164 | int* dest_x_return, int* dest_y_return,
|
---|
165 | Window* child_return)
|
---|
166 | {
|
---|
167 | /* KLUDGE: this isn't really a translate coordinates into some other
|
---|
168 | windows coordinate system...it only translates coordinates into the
|
---|
169 | root window (screen) coordinate system. */
|
---|
170 |
|
---|
171 | POINT point;
|
---|
172 |
|
---|
173 | point.x = src_x;
|
---|
174 | point.y = src_y;
|
---|
175 |
|
---|
176 | ClientToScreen(src, &point);
|
---|
177 |
|
---|
178 | *dest_x_return = point.x;
|
---|
179 | *dest_y_return = point.y;
|
---|
180 |
|
---|
181 | /* just to make compilers happy...we don't use the return value. */
|
---|
182 | return True;
|
---|
183 | }
|
---|
184 |
|
---|
185 | Status
|
---|
186 | XGetGeometry(Display* display, Window window, Window* root_return,
|
---|
187 | int* x_return, int* y_return,
|
---|
188 | unsigned int* width_return, unsigned int* height_return,
|
---|
189 | unsigned int *border_width_return, unsigned int* depth_return)
|
---|
190 | {
|
---|
191 | /* KLUDGE: doesn't return the border_width or depth or root, x & y
|
---|
192 | are in screen coordinates. */
|
---|
193 |
|
---|
194 | RECT rect;
|
---|
195 | POINT point;
|
---|
196 |
|
---|
197 | GetClientRect(window, &rect);
|
---|
198 |
|
---|
199 | point.x = 0;
|
---|
200 | point.y = 0;
|
---|
201 | ClientToScreen(window, &point);
|
---|
202 |
|
---|
203 | *x_return = point.x;
|
---|
204 | *y_return = point.y;
|
---|
205 | *width_return = rect.right;
|
---|
206 | *height_return = rect.bottom;
|
---|
207 |
|
---|
208 | /* just to make compilers happy...we don't use the return value. */
|
---|
209 | return 1;
|
---|
210 | }
|
---|
211 |
|
---|
212 | int
|
---|
213 | DisplayWidthMM(Display* display, int screen)
|
---|
214 | {
|
---|
215 | int width;
|
---|
216 | HWND hwnd = GetDesktopWindow();
|
---|
217 | HDC hdc = GetDC(hwnd);
|
---|
218 |
|
---|
219 | width = GetDeviceCaps(hdc, HORZSIZE);
|
---|
220 |
|
---|
221 | /* make sure to release this DC (it's the desktops, not ours) */
|
---|
222 | ReleaseDC(hwnd, hdc);
|
---|
223 |
|
---|
224 | return width;
|
---|
225 | }
|
---|
226 |
|
---|
227 | int
|
---|
228 | DisplayHeightMM(Display* display, int screen)
|
---|
229 | {
|
---|
230 | int height;
|
---|
231 | HWND hwnd = GetDesktopWindow();
|
---|
232 | HDC hdc = GetDC(hwnd);
|
---|
233 |
|
---|
234 | height = GetDeviceCaps(hdc, VERTSIZE);
|
---|
235 |
|
---|
236 | /* make sure to release this DC (it's the desktops, not ours) */
|
---|
237 | ReleaseDC(hwnd, hdc);
|
---|
238 |
|
---|
239 | return height;
|
---|
240 | }
|
---|
241 |
|
---|
242 | void
|
---|
243 | XWarpPointer(Display* display, Window src, Window dst,
|
---|
244 | int src_x, int src_y, int src_width, int src_height,
|
---|
245 | int dst_x, int dst_y)
|
---|
246 | {
|
---|
247 | /* KLUDGE: this isn't really a warp pointer into some other windows
|
---|
248 | coordinate system...it only warps the pointer into the root window
|
---|
249 | (screen) coordinate system. */
|
---|
250 |
|
---|
251 | POINT point;
|
---|
252 |
|
---|
253 | point.x = dst_x;
|
---|
254 | point.y = dst_y;
|
---|
255 | ClientToScreen(dst, &point);
|
---|
256 |
|
---|
257 | SetCursorPos(point.x, point.y);
|
---|
258 | }
|
---|
259 |
|
---|
260 | int
|
---|
261 | XPending(Display* display)
|
---|
262 | {
|
---|
263 | /* similar functionality...I don't think that it is exact, but this
|
---|
264 | will have to do. */
|
---|
265 | MSG msg;
|
---|
266 |
|
---|
267 | return PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE);
|
---|
268 | }
|
---|
269 |
|
---|
270 | /* the following function was stolen from the X sources as indicated. */
|
---|
271 |
|
---|
272 | /* Copyright Massachusetts Institute of Technology 1985, 1986, 1987 */
|
---|
273 | /* $XConsortium: XParseGeom.c,v 11.18 91/02/21 17:23:05 rws Exp $ */
|
---|
274 |
|
---|
275 | /*
|
---|
276 | Permission to use, copy, modify, distribute, and sell this software and its
|
---|
277 | documentation for any purpose is hereby granted without fee, provided that
|
---|
278 | the above copyright notice appear in all copies and that both that
|
---|
279 | copyright notice and this permission notice appear in supporting
|
---|
280 | documentation, and that the name of M.I.T. not be used in advertising or
|
---|
281 | publicity pertaining to distribution of the software without specific,
|
---|
282 | written prior permission. M.I.T. makes no representations about the
|
---|
283 | suitability of this software for any purpose. It is provided "as is"
|
---|
284 | without express or implied warranty.
|
---|
285 | */
|
---|
286 |
|
---|
287 | /*
|
---|
288 | * XParseGeometry parses strings of the form
|
---|
289 | * "=<width>x<height>{+-}<xoffset>{+-}<yoffset>", where
|
---|
290 | * width, height, xoffset, and yoffset are unsigned integers.
|
---|
291 | * Example: "=80x24+300-49"
|
---|
292 | * The equal sign is optional.
|
---|
293 | * It returns a bitmask that indicates which of the four values
|
---|
294 | * were actually found in the string. For each value found,
|
---|
295 | * the corresponding argument is updated; for each value
|
---|
296 | * not found, the corresponding argument is left unchanged.
|
---|
297 | */
|
---|
298 |
|
---|
299 | static int
|
---|
300 | ReadInteger(char *string, char **NextString)
|
---|
301 | {
|
---|
302 | register int Result = 0;
|
---|
303 | int Sign = 1;
|
---|
304 |
|
---|
305 | if (*string == '+')
|
---|
306 | string++;
|
---|
307 | else if (*string == '-')
|
---|
308 | {
|
---|
309 | string++;
|
---|
310 | Sign = -1;
|
---|
311 | }
|
---|
312 | for (; (*string >= '0') && (*string <= '9'); string++)
|
---|
313 | {
|
---|
314 | Result = (Result * 10) + (*string - '0');
|
---|
315 | }
|
---|
316 | *NextString = string;
|
---|
317 | if (Sign >= 0)
|
---|
318 | return (Result);
|
---|
319 | else
|
---|
320 | return (-Result);
|
---|
321 | }
|
---|
322 |
|
---|
323 | int XParseGeometry(char *string, int *x, int *y, unsigned int *width, unsigned int *height)
|
---|
324 | {
|
---|
325 | int mask = NoValue;
|
---|
326 | register char *strind;
|
---|
327 | unsigned int tempWidth, tempHeight;
|
---|
328 | int tempX, tempY;
|
---|
329 | char *nextCharacter;
|
---|
330 |
|
---|
331 | if ( (string == NULL) || (*string == '\0')) return(mask);
|
---|
332 | if (*string == '=')
|
---|
333 | string++; /* ignore possible '=' at beg of geometry spec */
|
---|
334 |
|
---|
335 | strind = (char *)string;
|
---|
336 | if (*strind != '+' && *strind != '-' && *strind != 'x') {
|
---|
337 | tempWidth = ReadInteger(strind, &nextCharacter);
|
---|
338 | if (strind == nextCharacter)
|
---|
339 | return (0);
|
---|
340 | strind = nextCharacter;
|
---|
341 | mask |= WidthValue;
|
---|
342 | }
|
---|
343 |
|
---|
344 | if (*strind == 'x' || *strind == 'X') {
|
---|
345 | strind++;
|
---|
346 | tempHeight = ReadInteger(strind, &nextCharacter);
|
---|
347 | if (strind == nextCharacter)
|
---|
348 | return (0);
|
---|
349 | strind = nextCharacter;
|
---|
350 | mask |= HeightValue;
|
---|
351 | }
|
---|
352 |
|
---|
353 | if ((*strind == '+') || (*strind == '-')) {
|
---|
354 | if (*strind == '-') {
|
---|
355 | strind++;
|
---|
356 | tempX = -ReadInteger(strind, &nextCharacter);
|
---|
357 | if (strind == nextCharacter)
|
---|
358 | return (0);
|
---|
359 | strind = nextCharacter;
|
---|
360 | mask |= XNegative;
|
---|
361 |
|
---|
362 | }
|
---|
363 | else
|
---|
364 | { strind++;
|
---|
365 | tempX = ReadInteger(strind, &nextCharacter);
|
---|
366 | if (strind == nextCharacter)
|
---|
367 | return(0);
|
---|
368 | strind = nextCharacter;
|
---|
369 | }
|
---|
370 | mask |= XValue;
|
---|
371 | if ((*strind == '+') || (*strind == '-')) {
|
---|
372 | if (*strind == '-') {
|
---|
373 | strind++;
|
---|
374 | tempY = -ReadInteger(strind, &nextCharacter);
|
---|
375 | if (strind == nextCharacter)
|
---|
376 | return(0);
|
---|
377 | strind = nextCharacter;
|
---|
378 | mask |= YNegative;
|
---|
379 |
|
---|
380 | }
|
---|
381 | else
|
---|
382 | {
|
---|
383 | strind++;
|
---|
384 | tempY = ReadInteger(strind, &nextCharacter);
|
---|
385 | if (strind == nextCharacter)
|
---|
386 | return(0);
|
---|
387 | strind = nextCharacter;
|
---|
388 | }
|
---|
389 | mask |= YValue;
|
---|
390 | }
|
---|
391 | }
|
---|
392 |
|
---|
393 | /* If strind isn't at the end of the string the it's an invalid
|
---|
394 | geometry specification. */
|
---|
395 |
|
---|
396 | if (*strind != '\0') return (0);
|
---|
397 |
|
---|
398 | if (mask & XValue)
|
---|
399 | *x = tempX;
|
---|
400 | if (mask & YValue)
|
---|
401 | *y = tempY;
|
---|
402 | if (mask & WidthValue)
|
---|
403 | *width = tempWidth;
|
---|
404 | if (mask & HeightValue)
|
---|
405 | *height = tempHeight;
|
---|
406 | return (mask);
|
---|
407 | }
|
---|