src/video/x11/SDL_x11shape.c
author Ryan C. Gordon <icculus@icculus.org>
Thu, 09 Apr 2015 22:28:37 -0400
changeset 9541 cf8fab52e33b
parent 8149 681eb46b8ac4
child 9619 b94b6d0bff0f
permissions -rw-r--r--
Merged Alex Szpakowski's iOS-improvement branch to default.

Fixes Bugzilla #2798.
Fixes Bugzilla #2212.
Fixes Bugzilla #2826.
Fixes Bugzilla #2661.
Fixes Bugzilla #1885.
Fixes Bugzilla #1578.
Fixes Bugzilla #2751.

(whew!)

Notable changes, from Alex's notes:

- The SDL_WINDOW_ALLOW_HIGHDPI flag is now needed (along with SDL_GL_GetDrawableSize or SDL_GetRendererOutputSize) to use Retina / high DPI resolutions, bringing SDL’s Retina-related behavior on iOS in line with Mac OS X. Window dimensions and display modes are now in the “points” (non-high DPI) coordinate system rather than pixels, whereas SDL_GL_GetDrawableSize is in pixels.

- Reworked the custom extended launch screen code:
- It now hides after the first SDL_PumpEvents call rather than SDL_CreateWindow, and it fades out in a similar manner to the system launch screen behavior.
- It now mirrors the system launch screen behavior when deciding which image to display: it falls back to using the Launch Images dictionary in Info.plist if the iOS 8+ launch screen nib isn’t available, and if the Launch Images dictionary doesn’t exist it uses the old standard launch image names.
- The extended launch screen can now be disabled via the SDL_IPHONE_LAUNCHSCREEN define in SDL_config_iphoneos.h.

- Added support for SDL_HINT_ACCELEROMETER_AS_JOYSTICK.

- Added access to a window view's renderbuffer and framebuffer to syswm.

- Added OpenGL ES debug labels for the Renderbuffer and Framebuffer Objects created with SDL_GL_CreateContext.

- Added support for sRGB OpenGL ES contexts on iOS 7+.

- Updated OpenGL ES contexts to support native-resolution rendering (when SDL_WINDOW_ALLOW_HIGHDPI is enabled) on the iPhone 6 Plus, i.e. 1080x1920 rather than 1242x2208.

- Updated SDL_GL_CreateContext, SDL_GL_SwapWindow, SDL_GL_MakeCurrent, and SDL_GL_DeleteContext to be more robust.

- Updated SDL windows to display a UIView at all times, even when an OpenGL context is not active. This allows rotation, touch events, and other windowing-related events to work properly without an active OpenGL context. It also makes it easier to use SDL_GetWindowWMInfo after creating a SDL window.

- Updated the iOS-specific Objective-C code to use cleaner and more modern language features and APIs, including ARC instead of manual reference counting.

- Updated SDL_HINT_ORIENTATIONS to allow disabling custom orientations if the hint is set with no valid orientation names.

- Fixed several rotation and orientation bugs with windows and display modes, especially in iOS 8+.

- Fixed SDL_SetWindowFullscreen failing to update the status bar visibility on iOS 7+.

- Fixed the orientation of the offset applied to the window’s view when the onscreen keyboard is shown in iOS 8+.

- Fixed SDL_IsScreenKeyboardShown (patch by Phil Hassey.)

- Fixed several major memory leaks caused by missing autorelease pool blocks in the iOS-specific Objective-C code.

- Removed several dead code paths.

- The iOS 7 SDK (Xcode 5) or newer is now required to build SDL for iOS.
eligottlieb@4777
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@8149
     3
  Copyright (C) 1997-2014 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
*/
icculus@8093
    21
#include "../../SDL_internal.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;
gabomdq@7679
    39
    SDL_ShapeData* data = NULL;
gabomdq@7679
    40
    int resized_properly;
icculus@4794
    41
icculus@4794
    42
#if SDL_VIDEO_DRIVER_X11_XSHAPE
eligottlieb@4838
    43
    if (SDL_X11_HAVE_XSHAPE) {  /* Make sure X server supports it. */
eligottlieb@4838
    44
        result = malloc(sizeof(SDL_WindowShaper));
eligottlieb@4838
    45
        result->window = window;
eligottlieb@4838
    46
        result->mode.mode = ShapeModeDefault;
eligottlieb@4838
    47
        result->mode.parameters.binarizationCutoff = 1;
eligottlieb@4851
    48
        result->userx = result->usery = 0;
gabomdq@7679
    49
        data = SDL_malloc(sizeof(SDL_ShapeData));
eligottlieb@4838
    50
        result->driverdata = data;
eligottlieb@4838
    51
        data->bitmapsize = 0;
eligottlieb@4838
    52
        data->bitmap = NULL;
eligottlieb@4838
    53
        window->shaper = result;
gabomdq@7679
    54
        resized_properly = X11_ResizeWindowShape(window);
eligottlieb@4845
    55
        SDL_assert(resized_properly == 0);
eligottlieb@4838
    56
    }
icculus@4794
    57
#endif
icculus@4791
    58
eligottlieb@4838
    59
    return result;
eligottlieb@4782
    60
}
eligottlieb@4782
    61
eligottlieb@4845
    62
int
eligottlieb@4838
    63
X11_ResizeWindowShape(SDL_Window* window) {
eligottlieb@4838
    64
    SDL_ShapeData* data = window->shaper->driverdata;
gabomdq@7679
    65
    unsigned int bitmapsize = window->w / 8;
eligottlieb@4845
    66
    SDL_assert(data != NULL);
slouken@7191
    67
eligottlieb@4838
    68
    if(window->w % 8 > 0)
eligottlieb@4838
    69
        bitmapsize += 1;
eligottlieb@4838
    70
    bitmapsize *= window->h;
eligottlieb@4838
    71
    if(data->bitmapsize != bitmapsize || data->bitmap == NULL) {
eligottlieb@4838
    72
        data->bitmapsize = bitmapsize;
eligottlieb@4838
    73
        if(data->bitmap != NULL)
eligottlieb@4838
    74
            free(data->bitmap);
eligottlieb@4838
    75
        data->bitmap = malloc(data->bitmapsize);
eligottlieb@4838
    76
        if(data->bitmap == NULL) {
icculus@7037
    77
            return SDL_SetError("Could not allocate memory for shaped-window bitmap.");
eligottlieb@4838
    78
        }
eligottlieb@4838
    79
    }
eligottlieb@4838
    80
    memset(data->bitmap,0,data->bitmapsize);
slouken@7191
    81
eligottlieb@4851
    82
    window->shaper->userx = window->x;
eligottlieb@4851
    83
    window->shaper->usery = window->y;
eligottlieb@4851
    84
    SDL_SetWindowPosition(window,-1000,-1000);
slouken@7191
    85
eligottlieb@4838
    86
    return 0;
eligottlieb@4782
    87
}
slouken@7191
    88
eligottlieb@4845
    89
int
eligottlieb@4846
    90
X11_SetWindowShape(SDL_WindowShaper *shaper,SDL_Surface *shape,SDL_WindowShapeMode *shape_mode) {
gabomdq@7679
    91
    SDL_ShapeData *data = NULL;
gabomdq@7679
    92
    SDL_WindowData *windowdata = NULL;
gabomdq@7682
    93
    Pixmap shapemask;
gabomdq@7679
    94
    
eligottlieb@4838
    95
    if(shaper == NULL || shape == NULL || shaper->driverdata == NULL)
eligottlieb@4838
    96
        return -1;
icculus@4794
    97
icculus@4794
    98
#if SDL_VIDEO_DRIVER_X11_XSHAPE
eligottlieb@4846
    99
    if(shape->format->Amask == 0 && SDL_SHAPEMODEALPHA(shape_mode->mode))
eligottlieb@4838
   100
        return -2;
eligottlieb@4838
   101
    if(shape->w != shaper->window->w || shape->h != shaper->window->h)
eligottlieb@4838
   102
        return -3;
gabomdq@7679
   103
    data = shaper->driverdata;
slouken@7191
   104
eligottlieb@4838
   105
    /* Assume that shaper->alphacutoff already has a value, because SDL_SetWindowShape() should have given it one. */
eligottlieb@4838
   106
    SDL_CalculateShapeBitmap(shaper->mode,shape,data->bitmap,8);
slouken@7191
   107
gabomdq@7679
   108
    windowdata = (SDL_WindowData*)(shaper->window->driverdata);
icculus@7827
   109
    shapemask = X11_XCreateBitmapFromData(windowdata->videodata->display,windowdata->xwindow,data->bitmap,shaper->window->w,shaper->window->h);
slouken@7191
   110
icculus@7827
   111
    X11_XShapeCombineMask(windowdata->videodata->display,windowdata->xwindow, ShapeBounding, 0, 0,shapemask, ShapeSet);
icculus@7827
   112
    X11_XSync(windowdata->videodata->display,False);
eligottlieb@4782
   113
icculus@7827
   114
    X11_XFreePixmap(windowdata->videodata->display,shapemask);
icculus@4794
   115
#endif
icculus@4794
   116
eligottlieb@4838
   117
    return 0;
eligottlieb@4782
   118
}
slouken@5481
   119
slouken@5481
   120
#endif /* SDL_VIDEO_DRIVER_X11 */
slouken@5481
   121