9front - general discussion about 9front
 help / color / mirror / Atom feed
* Re: [9front] patch: tweak plumb support
@ 2021-01-12 10:12 sirjofri
  2021-01-13 22:06 ` ori
  0 siblings, 1 reply; 9+ messages in thread
From: sirjofri @ 2021-01-12 10:12 UTC (permalink / raw)
  To: 9front

Hello,

Poke on this. Just a reminder.

sirjofri

.
21.12.2020 16:34:14 sirjofri+ml-9front@sirjofri.de:
> Hey all,
>
> this new patch version has only minor fixes compared to the previous
> version.  After more than a week not working on it I guess I'll just
> release this patch now, hoping it's good enough to be commited.
>
> Still open for improvements and discussion!
>
> sirjofri
>
> .
> diff -r 2647ea8b9793 sys/man/1/tweak
> --- a/sys/man/1/tweak Sun Dec 06 21:52:01 2020 +0100
> +++ b/sys/man/1/tweak Tue Dec 08 15:40:04 2020 +0100
> @@ -155,6 +155,16 @@
> Quit
> .IR tweak .
> The program will complain once about modified but unwritten files.
> +.PP
> +.I Tweak
> +listens to the
> +.I plumber
> +channel
> +.B imageedit
> +for filenames as well as image data. Plumbed image data is stored as 
files in
> +.B /tmp
> +and is automatically cleaned when exiting
> +.IR tweak .
> .SH SOURCE
> .B /sys/src/cmd/tweak.c
> .SH "SEE ALSO"
> diff -r 2647ea8b9793 sys/src/cmd/tweak.c
> --- a/sys/src/cmd/tweak.c Sun Dec 06 21:52:01 2020 +0100
> +++ b/sys/src/cmd/tweak.c Tue Dec 08 15:40:04 2020 +0100
> @@ -3,8 +3,11 @@
> #include <draw.h>
> #include <cursor.h>
> #include <event.h>
> +#include <plumb.h>
> #include <bio.h>
>
> +#define Eplumb 128
> +
> typedef struct Thing Thing;
>
> struct Thing
> @@ -157,6 +160,8 @@
> int    but1val = 0;
> int    but2val = 255;
> int    invert = 0;
> +int    blockinput = 0;
> +int    tmpindex = 0;
> Image    *values[256];
> Image    *greyvalues[256];
> uchar    data[8192];
> @@ -172,12 +177,60 @@
> void tclose1(Thing*);
>
> void
> +flushevents(ulong keys)
> +{
> + Event e;
> + if(keys == 0)
> +   return;
> + while(ecanread(keys))
> +   eread(keys, &e);
> +}
> +
> +char*
> +pshowdata(Plumbmsg *pm)
> +{
> + int fd;
> + char *file;
> + file = smprint("/tmp/tweaktmp.%d.%d", getpid(), tmpindex++);
> + if(file == 0)
> +   sysfatal("malloc failed: %r");
> + fd = create(file, OWRITE|OEXCL, 0600);
> + if(fd < 0){
> +   free(file);
> +   mesg("cannot create tmpfile.");
> +   return nil;
> + }
> + if(write(fd, pm->data, pm->ndata) != pm->ndata){
> +   mesg("error writing tmpfile: %r");
> +   close(fd);
> +   free(file);
> +   return nil;
> + }
> + close(fd);
> + return file;
> +}
> +
> +char*
> +pfilename(Plumbmsg *pm)
> +{
> + char *file;
> + if(pm->data[0] == '/')
> +   file = smprint("%.*s", pm->ndata, pm->data);
> + else
> +   file = smprint("%s/%.*s", pm->wdir, pm->ndata, pm->data);
> + return cleanname(file);
> +}
> +
> +void
> main(int argc, char *argv[])
> {
>   int i;
>   Event e;
>   Thing *t;
> + Plumbmsg *pm;
> + char *s;
>
> + srand(time(0));
>   mag = Mag;
>   if(initdraw(error, 0, "tweak") < 0){
>     fprint(2, "tweak: initdraw failed: %r\n");
> @@ -190,6 +243,7 @@
>       drawerror(display, "can't allocate image");
>   }
>   einit(Emouse|Ekeyboard);
> + eplumb(Eplumb, "imageedit");
>   eresized(0);
>   i = 1;
>   setjmp(err);
> @@ -214,10 +268,42 @@
>       }
>       if(mouse.buttons & 4)
>         menu();
> +     break;
> +   case Eplumb:
> +     pm = e.v;
> +     if(pm->ndata == 0)
> +       break;
> +     s = plumblookup(pm->attr, "action");
> +     if(s && strcmp(s, "showdata") == 0)
> +       file = pshowdata(pm);
> +     else
> +       file = pfilename(pm);
> +     if(!file){
> +       mesg("invalid plumb data");
> +       plumbfree(pm);
> +       break;
> +     }
> +     plumbfree(pm);
> +     t = tget(file);
> +     if(t)
> +       drawthing(t, 1);
> +     flushimage(display, 1);
> +     file = 0;
>     }
> }
>
> void
> +cleantmpfiles(void)
> +{
> + char *s;
> + for(tmpindex--; tmpindex >= 0; tmpindex--){
> +   s = smprint("/tmp/tweaktmp.%d.%d", getpid(), tmpindex);
> +   remove(s);
> +   free(s);
> + }
> +}
> +
> +void
> error(Display*, char *s)
> {
>   if(file)
> @@ -226,6 +312,7 @@
>     mesg("/dev/bitblt error: %s", s);
>   if(err[0])
>     longjmp(err, 1);
> + cleantmpfiles();
>   exits(s);
> }
>
> @@ -531,6 +618,12 @@
>   nt->tr.max.y = nt->tr.min.y + nl;
>   nt->er.max.y = nt->tr.max.y + Border;
>   text(nt);
> +
> + mesg("");
> + if(blockinput){
> +   flushevents(Emouse|Ekeyboard);
> +   blockinput = 0;
> + }
> }
>
> int
> @@ -557,6 +650,9 @@
>   uchar buf[256];
>   char *data;
>
> + mesg("reading %s", file);
> + blockinput = 1;
> +
>   buf[0] = '\0';
>   errstr((char*)buf, sizeof buf); /* flush pending error message */
>   memmove(oerr, err, sizeof err);
> @@ -2041,6 +2137,7 @@
>     buttons(Down);
>     if(mouse.buttons == 4){
>       buttons(Up);
> +     cleantmpfiles();
>       exits(0);
>     }
>     buttons(Up);

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [9front] patch: tweak plumb support
  2021-01-12 10:12 [9front] patch: tweak plumb support sirjofri
@ 2021-01-13 22:06 ` ori
  0 siblings, 0 replies; 9+ messages in thread
From: ori @ 2021-01-13 22:06 UTC (permalink / raw)
  To: 9front

Quoth sirjofri <sirjofri+ml-9front@sirjofri.de>:
> Hello,
> 
> Poke on this. Just a reminder.
> 
> sirjofri
> 
> .
> 21.12.2020 16:34:14 sirjofri+ml-9front@sirjofri.de:
> > Hey all,
> >
> > this new patch version has only minor fixes compared to the previous
> > version.  After more than a week not working on it I guess I'll just
> > release this patch now, hoping it's good enough to be commited.
> >
> > Still open for improvements and discussion!
> >
> > sirjofri
> >

Anyone who uses tweak have a chance to test this?

(I can review the code, but have no idea if the

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [9front] patch: tweak plumb support
  2020-12-07 20:26         ` sirjofri+ml-9front
@ 2020-12-21 15:34           ` sirjofri+ml-9front
  0 siblings, 0 replies; 9+ messages in thread
From: sirjofri+ml-9front @ 2020-12-21 15:34 UTC (permalink / raw)
  To: 9front

[-- Attachment #1: Type: text/plain, Size: 270 bytes --]

Hey all,

this new patch version has only minor fixes compared to the previous
version.  After more than a week not working on it I guess I'll just
release this patch now, hoping it's good enough to be commited.

Still open for improvements and discussion!

sirjofri

.

[-- Attachment #2: Type: text/plain, Size: 3698 bytes --]

diff -r 2647ea8b9793 sys/man/1/tweak
--- a/sys/man/1/tweak	Sun Dec 06 21:52:01 2020 +0100
+++ b/sys/man/1/tweak	Tue Dec 08 15:40:04 2020 +0100
@@ -155,6 +155,16 @@
 Quit
 .IR tweak .
 The program will complain once about modified but unwritten files.
+.PP
+.I Tweak
+listens to the
+.I plumber
+channel
+.B imageedit
+for filenames as well as image data. Plumbed image data is stored as files in
+.B /tmp
+and is automatically cleaned when exiting
+.IR tweak .
 .SH SOURCE
 .B /sys/src/cmd/tweak.c
 .SH "SEE ALSO"
diff -r 2647ea8b9793 sys/src/cmd/tweak.c
--- a/sys/src/cmd/tweak.c	Sun Dec 06 21:52:01 2020 +0100
+++ b/sys/src/cmd/tweak.c	Tue Dec 08 15:40:04 2020 +0100
@@ -3,8 +3,11 @@
 #include <draw.h>
 #include <cursor.h>
 #include <event.h>
+#include <plumb.h>
 #include <bio.h>
 
+#define Eplumb 128
+
 typedef struct	Thing	Thing;
 
 struct Thing
@@ -157,6 +160,8 @@
 int		but1val = 0;
 int		but2val = 255;
 int		invert = 0;
+int		blockinput = 0;
+int		tmpindex = 0;
 Image		*values[256];
 Image		*greyvalues[256];
 uchar		data[8192];
@@ -172,12 +177,60 @@
 void	tclose1(Thing*);
 
 void
+flushevents(ulong keys)
+{
+	Event e;
+	if(keys == 0)
+		return;
+	while(ecanread(keys))
+		eread(keys, &e);
+}
+
+char*
+pshowdata(Plumbmsg *pm)
+{
+	int fd;
+	char *file;
+	file = smprint("/tmp/tweaktmp.%d.%d", getpid(), tmpindex++);
+	if(file == 0)
+		sysfatal("malloc failed: %r");
+	fd = create(file, OWRITE|OEXCL, 0600);
+	if(fd < 0){
+		free(file);
+		mesg("cannot create tmpfile.");
+		return nil;
+	}
+	if(write(fd, pm->data, pm->ndata) != pm->ndata){
+		mesg("error writing tmpfile: %r");
+		close(fd);
+		free(file);
+		return nil;
+	}
+	close(fd);
+	return file;
+}
+
+char*
+pfilename(Plumbmsg *pm)
+{
+	char *file;
+	if(pm->data[0] == '/')
+		file = smprint("%.*s", pm->ndata, pm->data);
+	else
+		file = smprint("%s/%.*s", pm->wdir, pm->ndata, pm->data);
+	return cleanname(file);
+}
+
+void
 main(int argc, char *argv[])
 {
 	int i;
 	Event e;
 	Thing *t;
+	Plumbmsg *pm;
+	char *s;
 
+	srand(time(0));
 	mag = Mag;
 	if(initdraw(error, 0, "tweak") < 0){
 		fprint(2, "tweak: initdraw failed: %r\n");
@@ -190,6 +243,7 @@
 			drawerror(display, "can't allocate image");
 	}
 	einit(Emouse|Ekeyboard);
+	eplumb(Eplumb, "imageedit");
 	eresized(0);
 	i = 1;
 	setjmp(err);
@@ -214,10 +268,42 @@
 			}
 			if(mouse.buttons & 4)
 				menu();
+			break;
+		case Eplumb:
+			pm = e.v;
+			if(pm->ndata == 0)
+				break;
+			s = plumblookup(pm->attr, "action");
+			if(s && strcmp(s, "showdata") == 0)
+				file = pshowdata(pm);
+			else
+				file = pfilename(pm);
+			if(!file){
+				mesg("invalid plumb data");
+				plumbfree(pm);
+				break;
+			}
+			plumbfree(pm);
+			t = tget(file);
+			if(t)
+				drawthing(t, 1);
+			flushimage(display, 1);
+			file = 0;
 		}
 }
 
 void
+cleantmpfiles(void)
+{
+	char *s;
+	for(tmpindex--; tmpindex >= 0; tmpindex--){
+		s = smprint("/tmp/tweaktmp.%d.%d", getpid(), tmpindex);
+		remove(s);
+		free(s);
+	}
+}
+
+void
 error(Display*, char *s)
 {
 	if(file)
@@ -226,6 +312,7 @@
 		mesg("/dev/bitblt error: %s", s);
 	if(err[0])
 		longjmp(err, 1);
+	cleantmpfiles();
 	exits(s);
 }
 
@@ -531,6 +618,12 @@
 	nt->tr.max.y = nt->tr.min.y + nl;
 	nt->er.max.y = nt->tr.max.y + Border;
 	text(nt);
+
+	mesg("");
+	if(blockinput){
+		flushevents(Emouse|Ekeyboard);
+		blockinput = 0;
+	}
 }
 
 int
@@ -557,6 +650,9 @@
 	uchar buf[256];
 	char *data;
 
+	mesg("reading %s", file);
+	blockinput = 1;
+
 	buf[0] = '\0';
 	errstr((char*)buf, sizeof buf);	/* flush pending error message */
 	memmove(oerr, err, sizeof err);
@@ -2041,6 +2137,7 @@
 		buttons(Down);
 		if(mouse.buttons == 4){
 			buttons(Up);
+			cleantmpfiles();
 			exits(0);
 		}
 		buttons(Up);

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [9front] patch: tweak plumb support
  2020-12-07 15:43       ` ori
@ 2020-12-07 20:26         ` sirjofri+ml-9front
  2020-12-21 15:34           ` sirjofri+ml-9front
  0 siblings, 1 reply; 9+ messages in thread
From: sirjofri+ml-9front @ 2020-12-07 20:26 UTC (permalink / raw)
  To: 9front

[-- Attachment #1: Type: text/plain, Size: 1154 bytes --]

Hey all,

> I wonder if this'd better be done with a more complex plumb rule,

I'm currently working on some kind of "plumb channel selector menu".
The general mechanics work as expected, I'm on fine tuning.  I'll
present it when it's done (although I already showed a little on
discord).

> I don't think leaving trash in /tmp is really acceptable :/
> 
> +				file = smprint("/tmp/tweaktmp.%d", rand());
> 
> Use the pid, that way you know nobody else is using it,
> and you can get rid of the retry loop.

yep, done.  I got lots of feedback by kvik (very much thank you!) who
already pointed that out.

The now included patch considers kviks annotations, roughly:

- moves plumb message handling to two separate functions (showdata and
filenames)
- cleanup /tmp-files on exit
- better filename creation with %.*s (although I still think basic
filename handling should/could be included in plumb.h, many
applications need to deal with plumb filename messages).
- fd == 0 is a valid file descriptor.  Changed to fd < 0

I hope this looks better now.  I verified it worked using showdata
plumbs as well as file plumbs, and tmpfile cleanup.

sirjofri

.


[-- Attachment #2: Type: text/plain, Size: 3597 bytes --]

diff -r 2647ea8b9793 sys/man/1/tweak
--- a/sys/man/1/tweak	Sun Dec 06 21:52:01 2020 +0100
+++ b/sys/man/1/tweak	Mon Dec 07 21:17:12 2020 +0100
@@ -155,6 +155,16 @@
 Quit
 .IR tweak .
 The program will complain once about modified but unwritten files.
+.PP
+.I Tweak
+listens to the
+.I plumber
+channel
+.B imageedit
+for filenames as well as image data. Plumbed image data is stored as files in
+.B /tmp
+and is automatically cleaned when exiting
+.IR tweak .
 .SH SOURCE
 .B /sys/src/cmd/tweak.c
 .SH "SEE ALSO"
diff -r 2647ea8b9793 sys/src/cmd/tweak.c
--- a/sys/src/cmd/tweak.c	Sun Dec 06 21:52:01 2020 +0100
+++ b/sys/src/cmd/tweak.c	Mon Dec 07 21:17:12 2020 +0100
@@ -3,8 +3,11 @@
 #include <draw.h>
 #include <cursor.h>
 #include <event.h>
+#include <plumb.h>
 #include <bio.h>
 
+#define Eplumb 128
+
 typedef struct	Thing	Thing;
 
 struct Thing
@@ -157,6 +160,8 @@
 int		but1val = 0;
 int		but2val = 255;
 int		invert = 0;
+int		blockinput = 0;
+int		tmpindex = 0;
 Image		*values[256];
 Image		*greyvalues[256];
 uchar		data[8192];
@@ -172,12 +177,60 @@
 void	tclose1(Thing*);
 
 void
+flushevents(ulong keys)
+{
+	Event e;
+	if(keys == 0)
+		return;
+	while(ecanread(keys))
+		eread(keys, &e);
+}
+
+char*
+pshowdata(Plumbmsg *pm)
+{
+	int fd;
+	char *file;
+	file = smprint("/tmp/tweaktmp.%d.%d", getpid(), tmpindex++);
+	if(file == 0)
+		sysfatal("malloc failed: %r");
+	fd = create(file, OWRITE|OEXCL, 0600);
+	if(fd < 0){
+		free(file);
+		mesg("cannot create tmpfile.");
+		return nil;
+	}
+	if(write(fd, pm->data, pm->ndata) != pm->ndata){
+		mesg("error writing tmpfile: %r");
+		close(fd);
+		free(file);
+		return nil;
+	}
+	close(fd);
+	return file;
+}
+
+char*
+pfilename(Plumbmsg *pm)
+{
+	char *file;
+	if(pm->data[0] == '/')
+		file = cleanname(smprint("%.*s", pm->ndata, pm->data));
+	else
+		file = cleanname(smprint("%s/%.*s", pm->wdir, pm->ndata, pm->data));
+	return file;
+}
+
+void
 main(int argc, char *argv[])
 {
 	int i;
 	Event e;
 	Thing *t;
+	Plumbmsg *pm;
+	char *s;
 
+	srand(time(0));
 	mag = Mag;
 	if(initdraw(error, 0, "tweak") < 0){
 		fprint(2, "tweak: initdraw failed: %r\n");
@@ -190,6 +243,7 @@
 			drawerror(display, "can't allocate image");
 	}
 	einit(Emouse|Ekeyboard);
+	eplumb(Eplumb, "imageedit");
 	eresized(0);
 	i = 1;
 	setjmp(err);
@@ -214,6 +268,27 @@
 			}
 			if(mouse.buttons & 4)
 				menu();
+			break;
+		case Eplumb:
+			pm = e.v;
+			if(pm->ndata == 0)
+				break;
+			s = plumblookup(pm->attr, "action");
+			if(s && strcmp(s, "showdata") == 0)
+				file = pshowdata(pm);
+			else
+				file = pfilename(pm);
+			if(!file){
+				mesg("invalid plumb data");
+				plumbfree(pm);
+				break;
+			}
+			plumbfree(pm);
+			t = tget(file);
+			if(t)
+				drawthing(t, 1);
+			flushimage(display, 1);
+			file = 0;
 		}
 }
 
@@ -531,6 +606,12 @@
 	nt->tr.max.y = nt->tr.min.y + nl;
 	nt->er.max.y = nt->tr.max.y + Border;
 	text(nt);
+
+	mesg("");
+	if(blockinput){
+		flushevents(Emouse|Ekeyboard);
+		blockinput = 0;
+	}
 }
 
 int
@@ -557,6 +638,9 @@
 	uchar buf[256];
 	char *data;
 
+	mesg("reading %s", file);
+	blockinput = 1;
+
 	buf[0] = '\0';
 	errstr((char*)buf, sizeof buf);	/* flush pending error message */
 	memmove(oerr, err, sizeof err);
@@ -1992,6 +2076,17 @@
 }
 
 void
+cleantmpfiles(void)
+{
+	char *s;
+	for(tmpindex--; tmpindex >= 0; tmpindex--){
+		s = smprint("/tmp/tweaktmp.%d.%d", getpid(), tmpindex);
+		remove(s);
+		free(s);
+	}
+}
+
+void
 menu(void)
 {
 	Thing *t;
@@ -2041,6 +2136,7 @@
 		buttons(Down);
 		if(mouse.buttons == 4){
 			buttons(Up);
+			cleantmpfiles();
 			exits(0);
 		}
 		buttons(Up);

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [9front] patch: tweak plumb support
  2020-12-07  9:44     ` sirjofri+ml-9front
@ 2020-12-07 15:43       ` ori
  2020-12-07 20:26         ` sirjofri+ml-9front
  0 siblings, 1 reply; 9+ messages in thread
From: ori @ 2020-12-07 15:43 UTC (permalink / raw)
  To: sirjofri+ml-9front, 9front

I wonder if this'd better be done with a more complex plumb rule,

Quoth sirjofri+ml-9front@sirjofri.de:
> diff -r 2647ea8b9793 sys/man/1/tweak
> --- a/sys/man/1/tweak	Sun Dec 06 21:52:01 2020 +0100
> +++ b/sys/man/1/tweak	Mon Dec 07 10:38:50 2020 +0100
> @@ -155,6 +155,18 @@
>  Quit
>  .IR tweak .
>  The program will complain once about modified but unwritten files.
> +.PP
> +.I Tweak
> +listens to the
> +.I plumber
> +channel
> +.B imageedit
> +for filenames as well as image data. Plumbed image data is stored as files in
> +.B /tmp
> +and is
> +.I not
> +cleaned after exiting
> 

I don't think leaving trash in /tmp is really acceptable :/

+				file = smprint("/tmp/tweaktmp.%d", rand());

Use the pid, that way you know nobody else is using it,
and you can get rid of the retry loop.

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [9front] patch: tweak plumb support
  2020-12-05 15:13   ` sirjofri+ml-9front
@ 2020-12-07  9:44     ` sirjofri+ml-9front
  2020-12-07 15:43       ` ori
  0 siblings, 1 reply; 9+ messages in thread
From: sirjofri+ml-9front @ 2020-12-07  9:44 UTC (permalink / raw)
  To: 9front

[-- Attachment #1: Type: text/plain, Size: 71 bytes --]

Good morning,

I found a bug and made a fix. Here it is!

sirjofri

.


[-- Attachment #2: Type: text/plain, Size: 3340 bytes --]

diff -r 2647ea8b9793 sys/man/1/tweak
--- a/sys/man/1/tweak	Sun Dec 06 21:52:01 2020 +0100
+++ b/sys/man/1/tweak	Mon Dec 07 10:38:50 2020 +0100
@@ -155,6 +155,18 @@
 Quit
 .IR tweak .
 The program will complain once about modified but unwritten files.
+.PP
+.I Tweak
+listens to the
+.I plumber
+channel
+.B imageedit
+for filenames as well as image data. Plumbed image data is stored as files in
+.B /tmp
+and is
+.I not
+cleaned after exiting
+.IR tweak .
 .SH SOURCE
 .B /sys/src/cmd/tweak.c
 .SH "SEE ALSO"
diff -r 2647ea8b9793 sys/src/cmd/tweak.c
--- a/sys/src/cmd/tweak.c	Sun Dec 06 21:52:01 2020 +0100
+++ b/sys/src/cmd/tweak.c	Mon Dec 07 10:38:50 2020 +0100
@@ -3,8 +3,11 @@
 #include <draw.h>
 #include <cursor.h>
 #include <event.h>
+#include <plumb.h>
 #include <bio.h>
 
+#define Eplumb 128
+
 typedef struct	Thing	Thing;
 
 struct Thing
@@ -157,6 +160,7 @@
 int		but1val = 0;
 int		but2val = 255;
 int		invert = 0;
+int		blockinput = 0;
 Image		*values[256];
 Image		*greyvalues[256];
 uchar		data[8192];
@@ -172,12 +176,25 @@
 void	tclose1(Thing*);
 
 void
+flushevents(ulong keys)
+{
+	Event e;
+	if(keys == 0)
+		return;
+	while(ecanread(keys))
+		eread(keys, &e);
+}
+
+void
 main(int argc, char *argv[])
 {
-	int i;
+	int i, n;
 	Event e;
 	Thing *t;
+	Plumbmsg *pm;
+	char *s;
 
+	srand(time(0));
 	mag = Mag;
 	if(initdraw(error, 0, "tweak") < 0){
 		fprint(2, "tweak: initdraw failed: %r\n");
@@ -190,6 +207,7 @@
 			drawerror(display, "can't allocate image");
 	}
 	einit(Emouse|Ekeyboard);
+	eplumb(Eplumb, "imageedit");
 	eresized(0);
 	i = 1;
 	setjmp(err);
@@ -214,6 +232,63 @@
 			}
 			if(mouse.buttons & 4)
 				menu();
+			break;
+		case Eplumb:
+			pm = e.v;
+			if(pm->ndata == 0)
+				break;
+			s = plumblookup(pm->attr, "action");
+			if(s && strcmp(s, "showdata") == 0){
+				n = 0;
+			   Tmpretry:
+				file = smprint("/tmp/tweaktmp.%d", rand());
+				if(file == 0){
+					mesg("malloc failed: %r");
+					goto Plumbout;
+				}
+				i = create(file, OWRITE|OEXCL, 0600);
+				if(i == 0){
+					free(file);
+					if(n++ < 10)
+						goto Tmpretry;
+					else{
+						mesg("cannot create tmpfiles: too many attempts");
+						goto Plumberrout;
+					}
+				}
+				if(write(i, pm->data, pm->ndata) != pm->ndata){
+					mesg("error writing tmpfile: %r");
+					close(i);
+					free(file);
+					goto Plumberrout;
+				}
+				close(i);
+				goto Plumbout;
+			}
+			file = malloc(pm->ndata+1);
+			if(file == 0){
+				mesg("malloc failed: %r");
+				goto Plumberrout;
+			}
+			strncpy(file, pm->data, pm->ndata);
+			file[pm->ndata] = 0;
+			free(pm->data);
+			pm->data = file;
+			switch(pm->data[0]){
+			case '/':
+				file = pm->data;
+				break;
+			default:
+				file = smprint("%s/%s", pm->wdir, pm->data);
+			}
+		   Plumbout:
+			t = tget(file);
+			if(t)
+				drawthing(t, 1);
+			flushimage(display, 1);
+			file = 0;
+		   Plumberrout:
+			plumbfree(pm);
 		}
 }
 
@@ -531,6 +606,12 @@
 	nt->tr.max.y = nt->tr.min.y + nl;
 	nt->er.max.y = nt->tr.max.y + Border;
 	text(nt);
+
+	mesg("");
+	if(blockinput){
+		flushevents(Emouse|Ekeyboard);
+		blockinput = 0;
+	}
 }
 
 int
@@ -557,6 +638,9 @@
 	uchar buf[256];
 	char *data;
 
+	mesg("reading %s", file);
+	blockinput = 1;
+
 	buf[0] = '\0';
 	errstr((char*)buf, sizeof buf);	/* flush pending error message */
 	memmove(oerr, err, sizeof err);

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [9front] patch: tweak plumb support
  2020-12-04 10:47 ` sirjofri+ml-9front
@ 2020-12-05 15:13   ` sirjofri+ml-9front
  2020-12-07  9:44     ` sirjofri+ml-9front
  0 siblings, 1 reply; 9+ messages in thread
From: sirjofri+ml-9front @ 2020-12-05 15:13 UTC (permalink / raw)
  To: 9front

[-- Attachment #1: Type: text/plain, Size: 426 bytes --]

Hello,

this version adds experimental plumb support for direct file data
(action=showdata).  It just creates temporary files in
/tmp/tweaktmp.XXX, and does currently not clean the files after
closing (this could also be considered a feature).

The patch also includes adjustments to the man page.

If you want to test it, you need to "create" the imageedit channel:

echo plumb to imageedit >> /mnt/plumb/rules


sirjofri

.

[-- Attachment #2: Type: text/plain, Size: 3313 bytes --]

diff -r bdf5af28b857 sys/man/1/tweak
--- a/sys/man/1/tweak	Fri Dec 04 09:33:55 2020 +0100
+++ b/sys/man/1/tweak	Sat Dec 05 16:04:47 2020 +0100
@@ -155,6 +155,18 @@
 Quit
 .IR tweak .
 The program will complain once about modified but unwritten files.
+.PP
+.I Tweak
+listens to the
+.I plumber
+channel
+.B imageedit
+for filenames as well as image data. Plumbed image data is stored as files in
+.B /tmp
+and is
+.I not
+cleaned after exiting
+.IR tweak .
 .SH SOURCE
 .B /sys/src/cmd/tweak.c
 .SH "SEE ALSO"
diff -r bdf5af28b857 sys/src/cmd/tweak.c
--- a/sys/src/cmd/tweak.c	Fri Dec 04 09:33:55 2020 +0100
+++ b/sys/src/cmd/tweak.c	Sat Dec 05 16:04:47 2020 +0100
@@ -3,8 +3,11 @@
 #include <draw.h>
 #include <cursor.h>
 #include <event.h>
+#include <plumb.h>
 #include <bio.h>
 
+#define Eplumb 128
+
 typedef struct	Thing	Thing;
 
 struct Thing
@@ -157,6 +160,7 @@
 int		but1val = 0;
 int		but2val = 255;
 int		invert = 0;
+int		blockinput = 0;
 Image		*values[256];
 Image		*greyvalues[256];
 uchar		data[8192];
@@ -172,12 +176,24 @@
 void	tclose1(Thing*);
 
 void
+flushevents(ulong keys)
+{
+	Event e;
+	if(keys == 0)
+		return;
+	while(ecanread(keys))
+		eread(keys, &e);
+}
+
+void
 main(int argc, char *argv[])
 {
-	int i;
+	int i, n;
 	Event e;
 	Thing *t;
+	Plumbmsg *pm;
 
+	srand(time(0));
 	mag = Mag;
 	if(initdraw(error, 0, "tweak") < 0){
 		fprint(2, "tweak: initdraw failed: %r\n");
@@ -190,6 +206,7 @@
 			drawerror(display, "can't allocate image");
 	}
 	einit(Emouse|Ekeyboard);
+	eplumb(Eplumb, "imageedit");
 	eresized(0);
 	i = 1;
 	setjmp(err);
@@ -214,6 +231,62 @@
 			}
 			if(mouse.buttons & 4)
 				menu();
+			break;
+		case Eplumb:
+			pm = e.v;
+			if(pm->ndata == 0)
+				break;
+			if(strcmp(plumblookup(pm->attr, "action"), "showdata") == 0){
+				n = 0;
+			   Tmpretry:
+				file = smprint("/tmp/tweaktmp.%d", rand());
+				if(file == 0){
+					mesg("malloc failed: %r");
+					goto Plumbout;
+				}
+				i = create(file, OWRITE|OEXCL, 0600);
+				if(i == 0){
+					free(file);
+					if(n++ < 10)
+						goto Tmpretry;
+					else{
+						mesg("cannot create tmpfiles: too many attempts");
+						goto Plumberrout;
+					}
+				}
+				if(write(i, pm->data, pm->ndata) != pm->ndata){
+					mesg("error writing tmpfile: %r");
+					close(i);
+					free(file);
+					goto Plumberrout;
+				}
+				close(i);
+				goto Plumbout;
+			}
+			file = malloc(pm->ndata+1);
+			if(file == 0){
+				mesg("malloc failed: %r");
+				goto Plumberrout;
+			}
+			strncpy(file, pm->data, pm->ndata);
+			file[pm->ndata] = 0;
+			free(pm->data);
+			pm->data = file;
+			switch(pm->data[0]){
+			case '/':
+				file = pm->data;
+				break;
+			default:
+				file = smprint("%s/%s", pm->wdir, pm->data);
+			}
+		   Plumbout:
+			t = tget(file);
+			if(t)
+				drawthing(t, 1);
+			flushimage(display, 1);
+			file = 0;
+		   Plumberrout:
+			plumbfree(pm);
 		}
 }
 
@@ -531,6 +604,12 @@
 	nt->tr.max.y = nt->tr.min.y + nl;
 	nt->er.max.y = nt->tr.max.y + Border;
 	text(nt);
+
+	mesg("");
+	if(blockinput){
+		flushevents(Emouse|Ekeyboard);
+		blockinput = 0;
+	}
 }
 
 int
@@ -557,6 +636,9 @@
 	uchar buf[256];
 	char *data;
 
+	mesg("reading %s", file);
+	blockinput = 1;
+
 	buf[0] = '\0';
 	errstr((char*)buf, sizeof buf);	/* flush pending error message */
 	memmove(oerr, err, sizeof err);

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [9front] patch: tweak plumb support
  2020-12-04 10:30 sirjofri+ml-9front
@ 2020-12-04 10:47 ` sirjofri+ml-9front
  2020-12-05 15:13   ` sirjofri+ml-9front
  0 siblings, 1 reply; 9+ messages in thread
From: sirjofri+ml-9front @ 2020-12-04 10:47 UTC (permalink / raw)
  To: 9front

[-- Attachment #1: Type: text/plain, Size: 140 bytes --]

hello again,

second version, using smprint instead of strcpy hackery (I tried to not use stdio.h and didn't know of smprint).

sirjofri

.

[-- Attachment #2: Type: text/plain, Size: 2041 bytes --]

diff -r 73caf2698436 sys/src/cmd/tweak.c
--- a/sys/src/cmd/tweak.c	Tue Dec 01 09:23:16 2020 +0100
+++ b/sys/src/cmd/tweak.c	Fri Dec 04 11:45:15 2020 +0100
@@ -3,8 +3,11 @@
 #include <draw.h>
 #include <cursor.h>
 #include <event.h>
+#include <plumb.h>
 #include <bio.h>
 
+#define Eplumb 128
+
 typedef struct	Thing	Thing;
 
 struct Thing
@@ -157,6 +160,7 @@
 int		but1val = 0;
 int		but2val = 255;
 int		invert = 0;
+int		blockinput = 0;
 Image		*values[256];
 Image		*greyvalues[256];
 uchar		data[8192];
@@ -172,11 +176,22 @@
 void	tclose1(Thing*);
 
 void
+flushevents(ulong keys)
+{
+	Event e;
+	if(keys == 0)
+		return;
+	while(ecanread(keys))
+		eread(keys, &e);
+}
+
+void
 main(int argc, char *argv[])
 {
 	int i;
 	Event e;
 	Thing *t;
+	Plumbmsg *pm;
 
 	mag = Mag;
 	if(initdraw(error, 0, "tweak") < 0){
@@ -190,6 +205,7 @@
 			drawerror(display, "can't allocate image");
 	}
 	einit(Emouse|Ekeyboard);
+	eplumb(Eplumb, "imageedit");
 	eresized(0);
 	i = 1;
 	setjmp(err);
@@ -214,6 +230,34 @@
 			}
 			if(mouse.buttons & 4)
 				menu();
+			break;
+		case Eplumb:
+			pm = e.v;
+			if(pm->ndata == 0)
+				break;
+			file = malloc(pm->ndata+1);
+			if(file == 0){
+				mesg("malloc failed: %r");
+				goto Err;
+			}
+			strncpy(file, pm->data, pm->ndata);
+			file[pm->ndata] = 0;
+			free(pm->data);
+			pm->data = file;
+			switch(pm->data[0]){
+			case '/':
+				file = pm->data;
+				break;
+			default:
+				file = smprint("%s/%s", pm->wdir, pm->data);
+			}
+			t = tget(file);
+			if(t)
+				drawthing(t, 1);
+			flushimage(display, 1);
+			file = 0;
+		   Err:
+			plumbfree(pm);
 		}
 }
 
@@ -531,6 +575,12 @@
 	nt->tr.max.y = nt->tr.min.y + nl;
 	nt->er.max.y = nt->tr.max.y + Border;
 	text(nt);
+
+	mesg("");
+	if(blockinput){
+		flushevents(Emouse|Ekeyboard);
+		blockinput = 0;
+	}
 }
 
 int
@@ -557,6 +607,9 @@
 	uchar buf[256];
 	char *data;
 
+	mesg("reading %s", file);
+	blockinput = 1;
+
 	buf[0] = '\0';
 	errstr((char*)buf, sizeof buf);	/* flush pending error message */
 	memmove(oerr, err, sizeof err);

^ permalink raw reply	[flat|nested] 9+ messages in thread

* [9front] patch: tweak plumb support
@ 2020-12-04 10:30 sirjofri+ml-9front
  2020-12-04 10:47 ` sirjofri+ml-9front
  0 siblings, 1 reply; 9+ messages in thread
From: sirjofri+ml-9front @ 2020-12-04 10:30 UTC (permalink / raw)
  To: 9front

[-- Attachment #1: Type: text/plain, Size: 563 bytes --]

Hello all,

this patch enables basic plumbing in tweak.

Tweak listens to plumb channel 'imageedit' and opens the files (currently only paths are supported, no action=showdata).

Furthermore, while reading data from files a message is printed in the footer and all input events are removed from the stack afterwards. This results in blocked input while loading. Without this adjustment, the image is loaded and every input event is queued until loading is done, resulting in multiple 'open' prompts, for example.

Feel free to test, submit, comment.

sirjofri

.

[-- Attachment #2: Type: text/plain, Size: 2243 bytes --]

diff -r 73caf2698436 sys/src/cmd/tweak.c
--- a/sys/src/cmd/tweak.c	Tue Dec 01 09:23:16 2020 +0100
+++ b/sys/src/cmd/tweak.c	Fri Dec 04 11:23:24 2020 +0100
@@ -3,8 +3,11 @@
 #include <draw.h>
 #include <cursor.h>
 #include <event.h>
+#include <plumb.h>
 #include <bio.h>
 
+#define Eplumb 128
+
 typedef struct	Thing	Thing;
 
 struct Thing
@@ -157,6 +160,7 @@
 int		but1val = 0;
 int		but2val = 255;
 int		invert = 0;
+int		blockinput = 0;
 Image		*values[256];
 Image		*greyvalues[256];
 uchar		data[8192];
@@ -172,11 +176,22 @@
 void	tclose1(Thing*);
 
 void
+flushevents(ulong keys)
+{
+	Event e;
+	if(keys == 0)
+		return;
+	while(ecanread(keys))
+		eread(keys, &e);
+}
+
+void
 main(int argc, char *argv[])
 {
 	int i;
 	Event e;
 	Thing *t;
+	Plumbmsg *pm;
 
 	mag = Mag;
 	if(initdraw(error, 0, "tweak") < 0){
@@ -190,6 +205,7 @@
 			drawerror(display, "can't allocate image");
 	}
 	einit(Emouse|Ekeyboard);
+	eplumb(Eplumb, "imageedit");
 	eresized(0);
 	i = 1;
 	setjmp(err);
@@ -214,6 +230,42 @@
 			}
 			if(mouse.buttons & 4)
 				menu();
+			break;
+		case Eplumb:
+			pm = e.v;
+			if(pm->ndata == 0)
+				break;
+			file = malloc(pm->ndata+1);
+			if(file == 0){
+				mesg("malloc failed: %r");
+				goto Err;
+			}
+			strncpy(file, pm->data, pm->ndata);
+			file[pm->ndata] = 0;
+			free(pm->data);
+			pm->data = file;
+			switch(pm->data[0]){
+			case '/':
+				file = pm->data;
+				break;
+			default:
+				file = malloc(strlen(pm->wdir) + strlen(pm->data) + 2);
+				if(file == 0){
+					mesg("malloc failed: %r");
+					goto Err;
+				}
+	
+				strcpy(file, pm->wdir);
+				file[strlen(pm->wdir)] = '/';
+				strcpy(file+strlen(pm->wdir)+1, pm->data);
+			}
+			t = tget(file);
+			if(t)
+				drawthing(t, 1);
+			flushimage(display, 1);
+			file = 0;
+		   Err:
+			plumbfree(pm);
 		}
 }
 
@@ -531,6 +583,12 @@
 	nt->tr.max.y = nt->tr.min.y + nl;
 	nt->er.max.y = nt->tr.max.y + Border;
 	text(nt);
+
+	mesg("");
+	if(blockinput){
+		flushevents(Emouse|Ekeyboard);
+		blockinput = 0;
+	}
 }
 
 int
@@ -557,6 +615,9 @@
 	uchar buf[256];
 	char *data;
 
+	mesg("reading %s", file);
+	blockinput = 1;
+
 	buf[0] = '\0';
 	errstr((char*)buf, sizeof buf);	/* flush pending error message */
 	memmove(oerr, err, sizeof err);

^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2021-01-13 22:37 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-01-12 10:12 [9front] patch: tweak plumb support sirjofri
2021-01-13 22:06 ` ori
  -- strict thread matches above, loose matches on Subject: below --
2020-12-04 10:30 sirjofri+ml-9front
2020-12-04 10:47 ` sirjofri+ml-9front
2020-12-05 15:13   ` sirjofri+ml-9front
2020-12-07  9:44     ` sirjofri+ml-9front
2020-12-07 15:43       ` ori
2020-12-07 20:26         ` sirjofri+ml-9front
2020-12-21 15:34           ` sirjofri+ml-9front

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).