---------------------------------------------------------------------------
-- @author Julien Danjou <julien@danjou.info>
-- @copyright 2008 Julien Danjou
-- @release @AWESOME_VERSION@
---------------------------------------------------------------------------

-- Grab environment we need
local capi =
{
    mouse = mouse,
    screen = screen,
    client = client
}
local util = require("awful.util")
local client = require("awful.client")

--- Screen module for awful
-- awful.screen
local screen = {}

local data = {}
data.padding = {}

---
-- Return Xinerama screen number corresponding to the given (pixel) coordinates.
-- 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
function screen.getbycoord(x, y)
    for i = 1, capi.screen:count() do
        local geometry = capi.screen[i].geometry
        if((x < 0 or (x >= geometry.x and x < geometry.x + geometry.width))
           and (y < 0 or (y >= geometry.y and y < geometry.y + geometry.height))) then
            return i;
        end
    end
end

--- Give the focus to a screen, and move pointer.
-- @param screen Screen number.
function screen.focus(_screen)
    if _screen > capi.screen.count() then _screen = capi.mouse.screen end
    local c = client.focus.history.get(_screen, 0)
    if c then capi.client.focus = c end
    -- Move the mouse on the screen
    capi.mouse.screen = _screen
end

--- Give the focus to a screen, and move pointer, but relative to the current
-- focused screen.
-- @param i Value to add to the current focused screen index. 1 will focus next
-- screen, -1 would focus the previous one.
function screen.focus_relative(i)
    return screen.focus(util.cycle(capi.screen.count(), capi.mouse.screen + i))
end

--- Get or set the screen padding.
-- @param screen The screen object to change the padding on
-- @param padding The padding, an table with 'top', 'left', 'right' and/or
-- 'bottom'. Can be nil if you only want to retrieve padding
function screen.padding(_screen, padding)
    if padding then
        data.padding[_screen] = padding
        _screen:emit_signal("padding")
    end
    return data.padding[_screen]
end

for s = 1, capi.screen.count() do
    capi.screen[s]:add_signal("padding")
end

return screen

-- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80