init: Add init.c

This commit is contained in:
Martin Duquesnoy 2008-11-06 07:59:11 +01:00
parent 62f2e40ff4
commit b5ba311576
12 changed files with 230 additions and 131 deletions

View File

@ -27,6 +27,7 @@ project(${PROJECT_NAME} C)
# Definition of the wmfs source
set(wmfs_src
src/init.c
src/wmfs.c
src/config.c
src/event.c

View File

@ -162,6 +162,7 @@ void
client_hide(Client *c)
{
client_unmap(c);
c->hide = True;
setwinstate(c->win, IconicState);
return;
@ -432,6 +433,7 @@ void
client_unhide(Client *c)
{
client_map(c);
c->hide = False;
setwinstate(c->win, NormalState);
return;

View File

@ -511,7 +511,9 @@ init_conf(void)
if(j != i && strcmp(conf.tag[i].name, conf.tag[j].name) == 0)
fprintf(stderr, "WMFS Configuration: Warning! "
"tag \"%s\" is already defined\n", conf.tag[j].name);
seltag = 1;
for(i = 0; i < conf.ntag; ++i)
tags[i + 1] = conf.tag[i];
/* keybind */
conf.nkeybind = cfg_size(cfg_keys, "key");

View File

@ -49,7 +49,7 @@ draw_text(Drawable d, int x, int y, char* fg, uint bg, int pad, char *str)
DefaultColormap(dpy, screen), fg, &xftcolor);
/* Draw the text */
XftDrawStringUtf8(xftd, &xftcolor, xftfont, x, y, (FcChar8 *)str, strlen(str));
XftDrawStringUtf8(xftd, &xftcolor, font, x, y, (FcChar8 *)str, strlen(str));
/* Free the text color and XftDraw */
XftColorFree(dpy, DefaultVisual(dpy, screen), DefaultColormap(dpy, screen), &xftcolor);
@ -78,7 +78,7 @@ textw(const char *text)
if(!text)
return 0;
XftTextExtentsUtf8(dpy, xftfont, (FcChar8 *)text, strlen(text), &gl);
XftTextExtentsUtf8(dpy, font, (FcChar8 *)text, strlen(text), &gl);
return gl.width + xftfont->descent;
return gl.width + font->descent;
}

View File

@ -136,7 +136,7 @@ configurerequest(XEvent ev)
XConfigureWindow(dpy, ev.xconfigurerequest.window,
ev.xconfigurerequest.value_mask, &wc);
if((c = client_get(ev.xconfigurerequest.window)))
if(wc.y < mw && wc.x < mh)
if(wc.y < MAXW && wc.x < MAXH)
client_moveresize(c, geo, True);
return;
@ -202,29 +202,23 @@ focusin(XEvent ev)
void
grabbuttons(Client *c, Bool focused)
{
int i, j;
uint mod = conf.client.mod;
uint bl[] = {Button1, Button2, Button3, Button4, Button5};
uint ml[] = {mod, mod|LockMask, mod|numlockmask, mod|scrolllockmask,
mod|numlockmask|scrolllockmask, mod|LockMask|scrolllockmask,
mod|LockMask|numlockmask,mod|LockMask|numlockmask|scrolllockmask};
XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
if(focused)
{
XGrabButton(dpy, Button1, mod, c->win, False, ButtonMask, GrabModeAsync,GrabModeSync, None, None);
XGrabButton(dpy, Button1, mod|LockMask, c->win, False, ButtonMask, GrabModeAsync, GrabModeSync, None, None);
XGrabButton(dpy, Button1, mod|numlockmask, c->win, False, ButtonMask, GrabModeAsync,GrabModeSync, None, None);
XGrabButton(dpy, Button1, mod|LockMask|numlockmask, c->win, False, ButtonMask, GrabModeAsync, GrabModeSync, None, None);
XGrabButton(dpy, Button2, mod, c->win, False, ButtonMask, GrabModeAsync, GrabModeSync, None, None);
XGrabButton(dpy, Button2, mod|LockMask, c->win, False, ButtonMask, GrabModeAsync, GrabModeSync, None, None);
XGrabButton(dpy, Button2, mod|numlockmask, c->win, False, ButtonMask, GrabModeAsync, GrabModeSync, None, None);
XGrabButton(dpy, Button2, mod|LockMask|numlockmask, c->win, False, ButtonMask, GrabModeAsync, GrabModeSync, None, None);
XGrabButton(dpy, Button3, mod, c->win, False, ButtonMask, GrabModeAsync, GrabModeSync, None, None);
XGrabButton(dpy, Button3, mod|LockMask, c->win, False, ButtonMask, GrabModeAsync, GrabModeSync, None, None);
XGrabButton(dpy, Button3, mod|numlockmask, c->win, False, ButtonMask, GrabModeAsync, GrabModeSync, None, None);
XGrabButton(dpy, Button3, mod|LockMask|numlockmask, c->win, False, ButtonMask, GrabModeAsync, GrabModeSync, None, None);
}
for(i = 0; i < (sizeof bl / sizeof bl[0]); ++i)
for(j = 0; j < (sizeof ml / sizeof ml[0]); ++j)
XGrabButton(dpy, bl[i], ml[j], c->win, False,
ButtonMask, GrabModeAsync,GrabModeSync, None, None);
else
XGrabButton(dpy, AnyButton, AnyModifier, c->win, False, ButtonMask, GrabModeAsync, GrabModeSync, None, None);
XGrabButton(dpy, AnyButton, AnyModifier, c->win, False,
ButtonMask, GrabModeAsync, GrabModeSync, None, None);
return;
}
@ -232,17 +226,17 @@ grabbuttons(Client *c, Bool focused)
void
grabkeys(void)
{
uint i;
uint i, j;
KeyCode code;
uint ml[] = {LockMask, numlockmask, scrolllockmask, numlockmask|scrolllockmask,
LockMask|scrolllockmask, LockMask|numlockmask, LockMask|numlockmask|scrolllockmask};
XUngrabKey(dpy, AnyKey, AnyModifier, root);
for(i = 0; i < conf.nkeybind; ++i)
{
code = XKeysymToKeycode(dpy, keys[i].keysym);
XGrabKey(dpy, code, keys[i].mod, root, True, GrabModeAsync, GrabModeAsync);
XGrabKey(dpy, code, keys[i].mod|numlockmask, root, True, GrabModeAsync, GrabModeAsync);
XGrabKey(dpy, code, keys[i].mod|LockMask, root, True, GrabModeAsync, GrabModeAsync);
XGrabKey(dpy, code, keys[i].mod|LockMask|numlockmask, root, True, GrabModeAsync, GrabModeAsync);
for(j = 0; j < (sizeof ml / sizeof ml[0]); ++j)
XGrabKey(dpy, code, keys[i].mod|ml[j], root, True, GrabModeAsync, GrabModeAsync);
}
return;
@ -277,7 +271,6 @@ mapnotify(XEvent ev)
return;
}
/* MAPREQUEST */
void
maprequest(XEvent ev)
@ -311,7 +304,7 @@ mouseaction(Client *c, int x, int y, int type)
ocx = c->geo.x;
ocy = c->geo.y;
if(XGrabPointer(dpy, root, False, MouseMask, GrabModeAsync, GrabModeAsync,
None, cursor[((type) ?CurResize:CurMove)], CurrentTime) != GrabSuccess)
None, cursor[((type) ? CurResize:CurMove)], CurrentTime) != GrabSuccess)
return;
/* Warp pointer for resize */
if(type && !c->tile)
@ -448,6 +441,7 @@ propertynotify(XEvent ev)
return;
}
/* Handle */
void
getevent(void)

View File

@ -35,11 +35,11 @@
void
infobar_init(InfoBar *ib)
{
ib->geo.height = fonth + (float)4.5;
ib->geo.y = (conf.bartop) ? 0 : mh - ib->geo.height;
ib->geo.height = font->height + (float)4.5;
ib->geo.y = (conf.bartop) ? 0 : MAXH - ib->geo.height;
/* Create infobar barwindow */
ib->bar = bar_create(0, ib->geo.y, mw, ib->geo.height, 0, conf.colors.bar, False);
ib->bar = bar_create(0, ib->geo.y, MAXW, ib->geo.height, 0, conf.colors.bar, False);
/* Create layout switch & layout type switch barwindow */
ib->layout_switch = bar_create(0, (conf.bartop) ? ib->geo.y : ib->geo.y + 1,
@ -77,19 +77,23 @@ infobar_draw(void)
sprintf(buf, "mwfact: %.2f - nmaster: %d",
tags[seltag].mwfact,
tags[seltag].nmaster);
draw_text(infobar.bar->dr, infobar.lastsep + PAD/1.5, fonth, conf.colors.text, conf.colors.bar, 0, buf);
draw_text(infobar.bar->dr, infobar.lastsep + PAD/1.5, font->height, conf.colors.text, conf.colors.bar, 0, buf);
draw_rectangle(infobar.bar->dr, textw(buf) + infobar.lastsep + PAD,
0, conf.tagbordwidth, infobar.geo.height, conf.colors.tagbord);
/* Draw status text */
draw_text(infobar.bar->dr, mw - textw(infobar.statustext), fonth, conf.colors.text, conf.colors.bar, 0, infobar.statustext);
draw_text(infobar.bar->dr,
MAXW - textw(infobar.statustext),
font->height,
conf.colors.text,
conf.colors.bar, 0, infobar.statustext);
/* Bar border */
if(conf.tagbordwidth)
{
draw_rectangle(infobar.bar->dr, 0, ((conf.bartop) ? infobar.geo.height - 1: 0),
mw, 1, conf.colors.tagbord);
draw_rectangle(infobar.bar->dr, mw - textw(infobar.statustext) - 5,
MAXW, 1, conf.colors.tagbord);
draw_rectangle(infobar.bar->dr, MAXW - textw(infobar.statustext) - 5,
0, conf.tagbordwidth, infobar.geo.height, conf.colors.tagbord);
}
@ -119,7 +123,7 @@ infobar_draw_layout(void)
bar_move(infobar.layout_switch, px, py);
bar_resize(infobar.layout_switch, textw(symbol) + PAD, infobar.geo.height - 1);
draw_text(infobar.layout_switch->dr, PAD/2, fonth,
draw_text(infobar.layout_switch->dr, PAD/2, font->height,
conf.colors.layout_fg,
conf.colors.layout_bg,
PAD, symbol);
@ -133,7 +137,7 @@ infobar_draw_layout(void)
bar_refresh_color(infobar.layout_type_switch);
bar_move(infobar.layout_type_switch, px + infobar.layout_switch->geo.width + PAD/2, py);
bar_resize(infobar.layout_type_switch, textw(tags[seltag].layout.symbol) + PAD, infobar.geo.height - 1);
draw_text(infobar.layout_type_switch->dr, PAD/2, fonth,
draw_text(infobar.layout_type_switch->dr, PAD/2, font->height,
conf.colors.layout_fg,
conf.colors.layout_bg,
PAD, tags[seltag].layout.symbol);
@ -166,7 +170,7 @@ infobar_draw_taglist(Drawable dr)
sprintf(buf[i], "%s<%s>", tags[i+1].name, (client_pertag(i+1)) ? p : "");
/* Draw the string */
draw_text(dr, taglen[i], fonth,
draw_text(dr, taglen[i], font->height,
((i+1 == seltag) ? conf.colors.tagselfg : conf.colors.text),
((i+1 == seltag) ? conf.colors.tagselbg : conf.colors.bar), PAD, buf[i]);
@ -189,7 +193,7 @@ uicb_infobar_togglepos(uicb_t cmd)
sgeo.y = conf.titlebar.pos ? infobar.geo.height : infobar.geo.height + conf.titlebar.height;
else
sgeo.y = conf.titlebar.pos ? 0 : conf.titlebar.height;
infobar.geo.y = (conf.bartop) ? 0 : mh - infobar.geo.height;
infobar.geo.y = (conf.bartop) ? 0 : MAXH - infobar.geo.height;
bar_move(infobar.bar, 0, infobar.geo.y);
infobar_draw();
arrange();

149
src/init.c Normal file
View File

@ -0,0 +1,149 @@
/*
* wmfs.c
* Copyright © 2008 Martin Duquesnoy <xorg62@gmail.com>
* 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
init(void)
{
/* First init */
gc = DefaultGC (dpy, screen);
screen = DefaultScreen (dpy);
root = RootWindow (dpy, screen);
init_font();
init_cursor();
init_key();
init_atom();
init_root();
infobar_init(&infobar);
init_geometry();
grabkeys();
/* Warning about font */
if(conf.titlebar.height < font->height)
fprintf(stderr, "WMFS Warning: Font too big, can't draw any text in the titlebar.\n");
return;
}
void
init_font(void)
{
font = XftFontOpenName(dpy, screen, conf.font);
if(!font)
{
fprintf(stderr, "WMFS Error: Cannot initialize font\n");
font = XftFontOpenName(dpy, screen, "sans-10");
}
}
void
init_cursor(void)
{
cursor[CurNormal] = XCreateFontCursor(dpy, XC_left_ptr);
cursor[CurResize] = XCreateFontCursor(dpy, XC_sizing);
cursor[CurMove] = XCreateFontCursor(dpy, XC_fleur);
return;
}
void
init_key(void)
{
int i, j;
XModifierKeymap *modmap = XGetModifierMapping(dpy);
for(i = 0; i < 8; i++)
for(j = 0; j < modmap->max_keypermod; ++j)
{
if(modmap->modifiermap[i * modmap->max_keypermod + j]
== XKeysymToKeycode(dpy, XK_Num_Lock))
numlockmask = (1 << i);
if(modmap->modifiermap[i * modmap->max_keypermod + j]
== XKeysymToKeycode(dpy, XK_Scroll_Lock))
scrolllockmask = (1 << i);
}
XFreeModifiermap(modmap);
return;
}
void
init_atom(void)
{
wm_atom[WMState] = XInternAtom(dpy, "WM_STATE", False);
wm_atom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False);
wm_atom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
wm_atom[WMName] = XInternAtom(dpy, "WM_NAME", False);
net_atom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
net_atom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
XChangeProperty(dpy, root, net_atom[NetSupported], XA_ATOM, 32,
PropModeReplace, (unsigned char *) net_atom, NetLast);
return;
}
void
init_root(void)
{
XSetWindowAttributes at;
at.event_mask = KeyMask | ButtonPressMask | ButtonReleaseMask |
SubstructureRedirectMask | SubstructureNotifyMask |
EnterWindowMask | LeaveWindowMask | StructureNotifyMask ;
at.cursor = cursor[CurNormal];
XChangeWindowAttributes(dpy, root, CWEventMask | CWCursor, &at);
if(conf.root.background_command)
uicb_spawn(conf.root.background_command);
return;
}
void
init_geometry(void)
{
sgeo.x = 0;
if(conf.bartop)
sgeo.y = (conf.titlebar.pos)
? infobar.geo.height
: infobar.geo.height + conf.titlebar.height;
else
sgeo.y = (conf.titlebar.pos)
? 0
: conf.titlebar.height;
sgeo.width = MAXW;
sgeo.height = MAXH - (infobar.geo.height + conf.titlebar.height);
return;
}

View File

@ -88,7 +88,7 @@ struct Client
/* Border */
int border;
/* Client Layout Information */
Bool max, tile, free;
Bool max, tile, free, hide;
Bool hint, lmax, havetbar;
/* Struct in chains */
Client *next;

View File

@ -130,7 +130,7 @@ titlebar_update(Client *c)
bar_refresh_color(c->tbar);
/* Draw the client title in the titlebar *logeek* */
if(conf.titlebar.height + 1 > fonth)
if(conf.titlebar.height + 1 > font->height)
{
/* Set the text alignement */
switch(conf.titlebar.text_align)
@ -148,7 +148,7 @@ titlebar_update(Client *c)
}
/* Set y text position (always at the middle) and fg color */
pos_y = (fonth - (xftfont->descent ) - 1) + ((conf.titlebar.height - fonth) / 2);
pos_y = (font->height - (font->descent ) - 1) + ((conf.titlebar.height - font->height) / 2);
/* Draw title */
draw_text(c->tbar->dr, pos_x, pos_y, fg, bg, 0, c->title);

View File

@ -62,6 +62,27 @@ getcolor(char *color)
return xcolor.pixel;
}
long
getwinstate(Window win)
{
int f;
long ret = -1;
ulong n, e;
uchar *p = NULL;
Atom at;
if(XGetWindowProperty(dpy, win, wm_atom[WMState], 0L, 2L, False, wm_atom[WMState],
&at, &f, &n, &e, (uchar **)&p) != Success)
return -1;
if(n != 0)
ret = *p;
free(p);
return ret;
}
double
round(double x)
{

View File

@ -88,7 +88,7 @@ void
quit(void)
{
/* Exiting WMFS :'( */
XftFontClose(dpy, xftfont);
XftFontClose(dpy, font);
XFreeCursor(dpy, cursor[CurNormal]);
XFreeCursor(dpy, cursor[CurMove]);
XFreeCursor(dpy, cursor[CurResize]);
@ -104,89 +104,6 @@ quit(void)
return;
}
void
init(void)
{
XSetWindowAttributes at;
XModifierKeymap *modmap;
int i, j;
/* FIRST INIT */
gc = DefaultGC (dpy, screen);
screen = DefaultScreen (dpy);
root = RootWindow (dpy, screen);
mw = DisplayWidth (dpy, screen);
mh = DisplayHeight (dpy, screen);
/* INIT TAG / LAYOUT ATTRIBUTE */
seltag = 1;
for(i = 0; i < conf.ntag + 1; ++i)
tags[i] = conf.tag[i - 1];
/* INIT FONT */
xftfont = XftFontOpenName(dpy, screen, conf.font);
if(!xftfont)
{
fprintf(stderr, "WMFS Error: Cannot initialize font\n");
xftfont = XftFontOpenName(dpy, screen, "sans-10");
}
fonth = (xftfont->ascent + xftfont->descent);
/* INIT CURSOR */
cursor[CurNormal] = XCreateFontCursor(dpy, XC_left_ptr);
cursor[CurResize] = XCreateFontCursor(dpy, XC_sizing);
cursor[CurMove] = XCreateFontCursor(dpy, XC_fleur);
/* INIT MODIFIER */
modmap = XGetModifierMapping(dpy);
for(i = 0; i < 8; i++)
for(j = 0; j < modmap->max_keypermod; ++j)
if(modmap->modifiermap[i * modmap->max_keypermod + j]
== XKeysymToKeycode(dpy, XK_Num_Lock))
numlockmask = (1 << i);
XFreeModifiermap(modmap);
/* INIT ATOM */
wm_atom[WMState] = XInternAtom(dpy, "WM_STATE", False);
wm_atom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False);
wm_atom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
net_atom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
net_atom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
XChangeProperty(dpy, root, net_atom[NetSupported], XA_ATOM, 32,
PropModeReplace, (unsigned char *) net_atom, NetLast);
/* INIT ROOT */
at.event_mask = KeyMask | ButtonPressMask | ButtonReleaseMask |
SubstructureRedirectMask | SubstructureNotifyMask |
EnterWindowMask | LeaveWindowMask | StructureNotifyMask ;
at.cursor = cursor[CurNormal];
XChangeWindowAttributes(dpy, root, CWEventMask | CWCursor, &at);
if(conf.root.background_command)
uicb_spawn(conf.root.background_command);
/* INIT INFOBAR */
infobar_init(&infobar);
/* INIT WORKABLE SPACE GEOMETRY */
sgeo.x = 0;
if(conf.bartop)
sgeo.y = conf.titlebar.pos ? infobar.geo.height : infobar.geo.height + conf.titlebar.height;
else
sgeo.y = conf.titlebar.pos ? 0 : conf.titlebar.height;
sgeo.width = DisplayWidth(dpy, screen);
sgeo.height = DisplayHeight(dpy, screen) - (infobar.geo.height + conf.titlebar.height);
/* INIT STUFF */
grabkeys();
/* MISC WARNING */
if(conf.titlebar.height < fonth)
fprintf(stderr, "WMFS Warning: Font too big, can't draw any text in the titlebar.\n");
return;
}
void
mainloop(void)
{

View File

@ -57,6 +57,8 @@
#define ButtonMask (ButtonPressMask | ButtonReleaseMask | ButtonMotionMask)
#define MouseMask (ButtonMask | PointerMotionMask)
#define KeyMask (KeyPressMask | KeyReleaseMask)
#define MAXH DisplayHeight(dpy, screen)
#define MAXW DisplayWidth(dpy, screen)
#define ITOA(p ,n) sprintf(p, "%d", n)
#define debug(p) fprintf(stderr, "debug: %d\n", p)
#define PAD 8
@ -168,13 +170,21 @@ void uicb_layout_next(uicb_t);
void uicb_set_mwfact(uicb_t);
void uicb_set_nmaster(uicb_t);
/* init.c */
void init(void);
void init_atom(void);
void init_font(void);
void init_cursor(void);
void init_root(void);
void init_key(void);
void init_geometry(void);
/* wmfs.c */
void checkotherwm(void);
int errorhandler(Display *d, XErrorEvent *event);
int errorhandlerdummy(Display *d, XErrorEvent *event);
int errorhandlerstart(Display *d, XErrorEvent *event);
void quit(void);
void init(void);
void mainloop(void);
void scan(void);
void uicb_quit(uicb_t);
@ -188,7 +198,6 @@ GC gc;
Window root;
XRectangle sgeo;
int screen;
int mw, mh;
Conf conf;
Key *keys;
Bool exiting;
@ -200,8 +209,7 @@ Atom net_atom[NetLast];
Cursor cursor[CurLast];
/* Fonts */
int fonth;
XftFont *xftfont;
XftFont *font;
/* InfoBar */
InfoBar infobar;
@ -216,6 +224,7 @@ Client *selbytag[MAXTAG];
/* Other */
uint numlockmask;
uint scrolllockmask;
Variable confvar[256];
#endif /* WMFS_H */