Search code examples
haskellsignalsposix

How to handle SIGWINCH in haskell?


Hi I'm new to Haskell and been working on some Haskell code to handle signals on POSIX system.

Especially I want to handle SIGWINCH with Haskell

I googled about them and saw some documents on Hackage but I'm not pretty sure I can do something with it.

Then I found some post about mixing with C code like this below

#include <stdio.h>
#include <unistd.h>
#include <signal.h>

static void sig_handler(int sig) {
    if (SIGWINCH == sig) {
        printf("you resized terminal\n");
    }
}

int main() {
    signal(SIGWINCH, sig_handler);
    while (1) {
        pause();
    }

    return 0;
}

but I'm curious about something more in 'Haskell way', without any C codes.

I found an example about handling SIGINT signal but couldn't find anything about SIGWINCH.

How can I handle it in Haskell?


Solution

  • The following should allow you to install a handler to handle the (non-standard) SIGWINCH POSIX signal:

    import System.Posix.Signals
    import System.Posix.Signals.Exts
    
    main :: IO ()
    main = do
      installHandler sigWINCH (Catch handler) Nothing
    

    where handler is an action of type IO ().

    Happy Haskelling!