3 # Copyright (C) 2004, 2005 Richard Kettlewell
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19 # THIS PROGRAM IS NO LONGER MAINTAINED.
21 # It worked last time I tried running it, but all client maintenance
22 # effort is now devoted to the web interface and the GTK+ client
25 """Graphical user interface for DisOrder"""
38 ########################################################################
42 # The main (initial) thread of the program runs all GUI code. The GUI is only
43 # directly modified from inside this thread. We sometimes call this the
46 # We have a background thread, MonitorStateThread, which waits for changes to
47 # the server's state which we care about. Whenever such a change occurs it
48 # notifies all the widgets which care about it (and possibly other widgets;
49 # the current implementation is unsophisticated.)
51 # Widget poll() methods usually, but NOT ALWAYS, called in the
52 # MonitorStateThread. Other widget methods are call in the master thread.
54 # We have a separate disorder.client for each thread rather than locking a
55 # single client. MonitorStateThread also has a private disorder.client that
56 # it uses to watch for server state changes.
58 ########################################################################
61 # communication queue into thread containing Tk event loop
63 # Sets up a callback on the event loop (in this thread) which periodically
64 # checks the queue for elements; if any are found they are executed.
65 def __init__(self
, master
):
66 self
.q
= Queue
.Queue();
72 item
= self
.q
.get_nowait()
74 self
.master
.after_idle(self
.poll
)
76 self
.master
.after(100, self
.poll
)
81 ########################################################################
83 class ProgressBar(Canvas
):
85 def __init__(self
, master
=None, **kw
):
86 Canvas
.__init__(self
, master
, highlightthickness
=0, **kw
)
87 self
.outer
= self
.create_rectangle(0, 0, 0, 0,
91 self
.bar
= self
.create_rectangle(0, 0, 0, 0,
97 self
.bind("<Configure>", lambda e
: self
.redisplay())
99 def update(self
, current
, total
):
100 self
.current
= current
114 w
, h
= self
.winfo_width(), self
.winfo_height()
116 self
.coords(self
.outer
, 0, 0, w
- 1, h
- 1)
118 bw
= int((w
- 2) * self
.current
/ self
.total
)
119 self
.itemconfig(self
.bar
,
122 self
.coords(self
.bar
, 1, 1, bw
, h
- 2)
124 self
.itemconfig(self
.bar
,
127 self
.coords(self
.bar
, 1, 1, w
- 2, h
- 2)
129 # look up a track's name part, using client c. Maintains a cache.
131 def part(c
, track
, context
, part
):
132 key
= "%s-%s-%s" %
(part
, context
, track
)
134 if not part_cache
.has_key(key
) or part_cache
[key
]['when'] < now
- 3600:
135 part_cache
[key
] = {'when': now
,
136 'what': c
.part(track
, context
, part
)}
137 return part_cache
[key
]['what']
139 class PlayingWidget(Frame
):
140 # widget that always displays information about what's
142 def __init__(self
, master
=None, **kw
):
143 Frame
.__init__(self
, master
, **kw
)
144 # column 0 is descriptions, column 1 is the values
145 self
.columnconfigure(0,weight
=0)
146 self
.columnconfigure(1,weight
=1)
148 self
.field(0, 0, "artist", "Artist")
149 self
.field(1, 0, "album", "Album")
150 self
.field(2, 0, "title", "Title")
151 # column 1 also has the progress bar in it
152 self
.p
= ProgressBar(self
, height
=20)
153 self
.p
.grid(row
=3, column
=1, sticky
=E
+W
)
154 # column 2 has operation buttons
155 b
= Button(self
, text
="Quit", command
=self
.quit
)
156 b
.grid(row
=0, column
=2, sticky
=E
+W
)
157 b
= Button(self
, text
="Scratch", command
=self
.scratch
)
158 b
.grid(row
=1, column
=2, sticky
=E
+W
)
159 b
= Button(self
, text
="Recent", command
=self
.recent
)
160 b
.grid(row
=2, column
=2, sticky
=E
+W
)
166 def field(self
, row
, column
, name
, label
):
168 Label(self
, text
=label
).grid(row
=row
, column
=column
, sticky
=E
)
169 self
.fields
[name
] = Text(self
, height
=1, state
=DISABLED
)
170 self
.fields
[name
].grid(row
=row
, column
=column
+ 1, sticky
=W
+E
);
172 def set(self
, name
, value
):
173 # set a field's value
174 f
= self
.fields
[name
]
175 f
.config(state
=NORMAL
)
178 f
.config(state
=DISABLED
)
180 def playing(self
, p
):
181 # called with new what's-playing information
184 for tpart
in ['artist', 'album', 'title']:
185 values
[tpart
] = part(client
, p
['track'], 'display', tpart
)
187 self
.length
= client
.length(p
['track'])
188 except disorder
.operationError
:
190 self
.started
= int(p
['played'])
193 for k
in self
.fields
.keys():
195 self
.set(k
, values
[k
])
200 def length_bar(self
):
201 if self
.length
and self
.length
> 0:
202 self
.p
.update(time
.time() - self
.started
, self
.length
)
206 def update_length(self
):
208 self
.after(1000, self
.update_length
)
213 intercom
.put(lambda: self
.playing(p
))
222 def recent_close(self
):
223 self
.recentw
.destroy()
228 self
.recentw
.deiconify()
231 w
= 80*tracklistFont
.measure('A')
232 h
= 40*tracklistFont
.metrics("linespace")
233 self
.recentw
= Toplevel()
234 self
.recentw
.protocol("WM_DELETE_WINDOW", self
.recent_close
)
235 self
.recentw
.title("Recently Played")
236 # XXX for some reason Toplevel(width=w,height=h) doesn't seem to work
237 self
.recentw
.geometry("%dx%d" %
(w
,h
))
238 w
= RecentWidget(self
.recentw
)
239 w
.pack(fill
=BOTH
, expand
=1)
242 class TrackListWidget(Frame
):
243 def __init__(self
, master
=None, **kw
):
244 Frame
.__init__(self
, master
, **kw
)
245 self
.yscrollbar
= Scrollbar(self
)
246 self
.xscrollbar
= Scrollbar(self
, orient
=HORIZONTAL
)
247 self
.canvas
= Canvas(self
,
248 xscrollcommand
=self
.xscrollbar
.set,
249 yscrollcommand
=self
.yscrollbar
.set)
250 self
.xscrollbar
.config(command
=self
.canvas
.xview
)
251 self
.yscrollbar
.config(command
=self
.canvas
.yview
)
252 self
.canvas
.grid(row
=0, column
=0, sticky
=N
+S
+E
+W
)
253 self
.yscrollbar
.grid(row
=0, column
=1, sticky
=N
+S
)
254 self
.xscrollbar
.grid(row
=1, column
=0, sticky
=E
+W
)
255 self
.columnconfigure(0,weight
=1)
256 self
.rowconfigure(0,weight
=1)
258 self
.default_cursor
= self
['cursor']
259 self
.configure(cursor
="watch")
261 def queue(self
, q
, w_artists
, w_albums
, w_titles
, artists
, albums
, titles
):
262 # called with new queue state
263 # delete old contents
265 for i
in self
.canvas
.find_all():
266 self
.canvas
.delete(i
)
268 # if the call was queued but not received before the window was deleted
269 # we might get an error from Tcl/Tk, which no longer knows the window,
272 w
= tracklistHFont
.measure("Artist")
275 w
= tracklistHFont
.measure("Album")
278 w
= tracklistHFont
.measure("Title")
281 hheading
= tracklistHFont
.metrics("linespace")
282 h
= tracklistFont
.metrics('linespace')
284 x_album
= x_artist
+ w_artists
+ 16
285 x_title
= x_album
+ w_albums
+ 16
286 w
= x_title
+ w_titles
+ 8
287 self
.canvas
['scrollregion'] = (0, 0, w
, h
* len(artists
) + hheading
)
288 self
.canvas
.create_text(x_artist
, 0, text
="Artist",
291 self
.canvas
.create_text(x_album
, 0, text
="Album",
294 self
.canvas
.create_text(x_title
, 0, text
="Title",
298 for n
in range(0,len(artists
)):
303 self
.canvas
.create_text(x_artist
, y
, text
=artist
,
307 self
.canvas
.create_text(x_album
, y
, text
=album
,
311 self
.canvas
.create_text(x_title
, y
, text
=title
,
316 self
.configure(cursor
=self
.default_cursor
)
321 # we do the track name calculation in the background thread so that
322 # the gui can still be responsive
326 w_artists
= w_albums
= w_titles
= 16
328 artist
= part(c
, t
['track'], 'display', 'artist')
329 album
= part(c
, t
['track'], 'display', 'album')
330 title
= part(c
, t
['track'], 'display', 'title')
331 w
= tracklistFont
.measure(artist
)
334 w
= tracklistFont
.measure(album
)
337 w
= tracklistFont
.measure(title
)
340 artists
.append(artist
)
343 intercom
.put(lambda: self
.queue(q
, w_artists
, w_albums
, w_titles
,
344 artists
, albums
, titles
))
347 class QueueWidget(TrackListWidget
):
348 def __init__(self
, master
=None, **kw
):
349 TrackListWidget
.__init__(self
, master
, **kw
)
351 def getqueue(self
, c
):
354 class RecentWidget(TrackListWidget
):
355 def __init__(self
, master
=None, **kw
):
356 TrackListWidget
.__init__(self
, master
, **kw
)
358 def getqueue(self
, c
):
363 class MonitorStateThread
:
364 # thread to pick up current server state and publish it
366 # Creates a client and monitors it in a daemon thread for state changes.
367 # Whenever one occurs, call w.poll(c) for every member w of widgets with
368 # a client owned by the thread in which the call occurs.
369 def __init__(self
, widgets
, masterclient
=None):
370 self
.logclient
= disorder
.client()
371 self
.client
= disorder
.client()
372 self
.clientlock
= threading
.Lock()
374 masterclient
= disorder
.client()
375 self
.masterclient
= masterclient
376 self
.widgets
= widgets
377 self
.lock
= threading
.Lock()
379 self
.thread
= threading
.Thread(target
=self
.run
)
380 self
.thread
.setDaemon(True)
382 # spare thread for processing additions
383 self
.adderq
= Queue
.Queue()
384 self
.adder
= threading
.Thread(target
=self
.runadder
)
385 self
.adder
.setDaemon(True)
388 def notify(self
, line
):
390 widgets
= self
.widgets
393 self
.clientlock
.acquire()
395 self
.clientlock
.release()
400 self
.widgets
.append(w
)
402 self
.adderq
.put(lambda client
: w
.poll(client
))
404 def remove(self
, what
):
406 self
.widgets
.remove(what
)
411 self
.logclient
.log(lambda client
, line
: self
.notify(line
))
415 item
= self
.adderq
.get()
416 self
.clientlock
.acquire()
418 self
.clientlock
.release()
420 ########################################################################
431 -h, --help Display this message
432 -V, --version Display version number
434 tkdisorder is copyright (c) 2004, 2005 Richard Kettlewell.
437 ########################################################################
440 opts
, rest
= getopt
.getopt(sys
.argv
[1:], "Vh", ["version", "help"])
441 except getopt
.GetoptError
, e
:
442 sys
.stderr
.write("ERROR: %s, try --help for help\n" % e
.msg
)
445 if o
in ('-V', '--version'):
446 print "%s" % disorder
.version
449 if o
in ('h', '--help'):
454 client
= disorder
.client() # master thread's client
457 root
.title("DisOrder")
459 tracklistFont
= tkFont
.Font(family
='Helvetica', size
=10)
460 tracklistHFont
= tracklistFont
.copy()
461 tracklistHFont
.config(weight
="bold")
463 p
= PlayingWidget(root
)
464 p
.pack(fill
=BOTH
, expand
=1)
466 q
= QueueWidget(root
)
467 q
.pack(fill
=BOTH
, expand
=1)
469 intercom
= Intercom(root
) # only need a single intercom
470 mst
= MonitorStateThread([p
, q
], client
)