src/video/x11/SDL_x11framebuffer.c
author Sam Lantinga <slouken@libsdl.org>
Sat, 12 Mar 2011 13:21:57 -0800
changeset 5481 22dfc3958dc3
parent 5297 1800dc39b74c
child 5535 96594ac5fd1a
permissions -rw-r--r--
Fixed so code will compile with SDL_config_minimal.h
slouken@5182
     1
/*
slouken@5182
     2
    SDL - Simple DirectMedia Layer
slouken@5262
     3
    Copyright (C) 1997-2011 Sam Lantinga
slouken@5182
     4
slouken@5182
     5
    This library is free software; you can redistribute it and/or
slouken@5182
     6
    modify it under the terms of the GNU Lesser General Public
slouken@5182
     7
    License as published by the Free Software Foundation; either
slouken@5182
     8
    version 2.1 of the License, or (at your option) any later version.
slouken@5182
     9
slouken@5182
    10
    This library is distributed in the hope that it will be useful,
slouken@5182
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@5182
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@5182
    13
    Lesser General Public License for more details.
slouken@5182
    14
slouken@5182
    15
    You should have received a copy of the GNU Lesser General Public
slouken@5182
    16
    License along with this library; if not, write to the Free Software
slouken@5182
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
slouken@5182
    18
slouken@5182
    19
    Sam Lantinga
slouken@5182
    20
    slouken@libsdl.org
slouken@5182
    21
*/
slouken@5182
    22
#include "SDL_config.h"
slouken@5182
    23
slouken@5481
    24
#if SDL_VIDEO_DRIVER_X11
slouken@5481
    25
slouken@5182
    26
#include "SDL_x11video.h"
slouken@5182
    27
#include "SDL_x11framebuffer.h"
slouken@5182
    28
slouken@5182
    29
slouken@5182
    30
#ifndef NO_SHARED_MEMORY
slouken@5182
    31
slouken@5182
    32
/* Shared memory error handler routine */
slouken@5182
    33
static int shm_error;
slouken@5182
    34
static int (*X_handler)(Display *, XErrorEvent *) = NULL;
slouken@5182
    35
static int shm_errhandler(Display *d, XErrorEvent *e)
slouken@5182
    36
{
slouken@5182
    37
        if ( e->error_code == BadAccess ) {
slouken@5182
    38
            shm_error = True;
slouken@5182
    39
            return(0);
slouken@5182
    40
        } else
slouken@5182
    41
        return(X_handler(d,e));
slouken@5182
    42
}
slouken@5182
    43
slouken@5182
    44
static SDL_bool have_mitshm(void)
slouken@5182
    45
{
slouken@5182
    46
    /* Only use shared memory on local X servers */
slouken@5182
    47
    if ( (SDL_strncmp(XDisplayName(NULL), ":", 1) == 0) ||
slouken@5182
    48
         (SDL_strncmp(XDisplayName(NULL), "unix:", 5) == 0) ) {
slouken@5182
    49
        return SDL_X11_HAVE_SHM;
slouken@5182
    50
    }
slouken@5182
    51
    return SDL_FALSE;
slouken@5182
    52
}
slouken@5182
    53
slouken@5182
    54
#endif /* !NO_SHARED_MEMORY */
slouken@5182
    55
slouken@5182
    56
int
slouken@5182
    57
X11_CreateWindowFramebuffer(_THIS, SDL_Window * window, Uint32 * format,
slouken@5182
    58
                            void ** pixels, int *pitch)
slouken@5182
    59
{
slouken@5182
    60
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@5182
    61
    Display *display = data->videodata->display;
slouken@5182
    62
    XGCValues gcv;
slouken@5182
    63
    XVisualInfo vinfo;
slouken@5182
    64
slouken@5182
    65
    /* Free the old framebuffer surface */
slouken@5182
    66
    X11_DestroyWindowFramebuffer(_this, window);
slouken@5182
    67
slouken@5182
    68
    /* Create the graphics context for drawing */
slouken@5182
    69
    gcv.graphics_exposures = False;
slouken@5182
    70
    data->gc = XCreateGC(display, data->xwindow, GCGraphicsExposures, &gcv);
slouken@5182
    71
    if (!data->gc) {
slouken@5182
    72
        SDL_SetError("Couldn't create graphics context");
slouken@5182
    73
        return -1;
slouken@5182
    74
    }
slouken@5182
    75
slouken@5182
    76
    /* Find out the pixel format and depth */
slouken@5182
    77
    if (X11_GetVisualInfoFromVisual(display, data->visual, &vinfo) < 0) {
slouken@5182
    78
        SDL_SetError("Couldn't get window visual information");
slouken@5182
    79
        return -1;
slouken@5182
    80
    }
slouken@5182
    81
slouken@5182
    82
    *format = X11_GetPixelFormatFromVisualInfo(display, &vinfo);
slouken@5182
    83
    if (*format == SDL_PIXELFORMAT_UNKNOWN) {
slouken@5182
    84
        SDL_SetError("Unknown window pixel format");
slouken@5182
    85
        return -1;
slouken@5182
    86
    }
slouken@5182
    87
slouken@5182
    88
    /* Calculate pitch */
slouken@5182
    89
    *pitch = (((window->w * SDL_BYTESPERPIXEL(*format)) + 3) & ~3);
slouken@5182
    90
slouken@5182
    91
    /* Create the actual image */
slouken@5182
    92
#ifndef NO_SHARED_MEMORY
slouken@5182
    93
    if (have_mitshm()) {
slouken@5182
    94
        XShmSegmentInfo *shminfo = &data->shminfo;
slouken@5182
    95
slouken@5182
    96
        shminfo->shmid = shmget(IPC_PRIVATE, window->h*(*pitch), IPC_CREAT | 0777);
slouken@5182
    97
        if ( shminfo->shmid >= 0 ) {
slouken@5182
    98
            shminfo->shmaddr = (char *)shmat(shminfo->shmid, 0, 0);
slouken@5182
    99
            shminfo->readOnly = False;
slouken@5182
   100
            if ( shminfo->shmaddr != (char *)-1 ) {
slouken@5182
   101
                shm_error = False;
slouken@5182
   102
                X_handler = XSetErrorHandler(shm_errhandler);
slouken@5182
   103
                XShmAttach(display, shminfo);
slouken@5182
   104
                XSync(display, True);
slouken@5182
   105
                XSetErrorHandler(X_handler);
slouken@5182
   106
                if ( shm_error )
slouken@5182
   107
                    shmdt(shminfo->shmaddr);
slouken@5182
   108
            } else {
slouken@5182
   109
                shm_error = True;
slouken@5182
   110
            }
slouken@5182
   111
            shmctl(shminfo->shmid, IPC_RMID, NULL);
slouken@5182
   112
        } else {
slouken@5182
   113
            shm_error = True;
slouken@5182
   114
        }
slouken@5182
   115
        if (!shm_error) {
slouken@5182
   116
            data->ximage = XShmCreateImage(display, data->visual,
slouken@5182
   117
                             vinfo.depth, ZPixmap,
slouken@5182
   118
                             shminfo->shmaddr, shminfo, 
slouken@5182
   119
                             window->w, window->h);
slouken@5182
   120
            if (!data->ximage) {
slouken@5182
   121
                XShmDetach(display, shminfo);
slouken@5182
   122
                XSync(display, False);
slouken@5182
   123
                shmdt(shminfo->shmaddr);
slouken@5182
   124
            } else {
slouken@5182
   125
                /* Done! */
slouken@5182
   126
                data->use_mitshm = SDL_TRUE;
slouken@5182
   127
                *pixels = shminfo->shmaddr;
slouken@5182
   128
                return 0;
slouken@5182
   129
            }
slouken@5182
   130
        }
slouken@5182
   131
    }
slouken@5182
   132
#endif /* not NO_SHARED_MEMORY */
slouken@5182
   133
slouken@5182
   134
    *pixels = SDL_malloc(window->h*(*pitch));
slouken@5182
   135
    if (*pixels == NULL) {
slouken@5182
   136
        SDL_OutOfMemory();
slouken@5182
   137
        return -1;
slouken@5182
   138
    }
slouken@5182
   139
slouken@5182
   140
    data->ximage = XCreateImage(display, data->visual,
slouken@5182
   141
                      vinfo.depth, ZPixmap, 0, (char *)(*pixels), 
slouken@5182
   142
                      window->w, window->h, 32, 0);
slouken@5182
   143
    if (!data->ximage) {
slouken@5182
   144
        SDL_free(*pixels);
slouken@5182
   145
        SDL_SetError("Couldn't create XImage");
slouken@5182
   146
        return -1;
slouken@5182
   147
    }
slouken@5182
   148
    return 0;
slouken@5182
   149
}
slouken@5182
   150
slouken@5182
   151
int
slouken@5297
   152
X11_UpdateWindowFramebuffer(_THIS, SDL_Window * window, SDL_Rect * rects,
slouken@5297
   153
                            int numrects)
slouken@5182
   154
{
slouken@5182
   155
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@5182
   156
    Display *display = data->videodata->display;
slouken@5182
   157
    int i;
slouken@5182
   158
    SDL_Rect *rect;
slouken@5182
   159
slouken@5182
   160
#ifndef NO_SHARED_MEMORY
slouken@5182
   161
    if (data->use_mitshm) {
slouken@5182
   162
        for (i = 0; i < numrects; ++i) {
slouken@5182
   163
            rect = &rects[i];
slouken@5182
   164
slouken@5182
   165
            if (rect->w == 0 || rect->h == 0) { /* Clipped? */
slouken@5182
   166
                continue;
slouken@5182
   167
            }
slouken@5182
   168
            XShmPutImage(display, data->xwindow, data->gc, data->ximage,
slouken@5182
   169
                    rect->x, rect->y,
slouken@5182
   170
                    rect->x, rect->y, rect->w, rect->h, False);
slouken@5182
   171
        }
slouken@5182
   172
    }
slouken@5182
   173
    else
slouken@5182
   174
#endif /* !NO_SHARED_MEMORY */
slouken@5182
   175
    {
slouken@5182
   176
        for (i = 0; i < numrects; ++i) {
slouken@5182
   177
            rect = &rects[i];
slouken@5182
   178
slouken@5182
   179
            if (rect->w == 0 || rect->h == 0) { /* Clipped? */
slouken@5182
   180
                continue;
slouken@5182
   181
            }
slouken@5182
   182
            XPutImage(display, data->xwindow, data->gc, data->ximage,
slouken@5182
   183
                  rect->x, rect->y,
slouken@5182
   184
                  rect->x, rect->y, rect->w, rect->h);
slouken@5182
   185
        }
slouken@5182
   186
    }
slouken@5182
   187
    XSync(display, False);
slouken@5182
   188
}
slouken@5182
   189
slouken@5182
   190
void
slouken@5182
   191
X11_DestroyWindowFramebuffer(_THIS, SDL_Window * window)
slouken@5182
   192
{
slouken@5182
   193
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@5182
   194
    Display *display = data->videodata->display;
slouken@5182
   195
slouken@5182
   196
    if (data->ximage) {
slouken@5182
   197
        XDestroyImage(data->ximage);
slouken@5182
   198
slouken@5182
   199
#ifndef NO_SHARED_MEMORY
slouken@5182
   200
        if (data->use_mitshm) {
slouken@5182
   201
            XShmDetach(display, &data->shminfo);
slouken@5182
   202
            XSync(display, False);
slouken@5182
   203
            shmdt(data->shminfo.shmaddr);
slouken@5182
   204
            data->use_mitshm = SDL_FALSE;
slouken@5182
   205
        }
slouken@5182
   206
#endif /* !NO_SHARED_MEMORY */
slouken@5182
   207
slouken@5182
   208
        data->ximage = NULL;
slouken@5182
   209
    }
slouken@5182
   210
    if (data->gc) {
slouken@5182
   211
        XFreeGC(display, data->gc);
slouken@5182
   212
        data->gc = NULL;
slouken@5182
   213
    }
slouken@5182
   214
}
slouken@5182
   215
slouken@5481
   216
#endif /* SDL_VIDEO_DRIVER_X11 */
slouken@5481
   217
slouken@5182
   218
/* vi: set ts=4 sw=4 expandtab: */