#!/usr/local/bin/gosh

;; Simple Image Browser
;; 
;; Created:    <2001-06-17 18:08:20 foof>
;; Time-stamp: <2003-01-20 15:00:16 foof>
;; Author:     Alex Shinn <foof@debian.org>


;; load the SDL module and some useful srfi's
(use sdl)
(use sdl.image)
(use srfi-1)
(use srfi-2)
(use file.util)

;; initialize the video subsystem
(sdl-init SDL_INIT_VIDEO)

;; directory to search for images in
(define image-dir "/usr/share/pixmaps/")

;; build a ring of image file names
(define image-ring
  (directory-list image-dir :add-path? #t :children? #t
                  :filter (lambda (f) (file-is-regular? (string-append image-dir "/" f)))))

;; functions to cycle through the ring
(define (next-image)
  (let ((next (car image-ring)))
    (set! image-ring (cdr image-ring))
    next))

(define (prev-image)
  (let ((orig image-ring))
    (while (not (eq? (cddr image-ring) orig))
      (set! image-ring (cdr image-ring)))
    (let ((image (car image-ring)))
      (set! image-ring (cdr image-ring))
      image)))

;; display an image given a filename
(define (show file)
  (and-let* ((image (img-load file)))
    (sdl-set-video-mode (sdl-surface-w image) (sdl-surface-h image) 24 0)
    (sdl-blit-surface image #f (sdl-get-video-surface) #f)
    (sdl-flip (sdl-get-video-surface))))

;; show the first image
(show (next-image))

;; event handler
(let handle ((e (sdl-make-event)))
  (if (sdl-wait-event e)
    (let ((type (sdl-event-type e)))
      (cond ((equal? type SDL_KEYDOWN)
             (show (next-image)))
            ((equal? type SDL_MOUSEBUTTONDOWN)
             (sdl-quit)
             (exit)))))
  (handle e))


syntax highlighted by Code2HTML, v. 0.9.1