From d3e8d859b6b19c6be2921fa080dbbb865c69fe77 Mon Sep 17 00:00:00 2001 From: wes Date: Mon, 5 Mar 2012 23:02:22 -0500 Subject: [PATCH] made more better --- config_parser.rkt | 4 ++-- helpers.rkt | 2 ++ lazyplay.rkt | 34 ++++++++++++++++++---------------- 3 files changed, 22 insertions(+), 18 deletions(-) diff --git a/config_parser.rkt b/config_parser.rkt index 8fec05d..ee72421 100755 --- a/config_parser.rkt +++ b/config_parser.rkt @@ -7,7 +7,7 @@ (read ip)) (close-input-port ip) -(define settings (make-hash)) -(update settings parsed) +(define settings + (update (make-hash) parsed)) (provide settings) \ No newline at end of file diff --git a/helpers.rkt b/helpers.rkt index c8d0337..023ddc8 100644 --- a/helpers.rkt +++ b/helpers.rkt @@ -12,5 +12,7 @@ (define (flip f) (lambda (y x) (f x y))) + +(define nowhere (open-output-nowhere 'nowhere #t)) (provide (all-defined-out)) diff --git a/lazyplay.rkt b/lazyplay.rkt index 52a9df5..106fd77 100755 --- a/lazyplay.rkt +++ b/lazyplay.rkt @@ -2,15 +2,16 @@ #lang racket (require racket/system) +(require racket/pretty) (require "helpers.rkt") (require "config_parser.rkt") (define args (vector->list (current-command-line-arguments))) -(define file-types (make-hash)) (define media-player (string->path (hash-ref settings "player" "/usr/bin/mplayer"))) (define player-args (hash-ref settings "args" "")) -(update file-types (hash-ref settings "filetypes" '(("avi" #t)))) +(define file-types + (update (make-hash) (hash-ref settings "filetypes" '(("avi" #t))))) (define (file-list) ; list of files in the current working directory (map path->string ; get the strings from the list of paths @@ -55,44 +56,45 @@ (let* ((nullport (open-output-file (string->path "/dev/null") #:exists 'append))) ; we don't want any output from the process (call-with-values - (lambda () (subprocess nullport #f nullport media-player (car filenames) args)) + (lambda () (subprocess nullport #f nullport media-player (car filenames) args)) list))) ; convert the 4 return values into a list -(define (get-args message) - (cond ((false? message) player-args) +(define (get-args message args) + (cond ((false? message) args) (else message))) (define (play fnames played args) (cond ((null? fnames) '()) (else (let* ((results (play-files fnames args))) ; get the pid and the 3 i/o ports - ; send the other thread the new pid here - ; and the output port for piping input to the process - ; (thread-send controller-thread ((third results) (first results))) - (subprocess-wait (first results)) - (close-output-port (third results))) - (let* ((newfs (new-files played (play-list)))) - (play (append (cdr fnames) newfs) (update played newfs) (get-args (thread-try-receive))))))) + (subprocess-wait (first results)) ; block until a new file is started + (close-output-port (third results))) + (let* ((newfs (new-files played (play-list)))) ; get new list of files + (play + (append (cdr fnames) newfs) ; append new files to the tail of the list of old files + (update played newfs) ; update the set of played files + (get-args (thread-try-receive) args)))))) ; check for new arguments from controller (define (controller player-thread) (cond ((compose not thread-running?) player-thread ; if the thread is not running then return '())) - (display "> ") + (display "> ") ; TODO; add gnu readline support (let* ((input (read-line (current-input-port) 'linefeed))) input - (cond ((eof-object? input) '()) + (cond ((eof-object? input) (kill-thread player-thread)) ; check if received EOF, and kill player-thread (else (thread-send player-thread input) (controller player-thread))))) -(play-list) + (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 (define (check) ; (sleep 20) - (cond (((compose not thread-running?) player-thread) (kill-thread controller-thread)) + (cond (((compose not thread-running?) player-thread) + (kill-thread controller-thread)) (else (check)))) (check) \ No newline at end of file