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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
|
// Copyright (c) 2011 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 "views/touchui/touch_factory.h"
#include <gdk/gdkx.h>
#include <X11/extensions/XInput2.h>
#include "base/compiler_specific.h"
#include "base/logging.h"
#include "ui/base/x/x11_util.h"
// The X cursor is hidden if it is idle for kCursorIdleSeconds seconds.
static int kCursorIdleSeconds = 5;
namespace views {
// static
TouchFactory* TouchFactory::GetInstance() {
return Singleton<TouchFactory>::get();
}
TouchFactory::TouchFactory()
: is_cursor_visible_(true),
cursor_timer_(),
touch_device_list_() {
Pixmap blank;
XColor black;
static char nodata[] = { 0,0,0,0,0,0,0,0 };
black.red = black.green = black.blue = 0;
Display* display = ui::GetXDisplay();
blank = XCreateBitmapFromData(display, ui::GetX11RootWindow(), nodata, 8, 8);
invisible_cursor_ = XCreatePixmapCursor(display, blank, blank,
&black, &black, 0, 0);
SetCursorVisible(false, false);
}
TouchFactory::~TouchFactory() {
SetCursorVisible(true, false);
XFreeCursor(ui::GetXDisplay(), invisible_cursor_);
}
void TouchFactory::SetTouchDeviceList(
const std::vector<unsigned int>& devices) {
touch_device_lookup_.reset();
touch_device_list_.clear();
for (std::vector<unsigned int>::const_iterator iter = devices.begin();
iter != devices.end(); ++iter) {
DCHECK(*iter < touch_device_lookup_.size());
touch_device_lookup_[*iter] = true;
touch_device_list_.push_back(*iter);
}
}
bool TouchFactory::IsTouchDevice(unsigned deviceid) {
return deviceid < touch_device_lookup_.size() ?
touch_device_lookup_[deviceid] : false;
}
bool TouchFactory::GrabTouchDevices(Display* display, ::Window window) {
if (touch_device_list_.empty())
return true;
unsigned char mask[(XI_LASTEVENT + 7) / 8];
bool success = true;
memset(mask, 0, sizeof(mask));
XISetMask(mask, XI_ButtonPress);
XISetMask(mask, XI_ButtonRelease);
XISetMask(mask, XI_Motion);
XIEventMask evmask;
evmask.mask_len = sizeof(mask);
evmask.mask = mask;
for (std::vector<int>::const_iterator iter =
touch_device_list_.begin();
iter != touch_device_list_.end(); ++iter) {
evmask.deviceid = *iter;
Status status = XIGrabDevice(display, *iter, window, CurrentTime, None,
GrabModeAsync, GrabModeAsync, False, &evmask);
success = success && status == GrabSuccess;
}
return success;
}
bool TouchFactory::UngrabTouchDevices(Display* display) {
bool success = true;
for (std::vector<int>::const_iterator iter =
touch_device_list_.begin();
iter != touch_device_list_.end(); ++iter) {
Status status = XIUngrabDevice(display, *iter, CurrentTime);
success = success && status == GrabSuccess;
}
return success;
}
void TouchFactory::SetCursorVisible(bool show, bool start_timer) {
// The cursor is going to be shown. Reset the timer for hiding it.
if (show && start_timer) {
cursor_timer_.Stop();
cursor_timer_.Start(base::TimeDelta::FromSeconds(kCursorIdleSeconds),
this, &TouchFactory::HideCursorForInactivity),
} else {
cursor_timer_.Stop();
}
if (show == is_cursor_visible_)
return;
is_cursor_visible_ = show;
GdkDisplay* display = gdk_display_get_default();
if (!display)
return;
Display* xdisplay = GDK_DISPLAY_XDISPLAY(display);
Window window = DefaultRootWindow(xdisplay);
if (is_cursor_visible_) {
XUndefineCursor(xdisplay, window);
} else {
XDefineCursor(xdisplay, window, invisible_cursor_);
}
}
} // namespace views
|