[Tkabber-dev] r59 - trunk/plugins/now_playing

tkabber-svn at jabber.ru tkabber-svn at jabber.ru
Sun Sep 30 19:27:58 MSD 2007


Author: kostix
Date: 2007-09-30 19:27:58 +0400 (Sun, 30 Sep 2007)
New Revision: 59

Modified:
   trunk/plugins/now_playing/mpd.tcl
Log:
now_playing/mpd.tcl:
 * Interp alias named after the socket is now created for each successfully
   connected socket -- this new command installs given "fileevent readable"
   handler on the socket.
 * Implemented accumulating of player status and song info directly in socket
   handlers thus eliminating the need for special global array(s)
   to hold these states.


Modified: trunk/plugins/now_playing/mpd.tcl
===================================================================
--- trunk/plugins/now_playing/mpd.tcl	2007-09-30 15:04:52 UTC (rev 58)
+++ trunk/plugins/now_playing/mpd.tcl	2007-09-30 15:27:58 UTC (rev 59)
@@ -20,7 +20,7 @@
 		sock_connect_timeout   2
 		sock_reconnect_timeout 5
 		sock_user              ""
-		sock_password          zhoppa
+		sock_password          ""
 	}
 
 	proc my what {
@@ -173,15 +173,6 @@
 	}
 }
 
-proc now_playing::SockShutdown sock {
-	close $sock
-
-	variable $sock
-	if {[info exists $sock]} {
-		unset $sock
-	}
-}
-
 #### MPD:
 
 proc now_playing::laststate {op {val ""}} {
@@ -226,7 +217,8 @@
 	variable mpd
 
 	if {[array exists mpd]} {
-		SockShutdown $mpd(sock)
+		close $mpd(sock)
+		interp alias [mycmd $mpd(sock] {}
 		unset mpd
 	}
 }
@@ -252,7 +244,9 @@
 	if {$connected} {
 		fconfigure $sock -blocking no -buffering line -translation lf -encoding utf-8
 
-		fileevent $sock readable [mycmd mpd_read_hello $sock]
+		interp alias {} [mycmd $sock] {} fileevent $sock readable
+
+		$sock [mycmd mpd_read_hello $sock]
 	} else {
 		mpd_schedule_reconnect
 	}
@@ -276,7 +270,7 @@
 	variable options
 	if {$options(sock_password) != ""} {
 		puts $sock "password $options(sock_password)"
-		fileevent $sock readable [mycmd mpd_process_login $sock]
+		$sock [mycmd mpd_process_login $sock]
 	} else {
 		mpd_start_harvesting $sock
 	}
@@ -319,17 +313,7 @@
 	debugmsg tune [info level 0]
 
 	puts $sock status
-
-	variable $sock
-	upvar 0 $sock transient
-
-	array set transient {
-		state    unknown
-		playlist -1
-		songid   -1
-	}
-
-	fileevent $sock readable [mycmd mpd_yield_status $sock]
+	$sock [mycmd mpd_yield_status $sock [list]]
 }
 
 proc now_playing::mpd_schedule_next_yield sock {
@@ -342,7 +326,7 @@
 		[mycmd mpd_yield $sock]]
 }
 
-proc now_playing::mpd_yield_status sock {
+proc now_playing::mpd_yield_status {sock status} {
 	debugmsg tune [info level 0]
 
 	if {[gets $sock line] < 0} {
@@ -350,45 +334,45 @@
 		return
 	}
 
-	variable $sock
-	upvar 0 $sock {}
-
 	switch -glob -- $line {
 		state:*    {
-			set (state)    [mpd_getval $line state]
+			lappend status state    [mpd_getval $line state]
+			$sock [mycmd mpd_yield_status $sock $status]
 		}
 		playlist:* {
-			set (playlist) [mpd_getval $line playlist]
+			lappend status playlist [mpd_getval $line playlist]
+			$sock [mycmd mpd_yield_status $sock $status]
 		}
 		songid:*   {
-			set (songid)   [mpd_getval $line songid]
+			lappend status songid   [mpd_getval $line songid]
+			$sock [mycmd mpd_yield_status $sock $status]
 		}
 		ACK*       {
 			mpd_handle_error
 			die $line
 		}
 		OK         {
-			switch -- [mpd_process_status $sock] {
+			switch -- [mpd_process_status $status] {
 				stop {
 					mpd_schedule_next_yield $sock
 				}
 				proceed {
 					puts $sock currentsong
-					fileevent $sock readable [mycmd mpd_yield_songinfo $sock]
+					$sock [mycmd mpd_yield_songinfo $sock [list]]
 				}
 			}
 		}
 	}
 }
 
-proc now_playing::mpd_process_status sock {
+proc now_playing::mpd_process_status status {
 	debugmsg tune [info level 0]
 
 	variable options
 	variable lasttime
-	variable $sock
-	upvar 0 $sock {}
 
+	array set {} $status
+
 	switch -- $(state) {
 		stop  -
 		pause {
@@ -435,7 +419,7 @@
 	}
 }
 
-proc now_playing::mpd_yield_songinfo sock {
+proc now_playing::mpd_yield_songinfo {sock info} {
 	debugmsg tune [info level 0]
 
 	if {[gets $sock line] < 0} {
@@ -443,45 +427,45 @@
 		return
 	}
 
-	variable $sock
-	upvar 0 $sock {}
-
 	switch -glob -- $line {
 		Artist:* {
-			set (artist) [mpd_getval $line Artist]
+			lappend info artist [mpd_getval $line Artist]
+			$sock [mycmd mpd_yield_songinfo $sock $info]
 		}
 		Title:*  {
-			set (title)  [mpd_getval $line Title]
+			lappend info title  [mpd_getval $line Title]
+			$sock [mycmd mpd_yield_songinfo $sock $info]
 		}
 		Track:*  {
-			set (track)  [mpd_getval $line Track]
+			lappend info track  [mpd_getval $line Track]
+			$sock [mycmd mpd_yield_songinfo $sock $info]
 		}
 		Time:*   {
-			set (length) [mpd_getval $line Time]
+			lappend info length [mpd_getval $line Time]
+			$sock [mycmd mpd_yield_songinfo $sock $info]
 		}
 		Album:*  {
-			set (source) [mpd_getval $line Album]
+			lappend info source [mpd_getval $line Album]
+			$sock [mycmd mpd_yield_songinfo $sock $info]
 		}
 		ACK*     {
 			mpd_handle_error
 			die $line
 		}
 		OK       {
-			mpd_process_songinfo $sock
+			mpd_process_songinfo $sock $info
 		}
 	}
 }
 
-proc now_playing::mpd_process_songinfo sock {
+proc now_playing::mpd_process_songinfo {sock info} {
 	debugmsg tune [info level 0]
 
 	variable lasttime
 	variable connections
-	variable $sock
-	upvar 0 $sock transient
 
 	set opts [list]
-	foreach {key val} [array get transient] {
+	foreach {key val} $info {
 		lappend opts -$key $val
 	}
 
@@ -491,8 +475,6 @@
 
 	set lasttime [clock seconds]
 
-	# TODO must somehow clear track info here
-
 	mpd_schedule_next_yield $sock
 }
 



More information about the Tkabber-dev mailing list