5311a828369183a2c9cf8dfebafffb89cd67ac7f
[platform/framework/web/crosswalk.git] / src / ui / display / chromeos / touchscreen_delegate_impl.cc
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.
4
5 #include "ui/display/chromeos/touchscreen_delegate_impl.h"
6
7 #include <cmath>
8 #include <set>
9
10 #include "ui/display/types/chromeos/display_mode.h"
11 #include "ui/display/types/chromeos/display_snapshot.h"
12 #include "ui/display/types/chromeos/touchscreen_device_manager.h"
13
14 namespace ui {
15
16 TouchscreenDelegateImpl::TouchscreenDelegateImpl(
17     scoped_ptr<TouchscreenDeviceManager> touch_device_manager)
18     : touch_device_manager_(touch_device_manager.Pass()) {}
19
20 TouchscreenDelegateImpl::~TouchscreenDelegateImpl() {}
21
22 void TouchscreenDelegateImpl::AssociateTouchscreens(
23     std::vector<DisplayConfigurator::DisplayState>* displays) {
24   std::set<int> no_match_touchscreen;
25   std::vector<TouchscreenDevice> devices = touch_device_manager_->GetDevices();
26   for (size_t i = 0; i < devices.size(); ++i) {
27     bool found_mapping = false;
28     for (size_t j = 0; j < displays->size(); ++j) {
29       DisplayConfigurator::DisplayState* state = &(*displays)[j];
30       const DisplayMode* mode = state->display->native_mode();
31       if (state->touch_device_id != 0 || !mode)
32         continue;
33
34       // Allow 1 pixel difference between screen and touchscreen
35       // resolutions. Because in some cases for monitor resolution
36       // 1024x768 touchscreen's resolution would be 1024x768, but for
37       // some 1023x767. It really depends on touchscreen's firmware
38       // configuration.
39       if (std::abs(mode->size().width() - devices[i].size.width()) <= 1 &&
40           std::abs(mode->size().height() - devices[i].size.height()) <= 1) {
41         state->touch_device_id = devices[i].id;
42
43         VLOG(2) << "Found touchscreen for display "
44                 << state->display->display_id() << " touch_device_id "
45                 << state->touch_device_id << " size "
46                 << devices[i].size.ToString();
47         found_mapping = true;
48         break;
49       }
50     }
51
52     if (!found_mapping) {
53       no_match_touchscreen.insert(devices[i].id);
54       VLOG(2) << "No matching display for touch_device_id "
55               << devices[i].id << " size " << devices[i].size.ToString();
56     }
57   }
58
59   // Sometimes we can't find a matching screen for the touchscreen, e.g.
60   // due to the touchscreen's reporting range having no correlation with the
61   // screen's resolution. In this case, we arbitrarily assign unmatched
62   // touchscreens to unmatched screens.
63   for (std::set<int>::iterator it = no_match_touchscreen.begin();
64        it != no_match_touchscreen.end();
65        ++it) {
66     for (size_t i = 0; i < displays->size(); ++i) {
67       DisplayConfigurator::DisplayState* state = &(*displays)[i];
68       if (state->display->type() != DISPLAY_CONNECTION_TYPE_INTERNAL &&
69           state->display->native_mode() != NULL &&
70           state->touch_device_id == 0) {
71         state->touch_device_id = *it;
72         VLOG(2) << "Arbitrarily matching touchscreen "
73                 << state->touch_device_id << " to display "
74                 << state->display->display_id();
75         break;
76       }
77     }
78   }
79 }
80
81 }  // namespace ui