Skip to content

Commit

Permalink
demo: hook up Ctrl+L to refresh #379
Browse files Browse the repository at this point in the history
  • Loading branch information
dankamongmen committed Feb 24, 2020
1 parent 9db4bdd commit 0e3be34
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 9 deletions.
4 changes: 4 additions & 0 deletions src/demo/demo.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <assert.h>
#include <unistd.h>
#include <limits.h>
#include <pthread.h>
#include <stdatomic.h>
#include <version.h>
#include <notcurses/notcurses.h>
Expand Down Expand Up @@ -146,6 +147,9 @@ int demo_render(struct notcurses* nc);

#define DEMO_RENDER(nc) { int demo_render_err = demo_render(nc); if(demo_render_err){ return demo_render_err; }}

// locked by callers to notcurses_render() and notcurses_refresh(), all internal
extern pthread_mutex_t demo_render_lock;

// if you won't be doing things, and it's a long sleep, consider using
// demo_nanosleep(). it updates the HUD, which looks better to the user.
int demo_nanosleep(struct notcurses* nc, const struct timespec *ts);
Expand Down
8 changes: 7 additions & 1 deletion src/demo/hud.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
// their mouse. it should always be on the top of the z-stack.
struct ncplane* hud = NULL;

pthread_mutex_t demo_render_lock = PTHREAD_MUTEX_INITIALIZER;

// while the HUD is grabbed by the mouse, these are set to the position where
// the grab started. they are reset once the HUD is released.
static int hud_grab_x = -1;
Expand Down Expand Up @@ -438,5 +440,9 @@ int demo_render(struct notcurses* nc){
return -1;
}
}
return notcurses_render(nc);
// lock against a possible notcurses_refresh() on Ctrl+L
pthread_mutex_lock(&demo_render_lock);
int ret = notcurses_render(nc);
pthread_mutex_unlock(&demo_render_lock);
return ret;
}
21 changes: 14 additions & 7 deletions src/demo/input.c
Original file line number Diff line number Diff line change
Expand Up @@ -92,16 +92,23 @@ ultramegaok_demo(void* vnc){
continue;
}
if(nckey_mouse_p(ni.id)){
handle_mouse(&ni);
}else{
// if this was about the menu or HUD, pass to them, and continue
if(menu_or_hud_key(nc, &ni)){
if(handle_mouse(&ni)){
continue;
}
// go ahead and pass keyboard through to demo, even if it was a 'q'
// (this might cause the demo to exit immediately, as is desired)
pass_along(&ni);
}
if(id == 'L' && ni.ctrl){
pthread_mutex_lock(&demo_render_lock);
notcurses_refresh(nc);
pthread_mutex_unlock(&demo_render_lock);
continue;
}
// if this was about the menu or HUD, pass to them, and continue
if(menu_or_hud_key(nc, &ni)){
continue;
}
// go ahead and pass keyboard through to demo, even if it was a 'q'
// (this might cause the demo to exit immediately, as is desired)
pass_along(&ni);
}
return NULL;
}
Expand Down
2 changes: 1 addition & 1 deletion src/lib/internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ typedef struct ncselector {
uint64_t titlechannels; // title channels
uint64_t footchannels; // secondary and footer channels
uint64_t boxchannels; // border channels
int uarrowy, darrowy, arrowx;// location of scrollarrows, -1 if not present
int uarrowy, darrowy, arrowx;// location of scrollarrows, even if not present
} ncselector;

typedef struct ncdirect {
Expand Down

0 comments on commit 0e3be34

Please sign in to comment.