/
cxmms.py
297 lines (251 loc) · 7.29 KB
/
cxmms.py
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
#!/usr/bin/env python
#
# Copyright (C) 2005 Gopal Vijayaraghavan <gopalv82>
#
# * This program 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; version 2 of the License.
#
# A Couple of More Features Added by Tejas Dinkar <tejasdinkar@gmail.com>
#
# Cleanup of Code
# Shuffle Funtion
# Change Logging Style
# Search Function
# Fixed Bug regarding scroll bar
# Include better song Searching
#
# And yes, it might not work for you ? :)
import xmms, curses
import select
import sys
debug = False
def key_strokes():
# This Corresponds to each Key Stroke's ASCII Value
x = ord("x")
c = ord("c")
v = ord("v")
z = ord("z")
b = ord("b")
j = ord("j")
s = ord("s")
q = ord("q")
esc = (27, None)
up = (27, 79, 65)
down = (27, 79, 66)
right = (27, 79, 67)
left = (27, 79, 68)
enter = 10
backspace = 127
return locals()
if debug:
log = open('/tmp/cxmms.log','w')
else:
# This Sets Log File as /dev/null if needed
class logger:
def __init__(self):
self.write = lambda a: None
log = logger()
def format_time (time):
'''Just formats the Time'''
if time > 3600:
return "%02d:%02d:%02d" % (time/3600, (time % 3600)/60, time % 60)
else:
return "%02d:%02d" % (time/60,time % 60)
def logo(stdscr):
'''Draws the Logo'''
str = ".::Commandline XMMS::."
stdscr.insstr(3, 40-len(str)/2, str)
stdscr.refresh()
copyright = "(C) 2005, Blug.in"
stdscr.insstr(20, 70-len(copyright), copyright)
stdscr.refresh()
class xmms_main_window:
'''The Main Class... All work is here'''
def __init__(self, stdscr, top = 6,left = 10):
self.stdscr = stdscr
self.win = curses.newwin(13, 60, top, left)
self.win.border()
# Gets a list of recognised key - strokes
self.keys = key_strokes()
# The next block represents the various widgets
self.timers = self.win.subwin(2, 10, top+1, left + 3)
self.title = self.win.subwin(3, 40, top+1, left + 15)
self.playtime = self.win.subwin(2, 40, top+4, left + 15)
self.volume = self.win.subwin(7, 10, top+3, left + 2)
self.jump = self.win.subwin(6, 45, top+6, left + 12)
self.shuffle = self.win.subwin(1,1,top+10, left + 5)
if xmms.is_shuffle():
self.shuffle.insstr(0,0,"S")
self.windows = [self.timers, self.playtime, self.shuffle, self.volume, self.win, self.jump, self.title]
key = self.keys
# maks the keys to their appropriate function
self.keymaps = {
key["x"] : xmms.play,
key["c"] : xmms.pause,
key["v"] : xmms.stop,
key["z"] : xmms.playlist_prev,
key["b"] : xmms.playlist_next,
key["s"] : self.toggle_shuffle,
key["j"] : self.search,
key["q"] : sys.exit,
key["esc"] : sys.exit,
key["up"] : lambda : xmms.set_main_volume(min(100, xmms.get_main_volume() + 10)),
key["down"] : lambda : xmms.set_main_volume(max(0, xmms.get_main_volume() - 10)),
key["right"] : lambda : xmms.jump_to_time(xmms.get_output_time()+5000),
key["left"] : lambda : xmms.jump_to_time(max(0,xmms.get_output_time()-5000))
};
def get_key(self, timeout = 1):
'''Reads a Key from the Screen'''
# select() rocks, timeout == 1 sec
(read, write, err) = select.select([0], [], [], timeout)
# if any key pressed
if 0 in read:
key = self.win.getch()
# Checks specially for Arrow Keys
if key == 0x1b:
next = self.get_key(0)
if next == 79 or next == 91:
next = self.get_key(0)
return (27, 79, next)
return(key,next)
return int(key)
return None
def songs_that_match(self,string):
'''List of Songs with string in their song name'''
songs = []
for i in range(xmms.get_playlist_length()):
if string.lower() in xmms.get_playlist_title(i).lower():
songs.append(i)
return songs
def draw_jump(self,string):
'''This Draws Jump.... But also returns selected song'''
self.jump.clear()
self.jump.insstr(1,2,"Search: %s" % string)
songs = self.songs_that_match(string)
# saves the length of the matching song list
self.length = len(songs)
try:
slice = songs[self.base:self.base+3]
except:
slice = songs[:3]
i = 2
for song in slice:
if self.highlight + 2 == i:
style = curses.A_STANDOUT
else:
style = curses.A_NORMAL
self.jump.insstr(i,2,xmms.get_playlist_title(song)[:42],style)
i = i + 1
self.jump.border()
print >> log, "slice =", str(slice)
# return the song number, or -1
try:
return slice[self.highlight]
except:
return -1
def search(self):
'''This function searches, and plays songs if asked'''
self.jump.clear()
string = ""
# self.base is the first drawn song in jump window
# self.highlight is the highlighted song
self.base = 0
self.highlight = 0
# if any key pressed
while True:
song = self.draw_jump(string)
print >> log, "song returned %d\n" % song
self.update()
# select() rocks, timeout == 1 sec
key = self.get_key()
if key:
print >> log, "key pressed %s\n" % str(key)
if key == self.keys["esc"]:
self.jump.clear()
return
elif key == self.keys["enter"]:
if song != -1:
xmms.set_playlist_pos(song)
self.jump.clear()
return
elif key == self.keys["backspace"]:
string = string[:-1]
self.base = 0
self.highlight = 0
elif key == self.keys["down"]:
if self.highlight < 2:
self.highlight = self.highlight + 1
else:
self.base = min(self.base + 1, self.length - 3)
elif key == self.keys["up"]:
if self.highlight > 0:
self.highlight = self.highlight - 1
else:
self.base = max(self.base - 1, 0)
else:
# try used in case a non letter is passed
try:
string = string + chr(key)
self.base = 0
self.highlight = 0
except:
pass
def toggle_shuffle(self):
'''Simply Toggles the shuffle function'''
self.shuffle.clear()
if xmms.is_shuffle():
self.shuffle.insstr(0,0," ")
else:
self.shuffle.insstr(0,0,"S")
xmms.toggle_shuffle()
def update(self):
'''Updates all windows'''
# This block gets variables, like time, number, title. etc...
time = xmms.get_output_time()/1000
num = xmms.get_playlist_pos()
title = xmms.get_playlist_title(num)
shuffle = xmms.is_shuffle()
length = xmms.get_playlist_time(num) / 1000
t = format_time(time)
self.timers.clear()
self.timers.addstr(t)
self.title.clear()
self.title.addstr("%d. %s (%s)" % (num,title,format_time(length)))
t = (time * 40) / length
self.playtime.clear()
self.playtime.insstr(0,0,'.' * t)
self.playtime.insstr(0, min(t,39), '%',curses.A_BOLD)
if t < 39:
self.playtime.insstr(0,t+1,'.' * (39 -t))
self.volume.clear()
v = xmms.get_main_volume()
self.volume.insstr(0,0, 'Vol: %2d' % (v))
# draws the volume bar
v = int(round(v / 10))
for i in range(0, 5):
if (i * 2 < v):
self.volume.hline(6-i, 0, '#', 2*i-1, curses.A_BOLD)
else:
self.volume.hline(6-i, 0, '_', 2*i-1)
# # gratuitous use of lambda
map(lambda a: a.refresh(), self.windows)
def main_keyloop(self):
'''Main Loop'''
while True:
self.update()
key = self.get_key()
if key:
print >> log, "key pressed %s\n" % str(key)
if self.keymaps.has_key(key):
self.keymaps[key]()
def main(stdscr):
curses.savetty()
try:
logo(stdscr)
w = xmms_main_window(stdscr)
w.main_keyloop()
finally:
curses.resetty()
if __name__ == '__main__':
curses.wrapper(main)