commit ccbdb75eb0bea1fe173da99fee12dfa1a9dc382e
parent 023ea793d7f8276c8d80841f0362c34a6573225b
Author: glenda <glenda@9front.local>
Date: Sat, 30 May 2020 19:18:06 +0000
threadload
Diffstat:
M | TODO | | | 6 | +++++- |
M | gophra.c | | | 128 | +++++++++++++++++++++++++++++++++++++++++++++++-------------------------------- |
2 files changed, 82 insertions(+), 52 deletions(-)
diff --git a/TODO b/TODO
@@ -1,3 +1,7 @@
* README
* man page
-* move loading into separate thread
+* add status setting and border animation to threadload
+* change save to use threadload
+* sanitize funcs
+ - extracturl (rename into extracturi)
+
diff --git a/gophra.c b/gophra.c
@@ -30,23 +30,27 @@ struct History {
History *prev;
};
-Text text;
+typedef struct Loadctl Loadctl;
+struct Loadctl {
+ char *addr;
+ char *path;
+ char *query;
+ Channel *c;
+};
-History hzero, *hp;
+Text text;
+History hzero, *hp;
Image *Iborder, *Iwbg, *Iwfg, *Iwfgl, *Istatus;
-
Rectangle rstatus, rw;
-
char status[256], path[256], addr[256], type;
-
char *tmpfile = "/tmp/gophra.tmp";
-
long scroll;
+Channel *lc;
void usage(void);
+void threadload(void*);
void back(void);
-void clear(void);
void calcrects(void);
void drawborder(void);
void drawstatus(void);
@@ -55,7 +59,7 @@ void drawmenu(void);
int extracturl(char*);
char* getline(long);
void handlelink(char*, char, char*);
-int loadtext(char*, char*);
+void loadtext(char*, char*);
void runhold(void*);
void runpage(void*);
int save(char*, char*);
@@ -69,7 +73,8 @@ threadmain(int argc, char **argv)
Mouse mv;
Rune kv;
int rv[2];
-
+ Text tv;
+
long sline;
char *s;
int mpress;
@@ -97,6 +102,7 @@ threadmain(int argc, char **argv)
mpress = 0;
scroll = 0;
hp = &hzero;
+ lc = chancreate(sizeof(Text), 0);
snprint(status, 255, "gophra!");
@@ -118,10 +124,11 @@ threadmain(int argc, char **argv)
if((kc = initkeyboard(0)) == nil)
sysfatal("initkeyboard failed: %r");
- Alt alts[4] = {
+ Alt alts[] = {
{kc->c, &kv, CHANRCV},
{mc->c, &mv, CHANRCV},
{mc->resizec, rv, CHANRCV},
+ {lc, &tv, CHANRCV},
{0, 0, CHANEND},
};
@@ -190,11 +197,57 @@ threadmain(int argc, char **argv)
drawmenu();
flushimage(display, 1);
break;
+ case 3: /* loading */
+ realloc(text.data, 0);
+ text = tv;
+ scroll = 0;
+ drawmenu();
+ flushimage(display, 1);
+ break;
}
}
}
void
+threadload(void *v)
+{
+ int gfd;
+ Text text;
+ long n;
+ char buf[1024];
+
+ Loadctl *lctl;
+ lctl = v;
+
+ // set status to "dialing $addr..."
+ gfd = dial(lctl->addr, 0, 0, 0);
+ if (gfd == -1) {
+ // set status to "failed to dial $addr"
+ // send nill
+ return;
+ }
+ write(gfd, lctl->path, strlen(path));
+ write(gfd, "\n", 1);
+ // if there're query, send query
+ text.data = nil;
+ text.size = 0;
+ while ((n = read(gfd, buf, 1024)) > 0){
+ text.data = realloc(text.data, text.size + n);
+ memcpy(text.data + text.size, buf, n);
+ text.size += n;
+ // set status to "loading - $n bytes"
+ // border animation
+ }
+ // set status to done
+ send(lctl->c, &text);
+
+ realloc(lctl->path, 0);
+ realloc(lctl->addr, 0);
+ realloc(lctl->query, 0);
+
+}
+
+void
usage(void)
{
fprint(2, "usage: %s [gopher_uri]\n", argv0);
@@ -204,15 +257,13 @@ usage(void)
void
back(void)
{
- History *hold;
+ History *oldh;
if (hp == &hzero) return;
scroll = 0;
- hold = hp;
+ oldh = hp;
hp = hp->prev;
- free(hold);
+ free(oldh);
loadtext(hp->addr, hp->path);
- drawmenu();
- flushimage(display, 1);
}
void
@@ -323,7 +374,7 @@ getline(long ln)
{
char *sbuf, *sp, *tp;
long lc;
- sbuf = malloc(sizeof(char) * 1024);
+ sbuf = mallocz(sizeof(char) * 1024, 1);
sp = sbuf;
tp = text.data;
for (lc = 0; lc < ln; tp++) {
@@ -333,7 +384,6 @@ getline(long ln)
while (*tp != '\n') {
*sp = *tp;
sp++;
- *sp = 0;
tp++;
if (tp > text.data + text.size) return 0;
}
@@ -376,10 +426,7 @@ handlelink(char *addr, char type, char *path)
hp = hnew;
strncpy(hp->addr, addr, 256);
strncpy(hp->path, path, 256);
- loadtext(addr, path);
- scroll = 0;
- drawmenu();
- flushimage(display, 1);
+ loadtext(addr, path);
break;
case 'I':
if (save(addr, path) == 0) proccreate(runpage, 0, 1024 * 8);
@@ -391,44 +438,23 @@ handlelink(char *addr, char type, char *path)
}
}
-int
+void
loadtext(char *addr, char *path)
{
- int dcfd;
- long n;
- char buf[1024];
-
- dcfd = dial(addr, 0, 0, 0);
- if (dcfd > 0) {
- write(dcfd, path, strlen(path));
- write(dcfd, "\n", 1);
- } else {
- snprint(status, 255, "failed to dial \"%s\"", addr);
- drawstatus();
- flushimage(display, 1);
- return -1;
- }
-
- text.size = 0;
- while ((n = read(dcfd, buf, 1024)) > 0) {
- text.data = realloc(text.data, text.size + n);
- memcpy(text.data + text.size, buf, n);
- text.size += n;
- snprint(status, 255, "loading %s: %ldB", path, text.size);
- drawstatus();
- flushimage(display, 1);
- }
- snprint(status, 255, "done");
- drawstatus();
- flushimage(display, 1);
- return 0;
+ Loadctl *lctl;
+ lctl = malloc(sizeof(Loadctl));
+ lctl->c = lc;
+ lctl->addr = strdup(addr);
+ lctl->path = strdup(path);
+ lctl->query = nil;
+ threadcreate(threadload, lctl, 64 * 1024);
+ return;
}
void
runhold(void*)
{
- // TODO: this should be done through plumber
procexecl(nil, "/bin/window", "window", "-m", "hold", tmpfile, nil);
}