Managed bookmarks for supervised users.
[chromium-blink-merge.git] / ash / touch / touchscreen_util.cc
blobe68644db40164a38ac0c079f0d39a261a9973ff8
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 "ash/touch/touchscreen_util.h"
7 #include <set>
9 #include "base/logging.h"
10 #include "ui/events/devices/input_device.h"
12 namespace ash {
14 void AssociateTouchscreens(std::vector<DisplayInfo>* displays,
15 const std::vector<ui::TouchscreenDevice>& devices) {
16 std::set<int> no_match_touchscreen;
17 int internal_touchscreen = -1;
18 for (size_t i = 0; i < devices.size(); ++i) {
19 if (devices[i].type == ui::InputDeviceType::INPUT_DEVICE_INTERNAL) {
20 internal_touchscreen = i;
21 break;
25 DisplayInfo* internal_state = NULL;
26 for (size_t i = 0; i < displays->size(); ++i) {
27 DisplayInfo* state = &(*displays)[i];
28 if (state->id() == gfx::Display::InternalDisplayId() &&
29 !state->GetNativeModeSize().IsEmpty() &&
30 state->touch_support() == gfx::Display::TOUCH_SUPPORT_UNKNOWN) {
31 internal_state = state;
32 break;
36 if (internal_state && internal_touchscreen >= 0) {
37 internal_state->set_touch_device_id(devices[internal_touchscreen].id);
38 internal_state->set_touch_support(gfx::Display::TOUCH_SUPPORT_AVAILABLE);
39 VLOG(2) << "Found internal touchscreen for internal display "
40 << internal_state->id() << " touch_device_id "
41 << internal_state->touch_device_id() << " size "
42 << devices[internal_touchscreen].size.ToString();
45 for (size_t i = 0; i < devices.size(); ++i) {
46 if (internal_state && internal_state->touch_device_id() == devices[i].id)
47 continue;
49 bool found_mapping = false;
50 for (size_t j = 0; j < displays->size(); ++j) {
51 DisplayInfo* state = &(*displays)[j];
52 const gfx::Size native_size = state->GetNativeModeSize();
53 if (state->touch_support() == gfx::Display::TOUCH_SUPPORT_AVAILABLE ||
54 native_size.IsEmpty())
55 continue;
57 // Allow 1 pixel difference between screen and touchscreen
58 // resolutions. Because in some cases for monitor resolution
59 // 1024x768 touchscreen's resolution would be 1024x768, but for
60 // some 1023x767. It really depends on touchscreen's firmware
61 // configuration.
62 if (std::abs(native_size.width() - devices[i].size.width()) <= 1 &&
63 std::abs(native_size.height() - devices[i].size.height()) <= 1) {
64 state->set_touch_device_id(devices[i].id);
65 state->set_touch_support(gfx::Display::TOUCH_SUPPORT_AVAILABLE);
67 VLOG(2) << "Found touchscreen for display " << state->id()
68 << " touch_device_id " << state->touch_device_id() << " size "
69 << devices[i].size.ToString();
70 found_mapping = true;
71 break;
75 if (!found_mapping) {
76 no_match_touchscreen.insert(devices[i].id);
77 VLOG(2) << "No matching display for touch_device_id " << devices[i].id
78 << " size " << devices[i].size.ToString();
82 // Sometimes we can't find a matching screen for the touchscreen, e.g.
83 // due to the touchscreen's reporting range having no correlation with the
84 // screen's resolution. In this case, we arbitrarily assign unmatched
85 // touchscreens to unmatched screens.
86 for (std::set<int>::iterator it = no_match_touchscreen.begin();
87 it != no_match_touchscreen.end();
88 ++it) {
89 for (size_t i = 0; i < displays->size(); ++i) {
90 DisplayInfo* state = &(*displays)[i];
91 if (state->id() != gfx::Display::InternalDisplayId() &&
92 !state->GetNativeModeSize().IsEmpty() &&
93 state->touch_support() == gfx::Display::TOUCH_SUPPORT_UNKNOWN) {
94 state->set_touch_device_id(*it);
95 state->set_touch_support(gfx::Display::TOUCH_SUPPORT_AVAILABLE);
96 VLOG(2) << "Arbitrarily matching touchscreen "
97 << state->touch_device_id() << " to display " << state->id();
98 break;
104 } // namespace ash