about summary refs log blame commit diff stats
path: root/event.c
blob: 125c6a20353b881589033721d616104ae40c9822 (plain) (tree)
1
2
3
4
5
6
7
8
9



                                                              
                
                   
                       
                      
 
            







                               
    
 
                                                          
 


                    


                                 
                  
 

                   
                                                                                  
                                                                           


                                                                           
                                                               





                                             
                                          

                                                         
                                                  










                                                         
                     
                   
                      
                  
 

                   
                                                                                  

                                                                                     
                                                                
                 
                                                               





                                             
                                          



                                                               

                                                                        



                                                                                     
                                                






                                                         

           

                      

              
                  
                                              
 
                                  








                                                         


                         
                                              
                         



                                    

                                                                             
                                             
                         

                              
                                   

                              

                                                                             
                                               
                         





                              

                           
                  
                                                           
                     
                          
                              
 
                                         
                                   







                                             
                                                  
                                                     
                                    




















                                                                                        
                                  



                                                  









                                                                       
                                  
         




                        


                                                     

                                       




                      
                  
                                           
 
                                                                    

                       
                                                                      
                         
                                     
                             


                                                                            


           

                 
                  
                                       

                            
                                        
                                     
                                                    
                                     



           

                   
                                                               

                       
                                 

                                                                
                                  
                                             

                                                                              



                                                         
         






                                           



                                                      


           









                                          

                     
                                    
                                               









                                                                            

                                        




                         
                  

                                           



                                       
                                         
                                                     
                                                    

                               


                                                 
                                                                          
                                                                              
                                                      

                                                
                                           

                                      
                                                                              

                                     

                 




                      


                                     

                                       
 
 
            








                                              
                                        







                                          
                                                               


                       
                                                   

                                                            

                                                              

                                                                      



                                                                                    

         
/*
 * (C)opyright MMVI Anselm R. Garbe <garbeam at gmail dot com>
 * See LICENSE file for license details.
 */
#include "dwm.h"
#include <stdlib.h>
#include <X11/keysym.h>
#include <X11/Xatom.h>

/* static */

typedef struct {
	unsigned long mod;
	KeySym keysym;
	void (*func)(Arg *arg);
	Arg arg;
} Key;

KEYS

#define CLEANMASK(mask) (mask & ~(NUMLOCKMASK | LockMask))

static void
movemouse(Client *c)
{
	int x1, y1, ocx, ocy, di;
	unsigned int dui;
	Window dummy;
	XEvent ev;

	ocx = c->x;
	ocy = c->y;
	if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
			None, cursor[CurMove], CurrentTime) != GrabSuccess)
		return;
	XQueryPointer(dpy, root, &dummy, &dummy, &x1, &y1, &di, &di, &dui);
	for(;;) {
		XMaskEvent(dpy, MOUSEMASK | ExposureMask, &ev);
		switch (ev.type) {
		default: break;
		case Expose:
			handler[Expose](&ev);
			break;
		case MotionNotify:
			XSync(dpy, False);
			c->x = ocx + (ev.xmotion.x - x1);
			c->y = ocy + (ev.xmotion.y - y1);
			resize(c, False, TopLeft);
			break;
		case ButtonRelease:
			XUngrabPointer(dpy, CurrentTime);
			return;
		}
	}
}

static void
resizemouse(Client *c)
{
	int ocx, ocy;
	int nw, nh;
	Corner sticky;
	XEvent ev;

	ocx = c->x;
	ocy = c->y;
	if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
				None, cursor[CurResize], CurrentTime) != GrabSuccess)
		return;
	XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w, c->h);
	for(;;) {
		XMaskEvent(dpy, MOUSEMASK | ExposureMask, &ev);
		switch(ev.type) {
		default: break;
		case Expose:
			handler[Expose](&ev);
			break;
		case MotionNotify:
			XSync(dpy, False);
			if((nw = abs(ocx - ev.xmotion.x)))
				c->w = abs(ocx - ev.xmotion.x);
			if((nh = abs(ocy - ev.xmotion.y)))
				c->h = abs(ocy - ev.xmotion.y);
			c->x = (ocx <= ev.xmotion.x) ? ocx : ocx - c->w;
			c->y = (ocy <= ev.xmotion.y) ? ocy : ocy - c->h;
			if(ocx <= ev.xmotion.x)
				sticky = (ocy <= ev.xmotion.y) ? TopLeft : BotLeft;
			else
				sticky = (ocy <= ev.xmotion.y) ? TopRight : BotRight;
			resize(c, True, sticky);
			break;
		case ButtonRelease:
			XUngrabPointer(dpy, CurrentTime);
			return;
		}
	}
}

static void
buttonpress(XEvent *e)
{
	int x;
	Arg a;
	Client *c;
	XButtonPressedEvent *ev = &e->xbutton;

	if(barwin == ev->window) {
		x = 0;
		for(a.i = 0; a.i < ntags; a.i++) {
			x += textw(tags[a.i]);
			if(ev->x < x) {
				if(ev->button == Button3)
					toggleview(&a);
				else
					view(&a);
				return;
			}
		}
	}
	else if((c = getclient(ev->window))) {
		focus(c);
		switch(ev->button) {
		default:
			break;
		case Button1:
			if(!c->ismax && (arrange == dofloat || c->isfloat)) {
				restack(c);
				movemouse(c);
			}
			break;
		case Button2:
			zoom(NULL);
			break;
		case Button3:
			if(!c->ismax && (arrange == dofloat || c->isfloat)) {
				restack(c);
				resizemouse(c);
			}
			break;
		}
	}
}

static void
configurerequest(XEvent *e)
{
	Client *c;
	XConfigureRequestEvent *ev = &e->xconfigurerequest;
	XEvent synev;
	XWindowChanges wc;
	unsigned long newmask;

	if((c = getclient(ev->window))) {
		gravitate(c, True);
		if(ev->value_mask & CWX)
			c->x = ev->x;
		if(ev->value_mask & CWY)
			c->y = ev->y;
		if(ev->value_mask & CWWidth)
			c->w = ev->width;
		if(ev->value_mask & CWHeight)
			c->h = ev->height;
		if(ev->value_mask & CWBorderWidth)
			c->border = ev->border_width;
		gravitate(c, False);
		wc.x = c->x;
		wc.y = c->y;
		wc.width = c->w;
		wc.height = c->h;
		newmask = ev->value_mask & (~(CWSibling | CWStackMode | CWBorderWidth));
		if(newmask)
			XConfigureWindow(dpy, c->win, newmask, &wc);
		else {
			synev.type = ConfigureNotify;
			synev.xconfigure.display = dpy;
			synev.xconfigure.event = c->win;
			synev.xconfigure.window = c->win;
			synev.xconfigure.x = c->x;
			synev.xconfigure.y = c->y;
			synev.xconfigure.width = c->w;
			synev.xconfigure.height = c->h;
			synev.xconfigure.border_width = c->border;
			synev.xconfigure.above = None;
			/* Send synthetic ConfigureNotify */
			XSendEvent(dpy, c->win, True, NoEventMask, &synev);
		}
		XSync(dpy, False);
		if(c->isfloat)
			resize(c, False, TopLeft);
		else
			arrange(NULL);
	}
	else {
		wc.x = ev->x;
		wc.y = ev->y;
		wc.width = ev->width;
		wc.height = ev->height;
		wc.border_width = ev->border_width;
		wc.sibling = ev->above;
		wc.stack_mode = ev->detail;
		XConfigureWindow(dpy, ev->window, ev->value_mask, &wc);
		XSync(dpy, False);
	}
}

static void
destroynotify(XEvent *e)
{
	Client *c;
	XDestroyWindowEvent *ev = &e->xdestroywindow;

	if((c = getclient(ev->window)))
		unmanage(c);
}

static void
enternotify(XEvent *e)
{
	Client *c;
	XCrossingEvent *ev = &e->xcrossing;

	if(ev->mode != NotifyNormal || ev->detail == NotifyInferior)
		return;

	if((c = getclient(ev->window)) || (c = getctitle(ev->window)))
		focus(c);
	else if(ev->window == root) {
		issel = True;
		XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
		drawall();
	}
}

static void
expose(XEvent *e)
{
	Client *c;
	XExposeEvent *ev = &e->xexpose;

	if(ev->count == 0) {
		if(barwin == ev->window)
			drawstatus();
		else if((c = getctitle(ev->window)))
			drawtitle(c);
	}
}

static void
keypress(XEvent *e)
{
	static unsigned int len = sizeof(key) / sizeof(key[0]);
	unsigned int i;
	KeySym keysym;
	XKeyEvent *ev = &e->xkey;

	keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
	for(i = 0; i < len; i++) {
		if(keysym == key[i].keysym &&
				CLEANMASK(key[i].mod) == CLEANMASK(ev->state))
		{
			if(key[i].func)
				key[i].func(&key[i].arg);
			return;
		}
	}
}

static void
leavenotify(XEvent *e)
{
	XCrossingEvent *ev = &e->xcrossing;

	if((ev->window == root) && !ev->same_screen) {
		issel = False;
		drawall();
	}
}

static void
mappingnotify(XEvent *e)
{
	XMappingEvent *ev = &e->xmapping;

	XRefreshKeyboardMapping(ev);
	if(ev->request == MappingKeyboard)
		grabkeys();
}

static void
maprequest(XEvent *e)
{
	static XWindowAttributes wa;
	XMapRequestEvent *ev = &e->xmaprequest;

	if(!XGetWindowAttributes(dpy, ev->window, &wa))
		return;

	if(wa.override_redirect) {
		XSelectInput(dpy, ev->window,
				(StructureNotifyMask | PropertyChangeMask));
		return;
	}

	if(!getclient(ev->window))
		manage(ev->window, &wa);
}

static void
propertynotify(XEvent *e)
{
	Client *c;
	Window trans;
	XPropertyEvent *ev = &e->xproperty;

	if(ev->state == PropertyDelete)
		return; /* ignore */

	if((c = getclient(ev->window))) {
		if(ev->atom == wmatom[WMProtocols]) {
			c->proto = getproto(c->win);
			return;
		}
		switch (ev->atom) {
			default: break;
			case XA_WM_TRANSIENT_FOR:
				XGetTransientForHint(dpy, c->win, &trans);
				if(!c->isfloat && (c->isfloat = (trans != 0)))
					arrange(NULL);
				break;
			case XA_WM_NORMAL_HINTS:
				setsize(c);
				break;
		}
		if(ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
			settitle(c);
			drawtitle(c);
		}
	}
}

static void
unmapnotify(XEvent *e)
{
	Client *c;
	XUnmapEvent *ev = &e->xunmap;

	if((c = getclient(ev->window)))
		unmanage(c);
}

/* extern */

void (*handler[LASTEvent]) (XEvent *) = {
	[ButtonPress] = buttonpress,
	[ConfigureRequest] = configurerequest,
	[DestroyNotify] = destroynotify,
	[EnterNotify] = enternotify,
	[LeaveNotify] = leavenotify,
	[Expose] = expose,
	[KeyPress] = keypress,
	[MappingNotify] = mappingnotify,
	[MapRequest] = maprequest,
	[PropertyNotify] = propertynotify,
	[UnmapNotify] = unmapnotify
};

void
grabkeys()
{
	static unsigned int len = sizeof(key) / sizeof(key[0]);
	unsigned int i;
	KeyCode code;

	XUngrabKey(dpy, AnyKey, AnyModifier, root);
	for(i = 0; i < len; i++) {
		code = XKeysymToKeycode(dpy, key[i].keysym);
		XGrabKey(dpy, code, key[i].mod, root, True,
				GrabModeAsync, GrabModeAsync);
		XGrabKey(dpy, code, key[i].mod | LockMask, root, True,
				GrabModeAsync, GrabModeAsync);
		XGrabKey(dpy, code, key[i].mod | NUMLOCKMASK, root, True,
				GrabModeAsync, GrabModeAsync);
		XGrabKey(dpy, code, key[i].mod | NUMLOCKMASK | LockMask, root, True,
				GrabModeAsync, GrabModeAsync);
	}
}
n class="w"> Current_search_pattern = pattern; Current_search_direction = dir; } return true; } else if (key == TB_KEY_ESC || key == TB_KEY_CTRL_C) { return false; } else if (key == TB_KEY_ARROW_LEFT) { if (col > /*slash*/1) { --col; tb_set_cursor(col, bottom_screen_line); } } else if (key == TB_KEY_ARROW_RIGHT) { if (col-/*slash*/1 < SIZE(pattern)) { ++col; tb_set_cursor(col, bottom_screen_line); } } else if (key == TB_KEY_HOME || key == TB_KEY_CTRL_A) { col = /*skip slash*/1; tb_set_cursor(col, bottom_screen_line); } else if (key == TB_KEY_END || key == TB_KEY_CTRL_E) { col = SIZE(pattern)+/*skip slash*/1; tb_set_cursor(col, bottom_screen_line); } else if (key == TB_KEY_BACKSPACE || key == TB_KEY_BACKSPACE2) { if (col > /*slash*/1) { assert(col <= SIZE(pattern)+1); --col; // update pattern pattern.erase(col-/*slash*/1, /*len*/1); // update screen tb_set_cursor(col, bottom_screen_line); for (int x = col; x < SIZE(pattern)+/*skip slash*/1; ++x) tb_print(pattern.at(x-/*slash*/1), TB_WHITE, TB_BLACK); tb_print(' ', TB_WHITE, TB_BLACK); tb_set_cursor(col, bottom_screen_line); } } else if (key == TB_KEY_CTRL_K) { int old_pattern_size = SIZE(pattern); pattern.erase(col-/*slash*/1, SIZE(pattern) - (col-/*slash*/1)); tb_set_cursor(col, bottom_screen_line); for (int x = col; x < old_pattern_size+/*slash*/1; ++x) tb_print(' ', TB_WHITE, TB_BLACK); tb_set_cursor(col, bottom_screen_line); } else if (key == TB_KEY_CTRL_U) { int old_pattern_size = SIZE(pattern); pattern.erase(0, col-/*slash*/1); col = /*skip slash*/1; tb_set_cursor(col, bottom_screen_line); for (int x = /*slash*/1; x < SIZE(pattern)+/*skip slash*/1; ++x) tb_print(pattern.at(x-/*slash*/1), TB_WHITE, TB_BLACK); for (int x = SIZE(pattern)+/*slash*/1; x < old_pattern_size+/*skip slash*/1; ++x) tb_print(' ', TB_WHITE, TB_BLACK); tb_set_cursor(col, bottom_screen_line); } else if (key < 128) { // ascii only // update pattern char c = static_cast<char>(key); assert(col-1 >= 0); assert(col-1 <= SIZE(pattern)); pattern.insert(col-/*slash*/1, /*num*/1, c); // update screen for (int x = col; x < SIZE(pattern)+/*skip slash*/1; ++x) tb_print(pattern.at(x-/*slash*/1), TB_WHITE, TB_BLACK); ++col; tb_set_cursor(col, bottom_screen_line); } } } void render() { // Trace_index -> Last_printed_row, screen int screen_row = 0; for (screen_row = 0; screen_row < tb_height(); ++screen_row) { if (!contains_key(Trace_index, screen_row)) break; int trace_index = get(Trace_index, screen_row); trace_line& curr_line = Trace_stream->past_lines.at(trace_index); ostringstream out; if (screen_row < tb_height()-1) { int delta = lines_hidden(screen_row); // home-brew escape sequence for red if (delta > 1) { if (delta > 999) out << static_cast<char>(1); out << std::setw(6) << delta << "| "; if (delta > 999) out << static_cast<char>(2); } else { out << " "; } } else { out << " "; } out << std::setw(2) << curr_line.depth << ' ' << curr_line.label << ": " << curr_line.contents; int bg = bg_color(curr_line.depth, trace_index, screen_row); render_line(screen_row, out.str(), bg); } // clear rest of screen Last_printed_row = screen_row-1; for (; screen_row < tb_height(); ++screen_row) render_line(screen_row, "~", /*bg*/TB_BLACK); // move cursor back to display row at the end tb_set_cursor(0, Cursor_row); } int main(int argc, char* argv[]) { if (argc != 2) { cerr << "Usage: browse_trace <trace file>\n"; return 1; } load_trace(argv[1]); if (!Trace_stream) return 1; cerr << "computing min depth to display\n"; int min_depth = 9999; for (int i = 0; i < SIZE(Trace_stream->past_lines); ++i) { trace_line& curr_line = Trace_stream->past_lines.at(i); if (curr_line.depth < min_depth) min_depth = curr_line.depth; } cerr << "min depth is " << min_depth << '\n'; cerr << "computing lines to display\n"; for (int i = 0; i < SIZE(Trace_stream->past_lines); ++i) { if (Trace_stream->past_lines.at(i).depth == min_depth) Visible.insert(i); } tb_init(); tb_clear(); Cursor_row = 0; Top_of_screen = 0; refresh_screen_rows(); while (true) { render(); int key = read_key(); if (key == 'q' || key == 'Q' || key == TB_KEY_CTRL_C) break; if (key == 'j' || key == TB_KEY_ARROW_DOWN) { // move cursor one line down if (Cursor_row < Last_printed_row) ++Cursor_row; } else if (key == 'k' || key == TB_KEY_ARROW_UP) { // move cursor one line up if (Cursor_row > 0) --Cursor_row; } else if (key == 't') { // move cursor to top of screen Cursor_row = 0; } else if (key == 'c') { // move cursor to center of screen Cursor_row = tb_height()/2; while (!contains_key(Trace_index, Cursor_row)) --Cursor_row; } else if (key == 'b') { // move cursor to bottom of screen Cursor_row = tb_height()-1; while (!contains_key(Trace_index, Cursor_row)) --Cursor_row; } else if (key == 'T') { // scroll line at cursor to top of screen Top_of_screen = get(Trace_index, Cursor_row); Cursor_row = 0; refresh_screen_rows(); } else if (key == 'h' || key == TB_KEY_ARROW_LEFT) { // pan screen one character left if (Left_of_screen > 0) --Left_of_screen; } else if (key == 'l' || key == TB_KEY_ARROW_RIGHT) { // pan screen one character right ++Left_of_screen; } else if (key == 'H') { // pan screen one screen-width left Left_of_screen -= (tb_width() - 5); if (Left_of_screen < 0) Left_of_screen = 0; } else if (key == 'L') { // pan screen one screen-width right Left_of_screen += (tb_width() - 5); } else if (key == 'J' || key == TB_KEY_PGDN || key == TB_KEY_CTRL_F) { // page-down if (Trace_index.find(tb_height()-1) != Trace_index.end()) { Top_of_screen = get(Trace_index, tb_height()-1) + 1; refresh_screen_rows(); } } else if (key == 'K' || key == TB_KEY_PGUP || key == TB_KEY_CTRL_B) { // page-up is more convoluted for (int screen_row = tb_height(); screen_row > 0 && Top_of_screen > 0; --screen_row) { --Top_of_screen; if (Top_of_screen <= 0) break; while (Top_of_screen > 0 && !contains_key(Visible, Top_of_screen)) --Top_of_screen; } if (Top_of_screen >= 0) refresh_screen_rows(); } else if (key == 'g' || key == TB_KEY_HOME) { Top_of_screen = 0; Last_printed_row = 0; Cursor_row = 0; refresh_screen_rows(); } else if (key == 'G' || key == TB_KEY_END) { // go to bottom of trace; largely like page-up, interestingly Top_of_screen = SIZE(Trace_stream->past_lines)-1; for (int screen_row = tb_height(); screen_row > 0 && Top_of_screen > 0; --screen_row) { --Top_of_screen; if (Top_of_screen <= 0) break; while (Top_of_screen > 0 && !contains_key(Visible, Top_of_screen)) --Top_of_screen; } refresh_screen_rows(); render(); // move cursor to bottom Cursor_row = Last_printed_row; refresh_screen_rows(); } else if (key == TB_KEY_CARRIAGE_RETURN) { // expand lines under current by one level assert(contains_key(Trace_index, Cursor_row)); int start_index = get(Trace_index, Cursor_row); int index = 0; // simultaneously compute end_index and min_depth int min_depth = 9999; for (index = start_index+1; index < SIZE(Trace_stream->past_lines); ++index) { if (contains_key(Visible, index)) break; trace_line& curr_line = Trace_stream->past_lines.at(index); assert(curr_line.depth > Trace_stream->past_lines.at(start_index).depth); if (curr_line.depth < min_depth) min_depth = curr_line.depth; } int end_index = index; // mark as visible all intervening indices at min_depth for (index = start_index; index < end_index; ++index) { trace_line& curr_line = Trace_stream->past_lines.at(index); if (curr_line.depth == min_depth) { Visible.insert(index); } } refresh_screen_rows(); } else if (key == TB_KEY_BACKSPACE || key == TB_KEY_BACKSPACE2) { // collapse all lines under current assert(contains_key(Trace_index, Cursor_row)); int start_index = get(Trace_index, Cursor_row); int index = 0; // end_index is the next line at a depth same as or lower than start_index int initial_depth = Trace_stream->past_lines.at(start_index).depth; for (index = start_index+1; index < SIZE(Trace_stream->past_lines); ++index) { if (!contains_key(Visible, index)) continue; trace_line& curr_line = Trace_stream->past_lines.at(index); if (curr_line.depth <= initial_depth) break; } int end_index = index; // mark as visible all intervening indices at min_depth for (index = start_index+1; index < end_index; ++index) { Visible.erase(index); } refresh_screen_rows(); } else if (key == '/') { if (start_search_editor(FORWARD)) search(Current_search_pattern, Current_search_direction); } else if (key == '?') { if (start_search_editor(BACKWARD)) search(Current_search_pattern, Current_search_direction); } else if (key == 'n') { if (!Current_search_pattern.empty()) search(Current_search_pattern, Current_search_direction); } else if (key == 'N') { if (!Current_search_pattern.empty()) search(Current_search_pattern, opposite(Current_search_direction)); } } tb_clear(); tb_shutdown(); return 0; }