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