]> git.corax.cc Git - mwm/commitdiff
monitor: Add data type for monitor management
authorMatthias Kruk <m@m10k.eu>
Sun, 2 May 2021 00:51:26 +0000 (09:51 +0900)
committerMatthias Kruk <m@m10k.eu>
Sun, 2 May 2021 00:51:26 +0000 (09:51 +0900)
To keep track of connected monitors, a data type is necessary.
This commit adds the monitor datatype that is used to keep track
of the size and position of a monitor.

monitor.c [new file with mode: 0644]
monitor.h [new file with mode: 0644]

diff --git a/monitor.c b/monitor.c
new file mode 100644 (file)
index 0000000..9a618fc
--- /dev/null
+++ b/monitor.c
@@ -0,0 +1,101 @@
+#include <stdlib.h>
+#include <string.h>
+#include <errno.h>
+#include "monitor.h"
+
+struct monitor {
+       int id;
+       int x;
+       int y;
+       int w;
+       int h;
+};
+
+int monitor_new(int id, int x, int y, int w, int h,
+               struct monitor **monitor)
+{
+       struct monitor *mon;
+
+       if(!monitor) {
+               return(-EINVAL);
+       }
+
+       mon = malloc(sizeof(*mon));
+
+       if(!mon) {
+               return(-ENOMEM);
+       }
+
+       mon->id = id;
+       mon->x = x;
+       mon->y = y;
+       mon->w = w;
+       mon->h = h;
+
+       *monitor = mon;
+
+       return(0);
+}
+
+int monitor_free(struct monitor **monitor)
+{
+       if(!monitor) {
+               return(-EINVAL);
+       }
+
+       if(!*monitor) {
+               return(-EALREADY);
+       }
+
+       free(*monitor);
+       *monitor = NULL;
+
+       return(0);
+}
+
+int monitor_get_id(struct monitor *monitor)
+{
+       if(!monitor) {
+               return(-EINVAL);
+       }
+
+       return(monitor->id);
+}
+
+int monitor_get_geometry(struct monitor *monitor,
+                        int *x, int *y, int *w, int *h)
+{
+       if(!monitor) {
+               return(-EINVAL);
+       }
+
+       if(x) {
+               *x = monitor->x;
+       }
+       if(y) {
+               *y = monitor->y;
+       }
+       if(w) {
+               *w = monitor->w;
+       }
+       if(h) {
+               *h = monitor->h;
+       }
+
+       return(0);
+}
+
+int monitor_set_geometry(struct monitor *monitor,
+                        int x, int y, int w, int h)
+{
+       if(!monitor) {
+               return(-EINVAL);
+       }
+
+       monitor->x = x;
+       monitor->y = y;
+       monitor->w = w;
+       monitor->h = h;
+
+       return(0);
+}
diff --git a/monitor.h b/monitor.h
new file mode 100644 (file)
index 0000000..04f32bd
--- /dev/null
+++ b/monitor.h
@@ -0,0 +1,16 @@
+#ifndef MONITOR_H
+#define MONITOR_H 1
+
+struct monitor;
+
+int monitor_new(int id, int x, int y, int w, int h,
+               struct monitor **monitor);
+int monitor_free(struct monitor **monitor);
+
+int monitor_get_id(struct monitor *monitor);
+int monitor_get_geometry(struct monitor *monitor,
+                        int *x, int *y, int *w, int *h);
+int monitor_set_geometry(struct monitor *monitor,
+                        int x, int y, int w, int h);
+
+#endif /* MONITOR_H */