panelreel-demo: hook up pipe

pull/264/head
nick black 5 years ago
parent d6d1f752a2
commit 3d02318e29
No known key found for this signature in database
GPG Key ID: 5F43400C21CBFACC

@ -1,4 +1,5 @@
#include <errno.h> #include <errno.h>
#include <fcntl.h>
#include <unistd.h> #include <unistd.h>
#include <stdlib.h> #include <stdlib.h>
#include <assert.h> #include <assert.h>
@ -166,7 +167,7 @@ tablet_thread(void* vtabletctx){
tabletctx* tctx = vtabletctx; tabletctx* tctx = vtabletctx;
while(true){ while(true){
struct timespec ts; struct timespec ts;
ts.tv_sec = random() % 3 + MINSECONDS; ts.tv_sec = random() % 2 + MINSECONDS;
ts.tv_nsec = random() % 1000000000; ts.tv_nsec = random() % 1000000000;
nanosleep(&ts, NULL); nanosleep(&ts, NULL);
int action = random() % 5; int action = random() % 5;
@ -253,8 +254,16 @@ handle_input(struct notcurses* nc, struct panelreel* pr, int efd,
return key; return key;
} }
static int
close_pipes(int* pipes){
if(close(pipes[0]) | close(pipes[1])){ // intentional, avoid short-circuiting
return -1;
}
return 0;
}
static struct panelreel* static struct panelreel*
panelreel_demo_core(struct notcurses* nc, int efd, tabletctx** tctxs){ panelreel_demo_core(struct notcurses* nc, int efdr, int efdw, tabletctx** tctxs){
bool done = false; bool done = false;
int x = 8, y = 4; int x = 8, y = 4;
panelreel_options popts = { panelreel_options popts = {
@ -284,7 +293,7 @@ panelreel_demo_core(struct notcurses* nc, int efd, tabletctx** tctxs){
return NULL; return NULL;
} }
struct ncplane* w = notcurses_stdplane(nc); struct ncplane* w = notcurses_stdplane(nc);
struct panelreel* pr = panelreel_create(w, &popts, efd); struct panelreel* pr = panelreel_create(w, &popts, efdw);
if(pr == NULL){ if(pr == NULL){
fprintf(stderr, "Error creating panelreel\n"); fprintf(stderr, "Error creating panelreel\n");
return NULL; return NULL;
@ -323,14 +332,13 @@ panelreel_demo_core(struct notcurses* nc, int efd, tabletctx** tctxs){
// FIXME wclrtoeol(w); // FIXME wclrtoeol(w);
ncplane_set_fg_rgb(w, 0, 55, 218); ncplane_set_fg_rgb(w, 0, 55, 218);
wchar_t rw; wchar_t rw;
if((rw = handle_input(nc, pr, efd, &deadline)) <= 0){ if((rw = handle_input(nc, pr, efdr, &deadline)) <= 0){
done = true; done = true;
break; break;
} }
// FIXME clrtoeol(); // FIXME clrtoeol();
newtablet = NULL; newtablet = NULL;
switch(rw){ switch(rw){
case 'p': sleep(60); exit(EXIT_FAILURE); break;
case 'a': newtablet = new_tabletctx(pr, &id); break; case 'a': newtablet = new_tabletctx(pr, &id); break;
case 'b': newtablet = new_tabletctx(pr, &id); break; case 'b': newtablet = new_tabletctx(pr, &id); break;
case 'c': newtablet = new_tabletctx(pr, &id); break; case 'c': newtablet = new_tabletctx(pr, &id); break;
@ -363,29 +371,25 @@ panelreel_demo_core(struct notcurses* nc, int efd, tabletctx** tctxs){
int panelreel_demo(struct notcurses* nc){ int panelreel_demo(struct notcurses* nc){
tabletctx* tctxs = NULL; tabletctx* tctxs = NULL;
/* FIXME there's no eventfd on FreeBSD, so until we do a self-pipe int pipes[2];
* trick here or something, just pass -1. it means higher latency // freebsd doesn't have eventfd :/
* on our keyboard events in this demo. oh well. if(pipe2(pipes, O_CLOEXEC | O_NONBLOCK)){
int efd = eventfd(0, EFD_CLOEXEC | EFD_NONBLOCK); fprintf(stderr, "Error creating pipe (%s)\n", strerror(errno));
if(efd < 0){
fprintf(stderr, "Error creating eventfd (%s)\n", strerror(errno));
return -1; return -1;
}*/ }
int efd = -1;
struct panelreel* pr; struct panelreel* pr;
if((pr = panelreel_demo_core(nc, efd, &tctxs)) == NULL){ if((pr = panelreel_demo_core(nc, pipes[0], pipes[1], &tctxs)) == NULL){
close(efd); close_pipes(pipes);
return -1; return -1;
} }
while(tctxs){ while(tctxs){
kill_tablet(&tctxs); kill_tablet(&tctxs);
} }
close(efd); close_pipes(pipes);
if(panelreel_destroy(pr)){ if(panelreel_destroy(pr)){
fprintf(stderr, "Error destroying panelreel\n"); fprintf(stderr, "Error destroying panelreel\n");
return -1; return -1;
} }
close(efd);
if(demo_render(nc)){ if(demo_render(nc)){
return -1; return -1;
} }

@ -21,12 +21,12 @@ typedef struct tablet {
// * which row the focused tablet starts at (derived from focused window) // * which row the focused tablet starts at (derived from focused window)
// * the list of tablets (available from the focused tablet) // * the list of tablets (available from the focused tablet)
typedef struct panelreel { typedef struct panelreel {
ncplane* p; // ncplane this panelreel occupies, under tablets ncplane* p; // ncplane this panelreel occupies, under tablets
panelreel_options popts; // copied in panelreel_create() panelreel_options popts; // copied in panelreel_create()
int efd; // eventfd/pipe, signaled in panelreel_touch()
// doubly-linked list, a circular one when infinity scrolling is in effect. // doubly-linked list, a circular one when infinity scrolling is in effect.
// points at the focused tablet (when at least one tablet exists, one must be // points at the focused tablet (when at least one tablet exists, one must be
// focused), which might be anywhere on the screen (but is always visible). // focused), which might be anywhere on the screen (but is always visible).
int efd; // eventfd, signaled in panelreel_touch() if >= 0
tablet* tablets; tablet* tablets;
// these values could all be derived at any time, but keeping them computed // these values could all be derived at any time, but keeping them computed
// makes other things easier, or saves us time (at the cost of complexity). // makes other things easier, or saves us time (at the cost of complexity).
@ -782,8 +782,7 @@ int panelreel_touch(panelreel* pr, tablet* t){
if(pr->efd >= 0){ if(pr->efd >= 0){
uint64_t val = 1; uint64_t val = 1;
if(write(pr->efd, &val, sizeof(val)) != sizeof(val)){ if(write(pr->efd, &val, sizeof(val)) != sizeof(val)){
fprintf(stderr, "Error writing to eventfd %d (%s)\n", // fprintf(stderr, "Error writing to eventfd %d (%s)\n", pr->efd, strerror(errno));
pr->efd, strerror(errno));
ret = -1; ret = -1;
} }
} }

Loading…
Cancel
Save