Use libthread model; better abstractions

This commit is contained in:
Jaidyn Ann 2022-03-07 05:16:36 +00:00
parent db8cc7d089
commit 92f53de151
2 changed files with 123 additions and 47 deletions

134
winhat.c
View File

@ -2,6 +2,9 @@
#include <stdio.h> #include <stdio.h>
#include <libc.h> #include <libc.h>
#include <draw.h> #include <draw.h>
#include <thread.h>
#include <mouse.h>
#include <keyboard.h>
#include "winhat.h" #include "winhat.h"
@ -26,7 +29,7 @@ enum {
void void
main(int argc, char* argv[]) threadmain(int argc, char* argv[])
{ {
ARGBEGIN{ ARGBEGIN{
default: default:
@ -34,18 +37,68 @@ main(int argc, char* argv[])
} ARGEND; } ARGEND;
int winid = 0; int winid = 0;
if (argc == 1) if (argc != 1 || (argc == 1 && (winid = atoi(argv[0])) == 0))
winid = atoi(argv[0]);
if (winid <= 0)
usage(); usage();
/* if(initdraw(nil, nil, argv0) < 0) Mousectl* mctl;
Keyboardctl* kctl;
if((mctl = initmouse(nil, screen)) == nil)
sysfatal("%s: %r", argv0);
if((kctl = initkeyboard(nil)) == nil)
sysfatal("%s: %r", argv0);
if(initdraw(nil, nil, argv0) < 0)
sysfatal("%s: %r", argv0); sysfatal("%s: %r", argv0);
draw_own_window();
sleep(30000); */
monitor_window(winid); Rune kbd;
exits(0); Mouse mouse;
int resize[2];
enum{ MOUSE, RESIZE, KEYBD, NONE };
Alt alts[4] = {
{mctl->c, &mouse, CHANRCV},
{mctl->resizec, &resize, CHANRCV},
{kctl->c, &kbd, CHANRCV},
{nil, nil, CHANNOBLK}
};
char path[70];
int len = wctl_path(winid, path);
if(len <= 0)
sysfatal("Weird path error?");
int rect[4] = {0,0,0,0};
int focus = W_NOTCURRENT;
draw_own_window(focus);
int live = 1;
while(live == 1){
switch(alt(alts)){
case RESIZE:
draw_own_window(focus);
break;
case MOUSE:
if(mouse.buttons == 2){
kill_window(path);
}else if(mouse.buttons == 4){
hide_window(path);
hide_window("/mnt/wsys/wctl");
}
break;
case KEYBD:
if(kbd == 'q')
live = 0;
break;
case NONE:
monitor_window(path, rect, &focus);
sleep(100);
break;
default:
sysfatal("This... shouldn't happen. No event.");
break;
}
}
closekeyboard(kctl);
closemouse(mctl);
threadexitsall(nil);
} }
void void
@ -57,19 +110,16 @@ usage(void)
/* You will comply! YOU WILL COMPLY! */ /* You will comply! YOU WILL COMPLY! */
void void
monitor_window(int winId) monitor_window(const char* path, int rect[4], int* focus)
{ {
char path[70]; int status = check_window(path, rect, focus);
int len = wctl_path(winId, path); if(status == -2) {
if(len <= 0) kill_window("/mnt/wsys/wctl");
sysfatal("Weird path error?"); exits(0);
}
int status = 0;
int rect[4] = {0,0,0,0};
int focus = -2;
while((status = check_window(path, rect, &focus)) != -2) {
if(status & W_FOCUS_CHANGED) { if(status & W_FOCUS_CHANGED) {
draw_own_window(*focus);
} }
if(status & W_RECT_CHANGED) { if(status & W_RECT_CHANGED) {
if(resize_own_window(rect) == -1) { if(resize_own_window(rect) == -1) {
@ -78,8 +128,6 @@ monitor_window(int winId)
}else }else
focus_window(path); focus_window(path);
} }
sleep(100);
}
} }
/* Checks if the window's position or focus has changed /* Checks if the window's position or focus has changed
@ -124,6 +172,26 @@ focus_window(const char* path)
} }
} }
void
kill_window(const char* path)
{
int wctl_fd = open(path, OWRITE);
if(wctl_fd >= 0) {
fprint(wctl_fd, "delete\n");
close(wctl_fd);
}
}
void
hide_window(const char* path)
{
int wctl_fd = open(path, OWRITE);
if(wctl_fd >= 0) {
fprint(wctl_fd, "hide\n");
close(wctl_fd);
}
}
int int
resize_own_window(int rect[4]) resize_own_window(int rect[4])
{ {
@ -138,21 +206,27 @@ resize_own_window(int rect[4])
fprint(wctl_fd, "resize -r %d %d %d %d\n", fprint(wctl_fd, "resize -r %d %d %d %d\n",
rect[LEFT], rect[TOP] - 20, rect[RIGHT], rect[TOP]+20); rect[LEFT], rect[TOP] - 20, rect[RIGHT], rect[TOP]+20);
close(wctl_fd); close(wctl_fd);
draw_own_window();
return 0; return 0;
} }
void void
draw_own_window(void) draw_own_window(int focus)
{ {
Image* img = allocimage(display, Rect(0, 0, 100, 100), RGB24, 1, if(getwindow(display, Refnone) < 0)
DGreygreen); sysfatal("%s: %r", argv0);
// DGreygreen active, DGreyblue inactive
if(img == nil) int bg_color = DGreygreen;
if (focus == W_NOTCURRENT)
bg_color = DPalegreygreen;
Image* bg = allocimage(display, Rect(0, 0, 1, 1), RGB24, 1, bg_color);
if(bg == nil)
sysfatal("Couldn't make an Image-- you doing alright, friend?"); sysfatal("Couldn't make an Image-- you doing alright, friend?");
draw(screen, screen->r, img, nil, ZP);
freeimage(img); draw(screen, screen->r, bg, nil, ZP);
freeimage(bg);
flushimage(display, Refnone); flushimage(display, Refnone);
} }

View File

@ -1,13 +1,15 @@
void main(int argc, char** argv); void main(int argc, char** argv);
void usage(void); void usage(void);
void monitor_window(int winId); void monitor_window(const char* path, int rect[4], int* focus);
int check_window(const char* path, int rect[4], int* focus); int check_window(const char* path, int rect[4], int* focus);
void focus_window(const char* path); void focus_window(const char* path);
void kill_window(const char* path);
void hide_window(const char* path);
int resize_own_window(int rect[4]); int resize_own_window(int rect[4]);
void draw_own_window(void); void draw_own_window(int focus);
int wctl_accessibility(const char* path, int mode); int wctl_accessibility(const char* path, int mode);
int wctl_path(int winId, char* buff); int wctl_path(int winId, char* buff);