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