#include "glutint.h"
HDC XHDC;
XVisualInfo*
XGetVisualInfo(Display* display, long mask, XVisualInfo* template, int* nitems)
{
PIXELFORMATDESCRIPTOR* pfds;
int i, n;
n = DescribePixelFormat(XHDC, 0, 0, NULL);
pfds = (PIXELFORMATDESCRIPTOR*)malloc(sizeof(PIXELFORMATDESCRIPTOR) * n);
memset(pfds, 0, sizeof(PIXELFORMATDESCRIPTOR) * n);
for (i = 0; i < n; i++) {
DescribePixelFormat(XHDC, i + 1, sizeof(PIXELFORMATDESCRIPTOR), &pfds[i]);
}
*nitems = n;
return pfds;
}
Colormap
XCreateColormap(Display* display, Window root, Visual* visual, int alloc)
{
PIXELFORMATDESCRIPTOR pfd;
LOGPALETTE *logical;
HPALETTE palette;
int n;
memset(&pfd, 0, sizeof(PIXELFORMATDESCRIPTOR));
DescribePixelFormat(XHDC, GetPixelFormat(XHDC),
sizeof(PIXELFORMATDESCRIPTOR), &pfd);
if (!(pfd.dwFlags & PFD_NEED_PALETTE ||
pfd.iPixelType == PFD_TYPE_COLORINDEX))
{
return 0;
}
n = 1 << pfd.cColorBits;
logical = (LOGPALETTE*)malloc(sizeof(LOGPALETTE) +
sizeof(PALETTEENTRY) * n);
memset(logical, 0, sizeof(LOGPALETTE) + sizeof(PALETTEENTRY) * n);
logical->palVersion = 0x300;
logical->palNumEntries = n;
GetSystemPaletteEntries(XHDC, 0, 256, &logical->palPalEntry[0]);
if (pfd.iPixelType == PFD_TYPE_RGBA) {
int redMask = (1 << pfd.cRedBits) - 1;
int greenMask = (1 << pfd.cGreenBits) - 1;
int blueMask = (1 << pfd.cBlueBits) - 1;
int i;
for (i = 0; i < n; ++i) {
logical->palPalEntry[i].peRed =
(((i >> pfd.cRedShift) & redMask) * 255) / redMask;
logical->palPalEntry[i].peGreen =
(((i >> pfd.cGreenShift) & greenMask) * 255) / greenMask;
logical->palPalEntry[i].peBlue =
(((i >> pfd.cBlueShift) & blueMask) * 255) / blueMask;
logical->palPalEntry[i].peFlags = 0;
}
}
palette = CreatePalette(logical);
free(logical);
SelectPalette(XHDC, palette, FALSE);
RealizePalette(XHDC);
return palette;
}
void
XAllocColorCells(Display* display, Colormap colormap, Bool contig,
unsigned long plane_masks_return[], unsigned int nplanes,
unsigned long pixels_return[], unsigned int npixels)
{
}
void
XStoreColor(Display* display, Colormap colormap, XColor* color)
{
PALETTEENTRY pe;
pe.peRed = color->red / 256;
pe.peGreen = color->green / 256;
pe.peBlue = color->blue / 256;
pe.peFlags = PC_NOCOLLAPSE;
SetPaletteEntries(colormap, color->pixel, 1, &pe);
if (XHDC) {
UnrealizeObject(colormap);
SelectPalette(XHDC, colormap, FALSE);
RealizePalette(XHDC);
}
}
void
XSetWindowColormap(Display* display, Window window, Colormap colormap)
{
HDC hdc = GetDC(window);
SelectPalette(hdc, colormap, FALSE);
RealizePalette(hdc);
}
Bool
XTranslateCoordinates(Display *display, Window src, Window dst,
int src_x, int src_y,
int* dest_x_return, int* dest_y_return,
Window* child_return)
{
POINT point;
point.x = src_x;
point.y = src_y;
ClientToScreen(src, &point);
*dest_x_return = point.x;
*dest_y_return = point.y;
return True;
}
Status
XGetGeometry(Display* display, Window window, Window* root_return,
int* x_return, int* y_return,
unsigned int* width_return, unsigned int* height_return,
unsigned int *border_width_return, unsigned int* depth_return)
{
RECT rect;
POINT point;
GetClientRect(window, &rect);
point.x = 0;
point.y = 0;
ClientToScreen(window, &point);
*x_return = point.x;
*y_return = point.y;
*width_return = rect.right;
*height_return = rect.bottom;
return 1;
}
int
DisplayWidthMM(Display* display, int screen)
{
int width;
HWND hwnd = GetDesktopWindow();
HDC hdc = GetDC(hwnd);
width = GetDeviceCaps(hdc, HORZSIZE);
ReleaseDC(hwnd, hdc);
return width;
}
int
DisplayHeightMM(Display* display, int screen)
{
int height;
HWND hwnd = GetDesktopWindow();
HDC hdc = GetDC(hwnd);
height = GetDeviceCaps(hdc, VERTSIZE);
ReleaseDC(hwnd, hdc);
return height;
}
void
XWarpPointer(Display* display, Window src, Window dst,
int src_x, int src_y, int src_width, int src_height,
int dst_x, int dst_y)
{
POINT point;
point.x = dst_x;
point.y = dst_y;
ClientToScreen(dst, &point);
SetCursorPos(point.x, point.y);
}
int
XPending(Display* display)
{
MSG msg;
return PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE);
}
static int
ReadInteger(char *string, char **NextString)
{
register int Result = 0;
int Sign = 1;
if (*string == '+')
string++;
else if (*string == '-')
{
string++;
Sign = -1;
}
for (; (*string >= '0') && (*string <= '9'); string++)
{
Result = (Result * 10) + (*string - '0');
}
*NextString = string;
if (Sign >= 0)
return (Result);
else
return (-Result);
}
int XParseGeometry(char *string, int *x, int *y, unsigned int *width, unsigned int *height)
{
int mask = NoValue;
register char *strind;
unsigned int tempWidth, tempHeight;
int tempX, tempY;
char *nextCharacter;
if ( (string == NULL) || (*string == '\0')) return(mask);
if (*string == '=')
string++;
strind = (char *)string;
if (*strind != '+' && *strind != '-' && *strind != 'x') {
tempWidth = ReadInteger(strind, &nextCharacter);
if (strind == nextCharacter)
return (0);
strind = nextCharacter;
mask |= WidthValue;
}
if (*strind == 'x' || *strind == 'X') {
strind++;
tempHeight = ReadInteger(strind, &nextCharacter);
if (strind == nextCharacter)
return (0);
strind = nextCharacter;
mask |= HeightValue;
}
if ((*strind == '+') || (*strind == '-')) {
if (*strind == '-') {
strind++;
tempX = -ReadInteger(strind, &nextCharacter);
if (strind == nextCharacter)
return (0);
strind = nextCharacter;
mask |= XNegative;
}
else
{ strind++;
tempX = ReadInteger(strind, &nextCharacter);
if (strind == nextCharacter)
return(0);
strind = nextCharacter;
}
mask |= XValue;
if ((*strind == '+') || (*strind == '-')) {
if (*strind == '-') {
strind++;
tempY = -ReadInteger(strind, &nextCharacter);
if (strind == nextCharacter)
return(0);
strind = nextCharacter;
mask |= YNegative;
}
else
{
strind++;
tempY = ReadInteger(strind, &nextCharacter);
if (strind == nextCharacter)
return(0);
strind = nextCharacter;
}
mask |= YValue;
}
}
if (*strind != '\0') return (0);
if (mask & XValue)
*x = tempX;
if (mask & YValue)
*y = tempY;
if (mask & WidthValue)
*width = tempWidth;
if (mask & HeightValue)
*height = tempHeight;
return (mask);
}