1 // Copyright 2013 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #include "chrome/browser/chromeos/keyboard_driven_event_rewriter.h" 6 7 #include <X11/Xlib.h> 8 9 #include "chrome/browser/chromeos/login/user_manager.h" 10 #include "chrome/browser/chromeos/system/input_device_settings.h" 11 #include "ui/events/event.h" 12 13 namespace chromeos { 14 15 namespace { 16 17 const int kModifierMask = ui::EF_SHIFT_DOWN; 18 19 // Returns true if and only if it is on login screen (i.e. user is not logged 20 // in) and the keyboard driven flag in the OEM manifest is on. 21 bool ShouldStripModifiersForArrowKeysAndEnter() { 22 if (UserManager::IsInitialized() && 23 !UserManager::Get()->IsUserLoggedIn()) { 24 return system::keyboard_settings::ForceKeyboardDrivenUINavigation(); 25 } 26 27 return false; 28 } 29 30 } // namespace 31 32 KeyboardDrivenEventRewriter::KeyboardDrivenEventRewriter() {} 33 34 KeyboardDrivenEventRewriter::~KeyboardDrivenEventRewriter() {} 35 36 bool KeyboardDrivenEventRewriter::RewriteIfKeyboardDrivenOnLoginScreen( 37 ui::KeyEvent* event) { 38 if (!ShouldStripModifiersForArrowKeysAndEnter()) 39 return false; 40 41 return RewriteEvent(event); 42 } 43 44 bool KeyboardDrivenEventRewriter::RewriteForTesting(ui::KeyEvent* event) { 45 return RewriteEvent(event); 46 } 47 48 bool KeyboardDrivenEventRewriter::RewriteEvent(ui::KeyEvent* event) { 49 if ((event->flags() & kModifierMask) != kModifierMask) 50 return false; 51 52 if (event->key_code() != ui::VKEY_LEFT && 53 event->key_code() != ui::VKEY_RIGHT && 54 event->key_code() != ui::VKEY_UP && 55 event->key_code() != ui::VKEY_DOWN && 56 event->key_code() != ui::VKEY_RETURN && 57 event->key_code() != ui::VKEY_F6) { 58 return false; 59 } 60 61 XEvent* xev = event->native_event(); 62 XKeyEvent* xkey = &(xev->xkey); 63 xkey->state &= ~(ControlMask | Mod1Mask | ShiftMask); 64 event->set_flags(event->flags() & ~kModifierMask); 65 event->NormalizeFlags(); 66 return true; 67 } 68 69 } // namespace chromeos 70