1
# -*- Mode: Python; coding: utf-8; indent-tabs-mode: nil; tab-width: 4 -*-
3
# Autopilot Functional Test Tool
4
# Copyright (C) 2012-2013 Canonical
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.
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.
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/>.
21
"""The display module contaions support for getting screen information."""
23
from collections import OrderedDict
24
from autopilot.utilities import _pick_backend
25
from autopilot.input import Mouse
28
def is_rect_on_screen(screen_number, rect):
29
"""Returns True if *rect* is **entirely** on the specified screen, with no overlap."""
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)
35
def is_point_on_screen(screen_number, point):
36
"""Returns True if *point* is on the specified screen.
38
*point* must be an iterable type with two elements: (x, y)
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)
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())])
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)
60
# veebers TODO: Write this so it's usable.
61
# def drag_window_to_screen(self, window, screen):
62
# """Drags *window* to *screen*
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
69
# if not isinstance(window, BamfWindow):
70
# raise TypeError("Window must be a BamfWindow")
72
# if window.monitor == screen:
73
# logger.debug("Window %r is already on screen %d." % (window.x_id, screen))
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)
80
# logger.debug("Dragging window %r to screen %d." % (window.x_id, screen))
83
# keyboard = Keyboard()
84
# mouse.move(win_x + win_w/2, win_y + win_h/2)
85
# keyboard.press("Alt")
87
# keyboard.release("Alt")
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)
99
"""The base class/inteface for the display devices"""
102
def create(preferred_backend=''):
103
"""Get an instance of the Display class.
105
For more infomration on picking specific backends, see :ref:`tut-picking-backends`
107
:param preferred_backend: A string containing a hint as to which backend you
110
possible backends are:
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
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.
122
def get_x11_display():
123
from autopilot.display._X11 import Display
126
def get_upa_display():
127
from autopilot.display._upa import Display
130
backends = OrderedDict()
131
backends['X11'] = get_x11_display
132
backends['UPA'] = get_upa_display
133
return _pick_backend(backends, preferred_backend)
135
class BlacklistedDriverError(RuntimeError):
136
"""Cannot set primary monitor when running drivers listed in the driver blacklist."""
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.")
142
def get_primary_screen(self):
143
raise NotImplementedError("You cannot use this class directly.")
145
def get_screen_width(self, screen_number=0):
146
raise NotImplementedError("You cannot use this class directly.")
148
def get_screen_height(self, screen_number=0):
149
raise NotImplementedError("You cannot use this class directly.")
151
def get_screen_geometry(self, monitor_number):
152
"""Get the geometry for a particular monitor.
154
:return: Tuple containing (x, y, width, height).
157
raise NotImplementedError("You cannot use this class directly.")