-- Licensed under GNU General Public License v2 2014 Yauheni Kirylau -

 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
--[[
Licensed under GNU General Public License v2
* (c) 2014, Yauheni Kirylau
--]]
local awful = require("awful")
local async = require("actionless.async")
local h_table = require("actionless.table")
local parse = require("actionless.parse")
local dbus_cmd = "qdbus org.mpris.MediaPlayer2.clementine "
local clementine = {
player_status = {},
player_cmd = 'clementine'
}
-------------------------------------------------------------------------------
function clementine.toggle()
awful.util.spawn_with_shell(dbus_cmd .. "/org/mpris/MediaPlayer2 PlayPause")
end
function clementine.next_song()
awful.util.spawn_with_shell(dbus_cmd .. "/org/mpris/MediaPlayer2 Next")
end
function clementine.prev_song()
awful.util.spawn_with_shell(dbus_cmd .. "/org/mpris/MediaPlayer2 Previous")
end
-------------------------------------------------------------------------------
function clementine.update(parse_status_callback)
async.execute(
dbus_cmd .. " /org/mpris/MediaPlayer2 PlaybackStatus",
function(str) clementine.post_update(str, parse_status_callback) end
)
end
-------------------------------------------------------------------------------
function clementine.post_update(result_string, parse_status_callback)
clementine.player_status = {}
local state = nil
if result_string:match("Playing") then
state = 'play'
elseif result_string:match("Paused") then
state = 'pause'
end
clementine.player_status.state = state
if state == 'play' or state == 'pause' then
async.execute(
dbus_cmd .. "/Player GetMetadata",
function(str) clementine.parse_metadata(str, parse_status_callback) end
)
else
parse_status_callback(clementine.player_status)
end
end
-------------------------------------------------------------------------------
function clementine.parse_metadata(result_string, parse_status_callback)
local player_status = parse.find_values_in_string(
result_string,
"([%w]+): (.*)$",
{ file='location',
artist='artist',
title='title',
album='album',
date='year',
cover='arturl'
}
)
h_table.merge(clementine.player_status, player_status)
parse_status_callback(clementine.player_status)
end
return clementine