src/video/x11/SDL_x11shape.c
author Eli Gottlieb <eligottlieb@gmail.com>
Wed, 30 Jun 2010 16:19:44 -0400
changeset 4782 b6930aefd008
parent 4777 6e03d73054d7
child 4784 2878650e0dc9
permissions -rw-r--r--
Finished X11 shaped-window functionality and removed ellipse+polygon rendering.
eligottlieb@4777
     1
/*
eligottlieb@4777
     2
    SDL - Simple DirectMedia Layer
eligottlieb@4777
     3
    Copyright (C) 2010 Eli Gottlieb
eligottlieb@4777
     4
eligottlieb@4777
     5
    This library is free software; you can redistribute it and/or
eligottlieb@4777
     6
    modify it under the terms of the GNU Lesser General Public
eligottlieb@4777
     7
    License as published by the Free Software Foundation; either
eligottlieb@4777
     8
    version 2.1 of the License, or (at your option) any later version.
eligottlieb@4777
     9
eligottlieb@4777
    10
    This library is distributed in the hope that it will be useful,
eligottlieb@4777
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
eligottlieb@4777
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
eligottlieb@4777
    13
    Lesser General Public License for more details.
eligottlieb@4777
    14
eligottlieb@4777
    15
    You should have received a copy of the GNU Lesser General Public
eligottlieb@4777
    16
    License along with this library; if not, write to the Free Software
eligottlieb@4777
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
eligottlieb@4777
    18
eligottlieb@4777
    19
    Eli Gottlieb
eligottlieb@4777
    20
    eligottlieb@gmail.com
eligottlieb@4777
    21
*/
eligottlieb@4777
    22
eligottlieb@4782
    23
#include <X11/Xos.h>
eligottlieb@4782
    24
#include <X11/IntrinsicP.h>
eligottlieb@4782
    25
#include <X11/StringDefs.h>
eligottlieb@4782
    26
#include <X11/Xmu/Converters.h>
eligottlieb@4782
    27
#include <X11/extensions/shape.h>
eligottlieb@4782
    28
#include "SDL_x11shape.h"
eligottlieb@4782
    29
#include "SDL_x11window.h"
eligottlieb@4782
    30
#include "SDL_x11video.h"
eligottlieb@4777
    31
eligottlieb@4782
    32
SDL_WindowShaper* X11_CreateShaper(SDL_Window* window) {
eligottlieb@4782
    33
	SDL_WindowShaper* result = malloc(sizeof(SDL_WindowShaper));
eligottlieb@4782
    34
	result->window = window;
eligottlieb@4782
    35
	result->alphacutoff = 0;
eligottlieb@4782
    36
	result->usershownflag = 0;
eligottlieb@4782
    37
	result->driverdata = malloc(sizeof(SDL_ShapeData));
eligottlieb@4782
    38
	window->shaper = result;
eligottlieb@4782
    39
	int resized_properly = X11ResizeWindowShape(window);
eligottlieb@4782
    40
	assert(resized_properly == 0);
eligottlieb@4782
    41
	return result;
eligottlieb@4782
    42
}
eligottlieb@4782
    43
eligottlieb@4782
    44
int X11_ResizeWindowShape(SDL_Window* window) {
eligottlieb@4782
    45
	SDL_ShapeData* data = window->shaper->driverdata;
eligottlieb@4782
    46
	assert(data != NULL);
eligottlieb@4782
    47
	
eligottlieb@4782
    48
	unsigned int bitmapsize = window->w / 8;
eligottlieb@4782
    49
	if(window->w % 8 > 0)
eligottlieb@4782
    50
		bitmapsize += 1;
eligottlieb@4782
    51
	bitmapsize *= window->h;
eligottlieb@4782
    52
	if(data->bitmapsize != bitmapsize || data->bitmap == NULL) {
eligottlieb@4782
    53
		data->bitmapsize = bitmapsize;
eligottlieb@4782
    54
		if(data->bitmap != NULL)
eligottlieb@4782
    55
			free(data->bitmap);
eligottlieb@4782
    56
		data->bitmap = malloc(data->bitmapsize);
eligottlieb@4782
    57
		if(data->bitmap == NULL) {
eligottlieb@4782
    58
			SDL_SetError("Could not allocate memory for shaped-window bitmap.");
eligottlieb@4782
    59
			return -1;
eligottlieb@4782
    60
		}
eligottlieb@4782
    61
	}
eligottlieb@4782
    62
	
eligottlieb@4782
    63
	window->shaper->usershownflag = window->flags & SDL_WINDOW_SHOWN;
eligottlieb@4782
    64
	
eligottlieb@4782
    65
	return 0;
eligottlieb@4782
    66
}
eligottlieb@4782
    67
	
eligottlieb@4782
    68
int X11_SetWindowShape(SDL_WindowShaper *shaper,SDL_Surface *shape,SDL_WindowShapeMode *shapeMode) {
eligottlieb@4782
    69
	if(!SDL_ISPIXELFORMAT_ALPHA(SDL_MasksToPixelFormatEnum(shape->format->BitsPerPixel,shape->format->Rmask,shape->format->Gmask,shape->format->Bmask,shape->format->Amask)))
eligottlieb@4782
    70
		return -2;
eligottlieb@4782
    71
	if(shape->w != shaper->window->w || shape->h != shaper->window->h)
eligottlieb@4782
    72
		return -3;
eligottlieb@4782
    73
	SDL_ShapeData *data = shaper->driverdata;
eligottlieb@4782
    74
	assert(data != NULL);
eligottlieb@4782
    75
	
eligottlieb@4782
    76
	/* Assume that shaper->alphacutoff already has a value. */
eligottlieb@4782
    77
	SDL_CalculateShapeBitmap(shaper->alphacutoff,shape,data->bitmap);
eligottlieb@4782
    78
		
eligottlieb@4782
    79
	SDL_WindowData *windowdata = (SDL_WindowData*)(shaper->window->driverdata);
eligottlieb@4782
    80
	Pixmap shapemask = XCreateBitmapFromData(windowdata->videodata->display,windowdata->xwindow,data->bitmap,shaper->window->w,shaper->window->h);
eligottlieb@4782
    81
	
eligottlieb@4782
    82
	XShapeCombineMask(windowdata->videodata->display,windowdata->xwindow, ShapeBounding, 0, 0,shapemask, ShapeSet);
eligottlieb@4782
    83
	XSync(windowdata->videodata->display,False);
eligottlieb@4782
    84
eligottlieb@4782
    85
	XFreePixmap(windowdata->videodata->display,shapemask);
eligottlieb@4782
    86
	
eligottlieb@4782
    87
	return 0;
eligottlieb@4782
    88
}