From 653eb1a804a331297df1dae670357f5922a9e455 Mon Sep 17 00:00:00 2001 From: wes Date: Sat, 11 Aug 2012 22:34:52 -0400 Subject: [PATCH] changed to not exit when done playing and able to chdir/add new resources after done playing --- lazyplay.rkt | 50 ++++++++++++++++++++++++++++++++------------------ 1 file changed, 32 insertions(+), 18 deletions(-) diff --git a/lazyplay.rkt b/lazyplay.rkt index 464b62e..770a41b 100755 --- a/lazyplay.rkt +++ b/lazyplay.rkt @@ -17,6 +17,9 @@ (define file-types (update (make-hash) (hash-ref settings "filetypes" '(("avi" #t))))) +(define (update! newfs) + (update played (map (compose reverse ((curry list) #f)) newfs))) + (define (file-list) ; list of files in the current working directory (map path->string ; get the strings from the list of paths (directory-list (current-directory)))) @@ -86,7 +89,19 @@ (else message))) (define (play fnames played args) - (cond ((null? fnames) '()) + (cond ((null? fnames) + (let* ([new-data (thread-receive)]) + (match (list (hash-ref new-data 'new-dir) + (hash-ref new-data 'new-files) + (hash-ref new-data 'arguments)) + [(list #f a #f) + (update! a) + (play a played player-args)] + [(list a (list) #f) (current-directory a) + (update! (play-list)) + (play (play-list) played player-args)] + [a (display a)]))) + (else (let* ((results (play-files fnames args))) ; get the pid and the 3 i/o ports (subprocess-wait (first results)) ; block until a new file is started @@ -100,26 +115,24 @@ [_ (let ([newfs (append new-dir-files (hash-ref new-data 'new-files))]) (play (append (cdr fnames) newfs) ; append new files to the tail of the list of old files - (update played (map (compose reverse ((curry list) #f)) newfs)) ; update the set of played files + (update! newfs) ; update the set of played files (get-args (hash-ref new-data 'arguments) args)))]))))) ; check for new arguments from controller +;; returns a hash with the commands for the player thread +(define (parsed-hash args newdir newfiles) + (let ([result (make-hash)]) + (hash-set! result 'arguments args) + (hash-set! result 'new-dir newdir) + (hash-set! result 'new-files newfiles) + result)) + +;; parses a command to be sent to the player thread (define (parse-command cmd) (match (regexp-split #px"\\s" cmd) - [(list-rest "cmds" xs) - (let ([result (make-hash)]) - (hash-set! result 'arguments xs) - (hash-set! result 'new-files '()) - result)] - [(list-rest "add" xs) - (let ([result (make-hash)]) - (hash-set! result 'arguments #f) - (hash-set! result 'new-files (list (string-join xs ""))) - result)] - [_ (let ([result (make-hash)]) - (hash-set! result 'arguments #f) - (hash-set! result 'new-files '()) - result)])) - + [(list-rest "cmds" xs) (parsed-hash xs #f '())] + [(list-rest "add" xs) (parsed-hash #f #f (list (string-join xs "")))] + [(list-rest "chdir" xs) (parsed-hash #f (string-join xs "") '())] + [_ (parsed-hash #f #f '())])) (define (controller player-thread) (cond @@ -133,7 +146,8 @@ (thread-send player-thread (parse-command input)) (controller player-thread))))) -(define player-thread (thread (lambda () (play (play-list) played player-args)))) +(define player-thread (thread (lambda () + (play (play-list) played player-args)))) (define controller-thread (thread (lambda () (controller player-thread)))) ; check to see if the player is running, and if not then kill the controller