Supervised user whitelists: Cleanup
[chromium-blink-merge.git] / ui / wm / core / nested_accelerator_dispatcher_linux.cc
bloba5ce30a381ab174e29a953039a4a613578c2152d
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.
5 #include "ui/wm/core/nested_accelerator_dispatcher.h"
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"
17 #if defined(USE_X11)
18 #include <X11/Xlib.h>
19 #endif
21 namespace wm {
23 namespace {
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();
30 #elif defined(USE_X11)
31 bool IsKeyEvent(const XEvent* xev) {
32 return xev->type == KeyPress || xev->type == KeyRelease;
34 #else
35 #error Unknown build platform: you should have either use_ozone or use_x11.
36 #endif
38 scoped_ptr<ui::ScopedEventDispatcher> OverrideDispatcher(
39 ui::PlatformEventDispatcher* dispatcher) {
40 ui::PlatformEventSource* source = ui::PlatformEventSource::GetInstance();
41 return source ? source->OverrideDispatcher(dispatcher) : nullptr;
44 } // namespace
46 class NestedAcceleratorDispatcherLinux : public NestedAcceleratorDispatcher,
47 public ui::PlatformEventDispatcher {
48 public:
49 explicit NestedAcceleratorDispatcherLinux(NestedAcceleratorDelegate* delegate)
50 : NestedAcceleratorDispatcher(delegate),
51 restore_dispatcher_(OverrideDispatcher(this)) {}
53 ~NestedAcceleratorDispatcherLinux() override {}
55 private:
56 // AcceleratorDispatcher:
57 scoped_ptr<base::RunLoop> CreateRunLoop() override {
58 return make_scoped_ptr(new base::RunLoop());
61 // ui::PlatformEventDispatcher:
62 bool CanDispatchEvent(const ui::PlatformEvent& event) override {
63 return true;
66 uint32_t DispatchEvent(const ui::PlatformEvent& event) override {
67 if (IsKeyEvent(event)) {
68 ui::KeyEvent key_event(event);
69 ui::Accelerator accelerator = CreateAcceleratorFromKeyEvent(key_event);
71 switch (delegate_->ProcessAccelerator(accelerator)) {
72 case NestedAcceleratorDelegate::RESULT_PROCESS_LATER:
73 #if defined(USE_X11)
74 XPutBackEvent(event->xany.display, event);
75 #else
76 NOTIMPLEMENTED();
77 #endif
78 return ui::POST_DISPATCH_NONE;
79 case NestedAcceleratorDelegate::RESULT_PROCESSED:
80 return ui::POST_DISPATCH_NONE;
81 case NestedAcceleratorDelegate::RESULT_NOT_PROCESSED:
82 break;
85 ui::PlatformEventDispatcher* prev = *restore_dispatcher_;
87 uint32_t perform_default = ui::POST_DISPATCH_PERFORM_DEFAULT;
88 return prev ? prev->DispatchEvent(event) : perform_default;
91 scoped_ptr<ui::ScopedEventDispatcher> restore_dispatcher_;
93 DISALLOW_COPY_AND_ASSIGN(NestedAcceleratorDispatcherLinux);
96 scoped_ptr<NestedAcceleratorDispatcher> NestedAcceleratorDispatcher::Create(
97 NestedAcceleratorDelegate* delegate,
98 base::MessagePumpDispatcher* nested_dispatcher) {
99 return make_scoped_ptr(new NestedAcceleratorDispatcherLinux(delegate));
102 } // namespace wm