<commit_before>/* (C)opyright MMIV-MMVI <NAME> <<EMAIL>beam at gmail dot com>
 * (C)opyright MMVI-MMVII <NAME> <<EMAIL>>
 * See LICENSE file for license details.
 */
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <X11/keysym.h>
#include "wmii.h"
#include "printevent.h"

uint
flush_masked_events(long event_mask) {
	XEvent ev;
	uint n = 0;
	while(XCheckMaskEvent(blz.dpy, event_mask, &ev)) n++;
	return n;
}

static void
buttonrelease(XEvent *e) {
	Frame *f;
	Bar *b;
	XButtonPressedEvent *ev = &e->xbutton;
	if(ev->window == screen->barwin) {
		for(b=screen->lbar; b; b=b->next)
			if(ispointinrect(ev->x, ev->y, &b->brush.rect)) {
				write_event("LeftBarClick %d %s\n",
						ev->button, b->name);
				return;
			}
		for(b=screen->rbar; b; b=b->next)
			if(ispointinrect(ev->x, ev->y, &b->brush.rect)) {
				write_event("RightBarClick %d %s\n",
						ev->button, b->name);
				return;
			}
	}
	else if((f = frame_of_win(ev->window)))
		write_event("ClientClick 0x%x %d\n", f->client->win, ev->button);
}

static void
buttonpress(XEvent *e) {
	Frame *f;
	Bool inclient;
	XButtonPressedEvent *ev;

	ev = &e->xbutton;
	if((f = frame_of_win(ev->window))) {
		inclient = (ev->subwindow == f->client->win);
		if((ev->state & def.mod) == def.mod) {
			switch(ev->button) {
			case Button1:
				do_mouse_resize(f->client, False, CENTER);
				focus(f->client, True);
				frame_to_top(f);
				focus(f->client, True);
				break;
			case Button3:
				do_mouse_resize(f->client, False,
						quadofcoord(&f->rect, ev->x_root, ev->y_root));
				frame_to_top(f);
				focus(f->client, True);
				break;
			default: break;
				XAllowEvents(blz.dpy, ReplayPointer, ev->time);
			}
		}else{
			if(ev->button == Button1) {
				if(frame_to_top(f))
					restack_view(f->view);
				if(ispointinrect(ev->x, ev->y, &f->grabbox))
					do_mouse_resize(f->client, True, CENTER);
				else if(!ev->subwindow
				&& !ispointinrect(ev->x, ev->y, &f->titlebar))
					do_mouse_resize(f->client, False,
						quadofcoord(&f->rect, ev->x_root, ev->y_root));
				if(f->client != sel_client())
					focus(f->client, True);
			}
			if(ev->subwindow)
				XAllowEvents(blz.dpy, ReplayPointer, ev->time);
			else {
				XUngrabPointer(blz.dpy, ev->time);
				XSync(blz.dpy, False);
				write_event("ClientMouseDown 0x%x %d\n", f->client->win, ev->button);
			}
		}
	}else
		XAllowEvents(blz.dpy, ReplayPointer, ev->time);
}

static void
configurerequest(XEvent *e) {
	XConfigureRequestEvent *ev = &e->xconfigurerequest;
	XWindowChanges wc;
	XRectangle *frect;
	Client *c;
	Frame *f;

	c = client_of_win(ev->window);

	if(c) {
		f = c->sel;
		gravitate_client(c, True);
		if(ev->value_mask & CWX)
			c->rect.x = ev->x;
		if(ev->value_mask & CWY)
			c->rect.y = ev->y;
		if(ev->value_mask & CWWidth)
			c->rect.width = ev->width;
		if(ev->value_mask & CWHeight)
			c->rect.height = ev->height;
		if(ev->value_mask & CWBorderWidth)
			c->border = ev->border_width;
		gravitate_client(c, False);

		if((c->rect.height == screen->rect.height)
		&&(c->rect.width == screen->rect.width)) {
			c->fullscreen = True;
			if(c->sel) {
				if(!c->sel->area->floating)
					send_to_area(c->sel->view->area, c->sel);
				focus_client(c);
				restack_view(c->sel->view);
			}
		}

		if(c->sel->area->floating)
			frect=&c->sel->rect;
		else
			frect=&c->sel->revert;

		*frect = c->rect;
		frect->y -= labelh(&def.font);
		frect->x -= def.border;
		frect->width += 2 * def.border;
		frect->height += frame_delta_h();

		if(c->sel->area->floating || c->fullscreen)
			resize_client(c, frect);
		else
			configure_client(c);
	}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;
		ev->value_mask &= ~(CWStackMode|CWSibling);
		XConfigureWindow(blz.dpy, ev->window, ev->value_mask, &wc);
		XSync(blz.dpy, False);
	}
}

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

	if((c = client_of_win(ev->window)))
		destroy_client(c);
}

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

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

	if((c = client_of_win(ev->window))) {
		if(ev->detail != NotifyInferior && screen->focus != c) {
			if(verbose)
				fprintf(stderr, "enter_notify(c) => %s\n", c->name);
			focus(c, False);
			set_cursor(c, cursor[CurNormal]);
		}else if(verbose)
				fprintf(stderr, "enter_notify(c[NotifyInferior]) => %s\n", c->name);
	}
	else if((f = frame_of_win(ev->window)) && screen->focus != c) {
		if(verbose)
			fprintf(stderr, "enter_notify(f) => %s\n", f->client->name);
		if(f->area->floating || !f->collapsed)
			focus(f->client, False);
		set_frame_cursor(f, ev->x, ev->y);
	}
	else if(ev->window == blz.root) {
		sel_screen = True;
		draw_frames();
	}
}

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

	if((ev->window == blz.root) && !ev->same_screen) {
		sel_screen = True;
		draw_frames();
	}
}

void
print_focus(Client *c, char *to) {
		if(verbose) {
			fprintf(stderr, "screen->focus: %p => %p\n",
				screen->focus, c);
			fprintf(stderr, "\t%s => %s\n",
				screen->focus ? screen->focus->name : "<nil>",
				to);
		}<commit_msg>Fix some resize cursor bugs.<commit_after>/* (C)opyright MMIV-MMVI <NAME> <<EMAIL>>
 * (C)opyright MMVI-MMVII <NAME> <<EMAIL>>
 * See LICENSE file for license details.
 */
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <X11/keysym.h>
#include "wmii.h"
#include "printevent.h"

uint
flush_masked_events(long event_mask) {
	XEvent ev;
	uint n = 0;
	while(XCheckMaskEvent(blz.dpy, event_mask, &ev)) n++;
	return n;
}

static void
buttonrelease(XEvent *e) {
	Frame *f;
	Bar *b;
	XButtonPressedEvent *ev = &e->xbutton;
	if(ev->window == screen->barwin) {
		for(b=screen->lbar; b; b=b->next)
			if(ispointinrect(ev->x, ev->y, &b->brush.rect)) {
				write_event("LeftBarClick %d %s\n",
						ev->button, b->name);
				return;
			}
		for(b=screen->rbar; b; b=b->next)
			if(ispointinrect(ev->x, ev->y, &b->brush.rect)) {
				write_event("RightBarClick %d %s\n",
						ev->button, b->name);
				return;
			}
	}
	else if((f = frame_of_win(ev->window)))
		write_event("ClientClick 0x%x %d\n", f->client->win, ev->button);
}

static void
buttonpress(XEvent *e) {
	Frame *f;
	Bool inclient;
	XButtonPressedEvent *ev;

	ev = &e->xbutton;
	if((f = frame_of_win(ev->window))) {
		inclient = (ev->subwindow == f->client->win);
		if((ev->state & def.mod) == def.mod) {
			switch(ev->button) {
			case Button1:
				do_mouse_resize(f->client, False, CENTER);
				focus(f->client, True);
				frame_to_top(f);
				focus(f->client, True);
				break;
			case Button3:
				do_mouse_resize(f->client, False,
						quadofcoord(&f->rect, ev->x_root, ev->y_root));
				frame_to_top(f);
				focus(f->client, True);
				break;
			default: break;
				XAllowEvents(blz.dpy, ReplayPointer, ev->time);
			}
		}else{
			if(ev->button == Button1) {
				if(frame_to_top(f))
					restack_view(f->view);
				if(ispointinrect(ev->x, ev->y, &f->grabbox))
					do_mouse_resize(f->client, True, CENTER);
				else if(!ev->subwindow
				&& !ispointinrect(ev->x, ev->y, &f->titlebar))
					do_mouse_resize(f->client, False,
						quadofcoord(&f->rect, ev->x_root, ev->y_root));
				if(f->client != sel_client())
					focus(f->client, True);
			}
			if(ev->subwindow)
				XAllowEvents(blz.dpy, ReplayPointer, ev->time);
			else {
				XUngrabPointer(blz.dpy, ev->time);
				XSync(blz.dpy, False);
				write_event("ClientMouseDown 0x%x %d\n", f->client->win, ev->button);
			}
		}
	}else
		XAllowEvents(blz.dpy, ReplayPointer, ev->time);
}

static void
configurerequest(XEvent *e) {
	XConfigureRequestEvent *ev = &e->xconfigurerequest;
	XWindowChanges wc;
	XRectangle *frect;
	Client *c;
	Frame *f;

	c = client_of_win(ev->window);

	if(c) {
		f = c->sel;
		gravitate_client(c, True);
		if(ev->value_mask & CWX)
			c->rect.x = ev->x;
		if(ev->value_mask & CWY)
			c->rect.y = ev->y;
		if(ev->value_mask & CWWidth)
			c->rect.width = ev->width;
		if(ev->value_mask & CWHeight)
			c->rect.height = ev->height;
		if(ev->value_mask & CWBorderWidth)
			c->border = ev->border_width;
		gravitate_client(c, False);

		if((c->rect.height == screen->rect.height)
		&&(c->rect.width == screen->rect.width)) {
			c->fullscreen = True;
			if(c->sel) {
				if(!c->sel->area->floating)
					send_to_area(c->sel->view->area, c->sel);
				focus_client(c);
				restack_view(c->sel->view);
			}
		}

		if(c->sel->area->floating)
			frect=&c->sel->rect;
		else
			frect=&c->sel->revert;

		*frect = c->rect;
		frect->y -= labelh(&def.font);
		frect->x -= def.border;
		frect->width += 2 * def.border;
		frect->height += frame_delta_h();

		if(c->sel->area->floating || c->fullscreen)
			resize_client(c, frect);
		else
			configure_client(c);
	}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;
		ev->value_mask &= ~(CWStackMode|CWSibling);
		XConfigureWindow(blz.dpy, ev->window, ev->value_mask, &wc);
		XSync(blz.dpy, False);
	}
}

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

	if((c = client_of_win(ev->window)))
		destroy_client(c);
}

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

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

	if((c = client_of_win(ev->window))) {
		if(ev->detail != NotifyInferior) {
			if(screen->focus != c) {
				if(verbose)
					fprintf(stderr, "enter_notify(c) => %s\n", c->name);
				focus(c, False);
			}
			set_cursor(c, cursor[CurNormal]);
		}else if(verbose)
				fprintf(stderr, "enter_notify(c[NotifyInferior]) => %s\n", c->name);
	}
	else if((f = frame_of_win(ev->window))) {
		if(screen->focus != c) {
			if(verbose)
				fprintf(stderr, "enter_notify(f) => %s\n", f->client->name);
			if(f->area->floating || !f->collapsed)
				focus(f->client, False);
		}
		set_frame_cursor(f, ev->x, ev->y);
	}
	else if(ev->window == blz.root) {
		sel_screen = True;
		draw_frames();
	}
}

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

	if((ev->window == blz.root) && !ev->same_screen) {
		sel_screen = True;
		draw_frames();
	}
}

void
print_focus(Client *c, char *to) {
		if(verbose) {
			fprintf(stderr, "screen->focus: %p => %p\n",
				screen->focus, c);
			fprintf(stderr, "\t%s => %s\n",
				screen->focus ? screen->focus->name : "<nil>",
				to);
		}
{'fraction_non_alphanumeric': 0.19882653544052237, 'fraction_numerical': 0.0017034162960158986, 'mean_word_length': 3.588797221016066, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 329, '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': '4562240', 'n_tokens_mistral': 4964, 'n_tokens_neox': 4379, 'n_words': 766}