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