Skip to content
This repository
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 372 lines (333 sloc) 11.014 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371
-- I want to be your savery

local restore = { mt = {} }

local io = require('io')
local awful = require('awful')
local wibox = require('wibox')
local beautiful = require("beautiful")
local capi = {
    awesome = awesome,
    screen = screen,
    client = client,
}

local layouts = {}

--- example usage
-- myrestorelist = uzful.restore(layouts)
-- if myrestorelist[s].length > 0 then
-- myrestorelist[s].widget = uzful.widget.infobox({ screen = s,
-- size = function () return myrestorelist[s].fit() end,
-- position = "top", align = "left",
-- visible = true, ontop = true,
-- widget = myrestorelist[s].layout })
-- myrestorelist[s].layout:connect_signal("widget::updated", function ()
-- if myrestorelist[s].length == 0 then
-- myrestorelist[s].widget:hide()
-- myrestorelist[s].widget.screen = nil
-- else
-- myrestorelist[s].widget:update()
-- end
-- end)
-- end


local table2string
local function lualine(value, key, indent)
    local res = indent
    if key ~= nil then
        res = res .. '["' .. key .. '"] = '
    end
    if type(value) == "table" then
        res = res .. table2string(value, indent)
    elseif type(value) == "string" then
local s, _ = string.gsub(value, "\n", "")
        res = res .. '"' .. s .. '"'
    elseif type(value) == "boolean" then
        res = res .. (value and "true" or "false")
    else
        res = res .. value
    end
    res = res .. ",\n"
    return res
end

table2string = function (t, indent)
    local res = "{\n"
    local moarindent = indent .. "\t"
    for key, value in pairs(t) do
        res = res .. lualine(value,
            type(key) ~= "number" and key or nil,
            moarindent)
    end
    return res .. indent .. "}"
end

local function update_tag(cmd, tag, data)
    if data == nil then
        data = {}
    end
    for _,prop in ipairs({"layout","mwfact","ncol","nmaster","windowfact"}) do
        if cmd == 'get' or data[prop] == nil then
            data[prop] = awful.tag.getproperty(tag, prop)
            if prop == 'layout' then
                data.layout = data.layout.name
            end
        elseif cmd == 'set' then
            if prop == 'layout' then
                awful.layout.set(layouts[data.layout], tag)
            else
                awful.tag.setproperty(tag, prop, data[prop])
            end
        end
    end
    return data
end


local function update_window(cmd, client, data)
    if data == nil then
        data = {}
    end
    if cmd == 'set' and data.geometry ~= nil then
        client:geometry(data.geometry)
    elseif cmd == 'get' then
        data.geometry = client:geometry()
    end
    for _,prop in ipairs({"floating","floating_geometry","sticky","ontop","minimized","maximized","hidden","fullscreen","modal","maximized_horizontal","maximized_vertical","skip_taskbar"}) do
        if cmd == 'get' or data[prop] == nil then
            data[prop] = awful.client.property.get(client, prop)
        elseif cmd == 'set' then
            if prop ~= "floating_geometry" then
                awful.client.property.set(client, prop, data[prop])
            end
        end
    end
    for _,prop in ipairs({"instance","pid","screen"}) do
        if cmd == 'get' or data[prop] == nil then
            data[prop] = client[prop]
        elseif cmd == 'set' then
            if prop == "screen" then
                if data.screen > capi.screen.count() then
                    data.screen = capi.screen.count()
                end
                client[prop] = data[prop]
            end
        end
    end
    if cmd == 'set' and data.floating_geometry ~= nil then
        awful.client.floating.set(client, client.floating)
    end
    return data
end


local function get_command(pid)
    if pid == 0 then
        return ""
    else
        return awful.util.pread("ps --no-headers o args " .. pid)
    end
end


-- load savepoint data and prepare it as always same looking data structure
local function load(filename)
    print "* load savepoint"
    local data = {}
    local f = io.open(awful.util.getdir("config") .. "/" .. filename .. ".lua") -- FIXME
    if f ~= nil then
        f:close()
        data = require(filename)
    end

    return data
end

local function get_tag_numbers(tags)
    local ret = {}
    -- get tags numbers
    for _, tag in ipairs(tags) do
        table.insert(ret, awful.tag.getidx(tag))
    end
    return ret
end


function restore.disconnect()
    local filename = "_savepoint"
    local data = load(filename)
    for s = 1, capi.screen.count() do
        local screendata = data[s] or {}
        data[s] = screendata
        screendata.tags = get_tag_numbers(awful.tag.selectedlist(s))
        for t,tag in ipairs(awful.tag.gettags(s)) do
            screendata[t] = update_tag('get', tag, screendata[t])
        end
    end
    data.windows = {} -- always override last windows history
    for _, client in ipairs(capi.client.get(--[[all]])) do
        if client.pid ~= 0 then
            local window = update_window('get', client, {
                tags = get_tag_numbers(client:tags()),
                command = get_command(client.pid),
                id = client.window,
            })
            -- save
            table.insert(data.windows, window)
        end
    end


    print "* write savepoint"
    local f = io.open(awful.util.getdir("config") .. "/" .. filename .. ".lua", "w+")
    f:write("return " .. table2string(data, ""))
    f:close()

end


local function create_control(text, callback)
    local theme = beautiful.get()
    local ret = wibox.widget.textbox()
    ret:set_align("center")
    ret:set_text(text)
    local bg = wibox.widget.background()
    bg:set_widget(ret)
    bg:set_fg(theme.fg_normal)
    bg:set_bg(theme.bg_normal)
    ret:connect_signal("mouse::enter", function ()
        bg:set_fg(theme.fg_focus)
        bg:set_bg(theme.bg_focus)
    end)
    ret:connect_signal("mouse::leave", function ()
        bg:set_fg(theme.fg_normal)
        bg:set_bg(theme.bg_normal)
    end)
    ret:buttons(awful.button({ }, 1, callback))
    return bg
end

local function format_window_info(win)
    return ""..win.pid.. string.rep(" ",7-string.len(""..win.pid))..win.command
end

local function create_screen_info(screen)
    local ret
    ret = {
        layout = wibox.layout.flex.vertical(),
        controls = wibox.layout.flex.horizontal(),
        windows = setmetatable({}, {__mode = 'v'}),
        length = 0,
        fit = function ()
            return 242, ((ret.length + 1) * 12)
        end,
        rebuild = function ()
            local l = ret.layout
            l:reset()
            l:add(ret.controls)
            -- TODO show only from current tags
            for id,win in pairs(ret.windows) do
                if not win._removed then
                    l:add(win.text)
                end
            end
        end,
    }

    local ignorecontrol = create_control("ignore", function ()
        ret.length = 0
        for _, win in pairs(ret.windows) do
            win.remove()
        end
        ret.layout:reset()
    end)
    ret.controls:add(ignorecontrol)
    ret.controls:add(create_control("spawn", function ()
        -- remove spawncontrol to disable retry
        ret.controls:reset()
        ret.controls:add(ignorecontrol)
        -- spawn new process here and update pid at every place
        for _, win in pairs(ret.windows) do
            local spawn = awful.util.spawn(win.command, true, win.screen)
            print("spawn",win.command,win.screen,win.pid,"-> "..spawn)
            if type(spawn) == "number" then
                win.pid = spawn
                win.text:set_text(format_window_info(win))
            else
                -- fails
                ret.length = ret.length - 1
                win.text:set_text(spawn or "broken" .. " " .. win.command)
            end
        end
    end))

    ret.layout:add(ret.controls)
    return ret
end

local function create_window(data, win)
    local count = capi.screen.count()
    win = win or {}
    if win.screen > count then
        win.screen = count
    end
    win.command = win.command or ""
    win.text = wibox.widget.textbox()
    win.text:set_text(format_window_info(win))
    win.remove = function ()
        data.ids[win.id] = nil
        win._removed = true
    end
    return win
end

local function get_tags(tags, screen)
    -- get all tag userdatas
    local ret = {}
    local capitags = awful.tag.gettags(screen or 1)
    for _, t in ipairs(tags or {}) do
        table.insert(ret, capitags[t])
    end
    return ret
end

function restore.connect(Layouts)
    for _, layout in ipairs(Layouts) do
        layouts[awful.layout.getname(layout)] = layout
    end

    local ret = {}
    local data = load("_savepoint")
    data.windows = data.windows or {}
    ret.ids = {}

    -- make sure, that we have at least a screen and tag structure
    for s = 1, capi.screen.count() do
        local screendata = data[s]
        if screendata == nil then
            screendata = {}
            data[s] = screendata
        end
        for t,tag in ipairs(awful.tag.gettags(s)) do
            screendata[t] = update_tag('set', tag, screendata[t])
        end
        if screendata.tags and #screendata.tags then
            awful.tag.viewmore(get_tags(screendata.tags, s), s)
        end
        ret[s] = create_screen_info(s)
    end

    local randi = 0
    for _, window in ipairs(data.windows) do
        window = create_window(ret, window)
        if window.id == nil then
            randi = randi - 1
            window.id = randi
        end
        local win = ret[window.screen]
        win.layout:add(window.text)
        win.length = win.length + 1
        win.windows[window.id] = window
        ret.ids[window.id] = window -- kill switch
    end

    capi.awesome.connect_signal("exit", restore.disconnect)
    capi.client.connect_signal("manage", function (client)

        local window = ret.ids[client.window] -- matches after restart
        if window == nil then
            -- TODO better checks
            local cmd = get_command(client.pid)
            for id, win in pairs(ret.ids) do
                if win.command == cmd then
                    win.pid = client.pid
                    window = win
                    break
                end
            end
        end
        if window ~= nil then
            window = update_window('set', client, window)
            if window.tags and #window.tags then
                client:tags(get_tags(window.tags, client.screen))
            end
            local win = ret[client.screen]
            win.length = win.length - 1
            window.remove()
            win.rebuild()
        end
    end)

    data.windows = nil -- should be everything in ret.ids by now
    return ret
end

function restore.mt:__call(...)
    return restore.connect(...)
end

return setmetatable(restore, restore.mt)
Something went wrong with that request. Please try again.