summaryrefslogtreecommitdiffstats
path: root/base/message_pump_glib_x.h
blob: fc3f3b1145d78699cbb51ee10bdb86addf221e17 (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
// Copyright (c) 2010 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.

#ifndef BASE_MESSAGE_PUMP_GLIB_X_H
#define BASE_MESSAGE_PUMP_GLIB_X_H

#include "base/message_pump.h"
#include "base/message_pump_glib.h"

#include <bitset>
#include <set>

#include <glib.h>
#include <gtk/gtk.h>
#include <X11/X.h>

namespace base {

class MessagePumpGlibX : public MessagePumpForUI {
 public:
  MessagePumpGlibX();
  virtual ~MessagePumpGlibX();

  // MessagePumpForUI implementation.
  virtual bool RunOnce(GMainContext* context, bool block);

  // Indicates whether a GDK event was injected by chrome (when |true|) or if it
  // was captured and being processed by GDK (when |false|).
  bool IsDispatchingEvent(void) { return dispatching_event_; }

#if defined(HAVE_XINPUT2)
  // Setup an X Window for XInput2 events.
  void SetupXInput2ForXWindow(Window xid);
#endif

 private:
  static void EventDispatcherX(GdkEvent* event, gpointer data);

  // Update the lookup table and flag the events that should be captured and
  // processed so that GDK doesn't get to them.
  void InitializeEventsToCapture(void);

#if defined(HAVE_XINPUT2)
  // Initialize X2 input.
  void InitializeXInput2(void);

  // The opcode used for checking events.
  int xiopcode_;

  // The list of master pointer devices. We maintain this list so that it is not
  // necessary to query X for the list of devices for each GdkWindow created.
  std::set<int> masters_;

  // The list of slave (physical) pointer devices.
  // TODO(sad): This is currently unused, and may be removed eventually.
  std::set<int> slaves_;
#endif

  // The event source for GDK events.
  GSource* gdksource_;

  // The default GDK event dispatcher. This is stored so that it can be restored
  // when necessary during nested event dispatching.
  gboolean (*gdkdispatcher_)(GSource*, GSourceFunc, void*);

  // Indicates whether a GDK event was injected by chrome (when |true|) or if it
  // was captured and being processed by GDK (when |false|).
  bool dispatching_event_;

#if ! GTK_CHECK_VERSION(2,18,0)
// GDK_EVENT_LAST was introduced in GTK+ 2.18.0. For earlier versions, we pick a
// large enough value (the value of GDK_EVENT_LAST in 2.18.0) so that it works
// for all versions.
#define GDK_EVENT_LAST 37
#endif

  // We do not want to process all the events ourselves. So we use a lookup
  // table to quickly check if a particular event should be handled by us or if
  // it should be passed on to the default GDK handler.
  std::bitset<LASTEvent> capture_x_events_;
  std::bitset<GDK_EVENT_LAST> capture_gdk_events_;

  DISALLOW_COPY_AND_ASSIGN(MessagePumpGlibX);
};

}  // namespace base

#endif  // BASE_MESSAGE_PUMP_GLIB_X_H