summaryrefslogtreecommitdiff
path: root/graphics.c
diff options
context:
space:
mode:
authorPeter Mikkelsen <petermikkelsen10@gmail.com>2024-02-11 00:04:31 +0000
committerPeter Mikkelsen <petermikkelsen10@gmail.com>2024-02-11 00:04:31 +0000
commit7c6a945996a1d5510ff1412320ac7d07a0f82851 (patch)
treef77259f26b5c33307ba8ebc670951ed0acce1b00 /graphics.c
parentb5a96b97ee2f3fd30e5935d2b3c2c6e0d96c4640 (diff)
Start working on it
Diffstat (limited to 'graphics.c')
-rw-r--r--graphics.c123
1 files changed, 123 insertions, 0 deletions
diff --git a/graphics.c b/graphics.c
new file mode 100644
index 0000000..7339d8f
--- /dev/null
+++ b/graphics.c
@@ -0,0 +1,123 @@
+#include <u.h>
+#include <libc.h>
+#include <thread.h>
+#include <draw.h>
+#include <mouse.h>
+#include <keyboard.h>
+
+#include "guifs.h"
+
+Mousectl *mouse;
+Keyboardctl *keyboard;
+Channel *updatechan;
+
+void
+drawgui(GuiElement *g)
+{
+ GuiSpec spec = guispecs[g->type];
+ spec.draw(g);
+ for(int i = 0; i < g->nchildren; i++)
+ drawgui(g->children[i]);
+}
+
+void
+drawnone(GuiElement *g)
+{
+ Image *bg = getprop(g, Pbackground).colour->image;
+ draw(screen, g->rect, bg, nil, ZP);
+}
+
+void
+drawcontainer(GuiElement *g)
+{
+ Image *bg = getprop(g, Pbackground).colour->image;
+ draw(screen, g->rect, bg, nil, ZP);
+}
+
+Colour *
+mkcolour(ulong c)
+{
+ lockdisplay(display);
+ Colour *col = emalloc(sizeof(Colour));
+ col->image = allocimage(display, Rect(0,0,1,1), screen->chan, 1, c);
+ col->code = c;
+ unlockdisplay(display);
+
+ return col;
+}
+
+void
+updategui(int new)
+{
+ /* Trigger a call to resized by sending a message */
+ send(updatechan, &new);
+}
+
+void
+resized(int new)
+{
+ if(new && getwindow(display, Refnone) < 0)
+ sysfatal("can't reattach to window: %r");
+
+ if(root != nil){
+ layout(root, screen->r);
+ drawgui(root);
+ flushimage(display, 1);
+ }
+}
+
+void
+guiproc(void *)
+{
+ int i;
+ if(initdraw(nil, nil, "guifs") < 0)
+ sysfatal("initdraw failed");
+ display->locking = 1;
+
+ if((mouse = initmouse(nil, screen)) == nil)
+ sysfatal("initmouse failed");
+ if((keyboard = initkeyboard(nil)) == nil)
+ sysfatal("initkeyboard failed");
+
+ enum {
+ Aupdategui,
+ Aresize,
+ Amouse,
+ Aaltend,
+ };
+ Alt a[] = {
+ [Aupdategui] =
+ {updatechan, &i, CHANRCV},
+ [Aresize] =
+ {mouse->resizec, nil, CHANRCV},
+ [Amouse] =
+ {mouse->c, &mouse->Mouse, CHANRCV},
+ [Aaltend] =
+ {nil, nil, CHANEND},
+ };
+
+ while(1){
+ unlockdisplay(display);
+ int which = alt(a);
+ lockdisplay(display);
+
+ switch(which){
+ case Aupdategui:
+ resized(i);
+ break;
+ case Aresize:
+ resized(1);
+ break;
+ case Amouse:
+ break;
+ }
+ }
+}
+
+void
+initgraphics(void)
+{
+ updatechan = chancreate(sizeof(int), 0);
+ proccreate(guiproc, nil, mainstacksize);
+ updategui(1);
+}