2008-09-29 16:49:18 +02:00
|
|
|
---------------------------------------------------------------------------
|
2014-05-20 13:02:39 +02:00
|
|
|
--- Screen module for awful
|
|
|
|
--
|
2008-09-29 16:49:18 +02:00
|
|
|
-- @author Julien Danjou <julien@danjou.info>
|
|
|
|
-- @copyright 2008 Julien Danjou
|
|
|
|
-- @release @AWESOME_VERSION@
|
2014-05-20 13:02:39 +02:00
|
|
|
-- @module awful.screen
|
2008-09-29 16:49:18 +02:00
|
|
|
---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
-- Grab environment we need
|
|
|
|
local capi =
|
|
|
|
{
|
|
|
|
mouse = mouse,
|
|
|
|
screen = screen,
|
|
|
|
client = client
|
|
|
|
}
|
|
|
|
local util = require("awful.util")
|
2012-09-28 23:53:58 +02:00
|
|
|
|
2016-02-26 19:56:07 +01:00
|
|
|
local function get_screen(s)
|
|
|
|
return s and screen[s]
|
|
|
|
end
|
|
|
|
|
2012-09-28 23:53:58 +02:00
|
|
|
-- we use require("awful.client") inside functions to prevent circular dependencies.
|
|
|
|
local client
|
2008-09-29 16:49:18 +02:00
|
|
|
|
2012-06-12 20:13:09 +02:00
|
|
|
local screen = {}
|
2008-09-29 16:49:18 +02:00
|
|
|
|
2009-05-04 11:09:19 +02:00
|
|
|
local data = {}
|
|
|
|
data.padding = {}
|
|
|
|
|
2015-10-09 20:54:17 +02:00
|
|
|
screen.mouse_per_screen = {}
|
2015-10-08 03:52:03 +02:00
|
|
|
|
2016-02-26 19:56:07 +01:00
|
|
|
-- @param s Screen
|
2015-10-10 20:16:49 +02:00
|
|
|
-- @param x X coordinate of point
|
|
|
|
-- @param y Y coordinate of point
|
|
|
|
-- @return The squared distance of the screen to the provided point
|
|
|
|
function screen.getdistance_sq(s, x, y)
|
2016-02-26 19:56:07 +01:00
|
|
|
s = get_screen(s)
|
|
|
|
local geom = s.geometry
|
2015-10-10 20:16:49 +02:00
|
|
|
local dist_x, dist_y = 0, 0
|
|
|
|
if x < geom.x then
|
|
|
|
dist_x = geom.x - x
|
|
|
|
elseif x >= geom.x + geom.width then
|
|
|
|
dist_x = x - geom.x - geom.width
|
|
|
|
end
|
|
|
|
if y < geom.y then
|
|
|
|
dist_y = geom.y - y
|
|
|
|
elseif y >= geom.y + geom.height then
|
|
|
|
dist_y = y - geom.y - geom.height
|
|
|
|
end
|
|
|
|
return dist_x * dist_x + dist_y * dist_y
|
|
|
|
end
|
|
|
|
|
2012-01-15 11:28:17 +01:00
|
|
|
---
|
2016-02-26 19:56:07 +01:00
|
|
|
-- Return screen number corresponding to the given (pixel) coordinates.
|
2012-01-15 11:28:17 +01:00
|
|
|
-- The number returned can be used as an index into the global
|
|
|
|
-- `screen` table/object.
|
|
|
|
-- @param x The x coordinate
|
|
|
|
-- @param y The y coordinate
|
2015-10-10 20:16:49 +02:00
|
|
|
function screen.getbycoord(x, y)
|
2016-02-26 19:56:07 +01:00
|
|
|
local s = screen[1]
|
2015-10-10 20:16:49 +02:00
|
|
|
local dist = screen.getdistance_sq(s, x, y)
|
|
|
|
for i = 2, capi.screen:count() do
|
|
|
|
local d = screen.getdistance_sq(i, x, y)
|
|
|
|
if d < dist then
|
2016-02-26 19:56:07 +01:00
|
|
|
s, dist = screen[i], d
|
2012-01-15 11:28:17 +01:00
|
|
|
end
|
|
|
|
end
|
2016-02-26 19:56:07 +01:00
|
|
|
return s.index
|
2012-01-15 11:28:17 +01:00
|
|
|
end
|
|
|
|
|
2015-10-08 03:52:03 +02:00
|
|
|
--- Give the focus to a screen, and move pointer to last known position on this
|
|
|
|
-- screen, or keep position relative to the current focused screen
|
2015-01-31 16:40:46 +01:00
|
|
|
-- @param _screen Screen number (defaults / falls back to mouse.screen).
|
2012-06-12 20:13:09 +02:00
|
|
|
function screen.focus(_screen)
|
2012-09-28 23:53:58 +02:00
|
|
|
client = client or require("awful.client")
|
2016-02-26 19:56:07 +01:00
|
|
|
if type(_screen) == "number" and _screen > capi.screen.count() then _screen = screen.focused() end
|
|
|
|
_screen = get_screen(_screen)
|
2012-09-14 20:14:22 +02:00
|
|
|
|
|
|
|
-- screen and pos for current screen
|
2016-02-26 19:56:07 +01:00
|
|
|
local s = get_screen(capi.mouse.screen)
|
2015-10-08 03:52:03 +02:00
|
|
|
local pos
|
|
|
|
|
2015-10-09 20:54:17 +02:00
|
|
|
if not screen.mouse_per_screen[_screen] then
|
2015-10-08 03:52:03 +02:00
|
|
|
-- This is the first time we enter this screen,
|
|
|
|
-- keep relative mouse position on the new screen
|
2015-10-11 12:44:28 +02:00
|
|
|
pos = capi.mouse.coords()
|
2016-02-26 19:56:07 +01:00
|
|
|
local relx = (pos.x - s.geometry.x) / s.geometry.width
|
|
|
|
local rely = (pos.y - s.geometry.y) / s.geometry.height
|
2012-09-14 20:14:22 +02:00
|
|
|
|
2016-02-26 19:56:07 +01:00
|
|
|
pos.x = _screen.geometry.x + relx * _screen.geometry.width
|
|
|
|
pos.y = _screen.geometry.y + rely * _screen.geometry.height
|
2015-10-08 03:52:03 +02:00
|
|
|
else
|
|
|
|
-- restore mouse position
|
2015-10-09 20:54:17 +02:00
|
|
|
pos = screen.mouse_per_screen[_screen]
|
2015-10-08 03:52:03 +02:00
|
|
|
end
|
2012-09-14 20:14:22 +02:00
|
|
|
|
2015-10-08 04:40:59 +02:00
|
|
|
-- save pointer position of current screen
|
2015-10-09 20:54:17 +02:00
|
|
|
screen.mouse_per_screen[s] = capi.mouse.coords()
|
2015-10-08 04:40:59 +02:00
|
|
|
|
2012-09-14 20:14:22 +02:00
|
|
|
-- move cursor without triggering signals mouse::enter and mouse::leave
|
|
|
|
capi.mouse.coords(pos, true)
|
|
|
|
|
2016-02-27 09:24:19 +01:00
|
|
|
local c = client.focus.history.get(_screen, 0)
|
2015-05-13 13:36:28 +02:00
|
|
|
if c then
|
2015-06-19 22:24:12 +02:00
|
|
|
c:emit_signal("request::activate", "screen.focus", {raise=false})
|
2015-05-13 13:36:28 +02:00
|
|
|
end
|
2009-08-21 16:05:02 +02:00
|
|
|
end
|
|
|
|
|
2015-10-08 03:52:03 +02:00
|
|
|
--- Give the focus to a screen, and move pointer to last known position on this
|
|
|
|
-- screen, or keep position relative to the current focused screen
|
2012-09-14 21:49:14 +02:00
|
|
|
-- @param dir The direction, can be either "up", "down", "left" or "right".
|
2016-02-26 19:56:07 +01:00
|
|
|
-- @param _screen Screen.
|
2012-09-14 21:49:14 +02:00
|
|
|
function screen.focus_bydirection(dir, _screen)
|
2016-02-26 19:56:07 +01:00
|
|
|
local sel = get_screen(_screen or screen.focused())
|
2012-09-14 21:49:14 +02:00
|
|
|
if sel then
|
|
|
|
local geomtbl = {}
|
|
|
|
for s = 1, capi.screen.count() do
|
|
|
|
geomtbl[s] = capi.screen[s].geometry
|
|
|
|
end
|
2016-02-26 19:56:07 +01:00
|
|
|
local target = util.get_rectangle_in_direction(dir, geomtbl, sel.geometry)
|
2012-09-14 21:49:14 +02:00
|
|
|
if target then
|
|
|
|
return screen.focus(target)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-10-08 03:52:03 +02:00
|
|
|
--- Give the focus to a screen, and move pointer to last known position on this
|
|
|
|
-- screen, or keep position relative to the current focused screen
|
2009-08-21 16:05:02 +02:00
|
|
|
-- @param i Value to add to the current focused screen index. 1 will focus next
|
|
|
|
-- screen, -1 would focus the previous one.
|
2012-06-12 20:13:09 +02:00
|
|
|
function screen.focus_relative(i)
|
2015-01-31 16:40:46 +01:00
|
|
|
return screen.focus(util.cycle(capi.screen.count(), screen.focused() + i))
|
2008-09-29 16:49:18 +02:00
|
|
|
end
|
|
|
|
|
2009-05-04 11:09:19 +02:00
|
|
|
--- Get or set the screen padding.
|
2012-11-19 14:09:10 +01:00
|
|
|
-- @param _screen The screen object to change the padding on
|
2009-05-04 11:09:19 +02:00
|
|
|
-- @param padding The padding, an table with 'top', 'left', 'right' and/or
|
|
|
|
-- 'bottom'. Can be nil if you only want to retrieve padding
|
2012-06-12 20:13:09 +02:00
|
|
|
function screen.padding(_screen, padding)
|
2016-02-26 19:56:07 +01:00
|
|
|
_screen = get_screen(_screen)
|
2009-05-04 11:09:19 +02:00
|
|
|
if padding then
|
2012-06-12 20:13:09 +02:00
|
|
|
data.padding[_screen] = padding
|
|
|
|
_screen:emit_signal("padding")
|
2009-05-04 11:09:19 +02:00
|
|
|
end
|
2012-06-12 20:13:09 +02:00
|
|
|
return data.padding[_screen]
|
2009-05-04 11:09:19 +02:00
|
|
|
end
|
|
|
|
|
2015-01-31 16:40:46 +01:00
|
|
|
--- Get the focused screen.
|
|
|
|
-- This can be replaced in a user's config.
|
|
|
|
-- @treturn integer
|
|
|
|
function screen.focused()
|
|
|
|
return capi.mouse.screen
|
|
|
|
end
|
|
|
|
|
2014-03-30 16:37:19 +02:00
|
|
|
capi.screen.add_signal("padding")
|
2010-08-25 23:00:36 +02:00
|
|
|
|
2012-06-12 20:13:09 +02:00
|
|
|
return screen
|
|
|
|
|
2011-09-11 16:50:01 +02:00
|
|
|
-- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80
|