Skip to content

Commit

Permalink
bunch of handle_redraw sanity improvements
Browse files Browse the repository at this point in the history
this might finally cure the dreaded freeze on resize issue #31
- no longer handle the redraw character in main input handler
- avoid drawing the same preview twice unless absolutely needed
  • Loading branch information
Ckath committed Sep 11, 2021
1 parent 6050d2e commit 72bfdf2
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 6 deletions.
41 changes: 35 additions & 6 deletions fuf.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,14 @@
#include "inc/thr.h"
#include "inc/item.h"

#define CLI_PROGRAMS "vi nvim nano dhex man w3m elinks links2 links lynx"

static void handle_redraw(); /* utility */
static void handle_chld();
static void init();
static char ch_prompt(char *prefix);
static void str_prompt(char *prefix, char *result);
static void search_next(bool reverse);
static void open_with(char *launcher, char *file, bool cli);
static void reset_preview();
static void display_load(); /* async */
static void load_items();
static void load_preview();
Expand Down Expand Up @@ -127,6 +126,10 @@ init()
static char
ch_prompt(char *prefix)
{
/* has to be done to unfuck the resize situation */
reset_preview();
handle_redraw();

WINDOW *prompt = snewwin(1, COLS, LINES-1, 1);

bool bold = false;
Expand Down Expand Up @@ -156,6 +159,10 @@ ch_prompt(char *prefix)
static void
str_prompt(char *prefix, char *result)
{
/* has to be done to unfuck the resize situation */
reset_preview();
handle_redraw();

WINDOW *prompt = snewwin(1, COLS, LINES-1, 1);
smvwaddstr(prompt, 0, 0, prefix);
keypad(prompt, true);
Expand Down Expand Up @@ -235,10 +242,25 @@ open_with(char *launcher, char *file, bool cli)
} else { /* parent: check launched process */
if (cli) {
wait(NULL);
reset_preview();
handle_redraw(); /* redraw since its probably fucked */
}
}

handle_redraw(); /* redraw since its probably fucked */
}

static void
reset_preview()
{
/* extern bodgery for preview */
extern char preview_job[2][PATH_MAX];
extern pthread_mutex_t preview_pid_lock;

/* clear preview duplicate check to ensure reload */
pthread_mutex_lock(&preview_pid_lock);
strcpy(preview_job[0], "");
strcpy(preview_job[1], "");
pthread_mutex_unlock(&preview_pid_lock);
}

static void
Expand Down Expand Up @@ -306,6 +328,7 @@ load_preview()
extern pthread_mutex_t preview_lock;
extern pthread_mutex_t preview_pid_lock;
extern pid_t preview_pid[2];
extern char preview_job[2][PATH_MAX];
for (;;) {
pthread_mutex_lock(&preview_lock);
do {
Expand All @@ -326,6 +349,13 @@ load_preview()
col_px*(COLS/2-3), line_px*(LINES-2), /* preview img size */
col_px*(COLS/2+1), line_px); /* preview img pos */

/* try to avoid duplicate jobs */
pthread_mutex_lock(&preview_pid_lock);
strcpy(preview_job[nthr], preview_cmd);
pthread_mutex_unlock(&preview_pid_lock);
if (!strcmp(preview_job[nthr], preview_job[!nthr])) {
continue;
}

int fd;
pthread_mutex_lock(&preview_pid_lock);
Expand Down Expand Up @@ -538,9 +568,6 @@ main(int argc, char *argv[])
}
}
break;
case '\232': /* handle request for redraw after resize */
handle_redraw();
break;
case 'j':
key_down:
sel_item += sel_item < items_len-1 ? 1 : 0;
Expand Down Expand Up @@ -577,11 +604,13 @@ main(int argc, char *argv[])
break;
case 'r':
if (!items_loading) {
reset_preview();
strcpy(goto_item, items[sel_item].name);
start_load(load_items, display_load);
}
break;
case CTRL('l'):
reset_preview();
handle_redraw();
break;
case 'g':
Expand Down
2 changes: 2 additions & 0 deletions inc/thr.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include <pthread.h>
#include <signal.h>
#include <dirent.h> /* this is where PATH_MAX comes from, dont think about it */
#include <stdbool.h>
#include "thr.h"
#include "sysext.h"
Expand All @@ -16,6 +17,7 @@ pthread_mutex_t preview_lock = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t preview_pid_lock = PTHREAD_MUTEX_INITIALIZER;
pthread_spinlock_t redraw_lock;
pid_t preview_pid[2];
char preview_job[2][PATH_MAX];
_Atomic bool items_loading = false;
_Atomic bool pn = false;

Expand Down

0 comments on commit 72bfdf2

Please sign in to comment.