summaryrefslogtreecommitdiffstats
path: root/ash/accelerators/accelerator_dispatcher_linux.cc
blob: b5494dd2e129f32fa18db2c035b2f145aad8c6ff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ash/accelerators/accelerator_dispatcher.h"

#include "base/memory/scoped_ptr.h"
#include "base/run_loop.h"
#include "ui/events/event.h"
#include "ui/events/platform/platform_event_dispatcher.h"
#include "ui/events/platform/platform_event_source.h"
#include "ui/events/platform/scoped_event_dispatcher.h"

#if defined(USE_X11)
#include <X11/Xlib.h>
#endif

namespace ash {

namespace {

#if defined(USE_OZONE)
bool IsKeyEvent(const base::NativeEvent& native_event) {
  const ui::KeyEvent* event = static_cast<const ui::KeyEvent*>(native_event);
  return event->IsKeyEvent();
}
#elif defined(USE_X11)
bool IsKeyEvent(const XEvent* xev) {
  return xev->type == KeyPress || xev->type == KeyRelease;
}
#else
#error Unknown build platform: you should have either use_ozone or use_x11.
#endif

scoped_ptr<ui::ScopedEventDispatcher> OverrideDispatcher(
    ui::PlatformEventDispatcher* dispatcher) {
  ui::PlatformEventSource* source = ui::PlatformEventSource::GetInstance();
  return source ? source->OverrideDispatcher(dispatcher)
                : scoped_ptr<ui::ScopedEventDispatcher>();
}

}  // namespace

class AcceleratorDispatcherLinux : public AcceleratorDispatcher,
                                   public ui::PlatformEventDispatcher {
 public:
  AcceleratorDispatcherLinux()
      : restore_dispatcher_(OverrideDispatcher(this)) {}

  virtual ~AcceleratorDispatcherLinux() {}

 private:
  // AcceleratorDispatcher:
  virtual scoped_ptr<base::RunLoop> CreateRunLoop() OVERRIDE {
    return scoped_ptr<base::RunLoop>(new base::RunLoop());
  }

  // ui::PlatformEventDispatcher:
  virtual bool CanDispatchEvent(const ui::PlatformEvent& event) OVERRIDE {
    return true;
  }

  virtual uint32_t DispatchEvent(const ui::PlatformEvent& event) OVERRIDE {
    if (IsKeyEvent(event)) {
      ui::KeyEvent key_event(event, false);
      if (MenuClosedForPossibleAccelerator(key_event)) {
#if defined(USE_X11)
        XPutBackEvent(event->xany.display, event);
#else
        NOTIMPLEMENTED();
#endif
        return ui::POST_DISPATCH_NONE;
      }

      if (AcceleratorProcessedForKeyEvent(key_event))
        return ui::POST_DISPATCH_NONE;
    }

    ui::PlatformEventDispatcher* prev = *restore_dispatcher_;
    return prev ? prev->DispatchEvent(event)
                : ui::POST_DISPATCH_PERFORM_DEFAULT;
  }

  scoped_ptr<ui::ScopedEventDispatcher> restore_dispatcher_;

  DISALLOW_COPY_AND_ASSIGN(AcceleratorDispatcherLinux);
};

scoped_ptr<AcceleratorDispatcher> AcceleratorDispatcher::Create(
    base::MessagePumpDispatcher* nested_dispatcher) {
  return scoped_ptr<AcceleratorDispatcher>(new AcceleratorDispatcherLinux());
}

}  // namespace ash