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