shithub: monome

Download patch

ref: 525191c99f034ed807d670fabf634e36d36937d6
parent: ce0c4507d6c7e6460ed098e8a364194ea69b0e72
author: glenda <glenda@9front.local>
date: Thu Nov 26 23:01:37 EST 2020

selection program

--- a/monome.c
+++ b/monome.c
@@ -26,6 +26,8 @@
 			draw(screen, rectaddpt(buttons[i][j], screen->r.min),
 				buttonstate[i][j]? display->white: display->black,
 				nil, ZP);
+
+	flushimage(display, 1);
 }
 
 void
--- /dev/null
+++ b/select.c
@@ -1,0 +1,110 @@
+#include <u.h>
+#include <libc.h>
+#include <draw.h>
+#include <event.h>
+
+Image *back;
+int fd;
+Rectangle buttons[8][2];
+char saved[8];
+char **states;
+
+void
+redraw(Image *screen)
+{
+	int i;
+
+	draw(screen, screen->r, back, nil, ZP);
+
+	for(i = 0; i < 8; i++){
+		draw(screen, buttons[i][0], saved[i]? display->white: display->black, nil, ZP);
+		draw(screen, buttons[i][1], display->black, nil, ZP);
+	}
+
+	flushimage(display, 1);
+}
+
+void
+resize(Image *screen)
+{
+	int s, w, h;
+	int x, y;
+
+	x = screen->r.min.x;
+	y = screen->r.min.y;
+
+	w = Dx(screen->r)/8;
+	h = Dy(screen->r)/2;
+
+	for (s = 0; s < 8; s++) {
+		buttons[s][0] = insetrect(Rect(x + w*s, y, x + w*(s+1), y+h), 3);
+		buttons[s][1] = insetrect(Rect(x + w*s, y+h, x + w*(s+1), y+2*h), 3);
+	}
+}
+
+void
+save(int i)
+{
+	if (readn(fd, states[i], 6*16+2) != (6*16+2))
+		sysfatal("readn: %r");
+
+	saved[i] = 1;
+}
+
+void
+restore(int i)
+{
+	if (saved[i] == 0)
+		return;
+
+	write(fd, states[i], 6*16+2);
+}
+
+void
+eresized(int new)
+{
+	if(new && getwindow(display, Refnone) < 0)
+		fprint(2,"can't reattach to window");
+	resize(screen);
+	redraw(screen);
+}
+
+void
+main(int argc, char **argv)
+{
+	Mouse m;
+	Point xy;
+	int i;
+
+	fd = open("/mnt/monome/buttons", ORDWR);
+	if(fd < 0)
+		sysfatal("open /mnt/monome/buttons: %r");
+
+	if(initdraw(0, 0, "select") < 0)
+		sysfatal("initdraw: %r");
+
+	back = allocimage(display, Rect(0,0,1,1), CMAP8, 1, 0x777777FF);
+	if (back == nil)
+		sysfatal("allocimage: %r");
+
+	states = malloc(8 * sizeof(char*));
+	for(i = 0; i < 8; i++)
+		states[i] = calloc(1, 6*16+2);
+	memset(saved, '\0', 8);
+
+	einit(Emouse);
+	resize(screen);
+	redraw(screen);
+
+	for(;;m = emouse()){
+		if(m.buttons & 1){
+			for (i = 0; i < 8; i++) {
+				if (ptinrect(m.xy, buttons[i][0]))
+					save(i);
+				if (ptinrect(m.xy, buttons[i][1]))
+					restore(i);
+			}
+			redraw(screen);
+		}
+	}	
+}