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
)
42 : scoped_ptr
<ui::ScopedEventDispatcher
>();
47 class NestedAcceleratorDispatcherLinux
: public NestedAcceleratorDispatcher
,
48 public ui::PlatformEventDispatcher
{
50 explicit NestedAcceleratorDispatcherLinux(NestedAcceleratorDelegate
* delegate
)
51 : NestedAcceleratorDispatcher(delegate
),
52 restore_dispatcher_(OverrideDispatcher(this)) {}
54 virtual ~NestedAcceleratorDispatcherLinux() {}
57 // AcceleratorDispatcher:
58 virtual scoped_ptr
<base::RunLoop
> CreateRunLoop() OVERRIDE
{
59 return scoped_ptr
<base::RunLoop
>(new base::RunLoop());
62 // ui::PlatformEventDispatcher:
63 virtual bool CanDispatchEvent(const ui::PlatformEvent
& event
) OVERRIDE
{
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
);
72 switch (delegate_
->ProcessAccelerator(accelerator
)) {
73 case NestedAcceleratorDelegate::RESULT_PROCESS_LATER
:
75 XPutBackEvent(event
->xany
.display
, event
);
79 return ui::POST_DISPATCH_NONE
;
80 case NestedAcceleratorDelegate::RESULT_PROCESSED
:
81 return ui::POST_DISPATCH_NONE
;
82 case NestedAcceleratorDelegate::RESULT_NOT_PROCESSED
:
86 ui::PlatformEventDispatcher
* prev
= *restore_dispatcher_
;
88 return prev
? prev
->DispatchEvent(event
)
89 : ui::POST_DISPATCH_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 scoped_ptr
<NestedAcceleratorDispatcher
>(
101 new NestedAcceleratorDispatcherLinux(delegate
));