/*
 * BadWM - minimalistic window manager for the X Window System
 * Copyright (C) Robert Annessi <robert@annessi.at>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

#include <stdlib.h>
#include <stdio.h>
#include <strings.h>
#include <X11/Xatom.h>
#include "include/config.h"
#include "include/BadWM.h"

void handle_key_event(XKeyEvent *e) {
	Client *c = find_client(e->window);
	KeySym key = XKeycodeToKeysym(dpy,e->keycode,0);
	char *keystr = XKeysymToString(key);
	key = is_int(keystr);

	if (c == NULL) c = current;
	
	if (c) {
		if ((strcasecmp(keystr, config.keys->max)) == 0) { 
			maximise_horiz(c);
			maximise_vert(c);
			resize(c, 1);
		} else if (strcasecmp(keystr, config.keys->kill) == 0) { 
			send_wm_delete(c);
		} else if ((strcasecmp(keystr, config.keys->maxvert)) == 0) { 
			maximise_vert(c);
			resize(c, 1);
		} else if ((strcasecmp(keystr, config.keys->maxhoriz)) == 0) { 
			maximise_horiz(c);
			resize(c, 1);
		} else if ((strcasecmp(keystr, config.keys->lower)) == 0) { 
			XLowerWindow(dpy, c->parent);
		} else if ((strcasecmp(keystr, config.keys->fix)) == 0) { 
			c->vdesk = c->vdesk == STICKY ? vdesk : STICKY;
			client_update_current(c, current);
		} else if ((strcasecmp(keystr, config.keys->topleft)) == 0) {
			c->x = c->border;
			c->y = c->border;
			move(c, 1);
			setmouse(c->window, c->width/2, c->height/2);
		} else if ((strcasecmp(keystr, config.keys->topright)) == 0) {
			c->x = DisplayWidth(dpy, c->screen->screen)-c->width-c->border;
			c->y = c->border;
			move(c, 1);
			setmouse(c->window, c->width/2, c->height/2);
		} else if ((strcasecmp(keystr, config.keys->bottomleft)) == 0) {
			c->x = c->border;
			c->y = DisplayHeight(dpy, c->screen->screen)-c->height-c->border;
			move(c, 1);
			setmouse(c->window, c->width/2, c->height/2);
		} else if ((strcasecmp(keystr, config.keys->bottomright)) == 0) {
			c->x = DisplayWidth(dpy, c->screen->screen)-c->width-c->border;
			c->y = DisplayHeight(dpy, c->screen->screen)-c->height-c->border;
			move(c, 1);
			setmouse(c->window, c->width/2, c->height/2);
		}
	}
	if ((strcasecmp(keystr, config.keys->reload)) == 0) { 
		setconf();
	} else if ((strcasecmp(keystr, config.keys->next)) == 0) {
		next();
	} else if ((strcasecmp(keystr, config.keys->term)) == 0) { 
		spawn(config.global->term, config.global->term_args);
	} else if ((strcasecmp(keystr, config.keys->vddown)) == 0) {
		switch_vdesk_down();
	} else if ((strcasecmp(keystr, config.keys->vdup)) == 0) {
		switch_vdesk_up();
	} else if (key >= config.global->min_vdesk && key <= config.global->max_vdesks) {
		switch_vdesk(key);
	}
}

void handle_button_event(XButtonEvent *e) {
	Client *c = find_client(e->window);

	if (c && e->window != c->screen->root) {
		if (config.global->flip_resizelower == 0) {
			switch (e->button) {
				case Button1:
					move(c, 0); break;
				case Button2:
					XLowerWindow(dpy, c->parent); break;
				case Button3:
					resize(c, 0); break;
			}
		} else {
			switch (e->button) {
				case Button1:
					move(c, 0); break;
				case Button2:
					resize(c, 0); break;
				case Button3:
					XLowerWindow(dpy, c->parent); break;
			}
		}
	} else {
		/* if we are on the root window switch vdesks with the mouse wheel */
		switch (e->button) {
			case Button2:
				switch_vdesk(1);
				break;
			case Button4:
				switch_vdesk_up();
				break;
			case Button5:
				switch_vdesk_down();
				break;
		}
	}
}

void handle_configure_request(XConfigureRequestEvent *e) {
	Client *c = find_client(e->window);
	XWindowChanges wc;

	wc.sibling = e->above;
	wc.stack_mode = e->detail;
	if (c) {
		change_gravity(c,(opt_bw*-1));
		if (e->value_mask & CWWidth) c->width = e->width;
		if (e->value_mask & CWHeight) c->height = e->height;
		if (e->value_mask & CWX) c->x = e->x;
		if (e->value_mask & CWY) c->y = e->y;
		if (c->x == 0 && c->width >= DisplayWidth(dpy, c->screen->screen)) {
			c->x -= c->border;
		}
		if (c->y == 0 && c->height >= DisplayHeight(dpy, c->screen->screen)) {
			c->y -= c->border;
		}
		change_gravity(c,opt_bw);

		wc.x = c->x - c->border;
		wc.y = c->y - c->border;
		wc.width = c->width + (c->border*2);
		wc.height = c->height + (c->border*2);
		wc.border_width = 0;
		XConfigureWindow(dpy, c->parent, e->value_mask, &wc);
		send_config(c);
		#ifdef DEBUG
			bad_debug(1,"handle_configure_request() : window configured to %dx%d+%d+%d\n", wc.width, wc.height, wc.x, wc.y);
		#endif
	}

	wc.x = c ? c->border : e->x;
	wc.y = c ? c->border : e->y;
	wc.width = e->width;
	wc.height = e->height;
	XConfigureWindow(dpy, e->window, e->value_mask, &wc);
}

void handle_map_request(XMapRequestEvent *e) {
	Client *c = find_client(e->window);

	if (c) {
		if (c->vdesk != vdesk)
			switch_vdesk(c->vdesk);
		unhide(c, RAISE);
	} else {
		XWindowAttributes attr;
		#ifdef DEBUG
                	bad_debug(1,"don't know this window, calling make_new_client();\n");
		#endif
		XGetWindowAttributes(dpy, e->window, &attr);
		make_new_client(e->window, find_screen(attr.root));
	}
}

void handle_client_message(XClientMessageEvent *e) {
        Client *c = find_client(e->window);

        if (c && e->message_type == xa_wm_change_state &&
                e->format == 32 && e->data.l[0] == IconicState) hide(c);
}       

void handle_unmap_event(XUnmapEvent *e) {
	Client *c = find_client(e->window);

	if (c) {
		if (c->ignore_unmap) c->ignore_unmap--;
		else remove_client(c);
	}
}

void handle_shape_event(XShapeEvent *e) {
	Client *c = find_client(e->window);
	if (c)
		set_shape(c);
}

void handle_property_change(XPropertyEvent *e) {
	Client *c = find_client(e->window);
	long dummy;

	if (c) {
		if (e->atom == XA_WM_NORMAL_HINTS)
				XGetWMNormalHints(dpy, c->window, c->size, &dummy);
	}
}

void handle_enter_event(XCrossingEvent *e) {
	Client *c = find_client(e->window);
	if (c)
        	focus_client(c);
}


syntax highlighted by Code2HTML, v. 0.9.1