Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

patch: grab keyboard input #15

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions xnotify.1
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ is the same as
.BI "\-h " height
Maximum height (in pixels) of a notification window.
.TP
.BI \-k
Makes xnotify grab keyboard focus, and close notifications with ESC key.
.TP
.BI "\-m " monitor
Makes xnotify be displayed on the specified monitor.
Monitor numbers start from 0.
Expand Down
23 changes: 20 additions & 3 deletions xnotify.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <X11/Xatom.h>
#include <X11/Xresource.h>
#include <X11/Xft/Xft.h>
#include <X11/keysymdef.h>
#include <X11/extensions/Xinerama.h>
#include <Imlib2.h>
#include "xnotify.h"
Expand All @@ -35,6 +36,7 @@ static struct Ellipsis ellipsis;

/* flags */
static int oflag = 0; /* whether only one notification must exist at a time */
static int kflag = 0; /* whether to grab keyboard input */
volatile sig_atomic_t usrflag; /* 1 if for SIGUSR1, 2 for SIGUSR2, 0 otherwise */

/* include configuration structure */
Expand All @@ -44,7 +46,7 @@ volatile sig_atomic_t usrflag; /* 1 if for SIGUSR1, 2 for SIGUSR2, 0 otherwise
void
usage(void)
{
(void)fprintf(stderr, "usage: xnotify [-o] [-G gravity] [-b button] [-g geometry] [-h height] [-m monitor] [-s seconds]\n");
(void)fprintf(stderr, "usage: xnotify [-o] [-k] [-G gravity] [-b button] [-g geometry] [-h height] [-m monitor] [-s seconds]\n");
exit(1);
}

Expand Down Expand Up @@ -116,7 +118,7 @@ getoptions(int argc, char *argv[])
unsigned long n;
int ch;

while ((ch = getopt(argc, argv, "G:b:g:h:m:os:")) != -1) {
while ((ch = getopt(argc, argv, "G:b:g:h:km:os:")) != -1) {
switch (ch) {
case 'G':
config.gravityspec = optarg;
Expand Down Expand Up @@ -157,6 +159,10 @@ getoptions(int argc, char *argv[])
case 'o':
oflag = 1;
break;
case 'k':
oflag = 1;
kflag = 1;
break;
case 's':
if ((n = strtoul(optarg, NULL, 10)) < INT_MAX)
config.sec = n;
Expand Down Expand Up @@ -943,6 +949,7 @@ delitem(struct Queue *queue, struct Item *item)
for (i = 0; i < item->nlines; i++)
free(item->line[i]);
XFreePixmap(dpy, item->pixmap);
if(kflag) XUngrabKeyboard(dpy, CurrentTime);
XDestroyWindow(dpy, item->win);
if (item->prev)
item->prev->next = item->next;
Expand Down Expand Up @@ -1064,8 +1071,10 @@ readevent(struct Queue *queue)
while (XPending(dpy) && !XNextEvent(dpy, &ev)) {
switch (ev.type) {
case Expose:
if (ev.xexpose.count == 0 && (item = getitem(queue, ev.xexpose.window)) != NULL)
if (ev.xexpose.count == 0 && (item = getitem(queue, ev.xexpose.window)) != NULL) {
copypixmap(item);
if(kflag) XGrabKeyboard(dpy, ev.xexpose.window, False, GrabModeAsync, GrabModeAsync, CurrentTime);
}
break;
case ButtonPress:
if ((item = getitem(queue, ev.xbutton.window)) == NULL)
Expand All @@ -1074,6 +1083,14 @@ readevent(struct Queue *queue)
cmditem(item);
delitem(queue, item);
break;
case KeyPress:
if ((item = getitem(queue, ev.xkey.window)) == NULL)
break;
if ((XLookupKeysym(&ev.xkey, 0) == XK_Escape) ) {
if (item->cmd) cmditem(item);
delitem(queue, item);
}
break;
case MotionNotify:
if ((item = getitem(queue, ev.xmotion.window)) != NULL)
resettime(item);
Expand Down