summaryrefslogtreecommitdiffstats
path: root/chrome/browser/chromeos/touchpad.cc
blob: e5146b09d97ff12846e1d3db128464a4f768b449 (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
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
131
132
133
134
// Copyright (c) 2009 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 "chrome/browser/chromeos/touchpad.h"

#include <stdlib.h>
#include <string>
#include <vector>

#include "base/string_util.h"
#include "base/process_util.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/chrome_thread.h"
#include "chrome/common/notification_service.h"
#include "chrome/common/pref_member.h"
#include "chrome/common/pref_names.h"
#include "chrome/common/pref_service.h"

// Allows InvokeLater without adding refcounting.  The object is only deleted
// when its last InvokeLater is run anyway.
template <>
struct RunnableMethodTraits<Touchpad> {
  void RetainCallee(Touchpad*) {}
  void ReleaseCallee(Touchpad*) {}
};

// static
void Touchpad::RegisterUserPrefs(PrefService* prefs) {
  prefs->RegisterBooleanPref(prefs::kTapToClickEnabled, false);
  prefs->RegisterBooleanPref(prefs::kVertEdgeScrollEnabled, false);
  prefs->RegisterIntegerPref(prefs::kTouchpadSpeedFactor, 5);
  prefs->RegisterIntegerPref(prefs::kTouchpadSensitivity, 5);
}

void Touchpad::Init(PrefService* prefs) {
  tap_to_click_enabled_.Init(prefs::kTapToClickEnabled, prefs, this);
  vert_edge_scroll_enabled_.Init(prefs::kVertEdgeScrollEnabled, prefs, this);
  speed_factor_.Init(prefs::kTouchpadSpeedFactor, prefs, this);
  sensitivity_.Init(prefs::kTouchpadSensitivity, prefs, this);

  // Initialize touchpad settings to what's saved in user preferences.
  SetTapToClick();
  SetVertEdgeScroll();
  SetSpeedFactor();
  SetSensitivity();
}

void Touchpad::Observe(NotificationType type,
                       const NotificationSource& source,
                       const NotificationDetails& details) {
  if (type == NotificationType::PREF_CHANGED)
    NotifyPrefChanged(Details<std::wstring>(details).ptr());
}

void Touchpad::NotifyPrefChanged(const std::wstring* pref_name) {
  if (!pref_name || *pref_name == prefs::kTapToClickEnabled)
    SetTapToClick();
  if (!pref_name || *pref_name == prefs::kVertEdgeScrollEnabled)
    SetVertEdgeScroll();
  if (!pref_name || *pref_name == prefs::kTouchpadSpeedFactor)
    SetSpeedFactor();
  if (!pref_name || *pref_name == prefs::kTouchpadSensitivity)
    SetSensitivity();
}

void Touchpad::SetSynclientParam(const std::string& param, double value) {
  // If not running on the file thread, then re-run on the file thread.
  if (!ChromeThread::CurrentlyOn(ChromeThread::FILE)) {
    base::Thread* file_thread = g_browser_process->file_thread();
    if (file_thread)
      file_thread->message_loop()->PostTask(FROM_HERE,
          NewRunnableMethod(this, &Touchpad::SetSynclientParam, param, value));
  } else {
    // launch binary synclient to set the parameter
    std::vector<std::string> argv;
    argv.push_back("/usr/bin/synclient");
    argv.push_back(StringPrintf("%s=%f", param.c_str(), value));
    base::file_handle_mapping_vector no_files;
    base::ProcessHandle handle;
    if (!base::LaunchApp(argv, no_files, true, &handle))
      LOG(ERROR) << "Failed to call /usr/bin/synclient";
  }
}

void Touchpad::SetTapToClick() {
  // To disable tap-to-click (i.e. a tap on the touchpad is recognized as a left
  // mouse click event), we set MaxTapTime to 0. MaxTapTime is the maximum time
  // (in milliseconds) for detecting a tap. The default is 180.
  if (tap_to_click_enabled_.GetValue())
    SetSynclientParam("MaxTapTime", 180);
  else
    SetSynclientParam("MaxTapTime", 0);
}

void Touchpad::SetVertEdgeScroll() {
  // To disable vertical edge scroll, we set VertEdgeScroll to 0. Vertical edge
  // scroll lets you use the right edge of the touchpad to control the movement
  // of the vertical scroll bar.
  if (vert_edge_scroll_enabled_.GetValue())
    SetSynclientParam("VertEdgeScroll", 1);
  else
    SetSynclientParam("VertEdgeScroll", 0);
}

void Touchpad::SetSpeedFactor() {
  // To set speed factor, we use MaxSpeed. MinSpeed is set to 0.2.
  // MaxSpeed can go from 0.2 to 1.1. The preference is an integer between
  // 1 and 10, so we divide that by 10 and add 0.1 for the value of MaxSpeed.
  int value = speed_factor_.GetValue();
  if (value < 1)
    value = 1;
  if (value > 10)
    value = 10;
  // Convert from 1-10 to 0.2-1.1
  double d = static_cast<double>(value) / 10.0 + 0.1;
  SetSynclientParam("MaxSpeed", d);
}

void Touchpad::SetSensitivity() {
  // To set the touch sensitivity, we use FingerHigh, which represents the
  // the pressure needed for a tap to be registered. The range of FingerHigh
  // goes from 25 to 70. We store the sensitivity preference as an int from
  // 1 to 10. So we need to map the preference value of 1 to 10 to the
  // FingerHigh value of 25 to 70 inversely.
  int value = sensitivity_.GetValue();
  if (value < 1)
    value = 1;
  if (value > 10)
    value = 10;
  // Convert from 1-10 to 70-25.
  double d = (15 - value) * 5;
  SetSynclientParam("FingerHigh", d);
}