File: MPlayer.hs

package info (click to toggle)
haskell-gtk 0.15.7-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 3,964 kB
  • sloc: haskell: 3,346; ansic: 826; makefile: 161
file content (78 lines) | stat: -rw-r--r-- 2,172 bytes parent folder | download | duplicates (11)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
-- | MPlayer client demo
--  Author      :  Andy Stewart
--  Copyright   :  (c) 2010 Andy Stewart <lazycat.manatee@gmail.com>

module Main where

import Control.Applicative
import Control.Concurrent
import Control.Concurrent.STM
import Control.Monad
import Control.Monad.Trans
import Graphics.UI.Gtk
import System.Exit
import System.IO
import System.Process
import System.Environment
import Text.Printf

main :: IO ()
main = do
  args <- getArgs
  case args of
    [filepath] -> do
        initGUI

        mainWindow <- windowNew
        windowSetDefaultSize mainWindow 800 450
        windowSetPosition mainWindow WinPosCenter

        mplayer <- mplayerNew
        mplayerStick mplayer (toContainer mainWindow)

        mainWindow `afterShow` do
          mplayerRun mplayer filepath

          mainWindow `onDestroy` do
            mplayerQuit mplayer
            mainQuit

          return ()

        widgetShowAll mainWindow

        mainGUI
    _ -> putStrLn "Usage : mplayer file"

data MPlayer =
    MPlayer {mplayerWidget      :: DrawingArea
            ,mplayerHandle      :: TVar (Maybe (Handle, Handle, Handle, ProcessHandle))}

mplayerNew :: IO MPlayer
mplayerNew =
  MPlayer <$> drawingAreaNew
          <*> newTVarIO Nothing

mplayerStick :: MPlayer -> Container -> IO ()
mplayerStick (MPlayer {mplayerWidget = mWidget}) container = do
  widgetShowAll mWidget
  container `containerAdd` mWidget

mplayerRun :: MPlayer -> FilePath -> IO ()
mplayerRun (MPlayer {mplayerWidget = mWidget
                    ,mplayerHandle = mHandle}) filepath = do
  drawWindow <- widgetGetDrawWindow mWidget -- you just can get DrawWindow after widget realized
  wid <- liftM fromNativeWindowId $ drawableGetID drawWindow
  handle <- runInteractiveCommand $ printf "mplayer %s -slave -wid %d" filepath (wid :: Int)
  writeTVarIO mHandle (Just handle)

mplayerQuit :: MPlayer -> IO ()
mplayerQuit MPlayer {mplayerHandle = mHandle} = do
  handle <- readTVarIO mHandle
  case handle of
    Just (inp, _, _, _) -> hPutStrLn inp "quit"
    Nothing -> return ()

-- | The IO version of `writeTVar`.
writeTVarIO :: TVar a -> a -> IO ()
writeTVarIO a b = atomically $ writeTVar a b