<commit_before>#include <string.h>
#include <xcb/xcb_event.h>
#include "tfwm.h"
#include "list.h"
#include "events.h"

static void  buttonpress(xcb_generic_event_t *ev);
static void  circulaterequest(xcb_generic_event_t *ev);
static void  clientmessage(xcb_generic_event_t *ev);
static void  configurerequest(xcb_generic_event_t *ev);
static void  destroynotify(xcb_generic_event_t *ev);
static void  enternotify(xcb_generic_event_t *ev);
static void  keypress(xcb_generic_event_t *ev);
static void  mappingnotify(xcb_generic_event_t *ev);
static void  maprequest(xcb_generic_event_t *ev);
static void  requesterror(xcb_generic_event_t *ev);
static void  unmapnotify(xcb_generic_event_t *ev);
#ifdef DEBUG
static char *get_atom_name(xcb_atom_t atom);
#endif

void
handleevent(xcb_generic_event_t *ev) {
	switch (ev->response_type & ~0x80) {
		case XCB_BUTTON_PRESS:
			buttonpress(ev);
			break;
		case XCB_CIRCULATE_REQUEST:
			circulaterequest(ev);
			break;
		case XCB_CLIENT_MESSAGE:
			clientmessage(ev);
			break;
		case XCB_CONFIGURE_REQUEST:
			configurerequest(ev);
			break;
		case XCB_DESTROY_NOTIFY:
			destroynotify(ev);
			break;
		case XCB_ENTER_NOTIFY:
			enternotify(ev);
			break;
		case XCB_KEY_PRESS:
			keypress(ev);
			break;
		case XCB_MAPPING_NOTIFY:
			mappingnotify(ev);
			break;
		case XCB_MAP_REQUEST:
			maprequest(ev);
			break;
		case XCB_UNMAP_NOTIFY:
			unmapnotify(ev);
			break;
		case 0:
			requesterror(ev);
			break;
	}
}

void
buttonpress(xcb_generic_event_t *ev) {
	xcb_button_press_event_t *e = (xcb_button_press_event_t*)ev;
	Client *c;

	PRINTF("Event: button press: %#x\n", e->event);

	if ((c = wintoclient(e->event))) {
		if (c->win != sel->win) {
			focus(c);
		}
	}
	for (unsigned int i = 0; i < LENGTH(buttons); i++)
		if (buttons[i].button == e->detail &&
			CLEANMASK(buttons[i].mask) == CLEANMASK(e->state) &&
			buttons[i].func)
			if (sel != NULL && e->event != screen->root)
				buttons[i].func(&buttons[i].arg);
}

void
circulaterequest(xcb_generic_event_t *ev) {
	xcb_circulate_request_event_t *e = (xcb_circulate_request_event_t *)ev;
	PRINTF("Event: circulate request: %#x\n", e->window);
	xcb_circulate_window(conn, e->window, e->place);
}

void
clientmessage(xcb_generic_event_t *ev) {
	xcb_client_message_event_t *e = (xcb_client_message_event_t*)ev;
	Client *c;

#ifdef DEBUG
	char *name;
	name = get_atom_name(e->type);
	PRINTF("Event: client message win %#x, atom %s (%u)\n", e->window, name, e->type);
	free(name);
#endif

	if (!(c = wintoclient(e->window)))
		return;

	if (e->type == ewmh->_NET_WM_STATE) {
		if (e->data.data32[1] == ewmh->_NET_WM_STATE_FULLSCREEN ||
			e->data.data32[2] == ewmh->_NET_WM_STATE_FULLSCREEN) {
			if (e->data.data32[0] == XCB_EWMH_WM_STATE_ADD)
				maximizeclient(c, true);
			else if (e->data.data32[0] == XCB_EWMH_WM_STATE_REMOVE)
				maximizeclient(c, false);
			else if (e->data.data32[0] == XCB_EWMH_WM_STATE_TOGGLE)
				maximizeclient(c, !c->ismax);
		}
	} else if (e->type == ewmh->_NET_ACTIVE_WINDOW) {
		focus(c);
	} else if (e->type == ewmh->_NET_WM_DESKTOP) {
	} else if (e->type == ewmh->_NET_MOVERESIZE_WINDOW) {
	} else if (e->type == ewmh->_NET_CLOSE_WINDOW) {
		unmanage(c);
	}
}

#ifdef DEBUG
char *
get_atom_name(xcb_atom_t atom) {
	char *name = NULL;
	xcb_get_atom_name_reply_t *reply;
	int len;

	reply = xcb_get_atom_name_reply(conn, xcb_get_atom_name(conn, atom), NULL);
	if (!reply)
		return NULL;

	len = xcb_get_atom_name_name_length(reply);
	if (len) {
		if (!(name = malloc(len + 1)))
			err("can't allocate memory.");
		memcpy(name, xcb_get_atom_name_name(reply), len);
		name[len] = '\0';
	}

	free(reply);

	return name;
}
#endif

void
configurerequest(xcb_generic_event_t *ev) {
	xcb_configure_request_event_t *e = (xcb_configure_request_event_t *)ev;
	Client *c;
	unsigned int v[7];
	int i = 0;

	PRINTF("Event: configure request x %d y %d w %d h %d bw %d: %#x\n",
		   e->x, e->y, e->width, e->height, e->border_width, e->window);
	if ((c = wintoclient(e->window))) {
		if (e->value_mask & XCB_CONFIG_WINDOW_WIDTH)
			if (!c->ismax && !c->ishormax)
				v[i++] = c->geom.width = MIN(e->width, screen->width_in_pixels-2*BORDER_WIDTH);
		if (e->value_mask & XCB_CONFIG_WINDOW_HEIGHT)
			if (!c->ismax && !c->isvertmax)
				v[i++] = c->geom.height = MIN(e->height, screen->height_in_pixels-2*BORDER_WIDTH);<commit_msg>better configure request debug prints<commit_after>#include <string.h>
#include <xcb/xcb_event.h>
#include "tfwm.h"
#include "list.h"
#include "events.h"

static void  buttonpress(xcb_generic_event_t *ev);
static void  circulaterequest(xcb_generic_event_t *ev);
static void  clientmessage(xcb_generic_event_t *ev);
static void  configurerequest(xcb_generic_event_t *ev);
static void  destroynotify(xcb_generic_event_t *ev);
static void  enternotify(xcb_generic_event_t *ev);
static void  keypress(xcb_generic_event_t *ev);
static void  mappingnotify(xcb_generic_event_t *ev);
static void  maprequest(xcb_generic_event_t *ev);
static void  requesterror(xcb_generic_event_t *ev);
static void  unmapnotify(xcb_generic_event_t *ev);
#ifdef DEBUG
static char *get_atom_name(xcb_atom_t atom);
#endif

void
handleevent(xcb_generic_event_t *ev) {
	switch (ev->response_type & ~0x80) {
		case XCB_BUTTON_PRESS:
			buttonpress(ev);
			break;
		case XCB_CIRCULATE_REQUEST:
			circulaterequest(ev);
			break;
		case XCB_CLIENT_MESSAGE:
			clientmessage(ev);
			break;
		case XCB_CONFIGURE_REQUEST:
			configurerequest(ev);
			break;
		case XCB_DESTROY_NOTIFY:
			destroynotify(ev);
			break;
		case XCB_ENTER_NOTIFY:
			enternotify(ev);
			break;
		case XCB_KEY_PRESS:
			keypress(ev);
			break;
		case XCB_MAPPING_NOTIFY:
			mappingnotify(ev);
			break;
		case XCB_MAP_REQUEST:
			maprequest(ev);
			break;
		case XCB_UNMAP_NOTIFY:
			unmapnotify(ev);
			break;
		case 0:
			requesterror(ev);
			break;
	}
}

void
buttonpress(xcb_generic_event_t *ev) {
	xcb_button_press_event_t *e = (xcb_button_press_event_t*)ev;
	Client *c;

	PRINTF("Event: button press: %#x\n", e->event);

	if ((c = wintoclient(e->event))) {
		if (c->win != sel->win) {
			focus(c);
		}
	}
	for (unsigned int i = 0; i < LENGTH(buttons); i++)
		if (buttons[i].button == e->detail &&
			CLEANMASK(buttons[i].mask) == CLEANMASK(e->state) &&
			buttons[i].func)
			if (sel != NULL && e->event != screen->root)
				buttons[i].func(&buttons[i].arg);
}

void
circulaterequest(xcb_generic_event_t *ev) {
	xcb_circulate_request_event_t *e = (xcb_circulate_request_event_t *)ev;
	PRINTF("Event: circulate request: %#x\n", e->window);
	xcb_circulate_window(conn, e->window, e->place);
}

void
clientmessage(xcb_generic_event_t *ev) {
	xcb_client_message_event_t *e = (xcb_client_message_event_t*)ev;
	Client *c;

#ifdef DEBUG
	char *name;
	name = get_atom_name(e->type);
	PRINTF("Event: client message win %#x, atom %s (%u)\n", e->window, name, e->type);
	free(name);
#endif

	if (!(c = wintoclient(e->window)))
		return;

	if (e->type == ewmh->_NET_WM_STATE) {
		if (e->data.data32[1] == ewmh->_NET_WM_STATE_FULLSCREEN ||
			e->data.data32[2] == ewmh->_NET_WM_STATE_FULLSCREEN) {
			if (e->data.data32[0] == XCB_EWMH_WM_STATE_ADD)
				maximizeclient(c, true);
			else if (e->data.data32[0] == XCB_EWMH_WM_STATE_REMOVE)
				maximizeclient(c, false);
			else if (e->data.data32[0] == XCB_EWMH_WM_STATE_TOGGLE)
				maximizeclient(c, !c->ismax);
		}
	} else if (e->type == ewmh->_NET_ACTIVE_WINDOW) {
		focus(c);
	} else if (e->type == ewmh->_NET_WM_DESKTOP) {
	} else if (e->type == ewmh->_NET_MOVERESIZE_WINDOW) {
	} else if (e->type == ewmh->_NET_CLOSE_WINDOW) {
		unmanage(c);
	}
}

#ifdef DEBUG
char *
get_atom_name(xcb_atom_t atom) {
	char *name = NULL;
	xcb_get_atom_name_reply_t *reply;
	int len;

	reply = xcb_get_atom_name_reply(conn, xcb_get_atom_name(conn, atom), NULL);
	if (!reply)
		return NULL;

	len = xcb_get_atom_name_name_length(reply);
	if (len) {
		if (!(name = malloc(len + 1)))
			err("can't allocate memory.");
		memcpy(name, xcb_get_atom_name_name(reply), len);
		name[len] = '\0';
	}

	free(reply);

	return name;
}
#endif

void
configurerequest(xcb_generic_event_t *ev) {
	xcb_configure_request_event_t *e = (xcb_configure_request_event_t *)ev;
	Client *c;
	unsigned int v[7];
	int i = 0;

#ifdef DEBUG
	PRINTF("Event: configure request: win %#x: ", e->window);
	if (e->value_mask & XCB_CONFIG_WINDOW_X)
		PRINTF("x: %d ", e->x);
	if (e->value_mask & XCB_CONFIG_WINDOW_Y)
		PRINTF("y: %d ", e->y);
	if (e->value_mask & XCB_CONFIG_WINDOW_WIDTH)
		PRINTF("w: %u ", e->width);
	if (e->value_mask & XCB_CONFIG_WINDOW_HEIGHT)
		PRINTF("h: %u ", e->height);
	if (e->value_mask & XCB_CONFIG_WINDOW_BORDER_WIDTH)
		PRINTF("border: %u ", e->border_width);
	if (e->value_mask & XCB_CONFIG_WINDOW_SIBLING)
		PRINTF("sibling: %#x ", e->sibling);
	if (e->value_mask & XCB_CONFIG_WINDOW_STACK_MODE)
		PRINTF("stack mode: %u", e->stack_mode);
	PRINTF("\n");
#endif

	if ((c = wintoclient(e->window))) {
		if (e->value_mask & XCB_CONFIG_WINDOW_WIDTH)
			if (!c->ismax && !c->ishormax)
				v[i++] = c->geom.width = MIN(e->width, screen->width_in_pixels-2*BORDER_WIDTH);
		if (e->value_mask & XCB_CONFIG_WINDOW_HEIGHT)
			if (!c->ismax && !c->isvertmax)
				v[i++] = c->geom.height = MIN(e->height, screen->height_in_pixels-2*BORDER_WIDTH);
{'fraction_non_alphanumeric': 0.15294753921038398, 'fraction_numerical': 0.00562466197944835, 'mean_word_length': 4.923126201153107, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 122, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6824121', 'n_tokens_mistral': 4163, 'n_tokens_neox': 3863, 'n_words': 714}