src/video/x11/SDL_x11wm.c
author Ryan C. Gordon <icculus@icculus.org>
Fri, 05 Oct 2012 23:18:53 -0400
branchSDL-1.2
changeset 6563 ba187d0466e6
parent 6561 ea34ef329509
child 7411 14827e7e4d92
permissions -rw-r--r--
XFlush() after setting the icon.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
     1
/*
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
     2
    SDL - Simple DirectMedia Layer
6137
4720145f848b Happy New Year!
Sam Lantinga <slouken@libsdl.org>
parents: 4553
diff changeset
     3
    Copyright (C) 1997-2012 Sam Lantinga
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
     4
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
     5
    This library is free software; you can redistribute it and/or
1312
c9b51268668f Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents: 1168
diff changeset
     6
    modify it under the terms of the GNU Lesser General Public
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
     7
    License as published by the Free Software Foundation; either
1312
c9b51268668f Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents: 1168
diff changeset
     8
    version 2.1 of the License, or (at your option) any later version.
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
     9
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    10
    This library is distributed in the hope that it will be useful,
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
1312
c9b51268668f Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents: 1168
diff changeset
    13
    Lesser General Public License for more details.
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    14
1312
c9b51268668f Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents: 1168
diff changeset
    15
    You should have received a copy of the GNU Lesser General Public
c9b51268668f Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents: 1168
diff changeset
    16
    License along with this library; if not, write to the Free Software
c9b51268668f Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents: 1168
diff changeset
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    18
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    19
    Sam Lantinga
252
e8157fcb3114 Updated the source with the correct e-mail address
Sam Lantinga <slouken@libsdl.org>
parents: 246
diff changeset
    20
    slouken@libsdl.org
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    21
*/
1402
d910939febfa Use consistent identifiers for the various platforms we support.
Sam Lantinga <slouken@libsdl.org>
parents: 1361
diff changeset
    22
#include "SDL_config.h"
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    23
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    24
#include <X11/Xlib.h>
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    25
#include <X11/Xutil.h>
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    26
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    27
#include "SDL_version.h"
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    28
#include "SDL_timer.h"
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    29
#include "SDL_video.h"
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    30
#include "SDL_syswm.h"
1361
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents: 1358
diff changeset
    31
#include "../SDL_pixels_c.h"
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents: 1358
diff changeset
    32
#include "../../events/SDL_events_c.h"
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    33
#include "SDL_x11modes_c.h"
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    34
#include "SDL_x11wm_c.h"
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    35
236
3f09f52ac2cc Fixed X11 icon color allocation (thanks Mattias!)
Sam Lantinga <slouken@libsdl.org>
parents: 99
diff changeset
    36
static Uint8 reverse_byte(Uint8 x)
3f09f52ac2cc Fixed X11 icon color allocation (thanks Mattias!)
Sam Lantinga <slouken@libsdl.org>
parents: 99
diff changeset
    37
{
3f09f52ac2cc Fixed X11 icon color allocation (thanks Mattias!)
Sam Lantinga <slouken@libsdl.org>
parents: 99
diff changeset
    38
	x = (x & 0xaa) >> 1 | (x & 0x55) << 1;
3f09f52ac2cc Fixed X11 icon color allocation (thanks Mattias!)
Sam Lantinga <slouken@libsdl.org>
parents: 99
diff changeset
    39
	x = (x & 0xcc) >> 2 | (x & 0x33) << 2;
3f09f52ac2cc Fixed X11 icon color allocation (thanks Mattias!)
Sam Lantinga <slouken@libsdl.org>
parents: 99
diff changeset
    40
	x = (x & 0xf0) >> 4 | (x & 0x0f) << 4;
3f09f52ac2cc Fixed X11 icon color allocation (thanks Mattias!)
Sam Lantinga <slouken@libsdl.org>
parents: 99
diff changeset
    41
	return x;
3f09f52ac2cc Fixed X11 icon color allocation (thanks Mattias!)
Sam Lantinga <slouken@libsdl.org>
parents: 99
diff changeset
    42
}
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    43
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    44
void X11_SetIcon(_THIS, SDL_Surface *icon, Uint8 *mask)
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    45
{
6561
ea34ef329509 Try to use _NET_WM_ICON if possible for X11's SDL_WM_SetIcon() implementation.
Ryan C. Gordon <icculus@icculus.org>
parents: 6137
diff changeset
    46
	Atom _NET_WM_ICON = XInternAtom(SDL_Display, "_NET_WM_ICON", False);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    47
	SDL_Surface *sicon;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    48
	XWMHints *wmhints;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    49
	XImage *icon_image;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    50
	Pixmap icon_pixmap;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    51
	Pixmap mask_pixmap;
236
3f09f52ac2cc Fixed X11 icon color allocation (thanks Mattias!)
Sam Lantinga <slouken@libsdl.org>
parents: 99
diff changeset
    52
	Window icon_window = None;
3f09f52ac2cc Fixed X11 icon color allocation (thanks Mattias!)
Sam Lantinga <slouken@libsdl.org>
parents: 99
diff changeset
    53
	GC gc;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    54
	XGCValues GCvalues;
236
3f09f52ac2cc Fixed X11 icon color allocation (thanks Mattias!)
Sam Lantinga <slouken@libsdl.org>
parents: 99
diff changeset
    55
	int i, dbpp;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    56
	SDL_Rect bounds;
236
3f09f52ac2cc Fixed X11 icon color allocation (thanks Mattias!)
Sam Lantinga <slouken@libsdl.org>
parents: 99
diff changeset
    57
	Uint8 *LSBmask;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    58
	Visual *dvis;
236
3f09f52ac2cc Fixed X11 icon color allocation (thanks Mattias!)
Sam Lantinga <slouken@libsdl.org>
parents: 99
diff changeset
    59
	char *p;
3f09f52ac2cc Fixed X11 icon color allocation (thanks Mattias!)
Sam Lantinga <slouken@libsdl.org>
parents: 99
diff changeset
    60
	int masksize;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    61
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    62
	SDL_Lock_EventThread();
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    63
6561
ea34ef329509 Try to use _NET_WM_ICON if possible for X11's SDL_WM_SetIcon() implementation.
Ryan C. Gordon <icculus@icculus.org>
parents: 6137
diff changeset
    64
	if (_NET_WM_ICON) {   /* better interface for modern systems. */
ea34ef329509 Try to use _NET_WM_ICON if possible for X11's SDL_WM_SetIcon() implementation.
Ryan C. Gordon <icculus@icculus.org>
parents: 6137
diff changeset
    65
		SDL_PixelFormat format;
ea34ef329509 Try to use _NET_WM_ICON if possible for X11's SDL_WM_SetIcon() implementation.
Ryan C. Gordon <icculus@icculus.org>
parents: 6137
diff changeset
    66
		SDL_Surface *surface;
ea34ef329509 Try to use _NET_WM_ICON if possible for X11's SDL_WM_SetIcon() implementation.
Ryan C. Gordon <icculus@icculus.org>
parents: 6137
diff changeset
    67
		int propsize;
ea34ef329509 Try to use _NET_WM_ICON if possible for X11's SDL_WM_SetIcon() implementation.
Ryan C. Gordon <icculus@icculus.org>
parents: 6137
diff changeset
    68
		long *propdata;
ea34ef329509 Try to use _NET_WM_ICON if possible for X11's SDL_WM_SetIcon() implementation.
Ryan C. Gordon <icculus@icculus.org>
parents: 6137
diff changeset
    69
ea34ef329509 Try to use _NET_WM_ICON if possible for X11's SDL_WM_SetIcon() implementation.
Ryan C. Gordon <icculus@icculus.org>
parents: 6137
diff changeset
    70
		/* Convert the icon to ARGB for modern window managers */
ea34ef329509 Try to use _NET_WM_ICON if possible for X11's SDL_WM_SetIcon() implementation.
Ryan C. Gordon <icculus@icculus.org>
parents: 6137
diff changeset
    71
		SDL_memset(&format, 0, sizeof (format));
ea34ef329509 Try to use _NET_WM_ICON if possible for X11's SDL_WM_SetIcon() implementation.
Ryan C. Gordon <icculus@icculus.org>
parents: 6137
diff changeset
    72
		format.BitsPerPixel = 32;
ea34ef329509 Try to use _NET_WM_ICON if possible for X11's SDL_WM_SetIcon() implementation.
Ryan C. Gordon <icculus@icculus.org>
parents: 6137
diff changeset
    73
		format.BytesPerPixel = 4;
ea34ef329509 Try to use _NET_WM_ICON if possible for X11's SDL_WM_SetIcon() implementation.
Ryan C. Gordon <icculus@icculus.org>
parents: 6137
diff changeset
    74
		#if SDL_BYTEORDER == SDL_BIG_ENDIAN
ea34ef329509 Try to use _NET_WM_ICON if possible for X11's SDL_WM_SetIcon() implementation.
Ryan C. Gordon <icculus@icculus.org>
parents: 6137
diff changeset
    75
		format.Rshift = 8;
ea34ef329509 Try to use _NET_WM_ICON if possible for X11's SDL_WM_SetIcon() implementation.
Ryan C. Gordon <icculus@icculus.org>
parents: 6137
diff changeset
    76
		format.Gshift = 16;
ea34ef329509 Try to use _NET_WM_ICON if possible for X11's SDL_WM_SetIcon() implementation.
Ryan C. Gordon <icculus@icculus.org>
parents: 6137
diff changeset
    77
		format.Bshift = 24;
ea34ef329509 Try to use _NET_WM_ICON if possible for X11's SDL_WM_SetIcon() implementation.
Ryan C. Gordon <icculus@icculus.org>
parents: 6137
diff changeset
    78
		format.Ashift = 0;
ea34ef329509 Try to use _NET_WM_ICON if possible for X11's SDL_WM_SetIcon() implementation.
Ryan C. Gordon <icculus@icculus.org>
parents: 6137
diff changeset
    79
		#else
ea34ef329509 Try to use _NET_WM_ICON if possible for X11's SDL_WM_SetIcon() implementation.
Ryan C. Gordon <icculus@icculus.org>
parents: 6137
diff changeset
    80
		format.Rshift = 16;
ea34ef329509 Try to use _NET_WM_ICON if possible for X11's SDL_WM_SetIcon() implementation.
Ryan C. Gordon <icculus@icculus.org>
parents: 6137
diff changeset
    81
		format.Gshift = 8;
ea34ef329509 Try to use _NET_WM_ICON if possible for X11's SDL_WM_SetIcon() implementation.
Ryan C. Gordon <icculus@icculus.org>
parents: 6137
diff changeset
    82
		format.Bshift = 0;
ea34ef329509 Try to use _NET_WM_ICON if possible for X11's SDL_WM_SetIcon() implementation.
Ryan C. Gordon <icculus@icculus.org>
parents: 6137
diff changeset
    83
		format.Ashift = 24;
ea34ef329509 Try to use _NET_WM_ICON if possible for X11's SDL_WM_SetIcon() implementation.
Ryan C. Gordon <icculus@icculus.org>
parents: 6137
diff changeset
    84
		#endif
ea34ef329509 Try to use _NET_WM_ICON if possible for X11's SDL_WM_SetIcon() implementation.
Ryan C. Gordon <icculus@icculus.org>
parents: 6137
diff changeset
    85
		format.Rmask = 0xFF << format.Rshift;
ea34ef329509 Try to use _NET_WM_ICON if possible for X11's SDL_WM_SetIcon() implementation.
Ryan C. Gordon <icculus@icculus.org>
parents: 6137
diff changeset
    86
		format.Gmask = 0xFF << format.Gshift;
ea34ef329509 Try to use _NET_WM_ICON if possible for X11's SDL_WM_SetIcon() implementation.
Ryan C. Gordon <icculus@icculus.org>
parents: 6137
diff changeset
    87
		format.Bmask = 0xFF << format.Bshift;
ea34ef329509 Try to use _NET_WM_ICON if possible for X11's SDL_WM_SetIcon() implementation.
Ryan C. Gordon <icculus@icculus.org>
parents: 6137
diff changeset
    88
		format.Amask = 0xFF << format.Ashift;
ea34ef329509 Try to use _NET_WM_ICON if possible for X11's SDL_WM_SetIcon() implementation.
Ryan C. Gordon <icculus@icculus.org>
parents: 6137
diff changeset
    89
		format.alpha = SDL_ALPHA_OPAQUE;
ea34ef329509 Try to use _NET_WM_ICON if possible for X11's SDL_WM_SetIcon() implementation.
Ryan C. Gordon <icculus@icculus.org>
parents: 6137
diff changeset
    90
ea34ef329509 Try to use _NET_WM_ICON if possible for X11's SDL_WM_SetIcon() implementation.
Ryan C. Gordon <icculus@icculus.org>
parents: 6137
diff changeset
    91
		surface = SDL_ConvertSurface(icon, &format, 0);
ea34ef329509 Try to use _NET_WM_ICON if possible for X11's SDL_WM_SetIcon() implementation.
Ryan C. Gordon <icculus@icculus.org>
parents: 6137
diff changeset
    92
		if (!surface) {
ea34ef329509 Try to use _NET_WM_ICON if possible for X11's SDL_WM_SetIcon() implementation.
Ryan C. Gordon <icculus@icculus.org>
parents: 6137
diff changeset
    93
			return;
ea34ef329509 Try to use _NET_WM_ICON if possible for X11's SDL_WM_SetIcon() implementation.
Ryan C. Gordon <icculus@icculus.org>
parents: 6137
diff changeset
    94
		}
ea34ef329509 Try to use _NET_WM_ICON if possible for X11's SDL_WM_SetIcon() implementation.
Ryan C. Gordon <icculus@icculus.org>
parents: 6137
diff changeset
    95
ea34ef329509 Try to use _NET_WM_ICON if possible for X11's SDL_WM_SetIcon() implementation.
Ryan C. Gordon <icculus@icculus.org>
parents: 6137
diff changeset
    96
		/* Set the _NET_WM_ICON property */
ea34ef329509 Try to use _NET_WM_ICON if possible for X11's SDL_WM_SetIcon() implementation.
Ryan C. Gordon <icculus@icculus.org>
parents: 6137
diff changeset
    97
		propsize = 2 + (icon->w * icon->h);
ea34ef329509 Try to use _NET_WM_ICON if possible for X11's SDL_WM_SetIcon() implementation.
Ryan C. Gordon <icculus@icculus.org>
parents: 6137
diff changeset
    98
		propdata = SDL_malloc(propsize * sizeof(long));
ea34ef329509 Try to use _NET_WM_ICON if possible for X11's SDL_WM_SetIcon() implementation.
Ryan C. Gordon <icculus@icculus.org>
parents: 6137
diff changeset
    99
		if (propdata) {
ea34ef329509 Try to use _NET_WM_ICON if possible for X11's SDL_WM_SetIcon() implementation.
Ryan C. Gordon <icculus@icculus.org>
parents: 6137
diff changeset
   100
			const Uint32 alpha = format.Amask;
ea34ef329509 Try to use _NET_WM_ICON if possible for X11's SDL_WM_SetIcon() implementation.
Ryan C. Gordon <icculus@icculus.org>
parents: 6137
diff changeset
   101
			int x, y;
ea34ef329509 Try to use _NET_WM_ICON if possible for X11's SDL_WM_SetIcon() implementation.
Ryan C. Gordon <icculus@icculus.org>
parents: 6137
diff changeset
   102
			Uint32 *src;
ea34ef329509 Try to use _NET_WM_ICON if possible for X11's SDL_WM_SetIcon() implementation.
Ryan C. Gordon <icculus@icculus.org>
parents: 6137
diff changeset
   103
			long *dst;
ea34ef329509 Try to use _NET_WM_ICON if possible for X11's SDL_WM_SetIcon() implementation.
Ryan C. Gordon <icculus@icculus.org>
parents: 6137
diff changeset
   104
ea34ef329509 Try to use _NET_WM_ICON if possible for X11's SDL_WM_SetIcon() implementation.
Ryan C. Gordon <icculus@icculus.org>
parents: 6137
diff changeset
   105
			propdata[0] = icon->w;
ea34ef329509 Try to use _NET_WM_ICON if possible for X11's SDL_WM_SetIcon() implementation.
Ryan C. Gordon <icculus@icculus.org>
parents: 6137
diff changeset
   106
			propdata[1] = icon->h;
ea34ef329509 Try to use _NET_WM_ICON if possible for X11's SDL_WM_SetIcon() implementation.
Ryan C. Gordon <icculus@icculus.org>
parents: 6137
diff changeset
   107
			dst = &propdata[2];
ea34ef329509 Try to use _NET_WM_ICON if possible for X11's SDL_WM_SetIcon() implementation.
Ryan C. Gordon <icculus@icculus.org>
parents: 6137
diff changeset
   108
ea34ef329509 Try to use _NET_WM_ICON if possible for X11's SDL_WM_SetIcon() implementation.
Ryan C. Gordon <icculus@icculus.org>
parents: 6137
diff changeset
   109
			size_t maskidx = 0;
ea34ef329509 Try to use _NET_WM_ICON if possible for X11's SDL_WM_SetIcon() implementation.
Ryan C. Gordon <icculus@icculus.org>
parents: 6137
diff changeset
   110
			for (y = 0; y < icon->h; ++y) {
ea34ef329509 Try to use _NET_WM_ICON if possible for X11's SDL_WM_SetIcon() implementation.
Ryan C. Gordon <icculus@icculus.org>
parents: 6137
diff changeset
   111
				src = (Uint32*)((Uint8*)surface->pixels + y * surface->pitch);
ea34ef329509 Try to use _NET_WM_ICON if possible for X11's SDL_WM_SetIcon() implementation.
Ryan C. Gordon <icculus@icculus.org>
parents: 6137
diff changeset
   112
				for (x = 0; x < icon->w; ++x) {
ea34ef329509 Try to use _NET_WM_ICON if possible for X11's SDL_WM_SetIcon() implementation.
Ryan C. Gordon <icculus@icculus.org>
parents: 6137
diff changeset
   113
					const Uint32 pixel = *(src++);
ea34ef329509 Try to use _NET_WM_ICON if possible for X11's SDL_WM_SetIcon() implementation.
Ryan C. Gordon <icculus@icculus.org>
parents: 6137
diff changeset
   114
					if (mask[maskidx / 8] & (1<<(7-(maskidx % 8)))) {
ea34ef329509 Try to use _NET_WM_ICON if possible for X11's SDL_WM_SetIcon() implementation.
Ryan C. Gordon <icculus@icculus.org>
parents: 6137
diff changeset
   115
						*dst++ = pixel | alpha;
ea34ef329509 Try to use _NET_WM_ICON if possible for X11's SDL_WM_SetIcon() implementation.
Ryan C. Gordon <icculus@icculus.org>
parents: 6137
diff changeset
   116
					} else {
ea34ef329509 Try to use _NET_WM_ICON if possible for X11's SDL_WM_SetIcon() implementation.
Ryan C. Gordon <icculus@icculus.org>
parents: 6137
diff changeset
   117
						*dst++ = pixel & ~alpha;
ea34ef329509 Try to use _NET_WM_ICON if possible for X11's SDL_WM_SetIcon() implementation.
Ryan C. Gordon <icculus@icculus.org>
parents: 6137
diff changeset
   118
					}
ea34ef329509 Try to use _NET_WM_ICON if possible for X11's SDL_WM_SetIcon() implementation.
Ryan C. Gordon <icculus@icculus.org>
parents: 6137
diff changeset
   119
					maskidx++;
ea34ef329509 Try to use _NET_WM_ICON if possible for X11's SDL_WM_SetIcon() implementation.
Ryan C. Gordon <icculus@icculus.org>
parents: 6137
diff changeset
   120
				}
ea34ef329509 Try to use _NET_WM_ICON if possible for X11's SDL_WM_SetIcon() implementation.
Ryan C. Gordon <icculus@icculus.org>
parents: 6137
diff changeset
   121
			}
ea34ef329509 Try to use _NET_WM_ICON if possible for X11's SDL_WM_SetIcon() implementation.
Ryan C. Gordon <icculus@icculus.org>
parents: 6137
diff changeset
   122
ea34ef329509 Try to use _NET_WM_ICON if possible for X11's SDL_WM_SetIcon() implementation.
Ryan C. Gordon <icculus@icculus.org>
parents: 6137
diff changeset
   123
			XChangeProperty(SDL_Display, WMwindow, _NET_WM_ICON, XA_CARDINAL,
ea34ef329509 Try to use _NET_WM_ICON if possible for X11's SDL_WM_SetIcon() implementation.
Ryan C. Gordon <icculus@icculus.org>
parents: 6137
diff changeset
   124
			                32, PropModeReplace, (unsigned char *) propdata,
ea34ef329509 Try to use _NET_WM_ICON if possible for X11's SDL_WM_SetIcon() implementation.
Ryan C. Gordon <icculus@icculus.org>
parents: 6137
diff changeset
   125
			                propsize);
ea34ef329509 Try to use _NET_WM_ICON if possible for X11's SDL_WM_SetIcon() implementation.
Ryan C. Gordon <icculus@icculus.org>
parents: 6137
diff changeset
   126
		}
ea34ef329509 Try to use _NET_WM_ICON if possible for X11's SDL_WM_SetIcon() implementation.
Ryan C. Gordon <icculus@icculus.org>
parents: 6137
diff changeset
   127
ea34ef329509 Try to use _NET_WM_ICON if possible for X11's SDL_WM_SetIcon() implementation.
Ryan C. Gordon <icculus@icculus.org>
parents: 6137
diff changeset
   128
		SDL_FreeSurface(surface);
ea34ef329509 Try to use _NET_WM_ICON if possible for X11's SDL_WM_SetIcon() implementation.
Ryan C. Gordon <icculus@icculus.org>
parents: 6137
diff changeset
   129
		SDL_free(propdata);
ea34ef329509 Try to use _NET_WM_ICON if possible for X11's SDL_WM_SetIcon() implementation.
Ryan C. Gordon <icculus@icculus.org>
parents: 6137
diff changeset
   130
6563
ba187d0466e6 XFlush() after setting the icon.
Ryan C. Gordon <icculus@icculus.org>
parents: 6561
diff changeset
   131
		XFlush(SDL_Display);
6561
ea34ef329509 Try to use _NET_WM_ICON if possible for X11's SDL_WM_SetIcon() implementation.
Ryan C. Gordon <icculus@icculus.org>
parents: 6137
diff changeset
   132
		SDL_Unlock_EventThread();
ea34ef329509 Try to use _NET_WM_ICON if possible for X11's SDL_WM_SetIcon() implementation.
Ryan C. Gordon <icculus@icculus.org>
parents: 6137
diff changeset
   133
ea34ef329509 Try to use _NET_WM_ICON if possible for X11's SDL_WM_SetIcon() implementation.
Ryan C. Gordon <icculus@icculus.org>
parents: 6137
diff changeset
   134
		return;
ea34ef329509 Try to use _NET_WM_ICON if possible for X11's SDL_WM_SetIcon() implementation.
Ryan C. Gordon <icculus@icculus.org>
parents: 6137
diff changeset
   135
	}
ea34ef329509 Try to use _NET_WM_ICON if possible for X11's SDL_WM_SetIcon() implementation.
Ryan C. Gordon <icculus@icculus.org>
parents: 6137
diff changeset
   136
ea34ef329509 Try to use _NET_WM_ICON if possible for X11's SDL_WM_SetIcon() implementation.
Ryan C. Gordon <icculus@icculus.org>
parents: 6137
diff changeset
   137
	/* Do it the old way... */
ea34ef329509 Try to use _NET_WM_ICON if possible for X11's SDL_WM_SetIcon() implementation.
Ryan C. Gordon <icculus@icculus.org>
parents: 6137
diff changeset
   138
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   139
	/* The icon must use the default visual, depth and colormap of the
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   140
	   screen, so it might need a conversion */
236
3f09f52ac2cc Fixed X11 icon color allocation (thanks Mattias!)
Sam Lantinga <slouken@libsdl.org>
parents: 99
diff changeset
   141
	dvis = DefaultVisual(SDL_Display, SDL_Screen);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   142
	dbpp = DefaultDepth(SDL_Display, SDL_Screen);
236
3f09f52ac2cc Fixed X11 icon color allocation (thanks Mattias!)
Sam Lantinga <slouken@libsdl.org>
parents: 99
diff changeset
   143
	for(i = 0; i < this->hidden->nvisuals; i++) {
3f09f52ac2cc Fixed X11 icon color allocation (thanks Mattias!)
Sam Lantinga <slouken@libsdl.org>
parents: 99
diff changeset
   144
		if(this->hidden->visuals[i].visual == dvis) {
3f09f52ac2cc Fixed X11 icon color allocation (thanks Mattias!)
Sam Lantinga <slouken@libsdl.org>
parents: 99
diff changeset
   145
			dbpp = this->hidden->visuals[i].bpp;
3f09f52ac2cc Fixed X11 icon color allocation (thanks Mattias!)
Sam Lantinga <slouken@libsdl.org>
parents: 99
diff changeset
   146
			break;
3f09f52ac2cc Fixed X11 icon color allocation (thanks Mattias!)
Sam Lantinga <slouken@libsdl.org>
parents: 99
diff changeset
   147
		}
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   148
	}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   149
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   150
	/* The Visual struct is supposed to be opaque but we cheat a little */
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   151
	sicon = SDL_CreateRGBSurface(SDL_SWSURFACE, icon->w, icon->h,
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   152
				     dbpp,
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   153
				     dvis->red_mask, dvis->green_mask,
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   154
				     dvis->blue_mask, 0);
236
3f09f52ac2cc Fixed X11 icon color allocation (thanks Mattias!)
Sam Lantinga <slouken@libsdl.org>
parents: 99
diff changeset
   155
	if ( sicon == NULL )
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   156
		goto done;
236
3f09f52ac2cc Fixed X11 icon color allocation (thanks Mattias!)
Sam Lantinga <slouken@libsdl.org>
parents: 99
diff changeset
   157
3f09f52ac2cc Fixed X11 icon color allocation (thanks Mattias!)
Sam Lantinga <slouken@libsdl.org>
parents: 99
diff changeset
   158
	if(dbpp == 8) {
3f09f52ac2cc Fixed X11 icon color allocation (thanks Mattias!)
Sam Lantinga <slouken@libsdl.org>
parents: 99
diff changeset
   159
		/* Default visual is 8bit; we need to allocate colours from
3f09f52ac2cc Fixed X11 icon color allocation (thanks Mattias!)
Sam Lantinga <slouken@libsdl.org>
parents: 99
diff changeset
   160
		   the default colormap */
3f09f52ac2cc Fixed X11 icon color allocation (thanks Mattias!)
Sam Lantinga <slouken@libsdl.org>
parents: 99
diff changeset
   161
		SDL_Color want[256], got[256];
3f09f52ac2cc Fixed X11 icon color allocation (thanks Mattias!)
Sam Lantinga <slouken@libsdl.org>
parents: 99
diff changeset
   162
		int nwant;
3f09f52ac2cc Fixed X11 icon color allocation (thanks Mattias!)
Sam Lantinga <slouken@libsdl.org>
parents: 99
diff changeset
   163
		Colormap dcmap;
3f09f52ac2cc Fixed X11 icon color allocation (thanks Mattias!)
Sam Lantinga <slouken@libsdl.org>
parents: 99
diff changeset
   164
		int missing;
3f09f52ac2cc Fixed X11 icon color allocation (thanks Mattias!)
Sam Lantinga <slouken@libsdl.org>
parents: 99
diff changeset
   165
		dcmap = DefaultColormap(SDL_Display, SDL_Screen);
3f09f52ac2cc Fixed X11 icon color allocation (thanks Mattias!)
Sam Lantinga <slouken@libsdl.org>
parents: 99
diff changeset
   166
		if(icon->format->palette) {
3f09f52ac2cc Fixed X11 icon color allocation (thanks Mattias!)
Sam Lantinga <slouken@libsdl.org>
parents: 99
diff changeset
   167
			/* The icon has a palette as well - we just have to
3f09f52ac2cc Fixed X11 icon color allocation (thanks Mattias!)
Sam Lantinga <slouken@libsdl.org>
parents: 99
diff changeset
   168
			   find those colours */
3f09f52ac2cc Fixed X11 icon color allocation (thanks Mattias!)
Sam Lantinga <slouken@libsdl.org>
parents: 99
diff changeset
   169
			nwant = icon->format->palette->ncolors;
1336
3692456e7b0f Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents: 1312
diff changeset
   170
			SDL_memcpy(want, icon->format->palette->colors,
236
3f09f52ac2cc Fixed X11 icon color allocation (thanks Mattias!)
Sam Lantinga <slouken@libsdl.org>
parents: 99
diff changeset
   171
			       nwant * sizeof want[0]);
3f09f52ac2cc Fixed X11 icon color allocation (thanks Mattias!)
Sam Lantinga <slouken@libsdl.org>
parents: 99
diff changeset
   172
		} else {
3f09f52ac2cc Fixed X11 icon color allocation (thanks Mattias!)
Sam Lantinga <slouken@libsdl.org>
parents: 99
diff changeset
   173
			/* try the standard 6x6x6 cube for lack of better
3f09f52ac2cc Fixed X11 icon color allocation (thanks Mattias!)
Sam Lantinga <slouken@libsdl.org>
parents: 99
diff changeset
   174
			   ideas */
3f09f52ac2cc Fixed X11 icon color allocation (thanks Mattias!)
Sam Lantinga <slouken@libsdl.org>
parents: 99
diff changeset
   175
			int r, g, b, i;
3f09f52ac2cc Fixed X11 icon color allocation (thanks Mattias!)
Sam Lantinga <slouken@libsdl.org>
parents: 99
diff changeset
   176
			for(r = i = 0; r < 256; r += 0x33)
3f09f52ac2cc Fixed X11 icon color allocation (thanks Mattias!)
Sam Lantinga <slouken@libsdl.org>
parents: 99
diff changeset
   177
				for(g = 0; g < 256; g += 0x33)
3f09f52ac2cc Fixed X11 icon color allocation (thanks Mattias!)
Sam Lantinga <slouken@libsdl.org>
parents: 99
diff changeset
   178
					for(b = 0; b < 256; b += 0x33, i++) {
3f09f52ac2cc Fixed X11 icon color allocation (thanks Mattias!)
Sam Lantinga <slouken@libsdl.org>
parents: 99
diff changeset
   179
						want[i].r = r;
3f09f52ac2cc Fixed X11 icon color allocation (thanks Mattias!)
Sam Lantinga <slouken@libsdl.org>
parents: 99
diff changeset
   180
						want[i].g = g;
3f09f52ac2cc Fixed X11 icon color allocation (thanks Mattias!)
Sam Lantinga <slouken@libsdl.org>
parents: 99
diff changeset
   181
						want[i].b = b;
3f09f52ac2cc Fixed X11 icon color allocation (thanks Mattias!)
Sam Lantinga <slouken@libsdl.org>
parents: 99
diff changeset
   182
					}
3f09f52ac2cc Fixed X11 icon color allocation (thanks Mattias!)
Sam Lantinga <slouken@libsdl.org>
parents: 99
diff changeset
   183
			nwant = 216;
3f09f52ac2cc Fixed X11 icon color allocation (thanks Mattias!)
Sam Lantinga <slouken@libsdl.org>
parents: 99
diff changeset
   184
		}
3f09f52ac2cc Fixed X11 icon color allocation (thanks Mattias!)
Sam Lantinga <slouken@libsdl.org>
parents: 99
diff changeset
   185
		if(SDL_iconcolors) {
3f09f52ac2cc Fixed X11 icon color allocation (thanks Mattias!)
Sam Lantinga <slouken@libsdl.org>
parents: 99
diff changeset
   186
			/* free already allocated colours first */
3f09f52ac2cc Fixed X11 icon color allocation (thanks Mattias!)
Sam Lantinga <slouken@libsdl.org>
parents: 99
diff changeset
   187
			unsigned long freelist[512];
3f09f52ac2cc Fixed X11 icon color allocation (thanks Mattias!)
Sam Lantinga <slouken@libsdl.org>
parents: 99
diff changeset
   188
			int nfree = 0;
3f09f52ac2cc Fixed X11 icon color allocation (thanks Mattias!)
Sam Lantinga <slouken@libsdl.org>
parents: 99
diff changeset
   189
			for(i = 0; i < 256; i++) {
3f09f52ac2cc Fixed X11 icon color allocation (thanks Mattias!)
Sam Lantinga <slouken@libsdl.org>
parents: 99
diff changeset
   190
				while(SDL_iconcolors[i]) {
3f09f52ac2cc Fixed X11 icon color allocation (thanks Mattias!)
Sam Lantinga <slouken@libsdl.org>
parents: 99
diff changeset
   191
					freelist[nfree++] = i;
3f09f52ac2cc Fixed X11 icon color allocation (thanks Mattias!)
Sam Lantinga <slouken@libsdl.org>
parents: 99
diff changeset
   192
					SDL_iconcolors[i]--;
3f09f52ac2cc Fixed X11 icon color allocation (thanks Mattias!)
Sam Lantinga <slouken@libsdl.org>
parents: 99
diff changeset
   193
				}
3f09f52ac2cc Fixed X11 icon color allocation (thanks Mattias!)
Sam Lantinga <slouken@libsdl.org>
parents: 99
diff changeset
   194
			}
1575
3ba88cb7eb1b Updated dynamic X11 code. See details in Bugzilla #170.
Ryan C. Gordon <icculus@icculus.org>
parents: 1558
diff changeset
   195
			XFreeColors(GFX_Display, dcmap, freelist, nfree, 0);
236
3f09f52ac2cc Fixed X11 icon color allocation (thanks Mattias!)
Sam Lantinga <slouken@libsdl.org>
parents: 99
diff changeset
   196
		}
3f09f52ac2cc Fixed X11 icon color allocation (thanks Mattias!)
Sam Lantinga <slouken@libsdl.org>
parents: 99
diff changeset
   197
		if(!SDL_iconcolors)
1336
3692456e7b0f Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents: 1312
diff changeset
   198
			SDL_iconcolors = SDL_malloc(256 * sizeof *SDL_iconcolors);
3692456e7b0f Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents: 1312
diff changeset
   199
		SDL_memset(SDL_iconcolors, 0, 256 * sizeof *SDL_iconcolors);
236
3f09f52ac2cc Fixed X11 icon color allocation (thanks Mattias!)
Sam Lantinga <slouken@libsdl.org>
parents: 99
diff changeset
   200
3f09f52ac2cc Fixed X11 icon color allocation (thanks Mattias!)
Sam Lantinga <slouken@libsdl.org>
parents: 99
diff changeset
   201
		/* try to allocate the colours */
1336
3692456e7b0f Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents: 1312
diff changeset
   202
		SDL_memset(got, 0, sizeof got);
236
3f09f52ac2cc Fixed X11 icon color allocation (thanks Mattias!)
Sam Lantinga <slouken@libsdl.org>
parents: 99
diff changeset
   203
		missing = 0;
3f09f52ac2cc Fixed X11 icon color allocation (thanks Mattias!)
Sam Lantinga <slouken@libsdl.org>
parents: 99
diff changeset
   204
		for(i = 0; i < nwant; i++) {
3f09f52ac2cc Fixed X11 icon color allocation (thanks Mattias!)
Sam Lantinga <slouken@libsdl.org>
parents: 99
diff changeset
   205
			XColor c;
3f09f52ac2cc Fixed X11 icon color allocation (thanks Mattias!)
Sam Lantinga <slouken@libsdl.org>
parents: 99
diff changeset
   206
			c.red = want[i].r << 8;
3f09f52ac2cc Fixed X11 icon color allocation (thanks Mattias!)
Sam Lantinga <slouken@libsdl.org>
parents: 99
diff changeset
   207
			c.green = want[i].g << 8;
3f09f52ac2cc Fixed X11 icon color allocation (thanks Mattias!)
Sam Lantinga <slouken@libsdl.org>
parents: 99
diff changeset
   208
			c.blue = want[i].b << 8;
3f09f52ac2cc Fixed X11 icon color allocation (thanks Mattias!)
Sam Lantinga <slouken@libsdl.org>
parents: 99
diff changeset
   209
			c.flags = DoRed | DoGreen | DoBlue;
1575
3ba88cb7eb1b Updated dynamic X11 code. See details in Bugzilla #170.
Ryan C. Gordon <icculus@icculus.org>
parents: 1558
diff changeset
   210
			if(XAllocColor(GFX_Display, dcmap, &c)) {
236
3f09f52ac2cc Fixed X11 icon color allocation (thanks Mattias!)
Sam Lantinga <slouken@libsdl.org>
parents: 99
diff changeset
   211
				/* got the colour */
3f09f52ac2cc Fixed X11 icon color allocation (thanks Mattias!)
Sam Lantinga <slouken@libsdl.org>
parents: 99
diff changeset
   212
				SDL_iconcolors[c.pixel]++;
3f09f52ac2cc Fixed X11 icon color allocation (thanks Mattias!)
Sam Lantinga <slouken@libsdl.org>
parents: 99
diff changeset
   213
				got[c.pixel] = want[i];
3f09f52ac2cc Fixed X11 icon color allocation (thanks Mattias!)
Sam Lantinga <slouken@libsdl.org>
parents: 99
diff changeset
   214
			} else {
3f09f52ac2cc Fixed X11 icon color allocation (thanks Mattias!)
Sam Lantinga <slouken@libsdl.org>
parents: 99
diff changeset
   215
				missing = 1;
3f09f52ac2cc Fixed X11 icon color allocation (thanks Mattias!)
Sam Lantinga <slouken@libsdl.org>
parents: 99
diff changeset
   216
			}
3f09f52ac2cc Fixed X11 icon color allocation (thanks Mattias!)
Sam Lantinga <slouken@libsdl.org>
parents: 99
diff changeset
   217
		}
3f09f52ac2cc Fixed X11 icon color allocation (thanks Mattias!)
Sam Lantinga <slouken@libsdl.org>
parents: 99
diff changeset
   218
		if(missing) {
3f09f52ac2cc Fixed X11 icon color allocation (thanks Mattias!)
Sam Lantinga <slouken@libsdl.org>
parents: 99
diff changeset
   219
			/* Some colours were apparently missing, so we just
3f09f52ac2cc Fixed X11 icon color allocation (thanks Mattias!)
Sam Lantinga <slouken@libsdl.org>
parents: 99
diff changeset
   220
			   allocate all the rest as well */
3f09f52ac2cc Fixed X11 icon color allocation (thanks Mattias!)
Sam Lantinga <slouken@libsdl.org>
parents: 99
diff changeset
   221
			XColor cols[256];
3f09f52ac2cc Fixed X11 icon color allocation (thanks Mattias!)
Sam Lantinga <slouken@libsdl.org>
parents: 99
diff changeset
   222
			for(i = 0; i < 256; i++)
3f09f52ac2cc Fixed X11 icon color allocation (thanks Mattias!)
Sam Lantinga <slouken@libsdl.org>
parents: 99
diff changeset
   223
				cols[i].pixel = i;
1575
3ba88cb7eb1b Updated dynamic X11 code. See details in Bugzilla #170.
Ryan C. Gordon <icculus@icculus.org>
parents: 1558
diff changeset
   224
			XQueryColors(GFX_Display, dcmap, cols, 256);
236
3f09f52ac2cc Fixed X11 icon color allocation (thanks Mattias!)
Sam Lantinga <slouken@libsdl.org>
parents: 99
diff changeset
   225
			for(i = 0; i < 256; i++) {
3f09f52ac2cc Fixed X11 icon color allocation (thanks Mattias!)
Sam Lantinga <slouken@libsdl.org>
parents: 99
diff changeset
   226
				got[i].r = cols[i].red >> 8;
3f09f52ac2cc Fixed X11 icon color allocation (thanks Mattias!)
Sam Lantinga <slouken@libsdl.org>
parents: 99
diff changeset
   227
				got[i].g = cols[i].green >> 8;
3f09f52ac2cc Fixed X11 icon color allocation (thanks Mattias!)
Sam Lantinga <slouken@libsdl.org>
parents: 99
diff changeset
   228
				got[i].b = cols[i].blue >> 8;
3f09f52ac2cc Fixed X11 icon color allocation (thanks Mattias!)
Sam Lantinga <slouken@libsdl.org>
parents: 99
diff changeset
   229
				if(!SDL_iconcolors[i]) {
1575
3ba88cb7eb1b Updated dynamic X11 code. See details in Bugzilla #170.
Ryan C. Gordon <icculus@icculus.org>
parents: 1558
diff changeset
   230
					if(XAllocColor(GFX_Display, dcmap,
236
3f09f52ac2cc Fixed X11 icon color allocation (thanks Mattias!)
Sam Lantinga <slouken@libsdl.org>
parents: 99
diff changeset
   231
							cols + i)) {
3f09f52ac2cc Fixed X11 icon color allocation (thanks Mattias!)
Sam Lantinga <slouken@libsdl.org>
parents: 99
diff changeset
   232
						SDL_iconcolors[i] = 1;
3f09f52ac2cc Fixed X11 icon color allocation (thanks Mattias!)
Sam Lantinga <slouken@libsdl.org>
parents: 99
diff changeset
   233
					} else {
3f09f52ac2cc Fixed X11 icon color allocation (thanks Mattias!)
Sam Lantinga <slouken@libsdl.org>
parents: 99
diff changeset
   234
						/* index not available */
3f09f52ac2cc Fixed X11 icon color allocation (thanks Mattias!)
Sam Lantinga <slouken@libsdl.org>
parents: 99
diff changeset
   235
						got[i].r = 0;
3f09f52ac2cc Fixed X11 icon color allocation (thanks Mattias!)
Sam Lantinga <slouken@libsdl.org>
parents: 99
diff changeset
   236
						got[i].g = 0;
3f09f52ac2cc Fixed X11 icon color allocation (thanks Mattias!)
Sam Lantinga <slouken@libsdl.org>
parents: 99
diff changeset
   237
						got[i].b = 0;
3f09f52ac2cc Fixed X11 icon color allocation (thanks Mattias!)
Sam Lantinga <slouken@libsdl.org>
parents: 99
diff changeset
   238
					}
3f09f52ac2cc Fixed X11 icon color allocation (thanks Mattias!)
Sam Lantinga <slouken@libsdl.org>
parents: 99
diff changeset
   239
				}
3f09f52ac2cc Fixed X11 icon color allocation (thanks Mattias!)
Sam Lantinga <slouken@libsdl.org>
parents: 99
diff changeset
   240
			}
3f09f52ac2cc Fixed X11 icon color allocation (thanks Mattias!)
Sam Lantinga <slouken@libsdl.org>
parents: 99
diff changeset
   241
		}
3f09f52ac2cc Fixed X11 icon color allocation (thanks Mattias!)
Sam Lantinga <slouken@libsdl.org>
parents: 99
diff changeset
   242
3f09f52ac2cc Fixed X11 icon color allocation (thanks Mattias!)
Sam Lantinga <slouken@libsdl.org>
parents: 99
diff changeset
   243
		SDL_SetColors(sicon, got, 0, 256);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   244
	}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   245
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   246
	bounds.x = 0;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   247
	bounds.y = 0;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   248
	bounds.w = icon->w;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   249
	bounds.h = icon->h;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   250
	if ( SDL_LowerBlit(icon, &bounds, sicon, &bounds) < 0 )
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   251
		goto done;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   252
236
3f09f52ac2cc Fixed X11 icon color allocation (thanks Mattias!)
Sam Lantinga <slouken@libsdl.org>
parents: 99
diff changeset
   253
	/* We need the mask as given, except in LSBfirst format instead of
3f09f52ac2cc Fixed X11 icon color allocation (thanks Mattias!)
Sam Lantinga <slouken@libsdl.org>
parents: 99
diff changeset
   254
	   MSBfirst. Reverse the bits in each byte. */
3f09f52ac2cc Fixed X11 icon color allocation (thanks Mattias!)
Sam Lantinga <slouken@libsdl.org>
parents: 99
diff changeset
   255
	masksize = ((sicon->w + 7) >> 3) * sicon->h;
1336
3692456e7b0f Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents: 1312
diff changeset
   256
	LSBmask = SDL_malloc(masksize);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   257
	if ( LSBmask == NULL ) {
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   258
		goto done;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   259
	}
1336
3692456e7b0f Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents: 1312
diff changeset
   260
	SDL_memset(LSBmask, 0, masksize);
236
3f09f52ac2cc Fixed X11 icon color allocation (thanks Mattias!)
Sam Lantinga <slouken@libsdl.org>
parents: 99
diff changeset
   261
	for(i = 0; i < masksize; i++)
3f09f52ac2cc Fixed X11 icon color allocation (thanks Mattias!)
Sam Lantinga <slouken@libsdl.org>
parents: 99
diff changeset
   262
		LSBmask[i] = reverse_byte(mask[i]);
1575
3ba88cb7eb1b Updated dynamic X11 code. See details in Bugzilla #170.
Ryan C. Gordon <icculus@icculus.org>
parents: 1558
diff changeset
   263
	mask_pixmap = XCreatePixmapFromBitmapData(SDL_Display, WMwindow,
236
3f09f52ac2cc Fixed X11 icon color allocation (thanks Mattias!)
Sam Lantinga <slouken@libsdl.org>
parents: 99
diff changeset
   264
						  (char *)LSBmask,
3f09f52ac2cc Fixed X11 icon color allocation (thanks Mattias!)
Sam Lantinga <slouken@libsdl.org>
parents: 99
diff changeset
   265
						  sicon->w, sicon->h,
3f09f52ac2cc Fixed X11 icon color allocation (thanks Mattias!)
Sam Lantinga <slouken@libsdl.org>
parents: 99
diff changeset
   266
						  1L, 0L, 1);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   267
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   268
	/* Transfer the image to an X11 pixmap */
1575
3ba88cb7eb1b Updated dynamic X11 code. See details in Bugzilla #170.
Ryan C. Gordon <icculus@icculus.org>
parents: 1558
diff changeset
   269
	icon_image = XCreateImage(SDL_Display,
236
3f09f52ac2cc Fixed X11 icon color allocation (thanks Mattias!)
Sam Lantinga <slouken@libsdl.org>
parents: 99
diff changeset
   270
				  DefaultVisual(SDL_Display, SDL_Screen),
3f09f52ac2cc Fixed X11 icon color allocation (thanks Mattias!)
Sam Lantinga <slouken@libsdl.org>
parents: 99
diff changeset
   271
				  DefaultDepth(SDL_Display, SDL_Screen),
3f09f52ac2cc Fixed X11 icon color allocation (thanks Mattias!)
Sam Lantinga <slouken@libsdl.org>
parents: 99
diff changeset
   272
				  ZPixmap, 0, sicon->pixels,
3f09f52ac2cc Fixed X11 icon color allocation (thanks Mattias!)
Sam Lantinga <slouken@libsdl.org>
parents: 99
diff changeset
   273
				  sicon->w, sicon->h,
3f09f52ac2cc Fixed X11 icon color allocation (thanks Mattias!)
Sam Lantinga <slouken@libsdl.org>
parents: 99
diff changeset
   274
				  32, 0);
246
7c09c9e3b0c7 From: "Mattias Engdeg�rd" <f91-men@nada.kth.se>
Sam Lantinga <slouken@libsdl.org>
parents: 236
diff changeset
   275
	icon_image->byte_order = (SDL_BYTEORDER == SDL_BIG_ENDIAN)
7c09c9e3b0c7 From: "Mattias Engdeg�rd" <f91-men@nada.kth.se>
Sam Lantinga <slouken@libsdl.org>
parents: 236
diff changeset
   276
		                 ? MSBFirst : LSBFirst;
1575
3ba88cb7eb1b Updated dynamic X11 code. See details in Bugzilla #170.
Ryan C. Gordon <icculus@icculus.org>
parents: 1558
diff changeset
   277
	icon_pixmap = XCreatePixmap(SDL_Display, SDL_Root, sicon->w, sicon->h,
236
3f09f52ac2cc Fixed X11 icon color allocation (thanks Mattias!)
Sam Lantinga <slouken@libsdl.org>
parents: 99
diff changeset
   278
				    DefaultDepth(SDL_Display, SDL_Screen));
1575
3ba88cb7eb1b Updated dynamic X11 code. See details in Bugzilla #170.
Ryan C. Gordon <icculus@icculus.org>
parents: 1558
diff changeset
   279
	gc = XCreateGC(SDL_Display, icon_pixmap, 0, &GCvalues);
3ba88cb7eb1b Updated dynamic X11 code. See details in Bugzilla #170.
Ryan C. Gordon <icculus@icculus.org>
parents: 1558
diff changeset
   280
	XPutImage(SDL_Display, icon_pixmap, gc, icon_image,
236
3f09f52ac2cc Fixed X11 icon color allocation (thanks Mattias!)
Sam Lantinga <slouken@libsdl.org>
parents: 99
diff changeset
   281
		  0, 0, 0, 0, sicon->w, sicon->h);
1575
3ba88cb7eb1b Updated dynamic X11 code. See details in Bugzilla #170.
Ryan C. Gordon <icculus@icculus.org>
parents: 1558
diff changeset
   282
	XFreeGC(SDL_Display, gc);
3ba88cb7eb1b Updated dynamic X11 code. See details in Bugzilla #170.
Ryan C. Gordon <icculus@icculus.org>
parents: 1558
diff changeset
   283
	XDestroyImage(icon_image);
1336
3692456e7b0f Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents: 1312
diff changeset
   284
	SDL_free(LSBmask);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   285
	sicon->pixels = NULL;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   286
236
3f09f52ac2cc Fixed X11 icon color allocation (thanks Mattias!)
Sam Lantinga <slouken@libsdl.org>
parents: 99
diff changeset
   287
	/* Some buggy window managers (some versions of Enlightenment, it
3f09f52ac2cc Fixed X11 icon color allocation (thanks Mattias!)
Sam Lantinga <slouken@libsdl.org>
parents: 99
diff changeset
   288
	   seems) need an icon window *and* icon pixmap to work properly, while
3f09f52ac2cc Fixed X11 icon color allocation (thanks Mattias!)
Sam Lantinga <slouken@libsdl.org>
parents: 99
diff changeset
   289
	   it screws up others. The default is only to use a pixmap. */
1336
3692456e7b0f Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents: 1312
diff changeset
   290
	p = SDL_getenv("SDL_VIDEO_X11_ICONWIN");
236
3f09f52ac2cc Fixed X11 icon color allocation (thanks Mattias!)
Sam Lantinga <slouken@libsdl.org>
parents: 99
diff changeset
   291
	if(p && *p) {
1575
3ba88cb7eb1b Updated dynamic X11 code. See details in Bugzilla #170.
Ryan C. Gordon <icculus@icculus.org>
parents: 1558
diff changeset
   292
		icon_window = XCreateSimpleWindow(SDL_Display, SDL_Root,
236
3f09f52ac2cc Fixed X11 icon color allocation (thanks Mattias!)
Sam Lantinga <slouken@libsdl.org>
parents: 99
diff changeset
   293
						  0, 0, sicon->w, sicon->h, 0,
3f09f52ac2cc Fixed X11 icon color allocation (thanks Mattias!)
Sam Lantinga <slouken@libsdl.org>
parents: 99
diff changeset
   294
						  CopyFromParent,
3f09f52ac2cc Fixed X11 icon color allocation (thanks Mattias!)
Sam Lantinga <slouken@libsdl.org>
parents: 99
diff changeset
   295
						  CopyFromParent);
1575
3ba88cb7eb1b Updated dynamic X11 code. See details in Bugzilla #170.
Ryan C. Gordon <icculus@icculus.org>
parents: 1558
diff changeset
   296
		XSetWindowBackgroundPixmap(SDL_Display, icon_window,
236
3f09f52ac2cc Fixed X11 icon color allocation (thanks Mattias!)
Sam Lantinga <slouken@libsdl.org>
parents: 99
diff changeset
   297
					   icon_pixmap);
1575
3ba88cb7eb1b Updated dynamic X11 code. See details in Bugzilla #170.
Ryan C. Gordon <icculus@icculus.org>
parents: 1558
diff changeset
   298
		XClearWindow(SDL_Display, icon_window);
236
3f09f52ac2cc Fixed X11 icon color allocation (thanks Mattias!)
Sam Lantinga <slouken@libsdl.org>
parents: 99
diff changeset
   299
	}
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   300
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   301
	/* Set the window icon to the icon pixmap (and icon window) */
1575
3ba88cb7eb1b Updated dynamic X11 code. See details in Bugzilla #170.
Ryan C. Gordon <icculus@icculus.org>
parents: 1558
diff changeset
   302
	wmhints = XAllocWMHints();
4247
ee58103d5354 Fixed bug #586
Sam Lantinga <slouken@libsdl.org>
parents: 4159
diff changeset
   303
	wmhints->flags = (IconPixmapHint | IconMaskHint | InputHint);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   304
	wmhints->icon_pixmap = icon_pixmap;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   305
	wmhints->icon_mask = mask_pixmap;
4247
ee58103d5354 Fixed bug #586
Sam Lantinga <slouken@libsdl.org>
parents: 4159
diff changeset
   306
	wmhints->input = True;
236
3f09f52ac2cc Fixed X11 icon color allocation (thanks Mattias!)
Sam Lantinga <slouken@libsdl.org>
parents: 99
diff changeset
   307
	if(icon_window != None) {
3f09f52ac2cc Fixed X11 icon color allocation (thanks Mattias!)
Sam Lantinga <slouken@libsdl.org>
parents: 99
diff changeset
   308
		wmhints->flags |= IconWindowHint;
3f09f52ac2cc Fixed X11 icon color allocation (thanks Mattias!)
Sam Lantinga <slouken@libsdl.org>
parents: 99
diff changeset
   309
		wmhints->icon_window = icon_window;
3f09f52ac2cc Fixed X11 icon color allocation (thanks Mattias!)
Sam Lantinga <slouken@libsdl.org>
parents: 99
diff changeset
   310
	}
1575
3ba88cb7eb1b Updated dynamic X11 code. See details in Bugzilla #170.
Ryan C. Gordon <icculus@icculus.org>
parents: 1558
diff changeset
   311
	XSetWMHints(SDL_Display, WMwindow, wmhints);
3ba88cb7eb1b Updated dynamic X11 code. See details in Bugzilla #170.
Ryan C. Gordon <icculus@icculus.org>
parents: 1558
diff changeset
   312
	XFree(wmhints);
3ba88cb7eb1b Updated dynamic X11 code. See details in Bugzilla #170.
Ryan C. Gordon <icculus@icculus.org>
parents: 1558
diff changeset
   313
	XSync(SDL_Display, False);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   314
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   315
  done:
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   316
	SDL_Unlock_EventThread();
236
3f09f52ac2cc Fixed X11 icon color allocation (thanks Mattias!)
Sam Lantinga <slouken@libsdl.org>
parents: 99
diff changeset
   317
	SDL_FreeSurface(sicon);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   318
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   319
1767
ae9f6be81810 Fixed bug #196
Sam Lantinga <slouken@libsdl.org>
parents: 1765
diff changeset
   320
void X11_SetCaptionNoLock(_THIS, const char *title, const char *icon)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   321
{
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   322
	XTextProperty titleprop, iconprop;
1558
b46bb79cc197 Fixed bug #113:
Sam Lantinga <slouken@libsdl.org>
parents: 1505
diff changeset
   323
	Status status;
b46bb79cc197 Fixed bug #113:
Sam Lantinga <slouken@libsdl.org>
parents: 1505
diff changeset
   324
b46bb79cc197 Fixed bug #113:
Sam Lantinga <slouken@libsdl.org>
parents: 1505
diff changeset
   325
#ifdef X_HAVE_UTF8_STRING
1849
b5a4ac87b98c Fixed uninitialized variable warnings
Sam Lantinga <slouken@libsdl.org>
parents: 1767
diff changeset
   326
	Atom _NET_WM_NAME = 0;
b5a4ac87b98c Fixed uninitialized variable warnings
Sam Lantinga <slouken@libsdl.org>
parents: 1767
diff changeset
   327
	Atom _NET_WM_ICON_NAME = 0;
1558
b46bb79cc197 Fixed bug #113:
Sam Lantinga <slouken@libsdl.org>
parents: 1505
diff changeset
   328
b46bb79cc197 Fixed bug #113:
Sam Lantinga <slouken@libsdl.org>
parents: 1505
diff changeset
   329
	/* Look up some useful Atoms */
1575
3ba88cb7eb1b Updated dynamic X11 code. See details in Bugzilla #170.
Ryan C. Gordon <icculus@icculus.org>
parents: 1558
diff changeset
   330
	if (SDL_X11_HAVE_UTF8) {
3ba88cb7eb1b Updated dynamic X11 code. See details in Bugzilla #170.
Ryan C. Gordon <icculus@icculus.org>
parents: 1558
diff changeset
   331
		_NET_WM_NAME = XInternAtom(SDL_Display, "_NET_WM_NAME", False);
3ba88cb7eb1b Updated dynamic X11 code. See details in Bugzilla #170.
Ryan C. Gordon <icculus@icculus.org>
parents: 1558
diff changeset
   332
		_NET_WM_ICON_NAME = XInternAtom(SDL_Display, "_NET_WM_ICON_NAME", False);
3ba88cb7eb1b Updated dynamic X11 code. See details in Bugzilla #170.
Ryan C. Gordon <icculus@icculus.org>
parents: 1558
diff changeset
   333
	}
1558
b46bb79cc197 Fixed bug #113:
Sam Lantinga <slouken@libsdl.org>
parents: 1505
diff changeset
   334
#endif
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   335
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   336
	if ( title != NULL ) {
3998
098ac044cd2f Fixed bug #447
Sam Lantinga <slouken@libsdl.org>
parents: 3896
diff changeset
   337
		char *title_locale = SDL_iconv_utf8_locale(title);
098ac044cd2f Fixed bug #447
Sam Lantinga <slouken@libsdl.org>
parents: 3896
diff changeset
   338
		if ( !title_locale ) {
1558
b46bb79cc197 Fixed bug #113:
Sam Lantinga <slouken@libsdl.org>
parents: 1505
diff changeset
   339
			SDL_OutOfMemory();
b46bb79cc197 Fixed bug #113:
Sam Lantinga <slouken@libsdl.org>
parents: 1505
diff changeset
   340
			return;
b46bb79cc197 Fixed bug #113:
Sam Lantinga <slouken@libsdl.org>
parents: 1505
diff changeset
   341
		}
3998
098ac044cd2f Fixed bug #447
Sam Lantinga <slouken@libsdl.org>
parents: 3896
diff changeset
   342
		status = XStringListToTextProperty(&title_locale, 1, &titleprop);
098ac044cd2f Fixed bug #447
Sam Lantinga <slouken@libsdl.org>
parents: 3896
diff changeset
   343
		SDL_free(title_locale);
1558
b46bb79cc197 Fixed bug #113:
Sam Lantinga <slouken@libsdl.org>
parents: 1505
diff changeset
   344
		if ( status ) {
1575
3ba88cb7eb1b Updated dynamic X11 code. See details in Bugzilla #170.
Ryan C. Gordon <icculus@icculus.org>
parents: 1558
diff changeset
   345
			XSetTextProperty(SDL_Display, WMwindow, &titleprop, XA_WM_NAME);
3ba88cb7eb1b Updated dynamic X11 code. See details in Bugzilla #170.
Ryan C. Gordon <icculus@icculus.org>
parents: 1558
diff changeset
   346
			XFree(titleprop.value);
1558
b46bb79cc197 Fixed bug #113:
Sam Lantinga <slouken@libsdl.org>
parents: 1505
diff changeset
   347
		}
913
a7a8c282d62e Date: Mon, 28 Jun 2004 23:15:55 +0200
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
   348
#ifdef X_HAVE_UTF8_STRING
1575
3ba88cb7eb1b Updated dynamic X11 code. See details in Bugzilla #170.
Ryan C. Gordon <icculus@icculus.org>
parents: 1558
diff changeset
   349
		if (SDL_X11_HAVE_UTF8) {
3ba88cb7eb1b Updated dynamic X11 code. See details in Bugzilla #170.
Ryan C. Gordon <icculus@icculus.org>
parents: 1558
diff changeset
   350
			status = Xutf8TextListToTextProperty(SDL_Display,
3ba88cb7eb1b Updated dynamic X11 code. See details in Bugzilla #170.
Ryan C. Gordon <icculus@icculus.org>
parents: 1558
diff changeset
   351
					(char **)&title, 1, XUTF8StringStyle, &titleprop);
3ba88cb7eb1b Updated dynamic X11 code. See details in Bugzilla #170.
Ryan C. Gordon <icculus@icculus.org>
parents: 1558
diff changeset
   352
			if ( status == Success ) {
3ba88cb7eb1b Updated dynamic X11 code. See details in Bugzilla #170.
Ryan C. Gordon <icculus@icculus.org>
parents: 1558
diff changeset
   353
				XSetTextProperty(SDL_Display, WMwindow, &titleprop, _NET_WM_NAME);
3ba88cb7eb1b Updated dynamic X11 code. See details in Bugzilla #170.
Ryan C. Gordon <icculus@icculus.org>
parents: 1558
diff changeset
   354
				XFree(titleprop.value);
3ba88cb7eb1b Updated dynamic X11 code. See details in Bugzilla #170.
Ryan C. Gordon <icculus@icculus.org>
parents: 1558
diff changeset
   355
			}
1558
b46bb79cc197 Fixed bug #113:
Sam Lantinga <slouken@libsdl.org>
parents: 1505
diff changeset
   356
		}
913
a7a8c282d62e Date: Mon, 28 Jun 2004 23:15:55 +0200
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
   357
#endif
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   358
	}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   359
	if ( icon != NULL ) {
3998
098ac044cd2f Fixed bug #447
Sam Lantinga <slouken@libsdl.org>
parents: 3896
diff changeset
   360
		char *icon_locale = SDL_iconv_utf8_locale(icon);
098ac044cd2f Fixed bug #447
Sam Lantinga <slouken@libsdl.org>
parents: 3896
diff changeset
   361
		if ( !icon_locale ) {
1558
b46bb79cc197 Fixed bug #113:
Sam Lantinga <slouken@libsdl.org>
parents: 1505
diff changeset
   362
			SDL_OutOfMemory();
b46bb79cc197 Fixed bug #113:
Sam Lantinga <slouken@libsdl.org>
parents: 1505
diff changeset
   363
			return;
b46bb79cc197 Fixed bug #113:
Sam Lantinga <slouken@libsdl.org>
parents: 1505
diff changeset
   364
		}
3998
098ac044cd2f Fixed bug #447
Sam Lantinga <slouken@libsdl.org>
parents: 3896
diff changeset
   365
		status = XStringListToTextProperty(&icon_locale, 1, &iconprop);
098ac044cd2f Fixed bug #447
Sam Lantinga <slouken@libsdl.org>
parents: 3896
diff changeset
   366
		SDL_free(icon_locale);
1558
b46bb79cc197 Fixed bug #113:
Sam Lantinga <slouken@libsdl.org>
parents: 1505
diff changeset
   367
		if ( status ) {
1575
3ba88cb7eb1b Updated dynamic X11 code. See details in Bugzilla #170.
Ryan C. Gordon <icculus@icculus.org>
parents: 1558
diff changeset
   368
			XSetTextProperty(SDL_Display, WMwindow, &iconprop, XA_WM_ICON_NAME);
3ba88cb7eb1b Updated dynamic X11 code. See details in Bugzilla #170.
Ryan C. Gordon <icculus@icculus.org>
parents: 1558
diff changeset
   369
			XFree(iconprop.value);
1558
b46bb79cc197 Fixed bug #113:
Sam Lantinga <slouken@libsdl.org>
parents: 1505
diff changeset
   370
		}
913
a7a8c282d62e Date: Mon, 28 Jun 2004 23:15:55 +0200
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
   371
#ifdef X_HAVE_UTF8_STRING
1575
3ba88cb7eb1b Updated dynamic X11 code. See details in Bugzilla #170.
Ryan C. Gordon <icculus@icculus.org>
parents: 1558
diff changeset
   372
		if (SDL_X11_HAVE_UTF8) {
3ba88cb7eb1b Updated dynamic X11 code. See details in Bugzilla #170.
Ryan C. Gordon <icculus@icculus.org>
parents: 1558
diff changeset
   373
			status = Xutf8TextListToTextProperty(SDL_Display,
3ba88cb7eb1b Updated dynamic X11 code. See details in Bugzilla #170.
Ryan C. Gordon <icculus@icculus.org>
parents: 1558
diff changeset
   374
					(char **)&icon, 1, XUTF8StringStyle, &iconprop);
3ba88cb7eb1b Updated dynamic X11 code. See details in Bugzilla #170.
Ryan C. Gordon <icculus@icculus.org>
parents: 1558
diff changeset
   375
			if ( status == Success ) {
3ba88cb7eb1b Updated dynamic X11 code. See details in Bugzilla #170.
Ryan C. Gordon <icculus@icculus.org>
parents: 1558
diff changeset
   376
				XSetTextProperty(SDL_Display, WMwindow, &iconprop, _NET_WM_ICON_NAME);
3ba88cb7eb1b Updated dynamic X11 code. See details in Bugzilla #170.
Ryan C. Gordon <icculus@icculus.org>
parents: 1558
diff changeset
   377
				XFree(iconprop.value);
3ba88cb7eb1b Updated dynamic X11 code. See details in Bugzilla #170.
Ryan C. Gordon <icculus@icculus.org>
parents: 1558
diff changeset
   378
			}
1558
b46bb79cc197 Fixed bug #113:
Sam Lantinga <slouken@libsdl.org>
parents: 1505
diff changeset
   379
		}
913
a7a8c282d62e Date: Mon, 28 Jun 2004 23:15:55 +0200
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
   380
#endif
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   381
	}
1575
3ba88cb7eb1b Updated dynamic X11 code. See details in Bugzilla #170.
Ryan C. Gordon <icculus@icculus.org>
parents: 1558
diff changeset
   382
	XSync(SDL_Display, False);
1767
ae9f6be81810 Fixed bug #196
Sam Lantinga <slouken@libsdl.org>
parents: 1765
diff changeset
   383
}
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   384
1767
ae9f6be81810 Fixed bug #196
Sam Lantinga <slouken@libsdl.org>
parents: 1765
diff changeset
   385
void X11_SetCaption(_THIS, const char *title, const char *icon)
ae9f6be81810 Fixed bug #196
Sam Lantinga <slouken@libsdl.org>
parents: 1765
diff changeset
   386
{
ae9f6be81810 Fixed bug #196
Sam Lantinga <slouken@libsdl.org>
parents: 1765
diff changeset
   387
	SDL_Lock_EventThread();
ae9f6be81810 Fixed bug #196
Sam Lantinga <slouken@libsdl.org>
parents: 1765
diff changeset
   388
	X11_SetCaptionNoLock(this, title, icon);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   389
	SDL_Unlock_EventThread();
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   390
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   391
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   392
/* Iconify the window */
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   393
int X11_IconifyWindow(_THIS)
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   394
{
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   395
	int result;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   396
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   397
	SDL_Lock_EventThread();
1575
3ba88cb7eb1b Updated dynamic X11 code. See details in Bugzilla #170.
Ryan C. Gordon <icculus@icculus.org>
parents: 1558
diff changeset
   398
	result = XIconifyWindow(SDL_Display, WMwindow, SDL_Screen);
3ba88cb7eb1b Updated dynamic X11 code. See details in Bugzilla #170.
Ryan C. Gordon <icculus@icculus.org>
parents: 1558
diff changeset
   399
	XSync(SDL_Display, False);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   400
	SDL_Unlock_EventThread();
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   401
	return(result);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   402
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   403
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   404
SDL_GrabMode X11_GrabInputNoLock(_THIS, SDL_GrabMode mode)
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   405
{
236
3f09f52ac2cc Fixed X11 icon color allocation (thanks Mattias!)
Sam Lantinga <slouken@libsdl.org>
parents: 99
diff changeset
   406
	int result;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   407
4553
a1af511bbbdd RedHat patch: SDL-1.2.14-xio_error-rh603984.patch
Sam Lantinga <slouken@libsdl.org>
parents: 4247
diff changeset
   408
	if ( this->screen == NULL || SDL_Display == NULL ) {
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   409
		return(SDL_GRAB_OFF);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   410
	}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   411
	if ( ! SDL_Window ) {
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   412
		return(mode);	/* Will be set later on mode switch */
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   413
	}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   414
	if ( mode == SDL_GRAB_OFF ) {
1575
3ba88cb7eb1b Updated dynamic X11 code. See details in Bugzilla #170.
Ryan C. Gordon <icculus@icculus.org>
parents: 1558
diff changeset
   415
		XUngrabPointer(SDL_Display, CurrentTime);
3ba88cb7eb1b Updated dynamic X11 code. See details in Bugzilla #170.
Ryan C. Gordon <icculus@icculus.org>
parents: 1558
diff changeset
   416
		XUngrabKeyboard(SDL_Display, CurrentTime);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   417
	} else {
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   418
		if ( this->screen->flags & SDL_FULLSCREEN ) {
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   419
			/* Unbind the mouse from the fullscreen window */
1575
3ba88cb7eb1b Updated dynamic X11 code. See details in Bugzilla #170.
Ryan C. Gordon <icculus@icculus.org>
parents: 1558
diff changeset
   420
			XUngrabPointer(SDL_Display, CurrentTime);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   421
		}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   422
		/* Try to grab the mouse */
98
8a5aff5c1294 Fixed some problems with the fullscreen code. Wooo. :)
Sam Lantinga <slouken@lokigames.com>
parents: 88
diff changeset
   423
#if 0 /* We'll wait here until we actually grab, otherwise behavior undefined */
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   424
		for ( numtries = 0; numtries < 10; ++numtries ) {
98
8a5aff5c1294 Fixed some problems with the fullscreen code. Wooo. :)
Sam Lantinga <slouken@lokigames.com>
parents: 88
diff changeset
   425
#else
1767
ae9f6be81810 Fixed bug #196
Sam Lantinga <slouken@libsdl.org>
parents: 1765
diff changeset
   426
		for ( ; ; ) {
98
8a5aff5c1294 Fixed some problems with the fullscreen code. Wooo. :)
Sam Lantinga <slouken@lokigames.com>
parents: 88
diff changeset
   427
#endif
1575
3ba88cb7eb1b Updated dynamic X11 code. See details in Bugzilla #170.
Ryan C. Gordon <icculus@icculus.org>
parents: 1558
diff changeset
   428
			result = XGrabPointer(SDL_Display, SDL_Window, True, 0,
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   429
						GrabModeAsync, GrabModeAsync,
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   430
						SDL_Window, None, CurrentTime);
88
71774090f286 Hopefully fixed the fullscreen mode code for KDE
Sam Lantinga <slouken@lokigames.com>
parents: 0
diff changeset
   431
			if ( result == GrabSuccess ) {
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   432
				break;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   433
			}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   434
			SDL_Delay(100);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   435
		}
88
71774090f286 Hopefully fixed the fullscreen mode code for KDE
Sam Lantinga <slouken@lokigames.com>
parents: 0
diff changeset
   436
		if ( result != GrabSuccess ) {
71774090f286 Hopefully fixed the fullscreen mode code for KDE
Sam Lantinga <slouken@lokigames.com>
parents: 0
diff changeset
   437
			/* Uh, oh, what do we do here? */ ;
71774090f286 Hopefully fixed the fullscreen mode code for KDE
Sam Lantinga <slouken@lokigames.com>
parents: 0
diff changeset
   438
		}
98
8a5aff5c1294 Fixed some problems with the fullscreen code. Wooo. :)
Sam Lantinga <slouken@lokigames.com>
parents: 88
diff changeset
   439
		/* Now grab the keyboard */
1575
3ba88cb7eb1b Updated dynamic X11 code. See details in Bugzilla #170.
Ryan C. Gordon <icculus@icculus.org>
parents: 1558
diff changeset
   440
		XGrabKeyboard(SDL_Display, WMwindow, True,
88
71774090f286 Hopefully fixed the fullscreen mode code for KDE
Sam Lantinga <slouken@lokigames.com>
parents: 0
diff changeset
   441
				GrabModeAsync, GrabModeAsync, CurrentTime);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   442
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   443
		/* Raise the window if we grab the mouse */
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   444
		if ( !(this->screen->flags & SDL_FULLSCREEN) )
1575
3ba88cb7eb1b Updated dynamic X11 code. See details in Bugzilla #170.
Ryan C. Gordon <icculus@icculus.org>
parents: 1558
diff changeset
   445
			XRaiseWindow(SDL_Display, WMwindow);
98
8a5aff5c1294 Fixed some problems with the fullscreen code. Wooo. :)
Sam Lantinga <slouken@lokigames.com>
parents: 88
diff changeset
   446
8a5aff5c1294 Fixed some problems with the fullscreen code. Wooo. :)
Sam Lantinga <slouken@lokigames.com>
parents: 88
diff changeset
   447
		/* Make sure we register input focus */
8a5aff5c1294 Fixed some problems with the fullscreen code. Wooo. :)
Sam Lantinga <slouken@lokigames.com>
parents: 88
diff changeset
   448
		SDL_PrivateAppActive(1, SDL_APPINPUTFOCUS);
3896
1a327643e741 X11 backend: tell app that mouse focus has been obtained when grabbing the
Ryan C. Gordon <icculus@icculus.org>
parents: 3872
diff changeset
   449
		/* Since we grabbed the pointer, we have mouse focus, too. */
1a327643e741 X11 backend: tell app that mouse focus has been obtained when grabbing the
Ryan C. Gordon <icculus@icculus.org>
parents: 3872
diff changeset
   450
		SDL_PrivateAppActive(1, SDL_APPMOUSEFOCUS);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   451
	}
1575
3ba88cb7eb1b Updated dynamic X11 code. See details in Bugzilla #170.
Ryan C. Gordon <icculus@icculus.org>
parents: 1558
diff changeset
   452
	XSync(SDL_Display, False);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   453
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   454
	return(mode);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   455
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   456
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   457
SDL_GrabMode X11_GrabInput(_THIS, SDL_GrabMode mode)
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   458
{
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   459
	SDL_Lock_EventThread();
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   460
	mode = X11_GrabInputNoLock(this, mode);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   461
	SDL_Unlock_EventThread();
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   462
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   463
	return(mode);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   464
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   465
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   466
/* If 'info' is the right version, this function fills it and returns 1.
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   467
   Otherwise, in case of a version mismatch, it returns -1.
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   468
*/
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   469
static void lock_display(void)
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   470
{
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   471
	SDL_Lock_EventThread();
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   472
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   473
static void unlock_display(void)
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   474
{
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   475
	/* Make sure any X11 transactions are completed */
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   476
	SDL_VideoDevice *this = current_video;
1575
3ba88cb7eb1b Updated dynamic X11 code. See details in Bugzilla #170.
Ryan C. Gordon <icculus@icculus.org>
parents: 1558
diff changeset
   477
	XSync(SDL_Display, False);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   478
	SDL_Unlock_EventThread();
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   479
}
3872
be84332e761e Fixed bug #278
Sam Lantinga <slouken@libsdl.org>
parents: 1849
diff changeset
   480
be84332e761e Fixed bug #278
Sam Lantinga <slouken@libsdl.org>
parents: 1849
diff changeset
   481
#include <stdio.h>
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   482
int X11_GetWMInfo(_THIS, SDL_SysWMinfo *info)
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   483
{
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   484
	if ( info->version.major <= SDL_MAJOR_VERSION ) {
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   485
		info->subsystem = SDL_SYSWM_X11;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   486
		info->info.x11.display = SDL_Display;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   487
		info->info.x11.window = SDL_Window;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   488
		if ( SDL_VERSIONNUM(info->version.major,
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   489
		                    info->version.minor,
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   490
		                    info->version.patch) >= 1002 ) {
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   491
			info->info.x11.fswindow = FSwindow;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   492
			info->info.x11.wmwindow = WMwindow;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   493
		}
3872
be84332e761e Fixed bug #278
Sam Lantinga <slouken@libsdl.org>
parents: 1849
diff changeset
   494
be84332e761e Fixed bug #278
Sam Lantinga <slouken@libsdl.org>
parents: 1849
diff changeset
   495
be84332e761e Fixed bug #278
Sam Lantinga <slouken@libsdl.org>
parents: 1849
diff changeset
   496
		if ( SDL_VERSIONNUM(info->version.major,
be84332e761e Fixed bug #278
Sam Lantinga <slouken@libsdl.org>
parents: 1849
diff changeset
   497
		                    info->version.minor,
be84332e761e Fixed bug #278
Sam Lantinga <slouken@libsdl.org>
parents: 1849
diff changeset
   498
		                    info->version.patch) >= 1212 ) {
be84332e761e Fixed bug #278
Sam Lantinga <slouken@libsdl.org>
parents: 1849
diff changeset
   499
			info->info.x11.gfxdisplay = GFX_Display;
be84332e761e Fixed bug #278
Sam Lantinga <slouken@libsdl.org>
parents: 1849
diff changeset
   500
		}
be84332e761e Fixed bug #278
Sam Lantinga <slouken@libsdl.org>
parents: 1849
diff changeset
   501
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   502
		info->info.x11.lock_func = lock_display;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   503
		info->info.x11.unlock_func = unlock_display;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   504
		return(1);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   505
	} else {
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   506
		SDL_SetError("Application not compiled with SDL %d.%d\n",
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   507
					SDL_MAJOR_VERSION, SDL_MINOR_VERSION);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   508
		return(-1);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   509
	}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   510
}