dev: .mm / obj-c files -> clang-format and simplify includes
Some checks are pending
SonarCloud / Build and analyze (push) Waiting to run

This commit is contained in:
shenleban tongying 2024-11-12 05:19:59 -05:00 committed by GitHub
parent 8fd5b37335
commit 8fc71c9586
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 521 additions and 529 deletions

View file

@ -142,4 +142,7 @@ StatementMacros:
- QT_REQUIRE_VERSION - QT_REQUIRE_VERSION
UseCRLF: false UseCRLF: false
UseTab: Never UseTab: Never
---
Language: ObjC
BasedOnStyle: WebKit
... ...

View file

@ -30,8 +30,7 @@
#endif #endif
#ifdef Q_OS_MAC #ifdef Q_OS_MAC
#define __SECURITYHI__ #import <Carbon/Carbon.h>
#include <Carbon/Carbon.h>
#endif #endif
////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////

View file

@ -2,10 +2,10 @@
* Part of GoldenDict. Licensed under GPLv3 or later, see the LICENSE file */ * Part of GoldenDict. Licensed under GPLv3 or later, see the LICENSE file */
#include "hotkeywrapper.hh" #include "hotkeywrapper.hh"
#include <QTimer>
#include <QMessageBox> #include <QMessageBox>
#include <QPushButton>
#include <QObject> #include <QObject>
#include <QPushButton>
#include <QTimer>
#include <memory> #include <memory>
#import <Appkit/Appkit.h> #import <Appkit/Appkit.h>
@ -23,120 +23,114 @@
/// https://github.com/sindresorhus/KeyboardShortcuts/blob/9369a045a72a5296150879781321aecd228171db/readme.md?plain=1#L207 /// https://github.com/sindresorhus/KeyboardShortcuts/blob/9369a045a72a5296150879781321aecd228171db/readme.md?plain=1#L207
/// ///
namespace MacKeyMapping namespace MacKeyMapping {
{
// Convert Qt key codes to Mac OS X native codes // Convert Qt key codes to Mac OS X native codes
struct ReverseMapEntry struct ReverseMapEntry {
{ UniChar character;
UniChar character; UInt16 keyCode;
UInt16 keyCode;
}; };
static struct ReverseMapEntry * mapping; static struct ReverseMapEntry* mapping;
static int mapEntries = 0; static int mapEntries = 0;
void createMapping() void createMapping()
{ {
if( mapping == NULL ) if (mapping == NULL) {
{ TISInputSourceRef inputSourceRef = TISCopyInputSourceForLanguage(CFSTR("en"));
TISInputSourceRef inputSourceRef = TISCopyInputSourceForLanguage( CFSTR( "en" ) ); if (!inputSourceRef) {
if ( !inputSourceRef ) { inputSourceRef = TISCopyCurrentKeyboardInputSource();
inputSourceRef = TISCopyCurrentKeyboardInputSource(); }
} if (!inputSourceRef) {
if ( !inputSourceRef ) { return;
return; }
}
CFDataRef dataRef = ( CFDataRef )TISGetInputSourceProperty( inputSourceRef, CFDataRef dataRef = (CFDataRef)TISGetInputSourceProperty(inputSourceRef,
kTISPropertyUnicodeKeyLayoutData ); kTISPropertyUnicodeKeyLayoutData);
// this method returns null under macos Japanese input method(and also Chinese), which causes cmd+C+C not to be registered as a hotkey // this method returns null under macos Japanese input method(and also Chinese), which causes cmd+C+C not to be registered as a hotkey
if( !dataRef )
{
// solve the null value under Japanese keyboard
inputSourceRef = TISCopyCurrentKeyboardLayoutInputSource();
dataRef = static_cast<CFDataRef>((TISGetInputSourceProperty(inputSourceRef, kTISPropertyUnicodeKeyLayoutData)));
if (!dataRef) { if (!dataRef) {
return; // solve the null value under Japanese keyboard
inputSourceRef = TISCopyCurrentKeyboardLayoutInputSource();
dataRef = static_cast<CFDataRef>((TISGetInputSourceProperty(inputSourceRef, kTISPropertyUnicodeKeyLayoutData)));
if (!dataRef) {
return;
}
}
const UCKeyboardLayout* keyboardLayoutPtr = (const UCKeyboardLayout*)CFDataGetBytePtr(dataRef);
if (!keyboardLayoutPtr) {
return;
}
mapping = (struct ReverseMapEntry*)calloc(128, sizeof(struct ReverseMapEntry));
if (!mapping) {
return;
}
mapEntries = 0;
for (int i = 0; i < 128; i++) {
UInt32 theDeadKeyState = 0;
UniCharCount theLength = 0;
if (UCKeyTranslate(keyboardLayoutPtr, i, kUCKeyActionDisplay, 0, LMGetKbdType(),
kUCKeyTranslateNoDeadKeysBit, &theDeadKeyState, 1, &theLength,
&mapping[mapEntries].character)
== noErr
&& theLength > 0) {
if (isprint(mapping[mapEntries].character)) {
mapping[mapEntries++].keyCode = i;
}
}
} }
} }
const UCKeyboardLayout * keyboardLayoutPtr = ( const UCKeyboardLayout * )CFDataGetBytePtr( dataRef );
if( !keyboardLayoutPtr ) {
return;
} }
mapping = ( struct ReverseMapEntry * )calloc( 128 , sizeof(struct ReverseMapEntry) ); quint32 qtKeyToNativeKey(quint32 key)
if( !mapping ) {
return;
}
mapEntries = 0;
for( int i = 0; i < 128; i++ )
{
UInt32 theDeadKeyState = 0;
UniCharCount theLength = 0;
if( UCKeyTranslate( keyboardLayoutPtr, i, kUCKeyActionDisplay, 0, LMGetKbdType(),
kUCKeyTranslateNoDeadKeysBit, &theDeadKeyState, 1, &theLength,
&mapping[ mapEntries ].character ) == noErr && theLength > 0 )
{
if( isprint( mapping[ mapEntries ].character ) )
{
mapping[ mapEntries++ ].keyCode = i;
}
}
}
}
}
quint32 qtKeyToNativeKey( quint32 key )
{ {
createMapping(); createMapping();
if( mapping == NULL ) { if (mapping == NULL) {
return 0;
}
for (int i = 0; i < mapEntries; i++) {
if (mapping[i].character == key) {
return mapping[i].keyCode;
}
}
return 0; return 0;
} }
for( int i = 0; i < mapEntries; i++ )
{
if( mapping[ i ].character == key ) {
return mapping[ i ].keyCode;
}
}
return 0;
}
} // namespace MacKeyMapping } // namespace MacKeyMapping
static pascal OSStatus hotKeyHandler( EventHandlerCallRef /* nextHandler */, EventRef theEvent, void * userData ) static pascal OSStatus hotKeyHandler(EventHandlerCallRef /* nextHandler */, EventRef theEvent, void* userData)
{ {
EventHotKeyID hkID; EventHotKeyID hkID;
GetEventParameter( theEvent, kEventParamDirectObject, typeEventHotKeyID, NULL, sizeof(EventHotKeyID), NULL, &hkID ); GetEventParameter(theEvent, kEventParamDirectObject, typeEventHotKeyID, NULL, sizeof(EventHotKeyID), NULL, &hkID);
static_cast< HotkeyWrapper * >( userData )->activated( hkID.id ); static_cast<HotkeyWrapper*>(userData)->activated(hkID.id);
return noErr; return noErr;
} }
HotkeyWrapper::HotkeyWrapper( QObject *parent ) HotkeyWrapper::HotkeyWrapper(QObject* parent)
{ {
(void) parent; (void)parent;
hotKeyFunction = NewEventHandlerUPP( hotKeyHandler ); hotKeyFunction = NewEventHandlerUPP(hotKeyHandler);
EventTypeSpec type; EventTypeSpec type;
type.eventClass = kEventClassKeyboard; type.eventClass = kEventClassKeyboard;
type.eventKind = kEventHotKeyPressed; type.eventKind = kEventHotKeyPressed;
InstallApplicationEventHandler( hotKeyFunction, 1, &type, this, &handlerRef ); InstallApplicationEventHandler(hotKeyFunction, 1, &type, this, &handlerRef);
keyC = nativeKey( 'c' ); keyC = nativeKey('c');
} }
HotkeyWrapper::~HotkeyWrapper() HotkeyWrapper::~HotkeyWrapper()
{ {
unregister(); unregister();
RemoveEventHandler( handlerRef ); RemoveEventHandler(handlerRef);
} }
void HotkeyWrapper::waitKey2() void HotkeyWrapper::waitKey2()
{ {
state2 = false; state2 = false;
} }
void checkAndRequestAccessibilityPermission() void checkAndRequestAccessibilityPermission()
{ {
@ -159,196 +153,220 @@ void checkAndRequestAccessibilityPermission()
} }
} }
void HotkeyWrapper::activated( int hkId ) void HotkeyWrapper::activated(int hkId)
{ {
if ( state2 ) if (state2) { // wait for 2nd key
{ // wait for 2nd key
waitKey2(); // Cancel the 2nd-key wait stage waitKey2(); // Cancel the 2nd-key wait stage
if ( hkId == state2waiter.id + 1 || if (hkId == state2waiter.id + 1 || (hkId == state2waiter.id && state2waiter.key == state2waiter.key2)) {
( hkId == state2waiter.id && state2waiter.key == state2waiter.key2 ) ) emit hotkeyActivated(state2waiter.handle);
{ return;
emit hotkeyActivated( state2waiter.handle ); }
return;
} }
}
for ( int i = 0; i < hotkeys.count(); i++ ) for (int i = 0; i < hotkeys.count(); i++) {
{ HotkeyStruct& hs = hotkeys[i];
HotkeyStruct &hs = hotkeys[ i ]; if (hkId == hs.id) {
if( hkId == hs.id ) if (hs.key == keyC && hs.modifier == cmdKey) {
{ checkAndRequestAccessibilityPermission();
if( hs.key == keyC && hs.modifier == cmdKey )
{
checkAndRequestAccessibilityPermission();
// If that was a copy-to-clipboard shortcut, re-emit it back so it could // If that was a copy-to-clipboard shortcut, re-emit it back so it could
// reach its original destination so it could be acted upon. // reach its original destination so it could be acted upon.
UnregisterEventHotKey( hs.hkRef ); UnregisterEventHotKey(hs.hkRef);
sendCmdC(); sendCmdC();
EventHotKeyID hotKeyID; EventHotKeyID hotKeyID;
hotKeyID.signature = 'GDHK'; hotKeyID.signature = 'GDHK';
hotKeyID.id = hs.id; hotKeyID.id = hs.id;
RegisterEventHotKey( hs.key, hs.modifier, hotKeyID, GetApplicationEventTarget(), 0, &hs.hkRef ); RegisterEventHotKey(hs.key, hs.modifier, hotKeyID, GetApplicationEventTarget(), 0, &hs.hkRef);
} }
if ( hs.key2 == 0 ) { if (hs.key2 == 0) {
emit hotkeyActivated( hs.handle ); emit hotkeyActivated(hs.handle);
return; return;
} }
state2 = true; state2 = true;
state2waiter = hs; state2waiter = hs;
QTimer::singleShot( 500, this, SLOT( waitKey2() ) ); QTimer::singleShot(500, this, SLOT(waitKey2()));
return; return;
}
} }
}
state2 = false; state2 = false;
return; return;
} }
void HotkeyWrapper::unregister() void HotkeyWrapper::unregister()
{ {
for ( int i = 0; i < hotkeys.count(); i++ ) for (int i = 0; i < hotkeys.count(); i++) {
{ HotkeyStruct const& hk = hotkeys.at(i);
HotkeyStruct const & hk = hotkeys.at( i );
UnregisterEventHotKey( hk.hkRef ); UnregisterEventHotKey(hk.hkRef);
if ( hk.key2 && hk.key2 != hk.key ) { if (hk.key2 && hk.key2 != hk.key) {
UnregisterEventHotKey( hk.hkRef2 ); UnregisterEventHotKey(hk.hkRef2);
} }
} }
(static_cast< QHotkeyApplication * >( qApp ))->unregisterWrapper( this ); (static_cast<QHotkeyApplication*>(qApp))->unregisterWrapper(this);
} }
bool HotkeyWrapper::setGlobalKey( QKeySequence const & seq, int handle ) bool HotkeyWrapper::setGlobalKey(QKeySequence const& seq, int handle)
{ {
Config::HotKey hk(seq); Config::HotKey hk(seq);
return setGlobalKey(hk.key1,hk.key2,hk.modifiers,handle); return setGlobalKey(hk.key1, hk.key2, hk.modifiers, handle);
} }
bool HotkeyWrapper::setGlobalKey( int key, int key2, Qt::KeyboardModifiers modifier, int handle ) bool HotkeyWrapper::setGlobalKey(int key, int key2, Qt::KeyboardModifiers modifier, int handle)
{ {
if ( !key ) { if (!key) {
return false; // We don't monitor empty combinations return false; // We don't monitor empty combinations
}
quint32 vk = nativeKey(key);
if (vk == 0) {
return false;
}
quint32 vk2 = key2 ? nativeKey(key2) : 0;
static int nextId = 1;
if (nextId > 0xBFFF - 1) {
nextId = 1;
}
quint32 mod = 0;
if (modifier & Qt::CTRL) {
mod |= cmdKey;
}
if (modifier & Qt::ALT) {
mod |= optionKey;
}
if (modifier & Qt::SHIFT) {
mod |= shiftKey;
}
if (modifier & Qt::META) {
mod |= controlKey;
}
hotkeys.append(HotkeyStruct(vk, vk2, mod, handle, nextId));
HotkeyStruct& hk = hotkeys.last();
EventHotKeyID hotKeyID;
hotKeyID.signature = 'GDHK';
hotKeyID.id = nextId;
OSStatus ret = RegisterEventHotKey(vk, mod, hotKeyID, GetApplicationEventTarget(), 0, &hk.hkRef);
if (ret != 0) {
return false;
}
if (vk2 && vk2 != vk) {
hotKeyID.id = nextId + 1;
ret = RegisterEventHotKey(vk2, mod, hotKeyID, GetApplicationEventTarget(), 0, &hk.hkRef2);
}
nextId += 2;
return ret == 0;
} }
quint32 vk = nativeKey( key ); quint32 HotkeyWrapper::nativeKey(int key)
if( vk == 0 ) {
return false;
}
quint32 vk2 = key2 ? nativeKey( key2 ) : 0;
static int nextId = 1;
if( nextId > 0xBFFF - 1 ) {
nextId = 1;
}
quint32 mod = 0;
if( modifier & Qt::CTRL ) {
mod |= cmdKey;
}
if( modifier & Qt::ALT ) {
mod |= optionKey;
}
if( modifier & Qt::SHIFT ) {
mod |= shiftKey;
}
if( modifier & Qt::META ) {
mod |= controlKey;
}
hotkeys.append( HotkeyStruct( vk, vk2, mod, handle, nextId ) );
HotkeyStruct &hk = hotkeys.last();
EventHotKeyID hotKeyID;
hotKeyID.signature = 'GDHK';
hotKeyID.id = nextId;
OSStatus ret = RegisterEventHotKey( vk, mod, hotKeyID, GetApplicationEventTarget(), 0, &hk.hkRef );
if ( ret != 0 ) {
return false;
}
if ( vk2 && vk2 != vk )
{
hotKeyID.id = nextId + 1;
ret = RegisterEventHotKey( vk2, mod, hotKeyID, GetApplicationEventTarget(), 0, &hk.hkRef2 );
}
nextId += 2;
return ret == 0;
}
quint32 HotkeyWrapper::nativeKey( int key )
{ {
switch( key ) { switch (key) {
case Qt::Key_Escape: return 0x35; case Qt::Key_Escape:
case Qt::Key_Tab: return 0x30; return 0x35;
case Qt::Key_Backspace: return 0x33; case Qt::Key_Tab:
case Qt::Key_Return: return 0x24; return 0x30;
case Qt::Key_Enter: return 0x4c; case Qt::Key_Backspace:
case Qt::Key_Delete: return 0x75; return 0x33;
case Qt::Key_Clear: return 0x47; case Qt::Key_Return:
case Qt::Key_Home: return 0x73; return 0x24;
case Qt::Key_End: return 0x77; case Qt::Key_Enter:
case Qt::Key_Left: return 0x7b; return 0x4c;
case Qt::Key_Up: return 0x7e; case Qt::Key_Delete:
case Qt::Key_Right: return 0x7c; return 0x75;
case Qt::Key_Down: return 0x7d; case Qt::Key_Clear:
case Qt::Key_PageUp: return 0x74; return 0x47;
case Qt::Key_PageDown: return 0x79; case Qt::Key_Home:
case Qt::Key_CapsLock: return 0x57; return 0x73;
case Qt::Key_F1: return 0x7a; case Qt::Key_End:
case Qt::Key_F2: return 0x78; return 0x77;
case Qt::Key_F3: return 0x63; case Qt::Key_Left:
case Qt::Key_F4: return 0x76; return 0x7b;
case Qt::Key_F5: return 0x60; case Qt::Key_Up:
case Qt::Key_F6: return 0x61; return 0x7e;
case Qt::Key_F7: return 0x62; case Qt::Key_Right:
case Qt::Key_F8: return 0x64; return 0x7c;
case Qt::Key_F9: return 0x65; case Qt::Key_Down:
case Qt::Key_F10: return 0x6d; return 0x7d;
case Qt::Key_F11: return 0x67; case Qt::Key_PageUp:
case Qt::Key_F12: return 0x6f; return 0x74;
case Qt::Key_F13: return 0x69; case Qt::Key_PageDown:
case Qt::Key_F14: return 0x6b; return 0x79;
case Qt::Key_F15: return 0x71; case Qt::Key_CapsLock:
case Qt::Key_Help: return 0x72; return 0x57;
case Qt::Key_F1:
return 0x7a;
case Qt::Key_F2:
return 0x78;
case Qt::Key_F3:
return 0x63;
case Qt::Key_F4:
return 0x76;
case Qt::Key_F5:
return 0x60;
case Qt::Key_F6:
return 0x61;
case Qt::Key_F7:
return 0x62;
case Qt::Key_F8:
return 0x64;
case Qt::Key_F9:
return 0x65;
case Qt::Key_F10:
return 0x6d;
case Qt::Key_F11:
return 0x67;
case Qt::Key_F12:
return 0x6f;
case Qt::Key_F13:
return 0x69;
case Qt::Key_F14:
return 0x6b;
case Qt::Key_F15:
return 0x71;
case Qt::Key_Help:
return 0x72;
default:; default:;
} }
return MacKeyMapping::qtKeyToNativeKey( QChar( key ).toLower().toLatin1() ); return MacKeyMapping::qtKeyToNativeKey(QChar(key).toLower().toLatin1());
} }
void HotkeyWrapper::sendCmdC() void HotkeyWrapper::sendCmdC()
{ {
CGEventFlags flags = kCGEventFlagMaskCommand; CGEventFlags flags = kCGEventFlagMaskCommand;
CGEventRef ev; CGEventRef ev;
CGEventSourceRef source = CGEventSourceCreate( kCGEventSourceStateCombinedSessionState ); CGEventSourceRef source = CGEventSourceCreate(kCGEventSourceStateCombinedSessionState);
//press down // press down
ev = CGEventCreateKeyboardEvent( source, keyC, true ); ev = CGEventCreateKeyboardEvent(source, keyC, true);
CGEventSetFlags( ev, CGEventFlags( flags | CGEventGetFlags( ev ) ) ); //combine flags CGEventSetFlags(ev, CGEventFlags(flags | CGEventGetFlags(ev))); // combine flags
CGEventPost( kCGAnnotatedSessionEventTap, ev ); CGEventPost(kCGAnnotatedSessionEventTap, ev);
CFRelease( ev ); CFRelease(ev);
//press up // press up
ev = CGEventCreateKeyboardEvent( source, keyC, false ); ev = CGEventCreateKeyboardEvent(source, keyC, false);
CGEventSetFlags( ev, CGEventFlags( flags | CGEventGetFlags( ev ) ) ); //combine flags CGEventSetFlags(ev, CGEventFlags(flags | CGEventGetFlags(ev))); // combine flags
CGEventPost( kCGAnnotatedSessionEventTap, ev ); CGEventPost(kCGAnnotatedSessionEventTap, ev);
CFRelease( ev ); CFRelease(ev);
CFRelease( source ); CFRelease(source);
} }
EventHandlerUPP HotkeyWrapper::hotKeyFunction = NULL; EventHandlerUPP HotkeyWrapper::hotKeyFunction = NULL;

View file

@ -1,372 +1,344 @@
#include "macmouseover.hh" #include "macmouseover.hh"
#include <AppKit/NSTouch.h> #import <AppKit/AppKit.h>
#include <AppKit/NSEvent.h>
#include <AppKit/NSScreen.h>
#include <Foundation/NSAutoreleasePool.h>
#include <Foundation/Foundation.h>
#ifndef AVAILABLE_MAC_OS_X_VERSION_10_11_AND_LATER
#define kAXValueTypeCGPoint kAXValueCGPointType
#define kAXValueTypeCFRange kAXValueCFRangeType
#endif
const int mouseOverInterval = 300; const int mouseOverInterval = 300;
CGEventRef eventCallback( CGEventTapProxy proxy, CGEventType type, CGEventRef event, void *refcon ) CGEventRef eventCallback(CGEventTapProxy proxy, CGEventType type, CGEventRef event, void* refcon)
{ {
(void) proxy; (void)proxy;
if( type != kCGEventMouseMoved ) { if (type != kCGEventMouseMoved) {
return event;
}
static_cast<MacMouseOver*>(refcon)->mouseMoved();
return event; return event;
} }
static_cast< MacMouseOver * >( refcon )->mouseMoved();
return event;
}
static CGPoint carbonScreenPointFromCocoaScreenPoint( NSPoint cocoaPoint ) static CGPoint carbonScreenPointFromCocoaScreenPoint(NSPoint cocoaPoint)
{ {
NSScreen *foundScreen = nil; NSScreen* foundScreen = nil;
CGPoint thePoint; CGPoint thePoint;
for (NSScreen *screen in [NSScreen screens]) { for (NSScreen* screen in [NSScreen screens]) {
if (NSPointInRect(cocoaPoint, [screen frame])) { if (NSPointInRect(cocoaPoint, [screen frame])) {
foundScreen = screen; foundScreen = screen;
}
} }
}
if (foundScreen) { if (foundScreen) {
CGFloat screenHeight = [foundScreen frame].size.height; CGFloat screenHeight = [foundScreen frame].size.height;
thePoint = CGPointMake(cocoaPoint.x, screenHeight - cocoaPoint.y - 1); thePoint = CGPointMake(cocoaPoint.x, screenHeight - cocoaPoint.y - 1);
} } else {
else { thePoint = CGPointMake(0.0, 0.0);
thePoint = CGPointMake(0.0, 0.0); }
return thePoint;
} }
return thePoint; MacMouseOver& MacMouseOver::instance()
}
MacMouseOver & MacMouseOver::instance()
{ {
static MacMouseOver m; static MacMouseOver m;
return m; return m;
} }
MacMouseOver::MacMouseOver() : MacMouseOver::MacMouseOver()
pPref(NULL) : pPref(NULL)
, tapRef( 0 ) , tapRef(0)
, loop( 0 ) , loop(0)
{ {
mouseTimer.setSingleShot( true ); mouseTimer.setSingleShot(true);
connect( &mouseTimer, SIGNAL( timeout() ), this, SLOT( timerShot() ) ); connect(&mouseTimer, SIGNAL(timeout()), this, SLOT(timerShot()));
elementSystemWide = AXUIElementCreateSystemWide(); elementSystemWide = AXUIElementCreateSystemWide();
} }
MacMouseOver::~MacMouseOver() MacMouseOver::~MacMouseOver()
{ {
disableMouseOver(); disableMouseOver();
if( tapRef ) { if (tapRef) {
CFRelease( tapRef ); CFRelease(tapRef);
}
if (loop) {
CFRelease(loop);
}
if (elementSystemWide) {
CFRelease(elementSystemWide);
}
} }
if( loop ) { QString MacMouseOver::CFStringRefToQString(CFStringRef str)
CFRelease( loop );
}
if( elementSystemWide ) {
CFRelease( elementSystemWide );
}
}
QString MacMouseOver::CFStringRefToQString( CFStringRef str )
{ {
int length = CFStringGetLength( str ); int length = CFStringGetLength(str);
if( length == 0 ) { if (length == 0) {
return QString(); return QString();
} }
UniChar *chars = new UniChar[ length ]; UniChar* chars = new UniChar[length];
CFStringGetCharacters( str, CFRangeMake( 0, length ), chars ); CFStringGetCharacters(str, CFRangeMake(0, length), chars);
QString result = QString::fromUtf16( (char16_t*)chars, length ); QString result = QString::fromUtf16((char16_t*)chars, length);
delete[] chars; delete[] chars;
return result; return result;
} }
void MacMouseOver::mouseMoved() void MacMouseOver::mouseMoved()
{ {
mouseTimer.start( mouseOverInterval ); mouseTimer.start(mouseOverInterval);
} }
void MacMouseOver::enableMouseOver() void MacMouseOver::enableMouseOver()
{ {
mouseTimer.stop(); mouseTimer.stop();
if( !isAXAPIEnabled() ) { if (!isAXAPIEnabled()) {
return; return;
} }
if( !tapRef ) { if (!tapRef) {
tapRef = CGEventTapCreate( kCGAnnotatedSessionEventTap, kCGHeadInsertEventTap, tapRef = CGEventTapCreate(kCGAnnotatedSessionEventTap, kCGHeadInsertEventTap,
kCGEventTapOptionListenOnly, kCGEventTapOptionListenOnly,
CGEventMaskBit( kCGEventMouseMoved ), CGEventMaskBit(kCGEventMouseMoved),
eventCallback, this ); eventCallback, this);
} }
if( !tapRef ) { if (!tapRef) {
return; return;
} }
if( !loop ) { if (!loop) {
loop = CFMachPortCreateRunLoopSource( kCFAllocatorDefault, tapRef, 0 ); loop = CFMachPortCreateRunLoopSource(kCFAllocatorDefault, tapRef, 0);
} }
if( loop ) { if (loop) {
CFRunLoopAddSource( CFRunLoopGetMain(), loop, kCFRunLoopCommonModes ); CFRunLoopAddSource(CFRunLoopGetMain(), loop, kCFRunLoopCommonModes);
} }
} }
void MacMouseOver::disableMouseOver() void MacMouseOver::disableMouseOver()
{ {
mouseTimer.stop(); mouseTimer.stop();
if( loop ) { if (loop) {
CFRunLoopRemoveSource( CFRunLoopGetMain(), loop, kCFRunLoopCommonModes ); CFRunLoopRemoveSource(CFRunLoopGetMain(), loop, kCFRunLoopCommonModes);
} }
} }
void MacMouseOver::timerShot() void MacMouseOver::timerShot()
{ {
if( mouseMutex.tryLock( 0 ) ) { if (mouseMutex.tryLock(0)) {
mouseMutex.unlock(); mouseMutex.unlock();
} else { } else {
return; return;
} }
if( !pPref ) { if (!pPref) {
return; return;
} }
if( !pPref->enableScanPopupModifiers || checkModifiersPressed( pPref->scanPopupModifiers ) ) { if (!pPref->enableScanPopupModifiers || checkModifiersPressed(pPref->scanPopupModifiers)) {
handlePosition(); handlePosition();
} }
} }
void MacMouseOver::handlePosition() void MacMouseOver::handlePosition()
{ {
QMutexLocker _( &mouseMutex ); QMutexLocker _(&mouseMutex);
QString strToTranslate; QString strToTranslate;
NSAutoreleasePool * pool = [ [ NSAutoreleasePool alloc ] init ]; NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
CGPoint pt = carbonScreenPointFromCocoaScreenPoint( [NSEvent mouseLocation] ); CGPoint pt = carbonScreenPointFromCocoaScreenPoint([NSEvent mouseLocation]);
[ pool drain ]; [pool drain];
CFArrayRef names = 0; CFArrayRef names = 0;
AXUIElementRef elem = 0; AXUIElementRef elem = 0;
AXError err = AXUIElementCopyElementAtPosition( elementSystemWide, pt.x, pt.y, &elem ); AXError err = AXUIElementCopyElementAtPosition(elementSystemWide, pt.x, pt.y, &elem);
if( err != kAXErrorSuccess ) { if (err != kAXErrorSuccess) {
return; return;
} }
for( ; ; ) for (;;) {
{ CFTypeRef parameter = AXValueCreate(kAXValueTypeCGPoint, &pt);
CFTypeRef parameter = AXValueCreate( kAXValueTypeCGPoint, &pt ); CFTypeRef rangeValue;
CFTypeRef rangeValue; err = AXUIElementCopyParameterizedAttributeNames(elem, &names);
err = AXUIElementCopyParameterizedAttributeNames( elem, &names ); if (err != kAXErrorSuccess) {
if( err != kAXErrorSuccess ) {
break;
}
int numOfAttributes = CFArrayGetCount( names );
if( CFArrayContainsValue( names, CFRangeMake( 0, numOfAttributes ), CFSTR( "AXRangeForPosition" ) ) )
{
// Standard interface
err = AXUIElementCopyParameterizedAttributeValue( elem, kAXRangeForPositionParameterizedAttribute,
parameter, ( CFTypeRef * )&rangeValue );
CFRelease( parameter );
if( err != kAXErrorSuccess ) {
break;
}
CFStringRef stringValue;
CFRange decodedRange = CFRangeMake( 0, 0 );
bool b = AXValueGetValue( (AXValueRef)rangeValue, kAXValueTypeCFRange, &decodedRange );
CFRelease( rangeValue );
if( b )
{
int fromPos = decodedRange.location - 127;
if( fromPos < 0 ) {
fromPos = 0;
}
int wordPos = decodedRange.location - fromPos; // Cursor position in result string
CFRange range = CFRangeMake( fromPos, wordPos + 1 );
parameter = AXValueCreate( kAXValueTypeCFRange, &range );
err = AXUIElementCopyParameterizedAttributeValue( elem, kAXStringForRangeParameterizedAttribute,
parameter, (CFTypeRef *)&stringValue );
CFRelease( parameter );
if( err != kAXErrorSuccess ) {
break;
}
strToTranslate = CFStringRefToQString( stringValue );
CFRelease( stringValue );
// Read string further
for( int i = 1; i < 128; i++ )
{
range = CFRangeMake( decodedRange.location + i, 1 );
parameter = AXValueCreate( kAXValueTypeCFRange, &range );
err = AXUIElementCopyParameterizedAttributeValue( elem, kAXStringForRangeParameterizedAttribute,
parameter, (CFTypeRef *)&stringValue );
CFRelease( parameter );
if( err != kAXErrorSuccess ) {
break; break;
}
QString s = CFStringRefToQString( stringValue );
CFRelease( stringValue );
if( s[ 0 ].isLetterOrNumber() || s[ 0 ] == '-' ) {
strToTranslate += s;
} else {
break;
}
} }
handleRetrievedString( strToTranslate, wordPos ); int numOfAttributes = CFArrayGetCount(names);
} if (CFArrayContainsValue(names, CFRangeMake(0, numOfAttributes), CFSTR("AXRangeForPosition"))) {
} // Standard interface
else if( CFArrayContainsValue( names, CFRangeMake( 0, numOfAttributes ), CFSTR( "AXTextMarkerForPosition" ) ) ) err = AXUIElementCopyParameterizedAttributeValue(elem, kAXRangeForPositionParameterizedAttribute,
{ parameter, (CFTypeRef*)&rangeValue);
// Safari interface CFRelease(parameter);
CFTypeRef marker, range; if (err != kAXErrorSuccess) {
CFStringRef str; break;
err = AXUIElementCopyParameterizedAttributeValue( elem, CFSTR( "AXTextMarkerForPosition" ), }
parameter, ( CFTypeRef * )&marker );
CFRelease( parameter ); CFStringRef stringValue;
if( err != kAXErrorSuccess ) {
CFRange decodedRange = CFRangeMake(0, 0);
bool b = AXValueGetValue((AXValueRef)rangeValue, kAXValueTypeCFRange, &decodedRange);
CFRelease(rangeValue);
if (b) {
int fromPos = decodedRange.location - 127;
if (fromPos < 0) {
fromPos = 0;
}
int wordPos = decodedRange.location - fromPos; // Cursor position in result string
CFRange range = CFRangeMake(fromPos, wordPos + 1);
parameter = AXValueCreate(kAXValueTypeCFRange, &range);
err = AXUIElementCopyParameterizedAttributeValue(elem, kAXStringForRangeParameterizedAttribute,
parameter, (CFTypeRef*)&stringValue);
CFRelease(parameter);
if (err != kAXErrorSuccess) {
break;
}
strToTranslate = CFStringRefToQString(stringValue);
CFRelease(stringValue);
// Read string further
for (int i = 1; i < 128; i++) {
range = CFRangeMake(decodedRange.location + i, 1);
parameter = AXValueCreate(kAXValueTypeCFRange, &range);
err = AXUIElementCopyParameterizedAttributeValue(elem, kAXStringForRangeParameterizedAttribute,
parameter, (CFTypeRef*)&stringValue);
CFRelease(parameter);
if (err != kAXErrorSuccess) {
break;
}
QString s = CFStringRefToQString(stringValue);
CFRelease(stringValue);
if (s[0].isLetterOrNumber() || s[0] == '-') {
strToTranslate += s;
} else {
break;
}
}
handleRetrievedString(strToTranslate, wordPos);
}
} else if (CFArrayContainsValue(names, CFRangeMake(0, numOfAttributes), CFSTR("AXTextMarkerForPosition"))) {
// Safari interface
CFTypeRef marker, range;
CFStringRef str;
err = AXUIElementCopyParameterizedAttributeValue(elem, CFSTR("AXTextMarkerForPosition"),
parameter, (CFTypeRef*)&marker);
CFRelease(parameter);
if (err != kAXErrorSuccess) {
break;
}
err = AXUIElementCopyParameterizedAttributeValue(elem, CFSTR("AXLeftWordTextMarkerRangeForTextMarker"),
marker, (CFTypeRef*)&range);
CFRelease(marker);
if (err != kAXErrorSuccess) {
break;
}
err = AXUIElementCopyParameterizedAttributeValue(elem, CFSTR("AXStringForTextMarkerRange"),
range, (CFTypeRef*)&str);
CFRelease(range);
if (err == kAXErrorSuccess) {
strToTranslate = CFStringRefToQString(str);
CFRelease(str);
handleRetrievedString(strToTranslate, 0);
}
}
break; break;
}
err = AXUIElementCopyParameterizedAttributeValue( elem, CFSTR( "AXLeftWordTextMarkerRangeForTextMarker" ),
marker, ( CFTypeRef * )&range );
CFRelease( marker );
if( err != kAXErrorSuccess ) {
break;
}
err = AXUIElementCopyParameterizedAttributeValue( elem, CFSTR( "AXStringForTextMarkerRange" ),
range, ( CFTypeRef * )&str );
CFRelease( range );
if( err == kAXErrorSuccess )
{
strToTranslate = CFStringRefToQString( str );
CFRelease( str );
handleRetrievedString( strToTranslate, 0 );
}
} }
break; if (elem) {
} CFRelease(elem);
if( elem ) { }
CFRelease( elem ); if (names) {
} CFRelease(names);
if( names ) { }
CFRelease( names );
}
} }
void MacMouseOver::handleRetrievedString( QString & wordSeq, int wordSeqPos ) void MacMouseOver::handleRetrievedString(QString& wordSeq, int wordSeqPos)
{ {
if( wordSeq.isEmpty() ) { if (wordSeq.isEmpty()) {
return; return;
}
// locate the word inside the sequence
QString word;
if ( wordSeq[ wordSeqPos ].isSpace() )
{
// Currently we ignore such cases
return;
}
else
if ( !wordSeq[ wordSeqPos ].isLetterOrNumber() )
{
// Special case: the cursor points to something which doesn't look like a
// middle of the word -- assume that it's something that joins two words
// together.
int begin = wordSeqPos;
for( ; begin; --begin ) {
if ( !wordSeq[ begin - 1 ].isLetterOrNumber() ) {
break;
}
}
int end = wordSeqPos;
while( ++end < wordSeq.size() ) {
if ( !wordSeq[ end ].isLetterOrNumber() ) {
break;
}
}
if ( end - begin == 1 )
{
// Well, turns out it was just a single non-letter char, discard it
return;
} }
word = wordSeq.mid( begin, end - begin ); // locate the word inside the sequence
}
else
{
// Cursor points to a letter -- cut the word it points to
int begin = wordSeqPos; QString word;
for( ; begin; --begin ) { if (wordSeq[wordSeqPos].isSpace()) {
if ( !wordSeq[ begin - 1 ].isLetterOrNumber() ) { // Currently we ignore such cases
break; return;
} } else if (!wordSeq[wordSeqPos].isLetterOrNumber()) {
} // Special case: the cursor points to something which doesn't look like a
// middle of the word -- assume that it's something that joins two words
// together.
int end = wordSeqPos; int begin = wordSeqPos;
while( ++end < wordSeq.size() ) for (; begin; --begin) {
{ if (!wordSeq[begin - 1].isLetterOrNumber()) {
if ( !wordSeq[ end ].isLetterOrNumber() ) { break;
break; }
} }
int end = wordSeqPos;
while (++end < wordSeq.size()) {
if (!wordSeq[end].isLetterOrNumber()) {
break;
}
}
if (end - begin == 1) {
// Well, turns out it was just a single non-letter char, discard it
return;
}
word = wordSeq.mid(begin, end - begin);
} else {
// Cursor points to a letter -- cut the word it points to
int begin = wordSeqPos;
for (; begin; --begin) {
if (!wordSeq[begin - 1].isLetterOrNumber()) {
break;
}
}
int end = wordSeqPos;
while (++end < wordSeq.size()) {
if (!wordSeq[end].isLetterOrNumber()) {
break;
}
}
word = wordSeq.mid(begin, end - begin);
} }
word = wordSeq.mid( begin, end - begin );
}
// See if we have an RTL char. Reverse the whole string if we do. // See if we have an RTL char. Reverse the whole string if we do.
for( int x = 0; x < word.size(); ++x ) for (int x = 0; x < word.size(); ++x) {
{ QChar::Direction d = word[x].direction();
QChar::Direction d = word[ x ].direction();
if ( d == QChar::DirR || d == QChar::DirAL || if (d == QChar::DirR || d == QChar::DirAL || d == QChar::DirRLE || d == QChar::DirRLO) {
d == QChar::DirRLE || d == QChar::DirRLO ) std::reverse(word.begin(), word.end());
{ break;
std::reverse( word.begin(), word.end() ); }
break;
} }
}
emit instance().hovered( word, false ); emit instance().hovered(word, false);
} }
bool MacMouseOver::isAXAPIEnabled() bool MacMouseOver::isAXAPIEnabled()
{ {
if( NSFoundationVersionNumber >= 1000 ) { // MacOS 10.9+ if (NSFoundationVersionNumber >= 1000) { // MacOS 10.9+
return AXIsProcessTrusted(); return AXIsProcessTrusted();
} }
return AXAPIEnabled(); return AXAPIEnabled();
} }