src/video/x11/SDL_x11shape.c
author Eli Gottlieb <eligottlieb@gmail.com>
Tue, 10 Aug 2010 16:10:26 -0400
changeset 4846 4f1573996a65
parent 4845 61cb2d20a46f
child 4851 5624fb0190b5
permissions -rw-r--r--
Fixed a bug in X11 shaping that refused to use color-key mode.
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@4845
    23
#include "SDL_assert.h"
icculus@4794
    24
#include "SDL_x11video.h"
eligottlieb@4782
    25
#include "SDL_x11shape.h"
eligottlieb@4782
    26
#include "SDL_x11window.h"
eligottlieb@4777
    27
eligottlieb@4838
    28
SDL_Window*
eligottlieb@4838
    29
X11_CreateShapedWindow(const char *title,unsigned int x,unsigned int y,unsigned int w,unsigned int h,Uint32 flags) {
eligottlieb@4838
    30
    return SDL_CreateWindow(title,x,y,w,h,flags);
eligottlieb@4809
    31
}
eligottlieb@4809
    32
eligottlieb@4845
    33
SDL_WindowShaper*
eligottlieb@4838
    34
X11_CreateShaper(SDL_Window* window) {
eligottlieb@4838
    35
    SDL_WindowShaper* result = NULL;
icculus@4794
    36
icculus@4794
    37
#if SDL_VIDEO_DRIVER_X11_XSHAPE
eligottlieb@4838
    38
    if (SDL_X11_HAVE_XSHAPE) {  /* Make sure X server supports it. */
eligottlieb@4838
    39
        result = malloc(sizeof(SDL_WindowShaper));
eligottlieb@4838
    40
        result->window = window;
eligottlieb@4838
    41
        result->mode.mode = ShapeModeDefault;
eligottlieb@4838
    42
        result->mode.parameters.binarizationCutoff = 1;
eligottlieb@4838
    43
        result->usershownflag = 0;
eligottlieb@4845
    44
        SDL_ShapeData* data = SDL_malloc(sizeof(SDL_ShapeData));
eligottlieb@4838
    45
        result->driverdata = data;
eligottlieb@4838
    46
        data->bitmapsize = 0;
eligottlieb@4838
    47
        data->bitmap = NULL;
eligottlieb@4838
    48
        window->shaper = result;
eligottlieb@4838
    49
        int resized_properly = X11_ResizeWindowShape(window);
eligottlieb@4845
    50
        SDL_assert(resized_properly == 0);
eligottlieb@4838
    51
    }
icculus@4794
    52
#endif
icculus@4791
    53
eligottlieb@4838
    54
    return result;
eligottlieb@4782
    55
}
eligottlieb@4782
    56
eligottlieb@4845
    57
int
eligottlieb@4838
    58
X11_ResizeWindowShape(SDL_Window* window) {
eligottlieb@4838
    59
    SDL_ShapeData* data = window->shaper->driverdata;
eligottlieb@4845
    60
    SDL_assert(data != NULL);
eligottlieb@4838
    61
    
eligottlieb@4838
    62
    unsigned int bitmapsize = window->w / 8;
eligottlieb@4838
    63
    if(window->w % 8 > 0)
eligottlieb@4838
    64
        bitmapsize += 1;
eligottlieb@4838
    65
    bitmapsize *= window->h;
eligottlieb@4838
    66
    if(data->bitmapsize != bitmapsize || data->bitmap == NULL) {
eligottlieb@4838
    67
        data->bitmapsize = bitmapsize;
eligottlieb@4838
    68
        if(data->bitmap != NULL)
eligottlieb@4838
    69
            free(data->bitmap);
eligottlieb@4838
    70
        data->bitmap = malloc(data->bitmapsize);
eligottlieb@4838
    71
        if(data->bitmap == NULL) {
eligottlieb@4838
    72
            SDL_SetError("Could not allocate memory for shaped-window bitmap.");
eligottlieb@4838
    73
            return -1;
eligottlieb@4838
    74
        }
eligottlieb@4838
    75
    }
eligottlieb@4838
    76
    memset(data->bitmap,0,data->bitmapsize);
eligottlieb@4838
    77
    
eligottlieb@4838
    78
    window->shaper->usershownflag |= window->flags & SDL_WINDOW_SHOWN;
eligottlieb@4838
    79
    
eligottlieb@4838
    80
    return 0;
eligottlieb@4782
    81
}
eligottlieb@4838
    82
    
eligottlieb@4845
    83
int
eligottlieb@4846
    84
X11_SetWindowShape(SDL_WindowShaper *shaper,SDL_Surface *shape,SDL_WindowShapeMode *shape_mode) {
eligottlieb@4838
    85
    if(shaper == NULL || shape == NULL || shaper->driverdata == NULL)
eligottlieb@4838
    86
        return -1;
icculus@4794
    87
icculus@4794
    88
#if SDL_VIDEO_DRIVER_X11_XSHAPE
eligottlieb@4846
    89
    if(shape->format->Amask == 0 && SDL_SHAPEMODEALPHA(shape_mode->mode))
eligottlieb@4838
    90
        return -2;
eligottlieb@4838
    91
    if(shape->w != shaper->window->w || shape->h != shaper->window->h)
eligottlieb@4838
    92
        return -3;
eligottlieb@4838
    93
    SDL_ShapeData *data = shaper->driverdata;
eligottlieb@4838
    94
    
eligottlieb@4838
    95
    /* Assume that shaper->alphacutoff already has a value, because SDL_SetWindowShape() should have given it one. */
eligottlieb@4838
    96
    SDL_CalculateShapeBitmap(shaper->mode,shape,data->bitmap,8);
eligottlieb@4838
    97
        
eligottlieb@4838
    98
    SDL_WindowData *windowdata = (SDL_WindowData*)(shaper->window->driverdata);
eligottlieb@4838
    99
    Pixmap shapemask = XCreateBitmapFromData(windowdata->videodata->display,windowdata->xwindow,data->bitmap,shaper->window->w,shaper->window->h);
eligottlieb@4838
   100
    
eligottlieb@4838
   101
    XShapeCombineMask(windowdata->videodata->display,windowdata->xwindow, ShapeBounding, 0, 0,shapemask, ShapeSet);
eligottlieb@4838
   102
    XSync(windowdata->videodata->display,False);
eligottlieb@4782
   103
eligottlieb@4838
   104
    XFreePixmap(windowdata->videodata->display,shapemask);
icculus@4794
   105
#endif
icculus@4794
   106
eligottlieb@4838
   107
    return 0;
eligottlieb@4782
   108
}