Skip to content

Instantly share code, notes, and snippets.

@ben-cohen
Last active July 22, 2022 20:13
Show Gist options
  • Save ben-cohen/2a31fba7b9b954c47053810f06d31232 to your computer and use it in GitHub Desktop.
Save ben-cohen/2a31fba7b9b954c47053810f06d31232 to your computer and use it in GitHub Desktop.
// window-size-changed.c: Demo program to handle window changed events
//
// Ben Cohen, July 2022.
//
#include <sys/ioctl.h>
#include <stdio.h>
#include <unistd.h>
#include <signal.h>
int changed = 0;
void handler(int i)
{
changed = 1;
}
int main (int argc, char **argv)
{
signal(SIGWINCH, handler);
while (1)
{
usleep(1000);
if (changed)
{
changed = 0;
struct winsize terminal_size;
ioctl(STDOUT_FILENO, TIOCGWINSZ, &terminal_size);
printf ("rows %d\n", terminal_size.ws_row);
printf ("columns %d\n", terminal_size.ws_col);
printf ("x pixels %d\n", terminal_size.ws_xpixel);
printf ("y pixels %d\n", terminal_size.ws_ypixel);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment