--   This file is part of Reno desktop.
--
--  Reno desktop is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
--
--  Reno desktop is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
--
--  You should have received a copy of the GNU General Public License along with Reno desktop. If not, see <https://www.gnu.org/licenses/>. 
-- Base upon which modules are layered.
--libraries
local awful = require("awful")
local beautiful = require("beautiful")
local gears = require("gears")

awful.util.shell = global.shell
--error handling
if awesome.startup_errors then
    print("Error during startup: "..awesome.startup_errors)
end
do
    local in_error = false
    awesome.connect_signal("debug::error",function(err)
        if in_error then return end
        in_error = true
        print("Error during runtime: "..tostring(err))
        in_error = false
    end)
end

--screen setup
awful.screen.connect_for_each_screen(function(s)
    gears.wallpaper.maximized(root_path.."/background.jpg")
    awful.tag({ "1" }, s, awful.layout.suit.floating)
end)

--theme initialization
beautiful.init(root_path.."/themes/"..global.theme.."/theme.lua")

--client mouse bindings
clientbuttons = gears.table.join(
    awful.button({}, 1, function(c)
        c:emit_signal("request::activate", "mouse_click", {raise = true})
    end)
)

--client rules
awful.rules.rules = {
    { --catch-all rules
        rule = {},
        properties = { 
            focus = awful.client.focus.filter,
            raise = true,
            screen = awful.screen.preferred,
            border_width = beautiful.border_width,
            border_color = beautiful.border_normal,
            placement = awful.placement.no_overlap+awful.placement.no_offscreen
        }
    }
}

--client signals
client.connect_signal("manage", function(c)
    -- append the window at the end rather than at the top.
    -- if not awesome.startup then awful.client.setslave(c) end
    if awesome.startup
        --place client on available screens when screen count changes
        and not c.size_hints.user_position
        and not c.size_hints.program_position then
        awful.placement.no_offscreen(c)
    end
end)

client.connect_signal("focus", function(c)
    c.border_color = beautiful.border_focus
end)

client.connect_signal("unfocus", function(c)
    c.border_color = beautiful.border_normal
end)
--screen setup
awful.screen.connect_for_each_screen(function(s)
    awful.tag({ "1", "2", "3", "4" }, s, awful.layout.suit.floating)
end)
