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