├── dwm ├── drw.o ├── dwm.o ├── dwm.png ├── util.o ├── dwmclean ├── img ├── st-preview#1.png └── 2023-01-29_23-43.png ├── util.h ├── util.c ├── transient.c ├── config.mk ├── Makefile ├── drw.h ├── README.md ├── LICENSE ├── dwm.1 ├── config.h ├── config.def.h ├── drw.c ├── vanitygaps.c └── dwm.c /dwm: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/motolla/dwm/HEAD/dwm -------------------------------------------------------------------------------- /drw.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/motolla/dwm/HEAD/drw.o -------------------------------------------------------------------------------- /dwm.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/motolla/dwm/HEAD/dwm.o -------------------------------------------------------------------------------- /dwm.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/motolla/dwm/HEAD/dwm.png -------------------------------------------------------------------------------- /util.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/motolla/dwm/HEAD/util.o -------------------------------------------------------------------------------- /dwmclean: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | cp config.def.h config.h; make; sudo make install 3 | -------------------------------------------------------------------------------- /img/st-preview#1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/motolla/dwm/HEAD/img/st-preview#1.png -------------------------------------------------------------------------------- /img/2023-01-29_23-43.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/motolla/dwm/HEAD/img/2023-01-29_23-43.png -------------------------------------------------------------------------------- /util.h: -------------------------------------------------------------------------------- 1 | /* See LICENSE file for copyright and license details. */ 2 | 3 | #define MAX(A, B) ((A) > (B) ? (A) : (B)) 4 | #define MIN(A, B) ((A) < (B) ? (A) : (B)) 5 | #define BETWEEN(X, A, B) ((A) <= (X) && (X) <= (B)) 6 | 7 | void die(const char *fmt, ...); 8 | void *ecalloc(size_t nmemb, size_t size); 9 | -------------------------------------------------------------------------------- /util.c: -------------------------------------------------------------------------------- 1 | /* See LICENSE file for copyright and license details. */ 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include "util.h" 8 | 9 | void 10 | die(const char *fmt, ...) 11 | { 12 | va_list ap; 13 | 14 | va_start(ap, fmt); 15 | vfprintf(stderr, fmt, ap); 16 | va_end(ap); 17 | 18 | if (fmt[0] && fmt[strlen(fmt)-1] == ':') { 19 | fputc(' ', stderr); 20 | perror(NULL); 21 | } else { 22 | fputc('\n', stderr); 23 | } 24 | 25 | exit(1); 26 | } 27 | 28 | void * 29 | ecalloc(size_t nmemb, size_t size) 30 | { 31 | void *p; 32 | 33 | if (!(p = calloc(nmemb, size))) 34 | die("calloc:"); 35 | return p; 36 | } 37 | -------------------------------------------------------------------------------- /transient.c: -------------------------------------------------------------------------------- 1 | /* cc transient.c -o transient -lX11 */ 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | int main(void) { 9 | Display *d; 10 | Window r, f, t = None; 11 | XSizeHints h; 12 | XEvent e; 13 | 14 | d = XOpenDisplay(NULL); 15 | if (!d) 16 | exit(1); 17 | r = DefaultRootWindow(d); 18 | 19 | f = XCreateSimpleWindow(d, r, 100, 100, 400, 400, 0, 0, 0); 20 | h.min_width = h.max_width = h.min_height = h.max_height = 400; 21 | h.flags = PMinSize | PMaxSize; 22 | XSetWMNormalHints(d, f, &h); 23 | XStoreName(d, f, "floating"); 24 | XMapWindow(d, f); 25 | 26 | XSelectInput(d, f, ExposureMask); 27 | while (1) { 28 | XNextEvent(d, &e); 29 | 30 | if (t == None) { 31 | sleep(5); 32 | t = XCreateSimpleWindow(d, r, 50, 50, 100, 100, 0, 0, 0); 33 | XSetTransientForHint(d, t, f); 34 | XStoreName(d, t, "transient"); 35 | XMapWindow(d, t); 36 | XSelectInput(d, t, ExposureMask); 37 | } 38 | } 39 | 40 | XCloseDisplay(d); 41 | exit(0); 42 | } 43 | -------------------------------------------------------------------------------- /config.mk: -------------------------------------------------------------------------------- 1 | # dwm version 2 | VERSION = 6.4 3 | 4 | # Customize below to fit your system 5 | 6 | # paths 7 | PREFIX = /usr/local 8 | MANPREFIX = ${PREFIX}/share/man 9 | 10 | X11INC = /usr/X11R6/include 11 | X11LIB = /usr/X11R6/lib 12 | 13 | # Xinerama, comment if you don't want it 14 | XINERAMALIBS = -lXinerama 15 | XINERAMAFLAGS = -DXINERAMA 16 | 17 | # freetype 18 | FREETYPELIBS = -lfontconfig -lXft 19 | FREETYPEINC = /usr/include/freetype2 20 | # OpenBSD (uncomment) 21 | #FREETYPEINC = ${X11INC}/freetype2 22 | #MANPREFIX = ${PREFIX}/man 23 | 24 | # includes and libs 25 | INCS = -I${X11INC} -I${FREETYPEINC} 26 | LIBS = -L${X11LIB} -lX11 ${XINERAMALIBS} ${FREETYPELIBS} 27 | 28 | # flags 29 | CPPFLAGS = -D_DEFAULT_SOURCE -D_BSD_SOURCE -D_POSIX_C_SOURCE=200809L -DVERSION=\"${VERSION}\" ${XINERAMAFLAGS} 30 | #CFLAGS = -g -std=c99 -pedantic -Wall -O0 ${INCS} ${CPPFLAGS} 31 | CFLAGS = -std=c99 -pedantic -Wall -Wno-deprecated-declarations -Os ${INCS} ${CPPFLAGS} 32 | LDFLAGS = ${LIBS} 33 | 34 | # Solaris 35 | #CFLAGS = -fast ${INCS} -DVERSION=\"${VERSION}\" 36 | #LDFLAGS = ${LIBS} 37 | 38 | # compiler and linker 39 | CC = cc 40 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | # dwm - dynamic window manager 2 | # See LICENSE file for copyright and license details. 3 | 4 | include config.mk 5 | 6 | SRC = drw.c dwm.c util.c 7 | OBJ = ${SRC:.c=.o} 8 | 9 | all: options dwm 10 | 11 | options: 12 | @echo dwm build options: 13 | @echo "CFLAGS = ${CFLAGS}" 14 | @echo "LDFLAGS = ${LDFLAGS}" 15 | @echo "CC = ${CC}" 16 | 17 | .c.o: 18 | ${CC} -c ${CFLAGS} $< 19 | 20 | ${OBJ}: config.h config.mk 21 | 22 | config.h: 23 | cp config.def.h $@ 24 | 25 | dwm: ${OBJ} 26 | ${CC} -o $@ ${OBJ} ${LDFLAGS} 27 | 28 | clean: 29 | rm -f dwm ${OBJ} dwm-${VERSION}.tar.gz 30 | 31 | dist: clean 32 | mkdir -p dwm-${VERSION} 33 | cp -R LICENSE Makefile README config.def.h config.mk\ 34 | dwm.1 drw.h util.h ${SRC} dwm.png transient.c dwm-${VERSION} 35 | tar -cf dwm-${VERSION}.tar dwm-${VERSION} 36 | gzip dwm-${VERSION}.tar 37 | rm -rf dwm-${VERSION} 38 | 39 | install: all 40 | mkdir -p ${DESTDIR}${PREFIX}/bin 41 | cp -f dwm ${DESTDIR}${PREFIX}/bin 42 | chmod 755 ${DESTDIR}${PREFIX}/bin/dwm 43 | mkdir -p ${DESTDIR}${MANPREFIX}/man1 44 | sed "s/VERSION/${VERSION}/g" < dwm.1 > ${DESTDIR}${MANPREFIX}/man1/dwm.1 45 | chmod 644 ${DESTDIR}${MANPREFIX}/man1/dwm.1 46 | 47 | uninstall: 48 | rm -f ${DESTDIR}${PREFIX}/bin/dwm\ 49 | ${DESTDIR}${MANPREFIX}/man1/dwm.1 50 | 51 | .PHONY: all options clean dist install uninstall 52 | -------------------------------------------------------------------------------- /drw.h: -------------------------------------------------------------------------------- 1 | /* See LICENSE file for copyright and license details. */ 2 | 3 | typedef struct { 4 | Cursor cursor; 5 | } Cur; 6 | 7 | typedef struct Fnt { 8 | Display *dpy; 9 | unsigned int h; 10 | XftFont *xfont; 11 | FcPattern *pattern; 12 | struct Fnt *next; 13 | } Fnt; 14 | 15 | enum { ColFg, ColBg, ColBorder }; /* Clr scheme index */ 16 | typedef XftColor Clr; 17 | 18 | typedef struct { 19 | unsigned int w, h; 20 | Display *dpy; 21 | int screen; 22 | Window root; 23 | Drawable drawable; 24 | GC gc; 25 | Clr *scheme; 26 | Fnt *fonts; 27 | } Drw; 28 | 29 | /* Drawable abstraction */ 30 | Drw *drw_create(Display *dpy, int screen, Window win, unsigned int w, unsigned int h); 31 | void drw_resize(Drw *drw, unsigned int w, unsigned int h); 32 | void drw_free(Drw *drw); 33 | 34 | /* Fnt abstraction */ 35 | Fnt *drw_fontset_create(Drw* drw, const char *fonts[], size_t fontcount); 36 | void drw_fontset_free(Fnt* set); 37 | unsigned int drw_fontset_getwidth(Drw *drw, const char *text); 38 | unsigned int drw_fontset_getwidth_clamp(Drw *drw, const char *text, unsigned int n); 39 | void drw_font_getexts(Fnt *font, const char *text, unsigned int len, unsigned int *w, unsigned int *h); 40 | 41 | /* Colorscheme abstraction */ 42 | void drw_clr_create(Drw *drw, Clr *dest, const char *clrname); 43 | Clr *drw_scm_create(Drw *drw, const char *clrnames[], size_t clrcount); 44 | 45 | /* Cursor abstraction */ 46 | Cur *drw_cur_create(Drw *drw, int shape); 47 | void drw_cur_free(Drw *drw, Cur *cursor); 48 | 49 | /* Drawing context manipulation */ 50 | void drw_setfontset(Drw *drw, Fnt *set); 51 | void drw_setscheme(Drw *drw, Clr *scm); 52 | 53 | /* Drawing functions */ 54 | void drw_rect(Drw *drw, int x, int y, unsigned int w, unsigned int h, int filled, int invert); 55 | int drw_text(Drw *drw, int x, int y, unsigned int w, unsigned int h, unsigned int lpad, const char *text, int invert); 56 | 57 | /* Map functions */ 58 | void drw_map(Drw *drw, Window win, int x, int y, unsigned int w, unsigned int h); 59 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # DWM 2 | dwm is a dynamic window manager for X. It manages windows in tiled, monocle and floating layouts. All of the layouts can be applied dynamically, optimising the environment for the application in use and the task performed. 3 | 4 | # Preview 5 |

6 | 7 | 8 |

9 | 10 | # Quick Keybind 11 | ``` 12 | # Open Terminal (st) 13 | Alt + Enter 14 | 15 | # Close & Kill Program 16 | Alt + q 17 | 18 | # Switch Focus Window 19 | Alt + j/k 20 | 21 | # Toggle Layout 22 | Alt + Space 23 | ``` 24 | 25 | # Patch Applied 26 | + [dwm-actualfullscreen-20211013-cb3f58a.diff](https://dwm.suckless.org/patches/actualfullscreen/dwm-actualfullscreen-20211013-cb3f58a.diff) 27 | + [dwm-alttagsdecoration-2020010304-cb3f58a.diff](https://dwm.suckless.org/patches/alttagsdecoration/dwm-alttagsdecoration-2020010304-cb3f58a.diff) 28 | + [dwm-alwayscenter-20200625-f04cac6.diff](https://dwm.suckless.org/patches/alwayscenter/dwm-alwayscenter-20200625-f04cac6.diff) 29 | + [dwm-cfacts-20200913-61bb8b2.diff](https://dwm.suckless.org/patches/cfacts/dwm-cfacts-20200913-61bb8b2.diff) 30 | + [dwm-cfacts-vanitygaps-6.2.diff](https://dwm.suckless.org/patches/vanitygaps/dwm-cfacts-vanitygaps-6.2.diff) 31 | + [dwm-rainbowtags-6.2.diff](https://dwm.suckless.org/patches/rainbowtags/dwm-rainbowtags-6.2.diff) 32 | + [dwm-status2d-6.3.diff](https://dwm.suckless.org/patches/status2d/dwm-status2d-6.3.diff) 33 | + [dwm-notitle-20210715-138b405.diff](https://dwm.suckless.org/patches/notitle/dwm-notitle-20210715-138b405.diff) 34 | 35 | # Requirements 36 | + Void 37 | ``` 38 | xbps-install libXft-devel libX11-devel libXinerama-devel 39 | ``` 40 | ``` 41 | Nerd Font JetBrains 42 | ``` 43 | # Build & Install 44 | + Clone the repo 45 | 46 | ``` 47 | git clone https://github.com/gillgrite/dwm.git 48 | ``` 49 | + Build & Install dwm 50 | ``` 51 | ./dwmclean 52 | ``` 53 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT/X Consortium License 2 | 3 | © 2006-2019 Anselm R Garbe 4 | © 2006-2009 Jukka Salmi 5 | © 2006-2007 Sander van Dijk 6 | © 2007-2011 Peter Hartlich 7 | © 2007-2009 Szabolcs Nagy 8 | © 2007-2009 Christof Musik 9 | © 2007-2009 Premysl Hruby 10 | © 2007-2008 Enno Gottox Boland 11 | © 2008 Martin Hurton 12 | © 2008 Neale Pickett 13 | © 2009 Mate Nagy 14 | © 2010-2016 Hiltjo Posthuma 15 | © 2010-2012 Connor Lane Smith 16 | © 2011 Christoph Lohmann <20h@r-36.net> 17 | © 2015-2016 Quentin Rameau 18 | © 2015-2016 Eric Pruitt 19 | © 2016-2017 Markus Teich 20 | © 2020-2022 Chris Down 21 | 22 | Permission is hereby granted, free of charge, to any person obtaining a 23 | copy of this software and associated documentation files (the "Software"), 24 | to deal in the Software without restriction, including without limitation 25 | the rights to use, copy, modify, merge, publish, distribute, sublicense, 26 | and/or sell copies of the Software, and to permit persons to whom the 27 | Software is furnished to do so, subject to the following conditions: 28 | 29 | The above copyright notice and this permission notice shall be included in 30 | all copies or substantial portions of the Software. 31 | 32 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 33 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 34 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 35 | THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 36 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 37 | FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 38 | DEALINGS IN THE SOFTWARE. 39 | -------------------------------------------------------------------------------- /dwm.1: -------------------------------------------------------------------------------- 1 | .TH DWM 1 dwm\-VERSION 2 | .SH NAME 3 | dwm \- dynamic window manager 4 | .SH SYNOPSIS 5 | .B dwm 6 | .RB [ \-v ] 7 | .SH DESCRIPTION 8 | dwm is a dynamic window manager for X. It manages windows in tiled, monocle 9 | and floating layouts. Either layout can be applied dynamically, optimising the 10 | environment for the application in use and the task performed. 11 | .P 12 | In tiled layouts windows are managed in a master and stacking area. The master 13 | area on the left contains one window by default, and the stacking area on the 14 | right contains all other windows. The number of master area windows can be 15 | adjusted from zero to an arbitrary number. In monocle layout all windows are 16 | maximised to the screen size. In floating layout windows can be resized and 17 | moved freely. Dialog windows are always managed floating, regardless of the 18 | layout applied. 19 | .P 20 | Windows are grouped by tags. Each window can be tagged with one or multiple 21 | tags. Selecting certain tags displays all windows with these tags. 22 | .P 23 | Each screen contains a small status bar which displays all available tags, the 24 | layout, the title of the focused window, and the text read from the root window 25 | name property, if the screen is focused. A floating window is indicated with an 26 | empty square and a maximised floating window is indicated with a filled square 27 | before the windows title. The selected tags are indicated with a different 28 | color. The tags of the focused window are indicated with a filled square in the 29 | top left corner. The tags which are applied to one or more windows are 30 | indicated with an empty square in the top left corner. 31 | .P 32 | dwm draws a small border around windows to indicate the focus state. 33 | .SH OPTIONS 34 | .TP 35 | .B \-v 36 | prints version information to stderr, then exits. 37 | .SH USAGE 38 | .SS Status bar 39 | .TP 40 | .B X root window name 41 | is read and displayed in the status text area. It can be set with the 42 | .BR xsetroot (1) 43 | command. 44 | .TP 45 | .B Button1 46 | click on a tag label to display all windows with that tag, click on the layout 47 | label toggles between tiled and floating layout. 48 | .TP 49 | .B Button3 50 | click on a tag label adds/removes all windows with that tag to/from the view. 51 | .TP 52 | .B Mod1\-Button1 53 | click on a tag label applies that tag to the focused window. 54 | .TP 55 | .B Mod1\-Button3 56 | click on a tag label adds/removes that tag to/from the focused window. 57 | .SS Keyboard commands 58 | .TP 59 | .B Mod1\-Shift\-Return 60 | Start 61 | .BR st(1). 62 | .TP 63 | .B Mod1\-p 64 | Spawn 65 | .BR dmenu(1) 66 | for launching other programs. 67 | .TP 68 | .B Mod1\-, 69 | Focus previous screen, if any. 70 | .TP 71 | .B Mod1\-. 72 | Focus next screen, if any. 73 | .TP 74 | .B Mod1\-Shift\-, 75 | Send focused window to previous screen, if any. 76 | .TP 77 | .B Mod1\-Shift\-. 78 | Send focused window to next screen, if any. 79 | .TP 80 | .B Mod1\-b 81 | Toggles bar on and off. 82 | .TP 83 | .B Mod1\-t 84 | Sets tiled layout. 85 | .TP 86 | .B Mod1\-f 87 | Sets floating layout. 88 | .TP 89 | .B Mod1\-m 90 | Sets monocle layout. 91 | .TP 92 | .B Mod1\-space 93 | Toggles between current and previous layout. 94 | .TP 95 | .B Mod1\-j 96 | Focus next window. 97 | .TP 98 | .B Mod1\-k 99 | Focus previous window. 100 | .TP 101 | .B Mod1\-i 102 | Increase number of windows in master area. 103 | .TP 104 | .B Mod1\-d 105 | Decrease number of windows in master area. 106 | .TP 107 | .B Mod1\-l 108 | Increase master area size. 109 | .TP 110 | .B Mod1\-h 111 | Decrease master area size. 112 | .TP 113 | .B Mod1\-Return 114 | Zooms/cycles focused window to/from master area (tiled layouts only). 115 | .TP 116 | .B Mod1\-Shift\-c 117 | Close focused window. 118 | .TP 119 | .B Mod1\-Shift\-f 120 | Toggle fullscreen for focused window. 121 | .TP 122 | .B Mod1\-Shift\-space 123 | Toggle focused window between tiled and floating state. 124 | .TP 125 | .B Mod1\-Tab 126 | Toggles to the previously selected tags. 127 | .TP 128 | .B Mod1\-Shift\-[1..n] 129 | Apply nth tag to focused window. 130 | .TP 131 | .B Mod1\-Shift\-0 132 | Apply all tags to focused window. 133 | .TP 134 | .B Mod1\-Control\-Shift\-[1..n] 135 | Add/remove nth tag to/from focused window. 136 | .TP 137 | .B Mod1\-[1..n] 138 | View all windows with nth tag. 139 | .TP 140 | .B Mod1\-0 141 | View all windows with any tag. 142 | .TP 143 | .B Mod1\-Control\-[1..n] 144 | Add/remove all windows with nth tag to/from the view. 145 | .TP 146 | .B Mod1\-Shift\-q 147 | Quit dwm. 148 | .SS Mouse commands 149 | .TP 150 | .B Mod1\-Button1 151 | Move focused window while dragging. Tiled windows will be toggled to the floating state. 152 | .TP 153 | .B Mod1\-Button2 154 | Toggles focused window between floating and tiled state. 155 | .TP 156 | .B Mod1\-Button3 157 | Resize focused window while dragging. Tiled windows will be toggled to the floating state. 158 | .SH CUSTOMIZATION 159 | dwm is customized by creating a custom config.h and (re)compiling the source 160 | code. This keeps it fast, secure and simple. 161 | .SH SEE ALSO 162 | .BR dmenu (1), 163 | .BR st (1) 164 | .SH ISSUES 165 | Java applications which use the XToolkit/XAWT backend may draw grey windows 166 | only. The XToolkit/XAWT backend breaks ICCCM-compliance in recent JDK 1.5 and early 167 | JDK 1.6 versions, because it assumes a reparenting window manager. Possible workarounds 168 | are using JDK 1.4 (which doesn't contain the XToolkit/XAWT backend) or setting the 169 | environment variable 170 | .BR AWT_TOOLKIT=MToolkit 171 | (to use the older Motif backend instead) or running 172 | .B xprop -root -f _NET_WM_NAME 32a -set _NET_WM_NAME LG3D 173 | or 174 | .B wmname LG3D 175 | (to pretend that a non-reparenting window manager is running that the 176 | XToolkit/XAWT backend can recognize) or when using OpenJDK setting the environment variable 177 | .BR _JAVA_AWT_WM_NONREPARENTING=1 . 178 | .SH BUGS 179 | Send all bug reports with a patch to hackers@suckless.org. 180 | -------------------------------------------------------------------------------- /config.h: -------------------------------------------------------------------------------- 1 | /* See LICENSE file for copyright and license details. */ 2 | 3 | /* appearance */ 4 | static const unsigned int borderpx = 2; /* border pixel of windows */ 5 | static const unsigned int snap = 32; /* snap pixel */ 6 | static const unsigned int gappih = 12; /* horiz inner gap between windows */ 7 | static const unsigned int gappiv = 12; /* vert inner gap between windows */ 8 | static const unsigned int gappoh = 8; /* horiz outer gap between windows and screen edge */ 9 | static const unsigned int gappov = 4; /* vert outer gap between windows and screen edge */ 10 | static int smartgaps = 0; /* 1 means no outer gap when there is only one window */ 11 | static const int showbar = 1; /* 0 means no bar */ 12 | static const int topbar = 1; /* 0 means bottom bar */ 13 | static const char *fonts[] = { "JetBrainsMono NF:Semibold:size=14" }; 14 | static const char col_bg1[] = "#1d2021"; 15 | static const char col_bg2[] = "#282828"; 16 | static const char col_bg3[] = "#32302f"; 17 | static const char col_fg1[] = "#fbf1c7"; 18 | static const char col_acc[] = "#d65d0e"; 19 | static const char *colors[][3] = { 20 | /* fg bg border */ 21 | [SchemeNorm] = { col_bg3, col_bg1, col_bg2 }, 22 | [SchemeSel] = { col_fg1, col_fg1, col_acc }, 23 | }; 24 | 25 | /* tagging */ 26 | static const char *tags[] = { "󱍢", "󰈹", "", "", "", "" }; 27 | static const char *alttags[] = { "󱍢", "󰈹", "", "", "", "" }; 28 | 29 | static const char *tagsel[][2] = { 30 | { "#ebdbb2", col_bg1 }, 31 | { "#d65d0e", col_bg1 }, 32 | { "#98971a", col_bg1 }, 33 | { "#cc241d", col_bg1 }, 34 | { "#458588", col_bg1 }, 35 | { "#b8bb26", col_bg1 }, 36 | }; 37 | 38 | /* 39 | static const unsigned int ulinepad = 5; horizontal padding between the underline and tag 40 | static const unsigned int ulinestroke = 2; thickness / height of the underline 41 | static const unsigned int ulinevoffset = 0; how far above the bottom of the bar the line should appear 42 | static const int ulineall = 0; 1 to show underline on all tags, 0 for just the active ones */ 43 | 44 | static const Rule rules[] = { 45 | /* xprop(1): 46 | * WM_CLASS(STRING) = instance, class 47 | * WM_NAME(STRING) = title 48 | */ 49 | /* class instance title tags mask isfloating monitor */ 50 | { "Gimp", NULL, NULL, 0, 1, -1 }, 51 | { "Firefox", NULL, NULL, 2, 0, -1 }, 52 | { "qutebrowser", NULL, NULL, 2, 0, -1 }, 53 | { "pavucontrol", NULL, NULL, 0, 0, -1 }, 54 | }; 55 | 56 | /* layout(s) */ 57 | static const float mfact = 0.55; /* factor of master area size [0.05..0.95] */ 58 | static const int nmaster = 1; /* number of clients in master area */ 59 | static const int resizehints = 1; /* 1 means respect size hints in tiled resizals */ 60 | static const int lockfullscreen = 1; /* 1 will force focus on the fullscreen window */ 61 | 62 | #define FORCE_VSPLIT 1 /* nrowgrid layout: force two clients to always split vertically */ 63 | #include "vanitygaps.c" 64 | 65 | static const Layout layouts[] = { 66 | /* symbol arrange function */ 67 | { "[]=", tile }, /* first entry is default */ 68 | { "[M]", monocle }, 69 | { "[@]", spiral }, 70 | { "[\\]", dwindle }, 71 | { "H[]", deck }, 72 | { "TTT", bstack }, 73 | { "===", bstackhoriz }, 74 | { "HHH", grid }, 75 | { "###", nrowgrid }, 76 | { "---", horizgrid }, 77 | { ":::", gaplessgrid }, 78 | { "|M|", centeredmaster }, 79 | { ">M>", centeredfloatingmaster }, 80 | { "><>", NULL }, /* no layout function means floating behavior */ 81 | { NULL, NULL }, 82 | }; 83 | 84 | /* key definitions */ 85 | #define MODKEY Mod1Mask 86 | #define TAGKEYS(KEY,TAG) \ 87 | { MODKEY, KEY, view, {.ui = 1 << TAG} }, \ 88 | { MODKEY|ControlMask, KEY, toggleview, {.ui = 1 << TAG} }, \ 89 | { MODKEY|ShiftMask, KEY, tag, {.ui = 1 << TAG} }, \ 90 | { MODKEY|ControlMask|ShiftMask, KEY, toggletag, {.ui = 1 << TAG} }, 91 | 92 | /* helper for spawning shell commands in the pre dwm-5.0 fashion */ 93 | #define SHCMD(cmd) { .v = (const char*[]){ "/bin/sh", "-c", cmd, NULL } } 94 | 95 | /* commands */ 96 | static const char *dmenucmd[] = { "dmenu_run", "-c", "-l", "12", NULL }; 97 | static const char *termcmd[] = { "st", NULL }; 98 | 99 | /* volume commands */ 100 | static const char *uvol[] = { "pactl", "set-sink-volume", "0", "+5%", NULL }; 101 | static const char *dvol[] = { "pactl", "set-sink-volume", "0", "-5%", NULL }; 102 | static const char *mvol[] = { "pactl", "set-sink-mute", "0", "toggle", NULL }; 103 | 104 | /* brightness commands */ 105 | static const char *ubright[] = { "xbacklight", "-inc", "10", NULL }; 106 | static const char *dbright[] = { "xbacklight", "-dec", "10", NULL }; 107 | 108 | static const Key keys[] = { 109 | /* modifier key function argument */ 110 | { MODKEY, XK_p, spawn, {.v = dmenucmd } }, 111 | { MODKEY, XK_bracketright, spawn, SHCMD("~/bin/settings")}, 112 | { MODKEY, XK_bracketleft, spawn, SHCMD("~/bin/menuz")}, 113 | { MODKEY, XK_Return, spawn, {.v = termcmd } }, 114 | { MODKEY, XK_b, togglebar, {0} }, 115 | { MODKEY, XK_j, focusstack, {.i = +1 } }, 116 | { MODKEY, XK_k, focusstack, {.i = -1 } }, 117 | { MODKEY, XK_i, incnmaster, {.i = +1 } }, 118 | { MODKEY, XK_d, incnmaster, {.i = -1 } }, 119 | { MODKEY, XK_h, setmfact, {.f = -0.05} }, 120 | { MODKEY, XK_l, setmfact, {.f = +0.05} }, 121 | { MODKEY|ShiftMask, XK_h, setcfact, {.f = +0.25} }, 122 | { MODKEY|ShiftMask, XK_l, setcfact, {.f = -0.25} }, 123 | { MODKEY|ShiftMask, XK_o, setcfact, {.f = 0.00} }, 124 | { MODKEY|ShiftMask, XK_Return, zoom, {0} }, 125 | { MODKEY|Mod4Mask, XK_u, incrgaps, {.i = +1 } }, 126 | { MODKEY|Mod4Mask|ShiftMask, XK_u, incrgaps, {.i = -1 } }, 127 | { MODKEY|Mod4Mask, XK_i, incrigaps, {.i = +1 } }, 128 | { MODKEY|Mod4Mask|ShiftMask, XK_i, incrigaps, {.i = -1 } }, 129 | { MODKEY|Mod4Mask, XK_o, incrogaps, {.i = +1 } }, 130 | { MODKEY|Mod4Mask|ShiftMask, XK_o, incrogaps, {.i = -1 } }, 131 | { MODKEY|Mod4Mask, XK_6, incrihgaps, {.i = +1 } }, 132 | { MODKEY|Mod4Mask|ShiftMask, XK_6, incrihgaps, {.i = -1 } }, 133 | { MODKEY|Mod4Mask, XK_7, incrivgaps, {.i = +1 } }, 134 | { MODKEY|Mod4Mask|ShiftMask, XK_7, incrivgaps, {.i = -1 } }, 135 | { MODKEY|Mod4Mask, XK_8, incrohgaps, {.i = +1 } }, 136 | { MODKEY|Mod4Mask|ShiftMask, XK_8, incrohgaps, {.i = -1 } }, 137 | { MODKEY|Mod4Mask, XK_9, incrovgaps, {.i = +1 } }, 138 | { MODKEY|Mod4Mask|ShiftMask, XK_9, incrovgaps, {.i = -1 } }, 139 | { MODKEY|Mod4Mask, XK_0, togglegaps, {0} }, 140 | { MODKEY|Mod4Mask|ShiftMask, XK_0, defaultgaps, {0} }, 141 | { MODKEY, XK_Tab, view, {0} }, 142 | { MODKEY, XK_q, killclient, {0} }, 143 | { MODKEY, XK_t, setlayout, {.v = &layouts[0]} }, 144 | { MODKEY, XK_f, setlayout, {.v = &layouts[1]} }, 145 | { MODKEY, XK_m, setlayout, {.v = &layouts[2]} }, 146 | { MODKEY, XK_space, setlayout, {0} }, 147 | { MODKEY|ShiftMask, XK_space, togglefloating, {0} }, 148 | { MODKEY|ShiftMask, XK_f, togglefullscr, {0} }, 149 | { MODKEY, XK_0, view, {.ui = ~0 } }, 150 | { MODKEY|ShiftMask, XK_0, tag, {.ui = ~0 } }, 151 | { MODKEY, XK_comma, focusmon, {.i = -1 } }, 152 | { MODKEY, XK_period, focusmon, {.i = +1 } }, 153 | { MODKEY|ShiftMask, XK_comma, tagmon, {.i = -1 } }, 154 | { MODKEY|ShiftMask, XK_period, tagmon, {.i = +1 } }, 155 | { MODKEY, XK_Print, spawn, SHCMD("scrot -d 4 $HOME/Pictures/%d-%m-%H-%M-%S.png") }, 156 | { 0, XK_Home, spawn, SHCMD("flameshot screen --path ~/Pictures/") }, 157 | { 0, 0x1008FF02, spawn, {.v = ubright } }, 158 | { 0, 0x1008FF03, spawn, {.v = dbright } }, 159 | { 0, 0x1008FF13, spawn, {.v = uvol } }, 160 | { 0, 0x1008FF11, spawn, {.v = dvol } }, 161 | { 0, 0x1008FF12, spawn, {.v = mvol } }, 162 | TAGKEYS( XK_1, 0) 163 | TAGKEYS( XK_2, 1) 164 | TAGKEYS( XK_3, 2) 165 | TAGKEYS( XK_4, 3) 166 | TAGKEYS( XK_5, 4) 167 | TAGKEYS( XK_6, 5) 168 | TAGKEYS( XK_7, 6) 169 | TAGKEYS( XK_8, 7) 170 | TAGKEYS( XK_9, 8) 171 | { MODKEY|ShiftMask, XK_q, quit, {0} }, 172 | }; 173 | 174 | /* button definitions */ 175 | /* click can be ClkTagBar, ClkLtSymbol, ClkStatusText, ClkWinTitle, ClkClientWin, or ClkRootWin */ 176 | static const Button buttons[] = { 177 | /* click event mask button function argument */ 178 | { ClkLtSymbol, 0, Button1, setlayout, {0} }, 179 | { ClkLtSymbol, 0, Button3, setlayout, {.v = &layouts[2]} }, 180 | { ClkWinTitle, 0, Button2, zoom, {0} }, 181 | { ClkStatusText, 0, Button2, spawn, {.v = termcmd } }, 182 | { ClkClientWin, MODKEY, Button1, movemouse, {0} }, 183 | { ClkClientWin, MODKEY, Button2, togglefloating, {0} }, 184 | { ClkClientWin, MODKEY, Button3, resizemouse, {0} }, 185 | { ClkTagBar, 0, Button1, view, {0} }, 186 | { ClkTagBar, 0, Button3, toggleview, {0} }, 187 | { ClkTagBar, MODKEY, Button1, tag, {0} }, 188 | { ClkTagBar, MODKEY, Button3, toggletag, {0} }, 189 | }; 190 | 191 | -------------------------------------------------------------------------------- /config.def.h: -------------------------------------------------------------------------------- 1 | /* See LICENSE file for copyright and license details. */ 2 | 3 | /* appearance */ 4 | static const unsigned int borderpx = 2; /* border pixel of windows */ 5 | static const unsigned int snap = 32; /* snap pixel */ 6 | static const unsigned int gappih = 12; /* horiz inner gap between windows */ 7 | static const unsigned int gappiv = 12; /* vert inner gap between windows */ 8 | static const unsigned int gappoh = 8; /* horiz outer gap between windows and screen edge */ 9 | static const unsigned int gappov = 4; /* vert outer gap between windows and screen edge */ 10 | static int smartgaps = 0; /* 1 means no outer gap when there is only one window */ 11 | static const int showbar = 1; /* 0 means no bar */ 12 | static const int topbar = 1; /* 0 means bottom bar */ 13 | static const char *fonts[] = { "JetBrainsMono NF:Semibold:size=14" }; 14 | static const char col_bg1[] = "#1d2021"; 15 | static const char col_bg2[] = "#282828"; 16 | static const char col_bg3[] = "#32302f"; 17 | static const char col_fg1[] = "#fbf1c7"; 18 | static const char col_acc[] = "#d65d0e"; 19 | static const char *colors[][3] = { 20 | /* fg bg border */ 21 | [SchemeNorm] = { col_bg3, col_bg1, col_bg2 }, 22 | [SchemeSel] = { col_fg1, col_fg1, col_acc }, 23 | }; 24 | 25 | /* tagging */ 26 | static const char *tags[] = { "󱍢", "󰈹", "", "", "", "" }; 27 | static const char *alttags[] = { "󱍢", "󰈹", "", "", "", "" }; 28 | 29 | static const char *tagsel[][2] = { 30 | { "#ebdbb2", col_bg1 }, 31 | { "#d65d0e", col_bg1 }, 32 | { "#98971a", col_bg1 }, 33 | { "#cc241d", col_bg1 }, 34 | { "#458588", col_bg1 }, 35 | { "#b8bb26", col_bg1 }, 36 | }; 37 | 38 | /* 39 | static const unsigned int ulinepad = 5; horizontal padding between the underline and tag 40 | static const unsigned int ulinestroke = 2; thickness / height of the underline 41 | static const unsigned int ulinevoffset = 0; how far above the bottom of the bar the line should appear 42 | static const int ulineall = 0; 1 to show underline on all tags, 0 for just the active ones */ 43 | 44 | static const Rule rules[] = { 45 | /* xprop(1): 46 | * WM_CLASS(STRING) = instance, class 47 | * WM_NAME(STRING) = title 48 | */ 49 | /* class instance title tags mask isfloating monitor */ 50 | { "Gimp", NULL, NULL, 0, 1, -1 }, 51 | { "Firefox", NULL, NULL, 2, 0, -1 }, 52 | { "qutebrowser", NULL, NULL, 2, 0, -1 }, 53 | { "pavucontrol", NULL, NULL, 0, 0, -1 }, 54 | }; 55 | 56 | /* layout(s) */ 57 | static const float mfact = 0.55; /* factor of master area size [0.05..0.95] */ 58 | static const int nmaster = 1; /* number of clients in master area */ 59 | static const int resizehints = 1; /* 1 means respect size hints in tiled resizals */ 60 | static const int lockfullscreen = 1; /* 1 will force focus on the fullscreen window */ 61 | 62 | #define FORCE_VSPLIT 1 /* nrowgrid layout: force two clients to always split vertically */ 63 | #include "vanitygaps.c" 64 | 65 | static const Layout layouts[] = { 66 | /* symbol arrange function */ 67 | { "[]=", tile }, /* first entry is default */ 68 | { "[M]", monocle }, 69 | { "[@]", spiral }, 70 | { "[\\]", dwindle }, 71 | { "H[]", deck }, 72 | { "TTT", bstack }, 73 | { "===", bstackhoriz }, 74 | { "HHH", grid }, 75 | { "###", nrowgrid }, 76 | { "---", horizgrid }, 77 | { ":::", gaplessgrid }, 78 | { "|M|", centeredmaster }, 79 | { ">M>", centeredfloatingmaster }, 80 | { "><>", NULL }, /* no layout function means floating behavior */ 81 | { NULL, NULL }, 82 | }; 83 | 84 | /* key definitions */ 85 | #define MODKEY Mod1Mask 86 | #define TAGKEYS(KEY,TAG) \ 87 | { MODKEY, KEY, view, {.ui = 1 << TAG} }, \ 88 | { MODKEY|ControlMask, KEY, toggleview, {.ui = 1 << TAG} }, \ 89 | { MODKEY|ShiftMask, KEY, tag, {.ui = 1 << TAG} }, \ 90 | { MODKEY|ControlMask|ShiftMask, KEY, toggletag, {.ui = 1 << TAG} }, 91 | 92 | /* helper for spawning shell commands in the pre dwm-5.0 fashion */ 93 | #define SHCMD(cmd) { .v = (const char*[]){ "/bin/sh", "-c", cmd, NULL } } 94 | 95 | /* commands */ 96 | static const char *dmenucmd[] = { "dmenu_run", "-c", "-l", "12", NULL }; 97 | static const char *termcmd[] = { "st", NULL }; 98 | 99 | /* volume commands */ 100 | static const char *uvol[] = { "pactl", "set-sink-volume", "0", "+5%", NULL }; 101 | static const char *dvol[] = { "pactl", "set-sink-volume", "0", "-5%", NULL }; 102 | static const char *mvol[] = { "pactl", "set-sink-mute", "0", "toggle", NULL }; 103 | 104 | /* brightness commands */ 105 | static const char *ubright[] = { "xbacklight", "-inc", "10", NULL }; 106 | static const char *dbright[] = { "xbacklight", "-dec", "10", NULL }; 107 | 108 | static const Key keys[] = { 109 | /* modifier key function argument */ 110 | { MODKEY, XK_p, spawn, {.v = dmenucmd } }, 111 | { MODKEY, XK_bracketright, spawn, SHCMD("~/bin/settings")}, 112 | { MODKEY, XK_bracketleft, spawn, SHCMD("~/bin/menuz")}, 113 | { MODKEY, XK_Return, spawn, {.v = termcmd } }, 114 | { MODKEY, XK_b, togglebar, {0} }, 115 | { MODKEY, XK_j, focusstack, {.i = +1 } }, 116 | { MODKEY, XK_k, focusstack, {.i = -1 } }, 117 | { MODKEY, XK_i, incnmaster, {.i = +1 } }, 118 | { MODKEY, XK_d, incnmaster, {.i = -1 } }, 119 | { MODKEY, XK_h, setmfact, {.f = -0.05} }, 120 | { MODKEY, XK_l, setmfact, {.f = +0.05} }, 121 | { MODKEY|ShiftMask, XK_h, setcfact, {.f = +0.25} }, 122 | { MODKEY|ShiftMask, XK_l, setcfact, {.f = -0.25} }, 123 | { MODKEY|ShiftMask, XK_o, setcfact, {.f = 0.00} }, 124 | { MODKEY|ShiftMask, XK_Return, zoom, {0} }, 125 | { MODKEY|Mod4Mask, XK_u, incrgaps, {.i = +1 } }, 126 | { MODKEY|Mod4Mask|ShiftMask, XK_u, incrgaps, {.i = -1 } }, 127 | { MODKEY|Mod4Mask, XK_i, incrigaps, {.i = +1 } }, 128 | { MODKEY|Mod4Mask|ShiftMask, XK_i, incrigaps, {.i = -1 } }, 129 | { MODKEY|Mod4Mask, XK_o, incrogaps, {.i = +1 } }, 130 | { MODKEY|Mod4Mask|ShiftMask, XK_o, incrogaps, {.i = -1 } }, 131 | { MODKEY|Mod4Mask, XK_6, incrihgaps, {.i = +1 } }, 132 | { MODKEY|Mod4Mask|ShiftMask, XK_6, incrihgaps, {.i = -1 } }, 133 | { MODKEY|Mod4Mask, XK_7, incrivgaps, {.i = +1 } }, 134 | { MODKEY|Mod4Mask|ShiftMask, XK_7, incrivgaps, {.i = -1 } }, 135 | { MODKEY|Mod4Mask, XK_8, incrohgaps, {.i = +1 } }, 136 | { MODKEY|Mod4Mask|ShiftMask, XK_8, incrohgaps, {.i = -1 } }, 137 | { MODKEY|Mod4Mask, XK_9, incrovgaps, {.i = +1 } }, 138 | { MODKEY|Mod4Mask|ShiftMask, XK_9, incrovgaps, {.i = -1 } }, 139 | { MODKEY|Mod4Mask, XK_0, togglegaps, {0} }, 140 | { MODKEY|Mod4Mask|ShiftMask, XK_0, defaultgaps, {0} }, 141 | { MODKEY, XK_Tab, view, {0} }, 142 | { MODKEY, XK_q, killclient, {0} }, 143 | { MODKEY, XK_t, setlayout, {.v = &layouts[0]} }, 144 | { MODKEY, XK_f, setlayout, {.v = &layouts[1]} }, 145 | { MODKEY, XK_m, setlayout, {.v = &layouts[2]} }, 146 | { MODKEY, XK_space, setlayout, {0} }, 147 | { MODKEY|ShiftMask, XK_space, togglefloating, {0} }, 148 | { MODKEY|ShiftMask, XK_f, togglefullscr, {0} }, 149 | { MODKEY, XK_0, view, {.ui = ~0 } }, 150 | { MODKEY|ShiftMask, XK_0, tag, {.ui = ~0 } }, 151 | { MODKEY, XK_comma, focusmon, {.i = -1 } }, 152 | { MODKEY, XK_period, focusmon, {.i = +1 } }, 153 | { MODKEY|ShiftMask, XK_comma, tagmon, {.i = -1 } }, 154 | { MODKEY|ShiftMask, XK_period, tagmon, {.i = +1 } }, 155 | { MODKEY, XK_Print, spawn, SHCMD("scrot -d 4 $HOME/Pictures/%d-%m-%H-%M-%S.png") }, 156 | { 0, XK_Home, spawn, SHCMD("flameshot screen --path ~/Pictures/") }, 157 | { 0, 0x1008FF02, spawn, {.v = ubright } }, 158 | { 0, 0x1008FF03, spawn, {.v = dbright } }, 159 | { 0, 0x1008FF13, spawn, {.v = uvol } }, 160 | { 0, 0x1008FF11, spawn, {.v = dvol } }, 161 | { 0, 0x1008FF12, spawn, {.v = mvol } }, 162 | TAGKEYS( XK_1, 0) 163 | TAGKEYS( XK_2, 1) 164 | TAGKEYS( XK_3, 2) 165 | TAGKEYS( XK_4, 3) 166 | TAGKEYS( XK_5, 4) 167 | TAGKEYS( XK_6, 5) 168 | TAGKEYS( XK_7, 6) 169 | TAGKEYS( XK_8, 7) 170 | TAGKEYS( XK_9, 8) 171 | { MODKEY|ShiftMask, XK_q, quit, {0} }, 172 | }; 173 | 174 | /* button definitions */ 175 | /* click can be ClkTagBar, ClkLtSymbol, ClkStatusText, ClkWinTitle, ClkClientWin, or ClkRootWin */ 176 | static const Button buttons[] = { 177 | /* click event mask button function argument */ 178 | { ClkLtSymbol, 0, Button1, setlayout, {0} }, 179 | { ClkLtSymbol, 0, Button3, setlayout, {.v = &layouts[2]} }, 180 | { ClkWinTitle, 0, Button2, zoom, {0} }, 181 | { ClkStatusText, 0, Button2, spawn, {.v = termcmd } }, 182 | { ClkClientWin, MODKEY, Button1, movemouse, {0} }, 183 | { ClkClientWin, MODKEY, Button2, togglefloating, {0} }, 184 | { ClkClientWin, MODKEY, Button3, resizemouse, {0} }, 185 | { ClkTagBar, 0, Button1, view, {0} }, 186 | { ClkTagBar, 0, Button3, toggleview, {0} }, 187 | { ClkTagBar, MODKEY, Button1, tag, {0} }, 188 | { ClkTagBar, MODKEY, Button3, toggletag, {0} }, 189 | }; 190 | 191 | -------------------------------------------------------------------------------- /drw.c: -------------------------------------------------------------------------------- 1 | /* See LICENSE file for copyright and license details. */ 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | #include "drw.h" 9 | #include "util.h" 10 | 11 | #define UTF_INVALID 0xFFFD 12 | #define UTF_SIZ 4 13 | 14 | static const unsigned char utfbyte[UTF_SIZ + 1] = {0x80, 0, 0xC0, 0xE0, 0xF0}; 15 | static const unsigned char utfmask[UTF_SIZ + 1] = {0xC0, 0x80, 0xE0, 0xF0, 0xF8}; 16 | static const long utfmin[UTF_SIZ + 1] = { 0, 0, 0x80, 0x800, 0x10000}; 17 | static const long utfmax[UTF_SIZ + 1] = {0x10FFFF, 0x7F, 0x7FF, 0xFFFF, 0x10FFFF}; 18 | 19 | static long 20 | utf8decodebyte(const char c, size_t *i) 21 | { 22 | for (*i = 0; *i < (UTF_SIZ + 1); ++(*i)) 23 | if (((unsigned char)c & utfmask[*i]) == utfbyte[*i]) 24 | return (unsigned char)c & ~utfmask[*i]; 25 | return 0; 26 | } 27 | 28 | static size_t 29 | utf8validate(long *u, size_t i) 30 | { 31 | if (!BETWEEN(*u, utfmin[i], utfmax[i]) || BETWEEN(*u, 0xD800, 0xDFFF)) 32 | *u = UTF_INVALID; 33 | for (i = 1; *u > utfmax[i]; ++i) 34 | ; 35 | return i; 36 | } 37 | 38 | static size_t 39 | utf8decode(const char *c, long *u, size_t clen) 40 | { 41 | size_t i, j, len, type; 42 | long udecoded; 43 | 44 | *u = UTF_INVALID; 45 | if (!clen) 46 | return 0; 47 | udecoded = utf8decodebyte(c[0], &len); 48 | if (!BETWEEN(len, 1, UTF_SIZ)) 49 | return 1; 50 | for (i = 1, j = 1; i < clen && j < len; ++i, ++j) { 51 | udecoded = (udecoded << 6) | utf8decodebyte(c[i], &type); 52 | if (type) 53 | return j; 54 | } 55 | if (j < len) 56 | return 0; 57 | *u = udecoded; 58 | utf8validate(u, len); 59 | 60 | return len; 61 | } 62 | 63 | Drw * 64 | drw_create(Display *dpy, int screen, Window root, unsigned int w, unsigned int h) 65 | { 66 | Drw *drw = ecalloc(1, sizeof(Drw)); 67 | 68 | drw->dpy = dpy; 69 | drw->screen = screen; 70 | drw->root = root; 71 | drw->w = w; 72 | drw->h = h; 73 | drw->drawable = XCreatePixmap(dpy, root, w, h, DefaultDepth(dpy, screen)); 74 | drw->gc = XCreateGC(dpy, root, 0, NULL); 75 | XSetLineAttributes(dpy, drw->gc, 1, LineSolid, CapButt, JoinMiter); 76 | 77 | return drw; 78 | } 79 | 80 | void 81 | drw_resize(Drw *drw, unsigned int w, unsigned int h) 82 | { 83 | if (!drw) 84 | return; 85 | 86 | drw->w = w; 87 | drw->h = h; 88 | if (drw->drawable) 89 | XFreePixmap(drw->dpy, drw->drawable); 90 | drw->drawable = XCreatePixmap(drw->dpy, drw->root, w, h, DefaultDepth(drw->dpy, drw->screen)); 91 | } 92 | 93 | void 94 | drw_free(Drw *drw) 95 | { 96 | XFreePixmap(drw->dpy, drw->drawable); 97 | XFreeGC(drw->dpy, drw->gc); 98 | drw_fontset_free(drw->fonts); 99 | free(drw); 100 | } 101 | 102 | /* This function is an implementation detail. Library users should use 103 | * drw_fontset_create instead. 104 | */ 105 | static Fnt * 106 | xfont_create(Drw *drw, const char *fontname, FcPattern *fontpattern) 107 | { 108 | Fnt *font; 109 | XftFont *xfont = NULL; 110 | FcPattern *pattern = NULL; 111 | 112 | if (fontname) { 113 | /* Using the pattern found at font->xfont->pattern does not yield the 114 | * same substitution results as using the pattern returned by 115 | * FcNameParse; using the latter results in the desired fallback 116 | * behaviour whereas the former just results in missing-character 117 | * rectangles being drawn, at least with some fonts. */ 118 | if (!(xfont = XftFontOpenName(drw->dpy, drw->screen, fontname))) { 119 | fprintf(stderr, "error, cannot load font from name: '%s'\n", fontname); 120 | return NULL; 121 | } 122 | if (!(pattern = FcNameParse((FcChar8 *) fontname))) { 123 | fprintf(stderr, "error, cannot parse font name to pattern: '%s'\n", fontname); 124 | XftFontClose(drw->dpy, xfont); 125 | return NULL; 126 | } 127 | } else if (fontpattern) { 128 | if (!(xfont = XftFontOpenPattern(drw->dpy, fontpattern))) { 129 | fprintf(stderr, "error, cannot load font from pattern.\n"); 130 | return NULL; 131 | } 132 | } else { 133 | die("no font specified."); 134 | } 135 | 136 | font = ecalloc(1, sizeof(Fnt)); 137 | font->xfont = xfont; 138 | font->pattern = pattern; 139 | font->h = xfont->ascent + xfont->descent; 140 | font->dpy = drw->dpy; 141 | 142 | return font; 143 | } 144 | 145 | static void 146 | xfont_free(Fnt *font) 147 | { 148 | if (!font) 149 | return; 150 | if (font->pattern) 151 | FcPatternDestroy(font->pattern); 152 | XftFontClose(font->dpy, font->xfont); 153 | free(font); 154 | } 155 | 156 | Fnt* 157 | drw_fontset_create(Drw* drw, const char *fonts[], size_t fontcount) 158 | { 159 | Fnt *cur, *ret = NULL; 160 | size_t i; 161 | 162 | if (!drw || !fonts) 163 | return NULL; 164 | 165 | for (i = 1; i <= fontcount; i++) { 166 | if ((cur = xfont_create(drw, fonts[fontcount - i], NULL))) { 167 | cur->next = ret; 168 | ret = cur; 169 | } 170 | } 171 | return (drw->fonts = ret); 172 | } 173 | 174 | void 175 | drw_fontset_free(Fnt *font) 176 | { 177 | if (font) { 178 | drw_fontset_free(font->next); 179 | xfont_free(font); 180 | } 181 | } 182 | 183 | void 184 | drw_clr_create(Drw *drw, Clr *dest, const char *clrname) 185 | { 186 | if (!drw || !dest || !clrname) 187 | return; 188 | 189 | if (!XftColorAllocName(drw->dpy, DefaultVisual(drw->dpy, drw->screen), 190 | DefaultColormap(drw->dpy, drw->screen), 191 | clrname, dest)) 192 | die("error, cannot allocate color '%s'", clrname); 193 | dest->pixel |= 0xff << 24; 194 | } 195 | 196 | /* Wrapper to create color schemes. The caller has to call free(3) on the 197 | * returned color scheme when done using it. */ 198 | Clr * 199 | drw_scm_create(Drw *drw, const char *clrnames[], size_t clrcount) 200 | { 201 | size_t i; 202 | Clr *ret; 203 | 204 | /* need at least two colors for a scheme */ 205 | if (!drw || !clrnames || clrcount < 2 || !(ret = ecalloc(clrcount, sizeof(XftColor)))) 206 | return NULL; 207 | 208 | for (i = 0; i < clrcount; i++) 209 | drw_clr_create(drw, &ret[i], clrnames[i]); 210 | return ret; 211 | } 212 | 213 | void 214 | drw_setfontset(Drw *drw, Fnt *set) 215 | { 216 | if (drw) 217 | drw->fonts = set; 218 | } 219 | 220 | void 221 | drw_setscheme(Drw *drw, Clr *scm) 222 | { 223 | if (drw) 224 | drw->scheme = scm; 225 | } 226 | 227 | void 228 | drw_rect(Drw *drw, int x, int y, unsigned int w, unsigned int h, int filled, int invert) 229 | { 230 | if (!drw || !drw->scheme) 231 | return; 232 | XSetForeground(drw->dpy, drw->gc, invert ? drw->scheme[ColBg].pixel : drw->scheme[ColFg].pixel); 233 | if (filled) 234 | XFillRectangle(drw->dpy, drw->drawable, drw->gc, x, y, w, h); 235 | else 236 | XDrawRectangle(drw->dpy, drw->drawable, drw->gc, x, y, w - 1, h - 1); 237 | } 238 | 239 | int 240 | drw_text(Drw *drw, int x, int y, unsigned int w, unsigned int h, unsigned int lpad, const char *text, int invert) 241 | { 242 | int i, ty, ellipsis_x = 0; 243 | unsigned int tmpw, ew, ellipsis_w = 0, ellipsis_len; 244 | XftDraw *d = NULL; 245 | Fnt *usedfont, *curfont, *nextfont; 246 | int utf8strlen, utf8charlen, render = x || y || w || h; 247 | long utf8codepoint = 0; 248 | const char *utf8str; 249 | FcCharSet *fccharset; 250 | FcPattern *fcpattern; 251 | FcPattern *match; 252 | XftResult result; 253 | int charexists = 0, overflow = 0; 254 | /* keep track of a couple codepoints for which we have no match. */ 255 | enum { nomatches_len = 64 }; 256 | static struct { long codepoint[nomatches_len]; unsigned int idx; } nomatches; 257 | static unsigned int ellipsis_width = 0; 258 | 259 | if (!drw || (render && (!drw->scheme || !w)) || !text || !drw->fonts) 260 | return 0; 261 | 262 | if (!render) { 263 | w = invert ? invert : ~invert; 264 | } else { 265 | XSetForeground(drw->dpy, drw->gc, drw->scheme[invert ? ColFg : ColBg].pixel); 266 | XFillRectangle(drw->dpy, drw->drawable, drw->gc, x, y, w, h); 267 | d = XftDrawCreate(drw->dpy, drw->drawable, 268 | DefaultVisual(drw->dpy, drw->screen), 269 | DefaultColormap(drw->dpy, drw->screen)); 270 | x += lpad; 271 | w -= lpad; 272 | } 273 | 274 | usedfont = drw->fonts; 275 | if (!ellipsis_width && render) 276 | ellipsis_width = drw_fontset_getwidth(drw, "..."); 277 | while (1) { 278 | ew = ellipsis_len = utf8strlen = 0; 279 | utf8str = text; 280 | nextfont = NULL; 281 | while (*text) { 282 | utf8charlen = utf8decode(text, &utf8codepoint, UTF_SIZ); 283 | for (curfont = drw->fonts; curfont; curfont = curfont->next) { 284 | charexists = charexists || XftCharExists(drw->dpy, curfont->xfont, utf8codepoint); 285 | if (charexists) { 286 | drw_font_getexts(curfont, text, utf8charlen, &tmpw, NULL); 287 | if (ew + ellipsis_width <= w) { 288 | /* keep track where the ellipsis still fits */ 289 | ellipsis_x = x + ew; 290 | ellipsis_w = w - ew; 291 | ellipsis_len = utf8strlen; 292 | } 293 | 294 | if (ew + tmpw > w) { 295 | overflow = 1; 296 | /* called from drw_fontset_getwidth_clamp(): 297 | * it wants the width AFTER the overflow 298 | */ 299 | if (!render) 300 | x += tmpw; 301 | else 302 | utf8strlen = ellipsis_len; 303 | } else if (curfont == usedfont) { 304 | utf8strlen += utf8charlen; 305 | text += utf8charlen; 306 | ew += tmpw; 307 | } else { 308 | nextfont = curfont; 309 | } 310 | break; 311 | } 312 | } 313 | 314 | if (overflow || !charexists || nextfont) 315 | break; 316 | else 317 | charexists = 0; 318 | } 319 | 320 | if (utf8strlen) { 321 | if (render) { 322 | ty = y + (h - usedfont->h) / 2 + usedfont->xfont->ascent; 323 | XftDrawStringUtf8(d, &drw->scheme[invert ? ColBg : ColFg], 324 | usedfont->xfont, x, ty, (XftChar8 *)utf8str, utf8strlen); 325 | } 326 | x += ew; 327 | w -= ew; 328 | } 329 | if (render && overflow) 330 | drw_text(drw, ellipsis_x, y, ellipsis_w, h, 0, "...", invert); 331 | 332 | if (!*text || overflow) { 333 | break; 334 | } else if (nextfont) { 335 | charexists = 0; 336 | usedfont = nextfont; 337 | } else { 338 | /* Regardless of whether or not a fallback font is found, the 339 | * character must be drawn. */ 340 | charexists = 1; 341 | 342 | for (i = 0; i < nomatches_len; ++i) { 343 | /* avoid calling XftFontMatch if we know we won't find a match */ 344 | if (utf8codepoint == nomatches.codepoint[i]) 345 | goto no_match; 346 | } 347 | 348 | fccharset = FcCharSetCreate(); 349 | FcCharSetAddChar(fccharset, utf8codepoint); 350 | 351 | if (!drw->fonts->pattern) { 352 | /* Refer to the comment in xfont_create for more information. */ 353 | die("the first font in the cache must be loaded from a font string."); 354 | } 355 | 356 | fcpattern = FcPatternDuplicate(drw->fonts->pattern); 357 | FcPatternAddCharSet(fcpattern, FC_CHARSET, fccharset); 358 | FcPatternAddBool(fcpattern, FC_SCALABLE, FcTrue); 359 | 360 | FcConfigSubstitute(NULL, fcpattern, FcMatchPattern); 361 | FcDefaultSubstitute(fcpattern); 362 | match = XftFontMatch(drw->dpy, drw->screen, fcpattern, &result); 363 | 364 | FcCharSetDestroy(fccharset); 365 | FcPatternDestroy(fcpattern); 366 | 367 | if (match) { 368 | usedfont = xfont_create(drw, NULL, match); 369 | if (usedfont && XftCharExists(drw->dpy, usedfont->xfont, utf8codepoint)) { 370 | for (curfont = drw->fonts; curfont->next; curfont = curfont->next) 371 | ; /* NOP */ 372 | curfont->next = usedfont; 373 | } else { 374 | xfont_free(usedfont); 375 | nomatches.codepoint[++nomatches.idx % nomatches_len] = utf8codepoint; 376 | no_match: 377 | usedfont = drw->fonts; 378 | } 379 | } 380 | } 381 | } 382 | if (d) 383 | XftDrawDestroy(d); 384 | 385 | return x + (render ? w : 0); 386 | } 387 | 388 | void 389 | drw_map(Drw *drw, Window win, int x, int y, unsigned int w, unsigned int h) 390 | { 391 | if (!drw) 392 | return; 393 | 394 | XCopyArea(drw->dpy, drw->drawable, win, drw->gc, x, y, w, h, x, y); 395 | XSync(drw->dpy, False); 396 | } 397 | 398 | unsigned int 399 | drw_fontset_getwidth(Drw *drw, const char *text) 400 | { 401 | if (!drw || !drw->fonts || !text) 402 | return 0; 403 | return drw_text(drw, 0, 0, 0, 0, 0, text, 0); 404 | } 405 | 406 | unsigned int 407 | drw_fontset_getwidth_clamp(Drw *drw, const char *text, unsigned int n) 408 | { 409 | unsigned int tmp = 0; 410 | if (drw && drw->fonts && text && n) 411 | tmp = drw_text(drw, 0, 0, 0, 0, 0, text, n); 412 | return MIN(n, tmp); 413 | } 414 | 415 | void 416 | drw_font_getexts(Fnt *font, const char *text, unsigned int len, unsigned int *w, unsigned int *h) 417 | { 418 | XGlyphInfo ext; 419 | 420 | if (!font || !text) 421 | return; 422 | 423 | XftTextExtentsUtf8(font->dpy, font->xfont, (XftChar8 *)text, len, &ext); 424 | if (w) 425 | *w = ext.xOff; 426 | if (h) 427 | *h = font->h; 428 | } 429 | 430 | Cur * 431 | drw_cur_create(Drw *drw, int shape) 432 | { 433 | Cur *cur; 434 | 435 | if (!drw || !(cur = ecalloc(1, sizeof(Cur)))) 436 | return NULL; 437 | 438 | cur->cursor = XCreateFontCursor(drw->dpy, shape); 439 | 440 | return cur; 441 | } 442 | 443 | void 444 | drw_cur_free(Drw *drw, Cur *cursor) 445 | { 446 | if (!cursor) 447 | return; 448 | 449 | XFreeCursor(drw->dpy, cursor->cursor); 450 | free(cursor); 451 | } 452 | -------------------------------------------------------------------------------- /vanitygaps.c: -------------------------------------------------------------------------------- 1 | /* Key binding functions */ 2 | static void defaultgaps(const Arg *arg); 3 | static void incrgaps(const Arg *arg); 4 | static void incrigaps(const Arg *arg); 5 | static void incrogaps(const Arg *arg); 6 | static void incrohgaps(const Arg *arg); 7 | static void incrovgaps(const Arg *arg); 8 | static void incrihgaps(const Arg *arg); 9 | static void incrivgaps(const Arg *arg); 10 | static void togglegaps(const Arg *arg); 11 | /* Layouts (delete the ones you do not need) */ 12 | static void bstack(Monitor *m); 13 | static void bstackhoriz(Monitor *m); 14 | static void centeredmaster(Monitor *m); 15 | static void centeredfloatingmaster(Monitor *m); 16 | static void deck(Monitor *m); 17 | static void dwindle(Monitor *m); 18 | static void fibonacci(Monitor *m, int s); 19 | static void grid(Monitor *m); 20 | static void nrowgrid(Monitor *m); 21 | static void spiral(Monitor *m); 22 | static void tile(Monitor *m); 23 | /* Internals */ 24 | static void getgaps(Monitor *m, int *oh, int *ov, int *ih, int *iv, unsigned int *nc); 25 | static void getfacts(Monitor *m, int msize, int ssize, float *mf, float *sf, int *mr, int *sr); 26 | static void setgaps(int oh, int ov, int ih, int iv); 27 | 28 | /* Settings */ 29 | #if !PERTAG_PATCH 30 | static int enablegaps = 1; 31 | #endif // PERTAG_PATCH 32 | 33 | void 34 | setgaps(int oh, int ov, int ih, int iv) 35 | { 36 | if (oh < 0) oh = 0; 37 | if (ov < 0) ov = 0; 38 | if (ih < 0) ih = 0; 39 | if (iv < 0) iv = 0; 40 | 41 | selmon->gappoh = oh; 42 | selmon->gappov = ov; 43 | selmon->gappih = ih; 44 | selmon->gappiv = iv; 45 | arrange(selmon); 46 | } 47 | 48 | void 49 | togglegaps(const Arg *arg) 50 | { 51 | #if PERTAG_PATCH 52 | selmon->pertag->enablegaps[selmon->pertag->curtag] = !selmon->pertag->enablegaps[selmon->pertag->curtag]; 53 | #else 54 | enablegaps = !enablegaps; 55 | #endif // PERTAG_PATCH 56 | arrange(NULL); 57 | } 58 | 59 | void 60 | defaultgaps(const Arg *arg) 61 | { 62 | setgaps(gappoh, gappov, gappih, gappiv); 63 | } 64 | 65 | void 66 | incrgaps(const Arg *arg) 67 | { 68 | setgaps( 69 | selmon->gappoh + arg->i, 70 | selmon->gappov + arg->i, 71 | selmon->gappih + arg->i, 72 | selmon->gappiv + arg->i 73 | ); 74 | } 75 | 76 | void 77 | incrigaps(const Arg *arg) 78 | { 79 | setgaps( 80 | selmon->gappoh, 81 | selmon->gappov, 82 | selmon->gappih + arg->i, 83 | selmon->gappiv + arg->i 84 | ); 85 | } 86 | 87 | void 88 | incrogaps(const Arg *arg) 89 | { 90 | setgaps( 91 | selmon->gappoh + arg->i, 92 | selmon->gappov + arg->i, 93 | selmon->gappih, 94 | selmon->gappiv 95 | ); 96 | } 97 | 98 | void 99 | incrohgaps(const Arg *arg) 100 | { 101 | setgaps( 102 | selmon->gappoh + arg->i, 103 | selmon->gappov, 104 | selmon->gappih, 105 | selmon->gappiv 106 | ); 107 | } 108 | 109 | void 110 | incrovgaps(const Arg *arg) 111 | { 112 | setgaps( 113 | selmon->gappoh, 114 | selmon->gappov + arg->i, 115 | selmon->gappih, 116 | selmon->gappiv 117 | ); 118 | } 119 | 120 | void 121 | incrihgaps(const Arg *arg) 122 | { 123 | setgaps( 124 | selmon->gappoh, 125 | selmon->gappov, 126 | selmon->gappih + arg->i, 127 | selmon->gappiv 128 | ); 129 | } 130 | 131 | void 132 | incrivgaps(const Arg *arg) 133 | { 134 | setgaps( 135 | selmon->gappoh, 136 | selmon->gappov, 137 | selmon->gappih, 138 | selmon->gappiv + arg->i 139 | ); 140 | } 141 | 142 | void 143 | getgaps(Monitor *m, int *oh, int *ov, int *ih, int *iv, unsigned int *nc) 144 | { 145 | unsigned int n, oe, ie; 146 | #if PERTAG_PATCH 147 | oe = ie = selmon->pertag->enablegaps[selmon->pertag->curtag]; 148 | #else 149 | oe = ie = enablegaps; 150 | #endif // PERTAG_PATCH 151 | Client *c; 152 | 153 | for (n = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), n++); 154 | if (smartgaps && n == 1) { 155 | oe = 0; // outer gaps disabled when only one client 156 | } 157 | 158 | *oh = m->gappoh*oe; // outer horizontal gap 159 | *ov = m->gappov*oe; // outer vertical gap 160 | *ih = m->gappih*ie; // inner horizontal gap 161 | *iv = m->gappiv*ie; // inner vertical gap 162 | *nc = n; // number of clients 163 | } 164 | 165 | void 166 | getfacts(Monitor *m, int msize, int ssize, float *mf, float *sf, int *mr, int *sr) 167 | { 168 | unsigned int n; 169 | float mfacts = 0, sfacts = 0; 170 | int mtotal = 0, stotal = 0; 171 | Client *c; 172 | 173 | for (n = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), n++) 174 | if (n < m->nmaster) 175 | mfacts += c->cfact; 176 | else 177 | sfacts += c->cfact; 178 | 179 | for (n = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), n++) 180 | if (n < m->nmaster) 181 | mtotal += msize * (c->cfact / mfacts); 182 | else 183 | stotal += ssize * (c->cfact / sfacts); 184 | 185 | *mf = mfacts; // total factor of master area 186 | *sf = sfacts; // total factor of stack area 187 | *mr = msize - mtotal; // the remainder (rest) of pixels after a cfacts master split 188 | *sr = ssize - stotal; // the remainder (rest) of pixels after a cfacts stack split 189 | } 190 | 191 | /*** 192 | * Layouts 193 | */ 194 | 195 | /* 196 | * Bottomstack layout + gaps 197 | * https://dwm.suckless.org/patches/bottomstack/ 198 | */ 199 | static void 200 | bstack(Monitor *m) 201 | { 202 | unsigned int i, n; 203 | int oh, ov, ih, iv; 204 | int mx = 0, my = 0, mh = 0, mw = 0; 205 | int sx = 0, sy = 0, sh = 0, sw = 0; 206 | float mfacts, sfacts; 207 | int mrest, srest; 208 | Client *c; 209 | 210 | getgaps(m, &oh, &ov, &ih, &iv, &n); 211 | if (n == 0) 212 | return; 213 | 214 | sx = mx = m->wx + ov; 215 | sy = my = m->wy + oh; 216 | sh = mh = m->wh - 2*oh; 217 | mw = m->ww - 2*ov - iv * (MIN(n, m->nmaster) - 1); 218 | sw = m->ww - 2*ov - iv * (n - m->nmaster - 1); 219 | 220 | if (m->nmaster && n > m->nmaster) { 221 | sh = (mh - ih) * (1 - m->mfact); 222 | mh = mh - ih - sh; 223 | sx = mx; 224 | sy = my + mh + ih; 225 | } 226 | 227 | getfacts(m, mw, sw, &mfacts, &sfacts, &mrest, &srest); 228 | 229 | for (i = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), i++) { 230 | if (i < m->nmaster) { 231 | resize(c, mx, my, mw * (c->cfact / mfacts) + (i < mrest ? 1 : 0) - (2*c->bw), mh - (2*c->bw), 0); 232 | mx += WIDTH(c) + iv; 233 | } else { 234 | resize(c, sx, sy, sw * (c->cfact / sfacts) + ((i - m->nmaster) < srest ? 1 : 0) - (2*c->bw), sh - (2*c->bw), 0); 235 | sx += WIDTH(c) + iv; 236 | } 237 | } 238 | } 239 | 240 | static void 241 | bstackhoriz(Monitor *m) 242 | { 243 | unsigned int i, n; 244 | int oh, ov, ih, iv; 245 | int mx = 0, my = 0, mh = 0, mw = 0; 246 | int sx = 0, sy = 0, sh = 0, sw = 0; 247 | float mfacts, sfacts; 248 | int mrest, srest; 249 | Client *c; 250 | 251 | getgaps(m, &oh, &ov, &ih, &iv, &n); 252 | if (n == 0) 253 | return; 254 | 255 | sx = mx = m->wx + ov; 256 | sy = my = m->wy + oh; 257 | mh = m->wh - 2*oh; 258 | sh = m->wh - 2*oh - ih * (n - m->nmaster - 1); 259 | mw = m->ww - 2*ov - iv * (MIN(n, m->nmaster) - 1); 260 | sw = m->ww - 2*ov; 261 | 262 | if (m->nmaster && n > m->nmaster) { 263 | sh = (mh - ih) * (1 - m->mfact); 264 | mh = mh - ih - sh; 265 | sy = my + mh + ih; 266 | sh = m->wh - mh - 2*oh - ih * (n - m->nmaster); 267 | } 268 | 269 | getfacts(m, mw, sh, &mfacts, &sfacts, &mrest, &srest); 270 | 271 | for (i = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), i++) { 272 | if (i < m->nmaster) { 273 | resize(c, mx, my, mw * (c->cfact / mfacts) + (i < mrest ? 1 : 0) - (2*c->bw), mh - (2*c->bw), 0); 274 | mx += WIDTH(c) + iv; 275 | } else { 276 | resize(c, sx, sy, sw - (2*c->bw), sh * (c->cfact / sfacts) + ((i - m->nmaster) < srest ? 1 : 0) - (2*c->bw), 0); 277 | sy += HEIGHT(c) + ih; 278 | } 279 | } 280 | } 281 | 282 | /* 283 | * Centred master layout + gaps 284 | * https://dwm.suckless.org/patches/centeredmaster/ 285 | */ 286 | void 287 | centeredmaster(Monitor *m) 288 | { 289 | unsigned int i, n; 290 | int oh, ov, ih, iv; 291 | int mx = 0, my = 0, mh = 0, mw = 0; 292 | int lx = 0, ly = 0, lw = 0, lh = 0; 293 | int rx = 0, ry = 0, rw = 0, rh = 0; 294 | float mfacts = 0, lfacts = 0, rfacts = 0; 295 | int mtotal = 0, ltotal = 0, rtotal = 0; 296 | int mrest = 0, lrest = 0, rrest = 0; 297 | Client *c; 298 | 299 | getgaps(m, &oh, &ov, &ih, &iv, &n); 300 | if (n == 0) 301 | return; 302 | 303 | /* initialize areas */ 304 | mx = m->wx + ov; 305 | my = m->wy + oh; 306 | mh = m->wh - 2*oh - ih * ((!m->nmaster ? n : MIN(n, m->nmaster)) - 1); 307 | mw = m->ww - 2*ov; 308 | lh = m->wh - 2*oh - ih * (((n - m->nmaster) / 2) - 1); 309 | rh = m->wh - 2*oh - ih * (((n - m->nmaster) / 2) - ((n - m->nmaster) % 2 ? 0 : 1)); 310 | 311 | if (m->nmaster && n > m->nmaster) { 312 | /* go mfact box in the center if more than nmaster clients */ 313 | if (n - m->nmaster > 1) { 314 | /* ||<-S->|<---M--->|<-S->|| */ 315 | mw = (m->ww - 2*ov - 2*iv) * m->mfact; 316 | lw = (m->ww - mw - 2*ov - 2*iv) / 2; 317 | rw = (m->ww - mw - 2*ov - 2*iv) - lw; 318 | mx += lw + iv; 319 | } else { 320 | /* ||<---M--->|<-S->|| */ 321 | mw = (mw - iv) * m->mfact; 322 | lw = 0; 323 | rw = m->ww - mw - iv - 2*ov; 324 | } 325 | lx = m->wx + ov; 326 | ly = m->wy + oh; 327 | rx = mx + mw + iv; 328 | ry = m->wy + oh; 329 | } 330 | 331 | /* calculate facts */ 332 | for (n = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), n++) { 333 | if (!m->nmaster || n < m->nmaster) 334 | mfacts += c->cfact; 335 | else if ((n - m->nmaster) % 2) 336 | lfacts += c->cfact; // total factor of left hand stack area 337 | else 338 | rfacts += c->cfact; // total factor of right hand stack area 339 | } 340 | 341 | for (n = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), n++) 342 | if (!m->nmaster || n < m->nmaster) 343 | mtotal += mh * (c->cfact / mfacts); 344 | else if ((n - m->nmaster) % 2) 345 | ltotal += lh * (c->cfact / lfacts); 346 | else 347 | rtotal += rh * (c->cfact / rfacts); 348 | 349 | mrest = mh - mtotal; 350 | lrest = lh - ltotal; 351 | rrest = rh - rtotal; 352 | 353 | for (i = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), i++) { 354 | if (!m->nmaster || i < m->nmaster) { 355 | /* nmaster clients are stacked vertically, in the center of the screen */ 356 | resize(c, mx, my, mw - (2*c->bw), mh * (c->cfact / mfacts) + (i < mrest ? 1 : 0) - (2*c->bw), 0); 357 | my += HEIGHT(c) + ih; 358 | } else { 359 | /* stack clients are stacked vertically */ 360 | if ((i - m->nmaster) % 2 ) { 361 | resize(c, lx, ly, lw - (2*c->bw), lh * (c->cfact / lfacts) + ((i - 2*m->nmaster) < 2*lrest ? 1 : 0) - (2*c->bw), 0); 362 | ly += HEIGHT(c) + ih; 363 | } else { 364 | resize(c, rx, ry, rw - (2*c->bw), rh * (c->cfact / rfacts) + ((i - 2*m->nmaster) < 2*rrest ? 1 : 0) - (2*c->bw), 0); 365 | ry += HEIGHT(c) + ih; 366 | } 367 | } 368 | } 369 | } 370 | 371 | void 372 | centeredfloatingmaster(Monitor *m) 373 | { 374 | unsigned int i, n; 375 | float mfacts, sfacts; 376 | float mivf = 1.0; // master inner vertical gap factor 377 | int oh, ov, ih, iv, mrest, srest; 378 | int mx = 0, my = 0, mh = 0, mw = 0; 379 | int sx = 0, sy = 0, sh = 0, sw = 0; 380 | Client *c; 381 | 382 | getgaps(m, &oh, &ov, &ih, &iv, &n); 383 | if (n == 0) 384 | return; 385 | 386 | sx = mx = m->wx + ov; 387 | sy = my = m->wy + oh; 388 | sh = mh = m->wh - 2*oh; 389 | mw = m->ww - 2*ov - iv*(n - 1); 390 | sw = m->ww - 2*ov - iv*(n - m->nmaster - 1); 391 | 392 | if (m->nmaster && n > m->nmaster) { 393 | mivf = 0.8; 394 | /* go mfact box in the center if more than nmaster clients */ 395 | if (m->ww > m->wh) { 396 | mw = m->ww * m->mfact - iv*mivf*(MIN(n, m->nmaster) - 1); 397 | mh = m->wh * 0.9; 398 | } else { 399 | mw = m->ww * 0.9 - iv*mivf*(MIN(n, m->nmaster) - 1); 400 | mh = m->wh * m->mfact; 401 | } 402 | mx = m->wx + (m->ww - mw) / 2; 403 | my = m->wy + (m->wh - mh - 2*oh) / 2; 404 | 405 | sx = m->wx + ov; 406 | sy = m->wy + oh; 407 | sh = m->wh - 2*oh; 408 | } 409 | 410 | getfacts(m, mw, sw, &mfacts, &sfacts, &mrest, &srest); 411 | 412 | for (i = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), i++) 413 | if (i < m->nmaster) { 414 | /* nmaster clients are stacked horizontally, in the center of the screen */ 415 | resize(c, mx, my, mw * (c->cfact / mfacts) + (i < mrest ? 1 : 0) - (2*c->bw), mh - (2*c->bw), 0); 416 | mx += WIDTH(c) + iv*mivf; 417 | } else { 418 | /* stack clients are stacked horizontally */ 419 | resize(c, sx, sy, sw * (c->cfact / sfacts) + ((i - m->nmaster) < srest ? 1 : 0) - (2*c->bw), sh - (2*c->bw), 0); 420 | sx += WIDTH(c) + iv; 421 | } 422 | } 423 | 424 | /* 425 | * Deck layout + gaps 426 | * https://dwm.suckless.org/patches/deck/ 427 | */ 428 | void 429 | deck(Monitor *m) 430 | { 431 | unsigned int i, n; 432 | int oh, ov, ih, iv; 433 | int mx = 0, my = 0, mh = 0, mw = 0; 434 | int sx = 0, sy = 0, sh = 0, sw = 0; 435 | float mfacts, sfacts; 436 | int mrest, srest; 437 | Client *c; 438 | 439 | getgaps(m, &oh, &ov, &ih, &iv, &n); 440 | if (n == 0) 441 | return; 442 | 443 | sx = mx = m->wx + ov; 444 | sy = my = m->wy + oh; 445 | sh = mh = m->wh - 2*oh - ih * (MIN(n, m->nmaster) - 1); 446 | sw = mw = m->ww - 2*ov; 447 | 448 | if (m->nmaster && n > m->nmaster) { 449 | sw = (mw - iv) * (1 - m->mfact); 450 | mw = mw - iv - sw; 451 | sx = mx + mw + iv; 452 | sh = m->wh - 2*oh; 453 | } 454 | 455 | getfacts(m, mh, sh, &mfacts, &sfacts, &mrest, &srest); 456 | 457 | if (n - m->nmaster > 0) /* override layout symbol */ 458 | snprintf(m->ltsymbol, sizeof m->ltsymbol, "D %d", n - m->nmaster); 459 | 460 | for (i = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), i++) 461 | if (i < m->nmaster) { 462 | resize(c, mx, my, mw - (2*c->bw), mh * (c->cfact / mfacts) + (i < mrest ? 1 : 0) - (2*c->bw), 0); 463 | my += HEIGHT(c) + ih; 464 | } else { 465 | resize(c, sx, sy, sw - (2*c->bw), sh - (2*c->bw), 0); 466 | } 467 | } 468 | 469 | /* 470 | * Fibonacci layout + gaps 471 | * https://dwm.suckless.org/patches/fibonacci/ 472 | */ 473 | void 474 | fibonacci(Monitor *m, int s) 475 | { 476 | unsigned int i, n; 477 | int nx, ny, nw, nh; 478 | int oh, ov, ih, iv; 479 | int nv, hrest = 0, wrest = 0, r = 1; 480 | Client *c; 481 | 482 | getgaps(m, &oh, &ov, &ih, &iv, &n); 483 | if (n == 0) 484 | return; 485 | 486 | nx = m->wx + ov; 487 | ny = m->wy + oh; 488 | nw = m->ww - 2*ov; 489 | nh = m->wh - 2*oh; 490 | 491 | for (i = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next)) { 492 | if (r) { 493 | if ((i % 2 && (nh - ih) / 2 <= (bh + 2*c->bw)) 494 | || (!(i % 2) && (nw - iv) / 2 <= (bh + 2*c->bw))) { 495 | r = 0; 496 | } 497 | if (r && i < n - 1) { 498 | if (i % 2) { 499 | nv = (nh - ih) / 2; 500 | hrest = nh - 2*nv - ih; 501 | nh = nv; 502 | } else { 503 | nv = (nw - iv) / 2; 504 | wrest = nw - 2*nv - iv; 505 | nw = nv; 506 | } 507 | 508 | if ((i % 4) == 2 && !s) 509 | nx += nw + iv; 510 | else if ((i % 4) == 3 && !s) 511 | ny += nh + ih; 512 | } 513 | 514 | if ((i % 4) == 0) { 515 | if (s) { 516 | ny += nh + ih; 517 | nh += hrest; 518 | } 519 | else { 520 | nh -= hrest; 521 | ny -= nh + ih; 522 | } 523 | } 524 | else if ((i % 4) == 1) { 525 | nx += nw + iv; 526 | nw += wrest; 527 | } 528 | else if ((i % 4) == 2) { 529 | ny += nh + ih; 530 | nh += hrest; 531 | if (i < n - 1) 532 | nw += wrest; 533 | } 534 | else if ((i % 4) == 3) { 535 | if (s) { 536 | nx += nw + iv; 537 | nw -= wrest; 538 | } else { 539 | nw -= wrest; 540 | nx -= nw + iv; 541 | nh += hrest; 542 | } 543 | } 544 | if (i == 0) { 545 | if (n != 1) { 546 | nw = (m->ww - iv - 2*ov) - (m->ww - iv - 2*ov) * (1 - m->mfact); 547 | wrest = 0; 548 | } 549 | ny = m->wy + oh; 550 | } 551 | else if (i == 1) 552 | nw = m->ww - nw - iv - 2*ov; 553 | i++; 554 | } 555 | 556 | resize(c, nx, ny, nw - (2*c->bw), nh - (2*c->bw), False); 557 | } 558 | } 559 | 560 | void 561 | dwindle(Monitor *m) 562 | { 563 | fibonacci(m, 1); 564 | } 565 | 566 | void 567 | spiral(Monitor *m) 568 | { 569 | fibonacci(m, 0); 570 | } 571 | 572 | /* 573 | * Gappless grid layout + gaps (ironically) 574 | * https://dwm.suckless.org/patches/gaplessgrid/ 575 | */ 576 | void 577 | gaplessgrid(Monitor *m) 578 | { 579 | unsigned int i, n; 580 | int x, y, cols, rows, ch, cw, cn, rn, rrest, crest; // counters 581 | int oh, ov, ih, iv; 582 | Client *c; 583 | 584 | getgaps(m, &oh, &ov, &ih, &iv, &n); 585 | if (n == 0) 586 | return; 587 | 588 | /* grid dimensions */ 589 | for (cols = 0; cols <= n/2; cols++) 590 | if (cols*cols >= n) 591 | break; 592 | if (n == 5) /* set layout against the general calculation: not 1:2:2, but 2:3 */ 593 | cols = 2; 594 | rows = n/cols; 595 | cn = rn = 0; // reset column no, row no, client count 596 | 597 | ch = (m->wh - 2*oh - ih * (rows - 1)) / rows; 598 | cw = (m->ww - 2*ov - iv * (cols - 1)) / cols; 599 | rrest = (m->wh - 2*oh - ih * (rows - 1)) - ch * rows; 600 | crest = (m->ww - 2*ov - iv * (cols - 1)) - cw * cols; 601 | x = m->wx + ov; 602 | y = m->wy + oh; 603 | 604 | for (i = 0, c = nexttiled(m->clients); c; i++, c = nexttiled(c->next)) { 605 | if (i/rows + 1 > cols - n%cols) { 606 | rows = n/cols + 1; 607 | ch = (m->wh - 2*oh - ih * (rows - 1)) / rows; 608 | rrest = (m->wh - 2*oh - ih * (rows - 1)) - ch * rows; 609 | } 610 | resize(c, 611 | x, 612 | y + rn*(ch + ih) + MIN(rn, rrest), 613 | cw + (cn < crest ? 1 : 0) - 2*c->bw, 614 | ch + (rn < rrest ? 1 : 0) - 2*c->bw, 615 | 0); 616 | rn++; 617 | if (rn >= rows) { 618 | rn = 0; 619 | x += cw + ih + (cn < crest ? 1 : 0); 620 | cn++; 621 | } 622 | } 623 | } 624 | 625 | /* 626 | * Gridmode layout + gaps 627 | * https://dwm.suckless.org/patches/gridmode/ 628 | */ 629 | void 630 | grid(Monitor *m) 631 | { 632 | unsigned int i, n; 633 | int cx, cy, cw, ch, cc, cr, chrest, cwrest, cols, rows; 634 | int oh, ov, ih, iv; 635 | Client *c; 636 | 637 | getgaps(m, &oh, &ov, &ih, &iv, &n); 638 | 639 | /* grid dimensions */ 640 | for (rows = 0; rows <= n/2; rows++) 641 | if (rows*rows >= n) 642 | break; 643 | cols = (rows && (rows - 1) * rows >= n) ? rows - 1 : rows; 644 | 645 | /* window geoms (cell height/width) */ 646 | ch = (m->wh - 2*oh - ih * (rows - 1)) / (rows ? rows : 1); 647 | cw = (m->ww - 2*ov - iv * (cols - 1)) / (cols ? cols : 1); 648 | chrest = (m->wh - 2*oh - ih * (rows - 1)) - ch * rows; 649 | cwrest = (m->ww - 2*ov - iv * (cols - 1)) - cw * cols; 650 | for (i = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), i++) { 651 | cc = i / rows; 652 | cr = i % rows; 653 | cx = m->wx + ov + cc * (cw + iv) + MIN(cc, cwrest); 654 | cy = m->wy + oh + cr * (ch + ih) + MIN(cr, chrest); 655 | resize(c, cx, cy, cw + (cc < cwrest ? 1 : 0) - 2*c->bw, ch + (cr < chrest ? 1 : 0) - 2*c->bw, False); 656 | } 657 | } 658 | 659 | /* 660 | * Horizontal grid layout + gaps 661 | * https://dwm.suckless.org/patches/horizgrid/ 662 | */ 663 | void 664 | horizgrid(Monitor *m) { 665 | Client *c; 666 | unsigned int n, i; 667 | int oh, ov, ih, iv; 668 | int mx = 0, my = 0, mh = 0, mw = 0; 669 | int sx = 0, sy = 0, sh = 0, sw = 0; 670 | int ntop, nbottom = 1; 671 | float mfacts = 0, sfacts = 0; 672 | int mrest, srest, mtotal = 0, stotal = 0; 673 | 674 | /* Count windows */ 675 | getgaps(m, &oh, &ov, &ih, &iv, &n); 676 | if (n == 0) 677 | return; 678 | 679 | if (n <= 2) 680 | ntop = n; 681 | else { 682 | ntop = n / 2; 683 | nbottom = n - ntop; 684 | } 685 | sx = mx = m->wx + ov; 686 | sy = my = m->wy + oh; 687 | sh = mh = m->wh - 2*oh; 688 | sw = mw = m->ww - 2*ov; 689 | 690 | if (n > ntop) { 691 | sh = (mh - ih) / 2; 692 | mh = mh - ih - sh; 693 | sy = my + mh + ih; 694 | mw = m->ww - 2*ov - iv * (ntop - 1); 695 | sw = m->ww - 2*ov - iv * (nbottom - 1); 696 | } 697 | 698 | /* calculate facts */ 699 | for (i = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), i++) 700 | if (i < ntop) 701 | mfacts += c->cfact; 702 | else 703 | sfacts += c->cfact; 704 | 705 | for (i = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), i++) 706 | if (i < ntop) 707 | mtotal += mh * (c->cfact / mfacts); 708 | else 709 | stotal += sw * (c->cfact / sfacts); 710 | 711 | mrest = mh - mtotal; 712 | srest = sw - stotal; 713 | 714 | for (i = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), i++) 715 | if (i < ntop) { 716 | resize(c, mx, my, mw * (c->cfact / mfacts) + (i < mrest ? 1 : 0) - (2*c->bw), mh - (2*c->bw), 0); 717 | mx += WIDTH(c) + iv; 718 | } else { 719 | resize(c, sx, sy, sw * (c->cfact / sfacts) + ((i - ntop) < srest ? 1 : 0) - (2*c->bw), sh - (2*c->bw), 0); 720 | sx += WIDTH(c) + iv; 721 | } 722 | } 723 | 724 | /* 725 | * nrowgrid layout + gaps 726 | * https://dwm.suckless.org/patches/nrowgrid/ 727 | */ 728 | void 729 | nrowgrid(Monitor *m) 730 | { 731 | unsigned int n; 732 | int ri = 0, ci = 0; /* counters */ 733 | int oh, ov, ih, iv; /* vanitygap settings */ 734 | unsigned int cx, cy, cw, ch; /* client geometry */ 735 | unsigned int uw = 0, uh = 0, uc = 0; /* utilization trackers */ 736 | unsigned int cols, rows = m->nmaster + 1; 737 | Client *c; 738 | 739 | /* count clients */ 740 | getgaps(m, &oh, &ov, &ih, &iv, &n); 741 | 742 | /* nothing to do here */ 743 | if (n == 0) 744 | return; 745 | 746 | /* force 2 clients to always split vertically */ 747 | if (FORCE_VSPLIT && n == 2) 748 | rows = 1; 749 | 750 | /* never allow empty rows */ 751 | if (n < rows) 752 | rows = n; 753 | 754 | /* define first row */ 755 | cols = n / rows; 756 | uc = cols; 757 | cy = m->wy + oh; 758 | ch = (m->wh - 2*oh - ih*(rows - 1)) / rows; 759 | uh = ch; 760 | 761 | for (c = nexttiled(m->clients); c; c = nexttiled(c->next), ci++) { 762 | if (ci == cols) { 763 | uw = 0; 764 | ci = 0; 765 | ri++; 766 | 767 | /* next row */ 768 | cols = (n - uc) / (rows - ri); 769 | uc += cols; 770 | cy = m->wy + oh + uh + ih; 771 | uh += ch + ih; 772 | } 773 | 774 | cx = m->wx + ov + uw; 775 | cw = (m->ww - 2*ov - uw) / (cols - ci); 776 | uw += cw + iv; 777 | 778 | resize(c, cx, cy, cw - (2*c->bw), ch - (2*c->bw), 0); 779 | } 780 | } 781 | 782 | /* 783 | * Default tile layout + gaps 784 | */ 785 | static void 786 | tile(Monitor *m) 787 | { 788 | unsigned int i, n; 789 | int oh, ov, ih, iv; 790 | int mx = 0, my = 0, mh = 0, mw = 0; 791 | int sx = 0, sy = 0, sh = 0, sw = 0; 792 | float mfacts, sfacts; 793 | int mrest, srest; 794 | Client *c; 795 | 796 | getgaps(m, &oh, &ov, &ih, &iv, &n); 797 | if (n == 0) 798 | return; 799 | 800 | sx = mx = m->wx + ov; 801 | sy = my = m->wy + oh; 802 | mh = m->wh - 2*oh - ih * (MIN(n, m->nmaster) - 1); 803 | sh = m->wh - 2*oh - ih * (n - m->nmaster - 1); 804 | sw = mw = m->ww - 2*ov; 805 | 806 | if (m->nmaster && n > m->nmaster) { 807 | sw = (mw - iv) * (1 - m->mfact); 808 | mw = mw - iv - sw; 809 | sx = mx + mw + iv; 810 | } 811 | 812 | getfacts(m, mh, sh, &mfacts, &sfacts, &mrest, &srest); 813 | 814 | for (i = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), i++) 815 | if (i < m->nmaster) { 816 | resize(c, mx, my, mw - (2*c->bw), mh * (c->cfact / mfacts) + (i < mrest ? 1 : 0) - (2*c->bw), 0); 817 | my += HEIGHT(c) + ih; 818 | } else { 819 | resize(c, sx, sy, sw - (2*c->bw), sh * (c->cfact / sfacts) + ((i - m->nmaster) < srest ? 1 : 0) - (2*c->bw), 0); 820 | sy += HEIGHT(c) + ih; 821 | } 822 | } -------------------------------------------------------------------------------- /dwm.c: -------------------------------------------------------------------------------- 1 | /* See LICENSE file for copyright and license details. 2 | * 3 | * dynamic window manager is designed like any other X client as well. It is 4 | * driven through handling X events. In contrast to other X clients, a window 5 | * manager selects for SubstructureRedirectMask on the root window, to receive 6 | * events about window (dis-)appearance. Only one X connection at a time is 7 | * allowed to select for this event mask. 8 | * 9 | * The event handlers of dwm are organized in an array which is accessed 10 | * whenever a new event has been fetched. This allows event dispatching 11 | * in O(1) time. 12 | * 13 | * Each child of the root window is called a client, except windows which have 14 | * set the override_redirect flag. Clients are organized in a linked client 15 | * list on each monitor, the focus history is remembered through a stack list 16 | * on each monitor. Each client contains a bit array to indicate the tags of a 17 | * client. 18 | * 19 | * Keys and tagging rules are organized as arrays and defined in config.h. 20 | * 21 | * To understand everything else, start reading main(). 22 | */ 23 | #include 24 | #include 25 | #include 26 | #include 27 | #include 28 | #include 29 | #include 30 | #include 31 | #include 32 | #include 33 | #include 34 | #include 35 | #include 36 | #include 37 | #include 38 | #include 39 | #ifdef XINERAMA 40 | #include 41 | #endif /* XINERAMA */ 42 | #include 43 | 44 | #include "drw.h" 45 | #include "util.h" 46 | 47 | /* macros */ 48 | #define BUTTONMASK (ButtonPressMask|ButtonReleaseMask) 49 | #define CLEANMASK(mask) (mask & ~(numlockmask|LockMask) & (ShiftMask|ControlMask|Mod1Mask|Mod2Mask|Mod3Mask|Mod4Mask|Mod5Mask)) 50 | #define INTERSECT(x,y,w,h,m) (MAX(0, MIN((x)+(w),(m)->wx+(m)->ww) - MAX((x),(m)->wx)) \ 51 | * MAX(0, MIN((y)+(h),(m)->wy+(m)->wh) - MAX((y),(m)->wy))) 52 | #define ISVISIBLE(C) ((C->tags & C->mon->tagset[C->mon->seltags])) 53 | #define LENGTH(X) (sizeof X / sizeof X[0]) 54 | #define MOUSEMASK (BUTTONMASK|PointerMotionMask) 55 | #define WIDTH(X) ((X)->w + 2 * (X)->bw) 56 | #define HEIGHT(X) ((X)->h + 2 * (X)->bw) 57 | #define TAGMASK ((1 << LENGTH(tags)) - 1) 58 | #define TEXTW(X) (drw_fontset_getwidth(drw, (X)) + lrpad) 59 | 60 | /* enums */ 61 | enum { CurNormal, CurResize, CurMove, CurLast }; /* cursor */ 62 | enum { SchemeNorm, SchemeSel }; /* color schemes */ 63 | enum { NetSupported, NetWMName, NetWMState, NetWMCheck, 64 | NetWMFullscreen, NetActiveWindow, NetWMWindowType, 65 | NetWMWindowTypeDialog, NetClientList, NetLast }; /* EWMH atoms */ 66 | enum { WMProtocols, WMDelete, WMState, WMTakeFocus, WMLast }; /* default atoms */ 67 | enum { ClkTagBar, ClkLtSymbol, ClkStatusText, ClkWinTitle, 68 | ClkClientWin, ClkRootWin, ClkLast }; /* clicks */ 69 | 70 | typedef union { 71 | int i; 72 | unsigned int ui; 73 | float f; 74 | const void *v; 75 | } Arg; 76 | 77 | typedef struct { 78 | unsigned int click; 79 | unsigned int mask; 80 | unsigned int button; 81 | void (*func)(const Arg *arg); 82 | const Arg arg; 83 | } Button; 84 | 85 | typedef struct Monitor Monitor; 86 | typedef struct Client Client; 87 | struct Client { 88 | char name[256]; 89 | float mina, maxa; 90 | float cfact; 91 | int x, y, w, h; 92 | int oldx, oldy, oldw, oldh; 93 | int basew, baseh, incw, inch, maxw, maxh, minw, minh, hintsvalid; 94 | int bw, oldbw; 95 | unsigned int tags; 96 | int isfixed, isfloating, isurgent, neverfocus, oldstate, isfullscreen; 97 | Client *next; 98 | Client *snext; 99 | Monitor *mon; 100 | Window win; 101 | }; 102 | 103 | typedef struct { 104 | unsigned int mod; 105 | KeySym keysym; 106 | void (*func)(const Arg *); 107 | const Arg arg; 108 | } Key; 109 | 110 | typedef struct { 111 | const char *symbol; 112 | void (*arrange)(Monitor *); 113 | } Layout; 114 | 115 | struct Monitor { 116 | char ltsymbol[16]; 117 | float mfact; 118 | int nmaster; 119 | int num; 120 | int by; /* bar geometry */ 121 | int mx, my, mw, mh; /* screen size */ 122 | int wx, wy, ww, wh; /* window area */ 123 | int gappih; /* horizontal gap between windows */ 124 | int gappiv; /* vertical gap between windows */ 125 | int gappoh; /* horizontal outer gaps */ 126 | int gappov; /* vertical outer gaps */ 127 | unsigned int seltags; 128 | unsigned int sellt; 129 | unsigned int tagset[2]; 130 | int showbar; 131 | int topbar; 132 | Client *clients; 133 | Client *sel; 134 | Client *stack; 135 | Monitor *next; 136 | Window barwin; 137 | const Layout *lt[2]; 138 | }; 139 | 140 | typedef struct { 141 | const char *class; 142 | const char *instance; 143 | const char *title; 144 | unsigned int tags; 145 | int isfloating; 146 | int monitor; 147 | } Rule; 148 | 149 | /* function declarations */ 150 | static void applyrules(Client *c); 151 | static int applysizehints(Client *c, int *x, int *y, int *w, int *h, int interact); 152 | static void arrange(Monitor *m); 153 | static void arrangemon(Monitor *m); 154 | static void attach(Client *c); 155 | static void attachstack(Client *c); 156 | static void buttonpress(XEvent *e); 157 | static void checkotherwm(void); 158 | static void cleanup(void); 159 | static void cleanupmon(Monitor *mon); 160 | static void clientmessage(XEvent *e); 161 | static void configure(Client *c); 162 | static void configurenotify(XEvent *e); 163 | static void configurerequest(XEvent *e); 164 | static Monitor *createmon(void); 165 | static void destroynotify(XEvent *e); 166 | static void detach(Client *c); 167 | static void detachstack(Client *c); 168 | static Monitor *dirtomon(int dir); 169 | static void drawbar(Monitor *m); 170 | static void drawbars(void); 171 | static int drawstatusbar(Monitor *m, int bh, char* text); 172 | static void enternotify(XEvent *e); 173 | static void expose(XEvent *e); 174 | static void focus(Client *c); 175 | static void focusin(XEvent *e); 176 | static void focusmon(const Arg *arg); 177 | static void focusstack(const Arg *arg); 178 | static Atom getatomprop(Client *c, Atom prop); 179 | static int getrootptr(int *x, int *y); 180 | static long getstate(Window w); 181 | static int gettextprop(Window w, Atom atom, char *text, unsigned int size); 182 | static void grabbuttons(Client *c, int focused); 183 | static void grabkeys(void); 184 | static void incnmaster(const Arg *arg); 185 | static void keypress(XEvent *e); 186 | static void killclient(const Arg *arg); 187 | static void manage(Window w, XWindowAttributes *wa); 188 | static void mappingnotify(XEvent *e); 189 | static void maprequest(XEvent *e); 190 | static void monocle(Monitor *m); 191 | static void motionnotify(XEvent *e); 192 | static void movemouse(const Arg *arg); 193 | static Client *nexttiled(Client *c); 194 | static void pop(Client *c); 195 | static void propertynotify(XEvent *e); 196 | static void quit(const Arg *arg); 197 | static Monitor *recttomon(int x, int y, int w, int h); 198 | static void resize(Client *c, int x, int y, int w, int h, int interact); 199 | static void resizeclient(Client *c, int x, int y, int w, int h); 200 | static void resizemouse(const Arg *arg); 201 | static void restack(Monitor *m); 202 | static void run(void); 203 | static void scan(void); 204 | static int sendevent(Client *c, Atom proto); 205 | static void sendmon(Client *c, Monitor *m); 206 | static void setclientstate(Client *c, long state); 207 | static void setfocus(Client *c); 208 | static void setfullscreen(Client *c, int fullscreen); 209 | static void setlayout(const Arg *arg); 210 | static void setcfact(const Arg *arg); 211 | static void setmfact(const Arg *arg); 212 | static void setup(void); 213 | static void seturgent(Client *c, int urg); 214 | static void showhide(Client *c); 215 | static void sigchld(int unused); 216 | static void spawn(const Arg *arg); 217 | static void tag(const Arg *arg); 218 | static void tagmon(const Arg *arg); 219 | static void togglebar(const Arg *arg); 220 | static void togglefloating(const Arg *arg); 221 | static void togglefullscr(const Arg *arg); 222 | static void toggletag(const Arg *arg); 223 | static void toggleview(const Arg *arg); 224 | static void unfocus(Client *c, int setfocus); 225 | static void unmanage(Client *c, int destroyed); 226 | static void unmapnotify(XEvent *e); 227 | static void updatebarpos(Monitor *m); 228 | static void updatebars(void); 229 | static void updateclientlist(void); 230 | static int updategeom(void); 231 | static void updatenumlockmask(void); 232 | static void updatesizehints(Client *c); 233 | static void updatestatus(void); 234 | static void updatetitle(Client *c); 235 | static void updatewindowtype(Client *c); 236 | static void updatewmhints(Client *c); 237 | static void view(const Arg *arg); 238 | static Client *wintoclient(Window w); 239 | static Monitor *wintomon(Window w); 240 | static int xerror(Display *dpy, XErrorEvent *ee); 241 | static int xerrordummy(Display *dpy, XErrorEvent *ee); 242 | static int xerrorstart(Display *dpy, XErrorEvent *ee); 243 | static void zoom(const Arg *arg); 244 | 245 | /* variables */ 246 | static const char broken[] = "broken"; 247 | static char stext[1024]; 248 | static int screen; 249 | static int sw, sh; /* X display screen geometry width, height */ 250 | static int bh; /* bar height */ 251 | static int lrpad; /* sum of left and right padding for text */ 252 | static int (*xerrorxlib)(Display *, XErrorEvent *); 253 | static unsigned int numlockmask = 0; 254 | static void (*handler[LASTEvent]) (XEvent *) = { 255 | [ButtonPress] = buttonpress, 256 | [ClientMessage] = clientmessage, 257 | [ConfigureRequest] = configurerequest, 258 | [ConfigureNotify] = configurenotify, 259 | [DestroyNotify] = destroynotify, 260 | [EnterNotify] = enternotify, 261 | [Expose] = expose, 262 | [FocusIn] = focusin, 263 | [KeyPress] = keypress, 264 | [MappingNotify] = mappingnotify, 265 | [MapRequest] = maprequest, 266 | [MotionNotify] = motionnotify, 267 | [PropertyNotify] = propertynotify, 268 | [UnmapNotify] = unmapnotify 269 | }; 270 | static Atom wmatom[WMLast], netatom[NetLast]; 271 | static int running = 1; 272 | static Cur *cursor[CurLast]; 273 | static Clr **scheme; 274 | static Clr **tagscheme; 275 | static Display *dpy; 276 | static Drw *drw; 277 | static Monitor *mons, *selmon; 278 | static Window root, wmcheckwin; 279 | 280 | /* configuration, allows nested code to access above variables */ 281 | #include "config.h" 282 | 283 | /* compile-time check if all tags fit into an unsigned int bit array. */ 284 | struct NumTags { char limitexceeded[LENGTH(tags) > 31 ? -1 : 1]; }; 285 | 286 | /* function implementations */ 287 | void 288 | applyrules(Client *c) 289 | { 290 | const char *class, *instance; 291 | unsigned int i; 292 | const Rule *r; 293 | Monitor *m; 294 | XClassHint ch = { NULL, NULL }; 295 | 296 | /* rule matching */ 297 | c->isfloating = 0; 298 | c->tags = 0; 299 | XGetClassHint(dpy, c->win, &ch); 300 | class = ch.res_class ? ch.res_class : broken; 301 | instance = ch.res_name ? ch.res_name : broken; 302 | 303 | for (i = 0; i < LENGTH(rules); i++) { 304 | r = &rules[i]; 305 | if ((!r->title || strstr(c->name, r->title)) 306 | && (!r->class || strstr(class, r->class)) 307 | && (!r->instance || strstr(instance, r->instance))) 308 | { 309 | c->isfloating = r->isfloating; 310 | c->tags |= r->tags; 311 | for (m = mons; m && m->num != r->monitor; m = m->next); 312 | if (m) 313 | c->mon = m; 314 | } 315 | } 316 | if (ch.res_class) 317 | XFree(ch.res_class); 318 | if (ch.res_name) 319 | XFree(ch.res_name); 320 | c->tags = c->tags & TAGMASK ? c->tags & TAGMASK : c->mon->tagset[c->mon->seltags]; 321 | } 322 | 323 | int 324 | applysizehints(Client *c, int *x, int *y, int *w, int *h, int interact) 325 | { 326 | int baseismin; 327 | Monitor *m = c->mon; 328 | 329 | /* set minimum possible */ 330 | *w = MAX(1, *w); 331 | *h = MAX(1, *h); 332 | if (interact) { 333 | if (*x > sw) 334 | *x = sw - WIDTH(c); 335 | if (*y > sh) 336 | *y = sh - HEIGHT(c); 337 | if (*x + *w + 2 * c->bw < 0) 338 | *x = 0; 339 | if (*y + *h + 2 * c->bw < 0) 340 | *y = 0; 341 | } else { 342 | if (*x >= m->wx + m->ww) 343 | *x = m->wx + m->ww - WIDTH(c); 344 | if (*y >= m->wy + m->wh) 345 | *y = m->wy + m->wh - HEIGHT(c); 346 | if (*x + *w + 2 * c->bw <= m->wx) 347 | *x = m->wx; 348 | if (*y + *h + 2 * c->bw <= m->wy) 349 | *y = m->wy; 350 | } 351 | if (*h < bh) 352 | *h = bh; 353 | if (*w < bh) 354 | *w = bh; 355 | if (resizehints || c->isfloating || !c->mon->lt[c->mon->sellt]->arrange) { 356 | if (!c->hintsvalid) 357 | updatesizehints(c); 358 | /* see last two sentences in ICCCM 4.1.2.3 */ 359 | baseismin = c->basew == c->minw && c->baseh == c->minh; 360 | if (!baseismin) { /* temporarily remove base dimensions */ 361 | *w -= c->basew; 362 | *h -= c->baseh; 363 | } 364 | /* adjust for aspect limits */ 365 | if (c->mina > 0 && c->maxa > 0) { 366 | if (c->maxa < (float)*w / *h) 367 | *w = *h * c->maxa + 0.5; 368 | else if (c->mina < (float)*h / *w) 369 | *h = *w * c->mina + 0.5; 370 | } 371 | if (baseismin) { /* increment calculation requires this */ 372 | *w -= c->basew; 373 | *h -= c->baseh; 374 | } 375 | /* adjust for increment value */ 376 | if (c->incw) 377 | *w -= *w % c->incw; 378 | if (c->inch) 379 | *h -= *h % c->inch; 380 | /* restore base dimensions */ 381 | *w = MAX(*w + c->basew, c->minw); 382 | *h = MAX(*h + c->baseh, c->minh); 383 | if (c->maxw) 384 | *w = MIN(*w, c->maxw); 385 | if (c->maxh) 386 | *h = MIN(*h, c->maxh); 387 | } 388 | return *x != c->x || *y != c->y || *w != c->w || *h != c->h; 389 | } 390 | 391 | void 392 | arrange(Monitor *m) 393 | { 394 | if (m) 395 | showhide(m->stack); 396 | else for (m = mons; m; m = m->next) 397 | showhide(m->stack); 398 | if (m) { 399 | arrangemon(m); 400 | restack(m); 401 | } else for (m = mons; m; m = m->next) 402 | arrangemon(m); 403 | } 404 | 405 | void 406 | arrangemon(Monitor *m) 407 | { 408 | strncpy(m->ltsymbol, m->lt[m->sellt]->symbol, sizeof m->ltsymbol); 409 | if (m->lt[m->sellt]->arrange) 410 | m->lt[m->sellt]->arrange(m); 411 | } 412 | 413 | void 414 | attach(Client *c) 415 | { 416 | c->next = c->mon->clients; 417 | c->mon->clients = c; 418 | } 419 | 420 | void 421 | attachstack(Client *c) 422 | { 423 | c->snext = c->mon->stack; 424 | c->mon->stack = c; 425 | } 426 | 427 | void 428 | buttonpress(XEvent *e) 429 | { 430 | unsigned int i, x, click, occ; 431 | Arg arg = {0}; 432 | Client *c; 433 | Monitor *m; 434 | XButtonPressedEvent *ev = &e->xbutton; 435 | 436 | click = ClkRootWin; 437 | /* focus monitor if necessary */ 438 | if ((m = wintomon(ev->window)) && m != selmon) { 439 | unfocus(selmon->sel, 1); 440 | selmon = m; 441 | focus(NULL); 442 | } 443 | if (ev->window == selmon->barwin) { 444 | i = x = occ = 0; 445 | /* Bitmask of occupied tags */ 446 | for (c = m->clients; c; c = c->next) 447 | occ |= c->tags; 448 | 449 | do 450 | x += TEXTW(occ & 1 << i ? alttags[i] : tags[i]); 451 | while (ev->x >= x && ++i < LENGTH(tags)); 452 | if (i < LENGTH(tags)) { 453 | click = ClkTagBar; 454 | arg.ui = 1 << i; 455 | } else if (ev->x < x + TEXTW(selmon->ltsymbol)) 456 | click = ClkLtSymbol; 457 | else if (ev->x > selmon->ww - (int)TEXTW(stext)) 458 | click = ClkStatusText; 459 | else 460 | click = ClkWinTitle; 461 | } else if ((c = wintoclient(ev->window))) { 462 | focus(c); 463 | restack(selmon); 464 | XAllowEvents(dpy, ReplayPointer, CurrentTime); 465 | click = ClkClientWin; 466 | } 467 | for (i = 0; i < LENGTH(buttons); i++) 468 | if (click == buttons[i].click && buttons[i].func && buttons[i].button == ev->button 469 | && CLEANMASK(buttons[i].mask) == CLEANMASK(ev->state)) 470 | buttons[i].func(click == ClkTagBar && buttons[i].arg.i == 0 ? &arg : &buttons[i].arg); 471 | } 472 | 473 | void 474 | checkotherwm(void) 475 | { 476 | xerrorxlib = XSetErrorHandler(xerrorstart); 477 | /* this causes an error if some other window manager is running */ 478 | XSelectInput(dpy, DefaultRootWindow(dpy), SubstructureRedirectMask); 479 | XSync(dpy, False); 480 | XSetErrorHandler(xerror); 481 | XSync(dpy, False); 482 | } 483 | 484 | void 485 | cleanup(void) 486 | { 487 | Arg a = {.ui = ~0}; 488 | Layout foo = { "", NULL }; 489 | Monitor *m; 490 | size_t i; 491 | 492 | view(&a); 493 | selmon->lt[selmon->sellt] = &foo; 494 | for (m = mons; m; m = m->next) 495 | while (m->stack) 496 | unmanage(m->stack, 0); 497 | XUngrabKey(dpy, AnyKey, AnyModifier, root); 498 | while (mons) 499 | cleanupmon(mons); 500 | for (i = 0; i < CurLast; i++) 501 | drw_cur_free(drw, cursor[i]); 502 | for (i = 0; i < LENGTH(colors) + 1; i++) 503 | free(scheme[i]); 504 | free(scheme); 505 | XDestroyWindow(dpy, wmcheckwin); 506 | drw_free(drw); 507 | XSync(dpy, False); 508 | XSetInputFocus(dpy, PointerRoot, RevertToPointerRoot, CurrentTime); 509 | XDeleteProperty(dpy, root, netatom[NetActiveWindow]); 510 | } 511 | 512 | void 513 | cleanupmon(Monitor *mon) 514 | { 515 | Monitor *m; 516 | 517 | if (mon == mons) 518 | mons = mons->next; 519 | else { 520 | for (m = mons; m && m->next != mon; m = m->next); 521 | m->next = mon->next; 522 | } 523 | XUnmapWindow(dpy, mon->barwin); 524 | XDestroyWindow(dpy, mon->barwin); 525 | free(mon); 526 | } 527 | 528 | void 529 | clientmessage(XEvent *e) 530 | { 531 | XClientMessageEvent *cme = &e->xclient; 532 | Client *c = wintoclient(cme->window); 533 | 534 | if (!c) 535 | return; 536 | if (cme->message_type == netatom[NetWMState]) { 537 | if (cme->data.l[1] == netatom[NetWMFullscreen] 538 | || cme->data.l[2] == netatom[NetWMFullscreen]) 539 | setfullscreen(c, (cme->data.l[0] == 1 /* _NET_WM_STATE_ADD */ 540 | || (cme->data.l[0] == 2 /* _NET_WM_STATE_TOGGLE */ && !c->isfullscreen))); 541 | } else if (cme->message_type == netatom[NetActiveWindow]) { 542 | if (c != selmon->sel && !c->isurgent) 543 | seturgent(c, 1); 544 | } 545 | } 546 | 547 | void 548 | configure(Client *c) 549 | { 550 | XConfigureEvent ce; 551 | 552 | ce.type = ConfigureNotify; 553 | ce.display = dpy; 554 | ce.event = c->win; 555 | ce.window = c->win; 556 | ce.x = c->x; 557 | ce.y = c->y; 558 | ce.width = c->w; 559 | ce.height = c->h; 560 | ce.border_width = c->bw; 561 | ce.above = None; 562 | ce.override_redirect = False; 563 | XSendEvent(dpy, c->win, False, StructureNotifyMask, (XEvent *)&ce); 564 | } 565 | 566 | void 567 | configurenotify(XEvent *e) 568 | { 569 | Monitor *m; 570 | Client *c; 571 | XConfigureEvent *ev = &e->xconfigure; 572 | int dirty; 573 | 574 | /* TODO: updategeom handling sucks, needs to be simplified */ 575 | if (ev->window == root) { 576 | dirty = (sw != ev->width || sh != ev->height); 577 | sw = ev->width; 578 | sh = ev->height; 579 | if (updategeom() || dirty) { 580 | drw_resize(drw, sw, bh); 581 | updatebars(); 582 | for (m = mons; m; m = m->next) { 583 | for (c = m->clients; c; c = c->next) 584 | if (c->isfullscreen) 585 | resizeclient(c, m->mx, m->my, m->mw, m->mh); 586 | XMoveResizeWindow(dpy, m->barwin, m->wx, m->by, m->ww, bh); 587 | } 588 | focus(NULL); 589 | arrange(NULL); 590 | } 591 | } 592 | } 593 | 594 | void 595 | configurerequest(XEvent *e) 596 | { 597 | Client *c; 598 | Monitor *m; 599 | XConfigureRequestEvent *ev = &e->xconfigurerequest; 600 | XWindowChanges wc; 601 | 602 | if ((c = wintoclient(ev->window))) { 603 | if (ev->value_mask & CWBorderWidth) 604 | c->bw = ev->border_width; 605 | else if (c->isfloating || !selmon->lt[selmon->sellt]->arrange) { 606 | m = c->mon; 607 | if (ev->value_mask & CWX) { 608 | c->oldx = c->x; 609 | c->x = m->mx + ev->x; 610 | } 611 | if (ev->value_mask & CWY) { 612 | c->oldy = c->y; 613 | c->y = m->my + ev->y; 614 | } 615 | if (ev->value_mask & CWWidth) { 616 | c->oldw = c->w; 617 | c->w = ev->width; 618 | } 619 | if (ev->value_mask & CWHeight) { 620 | c->oldh = c->h; 621 | c->h = ev->height; 622 | } 623 | if ((c->x + c->w) > m->mx + m->mw && c->isfloating) 624 | c->x = m->mx + (m->mw / 2 - WIDTH(c) / 2); /* center in x direction */ 625 | if ((c->y + c->h) > m->my + m->mh && c->isfloating) 626 | c->y = m->my + (m->mh / 2 - HEIGHT(c) / 2); /* center in y direction */ 627 | if ((ev->value_mask & (CWX|CWY)) && !(ev->value_mask & (CWWidth|CWHeight))) 628 | configure(c); 629 | if (ISVISIBLE(c)) 630 | XMoveResizeWindow(dpy, c->win, c->x, c->y, c->w, c->h); 631 | } else 632 | configure(c); 633 | } else { 634 | wc.x = ev->x; 635 | wc.y = ev->y; 636 | wc.width = ev->width; 637 | wc.height = ev->height; 638 | wc.border_width = ev->border_width; 639 | wc.sibling = ev->above; 640 | wc.stack_mode = ev->detail; 641 | XConfigureWindow(dpy, ev->window, ev->value_mask, &wc); 642 | } 643 | XSync(dpy, False); 644 | } 645 | 646 | Monitor * 647 | createmon(void) 648 | { 649 | Monitor *m; 650 | 651 | m = ecalloc(1, sizeof(Monitor)); 652 | m->tagset[0] = m->tagset[1] = 1; 653 | m->mfact = mfact; 654 | m->nmaster = nmaster; 655 | m->showbar = showbar; 656 | m->topbar = topbar; 657 | m->gappih = gappih; 658 | m->gappiv = gappiv; 659 | m->gappoh = gappoh; 660 | m->gappov = gappov; 661 | m->lt[0] = &layouts[0]; 662 | m->lt[1] = &layouts[1 % LENGTH(layouts)]; 663 | strncpy(m->ltsymbol, layouts[0].symbol, sizeof m->ltsymbol); 664 | return m; 665 | } 666 | 667 | void 668 | destroynotify(XEvent *e) 669 | { 670 | Client *c; 671 | XDestroyWindowEvent *ev = &e->xdestroywindow; 672 | 673 | if ((c = wintoclient(ev->window))) 674 | unmanage(c, 1); 675 | } 676 | 677 | void 678 | detach(Client *c) 679 | { 680 | Client **tc; 681 | 682 | for (tc = &c->mon->clients; *tc && *tc != c; tc = &(*tc)->next); 683 | *tc = c->next; 684 | } 685 | 686 | void 687 | detachstack(Client *c) 688 | { 689 | Client **tc, *t; 690 | 691 | for (tc = &c->mon->stack; *tc && *tc != c; tc = &(*tc)->snext); 692 | *tc = c->snext; 693 | 694 | if (c == c->mon->sel) { 695 | for (t = c->mon->stack; t && !ISVISIBLE(t); t = t->snext); 696 | c->mon->sel = t; 697 | } 698 | } 699 | 700 | Monitor * 701 | dirtomon(int dir) 702 | { 703 | Monitor *m = NULL; 704 | 705 | if (dir > 0) { 706 | if (!(m = selmon->next)) 707 | m = mons; 708 | } else if (selmon == mons) 709 | for (m = mons; m->next; m = m->next); 710 | else 711 | for (m = mons; m->next != selmon; m = m->next); 712 | return m; 713 | } 714 | 715 | int 716 | drawstatusbar(Monitor *m, int bh, char* stext) { 717 | int ret, i, w, x, len; 718 | short isCode = 0; 719 | char *text; 720 | char *p; 721 | 722 | len = strlen(stext) + 1 ; 723 | if (!(text = (char*) malloc(sizeof(char)*len))) 724 | die("malloc"); 725 | p = text; 726 | memcpy(text, stext, len); 727 | 728 | /* compute width of the status text */ 729 | w = 0; 730 | i = -1; 731 | while (text[++i]) { 732 | if (text[i] == '^') { 733 | if (!isCode) { 734 | isCode = 1; 735 | text[i] = '\0'; 736 | w += TEXTW(text) - lrpad; 737 | text[i] = '^'; 738 | if (text[++i] == 'f') 739 | w += atoi(text + ++i); 740 | } else { 741 | isCode = 0; 742 | text = text + i + 1; 743 | i = -1; 744 | } 745 | } 746 | } 747 | if (!isCode) 748 | w += TEXTW(text) - lrpad; 749 | else 750 | isCode = 0; 751 | text = p; 752 | 753 | w += 2; /* 1px padding on both sides */ 754 | ret = x = m->ww - w; 755 | 756 | drw_setscheme(drw, scheme[LENGTH(colors)]); 757 | drw->scheme[ColFg] = scheme[SchemeNorm][ColFg]; 758 | drw->scheme[ColBg] = scheme[SchemeNorm][ColBg]; 759 | drw_rect(drw, x, 0, w, bh, 1, 1); 760 | x++; 761 | 762 | /* process status text */ 763 | i = -1; 764 | while (text[++i]) { 765 | if (text[i] == '^' && !isCode) { 766 | isCode = 1; 767 | 768 | text[i] = '\0'; 769 | w = TEXTW(text) - lrpad; 770 | drw_text(drw, x, 0, w, bh, 0, text, 0); 771 | 772 | x += w; 773 | 774 | /* process code */ 775 | while (text[++i] != '^') { 776 | if (text[i] == 'c') { 777 | char buf[8]; 778 | memcpy(buf, (char*)text+i+1, 7); 779 | buf[7] = '\0'; 780 | drw_clr_create(drw, &drw->scheme[ColFg], buf); 781 | i += 7; 782 | } else if (text[i] == 'b') { 783 | char buf[8]; 784 | memcpy(buf, (char*)text+i+1, 7); 785 | buf[7] = '\0'; 786 | drw_clr_create(drw, &drw->scheme[ColBg], buf); 787 | i += 7; 788 | } else if (text[i] == 'd') { 789 | drw->scheme[ColFg] = scheme[SchemeNorm][ColFg]; 790 | drw->scheme[ColBg] = scheme[SchemeNorm][ColBg]; 791 | } else if (text[i] == 'r') { 792 | int rx = atoi(text + ++i); 793 | while (text[++i] != ','); 794 | int ry = atoi(text + ++i); 795 | while (text[++i] != ','); 796 | int rw = atoi(text + ++i); 797 | while (text[++i] != ','); 798 | int rh = atoi(text + ++i); 799 | 800 | drw_rect(drw, rx + x, ry, rw, rh, 1, 0); 801 | } else if (text[i] == 'f') { 802 | x += atoi(text + ++i); 803 | } 804 | } 805 | 806 | text = text + i + 1; 807 | i=-1; 808 | isCode = 0; 809 | } 810 | } 811 | 812 | if (!isCode) { 813 | w = TEXTW(text) - lrpad; 814 | drw_text(drw, x, 0, w, bh, 0, text, 0); 815 | } 816 | 817 | drw_setscheme(drw, scheme[SchemeNorm]); 818 | free(p); 819 | 820 | return ret; 821 | } 822 | 823 | void 824 | drawbar(Monitor *m) 825 | { 826 | int x, w, tw = 0; 827 | /* int boxs = drw->fonts->h / 9; 828 | int boxw = drw->fonts->h / 6 + 2;*/ 829 | unsigned int i, occ = 0, urg = 0; 830 | const char *tagtext; 831 | Client *c; 832 | 833 | if (!m->showbar) 834 | return; 835 | 836 | /* draw status first so it can be overdrawn by tags later */ 837 | if (m == selmon) { /* status is only drawn on selected monitor */ 838 | tw = m->ww - drawstatusbar(m, bh, stext); 839 | } 840 | 841 | for (c = m->clients; c; c = c->next) { 842 | occ |= c->tags; 843 | if (c->isurgent) 844 | urg |= c->tags; 845 | } 846 | x = 0; 847 | for (i = 0; i < LENGTH(tags); i++) { 848 | tagtext = occ & 1 << i ? alttags[i] : tags[i]; 849 | w = TEXTW(tagtext); 850 | drw_setscheme(drw, (m->tagset[m->seltags] & 1 << i) || occ & 1 << i ? tagscheme[i] : scheme[SchemeNorm]); 851 | drw_text(drw, x, 0, w, bh, lrpad / 2, tagtext, urg & 1 << i); 852 | x += w; 853 | } 854 | w = TEXTW(m->ltsymbol); 855 | drw_setscheme(drw, scheme[SchemeNorm]); 856 | x = drw_text(drw, x, 0, w, bh, lrpad / 2, m->ltsymbol, 0); 857 | 858 | if ((w = m->ww - tw - x) > bh) { 859 | drw_setscheme(drw, scheme[SchemeNorm]); 860 | drw_rect(drw, x, 0, w, bh, 1, 1); 861 | } 862 | drw_map(drw, m->barwin, 0, 0, m->ww, bh); 863 | } 864 | 865 | void 866 | drawbars(void) 867 | { 868 | Monitor *m; 869 | 870 | for (m = mons; m; m = m->next) 871 | drawbar(m); 872 | } 873 | 874 | void 875 | enternotify(XEvent *e) 876 | { 877 | Client *c; 878 | Monitor *m; 879 | XCrossingEvent *ev = &e->xcrossing; 880 | 881 | if ((ev->mode != NotifyNormal || ev->detail == NotifyInferior) && ev->window != root) 882 | return; 883 | c = wintoclient(ev->window); 884 | m = c ? c->mon : wintomon(ev->window); 885 | if (m != selmon) { 886 | unfocus(selmon->sel, 1); 887 | selmon = m; 888 | } else if (!c || c == selmon->sel) 889 | return; 890 | focus(c); 891 | } 892 | 893 | void 894 | expose(XEvent *e) 895 | { 896 | Monitor *m; 897 | XExposeEvent *ev = &e->xexpose; 898 | 899 | if (ev->count == 0 && (m = wintomon(ev->window))) 900 | drawbar(m); 901 | } 902 | 903 | void 904 | focus(Client *c) 905 | { 906 | if (!c || !ISVISIBLE(c)) 907 | for (c = selmon->stack; c && !ISVISIBLE(c); c = c->snext); 908 | if (selmon->sel && selmon->sel != c) 909 | unfocus(selmon->sel, 0); 910 | if (c) { 911 | if (c->mon != selmon) 912 | selmon = c->mon; 913 | if (c->isurgent) 914 | seturgent(c, 0); 915 | detachstack(c); 916 | attachstack(c); 917 | grabbuttons(c, 1); 918 | XSetWindowBorder(dpy, c->win, scheme[SchemeSel][ColBorder].pixel); 919 | setfocus(c); 920 | } else { 921 | XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime); 922 | XDeleteProperty(dpy, root, netatom[NetActiveWindow]); 923 | } 924 | selmon->sel = c; 925 | drawbars(); 926 | } 927 | 928 | /* there are some broken focus acquiring clients needing extra handling */ 929 | void 930 | focusin(XEvent *e) 931 | { 932 | XFocusChangeEvent *ev = &e->xfocus; 933 | 934 | if (selmon->sel && ev->window != selmon->sel->win) 935 | setfocus(selmon->sel); 936 | } 937 | 938 | void 939 | focusmon(const Arg *arg) 940 | { 941 | Monitor *m; 942 | 943 | if (!mons->next) 944 | return; 945 | if ((m = dirtomon(arg->i)) == selmon) 946 | return; 947 | unfocus(selmon->sel, 0); 948 | selmon = m; 949 | focus(NULL); 950 | } 951 | 952 | void 953 | focusstack(const Arg *arg) 954 | { 955 | Client *c = NULL, *i; 956 | 957 | if (!selmon->sel || (selmon->sel->isfullscreen && lockfullscreen)) 958 | return; 959 | if (arg->i > 0) { 960 | for (c = selmon->sel->next; c && !ISVISIBLE(c); c = c->next); 961 | if (!c) 962 | for (c = selmon->clients; c && !ISVISIBLE(c); c = c->next); 963 | } else { 964 | for (i = selmon->clients; i != selmon->sel; i = i->next) 965 | if (ISVISIBLE(i)) 966 | c = i; 967 | if (!c) 968 | for (; i; i = i->next) 969 | if (ISVISIBLE(i)) 970 | c = i; 971 | } 972 | if (c) { 973 | focus(c); 974 | restack(selmon); 975 | } 976 | } 977 | 978 | Atom 979 | getatomprop(Client *c, Atom prop) 980 | { 981 | int di; 982 | unsigned long dl; 983 | unsigned char *p = NULL; 984 | Atom da, atom = None; 985 | 986 | if (XGetWindowProperty(dpy, c->win, prop, 0L, sizeof atom, False, XA_ATOM, 987 | &da, &di, &dl, &dl, &p) == Success && p) { 988 | atom = *(Atom *)p; 989 | XFree(p); 990 | } 991 | return atom; 992 | } 993 | 994 | int 995 | getrootptr(int *x, int *y) 996 | { 997 | int di; 998 | unsigned int dui; 999 | Window dummy; 1000 | 1001 | return XQueryPointer(dpy, root, &dummy, &dummy, x, y, &di, &di, &dui); 1002 | } 1003 | 1004 | long 1005 | getstate(Window w) 1006 | { 1007 | int format; 1008 | long result = -1; 1009 | unsigned char *p = NULL; 1010 | unsigned long n, extra; 1011 | Atom real; 1012 | 1013 | if (XGetWindowProperty(dpy, w, wmatom[WMState], 0L, 2L, False, wmatom[WMState], 1014 | &real, &format, &n, &extra, (unsigned char **)&p) != Success) 1015 | return -1; 1016 | if (n != 0) 1017 | result = *p; 1018 | XFree(p); 1019 | return result; 1020 | } 1021 | 1022 | int 1023 | gettextprop(Window w, Atom atom, char *text, unsigned int size) 1024 | { 1025 | char **list = NULL; 1026 | int n; 1027 | XTextProperty name; 1028 | 1029 | if (!text || size == 0) 1030 | return 0; 1031 | text[0] = '\0'; 1032 | if (!XGetTextProperty(dpy, w, &name, atom) || !name.nitems) 1033 | return 0; 1034 | if (name.encoding == XA_STRING) { 1035 | strncpy(text, (char *)name.value, size - 1); 1036 | } else if (XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success && n > 0 && *list) { 1037 | strncpy(text, *list, size - 1); 1038 | XFreeStringList(list); 1039 | } 1040 | text[size - 1] = '\0'; 1041 | XFree(name.value); 1042 | return 1; 1043 | } 1044 | 1045 | void 1046 | grabbuttons(Client *c, int focused) 1047 | { 1048 | updatenumlockmask(); 1049 | { 1050 | unsigned int i, j; 1051 | unsigned int modifiers[] = { 0, LockMask, numlockmask, numlockmask|LockMask }; 1052 | XUngrabButton(dpy, AnyButton, AnyModifier, c->win); 1053 | if (!focused) 1054 | XGrabButton(dpy, AnyButton, AnyModifier, c->win, False, 1055 | BUTTONMASK, GrabModeSync, GrabModeSync, None, None); 1056 | for (i = 0; i < LENGTH(buttons); i++) 1057 | if (buttons[i].click == ClkClientWin) 1058 | for (j = 0; j < LENGTH(modifiers); j++) 1059 | XGrabButton(dpy, buttons[i].button, 1060 | buttons[i].mask | modifiers[j], 1061 | c->win, False, BUTTONMASK, 1062 | GrabModeAsync, GrabModeSync, None, None); 1063 | } 1064 | } 1065 | 1066 | void 1067 | grabkeys(void) 1068 | { 1069 | updatenumlockmask(); 1070 | { 1071 | unsigned int i, j; 1072 | unsigned int modifiers[] = { 0, LockMask, numlockmask, numlockmask|LockMask }; 1073 | KeyCode code; 1074 | 1075 | XUngrabKey(dpy, AnyKey, AnyModifier, root); 1076 | for (i = 0; i < LENGTH(keys); i++) 1077 | if ((code = XKeysymToKeycode(dpy, keys[i].keysym))) 1078 | for (j = 0; j < LENGTH(modifiers); j++) 1079 | XGrabKey(dpy, code, keys[i].mod | modifiers[j], root, 1080 | True, GrabModeAsync, GrabModeAsync); 1081 | } 1082 | } 1083 | 1084 | void 1085 | incnmaster(const Arg *arg) 1086 | { 1087 | selmon->nmaster = MAX(selmon->nmaster + arg->i, 0); 1088 | arrange(selmon); 1089 | } 1090 | 1091 | #ifdef XINERAMA 1092 | static int 1093 | isuniquegeom(XineramaScreenInfo *unique, size_t n, XineramaScreenInfo *info) 1094 | { 1095 | while (n--) 1096 | if (unique[n].x_org == info->x_org && unique[n].y_org == info->y_org 1097 | && unique[n].width == info->width && unique[n].height == info->height) 1098 | return 0; 1099 | return 1; 1100 | } 1101 | #endif /* XINERAMA */ 1102 | 1103 | void 1104 | keypress(XEvent *e) 1105 | { 1106 | unsigned int i; 1107 | KeySym keysym; 1108 | XKeyEvent *ev; 1109 | 1110 | ev = &e->xkey; 1111 | keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0); 1112 | for (i = 0; i < LENGTH(keys); i++) 1113 | if (keysym == keys[i].keysym 1114 | && CLEANMASK(keys[i].mod) == CLEANMASK(ev->state) 1115 | && keys[i].func) 1116 | keys[i].func(&(keys[i].arg)); 1117 | } 1118 | 1119 | void 1120 | killclient(const Arg *arg) 1121 | { 1122 | if (!selmon->sel) 1123 | return; 1124 | if (!sendevent(selmon->sel, wmatom[WMDelete])) { 1125 | XGrabServer(dpy); 1126 | XSetErrorHandler(xerrordummy); 1127 | XSetCloseDownMode(dpy, DestroyAll); 1128 | XKillClient(dpy, selmon->sel->win); 1129 | XSync(dpy, False); 1130 | XSetErrorHandler(xerror); 1131 | XUngrabServer(dpy); 1132 | } 1133 | } 1134 | 1135 | void 1136 | manage(Window w, XWindowAttributes *wa) 1137 | { 1138 | Client *c, *t = NULL; 1139 | Window trans = None; 1140 | XWindowChanges wc; 1141 | 1142 | c = ecalloc(1, sizeof(Client)); 1143 | c->win = w; 1144 | /* geometry */ 1145 | c->x = c->oldx = wa->x; 1146 | c->y = c->oldy = wa->y; 1147 | c->w = c->oldw = wa->width; 1148 | c->h = c->oldh = wa->height; 1149 | c->oldbw = wa->border_width; 1150 | c->cfact = 1.0; 1151 | 1152 | updatetitle(c); 1153 | if (XGetTransientForHint(dpy, w, &trans) && (t = wintoclient(trans))) { 1154 | c->mon = t->mon; 1155 | c->tags = t->tags; 1156 | } else { 1157 | c->mon = selmon; 1158 | applyrules(c); 1159 | } 1160 | 1161 | if (c->x + WIDTH(c) > c->mon->wx + c->mon->ww) 1162 | c->x = c->mon->wx + c->mon->ww - WIDTH(c); 1163 | if (c->y + HEIGHT(c) > c->mon->wy + c->mon->wh) 1164 | c->y = c->mon->wy + c->mon->wh - HEIGHT(c); 1165 | c->x = MAX(c->x, c->mon->wx); 1166 | c->y = MAX(c->y, c->mon->wy); 1167 | c->bw = borderpx; 1168 | 1169 | wc.border_width = c->bw; 1170 | XConfigureWindow(dpy, w, CWBorderWidth, &wc); 1171 | XSetWindowBorder(dpy, w, scheme[SchemeNorm][ColBorder].pixel); 1172 | configure(c); /* propagates border_width, if size doesn't change */ 1173 | updatewindowtype(c); 1174 | updatesizehints(c); 1175 | updatewmhints(c); 1176 | c->x = c->mon->mx + (c->mon->mw - WIDTH(c)) / 2; 1177 | c->y = c->mon->my + (c->mon->mh - HEIGHT(c)) / 2; 1178 | XSelectInput(dpy, w, EnterWindowMask|FocusChangeMask|PropertyChangeMask|StructureNotifyMask); 1179 | grabbuttons(c, 0); 1180 | if (!c->isfloating) 1181 | c->isfloating = c->oldstate = trans != None || c->isfixed; 1182 | if (c->isfloating) 1183 | XRaiseWindow(dpy, c->win); 1184 | attach(c); 1185 | attachstack(c); 1186 | XChangeProperty(dpy, root, netatom[NetClientList], XA_WINDOW, 32, PropModeAppend, 1187 | (unsigned char *) &(c->win), 1); 1188 | XMoveResizeWindow(dpy, c->win, c->x + 2 * sw, c->y, c->w, c->h); /* some windows require this */ 1189 | setclientstate(c, NormalState); 1190 | if (c->mon == selmon) 1191 | unfocus(selmon->sel, 0); 1192 | c->mon->sel = c; 1193 | arrange(c->mon); 1194 | XMapWindow(dpy, c->win); 1195 | focus(NULL); 1196 | } 1197 | 1198 | void 1199 | mappingnotify(XEvent *e) 1200 | { 1201 | XMappingEvent *ev = &e->xmapping; 1202 | 1203 | XRefreshKeyboardMapping(ev); 1204 | if (ev->request == MappingKeyboard) 1205 | grabkeys(); 1206 | } 1207 | 1208 | void 1209 | maprequest(XEvent *e) 1210 | { 1211 | static XWindowAttributes wa; 1212 | XMapRequestEvent *ev = &e->xmaprequest; 1213 | 1214 | if (!XGetWindowAttributes(dpy, ev->window, &wa) || wa.override_redirect) 1215 | return; 1216 | if (!wintoclient(ev->window)) 1217 | manage(ev->window, &wa); 1218 | } 1219 | 1220 | void 1221 | monocle(Monitor *m) 1222 | { 1223 | unsigned int n = 0; 1224 | Client *c; 1225 | 1226 | for (c = m->clients; c; c = c->next) 1227 | if (ISVISIBLE(c)) 1228 | n++; 1229 | if (n > 0) /* override layout symbol */ 1230 | snprintf(m->ltsymbol, sizeof m->ltsymbol, "[%d]", n); 1231 | for (c = nexttiled(m->clients); c; c = nexttiled(c->next)) 1232 | resize(c, m->wx, m->wy, m->ww - 2 * c->bw, m->wh - 2 * c->bw, 0); 1233 | } 1234 | 1235 | void 1236 | motionnotify(XEvent *e) 1237 | { 1238 | static Monitor *mon = NULL; 1239 | Monitor *m; 1240 | XMotionEvent *ev = &e->xmotion; 1241 | 1242 | if (ev->window != root) 1243 | return; 1244 | if ((m = recttomon(ev->x_root, ev->y_root, 1, 1)) != mon && mon) { 1245 | unfocus(selmon->sel, 1); 1246 | selmon = m; 1247 | focus(NULL); 1248 | } 1249 | mon = m; 1250 | } 1251 | 1252 | void 1253 | movemouse(const Arg *arg) 1254 | { 1255 | int x, y, ocx, ocy, nx, ny; 1256 | Client *c; 1257 | Monitor *m; 1258 | XEvent ev; 1259 | Time lasttime = 0; 1260 | 1261 | if (!(c = selmon->sel)) 1262 | return; 1263 | if (c->isfullscreen) /* no support moving fullscreen windows by mouse */ 1264 | return; 1265 | restack(selmon); 1266 | ocx = c->x; 1267 | ocy = c->y; 1268 | if (XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync, 1269 | None, cursor[CurMove]->cursor, CurrentTime) != GrabSuccess) 1270 | return; 1271 | if (!getrootptr(&x, &y)) 1272 | return; 1273 | do { 1274 | XMaskEvent(dpy, MOUSEMASK|ExposureMask|SubstructureRedirectMask, &ev); 1275 | switch(ev.type) { 1276 | case ConfigureRequest: 1277 | case Expose: 1278 | case MapRequest: 1279 | handler[ev.type](&ev); 1280 | break; 1281 | case MotionNotify: 1282 | if ((ev.xmotion.time - lasttime) <= (1000 / 60)) 1283 | continue; 1284 | lasttime = ev.xmotion.time; 1285 | 1286 | nx = ocx + (ev.xmotion.x - x); 1287 | ny = ocy + (ev.xmotion.y - y); 1288 | if (abs(selmon->wx - nx) < snap) 1289 | nx = selmon->wx; 1290 | else if (abs((selmon->wx + selmon->ww) - (nx + WIDTH(c))) < snap) 1291 | nx = selmon->wx + selmon->ww - WIDTH(c); 1292 | if (abs(selmon->wy - ny) < snap) 1293 | ny = selmon->wy; 1294 | else if (abs((selmon->wy + selmon->wh) - (ny + HEIGHT(c))) < snap) 1295 | ny = selmon->wy + selmon->wh - HEIGHT(c); 1296 | if (!c->isfloating && selmon->lt[selmon->sellt]->arrange 1297 | && (abs(nx - c->x) > snap || abs(ny - c->y) > snap)) 1298 | togglefloating(NULL); 1299 | if (!selmon->lt[selmon->sellt]->arrange || c->isfloating) 1300 | resize(c, nx, ny, c->w, c->h, 1); 1301 | break; 1302 | } 1303 | } while (ev.type != ButtonRelease); 1304 | XUngrabPointer(dpy, CurrentTime); 1305 | if ((m = recttomon(c->x, c->y, c->w, c->h)) != selmon) { 1306 | sendmon(c, m); 1307 | selmon = m; 1308 | focus(NULL); 1309 | } 1310 | } 1311 | 1312 | Client * 1313 | nexttiled(Client *c) 1314 | { 1315 | for (; c && (c->isfloating || !ISVISIBLE(c)); c = c->next); 1316 | return c; 1317 | } 1318 | 1319 | void 1320 | pop(Client *c) 1321 | { 1322 | detach(c); 1323 | attach(c); 1324 | focus(c); 1325 | arrange(c->mon); 1326 | } 1327 | 1328 | void 1329 | propertynotify(XEvent *e) 1330 | { 1331 | Client *c; 1332 | Window trans; 1333 | XPropertyEvent *ev = &e->xproperty; 1334 | 1335 | if ((ev->window == root) && (ev->atom == XA_WM_NAME)) 1336 | updatestatus(); 1337 | else if (ev->state == PropertyDelete) 1338 | return; /* ignore */ 1339 | else if ((c = wintoclient(ev->window))) { 1340 | switch(ev->atom) { 1341 | default: break; 1342 | case XA_WM_TRANSIENT_FOR: 1343 | if (!c->isfloating && (XGetTransientForHint(dpy, c->win, &trans)) && 1344 | (c->isfloating = (wintoclient(trans)) != NULL)) 1345 | arrange(c->mon); 1346 | break; 1347 | case XA_WM_NORMAL_HINTS: 1348 | c->hintsvalid = 0; 1349 | break; 1350 | case XA_WM_HINTS: 1351 | updatewmhints(c); 1352 | drawbars(); 1353 | break; 1354 | } 1355 | if (ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) { 1356 | updatetitle(c); 1357 | if (c == c->mon->sel) 1358 | drawbar(c->mon); 1359 | } 1360 | if (ev->atom == netatom[NetWMWindowType]) 1361 | updatewindowtype(c); 1362 | } 1363 | } 1364 | 1365 | void 1366 | quit(const Arg *arg) 1367 | { 1368 | running = 0; 1369 | } 1370 | 1371 | Monitor * 1372 | recttomon(int x, int y, int w, int h) 1373 | { 1374 | Monitor *m, *r = selmon; 1375 | int a, area = 0; 1376 | 1377 | for (m = mons; m; m = m->next) 1378 | if ((a = INTERSECT(x, y, w, h, m)) > area) { 1379 | area = a; 1380 | r = m; 1381 | } 1382 | return r; 1383 | } 1384 | 1385 | void 1386 | resize(Client *c, int x, int y, int w, int h, int interact) 1387 | { 1388 | if (applysizehints(c, &x, &y, &w, &h, interact)) 1389 | resizeclient(c, x, y, w, h); 1390 | } 1391 | 1392 | void 1393 | resizeclient(Client *c, int x, int y, int w, int h) 1394 | { 1395 | XWindowChanges wc; 1396 | 1397 | c->oldx = c->x; c->x = wc.x = x; 1398 | c->oldy = c->y; c->y = wc.y = y; 1399 | c->oldw = c->w; c->w = wc.width = w; 1400 | c->oldh = c->h; c->h = wc.height = h; 1401 | wc.border_width = c->bw; 1402 | XConfigureWindow(dpy, c->win, CWX|CWY|CWWidth|CWHeight|CWBorderWidth, &wc); 1403 | configure(c); 1404 | XSync(dpy, False); 1405 | } 1406 | 1407 | void 1408 | resizemouse(const Arg *arg) 1409 | { 1410 | int ocx, ocy, nw, nh; 1411 | Client *c; 1412 | Monitor *m; 1413 | XEvent ev; 1414 | Time lasttime = 0; 1415 | 1416 | if (!(c = selmon->sel)) 1417 | return; 1418 | if (c->isfullscreen) /* no support resizing fullscreen windows by mouse */ 1419 | return; 1420 | restack(selmon); 1421 | ocx = c->x; 1422 | ocy = c->y; 1423 | if (XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync, 1424 | None, cursor[CurResize]->cursor, CurrentTime) != GrabSuccess) 1425 | return; 1426 | XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->bw - 1, c->h + c->bw - 1); 1427 | do { 1428 | XMaskEvent(dpy, MOUSEMASK|ExposureMask|SubstructureRedirectMask, &ev); 1429 | switch(ev.type) { 1430 | case ConfigureRequest: 1431 | case Expose: 1432 | case MapRequest: 1433 | handler[ev.type](&ev); 1434 | break; 1435 | case MotionNotify: 1436 | if ((ev.xmotion.time - lasttime) <= (1000 / 60)) 1437 | continue; 1438 | lasttime = ev.xmotion.time; 1439 | 1440 | nw = MAX(ev.xmotion.x - ocx - 2 * c->bw + 1, 1); 1441 | nh = MAX(ev.xmotion.y - ocy - 2 * c->bw + 1, 1); 1442 | if (c->mon->wx + nw >= selmon->wx && c->mon->wx + nw <= selmon->wx + selmon->ww 1443 | && c->mon->wy + nh >= selmon->wy && c->mon->wy + nh <= selmon->wy + selmon->wh) 1444 | { 1445 | if (!c->isfloating && selmon->lt[selmon->sellt]->arrange 1446 | && (abs(nw - c->w) > snap || abs(nh - c->h) > snap)) 1447 | togglefloating(NULL); 1448 | } 1449 | if (!selmon->lt[selmon->sellt]->arrange || c->isfloating) 1450 | resize(c, c->x, c->y, nw, nh, 1); 1451 | break; 1452 | } 1453 | } while (ev.type != ButtonRelease); 1454 | XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->bw - 1, c->h + c->bw - 1); 1455 | XUngrabPointer(dpy, CurrentTime); 1456 | while (XCheckMaskEvent(dpy, EnterWindowMask, &ev)); 1457 | if ((m = recttomon(c->x, c->y, c->w, c->h)) != selmon) { 1458 | sendmon(c, m); 1459 | selmon = m; 1460 | focus(NULL); 1461 | } 1462 | } 1463 | 1464 | void 1465 | restack(Monitor *m) 1466 | { 1467 | Client *c; 1468 | XEvent ev; 1469 | XWindowChanges wc; 1470 | 1471 | drawbar(m); 1472 | if (!m->sel) 1473 | return; 1474 | if (m->sel->isfloating || !m->lt[m->sellt]->arrange) 1475 | XRaiseWindow(dpy, m->sel->win); 1476 | if (m->lt[m->sellt]->arrange) { 1477 | wc.stack_mode = Below; 1478 | wc.sibling = m->barwin; 1479 | for (c = m->stack; c; c = c->snext) 1480 | if (!c->isfloating && ISVISIBLE(c)) { 1481 | XConfigureWindow(dpy, c->win, CWSibling|CWStackMode, &wc); 1482 | wc.sibling = c->win; 1483 | } 1484 | } 1485 | XSync(dpy, False); 1486 | while (XCheckMaskEvent(dpy, EnterWindowMask, &ev)); 1487 | } 1488 | 1489 | void 1490 | run(void) 1491 | { 1492 | XEvent ev; 1493 | /* main event loop */ 1494 | XSync(dpy, False); 1495 | while (running && !XNextEvent(dpy, &ev)) 1496 | if (handler[ev.type]) 1497 | handler[ev.type](&ev); /* call handler */ 1498 | } 1499 | 1500 | void 1501 | scan(void) 1502 | { 1503 | unsigned int i, num; 1504 | Window d1, d2, *wins = NULL; 1505 | XWindowAttributes wa; 1506 | 1507 | if (XQueryTree(dpy, root, &d1, &d2, &wins, &num)) { 1508 | for (i = 0; i < num; i++) { 1509 | if (!XGetWindowAttributes(dpy, wins[i], &wa) 1510 | || wa.override_redirect || XGetTransientForHint(dpy, wins[i], &d1)) 1511 | continue; 1512 | if (wa.map_state == IsViewable || getstate(wins[i]) == IconicState) 1513 | manage(wins[i], &wa); 1514 | } 1515 | for (i = 0; i < num; i++) { /* now the transients */ 1516 | if (!XGetWindowAttributes(dpy, wins[i], &wa)) 1517 | continue; 1518 | if (XGetTransientForHint(dpy, wins[i], &d1) 1519 | && (wa.map_state == IsViewable || getstate(wins[i]) == IconicState)) 1520 | manage(wins[i], &wa); 1521 | } 1522 | if (wins) 1523 | XFree(wins); 1524 | } 1525 | } 1526 | 1527 | void 1528 | sendmon(Client *c, Monitor *m) 1529 | { 1530 | if (c->mon == m) 1531 | return; 1532 | unfocus(c, 1); 1533 | detach(c); 1534 | detachstack(c); 1535 | c->mon = m; 1536 | c->tags = m->tagset[m->seltags]; /* assign tags of target monitor */ 1537 | attach(c); 1538 | attachstack(c); 1539 | focus(NULL); 1540 | arrange(NULL); 1541 | } 1542 | 1543 | void 1544 | setclientstate(Client *c, long state) 1545 | { 1546 | long data[] = { state, None }; 1547 | 1548 | XChangeProperty(dpy, c->win, wmatom[WMState], wmatom[WMState], 32, 1549 | PropModeReplace, (unsigned char *)data, 2); 1550 | } 1551 | 1552 | int 1553 | sendevent(Client *c, Atom proto) 1554 | { 1555 | int n; 1556 | Atom *protocols; 1557 | int exists = 0; 1558 | XEvent ev; 1559 | 1560 | if (XGetWMProtocols(dpy, c->win, &protocols, &n)) { 1561 | while (!exists && n--) 1562 | exists = protocols[n] == proto; 1563 | XFree(protocols); 1564 | } 1565 | if (exists) { 1566 | ev.type = ClientMessage; 1567 | ev.xclient.window = c->win; 1568 | ev.xclient.message_type = wmatom[WMProtocols]; 1569 | ev.xclient.format = 32; 1570 | ev.xclient.data.l[0] = proto; 1571 | ev.xclient.data.l[1] = CurrentTime; 1572 | XSendEvent(dpy, c->win, False, NoEventMask, &ev); 1573 | } 1574 | return exists; 1575 | } 1576 | 1577 | void 1578 | setfocus(Client *c) 1579 | { 1580 | if (!c->neverfocus) { 1581 | XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime); 1582 | XChangeProperty(dpy, root, netatom[NetActiveWindow], 1583 | XA_WINDOW, 32, PropModeReplace, 1584 | (unsigned char *) &(c->win), 1); 1585 | } 1586 | sendevent(c, wmatom[WMTakeFocus]); 1587 | } 1588 | 1589 | void 1590 | setfullscreen(Client *c, int fullscreen) 1591 | { 1592 | if (fullscreen && !c->isfullscreen) { 1593 | XChangeProperty(dpy, c->win, netatom[NetWMState], XA_ATOM, 32, 1594 | PropModeReplace, (unsigned char*)&netatom[NetWMFullscreen], 1); 1595 | c->isfullscreen = 1; 1596 | c->oldstate = c->isfloating; 1597 | c->oldbw = c->bw; 1598 | c->bw = 0; 1599 | c->isfloating = 1; 1600 | resizeclient(c, c->mon->mx, c->mon->my, c->mon->mw, c->mon->mh); 1601 | XRaiseWindow(dpy, c->win); 1602 | } else if (!fullscreen && c->isfullscreen){ 1603 | XChangeProperty(dpy, c->win, netatom[NetWMState], XA_ATOM, 32, 1604 | PropModeReplace, (unsigned char*)0, 0); 1605 | c->isfullscreen = 0; 1606 | c->isfloating = c->oldstate; 1607 | c->bw = c->oldbw; 1608 | c->x = c->oldx; 1609 | c->y = c->oldy; 1610 | c->w = c->oldw; 1611 | c->h = c->oldh; 1612 | resizeclient(c, c->x, c->y, c->w, c->h); 1613 | arrange(c->mon); 1614 | } 1615 | } 1616 | 1617 | void 1618 | setlayout(const Arg *arg) 1619 | { 1620 | if (!arg || !arg->v || arg->v != selmon->lt[selmon->sellt]) 1621 | selmon->sellt ^= 1; 1622 | if (arg && arg->v) 1623 | selmon->lt[selmon->sellt] = (Layout *)arg->v; 1624 | strncpy(selmon->ltsymbol, selmon->lt[selmon->sellt]->symbol, sizeof selmon->ltsymbol); 1625 | if (selmon->sel) 1626 | arrange(selmon); 1627 | else 1628 | drawbar(selmon); 1629 | } 1630 | 1631 | void setcfact(const Arg *arg) { 1632 | float f; 1633 | Client *c; 1634 | 1635 | c = selmon->sel; 1636 | 1637 | if(!arg || !c || !selmon->lt[selmon->sellt]->arrange) 1638 | return; 1639 | f = arg->f + c->cfact; 1640 | if(arg->f == 0.0) 1641 | f = 1.0; 1642 | else if(f < 0.25 || f > 4.0) 1643 | return; 1644 | c->cfact = f; 1645 | arrange(selmon); 1646 | } 1647 | 1648 | /* arg > 1.0 will set mfact absolutely */ 1649 | void 1650 | setmfact(const Arg *arg) 1651 | { 1652 | float f; 1653 | 1654 | if (!arg || !selmon->lt[selmon->sellt]->arrange) 1655 | return; 1656 | f = arg->f < 1.0 ? arg->f + selmon->mfact : arg->f - 1.0; 1657 | if (f < 0.05 || f > 0.95) 1658 | return; 1659 | selmon->mfact = f; 1660 | arrange(selmon); 1661 | } 1662 | 1663 | void 1664 | setup(void) 1665 | { 1666 | int i; 1667 | XSetWindowAttributes wa; 1668 | Atom utf8string; 1669 | 1670 | /* clean up any zombies immediately */ 1671 | sigchld(0); 1672 | 1673 | /* init screen */ 1674 | screen = DefaultScreen(dpy); 1675 | sw = DisplayWidth(dpy, screen); 1676 | sh = DisplayHeight(dpy, screen); 1677 | root = RootWindow(dpy, screen); 1678 | drw = drw_create(dpy, screen, root, sw, sh); 1679 | if (!drw_fontset_create(drw, fonts, LENGTH(fonts))) 1680 | die("no fonts could be loaded."); 1681 | lrpad = drw->fonts->h; 1682 | bh = drw->fonts->h + 2; 1683 | updategeom(); 1684 | /* init atoms */ 1685 | utf8string = XInternAtom(dpy, "UTF8_STRING", False); 1686 | wmatom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False); 1687 | wmatom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False); 1688 | wmatom[WMState] = XInternAtom(dpy, "WM_STATE", False); 1689 | wmatom[WMTakeFocus] = XInternAtom(dpy, "WM_TAKE_FOCUS", False); 1690 | netatom[NetActiveWindow] = XInternAtom(dpy, "_NET_ACTIVE_WINDOW", False); 1691 | netatom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False); 1692 | netatom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False); 1693 | netatom[NetWMState] = XInternAtom(dpy, "_NET_WM_STATE", False); 1694 | netatom[NetWMCheck] = XInternAtom(dpy, "_NET_SUPPORTING_WM_CHECK", False); 1695 | netatom[NetWMFullscreen] = XInternAtom(dpy, "_NET_WM_STATE_FULLSCREEN", False); 1696 | netatom[NetWMWindowType] = XInternAtom(dpy, "_NET_WM_WINDOW_TYPE", False); 1697 | netatom[NetWMWindowTypeDialog] = XInternAtom(dpy, "_NET_WM_WINDOW_TYPE_DIALOG", False); 1698 | netatom[NetClientList] = XInternAtom(dpy, "_NET_CLIENT_LIST", False); 1699 | /* init cursors */ 1700 | cursor[CurNormal] = drw_cur_create(drw, XC_left_ptr); 1701 | cursor[CurResize] = drw_cur_create(drw, XC_sizing); 1702 | cursor[CurMove] = drw_cur_create(drw, XC_fleur); 1703 | /* init appearance */ 1704 | scheme = ecalloc(LENGTH(colors) + 1, sizeof(Clr *)); 1705 | scheme[LENGTH(colors)] = drw_scm_create(drw, colors[0], 3); 1706 | if (LENGTH(tags) > LENGTH(tagsel)) 1707 | die("too few color schemes for the tags"); 1708 | for (i = 0; i < LENGTH(colors); i++) 1709 | scheme[i] = drw_scm_create(drw, colors[i], 3); 1710 | tagscheme = ecalloc(LENGTH(tagsel), sizeof(Clr *)); 1711 | for (i = 0; i < LENGTH(tagsel); i++) 1712 | tagscheme[i] = drw_scm_create(drw, tagsel[i], 2); 1713 | /* init bars */ 1714 | updatebars(); 1715 | updatestatus(); 1716 | /* supporting window for NetWMCheck */ 1717 | wmcheckwin = XCreateSimpleWindow(dpy, root, 0, 0, 1, 1, 0, 0, 0); 1718 | XChangeProperty(dpy, wmcheckwin, netatom[NetWMCheck], XA_WINDOW, 32, 1719 | PropModeReplace, (unsigned char *) &wmcheckwin, 1); 1720 | XChangeProperty(dpy, wmcheckwin, netatom[NetWMName], utf8string, 8, 1721 | PropModeReplace, (unsigned char *) "dwm", 3); 1722 | XChangeProperty(dpy, root, netatom[NetWMCheck], XA_WINDOW, 32, 1723 | PropModeReplace, (unsigned char *) &wmcheckwin, 1); 1724 | /* EWMH support per view */ 1725 | XChangeProperty(dpy, root, netatom[NetSupported], XA_ATOM, 32, 1726 | PropModeReplace, (unsigned char *) netatom, NetLast); 1727 | XDeleteProperty(dpy, root, netatom[NetClientList]); 1728 | /* select events */ 1729 | wa.cursor = cursor[CurNormal]->cursor; 1730 | wa.event_mask = SubstructureRedirectMask|SubstructureNotifyMask 1731 | |ButtonPressMask|PointerMotionMask|EnterWindowMask 1732 | |LeaveWindowMask|StructureNotifyMask|PropertyChangeMask; 1733 | XChangeWindowAttributes(dpy, root, CWEventMask|CWCursor, &wa); 1734 | XSelectInput(dpy, root, wa.event_mask); 1735 | grabkeys(); 1736 | focus(NULL); 1737 | } 1738 | 1739 | void 1740 | seturgent(Client *c, int urg) 1741 | { 1742 | XWMHints *wmh; 1743 | 1744 | c->isurgent = urg; 1745 | if (!(wmh = XGetWMHints(dpy, c->win))) 1746 | return; 1747 | wmh->flags = urg ? (wmh->flags | XUrgencyHint) : (wmh->flags & ~XUrgencyHint); 1748 | XSetWMHints(dpy, c->win, wmh); 1749 | XFree(wmh); 1750 | } 1751 | 1752 | void 1753 | showhide(Client *c) 1754 | { 1755 | if (!c) 1756 | return; 1757 | if (ISVISIBLE(c)) { 1758 | /* show clients top down */ 1759 | XMoveWindow(dpy, c->win, c->x, c->y); 1760 | if ((!c->mon->lt[c->mon->sellt]->arrange || c->isfloating) && !c->isfullscreen) 1761 | resize(c, c->x, c->y, c->w, c->h, 0); 1762 | showhide(c->snext); 1763 | } else { 1764 | /* hide clients bottom up */ 1765 | showhide(c->snext); 1766 | XMoveWindow(dpy, c->win, WIDTH(c) * -2, c->y); 1767 | } 1768 | } 1769 | 1770 | void 1771 | sigchld(int unused) 1772 | { 1773 | if (signal(SIGCHLD, sigchld) == SIG_ERR) 1774 | die("can't install SIGCHLD handler:"); 1775 | while (0 < waitpid(-1, NULL, WNOHANG)); 1776 | } 1777 | 1778 | void 1779 | spawn(const Arg *arg) 1780 | { 1781 | if (fork() == 0) { 1782 | if (dpy) 1783 | close(ConnectionNumber(dpy)); 1784 | setsid(); 1785 | execvp(((char **)arg->v)[0], (char **)arg->v); 1786 | die("dwm: execvp '%s' failed:", ((char **)arg->v)[0]); 1787 | } 1788 | } 1789 | 1790 | void 1791 | tag(const Arg *arg) 1792 | { 1793 | if (selmon->sel && arg->ui & TAGMASK) { 1794 | selmon->sel->tags = arg->ui & TAGMASK; 1795 | focus(NULL); 1796 | arrange(selmon); 1797 | } 1798 | } 1799 | 1800 | void 1801 | tagmon(const Arg *arg) 1802 | { 1803 | if (!selmon->sel || !mons->next) 1804 | return; 1805 | sendmon(selmon->sel, dirtomon(arg->i)); 1806 | } 1807 | 1808 | void 1809 | togglebar(const Arg *arg) 1810 | { 1811 | selmon->showbar = !selmon->showbar; 1812 | updatebarpos(selmon); 1813 | XMoveResizeWindow(dpy, selmon->barwin, selmon->wx, selmon->by, selmon->ww, bh); 1814 | arrange(selmon); 1815 | } 1816 | 1817 | void 1818 | togglefloating(const Arg *arg) 1819 | { 1820 | if (!selmon->sel) 1821 | return; 1822 | if (selmon->sel->isfullscreen) /* no support for fullscreen windows */ 1823 | return; 1824 | selmon->sel->isfloating = !selmon->sel->isfloating || selmon->sel->isfixed; 1825 | if (selmon->sel->isfloating) 1826 | resize(selmon->sel, selmon->sel->x, selmon->sel->y, 1827 | selmon->sel->w, selmon->sel->h, 0); 1828 | arrange(selmon); 1829 | } 1830 | 1831 | void 1832 | togglefullscr(const Arg *arg) 1833 | { 1834 | if(selmon->sel) 1835 | setfullscreen(selmon->sel, !selmon->sel->isfullscreen); 1836 | } 1837 | 1838 | void 1839 | toggletag(const Arg *arg) 1840 | { 1841 | unsigned int newtags; 1842 | 1843 | if (!selmon->sel) 1844 | return; 1845 | newtags = selmon->sel->tags ^ (arg->ui & TAGMASK); 1846 | if (newtags) { 1847 | selmon->sel->tags = newtags; 1848 | focus(NULL); 1849 | arrange(selmon); 1850 | } 1851 | } 1852 | 1853 | void 1854 | toggleview(const Arg *arg) 1855 | { 1856 | unsigned int newtagset = selmon->tagset[selmon->seltags] ^ (arg->ui & TAGMASK); 1857 | 1858 | if (newtagset) { 1859 | selmon->tagset[selmon->seltags] = newtagset; 1860 | focus(NULL); 1861 | arrange(selmon); 1862 | } 1863 | } 1864 | 1865 | void 1866 | unfocus(Client *c, int setfocus) 1867 | { 1868 | if (!c) 1869 | return; 1870 | grabbuttons(c, 0); 1871 | XSetWindowBorder(dpy, c->win, scheme[SchemeNorm][ColBorder].pixel); 1872 | if (setfocus) { 1873 | XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime); 1874 | XDeleteProperty(dpy, root, netatom[NetActiveWindow]); 1875 | } 1876 | } 1877 | 1878 | void 1879 | unmanage(Client *c, int destroyed) 1880 | { 1881 | Monitor *m = c->mon; 1882 | XWindowChanges wc; 1883 | 1884 | detach(c); 1885 | detachstack(c); 1886 | if (!destroyed) { 1887 | wc.border_width = c->oldbw; 1888 | XGrabServer(dpy); /* avoid race conditions */ 1889 | XSetErrorHandler(xerrordummy); 1890 | XSelectInput(dpy, c->win, NoEventMask); 1891 | XConfigureWindow(dpy, c->win, CWBorderWidth, &wc); /* restore border */ 1892 | XUngrabButton(dpy, AnyButton, AnyModifier, c->win); 1893 | setclientstate(c, WithdrawnState); 1894 | XSync(dpy, False); 1895 | XSetErrorHandler(xerror); 1896 | XUngrabServer(dpy); 1897 | } 1898 | free(c); 1899 | focus(NULL); 1900 | updateclientlist(); 1901 | arrange(m); 1902 | } 1903 | 1904 | void 1905 | unmapnotify(XEvent *e) 1906 | { 1907 | Client *c; 1908 | XUnmapEvent *ev = &e->xunmap; 1909 | 1910 | if ((c = wintoclient(ev->window))) { 1911 | if (ev->send_event) 1912 | setclientstate(c, WithdrawnState); 1913 | else 1914 | unmanage(c, 0); 1915 | } 1916 | } 1917 | 1918 | void 1919 | updatebars(void) 1920 | { 1921 | Monitor *m; 1922 | XSetWindowAttributes wa = { 1923 | .override_redirect = True, 1924 | .background_pixmap = ParentRelative, 1925 | .event_mask = ButtonPressMask|ExposureMask 1926 | }; 1927 | XClassHint ch = {"dwm", "dwm"}; 1928 | for (m = mons; m; m = m->next) { 1929 | if (m->barwin) 1930 | continue; 1931 | m->barwin = XCreateWindow(dpy, root, m->wx, m->by, m->ww, bh, 0, DefaultDepth(dpy, screen), 1932 | CopyFromParent, DefaultVisual(dpy, screen), 1933 | CWOverrideRedirect|CWBackPixmap|CWEventMask, &wa); 1934 | XDefineCursor(dpy, m->barwin, cursor[CurNormal]->cursor); 1935 | XMapRaised(dpy, m->barwin); 1936 | XSetClassHint(dpy, m->barwin, &ch); 1937 | } 1938 | } 1939 | 1940 | void 1941 | updatebarpos(Monitor *m) 1942 | { 1943 | m->wy = m->my; 1944 | m->wh = m->mh; 1945 | if (m->showbar) { 1946 | m->wh -= bh; 1947 | m->by = m->topbar ? m->wy : m->wy + m->wh; 1948 | m->wy = m->topbar ? m->wy + bh : m->wy; 1949 | } else 1950 | m->by = -bh; 1951 | } 1952 | 1953 | void 1954 | updateclientlist() 1955 | { 1956 | Client *c; 1957 | Monitor *m; 1958 | 1959 | XDeleteProperty(dpy, root, netatom[NetClientList]); 1960 | for (m = mons; m; m = m->next) 1961 | for (c = m->clients; c; c = c->next) 1962 | XChangeProperty(dpy, root, netatom[NetClientList], 1963 | XA_WINDOW, 32, PropModeAppend, 1964 | (unsigned char *) &(c->win), 1); 1965 | } 1966 | 1967 | int 1968 | updategeom(void) 1969 | { 1970 | int dirty = 0; 1971 | 1972 | #ifdef XINERAMA 1973 | if (XineramaIsActive(dpy)) { 1974 | int i, j, n, nn; 1975 | Client *c; 1976 | Monitor *m; 1977 | XineramaScreenInfo *info = XineramaQueryScreens(dpy, &nn); 1978 | XineramaScreenInfo *unique = NULL; 1979 | 1980 | for (n = 0, m = mons; m; m = m->next, n++); 1981 | /* only consider unique geometries as separate screens */ 1982 | unique = ecalloc(nn, sizeof(XineramaScreenInfo)); 1983 | for (i = 0, j = 0; i < nn; i++) 1984 | if (isuniquegeom(unique, j, &info[i])) 1985 | memcpy(&unique[j++], &info[i], sizeof(XineramaScreenInfo)); 1986 | XFree(info); 1987 | nn = j; 1988 | 1989 | /* new monitors if nn > n */ 1990 | for (i = n; i < nn; i++) { 1991 | for (m = mons; m && m->next; m = m->next); 1992 | if (m) 1993 | m->next = createmon(); 1994 | else 1995 | mons = createmon(); 1996 | } 1997 | for (i = 0, m = mons; i < nn && m; m = m->next, i++) 1998 | if (i >= n 1999 | || unique[i].x_org != m->mx || unique[i].y_org != m->my 2000 | || unique[i].width != m->mw || unique[i].height != m->mh) 2001 | { 2002 | dirty = 1; 2003 | m->num = i; 2004 | m->mx = m->wx = unique[i].x_org; 2005 | m->my = m->wy = unique[i].y_org; 2006 | m->mw = m->ww = unique[i].width; 2007 | m->mh = m->wh = unique[i].height; 2008 | updatebarpos(m); 2009 | } 2010 | /* removed monitors if n > nn */ 2011 | for (i = nn; i < n; i++) { 2012 | for (m = mons; m && m->next; m = m->next); 2013 | while ((c = m->clients)) { 2014 | dirty = 1; 2015 | m->clients = c->next; 2016 | detachstack(c); 2017 | c->mon = mons; 2018 | attach(c); 2019 | attachstack(c); 2020 | } 2021 | if (m == selmon) 2022 | selmon = mons; 2023 | cleanupmon(m); 2024 | } 2025 | free(unique); 2026 | } else 2027 | #endif /* XINERAMA */ 2028 | { /* default monitor setup */ 2029 | if (!mons) 2030 | mons = createmon(); 2031 | if (mons->mw != sw || mons->mh != sh) { 2032 | dirty = 1; 2033 | mons->mw = mons->ww = sw; 2034 | mons->mh = mons->wh = sh; 2035 | updatebarpos(mons); 2036 | } 2037 | } 2038 | if (dirty) { 2039 | selmon = mons; 2040 | selmon = wintomon(root); 2041 | } 2042 | return dirty; 2043 | } 2044 | 2045 | void 2046 | updatenumlockmask(void) 2047 | { 2048 | unsigned int i, j; 2049 | XModifierKeymap *modmap; 2050 | 2051 | numlockmask = 0; 2052 | modmap = XGetModifierMapping(dpy); 2053 | for (i = 0; i < 8; i++) 2054 | for (j = 0; j < modmap->max_keypermod; j++) 2055 | if (modmap->modifiermap[i * modmap->max_keypermod + j] 2056 | == XKeysymToKeycode(dpy, XK_Num_Lock)) 2057 | numlockmask = (1 << i); 2058 | XFreeModifiermap(modmap); 2059 | } 2060 | 2061 | void 2062 | updatesizehints(Client *c) 2063 | { 2064 | long msize; 2065 | XSizeHints size; 2066 | 2067 | if (!XGetWMNormalHints(dpy, c->win, &size, &msize)) 2068 | /* size is uninitialized, ensure that size.flags aren't used */ 2069 | size.flags = PSize; 2070 | if (size.flags & PBaseSize) { 2071 | c->basew = size.base_width; 2072 | c->baseh = size.base_height; 2073 | } else if (size.flags & PMinSize) { 2074 | c->basew = size.min_width; 2075 | c->baseh = size.min_height; 2076 | } else 2077 | c->basew = c->baseh = 0; 2078 | if (size.flags & PResizeInc) { 2079 | c->incw = size.width_inc; 2080 | c->inch = size.height_inc; 2081 | } else 2082 | c->incw = c->inch = 0; 2083 | if (size.flags & PMaxSize) { 2084 | c->maxw = size.max_width; 2085 | c->maxh = size.max_height; 2086 | } else 2087 | c->maxw = c->maxh = 0; 2088 | if (size.flags & PMinSize) { 2089 | c->minw = size.min_width; 2090 | c->minh = size.min_height; 2091 | } else if (size.flags & PBaseSize) { 2092 | c->minw = size.base_width; 2093 | c->minh = size.base_height; 2094 | } else 2095 | c->minw = c->minh = 0; 2096 | if (size.flags & PAspect) { 2097 | c->mina = (float)size.min_aspect.y / size.min_aspect.x; 2098 | c->maxa = (float)size.max_aspect.x / size.max_aspect.y; 2099 | } else 2100 | c->maxa = c->mina = 0.0; 2101 | c->isfixed = (c->maxw && c->maxh && c->maxw == c->minw && c->maxh == c->minh); 2102 | c->hintsvalid = 1; 2103 | } 2104 | 2105 | void 2106 | updatestatus(void) 2107 | { 2108 | if (!gettextprop(root, XA_WM_NAME, stext, sizeof(stext))) 2109 | strcpy(stext, "dwm-"VERSION); 2110 | drawbar(selmon); 2111 | } 2112 | 2113 | void 2114 | updatetitle(Client *c) 2115 | { 2116 | if (!gettextprop(c->win, netatom[NetWMName], c->name, sizeof c->name)) 2117 | gettextprop(c->win, XA_WM_NAME, c->name, sizeof c->name); 2118 | if (c->name[0] == '\0') /* hack to mark broken clients */ 2119 | strcpy(c->name, broken); 2120 | } 2121 | 2122 | void 2123 | updatewindowtype(Client *c) 2124 | { 2125 | Atom state = getatomprop(c, netatom[NetWMState]); 2126 | Atom wtype = getatomprop(c, netatom[NetWMWindowType]); 2127 | 2128 | if (state == netatom[NetWMFullscreen]) 2129 | setfullscreen(c, 1); 2130 | if (wtype == netatom[NetWMWindowTypeDialog]) 2131 | c->isfloating = 1; 2132 | } 2133 | 2134 | void 2135 | updatewmhints(Client *c) 2136 | { 2137 | XWMHints *wmh; 2138 | 2139 | if ((wmh = XGetWMHints(dpy, c->win))) { 2140 | if (c == selmon->sel && wmh->flags & XUrgencyHint) { 2141 | wmh->flags &= ~XUrgencyHint; 2142 | XSetWMHints(dpy, c->win, wmh); 2143 | } else 2144 | c->isurgent = (wmh->flags & XUrgencyHint) ? 1 : 0; 2145 | if (wmh->flags & InputHint) 2146 | c->neverfocus = !wmh->input; 2147 | else 2148 | c->neverfocus = 0; 2149 | XFree(wmh); 2150 | } 2151 | } 2152 | 2153 | void 2154 | view(const Arg *arg) 2155 | { 2156 | if ((arg->ui & TAGMASK) == selmon->tagset[selmon->seltags]) 2157 | return; 2158 | selmon->seltags ^= 1; /* toggle sel tagset */ 2159 | if (arg->ui & TAGMASK) 2160 | selmon->tagset[selmon->seltags] = arg->ui & TAGMASK; 2161 | focus(NULL); 2162 | arrange(selmon); 2163 | } 2164 | 2165 | Client * 2166 | wintoclient(Window w) 2167 | { 2168 | Client *c; 2169 | Monitor *m; 2170 | 2171 | for (m = mons; m; m = m->next) 2172 | for (c = m->clients; c; c = c->next) 2173 | if (c->win == w) 2174 | return c; 2175 | return NULL; 2176 | } 2177 | 2178 | Monitor * 2179 | wintomon(Window w) 2180 | { 2181 | int x, y; 2182 | Client *c; 2183 | Monitor *m; 2184 | 2185 | if (w == root && getrootptr(&x, &y)) 2186 | return recttomon(x, y, 1, 1); 2187 | for (m = mons; m; m = m->next) 2188 | if (w == m->barwin) 2189 | return m; 2190 | if ((c = wintoclient(w))) 2191 | return c->mon; 2192 | return selmon; 2193 | } 2194 | 2195 | /* There's no way to check accesses to destroyed windows, thus those cases are 2196 | * ignored (especially on UnmapNotify's). Other types of errors call Xlibs 2197 | * default error handler, which may call exit. */ 2198 | int 2199 | xerror(Display *dpy, XErrorEvent *ee) 2200 | { 2201 | if (ee->error_code == BadWindow 2202 | || (ee->request_code == X_SetInputFocus && ee->error_code == BadMatch) 2203 | || (ee->request_code == X_PolyText8 && ee->error_code == BadDrawable) 2204 | || (ee->request_code == X_PolyFillRectangle && ee->error_code == BadDrawable) 2205 | || (ee->request_code == X_PolySegment && ee->error_code == BadDrawable) 2206 | || (ee->request_code == X_ConfigureWindow && ee->error_code == BadMatch) 2207 | || (ee->request_code == X_GrabButton && ee->error_code == BadAccess) 2208 | || (ee->request_code == X_GrabKey && ee->error_code == BadAccess) 2209 | || (ee->request_code == X_CopyArea && ee->error_code == BadDrawable)) 2210 | return 0; 2211 | fprintf(stderr, "dwm: fatal error: request code=%d, error code=%d\n", 2212 | ee->request_code, ee->error_code); 2213 | return xerrorxlib(dpy, ee); /* may call exit */ 2214 | } 2215 | 2216 | int 2217 | xerrordummy(Display *dpy, XErrorEvent *ee) 2218 | { 2219 | return 0; 2220 | } 2221 | 2222 | /* Startup Error handler to check if another window manager 2223 | * is already running. */ 2224 | int 2225 | xerrorstart(Display *dpy, XErrorEvent *ee) 2226 | { 2227 | die("dwm: another window manager is already running"); 2228 | return -1; 2229 | } 2230 | 2231 | void 2232 | zoom(const Arg *arg) 2233 | { 2234 | Client *c = selmon->sel; 2235 | 2236 | if (!selmon->lt[selmon->sellt]->arrange || !c || c->isfloating) 2237 | return; 2238 | if (c == nexttiled(selmon->clients) && !(c = nexttiled(c->next))) 2239 | return; 2240 | pop(c); 2241 | } 2242 | 2243 | int 2244 | main(int argc, char *argv[]) 2245 | { 2246 | if (argc == 2 && !strcmp("-v", argv[1])) 2247 | die("dwm-"VERSION); 2248 | else if (argc != 1) 2249 | die("usage: dwm [-v]"); 2250 | if (!setlocale(LC_CTYPE, "") || !XSupportsLocale()) 2251 | fputs("warning: no locale support\n", stderr); 2252 | if (!(dpy = XOpenDisplay(NULL))) 2253 | die("dwm: cannot open display"); 2254 | checkotherwm(); 2255 | setup(); 2256 | #ifdef __OpenBSD__ 2257 | if (pledge("stdio rpath proc exec", NULL) == -1) 2258 | die("pledge"); 2259 | #endif /* __OpenBSD__ */ 2260 | scan(); 2261 | run(); 2262 | cleanup(); 2263 | XCloseDisplay(dpy); 2264 | return EXIT_SUCCESS; 2265 | } 2266 | --------------------------------------------------------------------------------