git home / emma home
logo

twn

Minimal terminal multiplexer without curses.
git clone https://git.y1.nz/archives/twn.tar.gz
README | Files | Log | Refs | LICENSE

main.c


      1 #include <stdio.h> /* for like everything. */
      2 #include <signal.h> /* for signal, SIGCHLD, and SIG_IGN */
      3 
      4 #include "window.h"
      5 #include "config.h"
      6 #include "direction.h"
      7 #include "term.h"
      8 
      9 int
     10 init(Window *root)
     11 {
     12 	int w, h;
     13 
     14 	/* automatically reap children */
     15 	signal(SIGCHLD, SIG_IGN); 
     16 
     17 	/* CSI ED: clear whole screen */
     18 	fprintf(stdout, "\033[2J");
     19 
     20 	/* determine base terminal's bounds */
     21 	if (!get_root_size(&w, &h)) {
     22 		fprintf(stderr, "Fatal: failed to measure base terminal :(" "\n");
     23 		return 1;
     24 	}
     25 
     26 	/* init root window */
     27 	root->l = 1; root->u = 1; root->r = w; root->d = h;
     28 	root->pl = ROOT_LEFT_PAD; root->pu = ROOT_UP_PAD;
     29 	root->pr = ROOT_RIGHT_PAD; root->pd = ROOT_DOWN_PAD;
     30 	root->child1 = NULL;
     31 	root->child2 = NULL;
     32 	root->parent = NULL;
     33 	root->t = NULL;
     34 
     35 	render(root, root);
     36 }
     37 
     38 void
     39 clean_up()
     40 {
     41 	/* move to the end of the screen */
     42 	printf("\033[9999;9999H");
     43 
     44 	/* reset bg */
     45 	printf("\033[49m");
     46 
     47 	/* print a cute message! */
     48 	printf("\n" "ALLL done :3" "\n");
     49 }
     50 
     51 int
     52 main(int argc, char **argv)
     53 {
     54 	Window _root; Window *root = &_root;
     55 	int result;
     56 
     57 	result = init(root);
     58 	if (result > 0) return result;
     59 
     60 	Window *focus = root;
     61 	Window *next;
     62 	char ch;
     63 	for (;;) {
     64 		ch = fgetc(stdin);
     65 		switch (ch) {
     66 		case 'h': next = find_next_window(root, focus, left); focus = next == NULL ? focus : next; break;
     67 		case 'k': next = find_next_window(root, focus, up); focus = next == NULL ? focus : next; break;
     68 		case 'l': next = find_next_window(root, focus, right); focus = next == NULL ? focus : next; break;
     69 		case 'j': next = find_next_window(root, focus, down); focus = next == NULL ? focus : next; break;
     70 		case 'H': focus = split_window(focus, left); break;
     71 		case 'K': focus = split_window(focus, up); break;
     72 		case 'L': focus = split_window(focus, right); break;
     73 		case 'J': focus = split_window(focus, down); break;
     74 		case 'q': focus = close_window(root, focus); break;
     75 		default: goto end;
     76 		}
     77 		if (focus == NULL) goto end;
     78 		render(root, focus);
     79 	}
     80 
     81 end:
     82 	clean_up();
     83 	printf("Terminating char was %d (%c)", (int) ch, ch);
     84 }

This webpage is intended to be an accessible preview of this repository. To get a fuller picture, clone it and use the git CLI.