about summary refs log tree commit diff stats
path: root/src
Commit message (Expand)AuthorAgeFilesLines
...
* support the comment/uncomment hotkey on MacsKartik K. Agaram2021-12-032-2/+3
* less ambiguous menusKartik K. Agaram2021-12-032-9/+9
* get rid of `Esc` hotkeyKartik K. Agaram2021-12-033-10/+9
* typosKartik K. Agaram2021-12-031-3/+3
* experimenting with different keysKartik K. Agaram2021-12-031-0/+2
* stop showing frequent save messagesKartik K. Agaram2021-12-031-1/+1
* show ^h in a couple more menusKartik K. Agaram2021-12-031-0/+2
* improve support for backspaceKartik K. Agaram2021-12-033-18/+15
* error message when no app is providedKartik K. Agaram2021-12-031-194/+6
* .Kartik K. Agaram2021-12-031-2/+3
* legible colors across all terminal configurationsKartik K. Agaram2021-12-031-1/+1
* show state of screen on runtime errorKartik K. Agaram2021-12-031-0/+7
* .Kartik K. Agaram2021-12-031-1/+1
* extract a helperKartik K. Agaram2021-12-031-18/+32
* fix a slight portability issue, maybeKartik K. Agaram2021-12-021-1/+1
* stop inserting ctrl- keys into programsKartik K. Agaram2021-11-301-4/+2
* better cross-platform backspace supportKartik K. Agaram2021-11-302-4/+10
* kilo bugfix: syntax highlight final word in lineKartik K. Agaram2021-11-291-1/+1
* gotcha: silent string concatenationKartik K. Agaram2021-11-291-1/+1
* .Kartik K. Agaram2021-11-291-7/+7
* a couple more global curses bindingsKartik K. Agaram2021-11-291-0/+3
* fix regression in showing error messagesKartik K. Agaram2021-11-291-4/+4
* .Kartik K. Agaram2021-11-291-2/+0
* let people fix bad images from within TelivaKartik K. Agaram2021-11-292-12/+28
* redo uncommentKartik K. Agaram2021-11-281-5/+6
* auto-indentKartik K. Agaram2021-11-281-1/+7
* tab key: always insert two spacesKartik K. Agaram2021-11-281-0/+5
* hide some 'advanced' hotkeys to support narrow windowsKartik K. Agaram2021-11-281-4/+0
* ctrl-/ to comment/uncomment lineKartik K. Agaram2021-11-282-0/+28
* ctrl-u/ctrl-k to make editing a bit more ergonomicKartik K. Agaram2021-11-282-1/+18
* special indent settings for kiloKartik K. Agaram2021-11-281-0/+3
* indentKartik K. Agaram2021-11-281-40/+40
* mac backspace attempt 3Kartik K. Agaram2021-11-281-1/+1
* fix half-assed mac os backspace commitKartik K. Agaram2021-11-282-4/+4
* sacrificing another LoC to ward off crashesKartik K. Agaram2021-11-281-0/+1
* editing notes sucks a little lessKartik K. Agaram2021-11-282-4/+10
* fix the bug described in commit cec57992b7Kartik K. Agaram2021-11-281-4/+12
* rename a fn and reorganize its responsibilitiesKartik K. Agaram2021-11-281-3/+3
* split a lumpy abstraction into two cleaner onesKartik K. Agaram2021-11-281-13/+19
* purge abstraction hiding teliva_editor_bufferKartik K. Agaram2021-11-281-10/+5
* inline another functionKartik K. Agaram2021-11-282-8/+2
* take one responsibility away from edit_imageKartik K. Agaram2021-11-281-4/+4
* .Kartik K. Agaram2021-11-281-1/+1
* inline an unnecessary functionKartik K. Agaram2021-11-281-7/+2
* start streamlining architectureKartik K. Agaram2021-11-282-3/+4
* fix going to big picture after saving editor stateKartik K. Agaram2021-11-281-7/+7
* yet another fucking makefile bugKartik K. Agaram2021-11-281-1/+1
* clear editor state across user-visible restartsKartik K. Agaram2021-11-281-1/+3
* use header everywhereKartik K. Agaram2021-11-281-3/+3
* unused variableKartik K. Agaram2021-11-281-1/+0
0 12:18:06 +0200 committer arg@10ksloc.org <unknown> 2006-07-20 12:18:06 +0200 cleaned up code' href='/acidbong/suckless/dwm/commit/main.c?h=master&id=dc5d967ee61046f899b3b49daeb9268c8161844a'>dc5d967 ^
1076f2b
1076f2b
b6ad663 ^
1076f2b






0053620 ^
1076f2b





b5159df ^










































b55bd70 ^





b5159df ^

e995c1b ^
b5159df ^






b55bd70 ^
b5159df ^










2e834e9 ^
b5159df ^

b5159df ^


adaa28a ^










bf35794 ^

3399650 ^
dba2306 ^
3399650 ^
0ff8065 ^
352e1b4 ^

3399650 ^
0ff8065 ^


352e1b4 ^
3399650 ^
0ff8065 ^
c09bf8d ^
080a38d ^
352e1b4 ^
3399650 ^



c0705ee ^
3399650 ^









e6cbe9c ^
3399650 ^

adaa28a ^

1076f2b
727449d ^
1076f2b

dba2306 ^
bf35794 ^


dba2306 ^


4641aa2 ^
dba2306 ^
dc5d967 ^





dba2306 ^

dc5d967 ^
dba2306 ^
4641aa2 ^

1076f2b


b5159df ^
dc5d967 ^
1076f2b
4970ef9 ^


1076f2b
4970ef9 ^

1076f2b


86d1224 ^
1076f2b
e571de8 ^
1076f2b


dba2306 ^

86d1224 ^
1076f2b
f60c597 ^
1076f2b
dba2306 ^

1076f2b
b6ad663 ^
f60c597 ^
dba2306 ^
2ffdc19 ^
1076f2b
b5159df ^
c0705ee ^
dba2306 ^
1076f2b
4e2c5b5 ^
0680c76 ^
b6ad663 ^
727449d ^
e3fd306 ^
0e5c819 ^
95e8d12 ^

e571de8 ^
b5159df ^

0e5c819 ^
b5159df ^
95e8d12 ^


0cf3ba0 ^
95e8d12 ^

c0705ee ^
0e5c819 ^

b5159df ^
b439892 ^
df74b26 ^
439e15d ^
1076f2b




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302




                                                              
                
                  

                   
                   
                   
                       
                           
                       


                       



                 

                                                    


                                      
                       






                       
            
 
                                                   
                            

           

         
                            
                    
                                           

                              



                                               
                                                   

                                      
                                    
                                                                           
                          
 
 
           
      

                            
                             
                             
 
                    






                                                                                           
                                                     





                            










































                                                                                                                    





                                                   

                      
                                                                                              






                                        
                                       










                                                                                       
                                                                         

                                                                        


                                     










                                                           

            
   
                  
 
                                      

                                 
 


                                                                                             
                                               
                              
                                
                                                    
                                              
                        



                      
                                       









                                                   
                          

 

              
 
                                 

 
  


                                                                              


                                     
 
                                      





                                                                                     

                                                                             
                                                  
                                                         

 


                            
                   
                  
 


                                                                                   
         

                                            


                              
                                                     
 
                                    


                                       

                                      
                                                                          
                                                          
                          
 

                                                                           
 
                          
                               
                                              
                          
 
                
                     
               
 
                                                                
                          
                    
                      
                        
                             

                                                  
                                 

                                                           
                                 
                           


                                                                                    
                                                                     

                                                                     
                                             

                         
                              
                                                  
                            
         




                           
/*
 * (C)opyright MMVI Anselm R. Garbe <garbeam at gmail dot com>
 * See LICENSE file for license details.
 */

#include "dwm.h"
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/select.h>
#include <X11/cursorfont.h>
#include <X11/keysym.h>
#include <X11/Xatom.h>
#include <X11/Xproto.h>

/* extern */

char stext[1024];
Bool *seltag;
int bx, by, bw, bh, bmw, mw, screen, sx, sy, sw, sh;
unsigned int ntags, numlockmask;
Atom wmatom[WMLast], netatom[NetLast];
Bool running = True;
Bool issel = True;
Bool maximized = False;
Client *clients = NULL;
Client *sel = NULL;
Cursor cursor[CurLast];
Display *dpy;
DC dc = {0};
Window root, barwin;

/* static */

static int (*xerrorxlib)(Display *, XErrorEvent *);
static Bool otherwm, readin;

static void
cleanup()
{
	close(STDIN_FILENO);
	while(sel) {
		resize(sel, True, TopLeft);
		unmanage(sel);
	}
	if(dc.font.set)
		XFreeFontSet(dpy, dc.font.set);
	else
		XFreeFont(dpy, dc.font.xfont);
	XUngrabKey(dpy, AnyKey, AnyModifier, root);
	XFreePixmap(dpy, dc.drawable);
	XFreeGC(dpy, dc.gc);
	XDestroyWindow(dpy, barwin);
	XSetInputFocus(dpy, PointerRoot, RevertToPointerRoot, CurrentTime);
	XSync(dpy, False);
}

static void
scan()
{
	unsigned int i, num;
	Window *wins, d1, d2;
	XWindowAttributes wa;

	wins = NULL;
	if(XQueryTree(dpy, root, &d1, &d2, &wins, &num)) {
		for(i = 0; i < num; i++) {
			if(!XGetWindowAttributes(dpy, wins[i], &wa))
				continue;
			if(wa.override_redirect || XGetTransientForHint(dpy, wins[i], &d1))
				continue;
			if(wa.map_state == IsViewable)
				manage(wins[i], &wa);
		}
	}
	if(wins)
		XFree(wins);
}

static void
setup()
{
	int i, j;
	unsigned int mask;
	Window w;
	XModifierKeymap *modmap;
	XSetWindowAttributes wa;

	/* init atoms */
	wmatom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False);
	wmatom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
	netatom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
	netatom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
	XChangeProperty(dpy, root, netatom[NetSupported], XA_ATOM, 32,
			PropModeReplace, (unsigned char *) netatom, NetLast);

	/* init cursors */
	cursor[CurNormal] = XCreateFontCursor(dpy, XC_left_ptr);
	cursor[CurResize] = XCreateFontCursor(dpy, XC_sizing);
	cursor[CurMove] = XCreateFontCursor(dpy, XC_fleur);

	modmap = XGetModifierMapping(dpy);
	for (i = 0; i < 8; i++) {
		for (j = 0; j < modmap->max_keypermod; j++) {
			if(modmap->modifiermap[i * modmap->max_keypermod + j] == XKeysymToKeycode(dpy, XK_Num_Lock))
				numlockmask = (1 << i);
		}
	}
	XFree(modmap);

	wa.event_mask = SubstructureRedirectMask | SubstructureNotifyMask | EnterWindowMask | LeaveWindowMask;
	wa.cursor = cursor[CurNormal];
	XChangeWindowAttributes(dpy, root, CWEventMask | CWCursor, &wa);

	grabkeys();
	initrregs();

	for(ntags = 0; tags[ntags]; ntags++);
	seltag = emallocz(sizeof(Bool) * ntags);
	seltag[0] = True;

	/* style */
	dc.norm[ColBG] = getcolor(NORMBGCOLOR);
	dc.norm[ColFG] = getcolor(NORMFGCOLOR);
	dc.sel[ColBG] = getcolor(SELBGCOLOR);
	dc.sel[ColFG] = getcolor(SELFGCOLOR);
	dc.status[ColBG] = getcolor(STATUSBGCOLOR);
	dc.status[ColFG] = getcolor(STATUSFGCOLOR);
	setfont(FONT);

	bmw = textw(FLOATSYMBOL) > textw(TILESYMBOL) ? textw(FLOATSYMBOL) : textw(TILESYMBOL);
	sx = sy = 0;
	sw = DisplayWidth(dpy, screen);
	sh = DisplayHeight(dpy, screen);
	mw = (sw * MASTERW) / 100;

	bx = by = 0;
	bw = sw;
	dc.h = bh = dc.font.height + 2;
	wa.override_redirect = 1;
	wa.background_pixmap = ParentRelative;
	wa.event_mask = ButtonPressMask | ExposureMask;
	barwin = XCreateWindow(dpy, root, bx, by, bw, bh, 0, DefaultDepth(dpy, screen),
			CopyFromParent, DefaultVisual(dpy, screen),
			CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
	XDefineCursor(dpy, barwin, cursor[CurNormal]);
	XMapRaised(dpy, barwin);

	dc.drawable = XCreatePixmap(dpy, root, sw, bh, DefaultDepth(dpy, screen));
	dc.gc = XCreateGC(dpy, root, 0, 0);
	XSetLineAttributes(dpy, dc.gc, 1, LineSolid, CapButt, JoinMiter);

	issel = XQueryPointer(dpy, root, &w, &w, &i, &i, &i, &i, &mask);
	strcpy(stext, "dwm-"VERSION);
}

/*
 * Startup Error handler to check if another window manager
 * is already running.
 */
static int
xerrorstart(Display *dsply, XErrorEvent *ee)
{
	otherwm = True;
	return -1;
}

/* extern */

int
getproto(Window w)
{
	int i, format, protos, status;
	unsigned long extra, res;
	Atom *protocols, real;

	protos = 0;
	status = XGetWindowProperty(dpy, w, wmatom[WMProtocols], 0L, 20L, False,
			XA_ATOM, &real, &format, &res, &extra, (unsigned char **)&protocols);
	if(status != Success || protocols == 0)
		return protos;
	for(i = 0; i < res; i++)
		if(protocols[i] == wmatom[WMDelete])
			protos |= PROTODELWIN;
	free(protocols);
	return protos;
}

void
sendevent(Window w, Atom a, long value)
{
	XEvent e;

	e.type = ClientMessage;
	e.xclient.window = w;
	e.xclient.message_type = a;
	e.xclient.format = 32;
	e.xclient.data.l[0] = value;
	e.xclient.data.l[1] = CurrentTime;
	XSendEvent(dpy, w, False, NoEventMask, &e);
	XSync(dpy, False);
}

void
quit(Arg *arg)
{
	readin = running = False;
}

/*
 * There's no way to check accesses to destroyed windows, thus those cases are
 * ignored (especially on UnmapNotify's).  Other types of errors call Xlibs
 * default error handler, which calls exit().
 */
int
xerror(Display *dpy, XErrorEvent *ee)
{
	if(ee->error_code == BadWindow
	|| (ee->request_code == X_SetInputFocus && ee->error_code == BadMatch)
	|| (ee->request_code == X_PolyText8 && ee->error_code == BadDrawable)
	|| (ee->request_code == X_PolyFillRectangle && ee->error_code == BadDrawable)
	|| (ee->request_code == X_PolySegment && ee->error_code == BadDrawable)
	|| (ee->request_code == X_ConfigureWindow && ee->error_code == BadMatch)
	|| (ee->request_code == X_GrabKey && ee->error_code == BadAccess))
		return 0;
	fprintf(stderr, "dwm: fatal error: request code=%d, error code=%d\n",
		ee->request_code, ee->error_code);
	return xerrorxlib(dpy, ee); /* may call exit() */
}

int
main(int argc, char *argv[])
{
	int r, xfd;
	fd_set rd;

	if(argc == 2 && !strncmp("-v", argv[1], 3)) {
		fputs("dwm-"VERSION", (C)opyright MMVI Anselm R. Garbe\n", stdout);
		exit(EXIT_SUCCESS);
	}
	else if(argc != 1)
		eprint("usage: dwm [-v]\n");

	dpy = XOpenDisplay(0);
	if(!dpy)
		eprint("dwm: cannot open display\n");

	xfd = ConnectionNumber(dpy);
	screen = DefaultScreen(dpy);
	root = RootWindow(dpy, screen);

	otherwm = False;
	XSetErrorHandler(xerrorstart);
	/* this causes an error if some other window manager is running */
	XSelectInput(dpy, root, SubstructureRedirectMask);
	XSync(dpy, False);

	if(otherwm)
		eprint("dwm: another window manager is already running\n");

	XSync(dpy, False);
	XSetErrorHandler(NULL);
	xerrorxlib = XSetErrorHandler(xerror);
	XSync(dpy, False);

	setup();
	drawstatus();
	scan();

	/* main event loop, also reads status text from stdin */
	XSync(dpy, False);
	procevent();
	readin = True;
	while(running) {
		FD_ZERO(&rd);
		if(readin)
			FD_SET(STDIN_FILENO, &rd);
		FD_SET(xfd, &rd);
		r = select(xfd + 1, &rd, NULL, NULL, NULL);
		if((r == -1) && (errno == EINTR))
			continue;
		if(r > 0) {
			if(readin && FD_ISSET(STDIN_FILENO, &rd)) {
				readin = NULL != fgets(stext, sizeof(stext), stdin);
				if(readin)
					stext[strlen(stext) - 1] = 0;
				else 
					strcpy(stext, "broken pipe");
				drawstatus();
			}
		}
		else if(r < 0)
			eprint("select failed\n");
		procevent();
	}
	cleanup();
	XCloseDisplay(dpy);

	return 0;
}