~jocave/checkbox/hybrid-amd-gpu-mods

« back to all changes in this revision

Viewing changes to providers/plainbox-provider-checkbox/bin/touchpad_test

  • Committer: Zygmunt Krynicki
  • Date: 2013-05-17 13:54:25 UTC
  • mto: This revision was merged to the branch mainline in revision 2130.
  • Revision ID: zygmunt.krynicki@canonical.com-20130517135425-cxcenxx5t0qrtbxd
checkbox-ng: add CheckBoxNG sub-project

CheckBoxNG (or lowercase as checkbox-ng, pypi:checkbox-ng) is a clean
implementation of CheckBox on top of PlainBox. It provides a new
executable, 'checkbox' that has some of the same commands that were
previously implemented in the plainbox package.

In particular CheckBoxNG comes with the 'checkbox sru' command
(the same one as in plainbox). Later on this sub-command will be removed
from plainbox.

CheckBoxNG depends on plainbox >= 0.3

Signed-off-by: Zygmunt Krynicki <zygmunt.krynicki@canonical.com>

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!/usr/bin/env python3
2
 
 
3
 
import sys
4
 
import gettext
5
 
 
6
 
from gettext import gettext as _
7
 
from gi.repository import Gio, Gtk, Gdk
8
 
from optparse import OptionParser
9
 
 
10
 
 
11
 
EXIT_WITH_FAILURE = 1
12
 
EXIT_WITH_SUCCESS = 0
13
 
EXIT_TIMEOUT = 30
14
 
 
15
 
 
16
 
class Direction(object):
17
 
 
18
 
    def __init__(self, name):
19
 
        self.name = name
20
 
        self.tested = False
21
 
        self.value = getattr(Gdk.ScrollDirection, name.upper())
22
 
 
23
 
 
24
 
class GtkScroller(object):
25
 
 
26
 
    exit_code = EXIT_WITH_FAILURE
27
 
 
28
 
    def __init__(self, directions, edge_scroll=False):
29
 
        self.directions = directions
30
 
        self.edge_scroll = edge_scroll
31
 
        self.touchpad_key = "org.gnome.settings-daemon.peripherals.touchpad"
32
 
        self.horiz_scroll_key = True
33
 
        source = Gio.SettingsSchemaSource.get_default()
34
 
        if not source.lookup(self.touchpad_key, True):
35
 
            self.touchpad_key = "org.gnome.desktop.peripherals.touchpad"
36
 
            self.horiz_scroll_key = False
37
 
        self.touchpad_settings = Gio.Settings.new(self.touchpad_key)
38
 
 
39
 
        # Initialize GTK constants
40
 
        self.ICON_SIZE = Gtk.IconSize.BUTTON
41
 
        self.ICON_TESTED = Gtk.STOCK_YES
42
 
        self.ICON_UNTESTED = Gtk.STOCK_INDEX
43
 
        self.ICON_NOT_REQUIRED = Gtk.STOCK_REMOVE
44
 
 
45
 
        self.button_factory = Gtk.Button
46
 
        self.hbox_factory = Gtk.HBox
47
 
        self.image_factory = Gtk.Image
48
 
        self.label_factory = Gtk.Label
49
 
        self.vbox_factory = Gtk.VBox
50
 
 
51
 
        # Create GTK window.
52
 
        window = Gtk.Window()
53
 
        window.set_type_hint(Gdk.WindowType.TOPLEVEL)
54
 
        window.add_events(
55
 
            Gdk.EventMask.SCROLL_MASK | Gdk.EventMask.SMOOTH_SCROLL_MASK)
56
 
        window.set_size_request(200, 100)
57
 
        window.set_resizable(False)
58
 
        window.set_title(_("Type Text"))
59
 
        window.connect("delete-event", lambda w, e: self.quit())
60
 
        window.connect("scroll-event", self.on_scroll)
61
 
        window.show()
62
 
 
63
 
        # Add common widgets to the window.
64
 
        vbox = self._add_vbox(window)
65
 
        self.label = self._add_label(vbox)
66
 
        button_hbox = self._add_hbox(vbox)
67
 
        validation_hbox = self._add_hbox(vbox)
68
 
        self.status = self._add_label(vbox)
69
 
        self.exit_button = self._add_button(vbox, Gtk.STOCK_CLOSE)
70
 
        self.exit_button.connect("clicked", lambda w: self.quit())
71
 
 
72
 
        # Add widgets for each direction.
73
 
        self.icons = {}
74
 
        for direction in self.directions:
75
 
            self._add_label(button_hbox, direction.name)
76
 
            self.icons[direction] = self._add_image(
77
 
                validation_hbox, Gtk.STOCK_INDEX)
78
 
 
79
 
        self.show_text(
80
 
            _("Please move the mouse cursor to this window.") +
81
 
            "\n" +
82
 
            _("Then scroll in each direction on your touchpad."))
83
 
 
84
 
    def _add_button(self, context, stock):
85
 
        button = self.button_factory(stock=stock)
86
 
        context.add(button)
87
 
        button.show()
88
 
        return button
89
 
 
90
 
    def _add_hbox(self, context, spacing=4):
91
 
        hbox = self.hbox_factory()
92
 
        context.add(hbox)
93
 
        hbox.set_spacing(4)
94
 
        hbox.show()
95
 
        return hbox
96
 
 
97
 
    def _add_image(self, context, stock):
98
 
        image = self.image_factory(stock=stock, icon_size=self.ICON_SIZE)
99
 
        context.add(image)
100
 
        image.show()
101
 
        return image
102
 
 
103
 
    def _add_label(self, context, text=None):
104
 
        label = self.label_factory()
105
 
        context.add(label)
106
 
        label.set_size_request(0, 0)
107
 
        label.set_line_wrap(True)
108
 
        if text:
109
 
            label.set_text(text)
110
 
        label.show()
111
 
        return label
112
 
 
113
 
    def _add_vbox(self, context):
114
 
        vbox = self.vbox_factory()
115
 
        vbox.set_homogeneous(False)
116
 
        vbox.set_spacing(8)
117
 
        context.add(vbox)
118
 
        vbox.show()
119
 
        return vbox
120
 
 
121
 
    def run(self):
122
 
        # Save touchpad settings.
123
 
        if self.horiz_scroll_key:
124
 
            self.saved_horiz_scroll_enabled = \
125
 
                self.touchpad_settings.get_boolean("horiz-scroll-enabled")
126
 
        self.saved_scroll_method = self.touchpad_settings.get_string(
127
 
            "scroll-method")
128
 
 
129
 
        # Set touchpad settings.
130
 
        if self.horiz_scroll_key:
131
 
            self.touchpad_settings.set_boolean("horiz-scroll-enabled", True)
132
 
        if self.edge_scroll:
133
 
            self.touchpad_settings.set_string(
134
 
                "scroll-method", "edge-scrolling")
135
 
 
136
 
        Gtk.main()
137
 
 
138
 
    def quit(self):
139
 
        # Reset touchpad settings.
140
 
        if self.horiz_scroll_key:
141
 
            self.touchpad_settings.set_boolean(
142
 
                "horiz-scroll-enabled", self.saved_horiz_scroll_enabled)
143
 
        self.touchpad_settings.set_string(
144
 
            "scroll-method", self.saved_scroll_method)
145
 
 
146
 
        Gtk.main_quit()
147
 
 
148
 
    def show_text(self, text, widget=None):
149
 
        if widget is None:
150
 
            widget = self.label
151
 
        widget.set_text(text)
152
 
 
153
 
    def found_direction(self, direction):
154
 
        direction.tested = True
155
 
        self.icons[direction].set_from_stock(
156
 
            self.ICON_TESTED, size=self.ICON_SIZE)
157
 
        self.check_directions()
158
 
 
159
 
    def check_directions(self):
160
 
        if all([direction.tested for direction in self.directions]):
161
 
            self.show_text(
162
 
                _("All required directions have been tested!"), self.status)
163
 
            self.exit_code = EXIT_WITH_SUCCESS
164
 
            self.exit_button.grab_focus()
165
 
 
166
 
    def on_scroll(self, window, event):
167
 
        for direction in self.directions:
168
 
            if direction.value == event.direction:
169
 
                self.found_direction(direction)
170
 
                break
171
 
 
172
 
        return True
173
 
 
174
 
 
175
 
def main(args):
176
 
    gettext.textdomain("checkbox")
177
 
 
178
 
    usage = """Usage: %prog DIRECTION... [--edge-scroll]"""
179
 
    parser = OptionParser(usage=usage)
180
 
    parser.add_option("--edge-scroll", action="store_true", default=False,
181
 
                      help="Force touchpad to use edge scrolling only")
182
 
    (options, args) = parser.parse_args(args)
183
 
 
184
 
    if not args:
185
 
        parser.error("Must specify directions to test.")
186
 
 
187
 
    directions = []
188
 
    for arg in args:
189
 
        try:
190
 
            direction = Direction(arg)
191
 
        except AttributeError:
192
 
            parser.error("Unsupported direction: %s" % arg)
193
 
        directions.append(direction)
194
 
 
195
 
    scroller = GtkScroller(directions, edge_scroll=options.edge_scroll)
196
 
    try:
197
 
        scroller.run()
198
 
    except KeyboardInterrupt:
199
 
        scroller.show_text(_("Test interrupted"), scroller.status)
200
 
        scroller.quit()
201
 
 
202
 
    return scroller.exit_code
203
 
 
204
 
if __name__ == "__main__":
205
 
    sys.exit(main(sys.argv[1:]))