3 * A tiled window manager for the terminal.
5 * It provides a virtual screen that can be scrolled horizontally and may carry
6 * any number of windows to be appeared, disappeared, resized and moved around.
7 * They have borders and a title bar and are positioned automtaically.
9 * Windows can be any width between 1 and 2^16 cells. The virtual screen grows
10 * with them as needed -- but only horizontally and only up to 2^16 cells. Their
11 * height is limited by the height of the terminal screen (maximum 2^16 cells).
13 * Windows' positioning can be influenced only indirectly: by resizing them, and
14 * by shifting their relative position inside the (currently invisible) chain
15 * that the window manager treats their plurality as. The first window goes into
16 * the top left corner of the virtual screen. Further windows are fitted as
17 * left-aligned as possible below their (chain-wise) closest predecessor that
18 * thrones over enough space to to contain them and is open to the right. If
19 * that fails, they're fitted up-right to the window with the rightmost border.
21 * TODO: Think up a more intuitive window positioning algorithm.
27 #include <stdint.h> /* for uint8_t, uint16_t, uint32_t */
28 #include <ncurses.h> /* for the WINDOW and chtype typedefs */
29 #include "yx_uint16.h" /* for yx_uint16 struct */
33 /* "Win" structs describe windows as frames located inside the virtual screen
34 * pad through which "winmaps" are visible, 2-dimensional maps of ncurses
35 * chtypes. If a winmap is bigger than its frame, scrolling hints will appear at
36 * the proper edges. Win structs are chained into a linked list of all the
37 * windows visible on the virtual screen and also contain pointers to what
38 * content is to be drawn inside the window, and by use of what method.
42 struct Win * prev; /* chain pointers; if 0, they mark the start or end */
43 struct Win * next; /* of the chain; if both are 0, Win is outside chain */
44 struct yx_uint16 framesize; /* window frame size to see winmap through */
45 struct yx_uint16 start; /* upper left corner of window in pad */
46 struct yx_uint16 center; /* winmap cell to center frame on if smaller*/
47 char * title; /* title to be used in window title bar */
48 void (* draw) (struct Win *); /* function that draws/updates the winmap */
49 chtype * winmap; /* sequence of cells, sorted into lines ... */
50 struct yx_uint16 winmapsize; /* ... with these geometry infos */
53 /* The window manager's parent struct WinMeta contains the virtual screen,
54 * relates it to the terminal screen and anchors the chain of visible windows.
58 WINDOW * screen; /* ncurses' pointer to the terminal screen */
59 WINDOW * pad; /* ncurses pad of virtual screen */
60 uint16_t pad_offset; /* number of cells view is moved to the right */
61 struct yx_uint16 padsize; /* virtual screen size */
62 struct Win * chain_start; /* first Win in chain; its _prev == 0 */
63 struct Win * chain_end; /* last Win in chain; its _next == 0 */
64 struct Win * active; /* Win highlighted/selected for manipulation */
69 /* Initialize empty "wmeta" on terminal "screen". All struct members are
70 * initialized to 0, except for the newly created virtual screen "pad" and its
71 * size (height: that of the terminal screen; width: 1).
73 extern void init_win_meta(WINDOW * screen);
75 /* Initialize a window child of "wmeta" to "title", "height" and "width" and
76 * appoint "func"() as its .draw to draw the winmap when it's visible. Other
77 * members of the Win struct pointed to by "wp" are initialized to 0.
79 * Pass 0 for "width" to make the window as wide as the terminal screen. Pass 0
80 * for "height" for the maximum allowed height: one cell smaller than that of
81 * the terminal screen. Pass negative values for either of them to make the
82 * window width/height so many cells smaller than what 0 would set. The maximum
83 * allowed height is also applied for positive height values that exceed it or
84 * negative values that would reduce the window height to less than 1 cell.
86 * Other members of the Win struct are initialized to 0.
88 extern void init_win(struct Win ** wp, char * title, int16_t height,
89 int16_t width, void * func);
91 /* Free allocated memory for an initialized Win / WinMeta structs. */
92 extern void free_winmeta();
93 extern void free_win(struct Win * win);
95 /* Append/suspend window "w" to/from chain of visible windows below "wmeta".
96 * Appended windows will become active. Suspended active windows will move the
97 * active window selection to their successor in the window chain or, failing
98 * that, their predecessor, or, failing that, to 0 (no window being active).
100 extern void append_win(struct Win * w);
101 extern void suspend_win(struct Win * w);
103 /* Apply scrolling offset "new_offset" to virtual screen if it is equal/greater
104 * 0 and does not push the view (further) beyond the virtual screen's border. If
105 * the view is already beyond the virtual screen's border due to it having
106 * shrunk after suspension of windows, only allow screen scrolling leftwards.
108 extern void reset_pad_offset(uint16_t new_offset);
110 /* Apply "size" to the active window if it provides a minimum size of 1x1 cells
111 * and is in height at least one cell smaller than the screen's vertical height
112 * (to provide space for the title bar). Does nothing if no window is active.
114 extern void resize_active_win(struct yx_uint16 size);
116 /* Cycle active window selection forwards ("dir" == "f") or backwards (any
117 * other "dir"). Wrap around in the windows chain if start / end of it is met.
118 * Does nothing if no window is active.
120 extern void cycle_active_win(char dir);
122 /* Move active window forwards ("dir" == "f") or backwards (any other "dir") in
123 * the window chain. Wrap around in the window chain if start / end of it is
124 * met. Does nothing if no window is active.
126 extern void shift_active_win(char dir);
128 /* Draw virtual screen including all windows. Also add scroll hints for where
129 * the edges of the terminal screen hit non-edges of and inside the virtual
130 * screen. Then update the terminal screen.
132 extern void draw_all_wins();