OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "ui/wm/core/nested_accelerator_dispatcher.h" |
6 | 6 |
7 #include "base/memory/scoped_ptr.h" | 7 #include "base/memory/scoped_ptr.h" |
8 #include "base/run_loop.h" | 8 #include "base/run_loop.h" |
9 #include "ui/events/event.h" | 9 #include "ui/events/event.h" |
10 #include "ui/events/platform/platform_event_dispatcher.h" | 10 #include "ui/events/platform/platform_event_dispatcher.h" |
11 #include "ui/events/platform/platform_event_source.h" | 11 #include "ui/events/platform/platform_event_source.h" |
12 #include "ui/events/platform/scoped_event_dispatcher.h" | 12 #include "ui/events/platform/scoped_event_dispatcher.h" |
| 13 #include "ui/wm/core/nested_accelerator_delegate.h" |
13 | 14 |
14 #if defined(USE_X11) | 15 #if defined(USE_X11) |
15 #include <X11/Xlib.h> | 16 #include <X11/Xlib.h> |
16 #endif | 17 #endif |
17 | 18 |
18 namespace ash { | 19 namespace wm { |
19 | 20 |
20 namespace { | 21 namespace { |
21 | 22 |
22 #if defined(USE_OZONE) | 23 #if defined(USE_OZONE) |
23 bool IsKeyEvent(const base::NativeEvent& native_event) { | 24 bool IsKeyEvent(const base::NativeEvent& native_event) { |
24 const ui::KeyEvent* event = static_cast<const ui::KeyEvent*>(native_event); | 25 const ui::KeyEvent* event = static_cast<const ui::KeyEvent*>(native_event); |
25 return event->IsKeyEvent(); | 26 return event->IsKeyEvent(); |
26 } | 27 } |
27 #elif defined(USE_X11) | 28 #elif defined(USE_X11) |
28 bool IsKeyEvent(const XEvent* xev) { | 29 bool IsKeyEvent(const XEvent* xev) { |
29 return xev->type == KeyPress || xev->type == KeyRelease; | 30 return xev->type == KeyPress || xev->type == KeyRelease; |
30 } | 31 } |
31 #else | 32 #else |
32 #error Unknown build platform: you should have either use_ozone or use_x11. | 33 #error Unknown build platform: you should have either use_ozone or use_x11. |
33 #endif | 34 #endif |
34 | 35 |
35 scoped_ptr<ui::ScopedEventDispatcher> OverrideDispatcher( | 36 scoped_ptr<ui::ScopedEventDispatcher> OverrideDispatcher( |
36 ui::PlatformEventDispatcher* dispatcher) { | 37 ui::PlatformEventDispatcher* dispatcher) { |
37 ui::PlatformEventSource* source = ui::PlatformEventSource::GetInstance(); | 38 ui::PlatformEventSource* source = ui::PlatformEventSource::GetInstance(); |
38 return source ? source->OverrideDispatcher(dispatcher) | 39 return source ? source->OverrideDispatcher(dispatcher) |
39 : scoped_ptr<ui::ScopedEventDispatcher>(); | 40 : scoped_ptr<ui::ScopedEventDispatcher>(); |
40 } | 41 } |
41 | 42 |
42 } // namespace | 43 } // namespace |
43 | 44 |
44 class AcceleratorDispatcherLinux : public AcceleratorDispatcher, | 45 class NestedAcceleratorDispatcherLinux : public NestedAcceleratorDispatcher, |
45 public ui::PlatformEventDispatcher { | 46 public ui::PlatformEventDispatcher { |
46 public: | 47 public: |
47 AcceleratorDispatcherLinux() | 48 explicit NestedAcceleratorDispatcherLinux(NestedAcceleratorDelegate* delegate) |
48 : restore_dispatcher_(OverrideDispatcher(this)) {} | 49 : NestedAcceleratorDispatcher(delegate), |
| 50 restore_dispatcher_(OverrideDispatcher(this)) {} |
49 | 51 |
50 virtual ~AcceleratorDispatcherLinux() {} | 52 virtual ~NestedAcceleratorDispatcherLinux() {} |
51 | 53 |
52 private: | 54 private: |
53 // AcceleratorDispatcher: | 55 // AcceleratorDispatcher: |
54 virtual scoped_ptr<base::RunLoop> CreateRunLoop() OVERRIDE { | 56 virtual scoped_ptr<base::RunLoop> CreateRunLoop() OVERRIDE { |
55 return scoped_ptr<base::RunLoop>(new base::RunLoop()); | 57 return scoped_ptr<base::RunLoop>(new base::RunLoop()); |
56 } | 58 } |
57 | 59 |
58 // ui::PlatformEventDispatcher: | 60 // ui::PlatformEventDispatcher: |
59 virtual bool CanDispatchEvent(const ui::PlatformEvent& event) OVERRIDE { | 61 virtual bool CanDispatchEvent(const ui::PlatformEvent& event) OVERRIDE { |
60 return true; | 62 return true; |
61 } | 63 } |
62 | 64 |
63 virtual uint32_t DispatchEvent(const ui::PlatformEvent& event) OVERRIDE { | 65 virtual uint32_t DispatchEvent(const ui::PlatformEvent& event) OVERRIDE { |
64 if (IsKeyEvent(event)) { | 66 if (IsKeyEvent(event)) { |
65 ui::KeyEvent key_event(event, false); | 67 ui::KeyEvent key_event(event, false); |
66 if (MenuClosedForPossibleAccelerator(key_event)) { | 68 if (!delegate_->ShouldProcessEventNow(key_event)) { |
67 #if defined(USE_X11) | 69 #if defined(USE_X11) |
68 XPutBackEvent(event->xany.display, event); | 70 XPutBackEvent(event->xany.display, event); |
69 #else | 71 #else |
70 NOTIMPLEMENTED(); | 72 NOTIMPLEMENTED(); |
71 #endif | 73 #endif |
72 return ui::POST_DISPATCH_NONE; | 74 return ui::POST_DISPATCH_NONE; |
73 } | 75 } |
74 | 76 |
75 if (AcceleratorProcessedForKeyEvent(key_event)) | 77 if (delegate_->ProcessEvent(key_event)) |
76 return ui::POST_DISPATCH_NONE; | 78 return ui::POST_DISPATCH_NONE; |
77 } | 79 } |
| 80 ui::PlatformEventDispatcher* prev = *restore_dispatcher_; |
78 | 81 |
79 ui::PlatformEventDispatcher* prev = *restore_dispatcher_; | |
80 return prev ? prev->DispatchEvent(event) | 82 return prev ? prev->DispatchEvent(event) |
81 : ui::POST_DISPATCH_PERFORM_DEFAULT; | 83 : ui::POST_DISPATCH_PERFORM_DEFAULT; |
82 } | 84 } |
83 | 85 |
84 scoped_ptr<ui::ScopedEventDispatcher> restore_dispatcher_; | 86 scoped_ptr<ui::ScopedEventDispatcher> restore_dispatcher_; |
85 | 87 |
86 DISALLOW_COPY_AND_ASSIGN(AcceleratorDispatcherLinux); | 88 DISALLOW_COPY_AND_ASSIGN(NestedAcceleratorDispatcherLinux); |
87 }; | 89 }; |
88 | 90 |
89 scoped_ptr<AcceleratorDispatcher> AcceleratorDispatcher::Create( | 91 scoped_ptr<NestedAcceleratorDispatcher> NestedAcceleratorDispatcher::Create( |
| 92 NestedAcceleratorDelegate* delegate, |
90 base::MessagePumpDispatcher* nested_dispatcher) { | 93 base::MessagePumpDispatcher* nested_dispatcher) { |
91 return scoped_ptr<AcceleratorDispatcher>(new AcceleratorDispatcherLinux()); | 94 return scoped_ptr<NestedAcceleratorDispatcher>( |
| 95 new NestedAcceleratorDispatcherLinux(delegate)); |
92 } | 96 } |
93 | 97 |
94 } // namespace ash | 98 } // namespace wm |
OLD | NEW |