From c8aa1fb7c8ed44886599ea4e04eed403392e2e34 Mon Sep 17 00:00:00 2001 From: Geenz Date: Mon, 17 Dec 2012 18:00:30 -0500 Subject: LLWindow: Move to using Cocoa for window and view creation along with setting up callbacks for event handling as such. --- indra/newview/llviewerkeyboard.cpp | 2 ++ 1 file changed, 2 insertions(+) (limited to 'indra/newview/llviewerkeyboard.cpp') diff --git a/indra/newview/llviewerkeyboard.cpp b/indra/newview/llviewerkeyboard.cpp index 1aa9fd8a45..6d91dad70e 100644 --- a/indra/newview/llviewerkeyboard.cpp +++ b/indra/newview/llviewerkeyboard.cpp @@ -653,6 +653,8 @@ BOOL LLViewerKeyboard::modeFromString(const std::string& string, S32 *mode) BOOL LLViewerKeyboard::handleKey(KEY translated_key, MASK translated_mask, BOOL repeated) { + LL_INFOS("Keyboard Handling") << "Handling key " << translated_key << LL_ENDL; + LL_INFOS("Keyboard Handling") << "Keyboard has focus? " << gFocusMgr.getKeyboardFocus() << LL_ENDL; // check for re-map EKeyboardMode mode = gViewerKeyboard.getMode(); U32 keyidx = (translated_mask<<16) | translated_key; -- cgit v1.3 From ddb48d51d996b18063b111faa3b7e709160074d9 Mon Sep 17 00:00:00 2001 From: Geenz Date: Tue, 1 Jan 2013 11:32:53 -0500 Subject: More things in this commit: - Removed the callback system in favor of simply defining functions in a header to later be implemented in whichever file is most convenient for what we want to do (i.e., calling LLWindow callbacks within LLWindowMacOSX, setting cursors in llwindowmacosx-objc.mm, etc.) - Viewer shutdown now works appropriately - Added a bit of debugging code to test if a key has been handled by the UI or not (useful for tracking down the mystery of the enter key not being handled) - Setup a cocoa quit handler within the application delegate that intercepts any termination requests --- indra/llwindow/llopenglview-objc.h | 30 ---- indra/llwindow/llopenglview-objc.mm | 214 ++++++++++------------------ indra/llwindow/llwindowmacosx-objc.h | 38 +++-- indra/llwindow/llwindowmacosx-objc.mm | 74 ++-------- indra/llwindow/llwindowmacosx.cpp | 73 +++++++--- indra/llwindow/llwindowmacosx.h | 2 +- indra/newview/llappviewermacosx-delegate.mm | 18 ++- indra/newview/llappviewermacosx-objc.h | 2 +- indra/newview/llappviewermacosx.cpp | 32 ++--- indra/newview/llviewerkeyboard.cpp | 2 +- 10 files changed, 201 insertions(+), 284 deletions(-) (limited to 'indra/newview/llviewerkeyboard.cpp') diff --git a/indra/llwindow/llopenglview-objc.h b/indra/llwindow/llopenglview-objc.h index 6b055bc665..20589e321d 100644 --- a/indra/llwindow/llopenglview-objc.h +++ b/indra/llwindow/llopenglview-objc.h @@ -15,7 +15,6 @@ @interface LLOpenGLView : NSOpenGLView { NSPoint mousePos; - ResizeCallback mResizeCallback; } - (id) initWithFrame:(NSRect)frame withSamples:(NSUInteger)samples andVsync:(BOOL)vsync; @@ -33,42 +32,13 @@ - (CGLContextObj) getCGLContextObj; - (CGLPixelFormatObj*)getCGLPixelFormatObj; -- (void) registerResizeCallback:(ResizeCallback)callback; @end @interface LLNSWindow : NSWindow { float mMousePos[2]; unsigned int mModifiers; - - KeyCallback mKeyDownCallback; - KeyCallback mKeyUpCallback; - UnicodeCallback mUnicodeCallback; - ModifierCallback mModifierCallback; - MouseCallback mMouseDownCallback; - MouseCallback mMouseUpCallback; - MouseCallback mMouseDoubleClickCallback; - MouseCallback mRightMouseDownCallback; - MouseCallback mRightMouseUpCallback; - MouseCallback mMouseMovedCallback; - ScrollWheelCallback mScrollWhellCallback; - VoidCallback mMouseExitCallback; - MouseCallback mDeltaUpdateCallback; } -- (void) registerKeyDownCallback:(KeyCallback)callback; -- (void) registerKeyUpCallback:(KeyCallback)callback; -- (void) registerUnicodeCallback:(UnicodeCallback)callback; -- (void) registerModifierCallback:(ModifierCallback)callback; -- (void) registerMouseDownCallback:(MouseCallback)callback; -- (void) registerMouseUpCallback:(MouseCallback)callback; -- (void) registerRightMouseDownCallback:(MouseCallback)callback; -- (void) registerRightMouseUpCallback:(MouseCallback)callback; -- (void) registerDoubleClickCallback:(MouseCallback)callback; -- (void) registerMouseMovedCallback:(MouseCallback)callback; -- (void) registerScrollCallback:(ScrollWheelCallback)callback; -- (void) registerMouseExitCallback:(VoidCallback)callback; -- (void) registerDeltaUpdateCallback:(MouseCallback)callback; - @end void setLLNSWindowRef(LLNSWindow* window); diff --git a/indra/llwindow/llopenglview-objc.mm b/indra/llwindow/llopenglview-objc.mm index 7c148f4acd..cb8d7b315f 100644 --- a/indra/llwindow/llopenglview-objc.mm +++ b/indra/llwindow/llopenglview-objc.mm @@ -19,12 +19,9 @@ - (void)windowResized:(NSNotification *)notification; { - if (mResizeCallback != nil) - { - NSSize size = [[self window] frame].size; - - mResizeCallback(size.width, size.height); - } + NSSize size = [self frame].size; + + callResize(size.width, size.height); } - (void)dealloc @@ -125,11 +122,6 @@ return (CGLPixelFormatObj*)[fmt CGLPixelFormatObj]; } -- (void) registerResizeCallback:(ResizeCallback)callback -{ - mResizeCallback = callback; -} - // Various events can be intercepted by our view, thus not reaching our window. // Intercept these events, and pass them to the window as needed. - Geenz @@ -183,6 +175,16 @@ [super flagsChanged:theEvent]; } +- (BOOL) becomeFirstResponder +{ + return [super becomeFirstResponder]; +} + +- (BOOL) resignFirstResponder +{ + return [super resignFirstResponder]; +} + @end // We use a custom NSWindow for our event handling. @@ -196,30 +198,19 @@ return self; } -- (void) keyDown:(NSEvent *)theEvent { - if (mKeyDownCallback != nil && mUnicodeCallback != nil) +- (void) keyDown:(NSEvent *)theEvent +{ + callKeyDown([theEvent keyCode], [theEvent modifierFlags]); + + NSString *chars = [theEvent characters]; + for (uint i = 0; i < [chars length]; i++) { - mKeyDownCallback([theEvent keyCode], [theEvent modifierFlags]); - - NSString *chars = [theEvent charactersIgnoringModifiers]; - for (uint i = 0; i < [chars length]; i++) - { - mUnicodeCallback([chars characterAtIndex:i], [theEvent modifierFlags]); - } - - // The viewer expects return to be submitted separately as a unicode character. - if ([theEvent keyCode] == 3 || [theEvent keyCode] == 13) - { - mUnicodeCallback([theEvent keyCode], [theEvent modifierFlags]); - } + callUnicodeCallback([chars characterAtIndex:i], [theEvent modifierFlags]); } } - (void) keyUp:(NSEvent *)theEvent { - if (mKeyUpCallback != nil) - { - mKeyUpCallback([theEvent keyCode], [theEvent modifierFlags]); - } + callKeyUp([theEvent keyCode], [theEvent modifierFlags]); } - (void)flagsChanged:(NSEvent *)theEvent { @@ -228,59 +219,42 @@ - (void) mouseDown:(NSEvent *)theEvent { - if (mMouseDoubleClickCallback != nil && mMouseDownCallback != nil) + if ([theEvent clickCount] >= 2) { - if ([theEvent clickCount] == 2) - { - mMouseDoubleClickCallback(mMousePos, [theEvent modifierFlags]); - } else if ([theEvent clickCount] == 1) { - mMouseDownCallback(mMousePos, [theEvent modifierFlags]); - } + callDoubleClick(mMousePos, [theEvent modifierFlags]); + } else if ([theEvent clickCount] == 1) { + callLeftMouseDown(mMousePos, [theEvent modifierFlags]); } } - (void) mouseUp:(NSEvent *)theEvent { - if (mMouseUpCallback != nil) - { - mMouseUpCallback(mMousePos, [theEvent modifierFlags]); - } + callLeftMouseUp(mMousePos, [theEvent modifierFlags]); } - (void) rightMouseDown:(NSEvent *)theEvent { - if (mRightMouseDownCallback != nil) - { - mRightMouseDownCallback(mMousePos, [theEvent modifierFlags]); - } + callRightMouseDown(mMousePos, [theEvent modifierFlags]); } - (void) rightMouseUp:(NSEvent *)theEvent { - if (mRightMouseUpCallback != nil) - { - mRightMouseUpCallback(mMousePos, [theEvent modifierFlags]); - } + callRightMouseUp(mMousePos, [theEvent modifierFlags]); } -- (void)mouseMoved:(NSEvent *)theEvent { - if (mDeltaUpdateCallback != nil && mMouseMovedCallback != nil) - { - float mouseDeltas[2] = { - [theEvent deltaX], - [theEvent deltaZ] - }; - - mDeltaUpdateCallback(mouseDeltas, 0); - - NSPoint mPoint = [theEvent locationInWindow]; - mMousePos[0] = mPoint.x; - mMousePos[1] = mPoint.y; - if (mMouseMovedCallback != nil) - { - mMouseMovedCallback(mMousePos, 0); - } - } +- (void)mouseMoved:(NSEvent *)theEvent +{ + float mouseDeltas[2] = { + [theEvent deltaX], + [theEvent deltaY] + }; + + callDeltaUpdate(mouseDeltas, 0); + + NSPoint mPoint = [theEvent locationInWindow]; + mMousePos[0] = mPoint.x; + mMousePos[1] = mPoint.y; + callMouseMoved(mMousePos, 0); } // NSWindow doesn't trigger mouseMoved when the mouse is being clicked and dragged. @@ -288,101 +262,59 @@ - (void) mouseDragged:(NSEvent *)theEvent { - if (mDeltaUpdateCallback != nil && mMouseMovedCallback != nil) - { - float mouseDeltas[2] = { - [theEvent deltaX], - [theEvent deltaZ] - }; - - mDeltaUpdateCallback(mouseDeltas, 0); - - NSPoint mPoint = [theEvent locationInWindow]; - mMousePos[0] = mPoint.x; - mMousePos[1] = mPoint.y; - mMouseMovedCallback(mMousePos, 0); - } -} - -- (void) scrollWheel:(NSEvent *)theEvent -{ - if (mScrollWhellCallback != nil) - { - mScrollWhellCallback(-[theEvent deltaY]); - } -} - -- (void) mouseExited:(NSEvent *)theEvent -{ - if (mMouseExitCallback != nil) - { - mMouseExitCallback(); - } -} - -- (void) registerKeyDownCallback:(KeyCallback)callback -{ - mKeyDownCallback = callback; -} - -- (void) registerKeyUpCallback:(KeyCallback)callback -{ - mKeyUpCallback = callback; -} - -- (void) registerUnicodeCallback:(UnicodeCallback)callback -{ - mUnicodeCallback = callback; -} - -- (void) registerModifierCallback:(ModifierCallback)callback -{ - mModifierCallback = callback; -} - -- (void) registerMouseDownCallback:(MouseCallback)callback -{ - mMouseDownCallback = callback; -} - -- (void) registerMouseUpCallback:(MouseCallback)callback -{ - mMouseUpCallback = callback; + // Trust the deltas supplied by NSEvent. + // The old CoreGraphics APIs we previously relied on are now flagged as obsolete. + // NSEvent isn't obsolete, and provides us with the correct deltas. + float mouseDeltas[2] = { + [theEvent deltaX], + [theEvent deltaY] + }; + + callDeltaUpdate(mouseDeltas, 0); + + NSPoint mPoint = [theEvent locationInWindow]; + mMousePos[0] = mPoint.x; + mMousePos[1] = mPoint.y; + callMouseMoved(mMousePos, 0); } -- (void) registerRightMouseDownCallback:(MouseCallback)callback +- (void) otherMouseDown:(NSEvent *)theEvent { - mRightMouseDownCallback = callback; + callMiddleMouseDown(mMousePos, 0); } -- (void) registerRightMouseUpCallback:(MouseCallback)callback +- (void) otherMouseUp:(NSEvent *)theEvent { - mRightMouseUpCallback = callback; + callMiddleMouseUp(mMousePos, 0); } -- (void) registerDoubleClickCallback:(MouseCallback)callback +- (void) otherMouseDragged:(NSEvent *)theEvent { - mMouseDoubleClickCallback = callback; + } -- (void) registerMouseMovedCallback:(MouseCallback)callback +- (void) scrollWheel:(NSEvent *)theEvent { - mMouseMovedCallback = callback; + callScrollMoved(-[theEvent deltaY]); } -- (void) registerScrollCallback:(ScrollWheelCallback)callback +- (void) mouseExited:(NSEvent *)theEvent { - mScrollWhellCallback = callback; + callMouseExit(); } -- (void) registerMouseExitCallback:(VoidCallback)callback +- (BOOL) becomeFirstResponder { - mMouseExitCallback = callback; + NSLog(@"Window gained focus!"); + callFocus(); + return true; } -- (void) registerDeltaUpdateCallback:(MouseCallback)callback +- (BOOL) resignFirstResponder { - mDeltaUpdateCallback = callback; + NSLog(@"Window lost focus!"); + callFocus(); + return true; } @end diff --git a/indra/llwindow/llwindowmacosx-objc.h b/indra/llwindow/llwindowmacosx-objc.h index abaeda1f91..655d63ac12 100644 --- a/indra/llwindow/llwindowmacosx-objc.h +++ b/indra/llwindow/llwindowmacosx-objc.h @@ -65,21 +65,33 @@ void getCursorPos(NSWindowRef window, float* pos); void makeWindowOrderFront(NSWindowRef window); void convertScreenToWindow(NSWindowRef window, float *coord); void convertWindowToScreen(NSWindowRef window, float *coord); +void convertScreenToView(NSWindowRef window, float *coord); void setWindowPos(NSWindowRef window, float* pos); +void closeWindow(NSWindowRef window); +void removeGLView(GLViewRef view); + +// These are all implemented in llwindowmacosx.cpp. +// This is largely for easier interop between Obj-C and C++ (at least in the viewer's case due to the BOOL vs. BOOL conflict) +void callKeyUp(unsigned short key, unsigned int mask); +void callKeyDown(unsigned short key, unsigned int mask); +void callUnicodeCallback(wchar_t character, unsigned int mask); +void callRightMouseDown(float *pos, unsigned int mask); +void callRightMouseUp(float *pos, unsigned int mask); +void callLeftMouseDown(float *pos, unsigned int mask); +void callLeftMouseUp(float *pos, unsigned int mask); +void callDoubleClick(float *pos, unsigned int mask); +void callResize(unsigned int width, unsigned int height); +void callMouseMoved(float *pos, unsigned int mask); +void callScrollMoved(float delta); +void callMouseExit(); +void callWindowFocus(); +void callWindowUnfocus(); +void callDeltaUpdate(float *delta, unsigned int mask); +void callMiddleMouseDown(float *pos, unsigned int mask); +void callMiddleMouseUp(float *pos, unsigned int mask); +void callFocus(); +void callFocusLost(); -void registerKeyUpCallback(NSWindowRef window, KeyCallback callback); -void registerKeyDownCallback(NSWindowRef window, KeyCallback callback); -void registerUnicodeCallback(NSWindowRef window, UnicodeCallback callback); -void registerMouseUpCallback(NSWindowRef window, MouseCallback callback); -void registerMouseDownCallback(NSWindowRef window, MouseCallback callback); -void registerRightMouseUpCallback(NSWindowRef window, MouseCallback callback); -void registerRightMouseDownCallback(NSWindowRef window, MouseCallback callback); -void registerDoubleClickCallback(NSWindowRef window, MouseCallback callback); -void registerResizeEventCallback(GLViewRef window, ResizeCallback callback); -void registerMouseMovedCallback(NSWindowRef window, MouseCallback callback); -void registerScrollCallback(NSWindowRef window, ScrollWheelCallback callback); -void registerMouseExitCallback(NSWindowRef window, VoidCallback callback); -void registerDeltaUpdateCallback(NSWindowRef window, MouseCallback callback); NSWindowRef getMainAppWindow(); GLViewRef getGLView(NSWindowRef window); diff --git a/indra/llwindow/llwindowmacosx-objc.mm b/indra/llwindow/llwindowmacosx-objc.mm index 07efc25ea6..f3972303f1 100644 --- a/indra/llwindow/llwindowmacosx-objc.mm +++ b/indra/llwindow/llwindowmacosx-objc.mm @@ -255,6 +255,15 @@ void convertScreenToWindow(NSWindowRef window, float *coord) coord[1] = point.y; } +void convertScreenToView(NSWindowRef window, float *coord) +{ + NSRect point; + point.origin.x = coord[0]; + point.origin.y = coord[1]; + point.origin = [(LLNSWindow*)window convertScreenToBase:point.origin]; + point.origin = [[(LLNSWindow*)window contentView] convertPoint:point.origin fromView:nil]; +} + void convertWindowToScreen(NSWindowRef window, float *coord) { NSPoint point; @@ -265,73 +274,20 @@ void convertWindowToScreen(NSWindowRef window, float *coord) coord[1] = point.y; } -void registerKeyUpCallback(NSWindowRef window, std::tr1::function callback) -{ - [(LLNSWindow*)window registerKeyUpCallback:callback]; -} - -void registerKeyDownCallback(NSWindowRef window, std::tr1::function callback) -{ - [(LLNSWindow*)window registerKeyDownCallback:callback]; -} - -void registerUnicodeCallback(NSWindowRef window, std::tr1::function callback) -{ - [(LLNSWindow*)window registerUnicodeCallback:callback]; -} - -void registerMouseUpCallback(NSWindowRef window, MouseCallback callback) -{ - [(LLNSWindow*)window registerMouseUpCallback:callback]; -} - -void registerMouseDownCallback(NSWindowRef window, MouseCallback callback) -{ - [(LLNSWindow*)window registerMouseDownCallback:callback]; -} - -void registerRightMouseUpCallback(NSWindowRef window, MouseCallback callback) -{ - [(LLNSWindow*)window registerRightMouseUpCallback:callback]; -} - -void registerRightMouseDownCallback(NSWindowRef window, MouseCallback callback) -{ - [(LLNSWindow*)window registerRightMouseDownCallback:callback]; -} - -void registerDoubleClickCallback(NSWindowRef window, MouseCallback callback) -{ - [(LLNSWindow*)window registerDoubleClickCallback:callback]; -} - -void registerResizeEventCallback(GLViewRef glview, ResizeCallback callback) -{ - [(LLOpenGLView*)glview registerResizeCallback:callback]; -} - -void registerMouseMovedCallback(NSWindowRef window, MouseCallback callback) -{ - [(LLNSWindow*)window registerMouseMovedCallback:callback]; -} - -void registerScrollCallback(NSWindowRef window, ScrollWheelCallback callback) -{ - [(LLNSWindow*)window registerScrollCallback:callback]; -} - -void registerMouseExitCallback(NSWindowRef window, VoidCallback callback) +void closeWindow(NSWindowRef window) { - [(LLNSWindow*)window registerMouseExitCallback:callback]; + [(LLNSWindow*)window close]; } -void registerDeltaUpdateCallback(NSWindowRef window, MouseCallback callback) +void removeGLView(GLViewRef view) { - [(LLNSWindow*)window registerDeltaUpdateCallback:callback]; + [(LLOpenGLView*)view removeFromSuperview]; + [(LLOpenGLView*)view release]; } NSWindowRef getMainAppWindow() { + [(LLNSWindow*)winRef setAcceptsMouseMovedEvents:TRUE]; return winRef; } diff --git a/indra/llwindow/llwindowmacosx.cpp b/indra/llwindow/llwindowmacosx.cpp index a616b2df2d..cfdfbe2138 100644 --- a/indra/llwindow/llwindowmacosx.cpp +++ b/indra/llwindow/llwindowmacosx.cpp @@ -198,7 +198,7 @@ LLWindowMacOSX::LLWindowMacOSX(LLWindowCallbacks* callbacks, } -// These functions are used as callbacks for event handling within Cocoa. +// These functions are used as wrappers for our internal event handling callbacks. // It's a good idea to wrap these to avoid reworking more code than we need to within LLWindow. void callKeyUp(unsigned short key, unsigned int mask) @@ -216,9 +216,14 @@ void callUnicodeCallback(wchar_t character, unsigned int mask) gWindowImplementation->getCallbacks()->handleUnicodeChar(character, mask); } -void callModifierCallback(unsigned int mask) +void callFocus() { - + gWindowImplementation->getCallbacks()->handleFocus(gWindowImplementation); +} + +void callFocusLost() +{ + gWindowImplementation->getCallbacks()->handleFocusLost(gWindowImplementation); } void callRightMouseDown(float *pos, MASK mask) @@ -302,17 +307,41 @@ void callWindowUnfocus() void callDeltaUpdate(float *delta, MASK mask) { - gWindowImplementation->updateMouseDeltas(); + gWindowImplementation->updateMouseDeltas(delta); +} + +void callMiddleMouseDown(float *pos, MASK mask) +{ + LLCoordGL outCoords; + outCoords.mX = llround(pos[0]); + outCoords.mY = llround(pos[1]); + float deltas[2]; + gWindowImplementation->getMouseDeltas(deltas); + outCoords.mX += deltas[0]; + outCoords.mY += deltas[1]; + gWindowImplementation->getCallbacks()->handleMiddleMouseDown(gWindowImplementation, outCoords, mask); +} + +void callMiddleMouseUp(float *pos, MASK mask) +{ + LLCoordGL outCoords; + outCoords.mX = llround(pos[0]); + outCoords.mY = llround(pos[1]); + float deltas[2]; + gWindowImplementation->getMouseDeltas(deltas); + outCoords.mX += deltas[0]; + outCoords.mY += deltas[1]; + gWindowImplementation->getCallbacks()->handleMiddleMouseUp(gWindowImplementation, outCoords, mask); } -void LLWindowMacOSX::updateMouseDeltas() +void LLWindowMacOSX::updateMouseDeltas(float* deltas) { if (mCursorDecoupled) { - CGMouseDelta x, y; - CGGetLastMouseDelta( &x, &y ); - mCursorLastEventDeltaX = x; - mCursorLastEventDeltaY = y; + mCursorLastEventDeltaX = llround(deltas[0]); + mCursorLastEventDeltaY = llround(-deltas[1]); + + if (mCursorIgnoreNextDelta) { @@ -320,6 +349,7 @@ void LLWindowMacOSX::updateMouseDeltas() mCursorLastEventDeltaY = 0; mCursorIgnoreNextDelta = FALSE; } + LL_INFOS("Delta Update") << "Last event delta: " << mCursorLastEventDeltaX << ", " << mCursorLastEventDeltaY << LL_ENDL; } else { mCursorLastEventDeltaX = 0; mCursorLastEventDeltaY = 0; @@ -342,6 +372,7 @@ BOOL LLWindowMacOSX::createContext(int x, int y, int width, int height, int bits { LL_INFOS("Window") << "Creating window..." << LL_ENDL; mWindow = getMainAppWindow(); + /* LL_INFOS("Window") << "Registering key callbacks..." << LL_ENDL; registerKeyDownCallback(mWindow, callKeyDown); registerKeyUpCallback(mWindow, callKeyUp); @@ -355,6 +386,7 @@ BOOL LLWindowMacOSX::createContext(int x, int y, int width, int height, int bits registerScrollCallback(mWindow, callScrollMoved); registerDeltaUpdateCallback(mWindow, callDeltaUpdate); registerMouseExitCallback(mWindow, callMouseExit); + */ } if(mContext == NULL) @@ -363,7 +395,7 @@ BOOL LLWindowMacOSX::createContext(int x, int y, int width, int height, int bits // Our OpenGL view is already defined within SecondLife.xib. // Get the view instead. mGLView = createOpenGLView(mWindow); - registerResizeEventCallback(mGLView, callResize); + //registerResizeEventCallback(mGLView, callResize); mContext = getCGLContextObj(mGLView); // Since we just created the context, it needs to be set up. glNeedsInit = TRUE; @@ -439,9 +471,7 @@ void LLWindowMacOSX::destroyContext() if(mContext != NULL) { LL_DEBUGS("Window") << "destroyContext: unhooking drawable " << LL_ENDL; - CGLSetCurrentContext(NULL); - mContext = NULL; } // Clean up remaining GL state before blowing away window @@ -454,16 +484,25 @@ void LLWindowMacOSX::destroyContext() mPixelFormat = NULL; } - // Close the window - if(mWindow != NULL) - { - } - // Clean up the GL context if(mContext != NULL) { CGLDestroyContext(mContext); } + + // Destroy our LLOpenGLView + if(mGLView != NULL) + { + removeGLView(mGLView); + mGLView = NULL; + } + + // Close the window + if(mWindow != NULL) + { + closeWindow(mWindow); + mWindow = NULL; + } } diff --git a/indra/llwindow/llwindowmacosx.h b/indra/llwindow/llwindowmacosx.h index 487af7658f..c5e1b2290b 100644 --- a/indra/llwindow/llwindowmacosx.h +++ b/indra/llwindow/llwindowmacosx.h @@ -121,7 +121,7 @@ public: void* getWindow() { return mWindow; } LLWindowCallbacks* getCallbacks() { return mCallbacks; } - void updateMouseDeltas(); + void updateMouseDeltas(float* deltas); void getMouseDeltas(float* delta); diff --git a/indra/newview/llappviewermacosx-delegate.mm b/indra/newview/llappviewermacosx-delegate.mm index 80cc1010e9..2e25997d32 100644 --- a/indra/newview/llappviewermacosx-delegate.mm +++ b/indra/newview/llappviewermacosx-delegate.mm @@ -23,17 +23,26 @@ frameTimer = nil; setLLNSWindowRef([self window]); - setLLOpenGLViewRef([self glview]); + //setLLOpenGLViewRef([self glview]); if (initViewer()) { - frameTimer = [NSTimer scheduledTimerWithTimeInterval:1.0/60 target:self selector:@selector(mainLoop) userInfo:nil repeats:YES]; + frameTimer = [NSTimer scheduledTimerWithTimeInterval:0.0 target:self selector:@selector(mainLoop) userInfo:nil repeats:YES]; } else { handleQuit(); } } -- (void) applicationWillTerminate:(NSNotification *)notification +- (NSApplicationDelegateReply) applicationShouldTerminate:(NSApplication *)sender { + if (!runMainLoop()) + { + handleQuit(); + return NSTerminateCancel; + } else { + [frameTimer release]; + cleanupViewer(); + return NSTerminateNow; + } } - (void) mainLoop @@ -42,7 +51,8 @@ if (appExiting) { [frameTimer release]; - handleQuit(); + cleanupViewer(); + [[NSApplication sharedApplication] terminate:self]; } } diff --git a/indra/newview/llappviewermacosx-objc.h b/indra/newview/llappviewermacosx-objc.h index 9ece6a1a6a..ed9017ca5d 100644 --- a/indra/newview/llappviewermacosx-objc.h +++ b/indra/newview/llappviewermacosx-objc.h @@ -16,4 +16,4 @@ bool initViewer(); void handleQuit(); bool runMainLoop(); void initMainLoop(); -void destroyMainLoop(); \ No newline at end of file +void cleanupViewer(); \ No newline at end of file diff --git a/indra/newview/llappviewermacosx.cpp b/indra/newview/llappviewermacosx.cpp index a1c8b7699d..6e7b91347b 100644 --- a/indra/newview/llappviewermacosx.cpp +++ b/indra/newview/llappviewermacosx.cpp @@ -55,15 +55,6 @@ namespace char** gArgV; LLAppViewerMacOSX* gViewerAppPtr; - - OSErr AEQuitHandler(const AppleEvent *messagein, AppleEvent *reply, long refIn) - { - OSErr result = noErr; - - LLAppViewer::instance()->userQuit(); - - return(result); - } } bool initViewer() @@ -95,26 +86,33 @@ bool initViewer() void handleQuit() { - if(!LLApp::isError()) - { - gViewerAppPtr->cleanup(); - } - - delete gViewerAppPtr; - gViewerAppPtr = NULL; + LLAppViewer::instance()->userQuit(); } bool runMainLoop() { bool ret = LLApp::isQuitting(); - if (!ret) + if (!ret && gViewerAppPtr != NULL) { ret = gViewerAppPtr->mainLoop(); + } else { + ret = true; } return ret; } +void cleanupViewer() +{ + if(!LLApp::isError()) + { + gViewerAppPtr->cleanup(); + } + + delete gViewerAppPtr; + gViewerAppPtr = NULL; +} + int main( int argc, char **argv ) { // Store off the command line args for use later. diff --git a/indra/newview/llviewerkeyboard.cpp b/indra/newview/llviewerkeyboard.cpp index 6d91dad70e..92d8f2937e 100644 --- a/indra/newview/llviewerkeyboard.cpp +++ b/indra/newview/llviewerkeyboard.cpp @@ -654,7 +654,6 @@ BOOL LLViewerKeyboard::modeFromString(const std::string& string, S32 *mode) BOOL LLViewerKeyboard::handleKey(KEY translated_key, MASK translated_mask, BOOL repeated) { LL_INFOS("Keyboard Handling") << "Handling key " << translated_key << LL_ENDL; - LL_INFOS("Keyboard Handling") << "Keyboard has focus? " << gFocusMgr.getKeyboardFocus() << LL_ENDL; // check for re-map EKeyboardMode mode = gViewerKeyboard.getMode(); U32 keyidx = (translated_mask<<16) | translated_key; @@ -677,6 +676,7 @@ BOOL LLViewerKeyboard::handleKey(KEY translated_key, MASK translated_mask, BOOL if(mKeysSkippedByUI.find(translated_key) != mKeysSkippedByUI.end()) { mKeyHandledByUI[translated_key] = FALSE; + LL_INFOS("Keyboard Handling") << "Key wasn't handled by UI!" << LL_ENDL; } else { -- cgit v1.3 From 258b77b64777a5ce5fef0ef066aa52b34b43ba65 Mon Sep 17 00:00:00 2001 From: Geenz Date: Mon, 25 Mar 2013 05:26:55 -0400 Subject: Additional IME support. LLPreeditor is largely good to go at this point, but there's still some work to do in getSegments. --- indra/llwindow/llkeyboardmacosx.cpp | 1 - indra/llwindow/llopenglview-objc.h | 4 ++ indra/llwindow/llopenglview-objc.mm | 97 ++++++++++++++++++++++---- indra/llwindow/llwindowmacosx-objc.h | 11 +++ indra/llwindow/llwindowmacosx-objc.mm | 45 ++++++++++-- indra/llwindow/llwindowmacosx.cpp | 126 ++++++++++++++++++++++++++++++---- indra/newview/Info-SecondLife.plist | 2 +- indra/newview/llviewerkeyboard.cpp | 1 - indra/newview/llviewerwindow.cpp | 1 - 9 files changed, 252 insertions(+), 36 deletions(-) (limited to 'indra/newview/llviewerkeyboard.cpp') diff --git a/indra/llwindow/llkeyboardmacosx.cpp b/indra/llwindow/llkeyboardmacosx.cpp index 274e92c225..85bb7b9aeb 100644 --- a/indra/llwindow/llkeyboardmacosx.cpp +++ b/indra/llwindow/llkeyboardmacosx.cpp @@ -253,7 +253,6 @@ BOOL LLKeyboardMacOSX::handleKeyUp(const U16 key, const U32 mask) if(translateNumpadKey(key, &translated_key)) { - LL_INFOS("Keyboard") << "Handled key!" << LL_ENDL; handled = handleTranslatedKeyUp(translated_key, translated_mask); } diff --git a/indra/llwindow/llopenglview-objc.h b/indra/llwindow/llopenglview-objc.h index 5134063193..8140421e44 100644 --- a/indra/llwindow/llopenglview-objc.h +++ b/indra/llwindow/llopenglview-objc.h @@ -20,6 +20,8 @@ std::string mLastDraggedUrl; unsigned int mModifiers; float mMousePos[2]; + bool mHasMarkedText; + unsigned int mMarkedTextLength; } - (id) initWithSamples:(NSUInteger)samples; - (id) initWithSamples:(NSUInteger)samples andVsync:(BOOL)vsync; @@ -40,6 +42,8 @@ - (unsigned long) getVramSize; +- (segment_t) getSegments:(NSAttributedString*)str; + @end @interface LLNSWindow : NSWindow diff --git a/indra/llwindow/llopenglview-objc.mm b/indra/llwindow/llopenglview-objc.mm index de159816e0..31b0e02ad8 100644 --- a/indra/llwindow/llopenglview-objc.mm +++ b/indra/llwindow/llopenglview-objc.mm @@ -281,15 +281,18 @@ - (void) keyDown:(NSEvent *)theEvent { [[self inputContext] handleEvent:theEvent]; - uint keycode = [theEvent keyCode]; - callKeyDown(keycode, mModifiers); - - // OS X intentionally does not send us key-up information on cmd-key combinations. - // This behaviour is not a bug, and only applies to cmd-combinations (no others). - // Since SL assumes we receive those, we fake it here. - if (mModifiers & NSCommandKeyMask) + if (!mHasMarkedText) { - callKeyUp([theEvent keyCode], mModifiers); + uint keycode = [theEvent keyCode]; + callKeyDown(keycode, mModifiers); + + // OS X intentionally does not send us key-up information on cmd-key combinations. + // This behaviour is not a bug, and only applies to cmd-combinations (no others). + // Since SL assumes we receive those, we fake it here. + if (mModifiers & NSCommandKeyMask) + { + callKeyUp([theEvent keyCode], mModifiers); + } } } @@ -348,7 +351,7 @@ - (BOOL)hasMarkedText { - return NO; + return mHasMarkedText; } - (NSRange)markedRange @@ -365,21 +368,73 @@ return NSMakeRange(range[0], range[1]); } -- (void)setMarkedText:(id)aString selectedRange:(NSRange)selectedRange replacementRange:(NSRange)replacementRange +- (segment_t) getSegments:(NSAttributedString*)str { + segment_t segments; + + int segment = 0; + + NSRange l; + NSRange r = NSMakeRange(0, [str length]); + + while (r.length > 0) + { + NSNumber *segmentAttrib = [str attribute:NSUnderlineStyleAttributeName atIndex:r.location longestEffectiveRange:&l inRange:r]; + + r = NSMakeRange(NSMaxRange(l), NSMaxRange(r) - NSMaxRange(l)); + bool standout; + if ([segmentAttrib integerValue] == 1) + { + standout = false; + } else { + standout = true; + } + segments.insert(std::pair(l.length, standout)); + + segment++; + } + return segments; +} + +- (void)setMarkedText:(id)aString selectedRange:(NSRange)selectedRange replacementRange:(NSRange)replacementRange +{ + if ([aString class] == NSClassFromString(@"NSConcreteMutableAttributedString")) + { + unsigned int selected[2] = { + selectedRange.location, + selectedRange.length + }; + + unsigned int replacement[2] = { + replacementRange.location, + replacementRange.length + }; + + NSLog(@"Attributed string: %@", aString); + + unichar text[[aString length]]; + [[aString mutableString] getCharacters:text range:NSMakeRange(0, [aString length])]; + segment_t segments = [self getSegments:(NSAttributedString *)aString]; + setMarkedText(text, selected, replacement, [aString length], segments); + mHasMarkedText = TRUE; + mMarkedTextLength = [aString length]; + } } - (void)unmarkText { - + resetPreedit(); + mHasMarkedText = FALSE; } +// We don't support attributed strings. - (NSArray *)validAttributesForMarkedText { return [NSArray array]; } +// See above. - (NSAttributedString *)attributedSubstringForProposedRange:(NSRange)aRange actualRange:(NSRangePointer)actualRange { return nil; @@ -387,9 +442,21 @@ - (void)insertText:(id)aString replacementRange:(NSRange)replacementRange { - for (NSInteger i = 0; i < [aString length]; i++) + if (!mHasMarkedText) { - callUnicodeCallback([aString characterAtIndex:i], mModifiers); + for (NSInteger i = 0; i < [aString length]; i++) + { + callUnicodeCallback([aString characterAtIndex:i], mModifiers); + } + } else { + // We may never get this point since unmarkText may be called before insertText ever gets called once we submit our text. + // But just in case... + resetPreedit(); + for (NSInteger i = 0; i < [aString length]; i++) + { + handleUnicodeCharacter([aString characterAtIndex:i]); + } + mHasMarkedText = FALSE; } } @@ -412,7 +479,9 @@ - (NSRect)firstRectForCharacterRange:(NSRange)aRange actualRange:(NSRangePointer)actualRange { - return NSZeroRect; + float pos[4] = {0, 0, 0, 0}; + getPreeditLocation(pos, mMarkedTextLength); + return NSMakeRect(pos[0], pos[1], pos[2], pos[3]); } - (void)doCommandBySelector:(SEL)aSelector diff --git a/indra/llwindow/llwindowmacosx-objc.h b/indra/llwindow/llwindowmacosx-objc.h index 57bb071690..2de185fed3 100644 --- a/indra/llwindow/llwindowmacosx-objc.h +++ b/indra/llwindow/llwindowmacosx-objc.h @@ -25,6 +25,10 @@ * $/LicenseInfo$ */ +#include + +typedef std::map segment_t; + // This will actually hold an NSCursor*, but that type is only available in objective C. typedef void *CursorRef; typedef void *NSWindowRef; @@ -71,6 +75,8 @@ void makeWindowOrderFront(NSWindowRef window); void convertScreenToWindow(NSWindowRef window, float *coord); void convertWindowToScreen(NSWindowRef window, float *coord); void convertScreenToView(NSWindowRef window, float *coord); +void convertRectToScreen(NSWindowRef window, float *coord); +void convertRectFromScreen(NSWindowRef window, float *coord); void setWindowPos(NSWindowRef window, float* pos); void closeWindow(NSWindowRef window); void removeGLView(GLViewRef view); @@ -113,6 +119,11 @@ void getPreeditSelectionRange(int *position, int *length); void getPreeditMarkedRange(int *position, int *length); void handleUnicodeCharacter(wchar_t c); void updatePreeditor(unsigned short *str); +void setPreeditMarkedRange(int position, int length); +void resetPreedit(); +int wstring_length(const std::basic_string & wstr, const int woffset, const int utf16_length, int *unaligned); +void setMarkedText(unsigned short *text, unsigned int *selectedRange, unsigned int *replacementRange, long text_len, segment_t segments); +void getPreeditLocation(float *location, unsigned int length); NSWindowRef getMainAppWindow(); GLViewRef getGLView(); diff --git a/indra/llwindow/llwindowmacosx-objc.mm b/indra/llwindow/llwindowmacosx-objc.mm index 9530785b83..1a0647485c 100644 --- a/indra/llwindow/llwindowmacosx-objc.mm +++ b/indra/llwindow/llwindowmacosx-objc.mm @@ -119,6 +119,7 @@ CursorRef createImageCursor(const char *fullpath, int hotspotX, int hotspotY) void setArrowCursor() { NSCursor *cursor = [NSCursor arrowCursor]; + [NSCursor unhide]; [cursor set]; } @@ -290,12 +291,44 @@ void makeWindowOrderFront(NSWindowRef window) void convertScreenToWindow(NSWindowRef window, float *coord) { - NSPoint point; - point.x = coord[0]; - point.y = coord[1]; - point = [(LLNSWindow*)window convertScreenToBase:point]; - coord[0] = point.x; - coord[1] = point.y; + NSRect point; + point.origin.x = coord[0]; + point.origin.y = coord[1]; + point = [(LLNSWindow*)window convertRectFromScreen:point]; + coord[0] = point.origin.x; + coord[1] = point.origin.y; +} + +void convertRectToScreen(NSWindowRef window, float *coord) +{ + NSRect point; + point.origin.x = coord[0]; + point.origin.y = coord[1]; + point.size.width = coord[2]; + point.size.height = coord[3]; + + point = [(LLNSWindow*)window convertRectToScreen:point]; + + coord[0] = point.origin.x; + coord[1] = point.origin.y; + coord[2] = point.size.width; + coord[3] = point.size.height; +} + +void convertRectFromScreen(NSWindowRef window, float *coord) +{ + NSRect point; + point.origin.x = coord[0]; + point.origin.y = coord[1]; + point.size.width = coord[2]; + point.size.height = coord[3]; + + point = [(LLNSWindow*)window convertRectFromScreen:point]; + + coord[0] = point.origin.x; + coord[1] = point.origin.y; + coord[2] = point.size.width; + coord[3] = point.size.height; } void convertScreenToView(NSWindowRef window, float *coord) diff --git a/indra/llwindow/llwindowmacosx.cpp b/indra/llwindow/llwindowmacosx.cpp index 7e4b9a84a1..4e8934b149 100644 --- a/indra/llwindow/llwindowmacosx.cpp +++ b/indra/llwindow/llwindowmacosx.cpp @@ -381,30 +381,27 @@ void callQuitHandler() } } -std::basic_string getPreeditString() +void getPreeditSelectionRange(int *position, int *length) { - std::basic_string str; if (gWindowImplementation->getPreeditor()) { - str = gWindowImplementation->getPreeditor()->getPreeditString(); + gWindowImplementation->getPreeditor()->getSelectionRange(position, length); } - - return str; } -void getPreeditSelectionRange(int *position, int *length) +void getPreeditMarkedRange(int *position, int *length) { if (gWindowImplementation->getPreeditor()) { - gWindowImplementation->getPreeditor()->getSelectionRange(position, length); + gWindowImplementation->getPreeditor()->getPreeditRange(position, length); } } -void getPreeditMarkedRange(int *position, int *length) +void setPreeditMarkedRange(int position, int length) { if (gWindowImplementation->getPreeditor()) { - gWindowImplementation->getPreeditor()->getPreeditRange(position, length); + gWindowImplementation->getPreeditor()->markAsPreedit(position, length); } } @@ -416,6 +413,69 @@ void handleUnicodeCharacter(wchar_t c) } } +void resetPreedit() +{ + if (gWindowImplementation->getPreeditor()) + { + gWindowImplementation->getPreeditor()->resetPreedit(); + } +} + +// For reasons of convenience, handle IME updates here. +// This largely mirrors the old implementation, only sans the carbon parameters. +void setMarkedText(unsigned short *unitext, unsigned int *selectedRange, unsigned int *replacementRange, long text_len, segment_t segments) +{ + if (gWindowImplementation->getPreeditor()) + { + LLPreeditor *preeditor = gWindowImplementation->getPreeditor(); + + // This should be a viable replacement for the kEventParamTextInputSendReplaceRange parameter. + if (replacementRange[0] < replacementRange[1]) + { + const LLWString& text = preeditor->getPreeditString(); + const S32 location = wstring_wstring_length_from_utf16_length(text, 0, replacementRange[0]); + const S32 length = wstring_wstring_length_from_utf16_length(text, location, replacementRange[1]); + preeditor->markAsPreedit(location, length); + } + + preeditor->resetPreedit(); + + LLWString fix_str = utf16str_to_wstring(llutf16string(unitext, text_len)); + + LLPreeditor::segment_lengths_t preedit_segment_lengths; + LLPreeditor::standouts_t preedit_standouts; + S32 caret_position = fix_str.length(); + + for (segment_t::iterator i = segments.begin(); i != segments.end(); i++) + { + preedit_segment_lengths.push_back(i->first); + preedit_standouts.push_back(i->second); + } + + preeditor->updatePreedit(fix_str, preedit_segment_lengths, preedit_standouts, caret_position); + } +} + +void getPreeditLocation(float *location, unsigned int length) +{ + if (gWindowImplementation->getPreeditor()) + { + LLPreeditor *preeditor = gWindowImplementation->getPreeditor(); + LLCoordGL coord; + LLCoordScreen screen; + LLRect rect; + + preeditor->getPreeditLocation(length, &coord, &rect, NULL); + + float c[4] = {coord.mX, coord.mY, 0, 0}; + + convertRectToScreen(gWindowImplementation->getWindow(), c); + + location[0] = c[0]; + location[1] = c[1]; + } +} + void LLWindowMacOSX::updateMouseDeltas(float* deltas) { if (mCursorDecoupled) @@ -1295,8 +1355,22 @@ BOOL LLWindowMacOSX::convertCoords(LLCoordScreen from, LLCoordGL *to) BOOL LLWindowMacOSX::convertCoords(LLCoordGL from, LLCoordScreen *to) { LLCoordWindow window_coord; + if (mFullscreen) + { + to->mX = from.mX; + to->mY = from.mY; + return TRUE; + } else if (mWindow) + { + convertCoords(from, &window_coord); + convertCoords(window_coord, to); + + LL_INFOS("Coords") << to->mX << ", " << to->mY << LL_ENDL; + + return TRUE; + } - return(convertCoords(from, &window_coord) && convertCoords(window_coord, to)); + return FALSE; } @@ -1425,7 +1499,7 @@ void LLWindowMacOSX::updateCursor() // Find out what they look like and replicate them. // These are essentially correct - case UI_CURSOR_WAIT: /* Apple purposely doesn't allow us to set the beachball cursor manually. */ break; + case UI_CURSOR_WAIT: /* Apple purposely doesn't allow us to set the beachball cursor manually. Let NSApp figure out when to do this. */ break; case UI_CURSOR_IBEAM: setIBeamCursor(); break; case UI_CURSOR_CROSS: setCrossCursor(); break; case UI_CURSOR_HAND: setPointingHandCursor(); break; @@ -1810,7 +1884,35 @@ static long getDictLong (CFDictionaryRef refDict, CFStringRef key) void LLWindowMacOSX::allowLanguageTextInput(LLPreeditor *preeditor, BOOL b) { - // TODO: IME support + if (preeditor != mPreeditor && !b) + { + // This condition may occur by a call to + // setEnabled(BOOL) against LLTextEditor or LLLineEditor + // when the control is not focused. + // We need to silently ignore the case so that + // the language input status of the focused control + // is not disturbed. + return; + } + + // Take care of old and new preeditors. + if (preeditor != mPreeditor || !b) + { + // We need to interrupt before updating mPreeditor, + // so that the fix string from input method goes to + // the old preeditor. + if (mLanguageTextInputAllowed) + { + interruptLanguageTextInput(); + } + mPreeditor = (b ? preeditor : NULL); + } + + if (b == mLanguageTextInputAllowed) + { + return; + } + mLanguageTextInputAllowed = b; } void LLWindowMacOSX::interruptLanguageTextInput() diff --git a/indra/newview/Info-SecondLife.plist b/indra/newview/Info-SecondLife.plist index 5db52f040f..8fb4e88bc3 100644 --- a/indra/newview/Info-SecondLife.plist +++ b/indra/newview/Info-SecondLife.plist @@ -13,7 +13,7 @@ CFBundleInfoDictionaryVersion 6.0 CFBundleName - Second Life + SecondLife CFBundlePackageType APPL CFBundleSignature diff --git a/indra/newview/llviewerkeyboard.cpp b/indra/newview/llviewerkeyboard.cpp index 92d8f2937e..f3b7c0fad4 100644 --- a/indra/newview/llviewerkeyboard.cpp +++ b/indra/newview/llviewerkeyboard.cpp @@ -653,7 +653,6 @@ BOOL LLViewerKeyboard::modeFromString(const std::string& string, S32 *mode) BOOL LLViewerKeyboard::handleKey(KEY translated_key, MASK translated_mask, BOOL repeated) { - LL_INFOS("Keyboard Handling") << "Handling key " << translated_key << LL_ENDL; // check for re-map EKeyboardMode mode = gViewerKeyboard.getMode(); U32 keyidx = (translated_mask<<16) | translated_key; diff --git a/indra/newview/llviewerwindow.cpp b/indra/newview/llviewerwindow.cpp index ccaa9245cf..48a69129eb 100755 --- a/indra/newview/llviewerwindow.cpp +++ b/indra/newview/llviewerwindow.cpp @@ -2429,7 +2429,6 @@ void LLViewerWindow::draw() // Takes a single keydown event, usually when UI is visible BOOL LLViewerWindow::handleKey(KEY key, MASK mask) { - LL_INFOS("Keyboard Handling") << "Handling key " << key << LL_ENDL; // hide tooltips on keypress LLToolTipMgr::instance().blockToolTips(); -- cgit v1.3