sim

the sim text editor
git clone git://ssnf.xyz/sim
Log | Files | Refs | README

posix.c (775B)


      1 #include <stdio.h>
      2 
      3 #include <sys/ioctl.h>
      4 #include <signal.h>
      5 #include <termios.h>
      6 
      7 #include "sim.h"
      8 
      9 static void handle(int);
     10 
     11 static struct termios initial_state;
     12 
     13 extern void
     14 win_end(void)
     15 {
     16 	printf(ED CSI "H");
     17 	tcsetattr(0, TCSAFLUSH, &initial_state);
     18 }
     19 
     20 extern void
     21 win_init(Window* w)
     22 {
     23 	struct termios raw_state;
     24 
     25 	tcgetattr(0, &initial_state);
     26 	raw_state = initial_state;
     27 	raw_state.c_lflag &= ~(ECHO|ICANON|ISIG);
     28 	tcsetattr(0, TCSAFLUSH, &raw_state);
     29 	setbuf(stdout, NULL);
     30 	printf(CSI "H\t" CSI "6n");
     31 	scanf("\x1b[%*d;%huR", &w->t);
     32 	--w->t;
     33 	handle(SIGWINCH);
     34 }
     35 
     36 extern void
     37 win_query(Window* w)
     38 {
     39 	struct winsize ws;
     40 
     41 	ioctl(0, TIOCGWINSZ, &ws);
     42 	w->wx = ws.ws_col;
     43 	w->wy = ws.ws_row;
     44 }
     45 
     46 static void
     47 handle(int sig)
     48 {
     49 	resize();
     50 	signal(SIGWINCH, handle);
     51 }