2008-11-14 16:16:09 +01:00
|
|
|
---------------------------------------------------------------------------
|
2014-05-20 13:02:39 +02:00
|
|
|
--- Mouse module for awful
|
|
|
|
--
|
2008-11-14 16:16:09 +01:00
|
|
|
-- @author Julien Danjou <julien@danjou.info>
|
|
|
|
-- @copyright 2008 Julien Danjou
|
|
|
|
-- @release @AWESOME_VERSION@
|
2014-05-20 13:02:39 +02:00
|
|
|
-- @module awful.mouse
|
2008-11-14 16:16:09 +01:00
|
|
|
---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
-- Grab environment we need
|
|
|
|
local layout = require("awful.layout")
|
2008-11-17 17:12:54 +01:00
|
|
|
local tag = require("awful.tag")
|
2008-11-14 16:16:09 +01:00
|
|
|
local aclient = require("awful.client")
|
2008-12-08 19:56:00 +01:00
|
|
|
local widget = require("awful.widget")
|
2009-05-05 17:32:53 +02:00
|
|
|
local awibox = require("awful.wibox")
|
2008-12-14 14:38:48 +01:00
|
|
|
local util = require("awful.util")
|
2008-12-03 23:03:44 +01:00
|
|
|
local type = type
|
2008-11-14 16:16:09 +01:00
|
|
|
local math = math
|
|
|
|
local ipairs = ipairs
|
|
|
|
local capi =
|
|
|
|
{
|
2009-04-03 17:15:14 +02:00
|
|
|
root = root,
|
2008-11-14 16:16:09 +01:00
|
|
|
mouse = mouse,
|
|
|
|
screen = screen,
|
|
|
|
client = client,
|
|
|
|
mousegrabber = mousegrabber,
|
|
|
|
}
|
|
|
|
|
2012-06-14 01:33:27 +02:00
|
|
|
local finder = require("awful.mouse.finder")
|
2009-09-11 13:43:02 +02:00
|
|
|
|
2012-06-14 01:33:27 +02:00
|
|
|
local mouse = {}
|
2008-11-14 16:16:09 +01:00
|
|
|
|
2012-06-14 01:33:27 +02:00
|
|
|
mouse.client = {}
|
|
|
|
mouse.wibox = {}
|
2008-11-14 16:16:09 +01:00
|
|
|
|
2009-02-15 20:12:45 +01:00
|
|
|
--- Get the client object under the pointer.
|
|
|
|
-- @return The client object under the pointer, if one can be found.
|
2012-06-14 01:33:27 +02:00
|
|
|
function mouse.client_under_pointer()
|
2008-12-03 23:03:44 +01:00
|
|
|
local obj = capi.mouse.object_under_pointer()
|
|
|
|
if type(obj) == "client" then
|
|
|
|
return obj
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-03-17 22:18:48 +01:00
|
|
|
--- Get the drawin object under the pointer.
|
|
|
|
-- @return The drawin object under the pointer, if one can be found.
|
|
|
|
function mouse.drawin_under_pointer()
|
2008-12-03 23:03:44 +01:00
|
|
|
local obj = capi.mouse.object_under_pointer()
|
2013-03-17 22:18:48 +01:00
|
|
|
if type(obj) == "drawin" then
|
2008-12-03 23:03:44 +01:00
|
|
|
return obj
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-11-14 16:16:09 +01:00
|
|
|
local function snap_outside(g, sg, snap)
|
|
|
|
if g.x < snap + sg.x + sg.width and g.x > sg.x + sg.width then
|
|
|
|
g.x = sg.x + sg.width
|
|
|
|
elseif g.x + g.width < sg.x and g.x + g.width > sg.x - snap then
|
|
|
|
g.x = sg.x - g.width
|
|
|
|
end
|
|
|
|
if g.y < snap + sg.y + sg.height and g.y > sg.y + sg.height then
|
|
|
|
g.y = sg.y + sg.height
|
|
|
|
elseif g.y + g.height < sg.y and g.y + g.height > sg.y - snap then
|
|
|
|
g.y = sg.y - g.height
|
|
|
|
end
|
|
|
|
return g
|
|
|
|
end
|
|
|
|
|
|
|
|
local function snap_inside(g, sg, snap)
|
2009-02-10 22:55:34 +01:00
|
|
|
local edgev = 'none'
|
|
|
|
local edgeh = 'none'
|
2009-05-25 15:26:13 +02:00
|
|
|
if math.abs(g.x) < snap + sg.x and g.x > sg.x then
|
2009-02-10 22:55:34 +01:00
|
|
|
edgev = 'left'
|
2008-11-14 16:16:09 +01:00
|
|
|
g.x = sg.x
|
|
|
|
elseif math.abs((sg.x + sg.width) - (g.x + g.width)) < snap then
|
2009-02-10 22:55:34 +01:00
|
|
|
edgev = 'right'
|
2008-11-14 16:16:09 +01:00
|
|
|
g.x = sg.x + sg.width - g.width
|
|
|
|
end
|
2009-05-25 15:26:13 +02:00
|
|
|
if math.abs(g.y) < snap + sg.y and g.y > sg.y then
|
2009-02-10 22:55:34 +01:00
|
|
|
edgeh = 'top'
|
2008-11-14 16:16:09 +01:00
|
|
|
g.y = sg.y
|
|
|
|
elseif math.abs((sg.y + sg.height) - (g.y + g.height)) < snap then
|
2009-02-10 22:55:34 +01:00
|
|
|
edgeh = 'bottom'
|
2008-11-14 16:16:09 +01:00
|
|
|
g.y = sg.y + sg.height - g.height
|
|
|
|
end
|
2009-02-10 22:55:34 +01:00
|
|
|
|
|
|
|
-- What is the dominant dimension?
|
|
|
|
if g.width > g.height then
|
|
|
|
return g, edgeh
|
|
|
|
else
|
|
|
|
return g, edgev
|
|
|
|
end
|
2008-11-14 16:16:09 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
--- Snap a client to the closest client or screen edge.
|
|
|
|
-- @param c The client to snap.
|
|
|
|
-- @param snap The pixel to snap clients.
|
|
|
|
-- @param x The client x coordinate.
|
|
|
|
-- @param y The client y coordinate.
|
2008-12-10 22:05:22 +01:00
|
|
|
-- @param fixed_x True if the client isn't allowed to move in the x direction.
|
|
|
|
-- @param fixed_y True if the client isn't allowed to move in the y direction.
|
2012-06-14 01:33:27 +02:00
|
|
|
function mouse.client.snap(c, snap, x, y, fixed_x, fixed_y)
|
2008-11-14 16:16:09 +01:00
|
|
|
local snap = snap or 8
|
2012-06-14 01:33:27 +02:00
|
|
|
local c = c or mouse.client.focus
|
2008-12-12 00:01:43 +01:00
|
|
|
local cur_geom = c:geometry()
|
|
|
|
local geom = c:geometry()
|
2011-03-20 22:51:00 +01:00
|
|
|
geom.width = geom.width + (2 * c.border_width)
|
|
|
|
geom.height = geom.height + (2 * c.border_width)
|
2009-02-07 19:40:08 +01:00
|
|
|
local edge = "none"
|
|
|
|
local edge2 = "none"
|
2008-11-14 16:16:09 +01:00
|
|
|
geom.x = x or geom.x
|
|
|
|
geom.y = y or geom.y
|
|
|
|
|
2009-02-07 19:40:08 +01:00
|
|
|
geom, edge = snap_inside(geom, capi.screen[c.screen].geometry, snap)
|
2009-08-18 15:45:17 +02:00
|
|
|
geom = snap_inside(geom, capi.screen[c.screen].workarea, snap)
|
2009-02-07 19:40:08 +01:00
|
|
|
|
2009-03-26 22:13:45 +01:00
|
|
|
-- Allow certain windows to snap to the edge of the workarea.
|
|
|
|
-- Only allow docking to workarea for consistency/to avoid problems.
|
2009-03-26 22:22:05 +01:00
|
|
|
if aclient.dockable.get(c) then
|
2009-02-07 19:40:08 +01:00
|
|
|
local struts = c:struts()
|
|
|
|
struts['left'] = 0
|
|
|
|
struts['right'] = 0
|
|
|
|
struts['top'] = 0
|
|
|
|
struts['bottom'] = 0
|
2009-05-25 15:05:26 +02:00
|
|
|
if edge ~= "none" and aclient.floating.get(c) then
|
|
|
|
if edge == "left" or edge == "right" then
|
|
|
|
struts[edge] = cur_geom.width
|
|
|
|
elseif edge == "top" or edge == "bottom" then
|
|
|
|
struts[edge] = cur_geom.height
|
2009-02-07 19:40:08 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
c:struts(struts)
|
|
|
|
end
|
2008-11-14 16:16:09 +01:00
|
|
|
|
2011-03-28 11:21:17 +02:00
|
|
|
geom.x = geom.x - (2 * c.border_width)
|
|
|
|
geom.y = geom.y - (2 * c.border_width)
|
|
|
|
|
2008-11-14 16:16:09 +01:00
|
|
|
for k, snapper in ipairs(aclient.visible(c.screen)) do
|
|
|
|
if snapper ~= c then
|
2008-12-12 00:01:43 +01:00
|
|
|
geom = snap_outside(geom, snapper:geometry(), snap)
|
2008-11-14 16:16:09 +01:00
|
|
|
end
|
|
|
|
end
|
2008-12-10 22:05:22 +01:00
|
|
|
|
2011-03-20 22:51:00 +01:00
|
|
|
geom.width = geom.width - (2 * c.border_width)
|
|
|
|
geom.height = geom.height - (2 * c.border_width)
|
2011-03-28 11:21:17 +02:00
|
|
|
geom.x = geom.x + (2 * c.border_width)
|
|
|
|
geom.y = geom.y + (2 * c.border_width)
|
2011-03-20 22:51:00 +01:00
|
|
|
|
2011-07-01 17:19:46 +02:00
|
|
|
-- It's easiest to undo changes afterwards if they're not allowed
|
|
|
|
if fixed_x then geom.x = cur_geom.x end
|
|
|
|
if fixed_y then geom.y = cur_geom.y end
|
|
|
|
|
2008-11-14 16:16:09 +01:00
|
|
|
return geom
|
|
|
|
end
|
|
|
|
|
|
|
|
--- Move a client.
|
|
|
|
-- @param c The client to move, or the focused one if nil.
|
|
|
|
-- @param snap The pixel to snap clients.
|
2015-08-22 14:50:42 +02:00
|
|
|
-- @param finished_cb An optional callback function, that will be called
|
|
|
|
-- when moving the client has been finished. The client
|
|
|
|
-- that has been moved will be passed to that function.
|
|
|
|
function mouse.client.move(c, snap, finished_cb)
|
2008-11-14 16:16:09 +01:00
|
|
|
local c = c or capi.client.focus
|
|
|
|
|
2009-02-07 15:40:50 +01:00
|
|
|
if not c
|
|
|
|
or c.fullscreen
|
2008-11-14 16:16:09 +01:00
|
|
|
or c.type == "desktop"
|
|
|
|
or c.type == "splash"
|
|
|
|
or c.type == "dock" then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2008-12-12 00:01:43 +01:00
|
|
|
local orig = c:geometry()
|
2008-11-14 16:16:09 +01:00
|
|
|
local m_c = capi.mouse.coords()
|
|
|
|
local dist_x = m_c.x - orig.x
|
|
|
|
local dist_y = m_c.y - orig.y
|
2008-12-10 22:05:22 +01:00
|
|
|
-- Only allow moving in the non-maximized directions
|
|
|
|
local fixed_x = c.maximized_horizontal
|
|
|
|
local fixed_y = c.maximized_vertical
|
2008-11-14 16:16:09 +01:00
|
|
|
|
2012-06-14 01:33:27 +02:00
|
|
|
capi.mousegrabber.run(function (_mouse)
|
|
|
|
for k, v in ipairs(_mouse.buttons) do
|
2008-11-14 16:16:09 +01:00
|
|
|
if v then
|
|
|
|
local lay = layout.get(c.screen)
|
2008-12-01 16:26:41 +01:00
|
|
|
if lay == layout.suit.floating or aclient.floating.get(c) then
|
2012-06-14 01:33:27 +02:00
|
|
|
local x = _mouse.x - dist_x
|
|
|
|
local y = _mouse.y - dist_y
|
|
|
|
c:geometry(mouse.client.snap(c, snap, x, y, fixed_x, fixed_y))
|
2008-11-25 17:01:06 +01:00
|
|
|
elseif lay ~= layout.suit.magnifier then
|
2009-02-07 15:40:50 +01:00
|
|
|
-- Only move the client to the mouse
|
|
|
|
-- screen if the target screen is not
|
|
|
|
-- floating.
|
|
|
|
-- Otherwise, we move if via geometry.
|
|
|
|
if layout.get(capi.mouse.screen) == layout.suit.floating then
|
2012-06-14 01:33:27 +02:00
|
|
|
local x = _mouse.x - dist_x
|
|
|
|
local y = _mouse.y - dist_y
|
|
|
|
c:geometry(mouse.client.snap(c, snap, x, y, fixed_x, fixed_y))
|
2009-02-07 15:40:50 +01:00
|
|
|
else
|
|
|
|
c.screen = capi.mouse.screen
|
|
|
|
end
|
2008-11-25 17:01:06 +01:00
|
|
|
if layout.get(c.screen) ~= layout.suit.floating then
|
2012-06-14 01:33:27 +02:00
|
|
|
local c_u_m = mouse.client_under_pointer()
|
2008-12-01 16:26:41 +01:00
|
|
|
if c_u_m and not aclient.floating.get(c_u_m) then
|
2008-11-14 16:16:09 +01:00
|
|
|
if c_u_m ~= c then
|
|
|
|
c:swap(c_u_m)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
end
|
2015-08-22 14:50:42 +02:00
|
|
|
if finished_cb then
|
|
|
|
finished_cb(c)
|
|
|
|
end
|
2008-11-14 16:16:09 +01:00
|
|
|
return false
|
|
|
|
end, "fleur")
|
|
|
|
end
|
|
|
|
|
2012-06-14 01:33:27 +02:00
|
|
|
mouse.client.dragtotag = { }
|
2008-12-14 14:38:48 +01:00
|
|
|
|
|
|
|
--- Move a client to a tag by dragging it onto the left / right side of the screen
|
|
|
|
-- @param c The client to move
|
2012-06-14 01:33:27 +02:00
|
|
|
function mouse.client.dragtotag.border(c)
|
|
|
|
capi.mousegrabber.run(function (_mouse)
|
2008-12-14 14:38:48 +01:00
|
|
|
local button_down = false
|
2012-06-14 01:33:27 +02:00
|
|
|
for _, v in ipairs(_mouse.buttons) do
|
2008-12-14 14:38:48 +01:00
|
|
|
if v then button_down = true end
|
|
|
|
end
|
2009-08-18 15:45:17 +02:00
|
|
|
local wa = capi.screen[c.screen].workarea
|
2012-06-14 01:33:27 +02:00
|
|
|
if _mouse.x >= wa.x + wa.width then
|
2008-12-14 14:38:48 +01:00
|
|
|
capi.mouse.coords({ x = wa.x + wa.width - 1 })
|
2012-06-14 01:33:27 +02:00
|
|
|
elseif _mouse.x <= wa.x then
|
2008-12-14 14:38:48 +01:00
|
|
|
capi.mouse.coords({ x = wa.x + 1 })
|
|
|
|
end
|
|
|
|
if not button_down then
|
2012-10-20 16:34:06 +02:00
|
|
|
local tags = tag.gettags(c.screen)
|
2008-12-14 14:38:48 +01:00
|
|
|
local t = tag.selected()
|
|
|
|
local idx
|
|
|
|
for i, v in ipairs(tags) do
|
|
|
|
if v == t then
|
|
|
|
idx = i
|
|
|
|
end
|
|
|
|
end
|
2012-06-14 01:33:27 +02:00
|
|
|
if _mouse.x > wa.x + wa.width - 10 then
|
2008-12-14 14:38:48 +01:00
|
|
|
local newtag = tags[util.cycle(#tags, idx + 1)]
|
|
|
|
aclient.movetotag(newtag, c)
|
|
|
|
tag.viewnext()
|
2012-06-14 01:33:27 +02:00
|
|
|
elseif _mouse.x < wa.x + 10 then
|
2008-12-14 14:38:48 +01:00
|
|
|
local newtag = tags[util.cycle(#tags, idx - 1)]
|
|
|
|
aclient.movetotag(newtag, c)
|
|
|
|
tag.viewprev()
|
|
|
|
end
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
return true
|
|
|
|
end, "fleur")
|
|
|
|
end
|
|
|
|
|
2008-12-10 19:57:58 +01:00
|
|
|
--- Move the wibox under the cursor
|
|
|
|
--@param w The wibox to move, or none to use that under the pointer
|
2012-06-14 01:33:27 +02:00
|
|
|
function mouse.wibox.move(w)
|
|
|
|
local w = w or mouse.wibox_under_pointer()
|
2008-12-10 19:57:58 +01:00
|
|
|
if not w then return end
|
|
|
|
|
|
|
|
local offset = {
|
2009-08-17 16:56:03 +02:00
|
|
|
x = w.x - capi.mouse.coords().x,
|
|
|
|
y = w.y - capi.mouse.coords().y
|
2008-12-10 19:57:58 +01:00
|
|
|
}
|
|
|
|
|
2012-06-14 01:33:27 +02:00
|
|
|
capi.mousegrabber.run(function (_mouse)
|
2008-12-10 19:57:58 +01:00
|
|
|
local button_down = false
|
2009-06-05 19:51:33 +02:00
|
|
|
if awibox.get_position(w) == "floating" then
|
2009-08-17 16:56:03 +02:00
|
|
|
w.x = capi.mouse.coords().x + offset.x
|
|
|
|
w.y = capi.mouse.coords().y + offset.y
|
2008-12-10 19:57:58 +01:00
|
|
|
else
|
2009-08-18 15:45:17 +02:00
|
|
|
local wa = capi.screen[capi.mouse.screen].workarea
|
2008-12-10 19:57:58 +01:00
|
|
|
|
|
|
|
if capi.mouse.coords()["y"] > wa.y + wa.height - 10 then
|
2009-06-05 19:51:33 +02:00
|
|
|
awibox.set_position(w, "bottom", w.screen)
|
2008-12-10 19:57:58 +01:00
|
|
|
elseif capi.mouse.coords()["y"] < wa.y + 10 then
|
2009-06-05 19:51:33 +02:00
|
|
|
awibox.set_position(w, "top", w.screen)
|
2008-12-10 19:57:58 +01:00
|
|
|
elseif capi.mouse.coords()["x"] > wa.x + wa.width - 10 then
|
2009-06-05 19:51:33 +02:00
|
|
|
awibox.set_position(w, "right", w.screen)
|
2008-12-10 19:57:58 +01:00
|
|
|
elseif capi.mouse.coords()["x"] < wa.x + 10 then
|
2009-06-05 19:51:33 +02:00
|
|
|
awibox.set_position(w, "left", w.screen)
|
2008-12-10 19:57:58 +01:00
|
|
|
end
|
|
|
|
w.screen = capi.mouse.screen
|
|
|
|
end
|
2012-06-14 01:33:27 +02:00
|
|
|
for k, v in ipairs(_mouse.buttons) do
|
2008-12-10 19:57:58 +01:00
|
|
|
if v then button_down = true end
|
|
|
|
end
|
|
|
|
if not button_down then
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
return true
|
|
|
|
end, "fleur")
|
|
|
|
end
|
|
|
|
|
2008-11-17 17:12:54 +01:00
|
|
|
--- Get a client corner coordinates.
|
|
|
|
-- @param c The client to get corner from, focused one by default.
|
|
|
|
-- @param corner The corner to use: auto, top_left, top_right, bottom_left,
|
|
|
|
-- bottom_right. Default is auto, and auto find the nearest corner.
|
|
|
|
-- @return Actual used corner and x and y coordinates.
|
2012-06-14 01:33:27 +02:00
|
|
|
function mouse.client.corner(c, corner)
|
2008-11-17 17:12:54 +01:00
|
|
|
local c = c or capi.client.focus
|
|
|
|
if not c then return end
|
|
|
|
|
|
|
|
local g = c:geometry()
|
|
|
|
|
|
|
|
if not corner or corner == "auto" then
|
|
|
|
local m_c = capi.mouse.coords()
|
|
|
|
if math.abs(g.y - m_c.y) < math.abs(g.y + g.height - m_c.y) then
|
|
|
|
if math.abs(g.x - m_c.x) < math.abs(g.x + g.width - m_c.x) then
|
|
|
|
corner = "top_left"
|
|
|
|
else
|
|
|
|
corner = "top_right"
|
|
|
|
end
|
|
|
|
else
|
|
|
|
if math.abs(g.x - m_c.x) < math.abs(g.x + g.width - m_c.x) then
|
|
|
|
corner = "bottom_left"
|
|
|
|
else
|
|
|
|
corner = "bottom_right"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
local x, y
|
|
|
|
if corner == "top_right" then
|
|
|
|
x = g.x + g.width
|
|
|
|
y = g.y
|
|
|
|
elseif corner == "top_left" then
|
|
|
|
x = g.x
|
|
|
|
y = g.y
|
|
|
|
elseif corner == "bottom_left" then
|
|
|
|
x = g.x
|
|
|
|
y = g.y + g.height
|
|
|
|
else
|
|
|
|
x = g.x + g.width
|
|
|
|
y = g.y + g.height
|
|
|
|
end
|
|
|
|
|
|
|
|
return corner, x, y
|
|
|
|
end
|
|
|
|
|
|
|
|
--- Resize a client.
|
|
|
|
-- @param c The client to resize, or the focused one by default.
|
|
|
|
-- @param corner The corner to grab on resize. Auto detected by default.
|
2012-06-14 01:33:27 +02:00
|
|
|
function mouse.client.resize(c, corner)
|
2008-11-17 17:12:54 +01:00
|
|
|
local c = c or capi.client.focus
|
|
|
|
|
|
|
|
if not c then return end
|
|
|
|
|
|
|
|
if c.fullscreen
|
|
|
|
or c.type == "desktop"
|
|
|
|
or c.type == "splash"
|
|
|
|
or c.type == "dock" then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
local lay = layout.get(c.screen)
|
2014-10-23 01:30:19 +02:00
|
|
|
local corner, x, y = mouse.client.corner(c, corner)
|
2008-11-17 17:12:54 +01:00
|
|
|
|
2008-12-01 16:26:41 +01:00
|
|
|
if lay == layout.suit.floating or aclient.floating.get(c) then
|
2014-10-23 01:30:19 +02:00
|
|
|
return layout.suit.floating.mouse_resize_handler(c, corner, x, y)
|
|
|
|
elseif lay.mouse_resize_handler then
|
|
|
|
return lay.mouse_resize_handler(c, corner, x, y)
|
2008-11-17 17:12:54 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-04-03 17:15:14 +02:00
|
|
|
-- Set the cursor at startup
|
|
|
|
capi.root.cursor("left_ptr")
|
|
|
|
|
2012-06-14 01:33:27 +02:00
|
|
|
return mouse
|
|
|
|
|
2011-09-11 16:50:01 +02:00
|
|
|
-- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80
|