From 2cdd6bc8950df6a179fd90ee2653810892520271 Mon Sep 17 00:00:00 2001 From: Martin Duquesnoy Date: Wed, 1 Oct 2008 18:33:27 +0200 Subject: [PATCH] [ALL + layout.c] Add layout.c --- CMakeLists.txt | 3 +- event.c | 2 +- layout.c | 260 +++++++++++++++++++++++++++++++++++++++++++++++++ wmfs.c | 228 ------------------------------------------- wmfs.h | 20 ++-- 5 files changed, 274 insertions(+), 239 deletions(-) create mode 100644 layout.c diff --git a/CMakeLists.txt b/CMakeLists.txt index 671b92d..7e39f59 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -28,7 +28,8 @@ set(wmfs_src wmfs.c config.c event.c - util.c) + util.c + layout.c) # Set the executable from the wmfs_src add_executable(wmfs ${wmfs_src}) diff --git a/event.c b/event.c index f950f27..7ad71a3 100644 --- a/event.c +++ b/event.c @@ -287,7 +287,7 @@ destroynotify(XEvent ev) if((c = getclient(ev.xdestroywindow.window))) unmanage(c); - return; + return; } /* ENTERNOTIFY */ diff --git a/layout.c b/layout.c new file mode 100644 index 0000000..aa335b5 --- /dev/null +++ b/layout.c @@ -0,0 +1,260 @@ +/* +* layout.c +* Copyright © 2008 Martin Duquesnoy +* All rights reserved. +* +* Redistribution and use in source and binary forms, with or without +* modification, are permitted provided that the following conditions are +* met: +* +* * Redistributions of source code must retain the above copyright +* notice, this list of conditions and the following disclaimer. +* * Redistributions in binary form must reproduce the above +* copyright notice, this list of conditions and the following disclaimer +* in the documentation and/or other materials provided with the +* distribution. +* * Neither the name of the nor the names of its +* contributors may be used to endorse or promote products derived from +* this software without specific prior written permission. +* +* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + +#include "wmfs.h" + +void +freelayout(void) +{ + Client *c; + + tags[seltag].layout.func = freelayout; + + for(c = clients; c; c = c->next) + { + if(!ishide(c)) + { + if(c->tile) + { + moveresize(c, c->ox, c->oy, c->ow, c->oh, True); + c->tile = False; + } + } + } + + return; +} + +/* Improved ! :) */ +void +layoutswitch(char *cmd) +{ + int i; + + for(i = 0; i < conf.nlayout; ++i) + { + if(tags[seltag].layout.symbol == conf.layout[i].symbol + && tags[seltag].layout.func == conf.layout[i].func) + { + if(cmd[0] == '+') + tags[seltag].layout = conf.layout[(i + 1) % conf.nlayout]; + else if(cmd[0] == '-') + tags[seltag].layout = conf.layout[(i + conf.nlayout - 1) % conf.nlayout]; + break; + } + } + arrange(); + + return; +} + +void +maxlayout(void) +{ + Client *c; + + tags[seltag].layout.func = maxlayout; + + for(c = nexttiled(clients); c; c = nexttiled(c->next)) + { + c->tile = False; + c->ox = c->x; + c->oy = c->y; + c->ow = c->w; + c->oh = c->h; + + moveresize(c, 0, (conf.ttbarheight + ((conf.bartop) ? barheight : 0)), + (mw-(conf.borderheight * 2)), + (mh-(conf.borderheight * 2) - conf.ttbarheight - barheight), False); + } + + return; +} + +/* To use in a for, select only the + * client who can be tiled */ +Client* +nexttiled(Client *c) +{ + for(; c && (c->max || c->free || ishide(c)); c = c->next); + + return c; +} + +void +set_mwfact(char *cmd) +{ + double c; + + if(!(sscanf(cmd, "%lf", &c))) + return; + if(tags[seltag].mwfact + c > 0.95 + || tags[seltag].mwfact + c < 0.05 + || tags[seltag].layout.func != tile) + return; + tags[seltag].mwfact += c; + arrange(); + + return; +} + +void +set_nmaster(char *cmd) +{ + int n = atoi(cmd); + + if(tags[seltag].nmaster + n == 0) + return; + tags[seltag].nmaster += n; + arrange(); + + return; +} + +void +tile(void) +{ + unsigned int i, n, x, y, yt, w, h, ww, hh, th; + unsigned int barto, bord, mwf, nm, mht; + Client *c; + + bord = conf.borderheight * 2; + barto = conf.ttbarheight + barheight; + mwf = tags[seltag].mwfact * mw; + nm = tags[seltag].nmaster; + mht = mh - ((conf.bartop) ? 0 : barheight); + + tags[seltag].layout.func = tile; + + /* count all the "can-be-tiled" client */ + for(n = 0, c = nexttiled(clients); c; c = nexttiled(c->next), ++n); + if(n == 0) + return; + + /* window geoms */ + hh = ((n <= nm) ? mht / (n > 0 ? n : 1) : mht / nm) - bord*2; + ww = (n <= nm) ? mw : mwf; + th = (n > nm) ? mht / (n - nm) : 0; + if(n > nm && th < barheight) + th = mht; + + x = 0; + y = yt = barto; + + if(!conf.bartop) + y = yt = conf.ttbarheight; + + for(i = 0, c = nexttiled(clients); c; c = nexttiled(c->next), i++) + { + c->max = False; + c->tile = True; + c->ox = c->x; c->oy = c->y; + c->ow = c->w; c->oh = c->h; + + /* MASTER CLIENT */ + if(i < nm) + { + y = yt + i * hh; + w = ww - bord; + h = hh; + /* remainder */ + if(i + 1 == (n < nm ? n : nm)) + h = (mht - hh*i) - + ((conf.bartop) ? barheight : 0); + h -= bord + conf.ttbarheight; + } + /* TILE CLIENT */ + else + { + if(i == nm) + { + y = yt; + x += ww; + } + w = mw - ww - bord; + /* remainder */ + if(i + 1 == n) + h = (barto + mht) - y - (bord + barto); + else + h = th - (bord + conf.ttbarheight) - bord*2; + } + moveresize(c, x, y, w, h, False); + if(n > nm && th != mht) + y = c->y + c->h + bord + conf.ttbarheight; + } + + return; +} + +void +tile_switch(char *cmd) +{ + Client *c; + + if(!sel || sel->hint || !sel->tile) + return; + if((c = sel) == nexttiled(clients)) + if(!(c = nexttiled(c->next))) + return; + detach(c); + attach(c); + focus(c); + arrange(); + + return; +} + +void +togglemax(char *cmd) +{ + if(!sel || ishide(sel) || sel->hint) + return; + if(!sel->max) + { + sel->ox = sel->x; sel->oy = sel->y; + sel->ow = sel->w; sel->oh = sel->h; + moveresize(sel, 0, (conf.ttbarheight + ((conf.bartop) ? barheight : 0)), + (mw-(conf.borderheight * 2)), + (mh-(conf.borderheight * 2)- conf.ttbarheight - barheight), False); + raiseclient(sel); + sel->max = True; + } + else if(sel->max) + { + moveresize(sel, sel->ox, sel->oy, sel->ow, sel->oh, False); + sel->max = False; + } + arrange(); + + return; +} + diff --git a/wmfs.c b/wmfs.c index dc042d8..f99f3ac 100644 --- a/wmfs.c +++ b/wmfs.c @@ -136,28 +136,6 @@ focus(Client *c) return; } -void -freelayout(void) -{ - Client *c; - - tags[seltag].layout.func = freelayout; - - for(c = clients; c; c = c->next) - { - if(!ishide(c)) - { - if(c->tile) - { - moveresize(c, c->ox, c->oy, c->ow, c->oh, True); - c->tile = False; - } - } - } - - return; -} - Client* getbutton(Window w) { @@ -460,29 +438,6 @@ killclient(char *cmd) return; } -/* Improved ! :) */ -void -layoutswitch(char *cmd) -{ - int i; - - for(i = 0; i < conf.nlayout; ++i) - { - if(tags[seltag].layout.symbol == conf.layout[i].symbol - && tags[seltag].layout.func == conf.layout[i].func) - { - if(cmd[0] == '+') - tags[seltag].layout = conf.layout[(i + 1) % conf.nlayout]; - else if(cmd[0] == '-') - tags[seltag].layout = conf.layout[(i + conf.nlayout - 1) % conf.nlayout]; - break; - } - } - arrange(); - - return; -} - void mainloop(void) { @@ -613,30 +568,6 @@ manage(Window w, XWindowAttributes *wa) return; } -void -maxlayout(void) -{ - Client *c; - - tags[seltag].layout.func = maxlayout; - - for(c = nexttiled(clients); c; c = nexttiled(c->next)) - { - c->tile = False; - c->ox = c->x; - c->oy = c->y; - c->ow = c->w; - c->oh = c->h; - - moveresize(c, 0, (conf.ttbarheight + ((conf.bartop) ? barheight : 0)), - (mw-(conf.borderheight * 2)), - (mh-(conf.borderheight * 2) - conf.ttbarheight - barheight), False); - } - - return; -} - - /* If the type is 0, this function will move, else, * this will resize */ void @@ -778,16 +709,6 @@ moveresize(Client *c, int x, int y, int w, int h, bool r) return; } -/* To use in a for, select only the - * client who can be tiled */ -Client* -nexttiled(Client *c) -{ - for(; c && (c->max || c->free || ishide(c)); c = c->next); - - return c; -} - void quit(char *cmd) { @@ -862,36 +783,6 @@ setwinstate(Window win, long state) return; } -void -set_mwfact(char *cmd) -{ - double c; - - if(!(sscanf(cmd, "%lf", &c))) - return; - if(tags[seltag].mwfact + c > 0.95 - || tags[seltag].mwfact + c < 0.05 - || tags[seltag].layout.func != tile) - return; - tags[seltag].mwfact += c; - arrange(); - - return; -} - -void -set_nmaster(char *cmd) -{ - int n = atoi(cmd); - - if(tags[seltag].nmaster + n == 0) - return; - tags[seltag].nmaster += n; - arrange(); - - return; -} - void setsizehints(Client *c) { @@ -1003,99 +894,6 @@ tagtransfert(char *cmd) return; } -void -tile(void) -{ - unsigned int i, n, x, y, yt, w, h, ww, hh, th; - unsigned int barto, bord, mwf, nm, mht; - Client *c; - - bord = conf.borderheight * 2; - barto = conf.ttbarheight + barheight; - mwf = tags[seltag].mwfact * mw; - nm = tags[seltag].nmaster; - mht = mh - ((conf.bartop) ? 0 : barheight); - - tags[seltag].layout.func = tile; - - /* count all the "can-be-tiled" client */ - for(n = 0, c = nexttiled(clients); c; c = nexttiled(c->next), ++n); - if(n == 0) - return; - - /* window geoms */ - hh = ((n <= nm) ? mht / (n > 0 ? n : 1) : mht / nm) - bord*2; - ww = (n <= nm) ? mw : mwf; - th = (n > nm) ? mht / (n - nm) : 0; - if(n > nm && th < barheight) - th = mht; - - x = 0; - y = yt = barto; - - if(!conf.bartop) - y = yt = conf.ttbarheight; - - for(i = 0, c = nexttiled(clients); c; c = nexttiled(c->next), i++) - { - c->max = False; - c->tile = True; - c->ox = c->x; c->oy = c->y; - c->ow = c->w; c->oh = c->h; - - /* MASTER CLIENT */ - if(i < nm) - { - y = yt + i * hh; - w = ww - bord; - h = hh; - /* remainder */ - if(i + 1 == (n < nm ? n : nm)) - h = (mht - hh*i) - - ((conf.bartop) ? barheight : 0); - h -= bord + conf.ttbarheight; - } - /* TILE CLIENT */ - else - { - if(i == nm) - { - y = yt; - x += ww; - } - w = mw - ww - bord; - /* remainder */ - if(i + 1 == n) - h = (barto + mht) - y - (bord + barto); - else - h = th - (bord + conf.ttbarheight) - bord*2; - } - moveresize(c, x, y, w, h, False); - if(n > nm && th != mht) - y = c->y + c->h + bord + conf.ttbarheight; - } - - return; -} - -void -tile_switch(char *cmd) -{ - Client *c; - - if(!sel || sel->hint || !sel->tile) - return; - if((c = sel) == nexttiled(clients)) - if(!(c = nexttiled(c->next))) - return; - detach(c); - attach(c); - focus(c); - arrange(); - - return; -} - void togglebarpos(char *cmd) { @@ -1118,32 +916,6 @@ togglebarpos(char *cmd) return; } - -void -togglemax(char *cmd) -{ - if(!sel || ishide(sel) || sel->hint) - return; - if(!sel->max) - { - sel->ox = sel->x; sel->oy = sel->y; - sel->ow = sel->w; sel->oh = sel->h; - moveresize(sel, 0, (conf.ttbarheight + ((conf.bartop) ? barheight : 0)), - (mw-(conf.borderheight * 2)), - (mh-(conf.borderheight * 2)- conf.ttbarheight - barheight), False); - raiseclient(sel); - sel->max = True; - } - else if(sel->max) - { - moveresize(sel, sel->ox, sel->oy, sel->ow, sel->oh, False); - sel->max = False; - } - arrange(); - - return; -} - void unhide(Client *c) { diff --git a/wmfs.h b/wmfs.h index 2d3df8a..bf405e4 100644 --- a/wmfs.h +++ b/wmfs.h @@ -212,6 +212,17 @@ void getevent(void); void *emalloc(unsigned int elemet, size_t size); void spawn(char *cmd); +/* layout.c */ +void freelayout(void); +void layoutswitch(char *cmd); +void maxlayout(void); +Client* nexttiled(Client *c); +void set_mwfact(char *cmd); +void set_nmaster(char *cmd); +void tile(void); +void tile_switch(char *cmd); +void togglemax(char *cmd); + /* wmfs.c */ void arrange(void); void attach(Client *c); @@ -220,7 +231,6 @@ void detach(Client *c); int errorhandler(Display *d, XErrorEvent *event); int errorhandlerdummy(Display *d, XErrorEvent *event); void focus(Client *c); -void freelayout(void); Client* getbutton(Window w); Client* getclient(Window w); Client* getnext(Client *c); @@ -234,28 +244,20 @@ void keymovex(char *cmd); void keymovey(char *cmd); void keyresize(char *cmd); void killclient(char *cmd); -void layoutswitch(char *cmd); void mainloop(void); void mapclient(Client *c); void manage(Window w, XWindowAttributes *wa); -void maxlayout(void); void mouseaction(Client *c, int x, int y, int type); void moveresize(Client *c, int x, int y, int w, int h, bool r); -Client* nexttiled(Client *c); void quit(char *cmd); void raiseclient(Client *c); void scan(void); void setborder(Window win, int color); void setwinstate(Window win, long state); -void set_mwfact(char *cmd); -void set_nmaster(char *cmd); void setsizehints(Client *c); void tag(char *cmd); void tagtransfert(char *cmd); -void tile(void); -void tile_switch(char *cmd); void togglebarpos(char *cmd); -void togglemax(char *cmd); void unhide(Client *c); void unmanage(Client *c); void updatebar(void);