src/video/x11/SDL_x11wm.c
author Ryan C. Gordon <icculus@icculus.org>
Wed, 22 Mar 2006 05:00:59 +0000
changeset 1575 3ba88cb7eb1b
parent 1558 b46bb79cc197
child 1659 14717b52abc0
child 1765 c2c6ff414ef5
permissions -rw-r--r--
Updated dynamic X11 code. See details in Bugzilla #170.
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@236
    80
		goto done;
slouken@0
    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@1575
   119
			XFreeColors(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@1575
   134
			if(XAllocColor(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@1575
   148
			XQueryColors(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@1575
   154
					if(XAllocColor(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@1575
   187
	mask_pixmap = XCreatePixmapFromBitmapData(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@1575
   193
	icon_image = XCreateImage(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@1575
   201
	icon_pixmap = XCreatePixmap(SDL_Display, SDL_Root, sicon->w, sicon->h,
slouken@236
   202
				    DefaultDepth(SDL_Display, SDL_Screen));
icculus@1575
   203
	gc = XCreateGC(SDL_Display, icon_pixmap, 0, &GCvalues);
icculus@1575
   204
	XPutImage(SDL_Display, icon_pixmap, gc, icon_image,
slouken@236
   205
		  0, 0, 0, 0, sicon->w, sicon->h);
icculus@1575
   206
	XFreeGC(SDL_Display, gc);
icculus@1575
   207
	XDestroyImage(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@1575
   216
		icon_window = XCreateSimpleWindow(SDL_Display, SDL_Root,
slouken@236
   217
						  0, 0, sicon->w, sicon->h, 0,
slouken@236
   218
						  CopyFromParent,
slouken@236
   219
						  CopyFromParent);
icculus@1575
   220
		XSetWindowBackgroundPixmap(SDL_Display, icon_window,
slouken@236
   221
					   icon_pixmap);
icculus@1575
   222
		XClearWindow(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@1575
   226
	wmhints = XAllocWMHints();
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@1575
   234
	XSetWMHints(SDL_Display, WMwindow, wmhints);
icculus@1575
   235
	XFree(wmhints);
icculus@1575
   236
	XSync(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@1558
   246
	Status status;
slouken@1558
   247
slouken@1558
   248
#ifdef X_HAVE_UTF8_STRING
slouken@1558
   249
	Atom _NET_WM_NAME;
slouken@1558
   250
	Atom _NET_WM_ICON_NAME;
slouken@1558
   251
slouken@1558
   252
	/* Look up some useful Atoms */
icculus@1575
   253
	if (SDL_X11_HAVE_UTF8) {
icculus@1575
   254
		_NET_WM_NAME = XInternAtom(SDL_Display, "_NET_WM_NAME", False);
icculus@1575
   255
		_NET_WM_ICON_NAME = XInternAtom(SDL_Display, "_NET_WM_ICON_NAME", False);
icculus@1575
   256
	}
slouken@1558
   257
#endif
slouken@0
   258
slouken@0
   259
	/* Lock the event thread, in multi-threading environments */
slouken@0
   260
	SDL_Lock_EventThread();
slouken@0
   261
slouken@0
   262
	if ( title != NULL ) {
slouken@1558
   263
		char *title_latin1 = SDL_iconv_utf8_latin1((char *)title);
slouken@1558
   264
		if ( !title_latin1 ) {
slouken@1558
   265
			SDL_OutOfMemory();
slouken@1558
   266
			return;
slouken@1558
   267
		}
icculus@1575
   268
		status = XStringListToTextProperty(&title_latin1, 1, &titleprop);
slouken@1558
   269
		SDL_free(title_latin1);
slouken@1558
   270
		if ( status ) {
icculus@1575
   271
			XSetTextProperty(SDL_Display, WMwindow, &titleprop, XA_WM_NAME);
icculus@1575
   272
			XFree(titleprop.value);
slouken@1558
   273
		}
slouken@913
   274
#ifdef X_HAVE_UTF8_STRING
icculus@1575
   275
		if (SDL_X11_HAVE_UTF8) {
icculus@1575
   276
			status = Xutf8TextListToTextProperty(SDL_Display,
icculus@1575
   277
					(char **)&title, 1, XUTF8StringStyle, &titleprop);
icculus@1575
   278
			if ( status == Success ) {
icculus@1575
   279
				XSetTextProperty(SDL_Display, WMwindow, &titleprop, _NET_WM_NAME);
icculus@1575
   280
				XFree(titleprop.value);
icculus@1575
   281
			}
slouken@1558
   282
		}
slouken@913
   283
#endif
slouken@0
   284
	}
slouken@0
   285
	if ( icon != NULL ) {
slouken@1558
   286
		char *icon_latin1 = SDL_iconv_utf8_latin1((char *)icon);
slouken@1558
   287
		if ( !icon_latin1 ) {
slouken@1558
   288
			SDL_OutOfMemory();
slouken@1558
   289
			return;
slouken@1558
   290
		}
icculus@1575
   291
		status = XStringListToTextProperty(&icon_latin1, 1, &iconprop);
slouken@1558
   292
		SDL_free(icon_latin1);
slouken@1558
   293
		if ( status ) {
icculus@1575
   294
			XSetTextProperty(SDL_Display, WMwindow, &iconprop, XA_WM_ICON_NAME);
icculus@1575
   295
			XFree(iconprop.value);
slouken@1558
   296
		}
slouken@913
   297
#ifdef X_HAVE_UTF8_STRING
icculus@1575
   298
		if (SDL_X11_HAVE_UTF8) {
icculus@1575
   299
			status = Xutf8TextListToTextProperty(SDL_Display,
icculus@1575
   300
					(char **)&icon, 1, XUTF8StringStyle, &iconprop);
icculus@1575
   301
			if ( status == Success ) {
icculus@1575
   302
				XSetTextProperty(SDL_Display, WMwindow, &iconprop, _NET_WM_ICON_NAME);
icculus@1575
   303
				XFree(iconprop.value);
icculus@1575
   304
			}
slouken@1558
   305
		}
slouken@913
   306
#endif
slouken@0
   307
	}
icculus@1575
   308
	XSync(SDL_Display, False);
slouken@0
   309
slouken@0
   310
	SDL_Unlock_EventThread();
slouken@0
   311
}
slouken@0
   312
slouken@0
   313
/* Iconify the window */
slouken@0
   314
int X11_IconifyWindow(_THIS)
slouken@0
   315
{
slouken@0
   316
	int result;
slouken@0
   317
slouken@0
   318
	SDL_Lock_EventThread();
icculus@1575
   319
	result = XIconifyWindow(SDL_Display, WMwindow, SDL_Screen);
icculus@1575
   320
	XSync(SDL_Display, False);
slouken@0
   321
	SDL_Unlock_EventThread();
slouken@0
   322
	return(result);
slouken@0
   323
}
slouken@0
   324
slouken@0
   325
SDL_GrabMode X11_GrabInputNoLock(_THIS, SDL_GrabMode mode)
slouken@0
   326
{
slouken@236
   327
	int result;
slouken@0
   328
slouken@0
   329
	if ( this->screen == NULL ) {
slouken@0
   330
		return(SDL_GRAB_OFF);
slouken@0
   331
	}
slouken@0
   332
	if ( ! SDL_Window ) {
slouken@0
   333
		return(mode);	/* Will be set later on mode switch */
slouken@0
   334
	}
slouken@0
   335
	if ( mode == SDL_GRAB_OFF ) {
icculus@1575
   336
		XUngrabPointer(SDL_Display, CurrentTime);
icculus@1575
   337
		XUngrabKeyboard(SDL_Display, CurrentTime);
slouken@0
   338
	} else {
slouken@0
   339
		if ( this->screen->flags & SDL_FULLSCREEN ) {
slouken@0
   340
			/* Unbind the mouse from the fullscreen window */
icculus@1575
   341
			XUngrabPointer(SDL_Display, CurrentTime);
slouken@0
   342
		}
slouken@0
   343
		/* Try to grab the mouse */
slouken@98
   344
#if 0 /* We'll wait here until we actually grab, otherwise behavior undefined */
slouken@0
   345
		for ( numtries = 0; numtries < 10; ++numtries ) {
slouken@98
   346
#else
slouken@98
   347
		while ( 1 ) {
slouken@98
   348
#endif
icculus@1575
   349
			result = XGrabPointer(SDL_Display, SDL_Window, True, 0,
slouken@0
   350
						GrabModeAsync, GrabModeAsync,
slouken@0
   351
						SDL_Window, None, CurrentTime);
slouken@88
   352
			if ( result == GrabSuccess ) {
slouken@0
   353
				break;
slouken@0
   354
			}
slouken@0
   355
			SDL_Delay(100);
slouken@0
   356
		}
slouken@88
   357
		if ( result != GrabSuccess ) {
slouken@88
   358
			/* Uh, oh, what do we do here? */ ;
slouken@88
   359
		}
slouken@98
   360
		/* Now grab the keyboard */
icculus@1575
   361
		XGrabKeyboard(SDL_Display, WMwindow, True,
slouken@88
   362
				GrabModeAsync, GrabModeAsync, CurrentTime);
slouken@0
   363
slouken@0
   364
		/* Raise the window if we grab the mouse */
slouken@0
   365
		if ( !(this->screen->flags & SDL_FULLSCREEN) )
icculus@1575
   366
			XRaiseWindow(SDL_Display, WMwindow);
slouken@98
   367
slouken@98
   368
		/* Make sure we register input focus */
slouken@98
   369
		SDL_PrivateAppActive(1, SDL_APPINPUTFOCUS);
slouken@0
   370
	}
icculus@1575
   371
	XSync(SDL_Display, False);
slouken@0
   372
slouken@0
   373
	return(mode);
slouken@0
   374
}
slouken@0
   375
slouken@0
   376
SDL_GrabMode X11_GrabInput(_THIS, SDL_GrabMode mode)
slouken@0
   377
{
slouken@0
   378
	SDL_Lock_EventThread();
slouken@0
   379
	mode = X11_GrabInputNoLock(this, mode);
slouken@0
   380
	SDL_Unlock_EventThread();
slouken@0
   381
slouken@0
   382
	return(mode);
slouken@0
   383
}
slouken@0
   384
slouken@0
   385
/* If 'info' is the right version, this function fills it and returns 1.
slouken@0
   386
   Otherwise, in case of a version mismatch, it returns -1.
slouken@0
   387
*/
slouken@0
   388
static void lock_display(void)
slouken@0
   389
{
slouken@0
   390
	SDL_Lock_EventThread();
slouken@0
   391
}
slouken@0
   392
static void unlock_display(void)
slouken@0
   393
{
slouken@0
   394
	/* Make sure any X11 transactions are completed */
slouken@0
   395
	SDL_VideoDevice *this = current_video;
icculus@1575
   396
	XSync(SDL_Display, False);
slouken@0
   397
	SDL_Unlock_EventThread();
slouken@0
   398
}
slouken@0
   399
int X11_GetWMInfo(_THIS, SDL_SysWMinfo *info)
slouken@0
   400
{
slouken@0
   401
	if ( info->version.major <= SDL_MAJOR_VERSION ) {
slouken@0
   402
		info->subsystem = SDL_SYSWM_X11;
slouken@0
   403
		info->info.x11.display = SDL_Display;
slouken@0
   404
		info->info.x11.window = SDL_Window;
slouken@0
   405
		if ( SDL_VERSIONNUM(info->version.major,
slouken@0
   406
		                    info->version.minor,
slouken@0
   407
		                    info->version.patch) >= 1002 ) {
slouken@0
   408
			info->info.x11.fswindow = FSwindow;
slouken@0
   409
			info->info.x11.wmwindow = WMwindow;
slouken@0
   410
		}
slouken@0
   411
		info->info.x11.lock_func = lock_display;
slouken@0
   412
		info->info.x11.unlock_func = unlock_display;
slouken@0
   413
		return(1);
slouken@0
   414
	} else {
slouken@0
   415
		SDL_SetError("Application not compiled with SDL %d.%d\n",
slouken@0
   416
					SDL_MAJOR_VERSION, SDL_MINOR_VERSION);
slouken@0
   417
		return(-1);
slouken@0
   418
	}
slouken@0
   419
}