Implemented Cocoa window support
authorSam Lantinga <slouken@libsdl.org>
Mon, 24 Jul 2006 05:03:02 +0000
changeset 19337ee5297340f7
parent 1932 dc864bcabcc4
child 1934 70139af5ac27
Implemented Cocoa window support
src/video/SDL_video.c
src/video/cocoa/SDL_cocoavideo.h
src/video/cocoa/SDL_cocoavideo.m
src/video/cocoa/SDL_cocoawindow.h
src/video/cocoa/SDL_cocoawindow.m
src/video/win32/SDL_win32window.h
test/Makefile.in
test/common.c
test/common.h
test/testwm2.c
     1.1 --- a/src/video/SDL_video.c	Sun Jul 23 09:16:14 2006 +0000
     1.2 +++ b/src/video/SDL_video.c	Mon Jul 24 05:03:02 2006 +0000
     1.3 @@ -1282,7 +1282,6 @@
     1.4              if (window->title) {
     1.5                  SDL_free(window->title);
     1.6              }
     1.7 -            SDL_free(window);
     1.8              if (j != display->num_windows - 1) {
     1.9                  SDL_memcpy(&display->windows[i],
    1.10                             &display->windows[i + 1],
     2.1 --- a/src/video/cocoa/SDL_cocoavideo.h	Sun Jul 23 09:16:14 2006 +0000
     2.2 +++ b/src/video/cocoa/SDL_cocoavideo.h	Mon Jul 24 05:03:02 2006 +0000
     2.3 @@ -34,8 +34,8 @@
     2.4  #include "SDL_cocoamouse.h"
     2.5  /*
     2.6  #include "SDL_cocoaopengl.h"
     2.7 +*/
     2.8  #include "SDL_cocoawindow.h"
     2.9 -*/
    2.10  
    2.11  /* Private display data */
    2.12  
     3.1 --- a/src/video/cocoa/SDL_cocoavideo.m	Sun Jul 23 09:16:14 2006 +0000
     3.2 +++ b/src/video/cocoa/SDL_cocoavideo.m	Mon Jul 24 05:03:02 2006 +0000
     3.3 @@ -73,7 +73,6 @@
     3.4      device->SetDisplayMode = Cocoa_SetDisplayMode;
     3.5      device->PumpEvents = Cocoa_PumpEvents;
     3.6  
     3.7 -    /*
     3.8      device->CreateWindow = Cocoa_CreateWindow;
     3.9      device->CreateWindowFrom = Cocoa_CreateWindowFrom;
    3.10      device->SetWindowTitle = Cocoa_SetWindowTitle;
    3.11 @@ -88,6 +87,7 @@
    3.12      device->SetWindowGrab = Cocoa_SetWindowGrab;
    3.13      device->DestroyWindow = Cocoa_DestroyWindow;
    3.14      device->GetWindowWMInfo = Cocoa_GetWindowWMInfo;
    3.15 +    /*
    3.16  #ifdef SDL_VIDEO_OPENGL
    3.17      device->GL_LoadLibrary = Cocoa_GL_LoadLibrary;
    3.18      device->GL_GetProcAddress = Cocoa_GL_GetProcAddress;
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/src/video/cocoa/SDL_cocoawindow.h	Mon Jul 24 05:03:02 2006 +0000
     4.3 @@ -0,0 +1,89 @@
     4.4 +/*
     4.5 +    SDL - Simple DirectMedia Layer
     4.6 +    Copyright (C) 1997-2006 Sam Lantinga
     4.7 +
     4.8 +    This library is free software; you can redistribute it and/or
     4.9 +    modify it under the terms of the GNU Lesser General Public
    4.10 +    License as published by the Free Software Foundation; either
    4.11 +    version 2.1 of the License, or (at your option) any later version.
    4.12 +
    4.13 +    This library is distributed in the hope that it will be useful,
    4.14 +    but WITHOUT ANY WARRANTY; without even the implied warranty of
    4.15 +    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    4.16 +    Lesser General Public License for more details.
    4.17 +
    4.18 +    You should have received a copy of the GNU Lesser General Public
    4.19 +    License along with this library; if not, write to the Free Software
    4.20 +    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    4.21 +
    4.22 +    Sam Lantinga
    4.23 +    slouken@libsdl.org
    4.24 +*/
    4.25 +#include "SDL_config.h"
    4.26 +
    4.27 +#ifndef _SDL_cocoawindow_h
    4.28 +#define _SDL_cocoawindow_h
    4.29 +
    4.30 +typedef struct SDL_WindowData SDL_WindowData;
    4.31 +
    4.32 +@interface Cocoa_WindowListener:NSResponder {
    4.33 +    SDL_WindowData *_data;
    4.34 +}
    4.35 +
    4.36 +-(void) listen:(SDL_WindowData *) data;
    4.37 +-(void) close;
    4.38 +
    4.39 +/* Window delegate functionality */
    4.40 +-(BOOL) windowShouldClose:(id) sender;
    4.41 +-(void) windowDidExpose:(NSNotification *) aNotification;
    4.42 +-(void) windowDidMove:(NSNotification *) aNotification;
    4.43 +-(void) windowDidResize:(NSNotification *) aNotification;
    4.44 +-(void) windowDidMiniaturize:(NSNotification *) aNotification;
    4.45 +-(void) windowDidDeminiaturize:(NSNotification *) aNotification;
    4.46 +-(void) windowDidBecomeKey:(NSNotification *) aNotification;
    4.47 +-(void) windowDidResignKey:(NSNotification *) aNotification;
    4.48 +-(void) windowDidHide:(NSNotification *) aNotification;
    4.49 +-(void) windowDidUnhide:(NSNotification *) aNotification;
    4.50 +
    4.51 +/* Window event handling */
    4.52 +-(void) mouseDown:(NSEvent *) theEvent;
    4.53 +-(void) rightMouseDown:(NSEvent *) theEvent;
    4.54 +-(void) otherMouseDown:(NSEvent *) theEvent;
    4.55 +-(void) mouseUp:(NSEvent *) theEvent;
    4.56 +-(void) rightMouseUp:(NSEvent *) theEvent;
    4.57 +-(void) otherMouseUp:(NSEvent *) theEvent;
    4.58 +-(void) mouseMoved:(NSEvent *) theEvent;
    4.59 +-(void) scrollWheel:(NSEvent *) theEvent;
    4.60 +-(void) mouseEntered:(NSEvent *) theEvent;
    4.61 +-(void) mouseExited:(NSEvent *) theEvent;
    4.62 +-(void) keyDown:(NSEvent *) theEvent;
    4.63 +-(void) keyUp:(NSEvent *) theEvent;
    4.64 +@end struct SDL_WindowData
    4.65 +{
    4.66 +    SDL_WindowID windowID;
    4.67 +    NSWindow *window;
    4.68 +    BOOL created;
    4.69 +    Cocoa_WindowListener *listener;
    4.70 +    struct SDL_VideoData *videodata;
    4.71 +};
    4.72 +
    4.73 +extern int Cocoa_CreateWindow(_THIS, SDL_Window * window);
    4.74 +extern int Cocoa_CreateWindowFrom(_THIS, SDL_Window * window,
    4.75 +                                  const void *data);
    4.76 +extern void Cocoa_SetWindowTitle(_THIS, SDL_Window * window);
    4.77 +extern void Cocoa_SetWindowPosition(_THIS, SDL_Window * window);
    4.78 +extern void Cocoa_SetWindowSize(_THIS, SDL_Window * window);
    4.79 +extern void Cocoa_ShowWindow(_THIS, SDL_Window * window);
    4.80 +extern void Cocoa_HideWindow(_THIS, SDL_Window * window);
    4.81 +extern void Cocoa_RaiseWindow(_THIS, SDL_Window * window);
    4.82 +extern void Cocoa_MaximizeWindow(_THIS, SDL_Window * window);
    4.83 +extern void Cocoa_MinimizeWindow(_THIS, SDL_Window * window);
    4.84 +extern void Cocoa_RestoreWindow(_THIS, SDL_Window * window);
    4.85 +extern void Cocoa_SetWindowGrab(_THIS, SDL_Window * window);
    4.86 +extern void Cocoa_DestroyWindow(_THIS, SDL_Window * window);
    4.87 +extern SDL_bool Cocoa_GetWindowWMInfo(_THIS, SDL_Window * window,
    4.88 +                                      struct SDL_SysWMinfo *info);
    4.89 +
    4.90 +#endif /* _SDL_cocoawindow_h */
    4.91 +
    4.92 +/* vi: set ts=4 sw=4 expandtab: */
     5.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.2 +++ b/src/video/cocoa/SDL_cocoawindow.m	Mon Jul 24 05:03:02 2006 +0000
     5.3 @@ -0,0 +1,557 @@
     5.4 +/*
     5.5 +    SDL - Simple DirectMedia Layer
     5.6 +    Copyright (C) 1997-2006 Sam Lantinga
     5.7 +
     5.8 +    This library is free software; you can redistribute it and/or
     5.9 +    modify it under the terms of the GNU Lesser General Public
    5.10 +    License as published by the Free Software Foundation; either
    5.11 +    version 2.1 of the License, or (at your option) any later version.
    5.12 +
    5.13 +    This library is distributed in the hope that it will be useful,
    5.14 +    but WITHOUT ANY WARRANTY; without even the implied warranty of
    5.15 +    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    5.16 +    Lesser General Public License for more details.
    5.17 +
    5.18 +    You should have received a copy of the GNU Lesser General Public
    5.19 +    License along with this library; if not, write to the Free Software
    5.20 +    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    5.21 +
    5.22 +    Sam Lantinga
    5.23 +    slouken@libsdl.org
    5.24 +*/
    5.25 +#include "SDL_config.h"
    5.26 +
    5.27 +#include "SDL_syswm.h"
    5.28 +#include "../SDL_sysvideo.h"
    5.29 +#include "../../events/SDL_keyboard_c.h"
    5.30 +#include "../../events/SDL_mouse_c.h"
    5.31 +#include "../../events/SDL_windowevents_c.h"
    5.32 +
    5.33 +#include "SDL_cocoavideo.h"
    5.34 +
    5.35 +static __inline__ void ConvertNSRect(NSRect *r)
    5.36 +{
    5.37 +    r->origin.y = CGDisplayPixelsHigh(kCGDirectMainDisplay) - r->origin.y - r->size.height;
    5.38 +}
    5.39 +
    5.40 +@implementation Cocoa_WindowListener
    5.41 +
    5.42 +- (void)listen:(SDL_WindowData *)data
    5.43 +{
    5.44 +    NSNotificationCenter *center;
    5.45 +
    5.46 +    _data = data;
    5.47 +
    5.48 +    center = [NSNotificationCenter defaultCenter];
    5.49 +
    5.50 +    [_data->window setNextResponder:self];
    5.51 +    if ([_data->window delegate] != nil) {
    5.52 +        [center addObserver:self selector:@selector(windowDisExpose:) name:NSWindowDidExposeNotification object:_data->window];
    5.53 +        [center addObserver:self selector:@selector(windowDidMove:) name:NSWindowDidMoveNotification object:_data->window];
    5.54 +        [center addObserver:self selector:@selector(windowDidResize:) name:NSWindowDidResizeNotification object:_data->window];
    5.55 +        [center addObserver:self selector:@selector(windowDidMiniaturize:) name:NSWindowDidMiniaturizeNotification object:_data->window];
    5.56 +        [center addObserver:self selector:@selector(windowDidDeminiaturize:) name:NSWindowDidDeminiaturizeNotification object:_data->window];
    5.57 +        [center addObserver:self selector:@selector(windowDidBecomeKey:) name:NSWindowDidBecomeKeyNotification object:_data->window];
    5.58 +        [center addObserver:self selector:@selector(windowDidResignKey:) name:NSWindowDidResignKeyNotification object:_data->window];
    5.59 +    } else {
    5.60 +        [_data->window setDelegate:self];
    5.61 +    }
    5.62 +    [center addObserver:self selector:@selector(windowDidHide:) name:NSApplicationDidHideNotification object:NSApp];
    5.63 +    [center addObserver:self selector:@selector(windowDidUnhide:) name:NSApplicationDidUnhideNotification object:NSApp];
    5.64 +
    5.65 +    [_data->window setAcceptsMouseMovedEvents:YES];
    5.66 +}
    5.67 +
    5.68 +- (void)close
    5.69 +{
    5.70 +    NSNotificationCenter *center;
    5.71 +
    5.72 +    center = [NSNotificationCenter defaultCenter];
    5.73 +
    5.74 +    [_data->window setNextResponder:nil];
    5.75 +    if ([_data->window delegate] != self) {
    5.76 +        [center removeObserver:self name:NSWindowDidExposeNotification object:_data->window];
    5.77 +        [center removeObserver:self name:NSWindowDidMoveNotification object:_data->window];
    5.78 +        [center removeObserver:self name:NSWindowDidResizeNotification object:_data->window];
    5.79 +        [center removeObserver:self name:NSWindowDidMiniaturizeNotification object:_data->window];
    5.80 +        [center removeObserver:self name:NSWindowDidDeminiaturizeNotification object:_data->window];
    5.81 +        [center removeObserver:self name:NSWindowDidBecomeKeyNotification object:_data->window];
    5.82 +        [center removeObserver:self name:NSWindowDidResignKeyNotification object:_data->window];
    5.83 +    } else {
    5.84 +        [_data->window setDelegate:nil];
    5.85 +    }
    5.86 +    [center removeObserver:self name:NSApplicationDidHideNotification object:NSApp];
    5.87 +    [center removeObserver:self name:NSApplicationDidUnhideNotification object:NSApp];
    5.88 +}
    5.89 +
    5.90 +- (BOOL)windowShouldClose:(id)sender
    5.91 +{
    5.92 +    SDL_SendWindowEvent(_data->windowID, SDL_WINDOWEVENT_CLOSE, 0, 0);
    5.93 +    return NO;
    5.94 +}
    5.95 +
    5.96 +- (void)windowDidExpose:(NSNotification *)aNotification
    5.97 +{
    5.98 +    SDL_SendWindowEvent(_data->windowID, SDL_WINDOWEVENT_EXPOSED, 0, 0);
    5.99 +}
   5.100 +
   5.101 +- (void)windowDidMove:(NSNotification *)aNotification
   5.102 +{
   5.103 +    int x, y;
   5.104 +    NSRect rect = [_data->window contentRectForFrameRect:[_data->window frame]];
   5.105 +    ConvertNSRect(&rect);
   5.106 +    x = (int)rect.origin.x;
   5.107 +    y = (int)rect.origin.y;
   5.108 +    SDL_SendWindowEvent(_data->windowID, SDL_WINDOWEVENT_MOVED, x, y);
   5.109 +}
   5.110 +
   5.111 +- (void)windowDidResize:(NSNotification *)aNotification
   5.112 +{
   5.113 +    int w, h;
   5.114 +    NSRect rect = [_data->window contentRectForFrameRect:[_data->window frame]];
   5.115 +    w = (int)rect.size.width;
   5.116 +    h = (int)rect.size.height;
   5.117 +    SDL_SendWindowEvent(_data->windowID, SDL_WINDOWEVENT_RESIZED, w, h);
   5.118 +}
   5.119 +
   5.120 +- (void)windowDidMiniaturize:(NSNotification *)aNotification
   5.121 +{
   5.122 +    SDL_SendWindowEvent(_data->windowID, SDL_WINDOWEVENT_MINIMIZED, 0, 0);
   5.123 +}
   5.124 +
   5.125 +- (void)windowDidDeminiaturize:(NSNotification *)aNotification
   5.126 +{
   5.127 +    SDL_SendWindowEvent(_data->windowID, SDL_WINDOWEVENT_RESTORED, 0, 0);
   5.128 +}
   5.129 +
   5.130 +- (void)windowDidBecomeKey:(NSNotification *)aNotification
   5.131 +{
   5.132 +    SDL_SendWindowEvent(_data->windowID, SDL_WINDOWEVENT_FOCUS_GAINED, 0, 0);
   5.133 +}
   5.134 +
   5.135 +- (void)windowDidResignKey:(NSNotification *)aNotification
   5.136 +{
   5.137 +    SDL_SendWindowEvent(_data->windowID, SDL_WINDOWEVENT_FOCUS_LOST, 0, 0);
   5.138 +}
   5.139 +
   5.140 +- (void)windowDidHide:(NSNotification *)aNotification
   5.141 +{
   5.142 +    SDL_SendWindowEvent(_data->windowID, SDL_WINDOWEVENT_HIDDEN, 0, 0);
   5.143 +}
   5.144 +
   5.145 +- (void)windowDidUnhide:(NSNotification *)aNotification
   5.146 +{
   5.147 +    SDL_SendWindowEvent(_data->windowID, SDL_WINDOWEVENT_SHOWN, 0, 0);
   5.148 +}
   5.149 +
   5.150 +- (void)mouseDown:(NSEvent *)theEvent
   5.151 +{
   5.152 +    int index;
   5.153 +
   5.154 +    index = _data->videodata->mouse;
   5.155 +    SDL_SendMouseButton(index, SDL_PRESSED, SDL_BUTTON_LEFT);
   5.156 +}
   5.157 +
   5.158 +- (void)rightMouseDown:(NSEvent *)theEvent
   5.159 +{
   5.160 +    int index;
   5.161 +
   5.162 +    index = _data->videodata->mouse;
   5.163 +    SDL_SendMouseButton(index, SDL_PRESSED, SDL_BUTTON_RIGHT);
   5.164 +}
   5.165 +
   5.166 +- (void)otherMouseDown:(NSEvent *)theEvent
   5.167 +{
   5.168 +    int index;
   5.169 +
   5.170 +    index = _data->videodata->mouse;
   5.171 +    SDL_SendMouseButton(index, SDL_PRESSED, SDL_BUTTON_MIDDLE);
   5.172 +}
   5.173 +
   5.174 +- (void)mouseUp:(NSEvent *)theEvent
   5.175 +{
   5.176 +    int index;
   5.177 +
   5.178 +    index = _data->videodata->mouse;
   5.179 +    SDL_SendMouseButton(index, SDL_RELEASED, SDL_BUTTON_LEFT);
   5.180 +}
   5.181 +
   5.182 +- (void)rightMouseUp:(NSEvent *)theEvent
   5.183 +{
   5.184 +    int index;
   5.185 +
   5.186 +    index = _data->videodata->mouse;
   5.187 +    SDL_SendMouseButton(index, SDL_RELEASED, SDL_BUTTON_RIGHT);
   5.188 +}
   5.189 +
   5.190 +- (void)otherMouseUp:(NSEvent *)theEvent
   5.191 +{
   5.192 +    int index;
   5.193 +
   5.194 +    index = _data->videodata->mouse;
   5.195 +    SDL_SendMouseButton(index, SDL_RELEASED, SDL_BUTTON_MIDDLE);
   5.196 +}
   5.197 +
   5.198 +- (void)mouseMoved:(NSEvent *)theEvent
   5.199 +{
   5.200 +    int index;
   5.201 +    SDL_Mouse *mouse;
   5.202 +    NSPoint point;
   5.203 +    NSRect rect = [_data->window contentRectForFrameRect:[_data->window frame]];
   5.204 +
   5.205 +    index = _data->videodata->mouse;
   5.206 +    mouse = SDL_GetMouse(index);
   5.207 +    if (mouse->focus != _data->windowID) {
   5.208 +        SDL_SetMouseFocus(index, _data->windowID);
   5.209 +    }
   5.210 +
   5.211 +    point = [NSEvent mouseLocation];
   5.212 +    point.x = point.x - rect.origin.x;
   5.213 +    point.y = rect.size.height - (point.y - rect.origin.y);
   5.214 +    SDL_SendMouseMotion(index, 0, (int)point.x, (int)point.y);
   5.215 +}
   5.216 +
   5.217 +- (void)scrollWheel:(NSEvent *)theEvent
   5.218 +{
   5.219 +fprintf(stderr, "scrollWheel\n");
   5.220 +}
   5.221 +
   5.222 +- (void)mouseEntered:(NSEvent *)theEvent
   5.223 +{
   5.224 +fprintf(stderr, "mouseEntered\n");
   5.225 +    SDL_SendWindowEvent(_data->windowID, SDL_WINDOWEVENT_ENTER, 0, 0);
   5.226 +}
   5.227 +
   5.228 +- (void)mouseExited:(NSEvent *)theEvent
   5.229 +{
   5.230 +fprintf(stderr, "mouseExited\n");
   5.231 +    SDL_SendWindowEvent(_data->windowID, SDL_WINDOWEVENT_LEAVE, 0, 0);
   5.232 +}
   5.233 +
   5.234 +- (void)keyDown:(NSEvent *)theEvent
   5.235 +{
   5.236 +fprintf(stderr, "keyDown\n");
   5.237 +}
   5.238 +
   5.239 +- (void)keyUp:(NSEvent *)theEvent
   5.240 +{
   5.241 +fprintf(stderr, "keyUp\n");
   5.242 +}
   5.243 +
   5.244 +@end
   5.245 +
   5.246 +static int
   5.247 +SetupWindowData(SDL_Window * window, NSWindow *nswindow, BOOL created)
   5.248 +{
   5.249 +    NSAutoreleasePool *pool;
   5.250 +    SDL_WindowData *data;
   5.251 +
   5.252 +    /* Allocate the window data */
   5.253 +    data = (SDL_WindowData *) SDL_malloc(sizeof(*data));
   5.254 +    if (!data) {
   5.255 +        SDL_OutOfMemory();
   5.256 +        return -1;
   5.257 +    }
   5.258 +    data->windowID = window->id;
   5.259 +    data->window = nswindow;
   5.260 +    data->created = created;
   5.261 +    data->videodata = (SDL_VideoData *) SDL_GetVideoDevice()->driverdata;
   5.262 +
   5.263 +    pool = [[NSAutoreleasePool alloc] init];
   5.264 +
   5.265 +    /* Create an event listener for the window */
   5.266 +    data->listener = [[Cocoa_WindowListener alloc] init];
   5.267 +    [data->listener listen:data];
   5.268 +
   5.269 +    /* Fill in the SDL window with the window data */
   5.270 +    {
   5.271 +        NSRect rect = [nswindow contentRectForFrameRect:[nswindow frame]];
   5.272 +        ConvertNSRect(&rect);
   5.273 +        window->x = (int)rect.origin.x;
   5.274 +        window->y = (int)rect.origin.y;
   5.275 +        window->w = (int)rect.size.width;
   5.276 +        window->h = (int)rect.size.height;
   5.277 +    }
   5.278 +    if ([nswindow isVisible]) {
   5.279 +        window->flags |= SDL_WINDOW_SHOWN;
   5.280 +    } else {
   5.281 +        window->flags &= ~SDL_WINDOW_SHOWN;
   5.282 +    }
   5.283 +    {
   5.284 +        unsigned int style = [nswindow styleMask];
   5.285 +
   5.286 +        if (style == NSBorderlessWindowMask) {
   5.287 +            window->flags |= SDL_WINDOW_BORDERLESS;
   5.288 +        } else {
   5.289 +            window->flags &= ~SDL_WINDOW_BORDERLESS;
   5.290 +        }
   5.291 +        if (style & NSResizableWindowMask) {
   5.292 +            window->flags |= SDL_WINDOW_RESIZABLE;
   5.293 +        } else {
   5.294 +            window->flags &= ~SDL_WINDOW_RESIZABLE;
   5.295 +        }
   5.296 +    }
   5.297 +    if ([nswindow isZoomed]) {
   5.298 +        window->flags |= SDL_WINDOW_MAXIMIZED;
   5.299 +    } else {
   5.300 +        window->flags &= ~SDL_WINDOW_MAXIMIZED;
   5.301 +    }
   5.302 +    if ([nswindow isMiniaturized]) {
   5.303 +        window->flags |= SDL_WINDOW_MINIMIZED;
   5.304 +    } else {
   5.305 +        window->flags &= ~SDL_WINDOW_MINIMIZED;
   5.306 +    }
   5.307 +    if ([nswindow isKeyWindow]) {
   5.308 +        int index = data->videodata->keyboard;
   5.309 +        window->flags |= SDL_WINDOW_INPUT_FOCUS;
   5.310 +        SDL_SetKeyboardFocus(index, data->windowID);
   5.311 +
   5.312 +        if (window->flags & SDL_WINDOW_INPUT_GRABBED) {
   5.313 +            /* FIXME */
   5.314 +        }
   5.315 +    }
   5.316 +
   5.317 +    /* All done! */
   5.318 +    [pool release];
   5.319 +    window->driverdata = data;
   5.320 +    return 0;
   5.321 +}
   5.322 +
   5.323 +int
   5.324 +Cocoa_CreateWindow(_THIS, SDL_Window * window)
   5.325 +{
   5.326 +    NSAutoreleasePool *pool;
   5.327 +    NSWindow *nswindow;
   5.328 +    NSRect rect;
   5.329 +    unsigned int style;
   5.330 +    NSString *title;
   5.331 +
   5.332 +    pool = [[NSAutoreleasePool alloc] init];
   5.333 +
   5.334 +    if ((window->flags & SDL_WINDOW_FULLSCREEN) ||
   5.335 +        window->x == SDL_WINDOWPOS_CENTERED) {
   5.336 +        rect.origin.x = (CGDisplayPixelsWide(kCGDirectMainDisplay) - window->w) / 2;
   5.337 +    } else if (window->x == SDL_WINDOWPOS_UNDEFINED) {
   5.338 +        rect.origin.x = 0;
   5.339 +    } else {
   5.340 +        rect.origin.x = window->x;
   5.341 +    }
   5.342 +    if ((window->flags & SDL_WINDOW_FULLSCREEN) ||
   5.343 +        window->y == SDL_WINDOWPOS_CENTERED) {
   5.344 +        rect.origin.y = (CGDisplayPixelsHigh(kCGDirectMainDisplay) - window->h) / 2;
   5.345 +    } else if (window->y == SDL_WINDOWPOS_UNDEFINED) {
   5.346 +        rect.origin.y = 0;
   5.347 +    } else {
   5.348 +        rect.origin.y = window->y;
   5.349 +    }
   5.350 +    rect.size.width = window->w;
   5.351 +    rect.size.height = window->h;
   5.352 +    ConvertNSRect(&rect);
   5.353 +
   5.354 +    if (window->flags & SDL_WINDOW_BORDERLESS) {
   5.355 +        style = NSBorderlessWindowMask;
   5.356 +    } else {
   5.357 +        style = (NSTitledWindowMask|NSClosableWindowMask|NSMiniaturizableWindowMask);
   5.358 +    }
   5.359 +    if (window->flags & SDL_WINDOW_RESIZABLE) {
   5.360 +        style |= NSResizableWindowMask;
   5.361 +    }
   5.362 +
   5.363 +    nswindow = [[NSWindow alloc] initWithContentRect:rect styleMask:style backing:NSBackingStoreBuffered defer:FALSE];
   5.364 +
   5.365 +    if (window->flags & SDL_WINDOW_SHOWN) {
   5.366 +        [nswindow makeKeyAndOrderFront:nil];
   5.367 +    }
   5.368 +    if (window->flags & SDL_WINDOW_MAXIMIZED) {
   5.369 +        [nswindow performZoom:nil];
   5.370 +    }
   5.371 +    if (window->flags & SDL_WINDOW_MINIMIZED) {
   5.372 +        [nswindow performMiniaturize:nil];
   5.373 +    }
   5.374 +
   5.375 +    if (window->title) {
   5.376 +        title = [[NSString alloc] initWithUTF8String:window->title];
   5.377 +        [nswindow setTitle:title];
   5.378 +        [nswindow setMiniwindowTitle:title];
   5.379 +        [title release];
   5.380 +    }
   5.381 +
   5.382 +    [pool release];
   5.383 +
   5.384 +    if (SetupWindowData(window, nswindow, YES) < 0) {
   5.385 +        [nswindow release];
   5.386 +        [pool release];
   5.387 +        return -1;
   5.388 +    }
   5.389 +#ifdef SDL_VIDEO_OPENGL
   5.390 +    /*
   5.391 +    if (window->flags & SDL_WINDOW_OPENGL) {
   5.392 +        if (Cocoa_GL_SetupWindow(_this, window) < 0) {
   5.393 +            Cocoa_DestroyWindow(_this, window);
   5.394 +            return -1;
   5.395 +        }
   5.396 +    }
   5.397 +    */
   5.398 +#endif
   5.399 +    return 0;
   5.400 +}
   5.401 +
   5.402 +int
   5.403 +Cocoa_CreateWindowFrom(_THIS, SDL_Window * window, const void *data)
   5.404 +{
   5.405 +    NSAutoreleasePool *pool;
   5.406 +    NSWindow *nswindow = (NSWindow *) data;
   5.407 +    NSString *title;
   5.408 +    int status;
   5.409 +
   5.410 +    pool = [[NSAutoreleasePool alloc] init];
   5.411 +
   5.412 +    /* Query the title from the existing window */
   5.413 +    title = [nswindow title];
   5.414 +    if (title) {
   5.415 +        window->title = SDL_strdup([title UTF8String]);
   5.416 +    }
   5.417 +
   5.418 +    [pool release];
   5.419 +
   5.420 +    return SetupWindowData(window, nswindow, NO);
   5.421 +}
   5.422 +
   5.423 +void
   5.424 +Cocoa_SetWindowTitle(_THIS, SDL_Window * window)
   5.425 +{
   5.426 +    NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->window;
   5.427 +    NSString *string;
   5.428 +
   5.429 +    string = [[NSString alloc] initWithUTF8String:window->title];
   5.430 +    [nswindow setTitle:string];
   5.431 +    [nswindow setMiniwindowTitle:string];
   5.432 +    [string release];
   5.433 +}
   5.434 +
   5.435 +void
   5.436 +Cocoa_SetWindowPosition(_THIS, SDL_Window * window)
   5.437 +{
   5.438 +    NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->window;
   5.439 +    NSRect rect;
   5.440 +
   5.441 +    rect.origin.x = window->x;
   5.442 +    rect.origin.y = window->y;
   5.443 +    rect.size.width = window->w;
   5.444 +    rect.size.height = window->h;
   5.445 +    ConvertNSRect(&rect);
   5.446 +    rect = [nswindow frameRectForContentRect:rect];
   5.447 +    [nswindow setFrameOrigin:rect.origin];
   5.448 +}
   5.449 +
   5.450 +void
   5.451 +Cocoa_SetWindowSize(_THIS, SDL_Window * window)
   5.452 +{
   5.453 +    NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->window;
   5.454 +    NSSize size;
   5.455 +
   5.456 +    size.width = window->w;
   5.457 +    size.height = window->h;
   5.458 +    [nswindow setContentSize:size];
   5.459 +}
   5.460 +
   5.461 +void
   5.462 +Cocoa_ShowWindow(_THIS, SDL_Window * window)
   5.463 +{
   5.464 +    NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->window;
   5.465 +
   5.466 +    [nswindow makeKeyAndOrderFront:nil];
   5.467 +}
   5.468 +
   5.469 +void
   5.470 +Cocoa_HideWindow(_THIS, SDL_Window * window)
   5.471 +{
   5.472 +    NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->window;
   5.473 +
   5.474 +    /* FIXME */
   5.475 +}
   5.476 +
   5.477 +void
   5.478 +Cocoa_RaiseWindow(_THIS, SDL_Window * window)
   5.479 +{
   5.480 +    NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->window;
   5.481 +
   5.482 +    [nswindow makeKeyAndOrderFront:nil];
   5.483 +}
   5.484 +
   5.485 +void
   5.486 +Cocoa_MaximizeWindow(_THIS, SDL_Window * window)
   5.487 +{
   5.488 +    NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->window;
   5.489 +
   5.490 +    [nswindow performZoom:nil];
   5.491 +}
   5.492 +
   5.493 +void
   5.494 +Cocoa_MinimizeWindow(_THIS, SDL_Window * window)
   5.495 +{
   5.496 +    NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->window;
   5.497 +
   5.498 +    [nswindow performMiniaturize:nil];
   5.499 +}
   5.500 +
   5.501 +void
   5.502 +Cocoa_RestoreWindow(_THIS, SDL_Window * window)
   5.503 +{
   5.504 +    NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->window;
   5.505 +
   5.506 +    /* FIXME */
   5.507 +}
   5.508 +
   5.509 +void
   5.510 +Cocoa_SetWindowGrab(_THIS, SDL_Window * window)
   5.511 +{
   5.512 +    if ((window->flags & SDL_WINDOW_INPUT_GRABBED) &&
   5.513 +        (window->flags & SDL_WINDOW_INPUT_FOCUS)) {
   5.514 +        /* FIXME: Grab mouse */
   5.515 +    } else {
   5.516 +        /* FIXME: Release mouse */
   5.517 +    }
   5.518 +}
   5.519 +
   5.520 +void
   5.521 +Cocoa_DestroyWindow(_THIS, SDL_Window * window)
   5.522 +{
   5.523 +    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
   5.524 +
   5.525 +    if (data) {
   5.526 +        NSAutoreleasePool *pool;
   5.527 +#ifdef SDL_VIDEO_OPENGL
   5.528 +        /*
   5.529 +        if (window->flags & SDL_WINDOW_OPENGL) {
   5.530 +            Cocoa_GL_CleanupWindow(_this, window);
   5.531 +        }
   5.532 +        */
   5.533 +#endif
   5.534 +        pool = [[NSAutoreleasePool alloc] init];
   5.535 +        [data->listener close];
   5.536 +        [data->listener release];
   5.537 +        if (data->created) {
   5.538 +            [data->window close];
   5.539 +        }
   5.540 +        SDL_free(data);
   5.541 +        [pool release];
   5.542 +    }
   5.543 +}
   5.544 +
   5.545 +SDL_bool
   5.546 +Cocoa_GetWindowWMInfo(_THIS, SDL_Window * window, SDL_SysWMinfo * info)
   5.547 +{
   5.548 +    NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->window;
   5.549 +
   5.550 +    if (info->version.major <= SDL_MAJOR_VERSION) {
   5.551 +        //info->window = nswindow;
   5.552 +        return SDL_TRUE;
   5.553 +    } else {
   5.554 +        SDL_SetError("Application not compiled with SDL %d.%d\n",
   5.555 +                     SDL_MAJOR_VERSION, SDL_MINOR_VERSION);
   5.556 +        return SDL_FALSE;
   5.557 +    }
   5.558 +}
   5.559 +
   5.560 +/* vi: set ts=4 sw=4 expandtab: */
     6.1 --- a/src/video/win32/SDL_win32window.h	Sun Jul 23 09:16:14 2006 +0000
     6.2 +++ b/src/video/win32/SDL_win32window.h	Mon Jul 24 05:03:02 2006 +0000
     6.3 @@ -24,9 +24,6 @@
     6.4  #ifndef _SDL_win32window_h
     6.5  #define _SDL_win32window_h
     6.6  
     6.7 -#include "../SDL_sysvideo.h"
     6.8 -#include "SDL_win32video.h"
     6.9 -
    6.10  typedef struct
    6.11  {
    6.12      SDL_WindowID windowID;
     7.1 --- a/test/Makefile.in	Sun Jul 23 09:16:14 2006 +0000
     7.2 +++ b/test/Makefile.in	Mon Jul 24 05:03:02 2006 +0000
     7.3 @@ -107,8 +107,8 @@
     7.4  testwm$(EXE): $(srcdir)/testwm.c
     7.5  	$(CC) -o $@ $? $(CFLAGS) $(LIBS)
     7.6  
     7.7 -testwm2$(EXE): $(srcdir)/testwm.c
     7.8 -	$(CC) -o $@ $? $(CFLAGS) $(LIBS)
     7.9 +testwm2$(EXE): $(srcdir)/testwm2.c $(srcdir)/common.c
    7.10 +	$(CC) -o $@ $(srcdir)/testwm2.c $(srcdir)/common.c $(CFLAGS) $(LIBS)
    7.11  
    7.12  threadwin$(EXE): $(srcdir)/threadwin.c
    7.13  	$(CC) -o $@ $? $(CFLAGS) $(LIBS)
     8.1 --- a/test/common.c	Sun Jul 23 09:16:14 2006 +0000
     8.2 +++ b/test/common.c	Mon Jul 24 05:03:02 2006 +0000
     8.3 @@ -660,8 +660,9 @@
     8.4                  return SDL_FALSE;
     8.5              }
     8.6  
     8.7 -            if (state->renderdriver
     8.8 -                || !(state->window_flags & SDL_WINDOW_OPENGL)) {
     8.9 +            if (!state->skip_renderer
    8.10 +                && (state->renderdriver
    8.11 +                    || !(state->window_flags & SDL_WINDOW_OPENGL))) {
    8.12                  m = -1;
    8.13                  if (state->renderdriver) {
    8.14                      SDL_RendererInfo info;
    8.15 @@ -784,6 +785,9 @@
    8.16              fprintf(stderr, "Window %d lost keyboard focus",
    8.17                      event->window.windowID);
    8.18              break;
    8.19 +        case SDL_WINDOWEVENT_CLOSE:
    8.20 +            fprintf(stderr, "Window %d closed", event->window.windowID);
    8.21 +            break;
    8.22          default:
    8.23              fprintf(stderr, "Window %d got unknown event %d",
    8.24                      event->window.windowID, event->window.event);
     9.1 --- a/test/common.h	Sun Jul 23 09:16:14 2006 +0000
     9.2 +++ b/test/common.h	Mon Jul 24 05:03:02 2006 +0000
     9.3 @@ -33,6 +33,7 @@
     9.4      /* Renderer info */
     9.5      const char *renderdriver;
     9.6      Uint32 render_flags;
     9.7 +    SDL_bool skip_renderer;
     9.8  
     9.9      /* Audio info */
    9.10      const char *audiodriver;
    10.1 --- a/test/testwm2.c	Sun Jul 23 09:16:14 2006 +0000
    10.2 +++ b/test/testwm2.c	Mon Jul 24 05:03:02 2006 +0000
    10.3 @@ -1,98 +1,56 @@
    10.4 -/* Simple program:  Move N sprites around on the screen as fast as possible */
    10.5  
    10.6 -#include "SDL.h"
    10.7 +#include <stdlib.h>
    10.8 +#include <stdio.h>
    10.9  
   10.10 -#define NUM_WINDOWS 2
   10.11 -#define WINDOW_W    640
   10.12 -#define WINDOW_H    480
   10.13 +#include "common.h"
   10.14  
   10.15 -static int num_windows;
   10.16 -static SDL_WindowID *windows;
   10.17 +static CommonState *state;
   10.18  
   10.19  /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
   10.20  static void
   10.21  quit(int rc)
   10.22  {
   10.23 -    if (windows) {
   10.24 -        SDL_free(windows);
   10.25 -    }
   10.26 -    SDL_Quit();
   10.27 +    CommonQuit(state);
   10.28      exit(rc);
   10.29  }
   10.30  
   10.31  int
   10.32  main(int argc, char *argv[])
   10.33  {
   10.34 -    int window_w, window_h;
   10.35      int i, done;
   10.36      SDL_Event event;
   10.37  
   10.38 -    /* Initialize SDL */
   10.39 -    if (SDL_Init(SDL_INIT_VIDEO) < 0) {
   10.40 -        fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
   10.41 -        return (1);
   10.42 +    /* Initialize test framework */
   10.43 +    state = CommonCreateState(argv, SDL_INIT_VIDEO);
   10.44 +    if (!state) {
   10.45 +        return 1;
   10.46 +    }
   10.47 +    state->skip_renderer = SDL_TRUE;
   10.48 +    for (i = 1; i < argc;) {
   10.49 +        int consumed;
   10.50 +
   10.51 +        consumed = CommonArg(state, i);
   10.52 +        if (consumed == 0) {
   10.53 +            consumed = -1;
   10.54 +        }
   10.55 +        if (consumed < 0) {
   10.56 +            fprintf(stderr, "Usage: %s %s", argv[0], CommonUsage(state));
   10.57 +            quit(1);
   10.58 +        }
   10.59 +        i += consumed;
   10.60 +    }
   10.61 +    if (!CommonInit(state)) {
   10.62 +        quit(2);
   10.63      }
   10.64  
   10.65 -    num_windows = NUM_WINDOWS;
   10.66 -    window_w = WINDOW_W;
   10.67 -    window_h = WINDOW_H;
   10.68 -    while (argc > 1) {
   10.69 -        if (strcmp(argv[argc - 1], "-width") == 0) {
   10.70 -            window_w = atoi(argv[argc]);
   10.71 -            --argc;
   10.72 -        } else if (strcmp(argv[argc - 1], "-height") == 0) {
   10.73 -            window_h = atoi(argv[argc]);
   10.74 -            --argc;
   10.75 -        } else {
   10.76 -            fprintf(stderr, "Usage: %s [-width] [-height]\n", argv[0]);
   10.77 -            quit(1);
   10.78 -        }
   10.79 -    }
   10.80 -
   10.81 -    /* Create the windows */
   10.82 -    windows = (SDL_WindowID *) SDL_malloc(num_windows * sizeof(*windows));
   10.83 -    if (!windows) {
   10.84 -        fprintf(stderr, "Out of memory!\n");
   10.85 -        quit(2);
   10.86 -    }
   10.87 -    for (i = 0; i < num_windows; ++i) {
   10.88 -        char title[32];
   10.89 -        int x, y;
   10.90 -
   10.91 -        SDL_snprintf(title, sizeof(title), "testwm %d", i + 1);
   10.92 -        if (i == 0) {
   10.93 -            x = SDL_WINDOWPOS_CENTERED;
   10.94 -            y = SDL_WINDOWPOS_CENTERED;
   10.95 -        } else {
   10.96 -            x = SDL_WINDOWPOS_UNDEFINED;
   10.97 -            y = SDL_WINDOWPOS_UNDEFINED;
   10.98 -        }
   10.99 -        windows[i] =
  10.100 -            SDL_CreateWindow(title, x, y, window_w, window_h,
  10.101 -                             SDL_WINDOW_SHOWN);
  10.102 -        if (!windows[i]) {
  10.103 -            fprintf(stderr, "Couldn't create window: %s\n", SDL_GetError());
  10.104 -            quit(2);
  10.105 -        }
  10.106 -    }
  10.107 -
  10.108 -    /* Loop, blitting sprites and waiting for a keystroke */
  10.109 +    /* Main render loop */
  10.110      done = 0;
  10.111      while (!done) {
  10.112          /* Check for events */
  10.113          while (SDL_PollEvent(&event)) {
  10.114 -            switch (event.type) {
  10.115 -            case SDL_KEYDOWN:
  10.116 -                /* Any keypress quits the app... */
  10.117 -            case SDL_QUIT:
  10.118 -                done = 1;
  10.119 -                break;
  10.120 -            default:
  10.121 -                break;
  10.122 -            }
  10.123 +            CommonEvent(state, &event, &done);
  10.124          }
  10.125      }
  10.126 -
  10.127      quit(0);
  10.128  }
  10.129