summaryrefslogtreecommitdiff
path: root/utils.lua
blob: 0a8d54f1f8df73fb3f935cb828413cb662562ad8 (plain)
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
local M = {}

local awful = require("awful")
local naughty = require("naughty")

function M.tag_desk_idx(t)
    return tonumber('0x' .. t.name:sub(1, 2))
end
function M.tag_page_idx(t)
    return tonumber('0x' .. t.name:sub(3, 4))
end

-- mapping from logical screen indices to indices corresponding to their
-- physical layout
function M.screen_physical(n)
    local function screen_cmp(s1, s2)
        return s1.geometry.x < s2.geometry.x
    end

    local screens = {}
    for s in screen do
        screens[s.index] = s
    end

    table.sort(screens, screen_cmp)

    if screens[n] ~= nil then
        return screens[n]
    end
    return nil
end

function M.screen_focus_physical(n)
    local s = M.screen_physical(n)
    if s then
        awful.screen.focus(s)
    end
end

-- audio control functions
function M.vol_control(wsp, n)
    local cmd = string.format("amixer set Master %d%%", math.abs(n)) .. (n < 0 and "-" or "+")
    awful.spawn.easy_async(cmd,
        function(stdout, stderr, exitreason, exitcode)
            if exitcode ~= 0 then
                naughty.notify({ preset = naughty.config.presets.warn,
                                 title  = "Error changing the audio volume",
                                 text   = stderr })
                return
            end

            local volume = string.match(stdout, '%[(%d+%%)%]')
            local muted  = string.match(stdout, '%[off%]') and ' (Muted)' or ''
            M.notify_singleton(wsp, 'volume',
                               { title  = "Audio volume", text = volume .. muted})
        end)
end

function M.vol_mute_toggle(wsp)
    local control = 'Master'
    awful.spawn.easy_async("amixer sset " .. control .. " toggle",
        function(stdout, stderr, exitreason, exitcode)
            if exitcode ~= 0 then
                naughty.notify({ preset = naughty.config.presets.warn,
                                 title  = "Error toggling the " .. control .. " control",
                                 text   = stderr })
                return
            end

            local text = string.match(stdout, '%[on%]') and 'Unmuted' or 'Muted'
            M.notify_singleton(wsp, 'mute',
                               { title  = "Audio", text = text })
        end)
end

function M.auto_mute_toggle(wsp)
    local control = "'Auto-Mute Mode'"
    awful.spawn.easy_async("amixer sget " .. control,
        function(stdout, stderr, exitreason, exitcode)
            if exitcode ~= 0 then
                naughty.notify({ preset = naughty.config.presets.warn,
                                 title  = "Error getting the auto-mute mode",
                                 text   = stderr })
                return
            end

            -- parse valid auto-mute items
            local items_raw = string.match(stdout, "\n%s*Items:%s*([^\n]+)\n")
            local items = {}
            for item in string.gmatch(items_raw, "'[^']+'") do
                table.insert(items, item)
            end

            -- parse current item
            local cur_item = string.match(stdout, "\n%s*Item0:%s*('[^'\n]+')\n")

            -- find the next item
            local next_item = nil
            for i, v in ipairs(items) do
                if v == cur_item then
                    next_item = items[(i % #items) + 1]
                    break
                end
            end
            if next_item == nil then return end

            -- set the next item
            awful.spawn.easy_async('amixer -q sset ' .. control .. ' ' .. next_item,
                function(stdout, stderr, exitreason, exitcode)
                    if exitcode ~= 0 then
                        naughty.notify({ preset = naughty.config.presets.warn,
                                         title  = "Error setting the auto-mute mode",
                                         text   = stderr })
                        return
                    end

                    M.notify_singleton(wsp, 'auto_mute',
                                       { title  = "Auto-mute", text   = next_item })
                end
            )
        end)
end

function M.spawn_current(command)
    awful.spawn(command, {tag = mouse.screen.selected_tag})
end

function M.screen_lock()
    awful.spawn.easy_async("xscreensaver-command -lock",
        function(stdout, stderr, exitreason, exitcode)
            if exitcode ~= 0 then
                naughty.notify({ preset = naughty.config.presets.critical,
                                 title = "Error locking the screen",
                                 text  = table.concat({stdout, stderr}, "\n") })

            end
        end)

end

function M.notify_singleton(wsp, id, args)
    local prev_notify = wsp.notify_tbl[id]
    if prev_notify and prev_notify.box.visible then
        args.replaces_id = prev_notify.id
    end
    wsp.notify_tbl[id] = naughty.notify(args)
end

function M.log(component, msg, ...)
    local prefix = string.format('[awesome/%s] ', component)
    local str    = string.format(msg, ...)

    print(prefix .. str)

    return prefix, str
end

function M.warn(component, msg, ...)
    local prefix, str = M.log(component, msg, ...)
    naughty.notify({ preset = naughty.config.presets.critical,
                     title = prefix,
                     text  = str })
end

return M