3 * A tiled window manager for the terminal.
5 * It provides a virtual screen that can be scrolled horizontally and may
6 * contain any number of windows that can be appeared, disappeared, resized and
7 * (somewhat) moved around. They have borders and a title bar and are positioned
8 * (in a bizarre fashion, see below) automatically.
10 * Windows can be almost any width (number has to fit into 16 bits); the virtual
11 * screen grows with them as needed -- but only horizontally. Their height is
12 * limited by the height of the terminal screen.
14 * Positioning of windows can only indirectly be influenced: by resizing them,
15 * and by shifting their relative position inside the (currently invisible)
16 * chain that the window manager treats their plurality as. The first window
17 * goes into the upper left corner of the virtual screen. Further visible
18 * windows are fitted left-aligned below their (chain-wise) closest predecessor
19 * that thrones over enough space to fit them in; failing that, they are placed
20 * to the right of the window with the rightmost border.
22 * TODO: Think up a more intuitive window positioning algorithm or at least make
23 * the chain that windows are positioned by visible.
25 * TODO: Ensure there are only windows as many / as big as fit into the maximum
26 * size of the virtual screen.
34 #include <stdint.h> /* for uint16_t, uint32_t */
35 #include <ncurses.h> /* for the WINDOW typedef */
36 #include "yx_uint16.h" /* for yx_uint16 coordinates */
39 /* Individual windows consist of potential (real if window is visible) ncurses
40 * WINDOWs wrapped inside Frame structs (that keep a window's designated size
41 * even when it is invisible) wrapped inside metadata-rich Win structs. Win
42 * structs are chained into a linked list of all the windows visible on the
43 * virtual screen and also contain pointers to what content is to be drawn
44 * inside the window, and by use of what method.
50 struct yx_uint16 size; /* designated size of curses_win */
55 struct Win * prev; /* prev=next=0 if Win is outside the chain */
57 struct yx_uint16 start; /* upper left corner (of WINDOW or border?) */
59 char * title; /* title to be shown on window border top */
60 void (* draw) (struct Win *); /* function to draw window content ("data") */
61 void * data; /* content to be drawn; draw() knows how */
66 /* The window manager's parent struct WinMeta provides the virtual screen and a
67 * representation of the terminal screen. It also anchors start and end of the
72 WINDOW * screen; /* terminal screen */
73 uint16_t pad_offset; /* number of cells view is moved to the right */
74 struct Frame pad; /* virtual screen */
75 struct Win * chain_start; /* first Win, whose .prev shall point to 0 */
76 struct Win * chain_end; /* last Win, whose .next shall point to 0 */
77 struct Win * active; /* window highlighted/selected for manipulation */
82 /* Create on the terminal "screen" an empty WinMeta whose virtual screen at the
83 * beginning is sized just like the terminal screen. Note that emptiness is
84 * marked by WinMeta.chain_start=0. Other struct values are also initialized 0.
86 * TODO: Why is an *empty* virtual screen's size that of the terminal screen?
88 extern struct WinMeta init_win_meta(WINDOW * screen);
92 /* Create a window below inside "wmeta" titled "title" and appointing "func"()
93 * to interpret and draw the content stored at "data" if the window is visible.
95 * The start size for the Frame will be a width of 20 cells and a height one
96 * less than the height of the virtual screen (so as to fit the title bar on top
97 * of the window). Other values will be initialized to 0. The window will stay
98 * invisible until appended to the chain of visible windows via append_win().
100 * TODO: Why a default start width instead of passing a start width?
102 extern struct Win init_win(struct WinMeta * wmeta, char * title,
103 void * data, void * func);
107 /* Append/suspend window "w" to/from chain of visible windows in "wmeta".
108 * Appended windows will become active. Suspended active windows will move the
109 * active window selection to their successor in the window chain or, failing
110 * that, their predecessor; if no window remains, none will be active.
112 extern void append_win(struct WinMeta * wmeta, struct Win * w);
113 extern void suspend_win(struct WinMeta * wmeta, struct Win * w);
117 /* Apply scrolling offset "new_offset" to virtual screen if it is sane, i.e.
118 * it's equal/greater zero and does not push the view (further) beyond the
119 * virtual screen's border. If the view is already beyond the virtual screen's
120 * border due to it having shrunk after suspension of windows, only allow view
121 * movement leftwards.
123 extern void reset_pad_offset(struct WinMeta * wmeta, uint16_t new_offset);
127 /* Apply new size "size" to the active window, but only if it provides for at
128 * least one cell width/height and is in height at least one cell smaller than
129 * the screen's vertical height (to provide space for the title bar).
131 extern void resize_active_win(struct WinMeta * wmeta, struct yx_uint16 size);
135 /* Cycle active window selection forwards (set dir="n") or backwards. Wrap
136 * around in the windows chain if start / end of it is met.
138 extern void cycle_active_win(struct WinMeta * wmeta, char dir);
142 /* Move active window forwards (set dir="f") or backwards (set dir="b"). Wrap
143 * around in the window chain if start / end of it is met.
145 extern void shift_active_win(struct WinMeta * wmeta, char dir);
149 /* Draw virtual screen including all windows. Also add scroll hints (a line
150 * stating that there is more to see on scrolling further into a certain
151 * direction) for where the edges of the terminal screen hit non-edges of and
152 * inside the virtual screen. Then update the terminal screen.
154 extern void draw_all_wins(struct WinMeta * wmeta);
158 /* Draw scroll hint (a line stating that there is more to see on scrolling
159 * further into a certain direction) into "frame" at position "pos" (describing
160 * a column or a row dependent on "dir" being *either* "<"/">" *or* something
161 * else). It will consist of a line of "dir" symbols bracketing a descriptive
162 * text stating the number of rows/columns further available beyond the hint.
164 extern void draw_scroll_hint(struct Frame * frame, uint16_t pos, uint32_t dist,