src/video/photon/SDL_ph_wm.c
author Sam Lantinga <slouken@lokigames.com>
Thu, 26 Apr 2001 16:45:43 +0000
changeset 0 74212992fb08
child 19 8cc4dbfab9ab
permissions -rw-r--r--
Initial revision
     1 /*
     2     SDL - Simple DirectMedia Layer
     3     Copyright (C) 1997, 1998, 1999, 2000, 2001  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@devolution.com
    21 */
    22 
    23 #ifdef SAVE_RCSID
    24 static char rcsid =
    25  "@(#) $Id$";
    26 #endif
    27 
    28 #include <stdlib.h>
    29 #include <string.h>
    30 #include <Ph.h>
    31 #include "SDL_version.h"
    32 #include "SDL_error.h"
    33 #include "SDL_timer.h"
    34 #include "SDL_video.h"
    35 #include "SDL_syswm.h"
    36 #include "SDL_events_c.h"
    37 #include "SDL_pixels_c.h"
    38 #include "SDL_ph_modes_c.h"
    39 #include "SDL_ph_wm_c.h"
    40 
    41 /* This is necessary for working properly with Enlightenment, etc. */
    42 #define USE_ICON_WINDOW
    43 
    44 void ph_SetIcon(_THIS, SDL_Surface *icon, Uint8 *mask)
    45 {
    46 
    47 #if 0 /*big*/
    48 	int ncolors;
    49 	PhImage_t *image;
    50 	PgColor_t* palette;
    51 
    52 	image = PhCreateImage( image,
    53                           	icon->w,
    54                           	icon->h,
    55                           	Pg_IMAGE_DIRECT_888,
    56                           	NULL, 0, 0 );
    57 
    58 /* ---------------------------------------- */
    59 	SDL_Surface *sicon;
    60 //	XWMHints *wmhints;
    61 //	XImage *icon_image;
    62 //	Pixmap icon_pixmap;
    63 //	Pixmap mask_pixmap;
    64 //	GC GC;
    65 //	XGCValues GCvalues;
    66 	int i, b, dbpp;
    67 	SDL_Rect bounds;
    68 	Uint8 *LSBmask, *color_tried;
    69 	Visual *dvis;
    70 
    71 	/* Lock the event thread, in multi-threading environments */
    72 	SDL_Lock_EventThread();
    73 
    74 	/* The icon must use the default visual, depth and colormap of the
    75 	   screen, so it might need a conversion */
    76 // ?	dbpp = DefaultDepth(SDL_Display, SDL_Screen);
    77 	switch(dbpp) {
    78 	case 15:
    79 	    dbpp = 16; break;
    80 	case 24:
    81 	    dbpp = 32; break;
    82 	}
    83 	dvis = DefaultVisual(SDL_Display, SDL_Screen);
    84 
    85 	/* The Visual struct is supposed to be opaque but we cheat a little */
    86 	sicon = SDL_CreateRGBSurface(SDL_SWSURFACE, icon->w, icon->h,
    87 				     dbpp,
    88 				     dvis->red_mask, dvis->green_mask,
    89 				     dvis->blue_mask, 0);
    90 
    91 	if ( sicon == NULL ) {
    92 		goto done;
    93 	}
    94 	/* If we already have allocated colours from the default colormap,
    95 	   copy them */
    96 	if(SDL_Visual == dvis && SDL_XColorMap == SDL_DisplayColormap
    97 	   && this->screen->format->palette && sicon->format->palette) {
    98 	    memcpy(sicon->format->palette->colors,
    99 		   this->screen->format->palette->colors,
   100 		   this->screen->format->palette->ncolors * sizeof(SDL_Color));
   101 	}
   102 
   103 	bounds.x = 0;
   104 	bounds.y = 0;
   105 	bounds.w = icon->w;
   106 	bounds.h = icon->h;
   107 	if ( SDL_LowerBlit(icon, &bounds, sicon, &bounds) < 0 )
   108 		goto done;
   109 
   110 	/* Lock down the colors used in the colormap */
   111 	color_tried = NULL;
   112 	if ( sicon->format->BitsPerPixel == 8 ) {
   113 		SDL_Palette *palette;
   114 		Uint8 *p;
   115 		XColor wanted;
   116 
   117 		palette = sicon->format->palette;
   118 		color_tried = malloc(palette->ncolors);
   119 		if ( color_tried == NULL ) {
   120 			goto done;
   121 		}
   122 		if ( SDL_iconcolors != NULL ) {
   123 			free(SDL_iconcolors);
   124 		}
   125 		SDL_iconcolors = malloc(palette->ncolors
   126 					* sizeof(*SDL_iconcolors));
   127 		if ( SDL_iconcolors == NULL ) {
   128 			free(color_tried);
   129 			goto done;
   130 		}
   131 		memset(color_tried, 0, palette->ncolors);
   132 		memset(SDL_iconcolors, 0,
   133 		       palette->ncolors * sizeof(*SDL_iconcolors));
   134 
   135 		p = (Uint8 *)sicon->pixels; 
   136 		for ( i = sicon->w*sicon->h; i > 0; --i, ++p ) {
   137 			if ( ! color_tried[*p] ) {
   138 				wanted.pixel = *p;
   139 				wanted.red   = (palette->colors[*p].r<<8);
   140 				wanted.green = (palette->colors[*p].g<<8);
   141 				wanted.blue  = (palette->colors[*p].b<<8);
   142 				wanted.flags = (DoRed|DoGreen|DoBlue);
   143 				if (XAllocColor(SDL_Display,
   144 						SDL_DisplayColormap, &wanted)) {
   145 					++SDL_iconcolors[wanted.pixel];
   146 				}
   147 				color_tried[*p] = 1;
   148 			}
   149 		}
   150 	}
   151 	if ( color_tried != NULL ) {
   152 		free(color_tried);
   153 	}
   154 
   155 	/* Translate mask data to LSB order and set the icon mask */
   156 	i = (sicon->w/8)*sicon->h;
   157 	LSBmask = (Uint8 *)malloc(i);
   158 	if ( LSBmask == NULL ) {
   159 		goto done;
   160 	}
   161 	memset(LSBmask, 0, i);
   162 	while ( --i >= 0 ) {
   163 		for ( b=0; b<8; ++b )
   164 			LSBmask[i] |= (((mask[i]>>b)&0x01)<<(7-b));
   165 	}
   166 	mask_pixmap = XCreatePixmapFromBitmapData(SDL_Display, WMwindow,
   167 					LSBmask, sicon->w, sicon->h, 1L, 0L, 1);
   168 
   169 	/* Transfer the image to an X11 pixmap */
   170 	icon_image = XCreateImage(SDL_Display,
   171 			DefaultVisual(SDL_Display, SDL_Screen),
   172 			DefaultDepth(SDL_Display, SDL_Screen),
   173 			ZPixmap, 0, (char *)sicon->pixels, sicon->w, sicon->h,
   174 			((sicon->format)->BytesPerPixel == 3) ? 32 :
   175 				(sicon->format)->BytesPerPixel*8, 0);
   176 	icon_pixmap = XCreatePixmap(SDL_Display, SDL_Root, sicon->w, sicon->h,
   177 			DefaultDepth(SDL_Display, SDL_Screen));
   178 	GC = XCreateGC(SDL_Display, icon_pixmap, 0, &GCvalues);
   179 	XPutImage(SDL_Display, icon_pixmap, GC, icon_image,
   180 					0, 0, 0, 0, sicon->w, sicon->h);
   181 	XFreeGC(SDL_Display, GC);
   182 	XDestroyImage(icon_image);
   183 	free(LSBmask);
   184 	sicon->pixels = NULL;
   185 
   186 #ifdef USE_ICON_WINDOW
   187 	/* Create an icon window and set the pixmap as its background */
   188 	icon_window = XCreateSimpleWindow(SDL_Display, SDL_Root,
   189 					0, 0, sicon->w, sicon->h, 0,
   190 					CopyFromParent, CopyFromParent);
   191 	XSetWindowBackgroundPixmap(SDL_Display, icon_window, icon_pixmap);
   192 	XClearWindow(SDL_Display, icon_window);
   193 #endif
   194 
   195 	/* Set the window icon to the icon pixmap (and icon window) */
   196 	wmhints = XAllocWMHints();
   197 	wmhints->flags = (IconPixmapHint | IconMaskHint);
   198 	wmhints->icon_pixmap = icon_pixmap;
   199 	wmhints->icon_mask = mask_pixmap;
   200 #ifdef USE_ICON_WINDOW
   201 	wmhints->flags |= IconWindowHint;
   202 	wmhints->icon_window = icon_window;
   203 #endif
   204 	XSetWMHints(SDL_Display, WMwindow, wmhints);
   205 	XFree(wmhints);
   206 	XSync(SDL_Display, False);
   207 
   208   done:
   209 	SDL_Unlock_EventThread();
   210 	if ( sicon != NULL ) {
   211 		SDL_FreeSurface(sicon);
   212 	}
   213 	
   214 #endif /*big*/
   215 	return;
   216 }
   217 
   218 void ph_SetCaption(_THIS, const char *title, const char *icon)
   219 {
   220 
   221 #if 0
   222 	XTextProperty titleprop, iconprop;
   223 
   224 	/* Lock the event thread, in multi-threading environments */
   225 	SDL_Lock_EventThread();
   226 
   227 	if ( title != NULL ) {
   228 		XStringListToTextProperty((char **)&title, 1, &titleprop);
   229 		XSetWMName(SDL_Display, WMwindow, &titleprop);
   230 		XFree(titleprop.value);
   231 	}
   232 	if ( icon != NULL ) {
   233 		XStringListToTextProperty((char **)&icon, 1, &iconprop);
   234 		XSetWMIconName(SDL_Display, WMwindow, &iconprop);
   235 		XFree(iconprop.value);
   236 	}
   237 	XSync(SDL_Display, False);
   238 
   239 	SDL_Unlock_EventThread();
   240 #endif
   241 }
   242 
   243 /* Iconify the window */
   244 int ph_IconifyWindow(_THIS)
   245 {
   246 	int result;
   247 	
   248 #if 0
   249 	SDL_Lock_EventThread();
   250 	result = XIconifyWindow(SDL_Display, WMwindow, SDL_Screen);
   251 	XSync(SDL_Display, False);
   252 	SDL_Unlock_EventThread();
   253 #endif
   254 	return(result);
   255 }
   256 
   257 SDL_GrabMode ph_GrabInputNoLock(_THIS, SDL_GrabMode mode)
   258 {
   259 #if 0 /*big*/
   260 	int numtries, result;
   261 
   262 	if ( this->screen == NULL ) {
   263 		return(SDL_GRAB_OFF);
   264 	}
   265 	if ( ! SDL_Window ) {
   266 		return(mode);	/* Will be set later on mode switch */
   267 	}
   268 	if ( mode == SDL_GRAB_OFF ) {
   269 		XUngrabPointer(SDL_Display, CurrentTime);
   270 		if ( this->screen->flags & SDL_FULLSCREEN ) {
   271 			/* Rebind the mouse to the fullscreen window */
   272 			for ( numtries = 0; numtries < 10; ++numtries ) {
   273 				result = XGrabPointer(SDL_Display, FSwindow,
   274 						True, 0,
   275 						GrabModeAsync, GrabModeAsync,
   276 						FSwindow, None, CurrentTime);
   277 				if ( result == AlreadyGrabbed ) {
   278 					break;
   279 				}
   280 				SDL_Delay(100);
   281 			}
   282 		}
   283 #ifdef GRAB_FULLSCREEN
   284 		if ( !(this->screen->flags & SDL_FULLSCREEN) )
   285 #endif
   286 		XUngrabKeyboard(SDL_Display, CurrentTime);
   287 	} else {
   288 		if ( this->screen->flags & SDL_FULLSCREEN ) {
   289 			/* Unbind the mouse from the fullscreen window */
   290 			XUngrabPointer(SDL_Display, CurrentTime);
   291 		}
   292 		/* Try to grab the mouse */
   293 		for ( numtries = 0; numtries < 10; ++numtries ) {
   294 			result = XGrabPointer(SDL_Display, SDL_Window, True, 0,
   295 						GrabModeAsync, GrabModeAsync,
   296 						SDL_Window, None, CurrentTime);
   297 			if ( result != AlreadyGrabbed ) {
   298 				break;
   299 			}
   300 			SDL_Delay(100);
   301 		}
   302 #ifdef GRAB_FULLSCREEN
   303 		if ( !(this->screen->flags & SDL_FULLSCREEN) )
   304 #endif
   305 		XGrabKeyboard(SDL_Display, WMwindow, True,
   306 			GrabModeAsync, GrabModeAsync, CurrentTime);
   307 	}
   308 	XSync(SDL_Display, False);
   309 
   310 
   311 #endif /*big*/
   312 	return(mode);
   313 }
   314 
   315 SDL_GrabMode ph_GrabInput(_THIS, SDL_GrabMode mode)
   316 {
   317 #if 0
   318 	SDL_Lock_EventThread();
   319 	mode = X11_GrabInputNoLock(this, mode);
   320 	SDL_Unlock_EventThread();
   321 #endif
   322 	return(mode);
   323 }
   324 
   325 /* If 'info' is the right version, this function fills it and returns 1.
   326    Otherwise, in case of a version mismatch, it returns -1.
   327 */
   328 static void lock_display(void)
   329 {
   330 	SDL_Lock_EventThread();
   331 }
   332 static void unlock_display(void)
   333 {
   334 #if 0
   335 	/* Make sure any X11 transactions are completed */
   336 	SDL_VideoDevice *this = current_video;
   337 	XSync(SDL_Display, False);
   338 	SDL_Unlock_EventThread();
   339 #endif
   340 }
   341 int ph_GetWMInfo(_THIS, SDL_SysWMinfo *info)
   342 {
   343 #if 0
   344 	if ( info->version.major <= SDL_MAJOR_VERSION ) {
   345 		info->subsystem = SDL_SYSWM_X11;
   346 		info->info.x11.display = SDL_Display;
   347 		info->info.x11.window = SDL_Window;
   348 		if ( SDL_VERSIONNUM(info->version.major,
   349 		                    info->version.minor,
   350 		                    info->version.patch) >= 1002 ) {
   351 			info->info.x11.fswindow = FSwindow;
   352 			info->info.x11.wmwindow = WMwindow;
   353 		}
   354 		info->info.x11.lock_func = lock_display;
   355 		info->info.x11.unlock_func = unlock_display;
   356 		return(1);
   357 	} else {
   358 		SDL_SetError("Application not compiled with SDL %d.%d\n",
   359 					SDL_MAJOR_VERSION, SDL_MINOR_VERSION);
   360 		return(-1);
   361 	}
   362 #endif
   363 }