src/video/wincommon/SDL_syswm.c
author Sam Lantinga <slouken@libsdl.org>
Fri, 14 Dec 2001 12:38:15 +0000
changeset 252 e8157fcb3114
parent 36 13ee9f4834ea
child 438 f9c5f135a8b9
permissions -rw-r--r--
Updated the source with the correct e-mail address
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@0
    37
#include "SDL_pixels_c.h"
slouken@0
    38
slouken@0
    39
#ifdef _WIN32_WCE
slouken@0
    40
#define DISABLE_ICON_SUPPORT
slouken@0
    41
#endif
slouken@0
    42
slouken@0
    43
/*	RJR: March 28, 2000
slouken@0
    44
	we need "SDL_cursor_c.h" for mods to WIN_GrabInput */
slouken@0
    45
#include "SDL_cursor_c.h"
slouken@0
    46
slouken@0
    47
/* The screen icon -- needs to be freed on SDL_VideoQuit() */
slouken@0
    48
HICON   screen_icn = NULL;
slouken@0
    49
slouken@0
    50
/* Win32 icon mask semantics are different from those of SDL:
slouken@0
    51
     SDL applies the mask to the icon and copies result to desktop.
slouken@0
    52
     Win32 applies the mask to the desktop and XORs the icon on.
slouken@0
    53
   This means that the SDL mask needs to be applied to the icon and
slouken@0
    54
   then inverted and passed to Win32.
slouken@0
    55
*/
slouken@0
    56
void WIN_SetWMIcon(_THIS, SDL_Surface *icon, Uint8 *mask)
slouken@0
    57
{
slouken@0
    58
#ifdef DISABLE_ICON_SUPPORT
slouken@0
    59
	return;
slouken@0
    60
#else
slouken@0
    61
	SDL_Palette *pal_256;
slouken@0
    62
	SDL_Surface *icon_256;
slouken@0
    63
	Uint8 *pdata, *pwin32;
slouken@0
    64
	Uint8 *mdata, *mwin32, m = 0;
slouken@0
    65
	int icon_len;
slouken@0
    66
	int icon_plen;
slouken@0
    67
	int icon_mlen;
slouken@0
    68
	int icon_pitch;
slouken@0
    69
	int mask_pitch;
slouken@0
    70
	SDL_Rect bounds;
slouken@0
    71
	int i, skip;
slouken@0
    72
	int row, col;
slouken@0
    73
	struct /* quasi-BMP format */ Win32Icon {
slouken@0
    74
		Uint32 biSize;
slouken@0
    75
		Sint32 biWidth;
slouken@0
    76
		Sint32 biHeight;
slouken@0
    77
		Uint16 biPlanes;
slouken@0
    78
		Uint16 biBitCount;
slouken@0
    79
		Uint32 biCompression;
slouken@0
    80
		Uint32 biSizeImage;
slouken@0
    81
		Sint32 biXPelsPerMeter;
slouken@0
    82
		Sint32 biYPelsPerMeter;
slouken@0
    83
		Uint32 biClrUsed;
slouken@0
    84
		Uint32 biClrImportant;
slouken@0
    85
		struct /* RGBQUAD -- note it's BGR ordered */ {
slouken@0
    86
			Uint8 rgbBlue;
slouken@0
    87
			Uint8 rgbGreen;
slouken@0
    88
			Uint8 rgbRed;
slouken@0
    89
			Uint8 rgbReserved;
slouken@0
    90
		} biColors[256];
slouken@0
    91
		/* Pixels:
slouken@0
    92
		Uint8 pixels[]
slouken@0
    93
		*/
slouken@0
    94
		/* Mask:
slouken@0
    95
		Uint8 mask[]
slouken@0
    96
		*/
slouken@0
    97
	} *icon_win32;
slouken@0
    98
	
slouken@0
    99
	/* Allocate the win32 bmp icon and set everything to zero */
slouken@0
   100
	icon_pitch = ((icon->w+3)&~3);
slouken@0
   101
	mask_pitch = ((icon->w+7)/8);
slouken@0
   102
	icon_plen = icon->h*icon_pitch;
slouken@0
   103
	icon_mlen = icon->h*mask_pitch;
slouken@0
   104
	icon_len = sizeof(*icon_win32)+icon_plen+icon_mlen;
slouken@0
   105
	icon_win32 = (struct Win32Icon *)alloca(icon_len);
slouken@0
   106
	if ( icon_win32 == NULL ) {
slouken@0
   107
		return;
slouken@0
   108
	}
slouken@0
   109
	memset(icon_win32, 0, icon_len);
slouken@0
   110
slouken@0
   111
	/* Set the basic BMP parameters */
slouken@0
   112
	icon_win32->biSize = sizeof(*icon_win32)-sizeof(icon_win32->biColors);
slouken@0
   113
	icon_win32->biWidth = icon->w;
slouken@0
   114
	icon_win32->biHeight = icon->h*2;
slouken@0
   115
	icon_win32->biPlanes = 1;
slouken@0
   116
	icon_win32->biBitCount = 8;
slouken@0
   117
	icon_win32->biSizeImage = icon_plen+icon_mlen;
slouken@0
   118
slouken@0
   119
	/* Allocate a standard 256 color icon surface */
slouken@0
   120
	icon_256 = SDL_CreateRGBSurface(SDL_SWSURFACE, icon->w, icon->h,
slouken@0
   121
					 icon_win32->biBitCount, 0, 0, 0, 0);
slouken@0
   122
	if ( icon_256 == NULL ) {
slouken@0
   123
		return;
slouken@0
   124
	}
slouken@0
   125
	pal_256 = icon_256->format->palette;
slouken@0
   126
	if (icon->format->palette && 
slouken@0
   127
		(icon->format->BitsPerPixel == icon_256->format->BitsPerPixel)){
slouken@0
   128
		Uint8 black;
slouken@0
   129
		memcpy(pal_256->colors, icon->format->palette->colors,
slouken@0
   130
					pal_256->ncolors*sizeof(SDL_Color));
slouken@0
   131
		/* Make sure that 0 is black! */
slouken@0
   132
		black = SDL_FindColor(pal_256, 0x00, 0x00, 0x00);
slouken@0
   133
		pal_256->colors[black] = pal_256->colors[0];
slouken@0
   134
		pal_256->colors[0].r = 0x00;
slouken@0
   135
		pal_256->colors[0].g = 0x00;
slouken@0
   136
		pal_256->colors[0].b = 0x00;
slouken@0
   137
	} else {
slouken@0
   138
		SDL_DitherColors(pal_256->colors,
slouken@0
   139
					icon_256->format->BitsPerPixel);
slouken@0
   140
	}
slouken@0
   141
slouken@0
   142
	/* Now copy color data to the icon BMP */
slouken@0
   143
	for ( i=0; i<(1<<icon_win32->biBitCount); ++i ) {
slouken@0
   144
		icon_win32->biColors[i].rgbRed = pal_256->colors[i].r;
slouken@0
   145
		icon_win32->biColors[i].rgbGreen = pal_256->colors[i].g;
slouken@0
   146
		icon_win32->biColors[i].rgbBlue = pal_256->colors[i].b;
slouken@0
   147
	}
slouken@0
   148
slouken@0
   149
	/* Convert icon to a standard surface format.  This may not always
slouken@0
   150
	   be necessary, as Windows supports a variety of BMP formats, but
slouken@0
   151
	   it greatly simplifies our code.
slouken@0
   152
	*/ 
slouken@0
   153
        bounds.x = 0;
slouken@0
   154
        bounds.y = 0;
slouken@0
   155
        bounds.w = icon->w;
slouken@0
   156
        bounds.h = icon->h;
slouken@0
   157
        if ( SDL_LowerBlit(icon, &bounds, icon_256, &bounds) < 0 ) {
slouken@0
   158
		SDL_FreeSurface(icon_256);
slouken@0
   159
                return;
slouken@0
   160
	}
slouken@0
   161
slouken@0
   162
	/* Copy pixels upside-down to icon BMP, masked with the icon mask */
slouken@0
   163
	if ( SDL_MUSTLOCK(icon_256) || (icon_256->pitch != icon_pitch) ) {
slouken@0
   164
		SDL_FreeSurface(icon_256);
slouken@0
   165
		SDL_SetError("Warning: Unexpected icon_256 characteristics");
slouken@0
   166
		return;
slouken@0
   167
	}
slouken@0
   168
	pdata = (Uint8 *)icon_256->pixels;
slouken@0
   169
	mdata = mask;
slouken@0
   170
	pwin32 = (Uint8 *)icon_win32+sizeof(*icon_win32)+icon_plen-icon_pitch;
slouken@0
   171
	skip = icon_pitch - icon->w;
slouken@0
   172
	for ( row=0; row<icon->h; ++row ) {
slouken@0
   173
		for ( col=0; col<icon->w; ++col ) {
slouken@0
   174
			if ( (col%8) == 0 ) {
slouken@0
   175
				m = *mdata++;
slouken@0
   176
			}
slouken@0
   177
			if ( (m&0x80) != 0x00 ) {
slouken@0
   178
				*pwin32 = *pdata;
slouken@0
   179
			}
slouken@0
   180
			m <<= 1;
slouken@0
   181
			++pdata;
slouken@0
   182
			++pwin32;
slouken@0
   183
		}
slouken@0
   184
		pdata  += skip;
slouken@0
   185
		pwin32 += skip;
slouken@0
   186
		pwin32 -= 2*icon_pitch;
slouken@0
   187
	}
slouken@0
   188
	SDL_FreeSurface(icon_256);
slouken@0
   189
slouken@0
   190
	/* Copy mask inverted and upside-down to icon BMP */
slouken@0
   191
	mdata = mask;
slouken@0
   192
	mwin32 = (Uint8 *)icon_win32
slouken@0
   193
			+sizeof(*icon_win32)+icon_plen+icon_mlen-mask_pitch;
slouken@0
   194
	for ( row=0; row<icon->h; ++row ) {
slouken@0
   195
		for ( col=0; col<mask_pitch; ++col ) {
slouken@0
   196
			*mwin32++ = ~*mdata++;
slouken@0
   197
		}
slouken@0
   198
		mwin32 -= 2*mask_pitch;
slouken@0
   199
	}
slouken@0
   200
slouken@0
   201
	/* Finally, create the icon handle and set the window icon */
slouken@0
   202
	screen_icn = CreateIconFromResourceEx((Uint8 *)icon_win32, icon_len,
slouken@0
   203
			TRUE, 0x00030000, icon->w, icon->h, LR_DEFAULTCOLOR);
slouken@0
   204
	if ( screen_icn == NULL ) {
slouken@0
   205
		SDL_SetError("Couldn't create Win32 icon handle");
slouken@0
   206
	} else {
slouken@0
   207
		SetClassLong(SDL_Window, GCL_HICON, (LONG)screen_icn);
slouken@0
   208
	}
slouken@0
   209
#endif /* DISABLE_ICON_SUPPORT */
slouken@0
   210
}
slouken@0
   211
slouken@0
   212
void WIN_SetWMCaption(_THIS, const char *title, const char *icon)
slouken@0
   213
{
slouken@0
   214
#ifdef _WIN32_WCE
slouken@0
   215
	/* WinCE uses the UNICODE version */
slouken@36
   216
	int nLen = strlen(title)+1;
slouken@36
   217
	LPWSTR lpszW = alloca(nLen*2);
slouken@0
   218
	MultiByteToWideChar(CP_ACP, 0, title, -1, lpszW, nLen);
slouken@0
   219
	SetWindowText(SDL_Window, lpszW);
slouken@0
   220
#else
slouken@0
   221
	SetWindowText(SDL_Window, title);
slouken@0
   222
#endif
slouken@0
   223
}
slouken@0
   224
slouken@0
   225
int WIN_IconifyWindow(_THIS)
slouken@0
   226
{
slouken@0
   227
	ShowWindow(SDL_Window, SW_MINIMIZE);
slouken@0
   228
	return(1);
slouken@0
   229
}
slouken@0
   230
slouken@0
   231
SDL_GrabMode WIN_GrabInput(_THIS, SDL_GrabMode mode)
slouken@0
   232
{
slouken@0
   233
	if ( mode == SDL_GRAB_OFF ) {
slouken@0
   234
		ClipCursor(NULL);
slouken@0
   235
		if ( !(SDL_cursorstate & CURSOR_VISIBLE) ) {
slouken@0
   236
		/*	RJR: March 28, 2000
slouken@0
   237
			must be leaving relative mode, move mouse from
slouken@0
   238
			center of window to where it belongs ... */
slouken@0
   239
			POINT pt;
slouken@0
   240
			int x, y;
slouken@0
   241
			SDL_GetMouseState(&x,&y);
slouken@0
   242
			pt.x = x;
slouken@0
   243
			pt.y = y;
slouken@0
   244
			ClientToScreen(SDL_Window, &pt);
slouken@0
   245
			SetCursorPos(pt.x,pt.y);
slouken@0
   246
		}
slouken@0
   247
	} else {
slouken@0
   248
		ClipCursor(&SDL_bounds);
slouken@0
   249
		if ( !(SDL_cursorstate & CURSOR_VISIBLE) ) {
slouken@0
   250
		/*	RJR: March 28, 2000
slouken@0
   251
			must be entering relative mode, get ready by
slouken@0
   252
			moving mouse to	center of window ... */
slouken@0
   253
			POINT pt;
slouken@0
   254
			pt.x = (SDL_VideoSurface->w/2);
slouken@0
   255
			pt.y = (SDL_VideoSurface->h/2);
slouken@0
   256
			ClientToScreen(SDL_Window, &pt);
slouken@0
   257
			SetCursorPos(pt.x, pt.y);
slouken@0
   258
		}
slouken@0
   259
	}
slouken@0
   260
	return(mode);
slouken@0
   261
}
slouken@0
   262
slouken@0
   263
/* If 'info' is the right version, this function fills it and returns 1.
slouken@0
   264
   Otherwise, in case of a version mismatch, it returns -1.
slouken@0
   265
*/
slouken@0
   266
int WIN_GetWMInfo(_THIS, SDL_SysWMinfo *info)
slouken@0
   267
{
slouken@0
   268
	if ( info->version.major <= SDL_MAJOR_VERSION ) {
slouken@0
   269
		info->window = SDL_Window;
slouken@0
   270
		return(1);
slouken@0
   271
	} else {
slouken@0
   272
		SDL_SetError("Application not compiled with SDL %d.%d\n",
slouken@0
   273
					SDL_MAJOR_VERSION, SDL_MINOR_VERSION);
slouken@0
   274
		return(-1);
slouken@0
   275
	}
slouken@0
   276
}