base: Change DCHECK_IS_ON to a macro DCHECK_IS_ON().
[chromium-blink-merge.git] / ui / wm / core / nested_accelerator_dispatcher_linux.cc
blob3016fc031b4c415a9f867db0ca26313d523c2b6c
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)
42 : scoped_ptr<ui::ScopedEventDispatcher>();
45 } // namespace
47 class NestedAcceleratorDispatcherLinux : public NestedAcceleratorDispatcher,
48 public ui::PlatformEventDispatcher {
49 public:
50 explicit NestedAcceleratorDispatcherLinux(NestedAcceleratorDelegate* delegate)
51 : NestedAcceleratorDispatcher(delegate),
52 restore_dispatcher_(OverrideDispatcher(this)) {}
54 ~NestedAcceleratorDispatcherLinux() override {}
56 private:
57 // AcceleratorDispatcher:
58 scoped_ptr<base::RunLoop> CreateRunLoop() override {
59 return make_scoped_ptr(new base::RunLoop());
62 // ui::PlatformEventDispatcher:
63 bool CanDispatchEvent(const ui::PlatformEvent& event) override {
64 return true;
67 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);
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;
86 ui::PlatformEventDispatcher* prev = *restore_dispatcher_;
88 uint32_t perform_default = ui::POST_DISPATCH_PERFORM_DEFAULT;
89 return prev ? prev->DispatchEvent(event) : perform_default;
92 scoped_ptr<ui::ScopedEventDispatcher> restore_dispatcher_;
94 DISALLOW_COPY_AND_ASSIGN(NestedAcceleratorDispatcherLinux);
97 scoped_ptr<NestedAcceleratorDispatcher> NestedAcceleratorDispatcher::Create(
98 NestedAcceleratorDelegate* delegate,
99 base::MessagePumpDispatcher* nested_dispatcher) {
100 return make_scoped_ptr(new NestedAcceleratorDispatcherLinux(delegate));
103 } // namespace wm