Skip to content

Commit

Permalink
add headless mode
Browse files Browse the repository at this point in the history
  • Loading branch information
qwx9 committed Jan 22, 2025
1 parent 4fe01b4 commit 5a90c6a
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 9 deletions.
25 changes: 25 additions & 0 deletions draw/draw.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@
#include "drw.h"
#include "ui.h"
#include "threads.h"
#include "cmd.h"

View view;
RNode *rnodes;
REdge *redges;
ssize ndnodes, ndedges;
Drawing drawing;
extern Channel *drawc;

static inline void
drawedge(REdge *r, RNode *u, RNode *v, int urev, int vrev)
Expand Down Expand Up @@ -201,6 +203,29 @@ redraw(int go)
return go;
}

static void
ticker(void *)
{
for(;;){
sleep(10);
sendul(drawc, Reqrefresh);
}
}

void
noloop(void)
{
ulong u;

newthread(ticker, nil, nil, nil, "ticker", mainstacksize);
while((u = recvul(drawc)) > 0){
switch(u){
case Reqredraw:
case Reqrefresh: pushcmd("exportlayout(\"%s\")", drawing.layfile); flushcmd(); break;
}
}
}

/* note: view screen dimensions are *not* necessarily set by the
* end of all initialization */
void
Expand Down
3 changes: 3 additions & 0 deletions draw/drw.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ enum{
DFstalelen = 1<<4,
DFmsaa = 1<<5,
DFhidpi = 1<<6,
DFnope = 1<<7,
};
struct Drawing{
int flags;
Expand All @@ -37,6 +38,7 @@ struct Drawing{
Range zbound;
float nodesz;
float fatness;
char *layfile;
};
extern Drawing drawing;

Expand Down Expand Up @@ -162,5 +164,6 @@ void setnodeshape(int);
void updatedraw(void);
void reqdraw(int);
void initcol(void);
void noloop(void);
void initsysdraw(void);
void initdrw(void);
4 changes: 3 additions & 1 deletion plan9/draw.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#include "threads.h"
#include <draw.h>

Channel *drawc;

struct Color{
u32int col;
Image *i;
Expand All @@ -13,7 +15,7 @@ struct Color{
static Point panmax, ΔZP;
static Rectangle viewr, statr;
static Image *viewfb, *selfb;
static Channel *drawc, *ticc, *framec;
static Channel *ticc, *framec;
static QLock ticker;
static ioff selected; /* FIXME */

Expand Down
4 changes: 3 additions & 1 deletion sokol/draw.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ void event(const sapp_event*);
typedef struct GLNode GLNode;
typedef struct GLEdge GLEdge;

static Channel *drawc;
Channel *drawc;
static int reqs;

void
Expand Down Expand Up @@ -367,6 +367,8 @@ initsysdraw(void)
void
evloop(void)
{
ulong u;

/* wait until at least one file asks for a redraw */
while(recvul(drawc) != Reqredraw)
;
Expand Down
20 changes: 13 additions & 7 deletions strpg.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,14 @@ pushfile(char *file, int type)
static void
help(void)
{
warn("usage: %s [-HMWZbhvw] [-f FILE] [-l ALG] [-t N] [-c FILE] FILE\n", argv0);
warn("usage: %s [-HMWZbhvw] [-f FILE] [-l ALG] [-n FILE] [-t N] [-c FILE] FILE\n", argv0);
warn(
"-b White-on-black color theme\n"
"-c FILE Load tags from csv FILE\n"
"-f FILE Load layout from FILE\n"
"-h Print usage information and exit\n"
"-l ALG Set layouting algorithm (default: pfr)\n"
"-n FILE Run layouting headless, saving to FILE periodically\n"
"-t N Set number of layouting threads (1-128, default: 4)\n"
"-v Print version and exit\n"
"-w Do not wait for all files to load to start layouting\n"
Expand All @@ -88,7 +89,7 @@ help(void)
static void
usage(void)
{
sysfatal("usage: %s [-HMWZbhvw] [-f FILE] [-l ALG] [-t N] [-c FILE] FILE", argv0);
sysfatal("usage: %s [-HMWZbhvw] [-f FILE] [-l ALG] [-n FILE] [-t N] [-c FILE] FILE", argv0);
}

static void
Expand Down Expand Up @@ -157,6 +158,10 @@ parseargs(int argc, char **argv)
else
sysfatal("unknown layout type");
break;
case 'n':
drawing.layfile = EARGF(usage());
drawing.flags |= DFnope;
break;
case 't':
nlaythreads = atoi(EARGF(usage()));
if(nlaythreads <= 0 || nlaythreads > 128){
Expand Down Expand Up @@ -194,11 +199,12 @@ main(int argc, char **argv)
init();
initlayout();
load();
initui();
if(debug & Debugload)
for(;;)
sleep(1000);
evloop();
if(drawing.flags & DFnope || debug & Debugload)
noloop();
else{
initui();
evloop();
}
quit();
return 0;
}
2 changes: 2 additions & 0 deletions ui/ui.c
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,8 @@ resetui(void)
void
initui(void)
{
if(drawing.flags & DFnope)
return;
initsysui();
view.fov = 45.0f;
view.tfov = tanf(view.fov / 2);
Expand Down

0 comments on commit 5a90c6a

Please sign in to comment.