Created
February 13, 2014 16:07
-
-
Save nc6/8977936 to your computer and use it in GitHub Desktop.
Bindings to ioctl for setting/getting window size.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# LANGUAGE ForeignFunctionInterface #-} | |
{-# LANGUAGE EmptyDataDecls #-} | |
#include <sys/ttycom.h> | |
#include <sys/ioctl.h> | |
#c | |
typedef struct winsize winsize; | |
#endc | |
module Tabula.TTY.Foreign where | |
import Control.Applicative | |
import Control.Monad (liftM) | |
import Foreign.Storable | |
import Foreign.Ptr | |
import Foreign.C | |
import System.Posix.IOCtl | |
{#enum define TIO {TIOCGWINSZ as GetWinsz, TIOCSWINSZ as SetWinsz} deriving (Eq) #} | |
data WindowSize = WindowSize { | |
ws_row :: Int | |
, ws_col :: Int | |
, ws_xpixel :: Int | |
, ws_ypixel :: Int | |
} | |
instance Storable WindowSize where | |
sizeOf _ = {#sizeof winsize#} | |
alignment _ = {#alignof winsize#} | |
peek p = WindowSize | |
<$> liftM fromIntegral ({#get winsize->ws_row #} p) | |
<*> liftM fromIntegral ({#get winsize->ws_col #} p) | |
<*> liftM fromIntegral ({#get winsize->ws_xpixel #} p) | |
<*> liftM fromIntegral ({#get winsize->ws_ypixel #} p) | |
poke p x = do | |
{#set winsize.ws_row #} p (fromIntegral $ ws_row) | |
{#set winsize.ws_col #} p (fromIntegral $ ws_col) | |
{#set winsize.ws_xpixel #} p (fromIntegral $ ws_xpixel) | |
{#set winsize.ws_ypixel #} p (fromIntegral $ ws_ypixel) | |
data TIOCGWINSZ | |
instance IOControl TIOCGWINSZ WindowSize where | |
ioctlReq = GetWinsz | |
data TIOCSWINSZ | |
instance IOControl TIOCSWINSZ WindowSize where | |
ioctlReq = SetWinsz |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The
{#enum define}
in this doesn't work, unfortunately. I moved to using hsc2hs instead.