Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
add basic support for the mouse
Basic support for the mouse is provided to be able to activate a frame
when clicking on it.

To activate the behavior, the mousefocuspolicy variable must be set:

set mousefocuspolicy click

Currently mousefocuspolicy only accept two values: none (no mouse
support at all) and click (activate a frame on click).
  • Loading branch information
parkouss committed Feb 1, 2017
1 parent c24d1e3 commit 15c2a0a
Show file tree
Hide file tree
Showing 8 changed files with 113 additions and 2 deletions.
62 changes: 62 additions & 0 deletions src/actions.c
Expand Up @@ -162,6 +162,7 @@ static cmdret * set_inputwidth (struct cmdarg **args);
static cmdret * set_waitcursor (struct cmdarg **args);
static cmdret * set_winfmt (struct cmdarg **args);
static cmdret * set_winname (struct cmdarg **args);
static cmdret * set_mousefocuspolicy (struct cmdarg **args);
static cmdret * set_framefmt (struct cmdarg **args);
static cmdret * set_fgcolor (struct cmdarg **args);
static cmdret * set_bgcolor (struct cmdarg **args);
Expand Down Expand Up @@ -373,6 +374,7 @@ init_set_vars (void)
add_set_var ("wingravity", set_wingravity, 1, "", arg_GRAVITY);
add_set_var ("winliststyle", set_winliststyle, 1, "", arg_STRING);
add_set_var ("winname", set_winname, 1, "", arg_STRING);
add_set_var ("mousefocuspolicy", set_mousefocuspolicy, 1, "", arg_STRING);
}

/* i_nrequired is the number required when called
Expand Down Expand Up @@ -1258,6 +1260,30 @@ ungrab_rat (void)
XUngrabPointer (dpy, CurrentTime);
}

static void
grab_button (void)
{
int j;
for (j=0; j<num_screens; j++)
{
rp_screen *screen = &screens[j];
XGrabButton(dpy, AnyButton, AnyModifier, screen->root,
True, ButtonPressMask,
GrabModeSync, GrabModeAsync, None, None);
}
}

static void
ungrab_button (void)
{
int j;
for (j=0; j<num_screens; j++)
{
rp_screen *screen = &screens[j];
XUngrabButton(dpy, AnyButton, AnyModifier, screen->root);
}
}

/* Unmanage window */
cmdret *
cmd_unmanage (int interactive, struct cmdarg **args)
Expand Down Expand Up @@ -4303,6 +4329,42 @@ set_winname (struct cmdarg **args)
return cmdret_new (RET_SUCCESS, NULL);
}

static cmdret *
set_mousefocuspolicy(struct cmdarg **args)
{
char *policy;

if (args[0] == NULL)
switch (defaults.mouse_focus_policy)
{
case MOUSE_FOCUS_POLICY_NONE:
return cmdret_new (RET_SUCCESS, "none");
case MOUSE_FOCUS_POLICY_CLICK:
return cmdret_new (RET_SUCCESS, "click");
default:
PRINT_DEBUG (("Unknown mouse_focus_policy\n"));
return cmdret_new (RET_FAILURE, "unknown");
}

policy = ARG_STRING(0);

if (!strncmp (policy, "none", sizeof ("none")))
{
defaults.mouse_focus_policy = MOUSE_FOCUS_POLICY_NONE;
ungrab_button();
}
else if (!strncmp (policy, "click", sizeof ("click")))
{
defaults.mouse_focus_policy = MOUSE_FOCUS_POLICY_CLICK;
grab_button();
}
else
return cmdret_new (RET_FAILURE,
"set mousefocuspolicy: invalid argument `%s'", policy);

return cmdret_new (RET_SUCCESS, NULL);
}

static cmdret *
set_framefmt (struct cmdarg **args)
{
Expand Down
3 changes: 3 additions & 0 deletions src/data.h
Expand Up @@ -275,6 +275,9 @@ struct rp_defaults

/* Frame indicator format */
char *frame_fmt;

/* Window focus policy for the mouse: none or click */
int mouse_focus_policy;
};

/* Information about a child process. */
Expand Down
20 changes: 19 additions & 1 deletion src/events.c
Expand Up @@ -793,6 +793,19 @@ selection_clear (void)
selection.len = 0;
}

static void
button_press (XButtonEvent * ev)
{
rp_frame * frame;

if (defaults.mouse_focus_policy == MOUSE_FOCUS_POLICY_CLICK)
{
frame = find_frame_at_cursor_pos (ev->x_root, ev->y_root);
if (frame)
set_active_frame(frame, 1);
}
}

/* Given an event, call the correct function to handle it. */
static void
delegate_event (XEvent *ev)
Expand Down Expand Up @@ -871,7 +884,12 @@ delegate_event (XEvent *ev)
PRINT_DEBUG (("--- Handling ConfigureNotify ---\n"));
configure_notify( &ev->xconfigure );
break;


case ButtonPress:
XAllowEvents (dpy, ReplayPointer, CurrentTime); /* ReplayPointer resends the mouse event */
button_press(&ev->xbutton);
break;

case MapNotify:
case Expose:
case MotionNotify:
Expand Down
4 changes: 4 additions & 0 deletions src/globals.h
Expand Up @@ -57,6 +57,10 @@
#define WIN_NAME_RES_CLASS 1
#define WIN_NAME_RES_NAME 2

/* Possible values for defaults.mouse_focus_policy */
#define MOUSE_FOCUS_POLICY_NONE 0
#define MOUSE_FOCUS_POLICY_CLICK 1

/* Possible directions to traverse the completions list. */
#define COMPLETION_NEXT 0
#define COMPLETION_PREVIOUS 1
Expand Down
2 changes: 2 additions & 0 deletions src/main.c
Expand Up @@ -583,6 +583,8 @@ init_defaults (void)
defaults.history_expansion = False;
defaults.frame_selectors = xstrdup ("");
defaults.maxundos = 20;

defaults.mouse_focus_policy = MOUSE_FOCUS_POLICY_NONE;
}

int
Expand Down
2 changes: 1 addition & 1 deletion src/screen.c
Expand Up @@ -269,7 +269,7 @@ init_screen (rp_screen *s, int screen_num)
XSelectInput(dpy, RootWindow (dpy, screen_num),
PropertyChangeMask | ColormapChangeMask
| SubstructureRedirectMask | SubstructureNotifyMask
| StructureNotifyMask);
| StructureNotifyMask | ButtonPressMask);
XSync (dpy, False);

/* Set the numset for the frames to our global numset. */
Expand Down
21 changes: 21 additions & 0 deletions src/split.c
Expand Up @@ -1088,3 +1088,24 @@ find_frame_number (int num)

return NULL;
}

rp_frame *
find_frame_at_cursor_pos (int x, int y)
{
int i;
rp_frame *cur;

for (i=0; i<num_screens; i++)
{
rp_screen *s = &screens[i];

list_for_each_entry (cur, &s->frames, node)
{
if (x >= (s->left + cur->x) && x <= (s->left + cur->x + cur->width)
&& y >= (s->top + cur->y) && y <= (s->top + cur->y + cur->height))
return cur;
}
}

return NULL;
}
1 change: 1 addition & 0 deletions src/split.h
Expand Up @@ -56,5 +56,6 @@ rp_frame *find_last_frame (void);
rp_frame * find_frame_number (int num);

rp_frame *current_frame (void);
rp_frame *find_frame_at_cursor_pos (int x, int y);

#endif

0 comments on commit 15c2a0a

Please sign in to comment.