diff options
author | Gregor Kleen <gkleen@yggdrasil.li> | 2016-02-14 21:43:23 +0000 |
---|---|---|
committer | Gregor Kleen <gkleen@yggdrasil.li> | 2016-02-14 21:43:23 +0000 |
commit | 0609858031013f9d95f8104739811d6413331e9b (patch) | |
tree | c47d097d2984a088d7793f2f25dcdd6cb592be7b /server/src/Thermoprint/Server | |
parent | d4c2170b56b94497e37c94e5e3c9ee6f18a2ed43 (diff) | |
download | thermoprint-0609858031013f9d95f8104739811d6413331e9b.tar thermoprint-0609858031013f9d95f8104739811d6413331e9b.tar.gz thermoprint-0609858031013f9d95f8104739811d6413331e9b.tar.bz2 thermoprint-0609858031013f9d95f8104739811d6413331e9b.tar.xz thermoprint-0609858031013f9d95f8104739811d6413331e9b.zip |
First work on driver for generic (cheap) printers
Diffstat (limited to 'server/src/Thermoprint/Server')
-rw-r--r-- | server/src/Thermoprint/Server/Printer/Generic.hs | 100 |
1 files changed, 100 insertions, 0 deletions
diff --git a/server/src/Thermoprint/Server/Printer/Generic.hs b/server/src/Thermoprint/Server/Printer/Generic.hs new file mode 100644 index 0000000..bf6ce3b --- /dev/null +++ b/server/src/Thermoprint/Server/Printer/Generic.hs | |||
@@ -0,0 +1,100 @@ | |||
1 | {-# LANGUAGE FlexibleContexts #-} | ||
2 | {-# LANGUAGE ViewPatterns #-} | ||
3 | {-# LANGUAGE OverloadedStrings #-} | ||
4 | |||
5 | module Thermoprint.Server.Printer.Generic | ||
6 | ( genericPrint | ||
7 | ) where | ||
8 | |||
9 | import Thermoprint.Printout | ||
10 | import Thermoprint.API (PrintingError(IOError)) | ||
11 | import Thermoprint.Server.Printer | ||
12 | |||
13 | import System.FileLock | ||
14 | import System.IO | ||
15 | |||
16 | import Data.Sequence (Seq, (|>), (<|), ViewL(..), viewl) | ||
17 | import qualified Data.Sequence as Seq | ||
18 | |||
19 | import qualified Data.ByteString.Lazy as Lazy (ByteString) | ||
20 | import qualified Data.ByteString.Lazy as LBS | ||
21 | import qualified Data.ByteString.Lazy.Char8 as CLBS | ||
22 | |||
23 | import qualified Data.Text.Lazy as Lazy (Text) | ||
24 | import qualified Data.Text.Lazy as TL | ||
25 | |||
26 | import Data.Encoding | ||
27 | import Data.Encoding.CP437 | ||
28 | import Data.Word | ||
29 | |||
30 | import Control.Monad | ||
31 | import Control.Monad.Reader | ||
32 | import Control.Monad.Trans.State | ||
33 | import Control.Monad.IO.Class | ||
34 | |||
35 | import Control.Monad.Trans.Resource | ||
36 | import Data.Acquire | ||
37 | |||
38 | import Control.Exception.Base (IOException) | ||
39 | import Control.Monad.Catch | ||
40 | |||
41 | import Data.Foldable | ||
42 | |||
43 | import Data.List (intersperse, genericReplicate, intercalate) | ||
44 | |||
45 | import Data.Int (Int64) | ||
46 | |||
47 | import Prelude hiding (mapM_, sequence_) | ||
48 | |||
49 | lockedFile :: FilePath -> Acquire (FileLock, Handle) | ||
50 | lockedFile f = mkAcquire ((,) <$> lockFile f Exclusive <*> openFile f AppendMode) (\(lock, h) -> () <$ hClose h <* unlockFile lock) | ||
51 | |||
52 | withLockedFile :: (MonadIO m, MonadMask m) => FilePath -> (Handle -> m a) -> m a | ||
53 | withLockedFile path f = withEx (lockedFile path) (f . snd) | ||
54 | |||
55 | genericPrint :: FilePath -> PrinterMethod | ||
56 | genericPrint path = PM $ flip catches handlers . withLockedFile path . print | ||
57 | where | ||
58 | print printout handle = liftIO $ runReaderT (bracket_ initialize (render printout) finalize) handle >> return Nothing | ||
59 | handlers = [ Handler $ return . Just . IOError . (show :: IOException -> String) | ||
60 | , Handler $ return . Just . EncError | ||
61 | ] | ||
62 | |||
63 | type Render = ReaderT Handle IO () | ||
64 | |||
65 | encode' :: ByteSink m => String -> m () | ||
66 | encode' = encode CP437 | ||
67 | |||
68 | width :: Int64 | ||
69 | width = 32 | ||
70 | |||
71 | esc :: Word8 | ||
72 | esc = 27 | ||
73 | |||
74 | escSequence :: [Word8] -> Render | ||
75 | escSequence = mapM_ pushWord8 . (esc:) | ||
76 | |||
77 | initialize :: Render | ||
78 | initialize = replicateM_ 2 $ escSequence [64] | ||
79 | |||
80 | finalize :: Render -- TODO: adjust this to produce proper padding | ||
81 | finalize = encode' "\n\n\n" | ||
82 | |||
83 | intersperse' :: b -> (a -> b) -> Seq a -> Seq b | ||
84 | intersperse' _ _ (viewl -> EmptyL) = Seq.empty | ||
85 | intersperse' _ f (viewl -> x :< (viewl -> EmptyL)) = Seq.singleton $ f x | ||
86 | intersperse' b f (viewl -> x :< xs) = f x <| b <| intersperse' b f xs | ||
87 | |||
88 | render :: Printout -> Render | ||
89 | render = sequence_ . intersperse' (encode' "\n\n") renderPar | ||
90 | |||
91 | renderPar :: Paragraph -> Render | ||
92 | renderPar = mapM_ renderChunk | ||
93 | where | ||
94 | renderChunk (Raw bs) = mapM_ pushWord8 $ LBS.unpack bs | ||
95 | renderChunk (Cooked block) = renderBlock block | ||
96 | |||
97 | renderBlock :: Block -> Render | ||
98 | renderBlock (VSpace n) = encode' $ genericReplicate n '\n' | ||
99 | renderBlock (NewlSep xs) = sequence_ intercalate (encode' "\n") . map renderBlock . toList $ xs | ||
100 | renderBlock (Line x) = undefined | ||