aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--wm.c71
-rw-r--r--wm.h30
2 files changed, 101 insertions, 0 deletions
diff --git a/wm.c b/wm.c
new file mode 100644
index 0000000..2b7270c
--- /dev/null
+++ b/wm.c
@@ -0,0 +1,71 @@
+#include "wm.h"
+
+#include <ncurses.h>
+#include <string.h>
+
+#include "err.h"
+#include "mem.h"
+
+window *wm_create_new_w(terminal *t, const char *name, int height, int width,
+ int start_y, int start_x) {
+ if (t->w_count >= WINDOWS_MAX) {
+ die("Windows count has arrived max");
+ }
+
+ WINDOW *win;
+ win = derwin(t->windows[0]->win, height, width, start_y, start_x);
+
+ int current_idx = t->w_count;
+
+ strncpy(t->windows[current_idx]->name, name, 31);
+ t->windows[current_idx]->win = win;
+ t->windows[current_idx]->start_y = start_y;
+ t->windows[current_idx]->start_x = start_x;
+ t->windows[current_idx]->width = width;
+ t->windows[current_idx]->height = height;
+
+ t->w_count++;
+
+ return t->windows[current_idx];
+}
+
+terminal *wm_init(void) {
+ terminal *t = xmalloc(sizeof(terminal));
+ t->windows = xmalloc(sizeof(window *) * WINDOWS_MAX);
+ for (int i = 0; i < WINDOWS_MAX; i++) {
+ t->windows[i] = xmalloc(sizeof(window));
+ }
+ t->w_count = 0;
+ getmaxyx(stdscr, t->maxy, t->maxx);
+ t->windows[0]->win = newwin(t->maxy, t->maxx, 0, 0);
+ t->windows[t->w_count]->start_y = 0;
+ t->windows[t->w_count]->start_x = 0;
+ t->windows[t->w_count]->height = t->maxy;
+ t->windows[t->w_count]->width = t->maxx;
+ t->w_count++;
+
+ return t;
+}
+
+int wm_fd_w_by_name(terminal *t, const char *name) {
+ for (int i = 0; i < t->w_count; i++) {
+ if (strcmp(t->windows[i]->name, name) == 0) continue;
+ return i;
+ }
+
+ return -1;
+}
+
+void wm_refresh_all(terminal *t) {
+ if (t->windows[0] && t->windows[0]->win) {
+ touchwin(t->windows[0]->win);
+ }
+
+ for (int i = 1; i < t->w_count; i++) {
+ if (t->windows[i] && t->windows[i]->win) {
+ wnoutrefresh(t->windows[i]->win);
+ }
+ }
+
+ doupdate();
+}
diff --git a/wm.h b/wm.h
new file mode 100644
index 0000000..ebef53a
--- /dev/null
+++ b/wm.h
@@ -0,0 +1,30 @@
+#ifndef SF_WM_H
+#define SF_WM_H
+
+#include <ncurses.h>
+
+#define WINDOWS_MAX 15
+
+typedef struct {
+ WINDOW *win;
+ char name[32];
+ int height;
+ int width;
+ int start_x;
+ int start_y;
+} window;
+
+typedef struct {
+ window **windows; /* windows[0] is root_win */
+ int w_count; /* index of windows */
+ int maxy;
+ int maxx;
+} terminal;
+
+terminal *wm_init(void);
+window *wm_create_new_w(terminal *t, const char *name, int width, int height,
+ int start_y, int start_x);
+int wm_fd_w_by_name(terminal *t, const char *name);
+void wm_refresh_all(terminal *t);
+
+#endif /* SF_WM_H */