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