From 3ba1c8a1e3472b1949e027b076ecf22247527014 Mon Sep 17 00:00:00 2001 From: Jiang Jiang Date: Thu, 6 Aug 2009 08:59:53 +0000 Subject: [PATCH] Further polish API, fix crash in test program. --- include/SDL_video.h | 13 +++++++-- src/video/SDL_sysvideo.h | 3 +- src/video/SDL_video.c | 6 ++-- src/video/cocoa/SDL_cocoakeyboard.h | 2 +- src/video/cocoa/SDL_cocoakeyboard.m | 43 ++++++++++++++--------------- src/video/cocoa/SDL_cocoamodes.m | 5 ++++ test/testime.c | 7 ++++- 7 files changed, 46 insertions(+), 33 deletions(-) diff --git a/include/SDL_video.h b/include/SDL_video.h index b24350ae2..fd0a4a8ca 100644 --- a/include/SDL_video.h +++ b/include/SDL_video.h @@ -1468,14 +1468,14 @@ extern DECLSPEC void SDLCALL SDL_GL_SwapWindow(SDL_WindowID windowID); extern DECLSPEC void SDLCALL SDL_GL_DeleteContext(SDL_GLContext context); /** - * \fn void SDL_StartTextInput(SDL_WindowID windowID) + * \fn void SDL_StartTextInput(void) * - * \brief Start accepting Unicode text input events in given window. + * \brief Start accepting Unicode text input events. * * \sa SDL_StopTextInput() * \sa SDL_SetTextInputRect() */ -extern DECLSPEC void SDLCALL SDL_StartTextInput(SDL_WindowID windowID); +extern DECLSPEC void SDLCALL SDL_StartTextInput(void); /** * \fn void SDL_StopTextInput(void) @@ -1495,6 +1495,13 @@ extern DECLSPEC void SDLCALL SDL_StopTextInput(void); */ extern DECLSPEC void SDLCALL SDL_SetTextInputRect(SDL_Rect *rect); +/** + * \fn SDL_WindowID SDL_GetFocusWindow(void) + * + * \brief Get focused window. + */ +extern DECLSPEC SDL_WindowID SDLCALL SDL_GetFocusWindow(void); + /* Ends C function definitions when using C++ */ #ifdef __cplusplus /* *INDENT-OFF* */ diff --git a/src/video/SDL_sysvideo.h b/src/video/SDL_sysvideo.h index 4eec6e3c1..60af11315 100644 --- a/src/video/SDL_sysvideo.h +++ b/src/video/SDL_sysvideo.h @@ -278,7 +278,7 @@ struct SDL_VideoDevice void (*SuspendScreenSaver) (_THIS); /* Text input */ - void (*StartTextInput) (_THIS, SDL_Window *window); + void (*StartTextInput) (_THIS); void (*StopTextInput) (_THIS); void (*SetTextInputRect) (_THIS, SDL_Rect *rect); @@ -428,7 +428,6 @@ extern void SDL_OnWindowHidden(SDL_Window * window); extern void SDL_OnWindowResized(SDL_Window * window); extern void SDL_OnWindowFocusGained(SDL_Window * window); extern void SDL_OnWindowFocusLost(SDL_Window * window); -extern SDL_WindowID SDL_GetFocusWindow(void); #endif /* _SDL_sysvideo_h */ diff --git a/src/video/SDL_video.c b/src/video/SDL_video.c index 4e0719525..56314bac6 100644 --- a/src/video/SDL_video.c +++ b/src/video/SDL_video.c @@ -3068,12 +3068,10 @@ SDL_GetWindowWMInfo(SDL_WindowID windowID, struct SDL_SysWMinfo *info) } void -SDL_StartTextInput(SDL_WindowID windowID) +SDL_StartTextInput(void) { - SDL_Window *window = SDL_GetWindowFromID(windowID); - if (_this->StartTextInput) { - _this->StartTextInput(_this, window); + _this->StartTextInput(_this); } } diff --git a/src/video/cocoa/SDL_cocoakeyboard.h b/src/video/cocoa/SDL_cocoakeyboard.h index cd089fd9b..1dc53df1c 100644 --- a/src/video/cocoa/SDL_cocoakeyboard.h +++ b/src/video/cocoa/SDL_cocoakeyboard.h @@ -28,7 +28,7 @@ extern void Cocoa_InitKeyboard(_THIS); extern void Cocoa_HandleKeyEvent(_THIS, NSEvent * event); extern void Cocoa_QuitKeyboard(_THIS); -extern void Cocoa_StartTextInput(_THIS, SDL_Window *window); +extern void Cocoa_StartTextInput(_THIS); extern void Cocoa_StopTextInput(_THIS); extern void Cocoa_SetTextInputRect(_THIS, SDL_Rect *rect); diff --git a/src/video/cocoa/SDL_cocoakeyboard.m b/src/video/cocoa/SDL_cocoakeyboard.m index 3cbd7280d..ba806ec13 100644 --- a/src/video/cocoa/SDL_cocoakeyboard.m +++ b/src/video/cocoa/SDL_cocoakeyboard.m @@ -54,7 +54,7 @@ #define NX_DEVICERCTLKEYMASK 0x00002000 #endif -@interface SDLTranslatorResponder : NSTextView +@interface SDLTranslatorResponder : NSView { NSString *_markedText; NSRange _markedRange; @@ -97,7 +97,6 @@ - (void) insertText:(id) aString - (void) doCommandBySelector:(SEL) myselector { - NSLog(@"doCommandBySelector, passed down"); [super doCommandBySelector: myselector]; } @@ -152,7 +151,8 @@ - (void) unmarkText - (NSRect) firstRectForCharacterRange: (NSRange) theRange { float windowHeight = [[self window] frame].size.height; - NSRect rect = NSMakeRect(_inputRect.x, windowHeight - _inputRect.y, _inputRect.w, _inputRect.h); + NSRect rect = NSMakeRect(_inputRect.x, windowHeight - _inputRect.y - _inputRect.h, + _inputRect.w, _inputRect.h); NSLog(@"firstRectForCharacterRange: (%d, %d): windowHeight = %g, rect = %@", theRange.location, theRange.length, windowHeight, @@ -584,15 +584,9 @@ - (NSArray *) validAttributesForMarkedText { SDL_VideoData *data = (SDL_VideoData *) _this->driverdata; SDL_Keyboard keyboard; - NSAutoreleasePool *pool; - pool = [[NSAutoreleasePool alloc] init]; - data->fieldEdit = [[SDLTranslatorResponder alloc] initWithFrame:NSMakeRect(0.0, 0.0, 0.0, 0.0)]; - [pool release]; - SDL_zero(keyboard); data->keyboard = SDL_AddKeyboard(&keyboard, -1); - [data->fieldEdit setKeyboard: data->keyboard]; UpdateKeymap(data); /* Set our own names for the platform-dependent but layout-independent keys */ @@ -605,20 +599,25 @@ - (NSArray *) validAttributesForMarkedText } void -Cocoa_StartTextInput(_THIS, SDL_Window *window) +Cocoa_StartTextInput(_THIS) { - SDL_VideoData *videoData = (SDL_VideoData *) _this->driverdata; - SDL_WindowData *windowData = (SDL_WindowData *) window->driverdata; - NSView *parentView = [windowData->window contentView]; + SDL_VideoData *data = (SDL_VideoData *) _this->driverdata; + NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; + NSView *parentView = [[NSApp keyWindow] contentView]; + + data->fieldEdit = [[SDLTranslatorResponder alloc] initWithFrame:NSMakeRect(0.0, 0.0, 0.0, 0.0)]; + [data->fieldEdit setKeyboard: data->keyboard]; - if (! [[videoData->fieldEdit superview] isEqual: parentView]) + if (! [[data->fieldEdit superview] isEqual: parentView]) { NSLog(@"add fieldEdit to window contentView"); - [videoData->fieldEdit removeFromSuperview]; - [parentView addSubview: videoData->fieldEdit]; - [windowData->window makeFirstResponder: videoData->fieldEdit]; + [data->fieldEdit removeFromSuperview]; + [parentView addSubview: data->fieldEdit]; + [[NSApp keyWindow] makeFirstResponder: data->fieldEdit]; } + [pool release]; + SDL_EventState(SDL_TEXTINPUT, SDL_ENABLE); SDL_EventState(SDL_TEXTEDITING, SDL_ENABLE); } @@ -636,7 +635,12 @@ - (NSArray *) validAttributesForMarkedText { SDL_VideoData *data = (SDL_VideoData *) _this->driverdata; + NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; [data->fieldEdit removeFromSuperview]; + [data->fieldEdit release]; + data->fieldEdit = nil; + [pool release]; + SDL_EventState(SDL_TEXTINPUT, SDL_IGNORE); SDL_EventState(SDL_TEXTEDITING, SDL_IGNORE); } @@ -676,7 +680,6 @@ - (NSArray *) validAttributesForMarkedText } if (SDL_EventState(SDL_TEXTINPUT, SDL_QUERY)) { /* FIXME CW 2007-08-16: only send those events to the field editor for which we actually want text events, not e.g. esc or function keys. Arrow keys in particular seem to produce crashes sometimes. */ - NSLog(@"interpretKeyEvents"); [data->fieldEdit interpretKeyEvents:[NSArray arrayWithObject:event]]; #if 0 text = [[event characters] UTF8String]; @@ -706,10 +709,6 @@ - (NSArray *) validAttributesForMarkedText NSAutoreleasePool *pool; SDL_DelKeyboard(data->keyboard); - - pool = [[NSAutoreleasePool alloc] init]; - [data->fieldEdit release]; - [pool release]; } /* vi: set ts=4 sw=4 expandtab: */ diff --git a/src/video/cocoa/SDL_cocoamodes.m b/src/video/cocoa/SDL_cocoamodes.m index 115abc3e0..e7eedd30e 100644 --- a/src/video/cocoa/SDL_cocoamodes.m +++ b/src/video/cocoa/SDL_cocoamodes.m @@ -248,6 +248,11 @@ - (void) setFrame:(NSRect)frame; CGReleaseDisplayFadeReservation(fade_token); } + NSRect frame = [[NSScreen mainScreen] frame]; + NSLog(@"mainScreen frame: %gx%g", frame.size.width, frame.size.height); + + [[NSApp mainWindow] makeKeyAndOrderFront: nil]; + /* There is a bug in Cocoa where NSScreen doesn't synchronize with CGDirectDisplay, so the main screen's frame is wrong. diff --git a/test/testime.c b/test/testime.c index da8dab56f..23cd8a2d4 100644 --- a/test/testime.c +++ b/test/testime.c @@ -65,6 +65,7 @@ void InitVideo(int argc, char *argv[]) void CleanupVideo() { + SDL_StopTextInput(); TTF_CloseFont(font); TTF_Quit(); } @@ -82,6 +83,8 @@ void InitInput() text[0] = 0; markedRect = textRect; markedText = NULL; + + SDL_StartTextInput(); } static void RenderText(SDL_Surface *sur, @@ -115,6 +118,8 @@ void Redraw() if (markedRect.w < 0) { SDL_Flip(screen); + // Stop text input because we cannot hold any more characters + SDL_StopTextInput(); return; } @@ -139,7 +144,7 @@ void Redraw() SDL_Flip(screen); - SDL_StartTextInput(&markedRect); + SDL_SetTextInputRect(&markedRect); } void