Further polish API, fix crash in test program. gsoc2009_IME
authorJiang Jiang <gzjjgod@gmail.com>
Thu, 06 Aug 2009 08:59:53 +0000
branchgsoc2009_IME
changeset 3136 962357f325e1
parent 3135 f4e553ec6a62
child 3137 311c678f3b2e
Further polish API, fix crash in test program.
include/SDL_video.h
src/video/SDL_sysvideo.h
src/video/SDL_video.c
src/video/cocoa/SDL_cocoakeyboard.h
src/video/cocoa/SDL_cocoakeyboard.m
src/video/cocoa/SDL_cocoamodes.m
test/testime.c
--- a/include/SDL_video.h	Thu Aug 06 08:56:48 2009 +0000
+++ b/include/SDL_video.h	Thu Aug 06 08:59:53 2009 +0000
@@ -1468,14 +1468,14 @@
 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_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* */
--- a/src/video/SDL_sysvideo.h	Thu Aug 06 08:56:48 2009 +0000
+++ b/src/video/SDL_sysvideo.h	Thu Aug 06 08:59:53 2009 +0000
@@ -278,7 +278,7 @@
     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_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 */
 
--- a/src/video/SDL_video.c	Thu Aug 06 08:56:48 2009 +0000
+++ b/src/video/SDL_video.c	Thu Aug 06 08:59:53 2009 +0000
@@ -3068,12 +3068,10 @@
 }
 
 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);
     }
 }
 
--- a/src/video/cocoa/SDL_cocoakeyboard.h	Thu Aug 06 08:56:48 2009 +0000
+++ b/src/video/cocoa/SDL_cocoakeyboard.h	Thu Aug 06 08:59:53 2009 +0000
@@ -28,7 +28,7 @@
 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);
 
--- a/src/video/cocoa/SDL_cocoakeyboard.m	Thu Aug 06 08:56:48 2009 +0000
+++ b/src/video/cocoa/SDL_cocoakeyboard.m	Thu Aug 06 08:59:53 2009 +0000
@@ -54,7 +54,7 @@
     #define NX_DEVICERCTLKEYMASK    0x00002000
 #endif
 
-@interface SDLTranslatorResponder : NSTextView
+@interface SDLTranslatorResponder : NSView <NSTextInput>
 {
     NSString *_markedText;
     NSRange   _markedRange;
@@ -97,7 +97,6 @@
 
 - (void) doCommandBySelector:(SEL) myselector
 {
-    NSLog(@"doCommandBySelector, passed down");
     [super doCommandBySelector: myselector];
 }
 
@@ -152,7 +151,8 @@
 - (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 @@
 {
     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 @@
 }
 
 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];
 
-    if (! [[videoData->fieldEdit superview] isEqual: parentView])
+    data->fieldEdit = [[SDLTranslatorResponder alloc] initWithFrame:NSMakeRect(0.0, 0.0, 0.0, 0.0)];
+    [data->fieldEdit setKeyboard: data->keyboard];
+
+    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 @@
 {
     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 @@
         }
         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 @@
     NSAutoreleasePool *pool;
 
     SDL_DelKeyboard(data->keyboard);
-
-    pool = [[NSAutoreleasePool alloc] init];
-    [data->fieldEdit release];
-    [pool release];
 }
 
 /* vi: set ts=4 sw=4 expandtab: */
--- a/src/video/cocoa/SDL_cocoamodes.m	Thu Aug 06 08:56:48 2009 +0000
+++ b/src/video/cocoa/SDL_cocoamodes.m	Thu Aug 06 08:59:53 2009 +0000
@@ -248,6 +248,11 @@
         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.
--- a/test/testime.c	Thu Aug 06 08:56:48 2009 +0000
+++ b/test/testime.c	Thu Aug 06 08:59:53 2009 +0000
@@ -65,6 +65,7 @@
 
 void CleanupVideo()
 {
+    SDL_StopTextInput();
     TTF_CloseFont(font);
     TTF_Quit();
 }
@@ -82,6 +83,8 @@
     text[0] = 0;
     markedRect = textRect;
     markedText = NULL;
+
+    SDL_StartTextInput();
 }
 
 static void RenderText(SDL_Surface *sur,
@@ -115,6 +118,8 @@
     if (markedRect.w < 0)
     {
         SDL_Flip(screen);
+        // Stop text input because we cannot hold any more characters
+        SDL_StopTextInput();
         return;
     }
 
@@ -139,7 +144,7 @@
 
     SDL_Flip(screen);
 
-    SDL_StartTextInput(&markedRect);
+    SDL_SetTextInputRect(&markedRect);
 }
 
 void