OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "ash/accelerators/accelerator_dispatcher.h" | 5 #include "ash/accelerators/accelerator_dispatcher.h" |
6 | 6 |
| 7 #if defined(USE_X11) |
| 8 #include <X11/Xlib.h> |
| 9 |
| 10 // Xlib defines RootWindow |
| 11 #ifdef RootWindow |
| 12 #undef RootWindow |
| 13 #endif |
| 14 #endif // defined(USE_X11) |
| 15 |
| 16 #include "ash/accelerators/accelerator_controller.h" |
| 17 #include "ash/shell.h" |
| 18 #include "ui/aura/env.h" |
| 19 #include "ui/aura/event.h" |
| 20 #include "ui/aura/root_window.h" |
| 21 #include "ui/base/accelerators/accelerator.h" |
| 22 #include "ui/base/events.h" |
| 23 |
7 namespace ash { | 24 namespace ash { |
| 25 namespace { |
| 26 |
| 27 const int kModifierMask = (ui::EF_SHIFT_DOWN | |
| 28 ui::EF_CONTROL_DOWN | |
| 29 ui::EF_ALT_DOWN); |
| 30 #if defined(OS_WIN) |
| 31 bool IsKeyEvent(const MSG& msg) { |
| 32 return |
| 33 msg.message == WM_KEYDOWN || msg.message == WM_SYSKEYDOWN || |
| 34 msg.message == WM_KEYUP || msg.message == WM_SYSKEYUP; |
| 35 } |
| 36 bool IsKeyRelease(const MSG& msg) { |
| 37 return msg.message == WM_KEYUP || msg.message == WM_SYSKEYUP; |
| 38 } |
| 39 #endif |
| 40 |
| 41 #if defined(USE_X11) |
| 42 bool IsKeyEvent(const XEvent* xev) { |
| 43 return xev->type == KeyPress || xev->type == KeyRelease; |
| 44 } |
| 45 bool IsKeyRelease(const XEvent* xev) { |
| 46 return xev->type == KeyRelease; |
| 47 } |
| 48 #endif |
| 49 |
| 50 } // namespace |
8 | 51 |
9 AcceleratorDispatcher::AcceleratorDispatcher( | 52 AcceleratorDispatcher::AcceleratorDispatcher( |
10 MessageLoop::Dispatcher* nested_dispatcher, aura::Window* associated_window) | 53 MessageLoop::Dispatcher* nested_dispatcher, aura::Window* associated_window) |
11 : nested_dispatcher_(nested_dispatcher), | 54 : nested_dispatcher_(nested_dispatcher), |
12 associated_window_(associated_window) { | 55 associated_window_(associated_window) { |
13 DCHECK(nested_dispatcher_); | 56 DCHECK(nested_dispatcher_); |
14 associated_window_->AddObserver(this); | 57 associated_window_->AddObserver(this); |
15 } | 58 } |
16 | 59 |
17 AcceleratorDispatcher::~AcceleratorDispatcher() { | 60 AcceleratorDispatcher::~AcceleratorDispatcher() { |
18 if (associated_window_) | 61 if (associated_window_) |
19 associated_window_->RemoveObserver(this); | 62 associated_window_->RemoveObserver(this); |
20 } | 63 } |
21 | 64 |
22 void AcceleratorDispatcher::OnWindowDestroying(aura::Window* window) { | 65 void AcceleratorDispatcher::OnWindowDestroying(aura::Window* window) { |
23 if (associated_window_ == window) | 66 if (associated_window_ == window) |
24 associated_window_ = NULL; | 67 associated_window_ = NULL; |
25 } | 68 } |
26 | 69 |
| 70 base::DispatchStatus AcceleratorDispatcher::Dispatch( |
| 71 const base::NativeEvent& event) { |
| 72 if (!associated_window_) |
| 73 return base::EVENT_QUIT; |
| 74 if (!ui::IsNoopEvent(event) && !associated_window_->CanReceiveEvents()) |
| 75 return aura::Env::GetInstance()->GetDispatcher()->Dispatch(event); |
| 76 |
| 77 if (IsKeyEvent(event)) { |
| 78 ash::AcceleratorController* accelerator_controller = |
| 79 ash::Shell::GetInstance()->accelerator_controller(); |
| 80 if (accelerator_controller) { |
| 81 ui::Accelerator accelerator(ui::KeyboardCodeFromNative(event), |
| 82 ui::EventFlagsFromNative(event) & kModifierMask); |
| 83 if (IsKeyRelease(event)) |
| 84 accelerator.set_type(ui::ET_KEY_RELEASED); |
| 85 if (accelerator_controller->Process(accelerator)) |
| 86 return base::EVENT_PROCESSED; |
| 87 accelerator.set_type(aura::TranslatedKeyEvent(event, false).type()); |
| 88 if (accelerator_controller->Process(accelerator)) |
| 89 return base::EVENT_PROCESSED; |
| 90 } |
| 91 } |
| 92 |
| 93 return nested_dispatcher_->Dispatch(event); |
| 94 } |
| 95 |
27 } // namespace ash | 96 } // namespace ash |
OLD | NEW |