~ubuntu-branches/ubuntu/saucy/autopilot/saucy-proposed

« back to all changes in this revision

Viewing changes to autopilot/display/__init__.py

  • Committer: Package Import Robot
  • Author(s): Didier Roche
  • Date: 2013-06-07 13:33:46 UTC
  • mfrom: (57.1.1 saucy-proposed)
  • Revision ID: package-import@ubuntu.com-20130607133346-42zvbl1h2k1v54ac
Tags: 1.3daily13.06.05-0ubuntu2
autopilot-touch only suggests python-ubuntu-platform-api for now.
It's not in distro and we need that requirement to be fulfilled to
have unity 7, 100 scopes and the touch stack to distro.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- Mode: Python; coding: utf-8; indent-tabs-mode: nil; tab-width: 4 -*-
 
2
#
 
3
# Autopilot Functional Test Tool
 
4
# Copyright (C) 2012-2013 Canonical
 
5
#
 
6
# This program is free software: you can redistribute it and/or modify
 
7
# it under the terms of the GNU General Public License as published by
 
8
# the Free Software Foundation, either version 3 of the License, or
 
9
# (at your option) any later version.
 
10
#
 
11
# This program is distributed in the hope that it will be useful,
 
12
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
# GNU General Public License for more details.
 
15
#
 
16
# You should have received a copy of the GNU General Public License
 
17
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
18
#
 
19
 
 
20
 
 
21
"""The display module contaions support for getting screen information."""
 
22
 
 
23
from collections import OrderedDict
 
24
from autopilot.utilities import _pick_backend
 
25
from autopilot.input import Mouse
 
26
 
 
27
 
 
28
def is_rect_on_screen(screen_number, rect):
 
29
    """Returns True if *rect* is **entirely** on the specified screen, with no overlap."""
 
30
    (x, y, w, h) = rect
 
31
    (mx, my, mw, mh) = Display.create().get_screen_geometry(screen_number)
 
32
    return (x >= mx and x + w <= mx + mw and y >= my and y + h <= my + mh)
 
33
 
 
34
 
 
35
def is_point_on_screen(screen_number, point):
 
36
    """Returns True if *point* is on the specified screen.
 
37
 
 
38
    *point* must be an iterable type with two elements: (x, y)
 
39
 
 
40
    """
 
41
    x, y = point
 
42
    (mx, my, mw, mh) = Display.create().get_screen_geometry(screen_number)
 
43
    return (x >= mx and x < mx + mw and y >= my and y < my + mh)
 
44
 
 
45
 
 
46
def is_point_on_any_screen(point):
 
47
    """Returns true if *point* is on any currently configured screen."""
 
48
    return any([is_point_on_screen(m, point) for m in range(Display.create().get_num_screens())])
 
49
 
 
50
 
 
51
def move_mouse_to_screen(screen_number):
 
52
    """Move the mouse to the center of the specified screen."""
 
53
    geo = Display.create().get_screen_geometry(screen_number)
 
54
    x = geo[0] + (geo[2] / 2)
 
55
    y = geo[1] + (geo[3] / 2)
 
56
    #dont animate this or it might not get there due to barriers
 
57
    Mouse.create().move(x, y, False)
 
58
 
 
59
 
 
60
# veebers TODO: Write this so it's usable.
 
61
# def drag_window_to_screen(self, window, screen):
 
62
#     """Drags *window* to *screen*
 
63
 
 
64
#     :param BamfWindow window: The window to drag
 
65
#     :param integer screen: The screen to drag the *window* to
 
66
#     :raises: **TypeError** if *window* is not a BamfWindow
 
67
 
 
68
#     """
 
69
#     if not isinstance(window, BamfWindow):
 
70
#         raise TypeError("Window must be a BamfWindow")
 
71
 
 
72
#     if window.monitor == screen:
 
73
#         logger.debug("Window %r is already on screen %d." % (window.x_id, screen))
 
74
#         return
 
75
 
 
76
#     assert(not window.is_maximized)
 
77
#     (win_x, win_y, win_w, win_h) = window.geometry
 
78
#     (mx, my, mw, mh) = self.get_screen_geometry(screen)
 
79
 
 
80
#     logger.debug("Dragging window %r to screen %d." % (window.x_id, screen))
 
81
 
 
82
#     mouse = Mouse()
 
83
#     keyboard = Keyboard()
 
84
#     mouse.move(win_x + win_w/2, win_y + win_h/2)
 
85
#     keyboard.press("Alt")
 
86
#     mouse.press()
 
87
#     keyboard.release("Alt")
 
88
 
 
89
#     # We do the movements in two steps, to reduce the risk of being
 
90
#     # blocked by the pointer barrier
 
91
#     target_x = mx + mw/2
 
92
#     target_y = my + mh/2
 
93
#     mouse.move(win_x, target_y, rate=20, time_between_events=0.005)
 
94
#     mouse.move(target_x, target_y, rate=20, time_between_events=0.005)
 
95
#     mouse.release()
 
96
 
 
97
 
 
98
class Display:
 
99
    """The base class/inteface for the display devices"""
 
100
 
 
101
    @staticmethod
 
102
    def create(preferred_backend=''):
 
103
        """Get an instance of the Display class.
 
104
 
 
105
        For more infomration on picking specific backends, see :ref:`tut-picking-backends`
 
106
 
 
107
        :param preferred_backend: A string containing a hint as to which backend you
 
108
            would like.
 
109
 
 
110
            possible backends are:
 
111
 
 
112
            * ``X11`` - Get display information from X11.
 
113
            * ``UPA`` - Get display information from the ubuntu platform API.
 
114
        :raises: RuntimeError if autopilot cannot instantate any of the possible
 
115
            backends.
 
116
        :raises: RuntimeError if the preferred_backend is specified and is not
 
117
            one of the possible backends for this device class.
 
118
        :raises: :class:`~autopilot.BackendException` if the preferred_backend is
 
119
            set, but that backend could not be instantiated.
 
120
 
 
121
        """
 
122
        def get_x11_display():
 
123
            from autopilot.display._X11 import Display
 
124
            return Display()
 
125
 
 
126
        def get_upa_display():
 
127
            from autopilot.display._upa import Display
 
128
            return Display()
 
129
 
 
130
        backends = OrderedDict()
 
131
        backends['X11'] = get_x11_display
 
132
        backends['UPA'] = get_upa_display
 
133
        return _pick_backend(backends, preferred_backend)
 
134
 
 
135
    class BlacklistedDriverError(RuntimeError):
 
136
        """Cannot set primary monitor when running drivers listed in the driver blacklist."""
 
137
 
 
138
    def get_num_screens(self):
 
139
        """Get the number of screens attached to the PC."""
 
140
        raise NotImplementedError("You cannot use this class directly.")
 
141
 
 
142
    def get_primary_screen(self):
 
143
        raise NotImplementedError("You cannot use this class directly.")
 
144
 
 
145
    def get_screen_width(self, screen_number=0):
 
146
        raise NotImplementedError("You cannot use this class directly.")
 
147
 
 
148
    def get_screen_height(self, screen_number=0):
 
149
        raise NotImplementedError("You cannot use this class directly.")
 
150
 
 
151
    def get_screen_geometry(self, monitor_number):
 
152
        """Get the geometry for a particular monitor.
 
153
 
 
154
        :return: Tuple containing (x, y, width, height).
 
155
 
 
156
        """
 
157
        raise NotImplementedError("You cannot use this class directly.")