Skip to content

Instantly share code, notes, and snippets.

@nc6
Created February 13, 2014 16:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nc6/8977936 to your computer and use it in GitHub Desktop.
Save nc6/8977936 to your computer and use it in GitHub Desktop.
Bindings to ioctl for setting/getting window size.
{-# 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
@nc6
Copy link
Author

nc6 commented Mar 7, 2014

The {#enum define} in this doesn't work, unfortunately. I moved to using hsc2hs instead.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment