-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinit.lua
316 lines (273 loc) · 9.86 KB
/
init.lua
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
----------------------------------------------------------------
--- Jammin'! A dbus-based media widget for awesome
--
-- @author Krampus <tetramor.ph>
-- @module jammin
----------------------------------------------------------------
local naughty = require("naughty")
local res, nifty = pcall(require, "nifty")
if not res then
local err_msg = "If you want to be jammin' you gotta get nifty!\n" ..
"https://github.com/thekrampus/awesome-nifty"
print("Error from jammin/init.lua: " .. err_msg)
naughty.notify{preset=naughty.config.presets.critical,
title="Jammin' error!",
text=err_msg}
return {}
end
local awful = require("awful")
local wibox = require("wibox")
local beautiful = require("beautiful")
local shape = require("gears.shape")
local jdbus = require("jammin.dbus")
local jammin = {
master_volume_widget = nil
}
jammin.__index = jammin
local function show_master_volume_widget()
if jammin.master_volume_widget ~= nil then
jammin.master_volume_widget:show{coords={x=10, y=20}}
end
end
--- Media controls
function jammin.playpause(player)
jdbus.send({cmd = "PlayPause", player = player})
end
function jammin.next(player)
jdbus.send({cmd = "Next", player = player})
end
function jammin.previous(player)
jdbus.send({cmd = "Previous", player = player})
end
function jammin.vol_set(n)
awful.spawn("amixer -q -M set Master " .. n .. "%")
end
function jammin.vol_up()
awful.spawn("amixer -q -M set Master 5%+")
-- show_master_volume_widget()
end
function jammin.vol_down()
awful.spawn("amixer -q -M set Master 5%-")
-- show_master_volume_widget()
end
function jammin.mute()
awful.spawn("amixer -q -M set Master playback toggle")
-- show_master_volume_widget()
end
-- Factory for a default async volume polling function
local function default_async_volume_factory(slider)
local function slider_refresh_callback(out, err, _, status)
if status ~= 0 then
print("\nError " .. status .. " from jammin' volume polling:")
print(err)
return
end
local vol_pct = out:match("Mono.+%[(%d+)%%%]")
if vol_pct then
slider.value = tonumber(vol_pct)
end
end
return function()
awful.spawn.easy_async("amixer -M get Master", slider_refresh_callback)
end
end
--- Construct a volumebar widget for this jammin' instance
function jammin.volumebar(args)
args = args or {}
local widget = args.widget
local theme = {
width = args.width or 20,
height = args.height or 220,
border_color = args.border_color or beautiful.menu_border_color,
border_width = args.border_width or beautiful.menu_border_width,
bg_normal = args.background_color or beautiful.menu_bg_normal
}
local slider = wibox.widget {
bar_shape = args.bar_shape or shape.rounded_bar,
bar_height = args.bar_height or 2,
bar_color = args.bar_color or beautiful.fg_focus,
handle_color = args.handle_color or "[0]#000000",
handle_shape = args.handle_shape or function(cr, w, h)
return shape.transform(shape.partially_rounded_rect)
: scale(0.9, 0.9) (cr, h, h, true, false, true, true, theme.width)
end,
handle_border_color = args.handle_border_color or beautiful.fg_focus,
handle_border_width = args.handle_border_width or 2,
handle_width = args.handle_width or theme.width,
handle_margins = args.handle_margins or {left=1, top=2},
bar_margins = args.bar_margins or {left=7, right=10, top=theme.width/2 - 1},
value = 100,
widget = wibox.widget.slider
}
local slider_handler = args.slider_handler or jammin.vol_set
slider:connect_signal("widget::redraw_needed",
function() slider_handler(slider.value) end)
local async_volume_poll_fn = (args.async_volume_factory or default_async_volume_factory)(slider)
widget:connect_signal("mouse::enter", async_volume_poll_fn)
local w = wibox.container {
wibox.container {
slider,
width = theme.height,
strategy = 'max',
widget = wibox.container.constraint
},
direction = 'east',
widget = wibox.container.rotate
}
return nifty.popup_widget(
w,
{
theme = theme,
timeout = args.popup_timeout or 3
}
)
end
function jammin:async_update(player)
jdbus.poll_async(player, function(p) self:on_propchange(p, nil) end)
end
--- (List | atom) -> (List)
local function normalize_list(value)
if type(value) == 'table' then
return value
elseif value ~= nil then
return {value}
end
end
--- (ISO 8601 datetime) -> ({year, month, day})
local function normalize_date(value)
local y, m, d = (value or ""):match("^(%d*)-(%d*)-(%d*)T")
return {year = y, month = m, day = d}
end
--- Handler function for Metadata change signals
-- Updates the musicbox to reflect the new track
function jammin:handle_trackchange(metadata)
local nfields = 0
for _ in pairs(metadata) do
nfields = nfields + 1
end
if nfields == 0 then
-- Empty metadata indicates that the music player has been closed
self:playback_handler("Closed")
else
-- Normalize the track data
local track = {
length_us = metadata["mpris:length"],
art_url = metadata["mpris:artUrl"],
album = metadata["xesam:album"],
album_artists = normalize_list(metadata["xesam:albumArtist"]),
artists = normalize_list(metadata["xesam:artist"]),
as_text = metadata["xesam:asText"],
bpm = metadata["xesam:audioBPM"],
auto_rating = metadata["xesam:autoRating"],
comments = normalize_list(metadata["xesam:comment"]),
created = normalize_date(metadata["xesam:contentCreated"]),
disc_number = metadata["xesam:discNumber"],
first_played = normalize_date(metadata["xesam:firstUsed"]),
genres = normalize_list(metadata["xesam:genre"]),
last_played = normalize_date(metadata["xesam:lastUsed"]),
lyricists = normalize_list(metadata["xesam:lyricist"]),
title = metadata["xesam:title"],
track_number = metadata["xesam:trackNumber"],
url = metadata["xesam:url"],
play_count = metadata["xesam:useCount"],
user_rating = metadata["xesam:userRating"]
}
self:track_handler(track)
end
end
-- XXX should this be removed?
-- --- Add a handler for DBus notifications through naughty for a given appname.
-- -- The default handler function assumes the notification title and text are the
-- -- track title and album respectively. The caller can override this by passing
-- -- their own handler function, which is set as the notification callback.
-- function jammin:add_notify_handler(appname, handler)
-- handler = handler or
-- function(_, _, _, icon, title, text, _, hints, _)
-- local i
-- if icon ~= "" then
-- i = icon
-- elseif hints.icon_data or hints.image_data then
-- -- TODO
-- end
-- self:on_notify(title, nil, text, i)
-- return false
-- end
-- local preset = naughty.config.presets[appname] or {}
-- preset.callback = handler
-- table.insert(naughty.dbus.config.mapping, {{appname=appname}, preset})
-- end
-- --- Handler for notification data. This should be called by notify handlers.
-- function jammin:on_notify(title, artist, album, icon)
-- if not self.track then
-- self.track = {}
-- end
-- self.track.title = self.track.title or title
-- self.track.artist = self.track.artist or artist
-- self.track.icon = self.track.icon or icon
-- self:refresh()
-- end
-- local pretty = require("pl.pretty")
--- Handler for PropertyChanged signals on org.freedesktop.DBus.Properties
function jammin:on_propchange(changed, invalidated)
-- print("DEBUG: jammin:on_propchange")
-- print("changed:")
-- pretty.dump(changed)
-- print("invalidated:")
-- pretty.dump(invalidated)
-- print("\n")
if changed.PlaybackStatus then
-- Track play/pause/stop signal
self:playback_handler(changed.PlaybackStatus)
end
if changed.Metadata then
-- Track change signal
self:handle_trackchange(changed.Metadata)
end
end
--- Factory for a default handler for playback status changes, if none is specified
local function default_playback_handler(self, status)
-- Just do nothing by default
end
--- A default handler for track changes, if none is specified
local function default_track_handler(self, data)
local artist = table.concat(data.artists, ', ')
self.widget:set_markup(
string.format("%s - %s",
data.title,
artist
)
)
self.tooltip:set_markup(
string.format("%s - %s\n" ..
"on %s (%s)",
data.title, artist,
data.album, data.created.year
)
)
end
--- Create a new jammin'! widget. Accepts a table of arguments as optional
-- parameters which override the hardcoded defaults.
--
-- @param playback_handler Handler function for playback status changes
-- @param track_handler Handler function for track status changes
-- @param tooltip_preset Table to be passed to `awful.tooltip`
function jammin.new(args)
local self = setmetatable({}, jammin)
args = args or {}
self.widget = wibox.widget.textbox("")
self.playback_handler = args.playback_handler or default_playback_handler
self.track_handler = args.track_handler or default_track_handler
self.tooltip = awful.tooltip(args.tooltip_preset or {})
self.tooltip:add_to_object(self.widget)
self:playback_handler("Stopped")
self:async_update()
-- Hook into DBus signals
jdbus.add_property_listener(function(...) self:on_propchange(...) end)
return self
end
setmetatable(jammin, {
__call = function(cls, ...)
return cls.new(...)
end
})
return jammin