awesome/mouse.c

233 lines
8.6 KiB
C
Raw Normal View History

/*
* mouse.c - mouse managing
*
* Copyright © 2007 Julien Danjou <julien@danjou.info>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
*/
2007-12-28 17:37:41 +01:00
#include <math.h>
#include "mouse.h"
#include "screen.h"
#include "layout.h"
#include "tag.h"
#include "util.h"
#include "event.h"
#include "window.h"
2008-01-01 17:25:48 +01:00
#include "client.h"
#include "layouts/floating.h"
2007-12-28 17:11:20 +01:00
#include "layouts/tile.h"
extern AwesomeConf globalconf;
/** Move client with mouse
* \param screen Screen ID
* \param arg Unused
* \ingroup ui_callback
*/
void
uicb_client_movemouse(int screen, char *arg __attribute__ ((unused)))
{
2008-01-05 20:18:30 +01:00
int x1, y, ocx, ocy, di, phys_screen;
unsigned int dui;
Window dummy;
XEvent ev;
2008-01-05 20:18:30 +01:00
Area area, geometry;
Client *c = globalconf.focus->client;
2008-01-12 23:25:36 +01:00
Layout *layout = get_current_layout(screen);
2008-01-02 17:17:34 +01:00
if(!c)
return;
2008-01-12 23:25:36 +01:00
if(layout->arrange != layout_floating
&& !c->isfloating)
uicb_client_togglefloating(screen, (char *) "be nice");
else
restack(screen);
area = get_screen_area(c->screen,
globalconf.screens[screen].statusbar,
&globalconf.screens[screen].padding);
2008-01-05 20:18:30 +01:00
ocx = geometry.x = c->geometry.x;
ocy = geometry.y = c->geometry.y;
2008-01-02 19:26:55 +01:00
phys_screen = get_phys_screen(c->screen);
2007-12-27 23:10:43 +01:00
if(XGrabPointer(globalconf.display,
2008-01-02 19:26:55 +01:00
RootWindow(globalconf.display, phys_screen),
False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
None, globalconf.cursor[CurMove], CurrentTime) != GrabSuccess)
return;
2007-12-27 23:10:43 +01:00
XQueryPointer(globalconf.display,
2008-01-02 19:26:55 +01:00
RootWindow(globalconf.display, phys_screen),
2007-12-28 17:37:41 +01:00
&dummy, &dummy, &x1, &y, &di, &di, &dui);
c->ismax = False;
for(;;)
{
2007-12-27 23:10:43 +01:00
XMaskEvent(globalconf.display, MOUSEMASK | ExposureMask | SubstructureRedirectMask, &ev);
switch (ev.type)
{
case ButtonRelease:
2007-12-27 23:10:43 +01:00
XUngrabPointer(globalconf.display, CurrentTime);
return;
case ConfigureRequest:
handle_event_configurerequest(&ev);
break;
case Expose:
handle_event_expose(&ev);
break;
case MapRequest:
handle_event_maprequest(&ev);
break;
case MotionNotify:
2008-01-05 20:18:30 +01:00
geometry.x = ocx + (ev.xmotion.x - x1);
geometry.y = ocy + (ev.xmotion.y - y);
if(abs(geometry.x) < globalconf.screens[screen].snap + area.x && geometry.x > area.x)
geometry.x = area.x;
else if(abs((area.x + area.width) - (geometry.x + c->geometry.width + 2 * c->border)) < globalconf.screens[screen].snap)
geometry.x = area.x + area.width - c->geometry.width - 2 * c->border;
if(abs(geometry.y) < globalconf.screens[screen].snap + area.y && geometry.y > area.y)
geometry.y = area.y;
else if(abs((area.y + area.height) - (geometry.y + c->geometry.height + 2 * c->border)) < globalconf.screens[screen].snap)
geometry.y = area.y + area.height - c->geometry.height - 2 * c->border;
geometry.width = c->geometry.width;
geometry.height = c->geometry.height;
client_resize(c, geometry, False);
while(XCheckMaskEvent(globalconf.display, PointerMotionMask, &ev));
break;
}
}
}
/** Resize client with mouse
* \param screen Screen ID
* \param arg Unused
* \ingroup ui_callback
*/
void
uicb_client_resizemouse(int screen, char *arg __attribute__ ((unused)))
{
2008-01-05 20:18:30 +01:00
int ocx = 0, ocy = 0, n;
XEvent ev;
Client *c = globalconf.focus->client;
Tag **curtags = get_current_tags(screen);
2008-01-12 23:25:36 +01:00
Layout *layout = curtags[0]->layout;
2008-01-05 20:18:30 +01:00
Area area = { 0, 0, 0, 0 }, geometry;
2007-12-28 17:11:20 +01:00
double mwfact;
2007-12-28 17:11:20 +01:00
/* only handle floating and tiled layouts */
if(c && !c->isfixed)
2007-12-28 17:11:20 +01:00
{
2008-01-12 23:25:36 +01:00
if(layout->arrange == layout_floating || c->isfloating)
2007-12-28 17:11:20 +01:00
{
restack(screen);
2008-01-05 19:38:50 +01:00
ocx = c->geometry.x;
ocy = c->geometry.y;
2007-12-28 17:11:20 +01:00
c->ismax = False;
}
else if (layout->arrange == layout_tile || layout->arrange == layout_tileleft
|| layout->arrange == layout_tiledown || layout->arrange == layout_tileup)
2007-12-28 17:11:20 +01:00
{
for(n = 0, c = globalconf.clients; c; c = c->next)
if(IS_TILED(c, screen))
n++;
2007-12-28 17:11:20 +01:00
if(n <= curtags[0]->nmaster) return;
2007-12-28 17:11:20 +01:00
for(c = globalconf.clients; c && !IS_TILED(c, screen); c = c->next);
if(!c) return;
area = get_screen_area(screen,
globalconf.screens[c->screen].statusbar,
&globalconf.screens[c->screen].padding);
}
}
else
return;
2008-01-02 19:26:55 +01:00
if(XGrabPointer(globalconf.display, RootWindow(globalconf.display,
get_phys_screen(c->screen)),
False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
None, globalconf.cursor[CurResize], CurrentTime) != GrabSuccess)
return;
2007-12-28 17:11:20 +01:00
if(curtags[0]->layout->arrange == layout_tileleft)
2008-01-05 19:38:50 +01:00
XWarpPointer(globalconf.display, None, c->win, 0, 0, 0, 0, 0, c->geometry.height + c->border - 1);
else if(curtags[0]->layout->arrange == layout_tiledown)
XWarpPointer(globalconf.display, None, c->win, 0, 0, 0, 0, c->geometry.width + c->border - 1, c->geometry.height + c->border - 1);
else if(curtags[0]->layout->arrange == layout_tileup)
XWarpPointer(globalconf.display, None, c->win, 0, 0, 0, 0, c->geometry.width + c->border - 1, 0);
2007-12-28 17:11:20 +01:00
else
2008-01-05 19:38:50 +01:00
XWarpPointer(globalconf.display, None, c->win, 0, 0, 0, 0, c->geometry.width + c->border - 1, c->geometry.height + c->border - 1);
2007-12-28 17:11:20 +01:00
for(;;)
{
2007-12-27 23:10:43 +01:00
XMaskEvent(globalconf.display, MOUSEMASK | ExposureMask | SubstructureRedirectMask, &ev);
switch (ev.type)
{
case ButtonRelease:
2007-12-27 23:10:43 +01:00
XUngrabPointer(globalconf.display, CurrentTime);
return;
case ConfigureRequest:
handle_event_configurerequest(&ev);
break;
case Expose:
handle_event_expose(&ev);
break;
case MapRequest:
handle_event_maprequest(&ev);
break;
case MotionNotify:
2008-01-12 23:25:36 +01:00
if(layout->arrange == layout_floating || c->isfloating)
2007-12-28 17:11:20 +01:00
{
2008-01-05 20:18:30 +01:00
if((geometry.width = ev.xmotion.x - ocx - 2 * c->border + 1) <= 0)
geometry.width = 1;
if((geometry.height = ev.xmotion.y - ocy - 2 * c->border + 1) <= 0)
geometry.height = 1;
geometry.x = c->geometry.x;
geometry.y = c->geometry.y;
client_resize(c, geometry, True);
2007-12-28 17:11:20 +01:00
}
else if(layout->arrange == layout_tile || layout->arrange == layout_tileleft
|| layout->arrange == layout_tileup || layout->arrange == layout_tiledown)
2007-12-28 17:11:20 +01:00
{
2008-01-12 23:25:36 +01:00
if(layout->arrange == layout_tile)
2007-12-28 17:11:20 +01:00
mwfact = (double) (ev.xmotion.x - area.x) / area.width;
else if(curtags[0]->layout->arrange == layout_tileleft)
2007-12-28 17:11:20 +01:00
mwfact = 1 - (double) (ev.xmotion.x - area.x) / area.width;
else if(curtags[0]->layout->arrange == layout_tiledown)
mwfact = (double) (ev.xmotion.y - area.y) / area.height;
else
mwfact = 1 - (double) (ev.xmotion.y - area.y) / area.height;
2007-12-28 17:11:20 +01:00
if(mwfact < 0.1) mwfact = 0.1;
else if(mwfact > 0.9) mwfact = 0.9;
2007-12-28 17:37:41 +01:00
if(fabs(curtags[0]->mwfact - mwfact) >= 0.05)
{
curtags[0]->mwfact = mwfact;
arrange(screen);
/* drop possibly arrived events while we where arrange()ing */
while(XCheckMaskEvent(globalconf.display, PointerMotionMask, &ev));
}
2007-12-28 17:11:20 +01:00
}
break;
}
}
2007-12-28 17:11:20 +01:00
p_delete(&curtags);
}
// vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80