src/video/wincommon/SDL_syswm.c
author Sam Lantinga <slouken@libsdl.org>
Sat, 17 Aug 2002 18:03:06 +0000
changeset 439 3ac8344e3872
parent 438 f9c5f135a8b9
child 1152 51a8702d8ecd
permissions -rw-r--r--
*** empty log message ***
slouken@0
     1
/*
slouken@0
     2
    SDL - Simple DirectMedia Layer
slouken@0
     3
    Copyright (C) 1997, 1998, 1999  Sam Lantinga
slouken@0
     4
slouken@0
     5
    This library is free software; you can redistribute it and/or
slouken@0
     6
    modify it under the terms of the GNU Library General Public
slouken@0
     7
    License as published by the Free Software Foundation; either
slouken@0
     8
    version 2 of the License, or (at your option) any later version.
slouken@0
     9
slouken@0
    10
    This library is distributed in the hope that it will be useful,
slouken@0
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@0
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@0
    13
    Library General Public License for more details.
slouken@0
    14
slouken@0
    15
    You should have received a copy of the GNU Library General Public
slouken@0
    16
    License along with this library; if not, write to the Free
slouken@0
    17
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
slouken@0
    18
slouken@0
    19
    Sam Lantinga
slouken@252
    20
    slouken@libsdl.org
slouken@0
    21
*/
slouken@0
    22
slouken@0
    23
#ifdef SAVE_RCSID
slouken@0
    24
static char rcsid =
slouken@0
    25
 "@(#) $Id$"
slouken@0
    26
#endif
slouken@0
    27
slouken@0
    28
#include <stdio.h>
slouken@0
    29
#include <malloc.h>
slouken@0
    30
#include <windows.h>
slouken@0
    31
slouken@0
    32
#include "SDL_version.h"
slouken@0
    33
#include "SDL_error.h"
slouken@0
    34
#include "SDL_video.h"
slouken@0
    35
#include "SDL_syswm.h"
slouken@0
    36
#include "SDL_syswm_c.h"
slouken@439
    37
#include "SDL_wingl_c.h"
slouken@0
    38
#include "SDL_pixels_c.h"
slouken@0
    39
slouken@0
    40
#ifdef _WIN32_WCE
slouken@0
    41
#define DISABLE_ICON_SUPPORT
slouken@0
    42
#endif
slouken@0
    43
slouken@0
    44
/*	RJR: March 28, 2000
slouken@0
    45
	we need "SDL_cursor_c.h" for mods to WIN_GrabInput */
slouken@0
    46
#include "SDL_cursor_c.h"
slouken@0
    47
slouken@0
    48
/* The screen icon -- needs to be freed on SDL_VideoQuit() */
slouken@0
    49
HICON   screen_icn = NULL;
slouken@0
    50
slouken@0
    51
/* Win32 icon mask semantics are different from those of SDL:
slouken@0
    52
     SDL applies the mask to the icon and copies result to desktop.
slouken@0
    53
     Win32 applies the mask to the desktop and XORs the icon on.
slouken@0
    54
   This means that the SDL mask needs to be applied to the icon and
slouken@0
    55
   then inverted and passed to Win32.
slouken@0
    56
*/
slouken@0
    57
void WIN_SetWMIcon(_THIS, SDL_Surface *icon, Uint8 *mask)
slouken@0
    58
{
slouken@0
    59
#ifdef DISABLE_ICON_SUPPORT
slouken@0
    60
	return;
slouken@0
    61
#else
slouken@0
    62
	SDL_Palette *pal_256;
slouken@0
    63
	SDL_Surface *icon_256;
slouken@0
    64
	Uint8 *pdata, *pwin32;
slouken@0
    65
	Uint8 *mdata, *mwin32, m = 0;
slouken@0
    66
	int icon_len;
slouken@0
    67
	int icon_plen;
slouken@0
    68
	int icon_mlen;
slouken@0
    69
	int icon_pitch;
slouken@0
    70
	int mask_pitch;
slouken@0
    71
	SDL_Rect bounds;
slouken@0
    72
	int i, skip;
slouken@0
    73
	int row, col;
slouken@0
    74
	struct /* quasi-BMP format */ Win32Icon {
slouken@0
    75
		Uint32 biSize;
slouken@0
    76
		Sint32 biWidth;
slouken@0
    77
		Sint32 biHeight;
slouken@0
    78
		Uint16 biPlanes;
slouken@0
    79
		Uint16 biBitCount;
slouken@0
    80
		Uint32 biCompression;
slouken@0
    81
		Uint32 biSizeImage;
slouken@0
    82
		Sint32 biXPelsPerMeter;
slouken@0
    83
		Sint32 biYPelsPerMeter;
slouken@0
    84
		Uint32 biClrUsed;
slouken@0
    85
		Uint32 biClrImportant;
slouken@0
    86
		struct /* RGBQUAD -- note it's BGR ordered */ {
slouken@0
    87
			Uint8 rgbBlue;
slouken@0
    88
			Uint8 rgbGreen;
slouken@0
    89
			Uint8 rgbRed;
slouken@0
    90
			Uint8 rgbReserved;
slouken@0
    91
		} biColors[256];
slouken@0
    92
		/* Pixels:
slouken@0
    93
		Uint8 pixels[]
slouken@0
    94
		*/
slouken@0
    95
		/* Mask:
slouken@0
    96
		Uint8 mask[]
slouken@0
    97
		*/
slouken@0
    98
	} *icon_win32;
slouken@0
    99
	
slouken@0
   100
	/* Allocate the win32 bmp icon and set everything to zero */
slouken@0
   101
	icon_pitch = ((icon->w+3)&~3);
slouken@0
   102
	mask_pitch = ((icon->w+7)/8);
slouken@0
   103
	icon_plen = icon->h*icon_pitch;
slouken@0
   104
	icon_mlen = icon->h*mask_pitch;
slouken@0
   105
	icon_len = sizeof(*icon_win32)+icon_plen+icon_mlen;
slouken@0
   106
	icon_win32 = (struct Win32Icon *)alloca(icon_len);
slouken@0
   107
	if ( icon_win32 == NULL ) {
slouken@0
   108
		return;
slouken@0
   109
	}
slouken@0
   110
	memset(icon_win32, 0, icon_len);
slouken@0
   111
slouken@0
   112
	/* Set the basic BMP parameters */
slouken@0
   113
	icon_win32->biSize = sizeof(*icon_win32)-sizeof(icon_win32->biColors);
slouken@0
   114
	icon_win32->biWidth = icon->w;
slouken@0
   115
	icon_win32->biHeight = icon->h*2;
slouken@0
   116
	icon_win32->biPlanes = 1;
slouken@0
   117
	icon_win32->biBitCount = 8;
slouken@0
   118
	icon_win32->biSizeImage = icon_plen+icon_mlen;
slouken@0
   119
slouken@0
   120
	/* Allocate a standard 256 color icon surface */
slouken@0
   121
	icon_256 = SDL_CreateRGBSurface(SDL_SWSURFACE, icon->w, icon->h,
slouken@0
   122
					 icon_win32->biBitCount, 0, 0, 0, 0);
slouken@0
   123
	if ( icon_256 == NULL ) {
slouken@0
   124
		return;
slouken@0
   125
	}
slouken@0
   126
	pal_256 = icon_256->format->palette;
slouken@0
   127
	if (icon->format->palette && 
slouken@0
   128
		(icon->format->BitsPerPixel == icon_256->format->BitsPerPixel)){
slouken@0
   129
		Uint8 black;
slouken@0
   130
		memcpy(pal_256->colors, icon->format->palette->colors,
slouken@0
   131
					pal_256->ncolors*sizeof(SDL_Color));
slouken@0
   132
		/* Make sure that 0 is black! */
slouken@0
   133
		black = SDL_FindColor(pal_256, 0x00, 0x00, 0x00);
slouken@0
   134
		pal_256->colors[black] = pal_256->colors[0];
slouken@0
   135
		pal_256->colors[0].r = 0x00;
slouken@0
   136
		pal_256->colors[0].g = 0x00;
slouken@0
   137
		pal_256->colors[0].b = 0x00;
slouken@0
   138
	} else {
slouken@0
   139
		SDL_DitherColors(pal_256->colors,
slouken@0
   140
					icon_256->format->BitsPerPixel);
slouken@0
   141
	}
slouken@0
   142
slouken@0
   143
	/* Now copy color data to the icon BMP */
slouken@0
   144
	for ( i=0; i<(1<<icon_win32->biBitCount); ++i ) {
slouken@0
   145
		icon_win32->biColors[i].rgbRed = pal_256->colors[i].r;
slouken@0
   146
		icon_win32->biColors[i].rgbGreen = pal_256->colors[i].g;
slouken@0
   147
		icon_win32->biColors[i].rgbBlue = pal_256->colors[i].b;
slouken@0
   148
	}
slouken@0
   149
slouken@0
   150
	/* Convert icon to a standard surface format.  This may not always
slouken@0
   151
	   be necessary, as Windows supports a variety of BMP formats, but
slouken@0
   152
	   it greatly simplifies our code.
slouken@0
   153
	*/ 
slouken@0
   154
        bounds.x = 0;
slouken@0
   155
        bounds.y = 0;
slouken@0
   156
        bounds.w = icon->w;
slouken@0
   157
        bounds.h = icon->h;
slouken@0
   158
        if ( SDL_LowerBlit(icon, &bounds, icon_256, &bounds) < 0 ) {
slouken@0
   159
		SDL_FreeSurface(icon_256);
slouken@0
   160
                return;
slouken@0
   161
	}
slouken@0
   162
slouken@0
   163
	/* Copy pixels upside-down to icon BMP, masked with the icon mask */
slouken@0
   164
	if ( SDL_MUSTLOCK(icon_256) || (icon_256->pitch != icon_pitch) ) {
slouken@0
   165
		SDL_FreeSurface(icon_256);
slouken@0
   166
		SDL_SetError("Warning: Unexpected icon_256 characteristics");
slouken@0
   167
		return;
slouken@0
   168
	}
slouken@0
   169
	pdata = (Uint8 *)icon_256->pixels;
slouken@0
   170
	mdata = mask;
slouken@0
   171
	pwin32 = (Uint8 *)icon_win32+sizeof(*icon_win32)+icon_plen-icon_pitch;
slouken@0
   172
	skip = icon_pitch - icon->w;
slouken@0
   173
	for ( row=0; row<icon->h; ++row ) {
slouken@0
   174
		for ( col=0; col<icon->w; ++col ) {
slouken@0
   175
			if ( (col%8) == 0 ) {
slouken@0
   176
				m = *mdata++;
slouken@0
   177
			}
slouken@0
   178
			if ( (m&0x80) != 0x00 ) {
slouken@0
   179
				*pwin32 = *pdata;
slouken@0
   180
			}
slouken@0
   181
			m <<= 1;
slouken@0
   182
			++pdata;
slouken@0
   183
			++pwin32;
slouken@0
   184
		}
slouken@0
   185
		pdata  += skip;
slouken@0
   186
		pwin32 += skip;
slouken@0
   187
		pwin32 -= 2*icon_pitch;
slouken@0
   188
	}
slouken@0
   189
	SDL_FreeSurface(icon_256);
slouken@0
   190
slouken@0
   191
	/* Copy mask inverted and upside-down to icon BMP */
slouken@0
   192
	mdata = mask;
slouken@0
   193
	mwin32 = (Uint8 *)icon_win32
slouken@0
   194
			+sizeof(*icon_win32)+icon_plen+icon_mlen-mask_pitch;
slouken@0
   195
	for ( row=0; row<icon->h; ++row ) {
slouken@0
   196
		for ( col=0; col<mask_pitch; ++col ) {
slouken@0
   197
			*mwin32++ = ~*mdata++;
slouken@0
   198
		}
slouken@0
   199
		mwin32 -= 2*mask_pitch;
slouken@0
   200
	}
slouken@0
   201
slouken@0
   202
	/* Finally, create the icon handle and set the window icon */
slouken@0
   203
	screen_icn = CreateIconFromResourceEx((Uint8 *)icon_win32, icon_len,
slouken@0
   204
			TRUE, 0x00030000, icon->w, icon->h, LR_DEFAULTCOLOR);
slouken@0
   205
	if ( screen_icn == NULL ) {
slouken@0
   206
		SDL_SetError("Couldn't create Win32 icon handle");
slouken@0
   207
	} else {
slouken@0
   208
		SetClassLong(SDL_Window, GCL_HICON, (LONG)screen_icn);
slouken@0
   209
	}
slouken@0
   210
#endif /* DISABLE_ICON_SUPPORT */
slouken@0
   211
}
slouken@0
   212
slouken@0
   213
void WIN_SetWMCaption(_THIS, const char *title, const char *icon)
slouken@0
   214
{
slouken@0
   215
#ifdef _WIN32_WCE
slouken@0
   216
	/* WinCE uses the UNICODE version */
slouken@36
   217
	int nLen = strlen(title)+1;
slouken@36
   218
	LPWSTR lpszW = alloca(nLen*2);
slouken@0
   219
	MultiByteToWideChar(CP_ACP, 0, title, -1, lpszW, nLen);
slouken@0
   220
	SetWindowText(SDL_Window, lpszW);
slouken@0
   221
#else
slouken@0
   222
	SetWindowText(SDL_Window, title);
slouken@0
   223
#endif
slouken@0
   224
}
slouken@0
   225
slouken@0
   226
int WIN_IconifyWindow(_THIS)
slouken@0
   227
{
slouken@0
   228
	ShowWindow(SDL_Window, SW_MINIMIZE);
slouken@0
   229
	return(1);
slouken@0
   230
}
slouken@0
   231
slouken@0
   232
SDL_GrabMode WIN_GrabInput(_THIS, SDL_GrabMode mode)
slouken@0
   233
{
slouken@0
   234
	if ( mode == SDL_GRAB_OFF ) {
slouken@0
   235
		ClipCursor(NULL);
slouken@0
   236
		if ( !(SDL_cursorstate & CURSOR_VISIBLE) ) {
slouken@0
   237
		/*	RJR: March 28, 2000
slouken@0
   238
			must be leaving relative mode, move mouse from
slouken@0
   239
			center of window to where it belongs ... */
slouken@0
   240
			POINT pt;
slouken@0
   241
			int x, y;
slouken@0
   242
			SDL_GetMouseState(&x,&y);
slouken@0
   243
			pt.x = x;
slouken@0
   244
			pt.y = y;
slouken@0
   245
			ClientToScreen(SDL_Window, &pt);
slouken@0
   246
			SetCursorPos(pt.x,pt.y);
slouken@0
   247
		}
slouken@0
   248
	} else {
slouken@0
   249
		ClipCursor(&SDL_bounds);
slouken@0
   250
		if ( !(SDL_cursorstate & CURSOR_VISIBLE) ) {
slouken@0
   251
		/*	RJR: March 28, 2000
slouken@0
   252
			must be entering relative mode, get ready by
slouken@0
   253
			moving mouse to	center of window ... */
slouken@0
   254
			POINT pt;
slouken@0
   255
			pt.x = (SDL_VideoSurface->w/2);
slouken@0
   256
			pt.y = (SDL_VideoSurface->h/2);
slouken@0
   257
			ClientToScreen(SDL_Window, &pt);
slouken@0
   258
			SetCursorPos(pt.x, pt.y);
slouken@0
   259
		}
slouken@0
   260
	}
slouken@0
   261
	return(mode);
slouken@0
   262
}
slouken@0
   263
slouken@0
   264
/* If 'info' is the right version, this function fills it and returns 1.
slouken@0
   265
   Otherwise, in case of a version mismatch, it returns -1.
slouken@0
   266
*/
slouken@0
   267
int WIN_GetWMInfo(_THIS, SDL_SysWMinfo *info)
slouken@0
   268
{
slouken@0
   269
	if ( info->version.major <= SDL_MAJOR_VERSION ) {
slouken@0
   270
		info->window = SDL_Window;
slouken@438
   271
		if ( SDL_VERSIONNUM(info->version.major,
slouken@438
   272
		                    info->version.minor,
slouken@438
   273
		                    info->version.patch) >=
slouken@439
   274
		     SDL_VERSIONNUM(1, 2, 5) ) {
slouken@438
   275
#ifdef HAVE_OPENGL
slouken@438
   276
			info->hglrc = GL_hrc;
slouken@438
   277
#else
slouken@438
   278
			info->hglrc = NULL;
slouken@438
   279
#endif
slouken@438
   280
		}
slouken@0
   281
		return(1);
slouken@0
   282
	} else {
slouken@0
   283
		SDL_SetError("Application not compiled with SDL %d.%d\n",
slouken@0
   284
					SDL_MAJOR_VERSION, SDL_MINOR_VERSION);
slouken@0
   285
		return(-1);
slouken@0
   286
	}
slouken@0
   287
}