From nobody Mon Apr 29 06:49:16 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zoho.com; dkim=fail spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1495841976735913.2984941038137; Fri, 26 May 2017 16:39:36 -0700 (PDT) Received: from localhost ([::1]:38898 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dEOpR-0006cv-2C for importer@patchew.org; Fri, 26 May 2017 19:39:33 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:60971) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dEOoc-0006Fs-3Q for qemu-devel@nongnu.org; Fri, 26 May 2017 19:38:43 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dEOoZ-00013q-0B for qemu-devel@nongnu.org; Fri, 26 May 2017 19:38:42 -0400 Received: from mail-pg0-x22b.google.com ([2607:f8b0:400e:c05::22b]:36090) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1dEOoY-00013j-Nv for qemu-devel@nongnu.org; Fri, 26 May 2017 19:38:38 -0400 Received: by mail-pg0-x22b.google.com with SMTP id x64so144015pgd.3 for ; Fri, 26 May 2017 16:38:38 -0700 (PDT) Received: from ianloic-macbookpro2.roam.corp.google.com.com ([100.118.184.234]) by smtp.gmail.com with ESMTPSA id p4sm3901573pfj.104.2017.05.26.16.38.35 (version=TLS1 cipher=AES128-SHA bits=128/128); Fri, 26 May 2017 16:38:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=from:to:cc:subject:date:message-id; bh=/0Xb6FJkB3cDyZp/tLzldjbJIbPytOB3zl7YcCuDJF4=; b=lHAATvpmSqJ0zsPB/GP4sXIaLV+T56amyQMmx1P6a1fgAUmhj82MRKyIEIB+1jsdRx dQbzrQnMheGE4rns1gczoMcVZ1/QCG37sz0U6jci7r0J1dprwks6kYJ6EerNFZsv2R1u +QA/o+jvp1wr2CJ4p80nD4xNGkCJyVTnL4MYpPmeosMeJ5IeLy9n69eL0ApEKqsD4w9Z pVKFVaIcsErFxkT9THy/pAk7In/j9jUTdls4N3wBUMh6nY54n6cu912WsTXazqA24Hp3 2YW2Hr38xVwJzSGEBThGwjqLgNkMfJN7rsfCOkGQ/61Ik+jcxCTjoE0dSI8ydQ4ZXoY0 5ByA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=/0Xb6FJkB3cDyZp/tLzldjbJIbPytOB3zl7YcCuDJF4=; b=LBrc51Wpo/I89AZea7chrho0wE5SxdPpJFkip4IfNhW9qk2DI4bsEeyU5KhTA/6iF/ 0/jukpZAhGWJhCrc/12S/03kkM968ZKgIBgRpSQkVaTJPnukbbXGy6KefC+NyaG0mafA UPBhFW2oeV12dv5nu8IFzWklmKwSbDigI5lkY9EzdBdpWDkEE1CVi89wvzmmYpLs+oLy qJMmZsXSsYCKvDRJCbW6dx9ZTvJ63sU1P2aI2Yf/PQdgWKC5Pojabf/0mI1d+EPJyCwT rqG1trh9iKcyuqAB2mMgDIZZ51TNG7MtSKz07GrJ9MUZJ3YgGz35phGwJna3lciM9n/R Xq5Q== X-Gm-Message-State: AODbwcBqgfNjBXQAF4HuM3WVPuCjySE9VRtwKl9bb/ka+xxtJDabT2V5 p7yTJm6s+ghhSRt56cQwVw== X-Received: by 10.99.165.9 with SMTP id n9mr5446790pgf.233.1495841916772; Fri, 26 May 2017 16:38:36 -0700 (PDT) To: qemu-devel@nongnu.org Date: Fri, 26 May 2017 16:38:16 -0700 Message-Id: <20170526233816.47627-1-ianloic@google.com> X-Mailer: git-send-email 2.13.0.rc1.294.g07d810a77f-goog X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:400e:c05::22b Subject: [Qemu-devel] [PATCH] Improve Cocoa modifier key handling X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , From: Ian McKellar via Qemu-devel Reply-To: Ian McKellar Cc: kraxel@redhat.com, Ian McKellar Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZohoMail: RDKM_2 RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" I had two problems with QEMU on macOS: 1) Sometimes when alt-tabbing to QEMU it would act as if the 'a' key was pressed so I'd get 'aaaaaaaaa....'. 2) Using Sikuli to programatically send keys to the QEMU window text like "foo_bar" would come out as "fooa-bar". They looked similar and after much digging the problem turned out to be the same. When QEMU's ui/cocoa.m received an NSFlagsChanged NSEvent it looked at the keyCode to determine what modifier key changed. This usually works fine but sometimes the keyCode is 0 and the app should instead be looking at the modifierFlags bitmask. Key code 0 is the 'a' key. I added code that handles keyCode =3D=3D 0 differently. It checks the modifierFlags and if they differ from QEMU's idea of which modifier keys are currently pressed it toggles those changed keys. This fixes my problems and seems work fine. Signed-off-by: Ian McKellar --- ui/cocoa.m | 60 ++++++++++++++++++++++++++++++++++++++++++++++++----------= -- 1 file changed, 48 insertions(+), 12 deletions(-) diff --git a/ui/cocoa.m b/ui/cocoa.m index 207555edf7..e89020929b 100644 --- a/ui/cocoa.m +++ b/ui/cocoa.m @@ -52,6 +52,8 @@ /* macOS 10.12 deprecated many constants, #define the new names for older = SDKs */ #if MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_12 #define NSEventMaskAny NSAnyEventMask +#define NSEventModifierFlagCapsLock NSAlphaShiftKeyMask +#define NSEventModifierFlagShift NSShiftKeyMask #define NSEventModifierFlagCommand NSCommandKeyMask #define NSEventModifierFlagControl NSControlKeyMask #define NSEventModifierFlagOption NSAlternateKeyMask @@ -268,7 +270,7 @@ static void handleAnyDeviceErrors(Error * err) NSWindow *fullScreenWindow; float cx,cy,cw,ch,cdx,cdy; CGDataProviderRef dataProviderRef; - int modifiers_state[256]; + BOOL modifiers_state[256]; BOOL isMouseGrabbed; BOOL isFullscreen; BOOL isAbsoluteEnabled; @@ -536,18 +538,59 @@ QemuCocoaView *cocoaView; } } =20 +- (void) toggleModifier: (int)keycode { + // Toggle the stored state. + modifiers_state[keycode] =3D !modifiers_state[keycode]; + // Send a keyup or keydown depending on the state. + qemu_input_event_send_key_qcode(dcl->con, keycode, modifiers_state[key= code]); +} + +- (void) toggleStatefulModifier: (int)keycode { + // Toggle the stored state. + modifiers_state[keycode] =3D !modifiers_state[keycode]; + // Generate keydown and keyup. + qemu_input_event_send_key_qcode(dcl->con, keycode, true); + qemu_input_event_send_key_qcode(dcl->con, keycode, false); +} + - (void) handleEvent:(NSEvent *)event { COCOA_DEBUG("QemuCocoaView: handleEvent\n"); =20 int buttons =3D 0; - int keycode; + int keycode =3D 0; bool mouse_event =3D false; NSPoint p =3D [event locationInWindow]; =20 switch ([event type]) { case NSEventTypeFlagsChanged: - keycode =3D cocoa_keycode_to_qemu([event keyCode]); + if ([event keyCode] =3D=3D 0) { + // When the Cocoa keyCode is zero that means keys should be + // synthesized based on the values in in the eventModifiers + // bitmask. + + if (qemu_console_is_graphic(NULL)) { + NSEventModifierFlags modifiers =3D [event modifierFlag= s]; + + if (!!(modifiers & NSEventModifierFlagCapsLock) !=3D != !modifiers_state[Q_KEY_CODE_CAPS_LOCK]) { + [self toggleStatefulModifier:Q_KEY_CODE_CAPS_LOCK]; + } + if (!!(modifiers & NSEventModifierFlagShift) !=3D !!mo= difiers_state[Q_KEY_CODE_SHIFT]) { + [self toggleModifier:Q_KEY_CODE_SHIFT]; + } + if (!!(modifiers & NSEventModifierFlagControl) !=3D !!= modifiers_state[Q_KEY_CODE_CTRL]) { + [self toggleModifier:Q_KEY_CODE_CTRL]; + } + if (!!(modifiers & NSEventModifierFlagOption) !=3D !!m= odifiers_state[Q_KEY_CODE_ALT]) { + [self toggleModifier:Q_KEY_CODE_ALT]; + } + if (!!(modifiers & NSEventModifierFlagCommand) !=3D !!= modifiers_state[Q_KEY_CODE_META_L]) { + [self toggleModifier:Q_KEY_CODE_META_L]; + } + } + } else { + keycode =3D cocoa_keycode_to_qemu([event keyCode]); + } =20 if ((keycode =3D=3D Q_KEY_CODE_META_L || keycode =3D=3D Q_KEY_= CODE_META_R) && !isMouseGrabbed) { @@ -559,16 +602,9 @@ QemuCocoaView *cocoaView; // emulate caps lock and num lock keydown and keyup if (keycode =3D=3D Q_KEY_CODE_CAPS_LOCK || keycode =3D=3D Q_KEY_CODE_NUM_LOCK) { - qemu_input_event_send_key_qcode(dcl->con, keycode, tru= e); - qemu_input_event_send_key_qcode(dcl->con, keycode, fal= se); + [self toggleStatefulModifier:keycode]; } else if (qemu_console_is_graphic(NULL)) { - if (modifiers_state[keycode] =3D=3D 0) { // keydown - qemu_input_event_send_key_qcode(dcl->con, keycode,= true); - modifiers_state[keycode] =3D 1; - } else { // keyup - qemu_input_event_send_key_qcode(dcl->con, keycode,= false); - modifiers_state[keycode] =3D 0; - } + [self toggleModifier:keycode]; } } =20 --=20 2.13.0.rc1.294.g07d810a77f-goog