Re: [dev] dmenu-4.5

2012-01-11 Thread Džen
Hey Connor

I've discovered a little imperfection when running dmenu with the -f
flag (note that this is really a tiny issue and maybe it would be better
to just ignore it).

Due to the fact that dmenu locks up the keyboard before reading data
from stdin, the user isn't able to exit dmenu while it is waiting for
(or reading) data from stdin. As far as I know the only solution would
be to switch over to a tty outside of X, then send a kill signal to
dmenu. This can be annoying if dmenu has to wait for data for a long
time or no data is coming at all, and the user simply wants to exit
dmenu.

IMO, dmenu should always exit if XK_Escape or Ctrl-C is pressed. This
could be solved by select()'ing through the stdin fd and X fd, in order
to be able to handle KeyPress events from X.

The attached patch works for me, however I'm not sure whether this is an
elegant solution. A possible issue with this patch might be the loss of
speed while reading data from stdin (due to the select() calls, etc).

What's your opinion?

-- 
Džen

On 08/01/12 12:37pm, Connor Lane Smith wrote:
 Hey all,
 
 I've released dmenu-4.5 [1][2]. A summary of changes:
 
   * Detects monitor based on input focus, not just pointer location.
   * Token matching instead of just substrings.
   * XIM composition support.
   * Paste from clipboard with C-S-y.
   * stest(1) replaces lsx(1).
   * M-[Gghjkl] restored for vi-like navigation.
   * And a couple of bug fixes.
 
 Finally, I probably won't be doing any more active development for
 dmenu, though I'll help fix any bugs if they come up.
 
 [1]: http://tools.suckless.org/dmenu
 [2]: http://hg.suckless.org/dmenu
 
 Thanks,
 cls
 
diff -r c596c060aedb dmenu.c
--- a/dmenu.c   Sun Jan 08 13:13:00 2012 +0100
+++ b/dmenu.c   Wed Jan 11 11:53:07 2012 +0100
@@ -4,6 +4,7 @@
 #include stdlib.h
 #include string.h
 #include strings.h
+#include errno.h
 #include unistd.h
 #include X11/Xlib.h
 #include X11/Xatom.h
@@ -466,20 +467,59 @@
 void
 readstdin(void) {
char buf[sizeof text], *p, *maxstr = NULL;
-   size_t i, max = 0, size = 0;
+   size_t i = 0, max = 0, size = 0;
 
-   /* read each line from stdin and add it to the item list */
-   for(i = 0; fgets(buf, sizeof buf, stdin); i++) {
-   if(i+1 = size / sizeof *items)
-   if(!(items = realloc(items, (size += BUFSIZ
-   eprintf(cannot realloc %u bytes:, size);
-   if((p = strchr(buf, '\n')))
-   *p = '\0';
-   if(!(items[i].text = strdup(buf)))
-   eprintf(cannot strdup %u bytes:, strlen(buf)+1);
-   if(strlen(items[i].text)  max)
+   fd_set set;
+   struct timeval timeout;
+   int ret, xfd;
+   XEvent ev;
+   KeySym ksym = NoSymbol;
+
+   xfd = ConnectionNumber(dc-dpy);
+
+   while (!feof(stdin)) {
+   timeout.tv_sec = 0;
+   timeout.tv_usec = 0;
+
+   FD_ZERO(set);
+   FD_SET(STDIN_FILENO, set);
+   FD_SET(xfd, set);
+
+   if ((ret = select(xfd + 1, set, NULL, NULL, timeout))  0) {
+   if (errno == EINTR)
+   continue;
+   eprintf(select(): %s, strerror(errno));
+   }
+
+   /* exit dmenu on XK_Escape or Ctrl-C */
+   if (FD_ISSET(xfd, set)) {
+   while (XPending(dc-dpy)) {
+   XNextEvent(dc-dpy, ev);
+
+   if (ev.type != KeyPress)
+   continue;
+
+   ksym = XLookupKeysym(ev.xkey, 0);
+   if (ksym == XK_Escape || (ev.xkey.state  
ControlMask  ksym == XK_c))
+   exit(EXIT_FAILURE);
+   }
+   }
+
+   /* read a line from stdin and add it to the item list */
+   else if (FD_ISSET(STDIN_FILENO, set)  fgets(buf, sizeof buf, 
stdin)) {
+   if(i+1 = size / sizeof *items)
+   if(!(items = realloc(items, (size += BUFSIZ
+   eprintf(cannot realloc %u bytes:, 
size);
+   if((p = strchr(buf, '\n')))
+   *p = '\0';
+   if(!(items[i].text = strdup(buf)))
+   eprintf(cannot strdup %u bytes:, 
strlen(buf)+1);
+   if(strlen(items[i].text)  max)
max = strlen(maxstr = items[i].text);
+   i++;
+   }
}
+
if(items)
items[i].text = NULL;
inputw = maxstr ? textw(dc, maxstr) : 0;


Re: [dev] dmenu-4.5

2012-01-11 Thread Rob
On 11 January 2012 10:55, Džen yvl...@gmail.com wrote:
 Hey Connor

 I've discovered a little imperfection when running dmenu with the -f
 flag (note that this is really a tiny issue and maybe it would be better
 to just ignore it).

 Due to the fact that dmenu locks up the keyboard before reading data
 from stdin, the user isn't able to exit dmenu while it is waiting for
 (or reading) data from stdin. As far as I know the only solution would
 be to switch over to a tty outside of X, then send a kill signal to
 dmenu. This can be annoying if dmenu has to wait for data for a long
 time or no data is coming at all, and the user simply wants to exit
 dmenu.

This is a known bug, we decided to leave it as it is, for the time
being, because we couldn't think of a simple way to solve it, and I
think there's a note somewhere about only using -f if dmenu is reading
from a non-tty. I think I did have dmenu set to print a warning if stdin
was a tty and -f was given, but I don't know what happened to it.

 IMO, dmenu should always exit if XK_Escape or Ctrl-C is pressed. This
 could be solved by select()'ing through the stdin fd and X fd, in order
 to be able to handle KeyPress events from X.

I hadn't thought of this solution, it increases the complexity, so I
suppose it's up to Connor, I'm at work at the moment so I can't test it
out either.

 The attached patch works for me, however I'm not sure whether this is an
 elegant solution. A possible issue with this patch might be the loss of
 speed while reading data from stdin (due to the select() calls, etc).

 What's your opinion?

I wouldn't imagine a few extra select calls would matter. What if we
used select() until we got input on stdin, then just read all of stdin
at once, not bothering to select() again? Unless you're reading over a
network, I don't think there's that much of a delay.

Cheers,
Rob



[dev] dmenu-4.5

2012-01-08 Thread Connor Lane Smith
Hey all,

I've released dmenu-4.5 [1][2]. A summary of changes:

  * Detects monitor based on input focus, not just pointer location.
  * Token matching instead of just substrings.
  * XIM composition support.
  * Paste from clipboard with C-S-y.
  * stest(1) replaces lsx(1).
  * M-[Gghjkl] restored for vi-like navigation.
  * And a couple of bug fixes.

Finally, I probably won't be doing any more active development for
dmenu, though I'll help fix any bugs if they come up.

[1]: http://tools.suckless.org/dmenu
[2]: http://hg.suckless.org/dmenu

Thanks,
cls



Re: [dev] dmenu-4.5

2012-01-08 Thread Bastien Dejean
Connor Lane Smith:

   * M-[Gghjkl] restored for vi-like navigation.

When lines  1, I would expect M-j to select the next item (because it's
below the current one), but I have to press M-l instead.

-- 
 b.d
(| |)
 ^ ^