view vixm/ @ 16:447b47ab396f

Implemented dequeue the proper way; Cleaned up stupidity in enqueue code
author Josef "Jeff" Sipek <>
date Sun, 20 Aug 2006 00:05:14 -0400
parents ae3451bedeb6
children 1c769ae67af4
line wrap: on
line source

# all the user interface related bits

import time, sys, re
from threading import Thread
import xmms

import playlist, song, util, control

def run():
	""" this is where we start execution """

	# first, let's create the two playlists
	print "Creating playlists..."
	lists = {}
	lists[playlist.LIST_PRIO]	= playlist.playlist()
	lists[playlist.LIST_DEFAULT]	= playlist.playlist(allowrandom=True)

	# read in the info for all the songs in XMMS's playlist
	print "Loading songs from XMMS's playlist..."
	songs = []
	listlength = xmms.control.get_playlist_length()
	for i in range(0,listlength):
		s =
	print "Instanciating ui thread..."
	ui = uiThread(lists)

	last = None
	while not ui.shutdown:
		# check which song we are playing now
		pos = xmms.control.get_playlist_pos()
		current = lists[playlist.LIST_DEFAULT][pos]

		# if it is different from what we played last time we
		# checked...
		if current != last:
				# pop song off the PRIO queue
				next = lists[playlist.LIST_PRIO].pop()

				# if successful, play the popped song
				idx = lists[playlist.LIST_DEFAULT].index(next)
				current = next
			except IndexError:
				# no song to pop
			except ValueError:
				print "WTF is going on?!"

		# update last played song
		last = current

		# sleep

class uiThread(Thread):
	""" This is the main ui thread class, it does all the magic
	necessary to have a vi-like interface """
	def __init__(self, lists):

		self.lists = lists
		self.shutdown = False

		# commad list:
		#	quit	'q[!]'
		#		quit fails if there are list changes in
		#		memory that haven't been saved. The optional
		#		'!' forces the quit
		#	number	'[range]n [playlistid]'
		#		prints playlist [playlistid] (default is
		#		LIST_PRIO) with each entry being numbered.
		#		If optional range is supplied, only the
		#		songs in that range are printed. The range
		#		string is standard ed-like line range (see
		#		below for details)
		#	list	'[range]l [playlistid]'
		#		virtually identical to the number command
		#		above, however the lines are not numbered.
		#		The same rules apply to the range and
		#		playlistid arguments
		#	enqueue	'a songid'
		#		enqueue a song songid from LIST_DEFAULT onto
		#		LIST_PRIO. The enqueued song is added to the
		#		end of LIST_PRIO
		#	dequeue	'[range]d'
		#		remove songs in range from LIST_PRIO. The
		#		range is in LIST_PRIO, NOT LIST_DEFAULT
		# range:
		#	''	first entry; shortcut for '1'
		#	'%'	entire list; shortcut for '1,$'
		#	'$'	last entry
		#	'n'	entry on line n
		#	'm,n'	range of entries starting on line m and
		#		ending on line n. Both m and n are included
		#		in the list. m or n can both be an integer,
		#		'' or '$'.
		# command table format:
		#	"command regexp":
		#		(function to call, range allowed, help str)
		self.cmdtable = {
			"n( ([0-9]+)){,1}":
				 "[range]n [playlistid]",
				 "numbered display of lines in range in playlistid"),
			"l( ([0-9]+)){,1}":
				 "[range]l [playlistid]",
				 "list lines in range in playlistid"),
			"a ([0-9]+)":
				 "a [songid]",
				 "add songid from default to priority"),
				 "remove songs in range from priority"),
				 "this help list"),

	def __cmd(self, txt):
		range_str = "(%|\\$|(\\$|[0-9]+){,1}(,(\\$|[0-9]+)){,1}){,1}"

		cmdtable = self.cmdtable
		for c in cmdtable:
			rstr  = "^"
			rstr += (cmdtable[c][1] and range_str or "")
			rstr += c
			rstr += "$"

			m =, txt)
			if not m:

			gr = m.groups()

			start = end = -1
			if cmdtable[c][1]:
				# parse range info

				if gr[3]:
					# we got a 'm,n'
					start = (gr[1] or '')
					end = gr[3]
				elif gr[1]:
					# we got a 'n'
					start = gr[1]
					end = gr[1]
				elif gr[0]:
					# we got a '$' or '%'
					start = (gr[0] == '%' and '1' or '$')
					end = '$'
					# no range specified
					start = '1'
					end = '1'
				start = util.special2int(start)
				end = util.special2int(end)

				gr = list(gr)

			gr = list(gr)

				cmdtable[c][0](self, start, end, gr)
			except ValueError:
				print "Invalid argument/value"
		print "Invalid command \"%s\"" % (txt,)

	def search(self, lid, regexp):
		print "Seaching list %d for '%s':" % (lid, regexp)
		for song in self.lists[lid].search(regexp):
			idx = self.lists[lid].index(song)+1
			print "%d. %s" % (idx, str(song))

	def run(self):
		while not self.shutdown:
			tmp = sys.stdin.readline().strip()
			if tmp.startswith("/"):
				# '/ABC' - searching, tmp[1:]), tmp[1:])
				# 'ABC' - commands