src/video/x11/SDL_x11wm.c
author Sam Lantinga <slouken@libsdl.org>
Wed, 06 Mar 2002 11:23:08 +0000
changeset 297 f6ffac90895c
parent 252 e8157fcb3114
child 769 b8d311d90021
permissions -rw-r--r--
Updated copyright information for 2002
     1 /*
     2     SDL - Simple DirectMedia Layer
     3     Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002  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 <stdlib.h>
    29 #include <string.h>
    30 #include <X11/Xlib.h>
    31 #include <X11/Xutil.h>
    32 
    33 #include "SDL_version.h"
    34 #include "SDL_error.h"
    35 #include "SDL_timer.h"
    36 #include "SDL_video.h"
    37 #include "SDL_syswm.h"
    38 #include "SDL_events_c.h"
    39 #include "SDL_pixels_c.h"
    40 #include "SDL_x11modes_c.h"
    41 #include "SDL_x11wm_c.h"
    42 
    43 static Uint8 reverse_byte(Uint8 x)
    44 {
    45 	x = (x & 0xaa) >> 1 | (x & 0x55) << 1;
    46 	x = (x & 0xcc) >> 2 | (x & 0x33) << 2;
    47 	x = (x & 0xf0) >> 4 | (x & 0x0f) << 4;
    48 	return x;
    49 }
    50 
    51 void X11_SetIcon(_THIS, SDL_Surface *icon, Uint8 *mask)
    52 {
    53 	SDL_Surface *sicon;
    54 	XWMHints *wmhints;
    55 	XImage *icon_image;
    56 	Pixmap icon_pixmap;
    57 	Pixmap mask_pixmap;
    58 	Window icon_window = None;
    59 	GC gc;
    60 	XGCValues GCvalues;
    61 	int i, dbpp;
    62 	SDL_Rect bounds;
    63 	Uint8 *LSBmask;
    64 	Visual *dvis;
    65 	char *p;
    66 	int masksize;
    67 
    68 	SDL_Lock_EventThread();
    69 
    70 	/* The icon must use the default visual, depth and colormap of the
    71 	   screen, so it might need a conversion */
    72 	dvis = DefaultVisual(SDL_Display, SDL_Screen);
    73 	dbpp = DefaultDepth(SDL_Display, SDL_Screen);
    74 	for(i = 0; i < this->hidden->nvisuals; i++) {
    75 		if(this->hidden->visuals[i].visual == dvis) {
    76 			dbpp = this->hidden->visuals[i].bpp;
    77 			break;
    78 		}
    79 	}
    80 
    81 	/* The Visual struct is supposed to be opaque but we cheat a little */
    82 	sicon = SDL_CreateRGBSurface(SDL_SWSURFACE, icon->w, icon->h,
    83 				     dbpp,
    84 				     dvis->red_mask, dvis->green_mask,
    85 				     dvis->blue_mask, 0);
    86 	if ( sicon == NULL )
    87 		goto done;
    88 
    89 	if(dbpp == 8) {
    90 		/* Default visual is 8bit; we need to allocate colours from
    91 		   the default colormap */
    92 		SDL_Color want[256], got[256];
    93 		int nwant;
    94 		Colormap dcmap;
    95 		int missing;
    96 		dcmap = DefaultColormap(SDL_Display, SDL_Screen);
    97 		if(icon->format->palette) {
    98 			/* The icon has a palette as well - we just have to
    99 			   find those colours */
   100 			nwant = icon->format->palette->ncolors;
   101 			memcpy(want, icon->format->palette->colors,
   102 			       nwant * sizeof want[0]);
   103 		} else {
   104 			/* try the standard 6x6x6 cube for lack of better
   105 			   ideas */
   106 			int r, g, b, i;
   107 			for(r = i = 0; r < 256; r += 0x33)
   108 				for(g = 0; g < 256; g += 0x33)
   109 					for(b = 0; b < 256; b += 0x33, i++) {
   110 						want[i].r = r;
   111 						want[i].g = g;
   112 						want[i].b = b;
   113 					}
   114 			nwant = 216;
   115 		}
   116 		if(SDL_iconcolors) {
   117 			/* free already allocated colours first */
   118 			unsigned long freelist[512];
   119 			int nfree = 0;
   120 			for(i = 0; i < 256; i++) {
   121 				while(SDL_iconcolors[i]) {
   122 					freelist[nfree++] = i;
   123 					SDL_iconcolors[i]--;
   124 				}
   125 			}
   126 			XFreeColors(GFX_Display, dcmap, freelist, nfree, 0);
   127 		}
   128 		if(!SDL_iconcolors)
   129 			SDL_iconcolors = malloc(256 * sizeof *SDL_iconcolors);
   130 		memset(SDL_iconcolors, 0, 256 * sizeof *SDL_iconcolors);
   131 
   132 		/* try to allocate the colours */
   133 		memset(got, 0, sizeof got);
   134 		missing = 0;
   135 		for(i = 0; i < nwant; i++) {
   136 			XColor c;
   137 			c.red = want[i].r << 8;
   138 			c.green = want[i].g << 8;
   139 			c.blue = want[i].b << 8;
   140 			c.flags = DoRed | DoGreen | DoBlue;
   141 			if(XAllocColor(GFX_Display, dcmap, &c)) {
   142 				/* got the colour */
   143 				SDL_iconcolors[c.pixel]++;
   144 				got[c.pixel] = want[i];
   145 			} else {
   146 				missing = 1;
   147 			}
   148 		}
   149 		if(missing) {
   150 			/* Some colours were apparently missing, so we just
   151 			   allocate all the rest as well */
   152 			XColor cols[256];
   153 			for(i = 0; i < 256; i++)
   154 				cols[i].pixel = i;
   155 			XQueryColors(GFX_Display, dcmap, cols, 256);
   156 			for(i = 0; i < 256; i++) {
   157 				got[i].r = cols[i].red >> 8;
   158 				got[i].g = cols[i].green >> 8;
   159 				got[i].b = cols[i].blue >> 8;
   160 				if(!SDL_iconcolors[i]) {
   161 					if(XAllocColor(GFX_Display, dcmap,
   162 							cols + i)) {
   163 						SDL_iconcolors[i] = 1;
   164 					} else {
   165 						/* index not available */
   166 						got[i].r = 0;
   167 						got[i].g = 0;
   168 						got[i].b = 0;
   169 					}
   170 				}
   171 			}
   172 		}
   173 
   174 		SDL_SetColors(sicon, got, 0, 256);
   175 	}
   176 
   177 	bounds.x = 0;
   178 	bounds.y = 0;
   179 	bounds.w = icon->w;
   180 	bounds.h = icon->h;
   181 	if ( SDL_LowerBlit(icon, &bounds, sicon, &bounds) < 0 )
   182 		goto done;
   183 
   184 	/* We need the mask as given, except in LSBfirst format instead of
   185 	   MSBfirst. Reverse the bits in each byte. */
   186 	masksize = ((sicon->w + 7) >> 3) * sicon->h;
   187 	LSBmask = malloc(masksize);
   188 	if ( LSBmask == NULL ) {
   189 		goto done;
   190 	}
   191 	memset(LSBmask, 0, masksize);
   192 	for(i = 0; i < masksize; i++)
   193 		LSBmask[i] = reverse_byte(mask[i]);
   194 	mask_pixmap = XCreatePixmapFromBitmapData(SDL_Display, WMwindow,
   195 						  (char *)LSBmask,
   196 						  sicon->w, sicon->h,
   197 						  1L, 0L, 1);
   198 
   199 	/* Transfer the image to an X11 pixmap */
   200 	icon_image = XCreateImage(SDL_Display,
   201 				  DefaultVisual(SDL_Display, SDL_Screen),
   202 				  DefaultDepth(SDL_Display, SDL_Screen),
   203 				  ZPixmap, 0, sicon->pixels,
   204 				  sicon->w, sicon->h,
   205 				  32, 0);
   206 	icon_image->byte_order = (SDL_BYTEORDER == SDL_BIG_ENDIAN)
   207 		                 ? MSBFirst : LSBFirst;
   208 	icon_pixmap = XCreatePixmap(SDL_Display, SDL_Root, sicon->w, sicon->h,
   209 				    DefaultDepth(SDL_Display, SDL_Screen));
   210 	gc = XCreateGC(SDL_Display, icon_pixmap, 0, &GCvalues);
   211 	XPutImage(SDL_Display, icon_pixmap, gc, icon_image,
   212 		  0, 0, 0, 0, sicon->w, sicon->h);
   213 	XFreeGC(SDL_Display, gc);
   214 	XDestroyImage(icon_image);
   215 	free(LSBmask);
   216 	sicon->pixels = NULL;
   217 
   218 	/* Some buggy window managers (some versions of Enlightenment, it
   219 	   seems) need an icon window *and* icon pixmap to work properly, while
   220 	   it screws up others. The default is only to use a pixmap. */
   221 	p = getenv("SDL_VIDEO_X11_ICONWIN");
   222 	if(p && *p) {
   223 		icon_window = XCreateSimpleWindow(SDL_Display, SDL_Root,
   224 						  0, 0, sicon->w, sicon->h, 0,
   225 						  CopyFromParent,
   226 						  CopyFromParent);
   227 		XSetWindowBackgroundPixmap(SDL_Display, icon_window,
   228 					   icon_pixmap);
   229 		XClearWindow(SDL_Display, icon_window);
   230 	}
   231 
   232 	/* Set the window icon to the icon pixmap (and icon window) */
   233 	wmhints = XAllocWMHints();
   234 	wmhints->flags = (IconPixmapHint | IconMaskHint);
   235 	wmhints->icon_pixmap = icon_pixmap;
   236 	wmhints->icon_mask = mask_pixmap;
   237 	if(icon_window != None) {
   238 		wmhints->flags |= IconWindowHint;
   239 		wmhints->icon_window = icon_window;
   240 	}
   241 	XSetWMHints(SDL_Display, WMwindow, wmhints);
   242 	XFree(wmhints);
   243 	XSync(SDL_Display, False);
   244 
   245   done:
   246 	SDL_Unlock_EventThread();
   247 	SDL_FreeSurface(sicon);
   248 }
   249 
   250 void X11_SetCaption(_THIS, const char *title, const char *icon)
   251 {
   252 	XTextProperty titleprop, iconprop;
   253 
   254 	/* Lock the event thread, in multi-threading environments */
   255 	SDL_Lock_EventThread();
   256 
   257 	if ( title != NULL ) {
   258 		XStringListToTextProperty((char **)&title, 1, &titleprop);
   259 		XSetWMName(SDL_Display, WMwindow, &titleprop);
   260 		XFree(titleprop.value);
   261 	}
   262 	if ( icon != NULL ) {
   263 		XStringListToTextProperty((char **)&icon, 1, &iconprop);
   264 		XSetWMIconName(SDL_Display, WMwindow, &iconprop);
   265 		XFree(iconprop.value);
   266 	}
   267 	XSync(SDL_Display, False);
   268 
   269 	SDL_Unlock_EventThread();
   270 }
   271 
   272 /* Iconify the window */
   273 int X11_IconifyWindow(_THIS)
   274 {
   275 	int result;
   276 
   277 	SDL_Lock_EventThread();
   278 	result = XIconifyWindow(SDL_Display, WMwindow, SDL_Screen);
   279 	XSync(SDL_Display, False);
   280 	SDL_Unlock_EventThread();
   281 	return(result);
   282 }
   283 
   284 SDL_GrabMode X11_GrabInputNoLock(_THIS, SDL_GrabMode mode)
   285 {
   286 	int result;
   287 
   288 	if ( this->screen == NULL ) {
   289 		return(SDL_GRAB_OFF);
   290 	}
   291 	if ( ! SDL_Window ) {
   292 		return(mode);	/* Will be set later on mode switch */
   293 	}
   294 	if ( mode == SDL_GRAB_OFF ) {
   295 		XUngrabPointer(SDL_Display, CurrentTime);
   296 		XUngrabKeyboard(SDL_Display, CurrentTime);
   297 	} else {
   298 		if ( this->screen->flags & SDL_FULLSCREEN ) {
   299 			/* Unbind the mouse from the fullscreen window */
   300 			XUngrabPointer(SDL_Display, CurrentTime);
   301 		}
   302 		/* Try to grab the mouse */
   303 #if 0 /* We'll wait here until we actually grab, otherwise behavior undefined */
   304 		for ( numtries = 0; numtries < 10; ++numtries ) {
   305 #else
   306 		while ( 1 ) {
   307 #endif
   308 			result = XGrabPointer(SDL_Display, SDL_Window, True, 0,
   309 						GrabModeAsync, GrabModeAsync,
   310 						SDL_Window, None, CurrentTime);
   311 			if ( result == GrabSuccess ) {
   312 				break;
   313 			}
   314 			SDL_Delay(100);
   315 		}
   316 		if ( result != GrabSuccess ) {
   317 			/* Uh, oh, what do we do here? */ ;
   318 		}
   319 		/* Now grab the keyboard */
   320 		XGrabKeyboard(SDL_Display, WMwindow, True,
   321 				GrabModeAsync, GrabModeAsync, CurrentTime);
   322 
   323 		/* Raise the window if we grab the mouse */
   324 		if ( !(this->screen->flags & SDL_FULLSCREEN) )
   325 			XRaiseWindow(SDL_Display, WMwindow);
   326 
   327 		/* Make sure we register input focus */
   328 		SDL_PrivateAppActive(1, SDL_APPINPUTFOCUS);
   329 	}
   330 	XSync(SDL_Display, False);
   331 
   332 	return(mode);
   333 }
   334 
   335 SDL_GrabMode X11_GrabInput(_THIS, SDL_GrabMode mode)
   336 {
   337 	SDL_Lock_EventThread();
   338 	mode = X11_GrabInputNoLock(this, mode);
   339 	SDL_Unlock_EventThread();
   340 
   341 	return(mode);
   342 }
   343 
   344 /* If 'info' is the right version, this function fills it and returns 1.
   345    Otherwise, in case of a version mismatch, it returns -1.
   346 */
   347 static void lock_display(void)
   348 {
   349 	SDL_Lock_EventThread();
   350 }
   351 static void unlock_display(void)
   352 {
   353 	/* Make sure any X11 transactions are completed */
   354 	SDL_VideoDevice *this = current_video;
   355 	XSync(SDL_Display, False);
   356 	SDL_Unlock_EventThread();
   357 }
   358 int X11_GetWMInfo(_THIS, SDL_SysWMinfo *info)
   359 {
   360 	if ( info->version.major <= SDL_MAJOR_VERSION ) {
   361 		info->subsystem = SDL_SYSWM_X11;
   362 		info->info.x11.display = SDL_Display;
   363 		info->info.x11.window = SDL_Window;
   364 		if ( SDL_VERSIONNUM(info->version.major,
   365 		                    info->version.minor,
   366 		                    info->version.patch) >= 1002 ) {
   367 			info->info.x11.fswindow = FSwindow;
   368 			info->info.x11.wmwindow = WMwindow;
   369 		}
   370 		info->info.x11.lock_func = lock_display;
   371 		info->info.x11.unlock_func = unlock_display;
   372 		return(1);
   373 	} else {
   374 		SDL_SetError("Application not compiled with SDL %d.%d\n",
   375 					SDL_MAJOR_VERSION, SDL_MINOR_VERSION);
   376 		return(-1);
   377 	}
   378 }