src/video/wincommon/SDL_syswm.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 13 Mar 2006 01:33:58 +0000
changeset 1505 4d005dfbb7f5
parent 1481 deb22b9fe970
child 1510 720f8bb49d7d
permissions -rw-r--r--
Fixed bug #139
The text in SDL_WM_SetCaption() is in UTF-8 encoding.
slouken@0
     1
/*
slouken@0
     2
    SDL - Simple DirectMedia Layer
slouken@1312
     3
    Copyright (C) 1997-2006 Sam Lantinga
slouken@0
     4
slouken@0
     5
    This library is free software; you can redistribute it and/or
slouken@1312
     6
    modify it under the terms of the GNU Lesser General Public
slouken@0
     7
    License as published by the Free Software Foundation; either
slouken@1312
     8
    version 2.1 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@1312
    13
    Lesser General Public License for more details.
slouken@0
    14
slouken@1312
    15
    You should have received a copy of the GNU Lesser General Public
slouken@1312
    16
    License along with this library; if not, write to the Free Software
slouken@1312
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
slouken@0
    18
slouken@0
    19
    Sam Lantinga
slouken@252
    20
    slouken@libsdl.org
slouken@0
    21
*/
slouken@1402
    22
#include "SDL_config.h"
slouken@0
    23
slouken@1433
    24
#define WIN32_LEAN_AND_MEAN
slouken@1433
    25
#include <windows.h>
slouken@0
    26
slouken@0
    27
#include "SDL_version.h"
slouken@0
    28
#include "SDL_video.h"
slouken@1361
    29
#include "SDL_loadso.h"
slouken@0
    30
#include "SDL_syswm.h"
slouken@1361
    31
#include "../SDL_pixels_c.h"
slouken@1361
    32
#include "../SDL_cursor_c.h"
slouken@0
    33
#include "SDL_syswm_c.h"
slouken@439
    34
#include "SDL_wingl_c.h"
icculus@1152
    35
slouken@0
    36
slouken@0
    37
#ifdef _WIN32_WCE
slouken@0
    38
#define DISABLE_ICON_SUPPORT
slouken@0
    39
#endif
slouken@0
    40
slouken@0
    41
/* The screen icon -- needs to be freed on SDL_VideoQuit() */
slouken@0
    42
HICON   screen_icn = NULL;
slouken@0
    43
icculus@1152
    44
#ifdef _WIN32_WCE
icculus@1152
    45
icculus@1152
    46
BOOL (WINAPI *CoreCatchInput)(int flag) = NULL;
icculus@1152
    47
int input_catched = 0;
icculus@1152
    48
HINSTANCE coredll = NULL;
icculus@1152
    49
icculus@1152
    50
// the same API call that gx.dll does to catch the input
icculus@1152
    51
void LoadInputCatchFunc()
icculus@1152
    52
{
icculus@1152
    53
	coredll = SDL_LoadObject("coredll.dll");
icculus@1152
    54
	if( coredll )
icculus@1152
    55
	{
icculus@1152
    56
		CoreCatchInput = (int (WINAPI *)(int)) GetProcAddress(coredll, (const unsigned short *) 1453);
icculus@1152
    57
	}
icculus@1152
    58
}
icculus@1152
    59
icculus@1152
    60
#endif
icculus@1152
    61
icculus@1152
    62
slouken@0
    63
/* Win32 icon mask semantics are different from those of SDL:
slouken@0
    64
     SDL applies the mask to the icon and copies result to desktop.
slouken@0
    65
     Win32 applies the mask to the desktop and XORs the icon on.
slouken@0
    66
   This means that the SDL mask needs to be applied to the icon and
slouken@0
    67
   then inverted and passed to Win32.
slouken@0
    68
*/
slouken@0
    69
void WIN_SetWMIcon(_THIS, SDL_Surface *icon, Uint8 *mask)
slouken@0
    70
{
slouken@0
    71
#ifdef DISABLE_ICON_SUPPORT
slouken@0
    72
	return;
slouken@0
    73
#else
slouken@0
    74
	SDL_Palette *pal_256;
slouken@0
    75
	SDL_Surface *icon_256;
slouken@0
    76
	Uint8 *pdata, *pwin32;
slouken@0
    77
	Uint8 *mdata, *mwin32, m = 0;
slouken@0
    78
	int icon_len;
slouken@0
    79
	int icon_plen;
slouken@0
    80
	int icon_mlen;
slouken@0
    81
	int icon_pitch;
slouken@0
    82
	int mask_pitch;
slouken@0
    83
	SDL_Rect bounds;
slouken@0
    84
	int i, skip;
slouken@0
    85
	int row, col;
slouken@0
    86
	struct /* quasi-BMP format */ Win32Icon {
slouken@0
    87
		Uint32 biSize;
slouken@0
    88
		Sint32 biWidth;
slouken@0
    89
		Sint32 biHeight;
slouken@0
    90
		Uint16 biPlanes;
slouken@0
    91
		Uint16 biBitCount;
slouken@0
    92
		Uint32 biCompression;
slouken@0
    93
		Uint32 biSizeImage;
slouken@0
    94
		Sint32 biXPelsPerMeter;
slouken@0
    95
		Sint32 biYPelsPerMeter;
slouken@0
    96
		Uint32 biClrUsed;
slouken@0
    97
		Uint32 biClrImportant;
slouken@0
    98
		struct /* RGBQUAD -- note it's BGR ordered */ {
slouken@0
    99
			Uint8 rgbBlue;
slouken@0
   100
			Uint8 rgbGreen;
slouken@0
   101
			Uint8 rgbRed;
slouken@0
   102
			Uint8 rgbReserved;
slouken@0
   103
		} biColors[256];
slouken@0
   104
		/* Pixels:
slouken@0
   105
		Uint8 pixels[]
slouken@0
   106
		*/
slouken@0
   107
		/* Mask:
slouken@0
   108
		Uint8 mask[]
slouken@0
   109
		*/
slouken@0
   110
	} *icon_win32;
slouken@0
   111
	
slouken@0
   112
	/* Allocate the win32 bmp icon and set everything to zero */
slouken@0
   113
	icon_pitch = ((icon->w+3)&~3);
slouken@0
   114
	mask_pitch = ((icon->w+7)/8);
slouken@0
   115
	icon_plen = icon->h*icon_pitch;
slouken@0
   116
	icon_mlen = icon->h*mask_pitch;
slouken@0
   117
	icon_len = sizeof(*icon_win32)+icon_plen+icon_mlen;
slouken@1330
   118
	icon_win32 = (struct Win32Icon *)SDL_stack_alloc(Uint8, icon_len);
slouken@0
   119
	if ( icon_win32 == NULL ) {
slouken@0
   120
		return;
slouken@0
   121
	}
slouken@1336
   122
	SDL_memset(icon_win32, 0, icon_len);
slouken@0
   123
slouken@0
   124
	/* Set the basic BMP parameters */
slouken@0
   125
	icon_win32->biSize = sizeof(*icon_win32)-sizeof(icon_win32->biColors);
slouken@0
   126
	icon_win32->biWidth = icon->w;
slouken@0
   127
	icon_win32->biHeight = icon->h*2;
slouken@0
   128
	icon_win32->biPlanes = 1;
slouken@0
   129
	icon_win32->biBitCount = 8;
slouken@0
   130
	icon_win32->biSizeImage = icon_plen+icon_mlen;
slouken@0
   131
slouken@0
   132
	/* Allocate a standard 256 color icon surface */
slouken@0
   133
	icon_256 = SDL_CreateRGBSurface(SDL_SWSURFACE, icon->w, icon->h,
slouken@0
   134
					 icon_win32->biBitCount, 0, 0, 0, 0);
slouken@0
   135
	if ( icon_256 == NULL ) {
slouken@1330
   136
		SDL_stack_free(icon_win32);
slouken@0
   137
		return;
slouken@0
   138
	}
slouken@0
   139
	pal_256 = icon_256->format->palette;
slouken@0
   140
	if (icon->format->palette && 
slouken@0
   141
		(icon->format->BitsPerPixel == icon_256->format->BitsPerPixel)){
slouken@0
   142
		Uint8 black;
slouken@1336
   143
		SDL_memcpy(pal_256->colors, icon->format->palette->colors,
slouken@0
   144
					pal_256->ncolors*sizeof(SDL_Color));
slouken@0
   145
		/* Make sure that 0 is black! */
slouken@0
   146
		black = SDL_FindColor(pal_256, 0x00, 0x00, 0x00);
slouken@0
   147
		pal_256->colors[black] = pal_256->colors[0];
slouken@0
   148
		pal_256->colors[0].r = 0x00;
slouken@0
   149
		pal_256->colors[0].g = 0x00;
slouken@0
   150
		pal_256->colors[0].b = 0x00;
slouken@0
   151
	} else {
slouken@0
   152
		SDL_DitherColors(pal_256->colors,
slouken@0
   153
					icon_256->format->BitsPerPixel);
slouken@0
   154
	}
slouken@0
   155
slouken@0
   156
	/* Now copy color data to the icon BMP */
slouken@0
   157
	for ( i=0; i<(1<<icon_win32->biBitCount); ++i ) {
slouken@0
   158
		icon_win32->biColors[i].rgbRed = pal_256->colors[i].r;
slouken@0
   159
		icon_win32->biColors[i].rgbGreen = pal_256->colors[i].g;
slouken@0
   160
		icon_win32->biColors[i].rgbBlue = pal_256->colors[i].b;
slouken@0
   161
	}
slouken@0
   162
slouken@0
   163
	/* Convert icon to a standard surface format.  This may not always
slouken@0
   164
	   be necessary, as Windows supports a variety of BMP formats, but
slouken@0
   165
	   it greatly simplifies our code.
slouken@0
   166
	*/ 
slouken@1330
   167
    bounds.x = 0;
slouken@1330
   168
    bounds.y = 0;
slouken@1330
   169
    bounds.w = icon->w;
slouken@1330
   170
    bounds.h = icon->h;
slouken@1330
   171
    if ( SDL_LowerBlit(icon, &bounds, icon_256, &bounds) < 0 ) {
slouken@1330
   172
	    SDL_stack_free(icon_win32);
slouken@0
   173
		SDL_FreeSurface(icon_256);
slouken@1330
   174
        return;
slouken@0
   175
	}
slouken@0
   176
slouken@0
   177
	/* Copy pixels upside-down to icon BMP, masked with the icon mask */
slouken@0
   178
	if ( SDL_MUSTLOCK(icon_256) || (icon_256->pitch != icon_pitch) ) {
slouken@1330
   179
		SDL_stack_free(icon_win32);
slouken@0
   180
		SDL_FreeSurface(icon_256);
slouken@0
   181
		SDL_SetError("Warning: Unexpected icon_256 characteristics");
slouken@0
   182
		return;
slouken@0
   183
	}
slouken@0
   184
	pdata = (Uint8 *)icon_256->pixels;
slouken@0
   185
	mdata = mask;
slouken@0
   186
	pwin32 = (Uint8 *)icon_win32+sizeof(*icon_win32)+icon_plen-icon_pitch;
slouken@0
   187
	skip = icon_pitch - icon->w;
slouken@0
   188
	for ( row=0; row<icon->h; ++row ) {
slouken@0
   189
		for ( col=0; col<icon->w; ++col ) {
slouken@0
   190
			if ( (col%8) == 0 ) {
slouken@0
   191
				m = *mdata++;
slouken@0
   192
			}
slouken@0
   193
			if ( (m&0x80) != 0x00 ) {
slouken@0
   194
				*pwin32 = *pdata;
slouken@0
   195
			}
slouken@0
   196
			m <<= 1;
slouken@0
   197
			++pdata;
slouken@0
   198
			++pwin32;
slouken@0
   199
		}
slouken@0
   200
		pdata  += skip;
slouken@0
   201
		pwin32 += skip;
slouken@0
   202
		pwin32 -= 2*icon_pitch;
slouken@0
   203
	}
slouken@0
   204
	SDL_FreeSurface(icon_256);
slouken@0
   205
slouken@0
   206
	/* Copy mask inverted and upside-down to icon BMP */
slouken@0
   207
	mdata = mask;
slouken@0
   208
	mwin32 = (Uint8 *)icon_win32
slouken@0
   209
			+sizeof(*icon_win32)+icon_plen+icon_mlen-mask_pitch;
slouken@0
   210
	for ( row=0; row<icon->h; ++row ) {
slouken@0
   211
		for ( col=0; col<mask_pitch; ++col ) {
slouken@0
   212
			*mwin32++ = ~*mdata++;
slouken@0
   213
		}
slouken@0
   214
		mwin32 -= 2*mask_pitch;
slouken@0
   215
	}
slouken@0
   216
slouken@0
   217
	/* Finally, create the icon handle and set the window icon */
slouken@0
   218
	screen_icn = CreateIconFromResourceEx((Uint8 *)icon_win32, icon_len,
slouken@0
   219
			TRUE, 0x00030000, icon->w, icon->h, LR_DEFAULTCOLOR);
slouken@0
   220
	if ( screen_icn == NULL ) {
slouken@0
   221
		SDL_SetError("Couldn't create Win32 icon handle");
slouken@0
   222
	} else {
slouken@1481
   223
		SetClassLongPtr(SDL_Window, GCLP_HICON, (LONG_PTR)screen_icn);
slouken@0
   224
	}
slouken@1330
   225
	SDL_stack_free(icon_win32);
slouken@0
   226
#endif /* DISABLE_ICON_SUPPORT */
slouken@0
   227
}
slouken@0
   228
slouken@0
   229
void WIN_SetWMCaption(_THIS, const char *title, const char *icon)
slouken@0
   230
{
slouken@0
   231
#ifdef _WIN32_WCE
slouken@0
   232
	/* WinCE uses the UNICODE version */
slouken@1505
   233
	LPWSTR lpszW = SDL_iconv_utf8_ucs2(title);
slouken@0
   234
	SetWindowText(SDL_Window, lpszW);
slouken@1505
   235
	SDL_free(lpszW);
slouken@0
   236
#else
slouken@1505
   237
	char *lpsz = SDL_iconv_utf8_latin1(title);
slouken@1505
   238
	SetWindowText(SDL_Window, lpsz);
slouken@1505
   239
	SDL_free(lpsz);
slouken@0
   240
#endif
slouken@0
   241
}
slouken@0
   242
slouken@0
   243
int WIN_IconifyWindow(_THIS)
slouken@0
   244
{
slouken@0
   245
	ShowWindow(SDL_Window, SW_MINIMIZE);
slouken@0
   246
	return(1);
slouken@0
   247
}
slouken@0
   248
slouken@0
   249
SDL_GrabMode WIN_GrabInput(_THIS, SDL_GrabMode mode)
slouken@0
   250
{
slouken@0
   251
	if ( mode == SDL_GRAB_OFF ) {
slouken@0
   252
		ClipCursor(NULL);
slouken@0
   253
		if ( !(SDL_cursorstate & CURSOR_VISIBLE) ) {
slouken@0
   254
		/*	RJR: March 28, 2000
slouken@0
   255
			must be leaving relative mode, move mouse from
slouken@0
   256
			center of window to where it belongs ... */
slouken@0
   257
			POINT pt;
slouken@0
   258
			int x, y;
slouken@0
   259
			SDL_GetMouseState(&x,&y);
slouken@0
   260
			pt.x = x;
slouken@0
   261
			pt.y = y;
slouken@0
   262
			ClientToScreen(SDL_Window, &pt);
slouken@0
   263
			SetCursorPos(pt.x,pt.y);
slouken@0
   264
		}
icculus@1152
   265
#ifdef _WIN32_WCE
icculus@1152
   266
		if( input_catched )
icculus@1152
   267
		{
icculus@1152
   268
			if( !CoreCatchInput ) LoadInputCatchFunc();
icculus@1152
   269
icculus@1152
   270
			if( CoreCatchInput )
icculus@1152
   271
				CoreCatchInput(0);
icculus@1152
   272
		}
icculus@1152
   273
#endif
slouken@0
   274
	} else {
slouken@0
   275
		ClipCursor(&SDL_bounds);
slouken@0
   276
		if ( !(SDL_cursorstate & CURSOR_VISIBLE) ) {
slouken@0
   277
		/*	RJR: March 28, 2000
slouken@0
   278
			must be entering relative mode, get ready by
slouken@0
   279
			moving mouse to	center of window ... */
slouken@0
   280
			POINT pt;
slouken@0
   281
			pt.x = (SDL_VideoSurface->w/2);
slouken@0
   282
			pt.y = (SDL_VideoSurface->h/2);
slouken@0
   283
			ClientToScreen(SDL_Window, &pt);
slouken@0
   284
			SetCursorPos(pt.x, pt.y);
slouken@0
   285
		}
icculus@1152
   286
#ifdef _WIN32_WCE
icculus@1152
   287
		if( !input_catched )
icculus@1152
   288
		{
icculus@1152
   289
			if( !CoreCatchInput ) LoadInputCatchFunc();
icculus@1152
   290
icculus@1152
   291
			if( CoreCatchInput )
icculus@1152
   292
				CoreCatchInput(1);
icculus@1152
   293
		}
icculus@1152
   294
#endif
slouken@0
   295
	}
slouken@0
   296
	return(mode);
slouken@0
   297
}
slouken@0
   298
slouken@0
   299
/* If 'info' is the right version, this function fills it and returns 1.
slouken@0
   300
   Otherwise, in case of a version mismatch, it returns -1.
slouken@0
   301
*/
slouken@0
   302
int WIN_GetWMInfo(_THIS, SDL_SysWMinfo *info)
slouken@0
   303
{
slouken@0
   304
	if ( info->version.major <= SDL_MAJOR_VERSION ) {
slouken@0
   305
		info->window = SDL_Window;
slouken@438
   306
		if ( SDL_VERSIONNUM(info->version.major,
slouken@438
   307
		                    info->version.minor,
slouken@438
   308
		                    info->version.patch) >=
slouken@439
   309
		     SDL_VERSIONNUM(1, 2, 5) ) {
slouken@1361
   310
#if SDL_VIDEO_OPENGL
slouken@438
   311
			info->hglrc = GL_hrc;
slouken@438
   312
#else
slouken@438
   313
			info->hglrc = NULL;
slouken@438
   314
#endif
slouken@438
   315
		}
slouken@0
   316
		return(1);
slouken@0
   317
	} else {
slouken@0
   318
		SDL_SetError("Application not compiled with SDL %d.%d\n",
slouken@0
   319
					SDL_MAJOR_VERSION, SDL_MINOR_VERSION);
slouken@0
   320
		return(-1);
slouken@0
   321
	}
slouken@0
   322
}