~ubuntu-branches/ubuntu/precise/boinc/precise

« back to all changes in this revision

Viewing changes to samples/glut/win32_x11.c

Tags: 6.12.8+dfsg-1
* New upstream release.
* Simplified debian/rules

Show diffs side-by-side

added added

removed removed

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