ref: df658e1cb061e52186c504e84a8430163e9a090b
parent: 35bac6a836a15c60f6b7b6f26d0f8d8545e8e1a0
author: henesy <henesy.dev@gmail.com>
date: Sun Mar 13 11:56:26 EDT 2022
catch keyboard events
--- a/fs.h
+++ b/fs.h
@@ -10,3 +10,9 @@
// Init the draw screen
void initscreen(void*);
+// Put a rune on the screen (x,y)
+Rune* putrune(Rune, usize, usize);
+// Put a string on the screen (y)
+Rune* putstring(Rune*, usize);
+// Clear the screen
+void clear(void);
--- a/main.c
+++ b/main.c
@@ -63,35 +63,56 @@
cmd = toks[0];
if(strcmp(cmd, "clear") == 0){
+ clear();
}else if(strcmp(cmd, "newwin") == 0){
+ // We can keep a ring of buffers for the screen and flip thru
+ return "fail: not impl";
}else if(strcmp(cmd, "delwin") == 0){
+ return "fail: not impl";
}else if(strcmp(cmd, "endwin") == 0){
+ return "fail: not impl";
}else if(strcmp(cmd, "mvprintw") == 0){
+ return "fail: not impl";
}else if(strcmp(cmd, "mvaddch") == 0){
+ // Expect: mvaddch x y r
+ if(ntoks < 4)
+ return "usage: mvaddch x y r";
+
+
+
}else if(strcmp(cmd, "getch") == 0){
+ return "fail: not impl";
}else if(strcmp(cmd, "initscr") == 0){
+ // We start the screen anyways, so leave it be
+ ;
}else if(strcmp(cmd, "raw") == 0){
+ return "fail: not impl";
}else if(strcmp(cmd, "noecho") == 0){
+ return "fail: not impl";
}else if(strcmp(cmd, "cursset") == 0){
+ return "fail: not impl";
}else if(strcmp(cmd, "setescdelay") == 0){
+ return "fail: not impl";
}else if(strcmp(cmd, "refresh") == 0){
+ return "fail: not impl";
}else if(strcmp(cmd, "wrefresh") == 0){
+ return "fail: not impl";
}else if(strcmp(cmd, "keypad") == 0){
-
+ return "fail: not impl";
}
free(toks);
return nil;
--- a/screen.c
+++ b/screen.c
@@ -19,6 +19,52 @@
char *buttons[] = {"exit", 0};
Menu menu = { buttons };
+// Clear the screen
+// TODO - do we need to free the screen buffer?
+void
+clear(void)
+{
+ eresized(0);
+}
+
+// Put a rune on the screen (x,y)
+// Returns nil or an error message
+Rune*
+putrune(Rune r, usize x, usize y)
+{
+ if(y >= sheight)
+ return L"fail: y out of bounds";
+
+ if(x >= swidth)
+ return L"fail: x out of bounds";
+
+ lock(&slock);
+
+ (*sbuf)[y][x] = r;
+
+ unlock(&slock);
+ return nil;
+}
+
+// Put a string on the screen (y)
+// Must be allocated string
+// Returns nil or an error message
+Rune*
+putstring(Rune *s, usize y)
+{
+ if(y >= sheight)
+ return L"fail: y out of bounds";
+
+ lock(&slock);
+
+ free((*sbuf)[y]);
+ (*sbuf)[y] = s;
+
+
+ unlock(&slock);
+ return nil;
+}
+
// Free a 2D buffer
void
freebuf(Rune **buf, usize height)
@@ -37,6 +83,10 @@
int i;
Point p;
+ if((*sbuf) == nil)
+ // No buffer, no problems
+ return;
+
lock(&slock);
Point at = screen->r.min;
for(i = 0; i < sheight; i++){
@@ -92,6 +142,7 @@
}
}
unlock(&slock);
+ //putstring(L"Quack☹☺", 2);
}
// Initialize the screen, spins forever
@@ -105,7 +156,7 @@
if(initdraw(nil, nil, "cursedfs") < 0)
sysfatal("initdraw failed: %r");
- einit(Emouse);
+ einit(Emouse | Ekeyboard);
// Timer in ms
timer = etimer(0, 15);
@@ -141,17 +192,25 @@
* pressed and the first menu option selected
* then exit.. */
- if(
- (e == Emouse)
- && (ev.mouse.buttons & 4)
- && (emenuhit(3, &ev.mouse, &menu) == 0)
- )
- threadexitsall(nil);
- else
- if(e == timer){
- // Might not want this
- eresized(0);
- renderbuf();
- }
+ char kbdc;
+ Rune *s;
+ if(e == timer){
+ // Render the screen buffer on ticks (top prio)
+ eresized(0);
+ renderbuf();
+
+ }else if(e == Ekeyboard){
+ kbdc = ev.kbdc;
+ // Alt to optionally send if getch channel is listening
+
+ }else if(e == Emouse){
+ if((ev.mouse.buttons & 4)
+ && (emenuhit(3, &ev.mouse, &menu) == 0)
+ )
+ threadexitsall(nil);
+
+ // Alt to optionally send if getm channel is listening
+
+ }
}
}