This repository has been archived on 2024-09-14. You can view files and clone it, but cannot push or open issues or pull requests.
samu/kbfunc.c

813 lines
18 KiB
C
Raw Permalink Normal View History

2024-09-14 13:22:36 +02:00
/*
*
* Copyright (c) 2004 Martin Murray <mmurray@monkey.org>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*
* $OpenBSD$
*/
/* For FreeBSD. */
#define _WITH_GETLINE
#include <sys/types.h>
#include "queue.h"
#include <sys/stat.h>
#include <dirent.h>
#include <err.h>
#include <errno.h>
#include <limits.h>
#include <paths.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "samu.h"
#define HASH_MARKER "|1|"
extern sig_atomic_t cwm_status;
static void kbfunc_amount(int, int, int *, int *);
static void kbfunc_client_move_kb(void *, struct cargs *);
static void kbfunc_client_move_mb(void *, struct cargs *);
static void kbfunc_client_resize_kb(void *, struct cargs *);
static void kbfunc_client_resize_mb(void *, struct cargs *);
static void kbfunc_client_resize_mb_original(struct screen_ctx *, struct client_ctx *);
static void kbfunc_client_resize_mb_corners(struct screen_ctx *, struct client_ctx *);
void
kbfunc_cwm_status(void *ctx, struct cargs *cargs)
{
cwm_status = cargs->flag;
}
static void
kbfunc_amount(int flags, int amt, int *mx, int *my)
{
#define CWM_FACTOR 10
if (flags & CWM_BIGAMOUNT)
amt *= CWM_FACTOR;
switch (flags & (CWM_UP | CWM_DOWN | CWM_LEFT | CWM_RIGHT)) {
case CWM_UP:
*my -= amt;
break;
case CWM_DOWN:
*my += amt;
break;
case CWM_RIGHT:
*mx += amt;
break;
case CWM_LEFT:
*mx -= amt;
break;
}
}
void
kbfunc_ptrmove(void *ctx, struct cargs *cargs)
{
struct screen_ctx *sc = ctx;
int x, y;
int mx = 0, my = 0;
kbfunc_amount(cargs->flag, Conf.mamount, &mx, &my);
xu_ptr_get(sc->rootwin, &x, &y);
xu_ptr_set(sc->rootwin, x + mx, y + my);
}
void
kbfunc_client_move(void *ctx, struct cargs *cargs)
{
if (cargs->xev == CWM_XEV_BTN)
kbfunc_client_move_mb(ctx, cargs);
else
kbfunc_client_move_kb(ctx, cargs);
}
void
kbfunc_client_resize(void *ctx, struct cargs *cargs)
{
if (cargs->xev == CWM_XEV_BTN)
kbfunc_client_resize_mb(ctx, cargs);
else
kbfunc_client_resize_kb(ctx, cargs);
}
static void
kbfunc_client_move_kb(void *ctx, struct cargs *cargs)
{
struct client_ctx *cc = ctx;
struct screen_ctx *sc = cc->sc;
struct geom area;
int mx = 0, my = 0;
if (cc->flags & CLIENT_FREEZE)
return;
kbfunc_amount(cargs->flag, Conf.mamount, &mx, &my);
cc->geom.x += mx;
if (cc->geom.x < -(cc->geom.w + cc->bwidth - 1))
cc->geom.x = -(cc->geom.w + cc->bwidth - 1);
if (cc->geom.x > (sc->view.w - cc->bwidth - 1))
cc->geom.x = sc->view.w - cc->bwidth - 1;
cc->geom.y += my;
if (cc->geom.y < -(cc->geom.h + cc->bwidth - 1))
cc->geom.y = -(cc->geom.h + cc->bwidth - 1);
if (cc->geom.y > (sc->view.h - cc->bwidth - 1))
cc->geom.y = sc->view.h - cc->bwidth - 1;
area = screen_area(sc,
cc->geom.x + cc->geom.w / 2,
cc->geom.y + cc->geom.h / 2, 1);
cc->geom.x += client_snapcalc(cc->geom.x,
cc->geom.x + cc->geom.w + (cc->bwidth * 2),
area.x, area.x + area.w, sc->snapdist);
cc->geom.y += client_snapcalc(cc->geom.y,
cc->geom.y + cc->geom.h + (cc->bwidth * 2),
area.y, area.y + area.h, sc->snapdist);
client_move(cc);
client_ptr_inbound(cc, 1);
XSync(X_Dpy, True);
}
static void
kbfunc_client_move_mb(void *ctx, struct cargs *cargs)
{
struct client_ctx *cc = ctx;
XEvent ev;
Time ltime = 0;
struct screen_ctx *sc = cc->sc;
struct geom area;
int move = 1;
client_raise(cc);
if (cc->flags & CLIENT_FREEZE)
return;
client_ptr_inbound(cc, 1);
if (XGrabPointer(X_Dpy, cc->win, False, MOUSEMASK,
GrabModeAsync, GrabModeAsync, None, Conf.cursor[CF_MOVE],
CurrentTime) != GrabSuccess)
return;
screen_prop_win_create(sc, cc->win);
screen_prop_win_draw(sc, "%+5d%+5d", cc->geom.x, cc->geom.y);
while (move) {
XMaskEvent(X_Dpy, MOUSEMASK, &ev);
switch (ev.type) {
case MotionNotify:
/* not more than 60 times / second */
if ((ev.xmotion.time - ltime) <= (1000 / 60))
continue;
ltime = ev.xmotion.time;
cc->geom.x = ev.xmotion.x_root - cc->ptr.x - cc->bwidth;
cc->geom.y = ev.xmotion.y_root - cc->ptr.y - cc->bwidth;
area = screen_area(sc,
cc->geom.x + cc->geom.w / 2,
cc->geom.y + cc->geom.h / 2, 1);
cc->geom.x += client_snapcalc(cc->geom.x,
cc->geom.x + cc->geom.w + (cc->bwidth * 2),
area.x, area.x + area.w, sc->snapdist);
cc->geom.y += client_snapcalc(cc->geom.y,
cc->geom.y + cc->geom.h + (cc->bwidth * 2),
area.y, area.y + area.h, sc->snapdist);
client_move(cc);
screen_prop_win_draw(sc,
"%+5d%+5d", cc->geom.x, cc->geom.y);
break;
case ButtonRelease:
move = 0;
break;
}
}
if (ltime)
client_move(cc);
screen_prop_win_destroy(sc);
XUngrabPointer(X_Dpy, CurrentTime);
}
static void
kbfunc_client_resize_kb(void *ctx, struct cargs *cargs)
{
struct client_ctx *cc = ctx;
int mx = 0, my = 0;
int amt = 1;
if (cc->flags & CLIENT_FREEZE)
return;
if (!(cc->hint.flags & PResizeInc))
amt = Conf.mamount;
kbfunc_amount(cargs->flag, amt, &mx, &my);
if ((cc->geom.w += mx * cc->hint.incw) < cc->hint.minw)
cc->geom.w = cc->hint.minw;
if ((cc->geom.h += my * cc->hint.inch) < cc->hint.minh)
cc->geom.h = cc->hint.minh;
if (cc->geom.x + cc->geom.w + cc->bwidth - 1 < 0)
cc->geom.x = -(cc->geom.w + cc->bwidth - 1);
if (cc->geom.y + cc->geom.h + cc->bwidth - 1 < 0)
cc->geom.y = -(cc->geom.h + cc->bwidth - 1);
client_resize(cc, 1);
client_ptr_inbound(cc, 1);
XSync(X_Dpy, True);
}
static void
kbfunc_client_resize_mb(void *ctx, struct cargs *cargs)
{
struct client_ctx *cc = ctx;
struct screen_ctx *sc = cc->sc;
if (cc->flags & CLIENT_FREEZE)
return;
client_raise(cc);
client_ptr_save(cc);
if (Conf.resizeallcorners)
kbfunc_client_resize_mb_corners(sc, cc);
else
kbfunc_client_resize_mb_original(sc, cc);
/* Make sure the pointer stays within the window. */
client_ptr_inbound(cc, 0);
}
static void
kbfunc_client_resize_mb_original(struct screen_ctx *sc, struct client_ctx *cc)
{
int resize = 1;
Time ltime = 0;
XEvent ev;
xu_ptr_set(cc->win, cc->geom.w, cc->geom.h);
if (XGrabPointer(X_Dpy, cc->win, False, MOUSEMASK,
GrabModeAsync, GrabModeAsync, None, Conf.cursor[CF_RESIZE],
CurrentTime) != GrabSuccess)
return;
screen_prop_win_create(sc, cc->win);
screen_prop_win_draw(sc, "%4d x %-4d", cc->dim.w, cc->dim.h);
while (resize) {
XMaskEvent(X_Dpy, MOUSEMASK, &ev);
switch (ev.type) {
case MotionNotify:
/* not more than 60 times / second */
if ((ev.xmotion.time - ltime) <= (1000 / 60))
continue;
ltime = ev.xmotion.time;
cc->geom.w = ev.xmotion.x;
cc->geom.h = ev.xmotion.y;
client_apply_sizehints(cc);
client_resize(cc, 1);
screen_prop_win_draw(sc,
"%4d x %-4d", cc->dim.w, cc->dim.h);
break;
case ButtonRelease:
resize = 0;
break;
}
}
if (ltime)
client_resize(cc, 1);
screen_prop_win_destroy(sc);
XUngrabPointer(X_Dpy, CurrentTime);
}
static void
kbfunc_client_resize_mb_corners(struct screen_ctx *sc, struct client_ctx *cc)
{
Cursor cursor;
int resize = 1, orig_h, orig_w, orig_x, orig_y, x, y;
Time ltime = 0;
XEvent ev;
xu_ptr_get(sc->rootwin, &x, &y);
if (x < cc->geom.x + cc->geom.w / 2) {
if (y < cc->geom.y + cc->geom.h / 2)
cursor = Conf.cursor[CF_RESIZE_NW];
else
cursor = Conf.cursor[CF_RESIZE_SW];
} else {
if (y < cc->geom.y + cc->geom.h / 2)
cursor = Conf.cursor[CF_RESIZE_NE];
else
cursor = Conf.cursor[CF_RESIZE_SE];
}
if (XGrabPointer(X_Dpy, cc->win, False, MOUSEMASK,
GrabModeAsync, GrabModeAsync, None, cursor,
CurrentTime) != GrabSuccess)
return;
orig_x = cc->geom.x;
orig_y = cc->geom.y;
orig_w = cc->geom.w;
orig_h = cc->geom.h;
orig_x = cc->geom.x;
orig_y = cc->geom.y;
orig_w = cc->geom.w;
orig_h = cc->geom.h;
screen_prop_win_create(sc, cc->win);
screen_prop_win_draw(sc, "%4d x %-4d", cc->dim.w, cc->dim.h);
while (resize) {
XMaskEvent(X_Dpy, MOUSEMASK, &ev);
switch (ev.type) {
case MotionNotify:
/* not more than 60 times / second */
if ((ev.xmotion.time - ltime) <= (1000 / 60))
continue;
ltime = ev.xmotion.time;
if (x - orig_x < orig_w / 2) {
if (y - orig_y < orig_h / 2) {
cc->geom.x = orig_x + ev.xmotion.x_root - x;
cc->geom.w = orig_w - ev.xmotion.x_root + x;
cc->geom.y = orig_y + ev.xmotion.y_root - y;
cc->geom.h = orig_h - ev.xmotion.y_root + y;
client_apply_sizehints(cc);
cc->geom.x -= cc->geom.x + cc->geom.w - orig_x - orig_w;
cc->geom.y -= cc->geom.y + cc->geom.h - orig_y - orig_h;
} else {
cc->geom.x = orig_x + ev.xmotion.x_root - x;
cc->geom.w = orig_w - ev.xmotion.x_root + x;
cc->geom.h = orig_h + ev.xmotion.y_root - y;
client_apply_sizehints(cc);
cc->geom.x -= cc->geom.x + cc->geom.w - orig_x - orig_w;
}
} else {
if (y - orig_y < orig_h / 2) {
cc->geom.w = orig_w + ev.xmotion.x_root - x;
cc->geom.y = orig_y + ev.xmotion.y_root - y;
cc->geom.h = orig_h - ev.xmotion.y_root + y;
client_apply_sizehints(cc);
cc->geom.y -= cc->geom.y + cc->geom.h - orig_y - orig_h;
} else {
cc->geom.w = orig_w + ev.xmotion.x_root - x;
cc->geom.h = orig_h + ev.xmotion.y_root - y;
client_apply_sizehints(cc);
}
}
client_move(cc);
client_resize(cc, 0);
screen_prop_win_draw(sc,
"%4d x %-4d", cc->dim.w, cc->dim.h);
break;
case ButtonRelease:
resize = 0;
break;
}
}
if (ltime)
client_resize(cc, 1);
screen_prop_win_destroy(sc);
XUngrabPointer(X_Dpy, CurrentTime);
}
void
kbfunc_client_snap(void *ctx, struct cargs *cargs)
{
struct client_ctx *cc = ctx;
struct screen_ctx *sc = cc->sc;
struct geom area;
int flags;
XRaiseWindow(X_Dpy, cc->win);
area = screen_area(sc,
cc->geom.x + cc->geom.w / 2,
cc->geom.y + cc->geom.h / 2, 1);
flags = cargs->flag;
if (flags) {
while (flags) {
if (flags & CWM_UP) {
cc->geom.y = area.y;
flags &= ~CWM_UP;
}
if (flags & CWM_LEFT) {
cc->geom.x = area.x;
flags &= ~CWM_LEFT;
}
if (flags & CWM_RIGHT) {
cc->geom.x = area.x + area.w - cc->geom.w - (cc->bwidth * 2);
flags &= ~CWM_RIGHT;
}
if (flags & CWM_DOWN) {
cc->geom.y = area.y + area.h - cc->geom.h - (cc->bwidth * 2);
flags &= ~CWM_DOWN;
}
}
} else {
cc->geom.x = area.x + (area.w - cc->geom.w) / 2 - cc->bwidth;
cc->geom.y = area.y + (area.h - cc->geom.h) / 2 - cc->bwidth;
}
client_move(cc);
client_ptr_inbound(cc, 1);
}
void
kbfunc_client_tile(void *ctx, struct cargs *cargs)
{
struct client_ctx *cc = ctx;
struct screen_ctx *sc = cc->sc;
struct geom area;
int flags;
XRaiseWindow(X_Dpy, cc->win);
area = screen_area(sc,
cc->geom.x + cc->geom.w / 2,
cc->geom.y + cc->geom.h / 2, 1);
flags = cargs->flag;
if (flags & CWM_UP) {
cc->geom.y = area.y;
cc->geom.h = area.h / 2 - (cc->bwidth * 2) - cc->tgap;
} else if (flags & CWM_DOWN) {
cc->geom.y = area.y + area.h / 2;
cc->geom.h = area.h / 2 - (cc->bwidth * 2);
} else {
cc->geom.y = area.y;
cc->geom.h = area.h - (cc->bwidth * 2);
/*cc->geom.y = area.y;*/
/*cc->geom.h = area.h - (cc->bwidth * 2) - (cc->tgap / 2);*/
}
if (flags & CWM_LEFT) {
cc->geom.x = area.x;
cc->geom.w = area.w / 2 - (cc->bwidth * 2) - cc->tgap;
} else if (flags & CWM_RIGHT) {
cc->geom.x = area.x + area.w / 2;
cc->geom.w = area.w / 2 - (cc->bwidth * 2);
} else {
cc->geom.x = area.x;
cc->geom.w = area.w - (cc->bwidth * 2) - (cc->tgap / 2);
}
client_resize(cc, 1);
client_move(cc);
client_ptr_inbound(cc, 1);
XSync(X_Dpy, True);
}
void
kbfunc_client_close(void *ctx, struct cargs *cargs)
{
client_close(ctx);
}
void
kbfunc_client_lower(void *ctx, struct cargs *cargs)
{
client_ptr_save(ctx);
client_lower(ctx);
}
void
kbfunc_client_raise(void *ctx, struct cargs *cargs)
{
client_raise(ctx);
}
void
kbfunc_client_center (void *ctx, struct cargs *cargs)
{
client_center(ctx);
}
void
kbfunc_client_grow (void *ctx, struct cargs *cargs)
{
client_grow(ctx);
}
void
kbfunc_client_shrink (void *ctx, struct cargs *cargs)
{
client_shrink(ctx);
}
void
kbfunc_client_hide(void *ctx, struct cargs *cargs)
{
client_hide(ctx);
}
void
kbfunc_client_toggle_freeze(void *ctx, struct cargs *cargs)
{
client_toggle_freeze(ctx);
}
void
kbfunc_client_toggle_sticky(void *ctx, struct cargs *cargs)
{
client_toggle_sticky(ctx);
}
void
kbfunc_client_toggle_fullscreen(void *ctx, struct cargs *cargs)
{
client_toggle_fullscreen(ctx);
}
void
kbfunc_client_toggle_maximize(void *ctx, struct cargs *cargs)
{
client_toggle_maximize(ctx);
}
void
kbfunc_client_toggle_hmaximize(void *ctx, struct cargs *cargs)
{
client_toggle_hmaximize(ctx);
}
void
kbfunc_client_toggle_vmaximize(void *ctx, struct cargs *cargs)
{
client_toggle_vmaximize(ctx);
}
void
kbfunc_client_htile(void *ctx, struct cargs *cargs)
{
client_htile(ctx);
}
void
kbfunc_client_vtile(void *ctx, struct cargs *cargs)
{
client_vtile(ctx);
}
void
kbfunc_client_cycle(void *ctx, struct cargs *cargs)
{
struct screen_ctx *sc = ctx;
struct client_ctx *newcc, *oldcc, *prevcc;
int again = 1, flags = cargs->flag;
/* For X apps that ignore/steal events. */
if (cargs->xev == CWM_XEV_KEY)
XGrabKeyboard(X_Dpy, sc->rootwin, True,
GrabModeAsync, GrabModeAsync, CurrentTime);
if (TAILQ_EMPTY(&sc->clientq))
return;
prevcc = TAILQ_FIRST(&sc->clientq);
oldcc = client_current(sc);
if (oldcc == NULL)
oldcc = (flags & CWM_CYCLE_REVERSE) ?
TAILQ_LAST(&sc->clientq, client_q) :
TAILQ_FIRST(&sc->clientq);
newcc = oldcc;
while (again) {
again = 0;
newcc = (flags & CWM_CYCLE_REVERSE) ? client_prev(newcc) :
client_next(newcc);
/* Only cycle visible and non-ignored windows. */
if ((newcc->flags & (CLIENT_SKIP_CYCLE)) ||
((flags & CWM_CYCLE_INGROUP) &&
(newcc->gc != oldcc->gc)))
again = 1;
/* Is oldcc the only non-hidden window? */
if (newcc == oldcc) {
if (again)
return; /* No windows visible. */
break;
}
}
/* Reset when cycling mod is released. XXX I hate this hack */
sc->cycling = 1;
client_ptr_save(oldcc);
client_raise(prevcc);
client_raise(newcc);
if (!client_inbound(newcc, newcc->ptr.x, newcc->ptr.y)) {
newcc->ptr.x = newcc->geom.w / 2;
newcc->ptr.y = newcc->geom.h / 2;
}
client_ptr_warp(newcc);
/* When no client is active, warp pointer to last active. */
if (oldcc->flags & (CLIENT_ACTIVE))
client_ptr_warp(newcc);
else if (oldcc->flags & (CLIENT_SKIP_CYCLE))
client_ptr_warp(newcc);
else {
client_raise(oldcc);
client_ptr_warp(oldcc);
}
}
void
kbfunc_client_toggle_group(void *ctx, struct cargs *cargs)
{
struct client_ctx *cc = ctx;
/* For X apps that ignore/steal events. */
if (cargs->xev == CWM_XEV_KEY)
XGrabKeyboard(X_Dpy, cc->win, True,
GrabModeAsync, GrabModeAsync, CurrentTime);
group_toggle_membership(cc);
}
void
kbfunc_client_movetogroup(void *ctx, struct cargs *cargs)
{
group_movetogroup(ctx, cargs->flag);
}
void
kbfunc_group_only(void *ctx, struct cargs *cargs)
{
group_only(ctx, cargs->flag);
}
void
kbfunc_group_last(void *ctx, struct cargs *cargs)
{
struct screen_ctx *sc = ctx;
group_only(ctx, sc->group_last->num);
}
void
kbfunc_group_toggle(void *ctx, struct cargs *cargs)
{
group_toggle(ctx, cargs->flag);
}
void
kbfunc_group_toggle_all(void *ctx, struct cargs *cargs)
{
group_toggle_all(ctx);
}
void
kbfunc_group_close(void *ctx, struct cargs *cargs)
{
group_close(ctx, cargs->flag);
}
void
kbfunc_group_cycle(void *ctx, struct cargs *cargs)
{
group_cycle(ctx, cargs->flag);
}
void
kbfunc_exec_cmd(void *ctx, struct cargs *cargs)
{
u_spawn(cargs->cmd);
}
void
kbfunc_exec_term(void *ctx, struct cargs *cargs)
{
struct cmd_ctx *cmd;
TAILQ_FOREACH(cmd, &Conf.cmdq, entry) {
if (strcmp(cmd->name, "term") == 0)
u_spawn(cmd->path);
}
}
void
kbfunc_exec_lock(void *ctx, struct cargs *cargs)
{
struct cmd_ctx *cmd;
TAILQ_FOREACH(cmd, &Conf.cmdq, entry) {
if (strcmp(cmd->name, "lock") == 0)
u_spawn(cmd->path);
}
}
void
kbfunc_exec_dmenu(void *ctx, struct cargs *cargs)
{
struct cmd_ctx *cmd;
TAILQ_FOREACH(cmd, &Conf.cmdq, entry) {
if (strcmp(cmd->name, "dmenu") == 0)
u_spawn(cmd->path);
}
}
void
kbfunc_exec_dextra(void *ctx, struct cargs *cargs)
{
struct cmd_ctx *cmd;
TAILQ_FOREACH(cmd, &Conf.cmdq, entry) {
if (strcmp(cmd->name, "dextra") == 0)
u_spawn(cmd->path);
}
}
void
kbfunc_exec_dprint(void *ctx, struct cargs *cargs)
{
struct cmd_ctx *cmd;
TAILQ_FOREACH(cmd, &Conf.cmdq, entry) {
if (strcmp(cmd->name, "dprint") == 0)
u_spawn(cmd->path);
}
}
void
kbfunc_exec_teleporter(void *ctx, struct cargs *cargs)
{
struct cmd_ctx *cmd;
TAILQ_FOREACH(cmd, &Conf.cmdq, entry) {
if (strcmp(cmd->name, "teleporter") == 0)
u_spawn(cmd->path);
}
}
void
kbfunc_exec_vol_up(void *ctx, struct cargs *cargs)
{
struct cmd_ctx *cmd;
TAILQ_FOREACH(cmd, &Conf.cmdq, entry) {
if (strcmp(cmd->name, "vol-up") == 0)
u_spawn(cmd->path);
}
}
void
kbfunc_exec_vol_down(void *ctx, struct cargs *cargs)
{
struct cmd_ctx *cmd;
TAILQ_FOREACH(cmd, &Conf.cmdq, entry) {
if (strcmp(cmd->name, "vol-down") == 0)
u_spawn(cmd->path);
}
}
void
kbfunc_exec_vol_mute(void *ctx, struct cargs *cargs)
{
struct cmd_ctx *cmd;
TAILQ_FOREACH(cmd, &Conf.cmdq, entry) {
if (strcmp(cmd->name, "vol-mute") == 0)
u_spawn(cmd->path);
}
}