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"
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
;
35 #error Unknown build platform: you should have either use_ozone or use_x11.
38 scoped_ptr
<ui::ScopedEventDispatcher
> OverrideDispatcher(
39 ui::PlatformEventDispatcher
* dispatcher
) {
40 ui::PlatformEventSource
* source
= ui::PlatformEventSource::GetInstance();
41 return source
? source
->OverrideDispatcher(dispatcher
) : nullptr;
46 class NestedAcceleratorDispatcherLinux
: public NestedAcceleratorDispatcher
,
47 public ui::PlatformEventDispatcher
{
49 explicit NestedAcceleratorDispatcherLinux(NestedAcceleratorDelegate
* delegate
)
50 : NestedAcceleratorDispatcher(delegate
),
51 restore_dispatcher_(OverrideDispatcher(this)) {}
53 ~NestedAcceleratorDispatcherLinux() override
{}
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
{
66 uint32_t DispatchEvent(const ui::PlatformEvent
& event
) override
{
67 if (IsKeyEvent(event
)) {
68 ui::Accelerator
accelerator((ui::KeyEvent(event
)));
70 switch (delegate_
->ProcessAccelerator(accelerator
)) {
71 case NestedAcceleratorDelegate::RESULT_PROCESS_LATER
:
73 XPutBackEvent(event
->xany
.display
, event
);
77 return ui::POST_DISPATCH_NONE
;
78 case NestedAcceleratorDelegate::RESULT_PROCESSED
:
79 return ui::POST_DISPATCH_NONE
;
80 case NestedAcceleratorDelegate::RESULT_NOT_PROCESSED
:
84 ui::PlatformEventDispatcher
* prev
= *restore_dispatcher_
;
86 uint32_t perform_default
= ui::POST_DISPATCH_PERFORM_DEFAULT
;
87 return prev
? prev
->DispatchEvent(event
) : perform_default
;
90 scoped_ptr
<ui::ScopedEventDispatcher
> restore_dispatcher_
;
92 DISALLOW_COPY_AND_ASSIGN(NestedAcceleratorDispatcherLinux
);
95 scoped_ptr
<NestedAcceleratorDispatcher
> NestedAcceleratorDispatcher::Create(
96 NestedAcceleratorDelegate
* delegate
,
97 base::MessagePumpDispatcher
* nested_dispatcher
) {
98 return make_scoped_ptr(new NestedAcceleratorDispatcherLinux(delegate
));