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