src/video/x11/SDL_x11shape.c
author Ryan C. Gordon <icculus@icculus.org>
Thu, 13 Oct 2011 01:08:30 -0400
changeset 5981 75caa8a7d559
parent 5535 96594ac5fd1a
child 6138 4c64952a58fb
permissions -rw-r--r--
Fixed a whole slew of compiler warnings that -Wall exposed.
eligottlieb@4777
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@5535
     3
  Copyright (C) 1997-2011 Sam Lantinga <slouken@libsdl.org>
eligottlieb@4777
     4
slouken@5535
     5
  This software is provided 'as-is', without any express or implied
slouken@5535
     6
  warranty.  In no event will the authors be held liable for any damages
slouken@5535
     7
  arising from the use of this software.
eligottlieb@4777
     8
slouken@5535
     9
  Permission is granted to anyone to use this software for any purpose,
slouken@5535
    10
  including commercial applications, and to alter it and redistribute it
slouken@5535
    11
  freely, subject to the following restrictions:
eligottlieb@4777
    12
slouken@5535
    13
  1. The origin of this software must not be misrepresented; you must not
slouken@5535
    14
     claim that you wrote the original software. If you use this software
slouken@5535
    15
     in a product, an acknowledgment in the product documentation would be
slouken@5535
    16
     appreciated but is not required.
slouken@5535
    17
  2. Altered source versions must be plainly marked as such, and must not be
slouken@5535
    18
     misrepresented as being the original software.
slouken@5535
    19
  3. This notice may not be removed or altered from any source distribution.
eligottlieb@4777
    20
*/
slouken@5481
    21
#include "SDL_config.h"
slouken@5481
    22
slouken@5481
    23
#if SDL_VIDEO_DRIVER_X11
eligottlieb@4777
    24
eligottlieb@4845
    25
#include "SDL_assert.h"
icculus@4794
    26
#include "SDL_x11video.h"
eligottlieb@4782
    27
#include "SDL_x11shape.h"
eligottlieb@4782
    28
#include "SDL_x11window.h"
icculus@5981
    29
#include "../SDL_shape_internals.h"
eligottlieb@4777
    30
eligottlieb@4838
    31
SDL_Window*
eligottlieb@4838
    32
X11_CreateShapedWindow(const char *title,unsigned int x,unsigned int y,unsigned int w,unsigned int h,Uint32 flags) {
eligottlieb@4838
    33
    return SDL_CreateWindow(title,x,y,w,h,flags);
eligottlieb@4809
    34
}
eligottlieb@4809
    35
eligottlieb@4845
    36
SDL_WindowShaper*
eligottlieb@4838
    37
X11_CreateShaper(SDL_Window* window) {
eligottlieb@4838
    38
    SDL_WindowShaper* result = NULL;
icculus@4794
    39
icculus@4794
    40
#if SDL_VIDEO_DRIVER_X11_XSHAPE
eligottlieb@4838
    41
    if (SDL_X11_HAVE_XSHAPE) {  /* Make sure X server supports it. */
eligottlieb@4838
    42
        result = malloc(sizeof(SDL_WindowShaper));
eligottlieb@4838
    43
        result->window = window;
eligottlieb@4838
    44
        result->mode.mode = ShapeModeDefault;
eligottlieb@4838
    45
        result->mode.parameters.binarizationCutoff = 1;
eligottlieb@4851
    46
        result->userx = result->usery = 0;
eligottlieb@4845
    47
        SDL_ShapeData* data = SDL_malloc(sizeof(SDL_ShapeData));
eligottlieb@4838
    48
        result->driverdata = data;
eligottlieb@4838
    49
        data->bitmapsize = 0;
eligottlieb@4838
    50
        data->bitmap = NULL;
eligottlieb@4838
    51
        window->shaper = result;
eligottlieb@4838
    52
        int resized_properly = X11_ResizeWindowShape(window);
eligottlieb@4845
    53
        SDL_assert(resized_properly == 0);
eligottlieb@4838
    54
    }
icculus@4794
    55
#endif
icculus@4791
    56
eligottlieb@4838
    57
    return result;
eligottlieb@4782
    58
}
eligottlieb@4782
    59
eligottlieb@4845
    60
int
eligottlieb@4838
    61
X11_ResizeWindowShape(SDL_Window* window) {
eligottlieb@4838
    62
    SDL_ShapeData* data = window->shaper->driverdata;
eligottlieb@4845
    63
    SDL_assert(data != NULL);
eligottlieb@4838
    64
    
eligottlieb@4838
    65
    unsigned int bitmapsize = window->w / 8;
eligottlieb@4838
    66
    if(window->w % 8 > 0)
eligottlieb@4838
    67
        bitmapsize += 1;
eligottlieb@4838
    68
    bitmapsize *= window->h;
eligottlieb@4838
    69
    if(data->bitmapsize != bitmapsize || data->bitmap == NULL) {
eligottlieb@4838
    70
        data->bitmapsize = bitmapsize;
eligottlieb@4838
    71
        if(data->bitmap != NULL)
eligottlieb@4838
    72
            free(data->bitmap);
eligottlieb@4838
    73
        data->bitmap = malloc(data->bitmapsize);
eligottlieb@4838
    74
        if(data->bitmap == NULL) {
eligottlieb@4838
    75
            SDL_SetError("Could not allocate memory for shaped-window bitmap.");
eligottlieb@4838
    76
            return -1;
eligottlieb@4838
    77
        }
eligottlieb@4838
    78
    }
eligottlieb@4838
    79
    memset(data->bitmap,0,data->bitmapsize);
eligottlieb@4838
    80
    
eligottlieb@4851
    81
    window->shaper->userx = window->x;
eligottlieb@4851
    82
    window->shaper->usery = window->y;
eligottlieb@4851
    83
    SDL_SetWindowPosition(window,-1000,-1000);
eligottlieb@4838
    84
    
eligottlieb@4838
    85
    return 0;
eligottlieb@4782
    86
}
eligottlieb@4838
    87
    
eligottlieb@4845
    88
int
eligottlieb@4846
    89
X11_SetWindowShape(SDL_WindowShaper *shaper,SDL_Surface *shape,SDL_WindowShapeMode *shape_mode) {
eligottlieb@4838
    90
    if(shaper == NULL || shape == NULL || shaper->driverdata == NULL)
eligottlieb@4838
    91
        return -1;
icculus@4794
    92
icculus@4794
    93
#if SDL_VIDEO_DRIVER_X11_XSHAPE
eligottlieb@4846
    94
    if(shape->format->Amask == 0 && SDL_SHAPEMODEALPHA(shape_mode->mode))
eligottlieb@4838
    95
        return -2;
eligottlieb@4838
    96
    if(shape->w != shaper->window->w || shape->h != shaper->window->h)
eligottlieb@4838
    97
        return -3;
eligottlieb@4838
    98
    SDL_ShapeData *data = shaper->driverdata;
eligottlieb@4838
    99
    
eligottlieb@4838
   100
    /* Assume that shaper->alphacutoff already has a value, because SDL_SetWindowShape() should have given it one. */
eligottlieb@4838
   101
    SDL_CalculateShapeBitmap(shaper->mode,shape,data->bitmap,8);
eligottlieb@4838
   102
        
eligottlieb@4838
   103
    SDL_WindowData *windowdata = (SDL_WindowData*)(shaper->window->driverdata);
eligottlieb@4838
   104
    Pixmap shapemask = XCreateBitmapFromData(windowdata->videodata->display,windowdata->xwindow,data->bitmap,shaper->window->w,shaper->window->h);
eligottlieb@4838
   105
    
eligottlieb@4838
   106
    XShapeCombineMask(windowdata->videodata->display,windowdata->xwindow, ShapeBounding, 0, 0,shapemask, ShapeSet);
eligottlieb@4838
   107
    XSync(windowdata->videodata->display,False);
eligottlieb@4782
   108
eligottlieb@4838
   109
    XFreePixmap(windowdata->videodata->display,shapemask);
icculus@4794
   110
#endif
icculus@4794
   111
eligottlieb@4838
   112
    return 0;
eligottlieb@4782
   113
}
slouken@5481
   114
slouken@5481
   115
#endif /* SDL_VIDEO_DRIVER_X11 */
slouken@5481
   116