<commit_before>/*
 * (C)opyright MMIV-MMVI <NAME> <<EMAIL>>
 * See LICENSE file for license details.
 */

#include <stdlib.h>
#include <string.h>

#include "wm.h"

static Vector *
view2vector(ViewVector *vv)
{
	return (Vector *) vv;
}

View *
alloc_view(char *name)
{
	static unsigned short id = 1;
	View *v = cext_emallocz(sizeof(View));

	v->id = id++;
	cext_strlcpy(v->name, name, sizeof(v->name));
	alloc_area(v);
	alloc_area(v);
	sel = view.size;
	cext_vattach(view2vector(&view), v);
	return v;
}

void
destroy_view(View *v)
{
	while(v->area.size)
		destroy_area(v->area.data[0]);

	cext_vdetach(view2vector(&view), v);
	if(sel >= view.size)
		sel = 0;

	free(v);
}

int
view2index(View *v)
{
	int i;
	for(i = 0; i < view.size; i++)
		if(v == view.data[i])
			return i;
	return -1;
}

static void
update_frame_selectors(View *v)
{
	unsigned int i, j;

	/* select correct frames of clients */
	for(i = 0; i < client.size; i++)
		for(j = 0; j < client.data[i]->frame.size; j++)
			if(client.data[i]->frame.data[j]->area->view == v)
				client.data[i]->sel = j;
}

void
focus_view(View *v)
{
	Client *c;
	unsigned int i;

	/* cleanup other empty views */
	for(i = 0; i < view.size; i++)
		if(!hasclient(view.data[i])) {
			destroy_view(view.data[i]);
			i--;
		}

	XGrabServer(dpy);
	sel = view2index(v);

	cext_strlcpy(def.tag, v->name, sizeof(def.tag));

	update_frame_selectors(v);

	/* gives all(!) clients proper geometry (for use of different tags) */
	for(i = 0; i < client.size; i++)
		if(client.data[i]->frame.size) {
			Frame *f = client.data[i]->frame.data[client.data[i]->sel];
			if(f->area->view == v) {
				XMoveWindow(dpy, client.data[i]->framewin, f->rect.x, f->rect.y);
				if(client.data[i]->frame.size > 1)
					resize_client(client.data[i], &f->rect, False);
				draw_client(client.data[i]);
			}
			else
				XMoveWindow(dpy, client.data[i]->framewin,
						2 * rect.width + f->rect.x, f->rect.y);
		}
	if((c = sel_client_of_view(v)))
		focus_client(c);
	update_bar_tags();
	XSync(dpy, False);
	XUngrabServer(dpy);
}

XRectangle *
rectangles(View *v, Bool isfloat, unsigned int *num)
{
	XRectangle *result = nil;
	unsigned int i;

	*num = 0;
	if(isfloat)
		*num = v->area.data[0]->frame.size;
	else {
		for(i = 1; i < v->area.size; i++)
			*num += v->area.data[i]->frame.size;
	}

	if(*num) {
		result = cext_emallocz(*num * sizeof(XRectangle));
		if(isfloat) {
			for(i = 0; i < *num; i++)
				result[i] = v->area.data[0]->frame.data[0]->rect;
		}
		else {
			unsigned int j, n = 0;
			for(i = 1; i < v->area.size; i++) {
				for(j = 0; j < v->area.data[i]->frame.size; j++)
					result[n++] = v->area.data[i]->frame.data[j]->rect;
			}
		}
	}
	return result;
}

int
vid2index(unsigned short id)
{
	int i;
	for(i = 0; i < view.size; i++)
		if(view.data[i]->id == id)
			return i;
	return -1;
}

View *
name2view(char *name)
{
	View *v = nil;
	unsigned int i;

	for(i = 0; i < view.size; i++)
		if(!strncmp(view.data[i]->name, name, strlen(name)))
			return v;
	return nil;
}

View *
get_view(char *name)
{<commit_msg>fixed name2view bug<commit_after>/*
 * (C)opyright MMIV-MMVI <NAME> <<EMAIL>>
 * See LICENSE file for license details.
 */

#include <stdlib.h>
#include <string.h>

#include "wm.h"

static Vector *
view2vector(ViewVector *vv)
{
	return (Vector *) vv;
}

View *
alloc_view(char *name)
{
	static unsigned short id = 1;
	View *v = cext_emallocz(sizeof(View));

	v->id = id++;
	cext_strlcpy(v->name, name, sizeof(v->name));
	alloc_area(v);
	alloc_area(v);
	sel = view.size;
	cext_vattach(view2vector(&view), v);
	return v;
}

void
destroy_view(View *v)
{
	while(v->area.size)
		destroy_area(v->area.data[0]);

	cext_vdetach(view2vector(&view), v);
	if(sel >= view.size)
		sel = 0;

	free(v);
}

int
view2index(View *v)
{
	int i;
	for(i = 0; i < view.size; i++)
		if(v == view.data[i])
			return i;
	return -1;
}

static void
update_frame_selectors(View *v)
{
	unsigned int i, j;

	/* select correct frames of clients */
	for(i = 0; i < client.size; i++)
		for(j = 0; j < client.data[i]->frame.size; j++)
			if(client.data[i]->frame.data[j]->area->view == v)
				client.data[i]->sel = j;
}

void
focus_view(View *v)
{
	Client *c;
	unsigned int i;

	fprintf(stderr, "%d\n", view.size);
	/* cleanup other empty views */
	for(i = 0; i < view.size; i++)
		if(!hasclient(view.data[i])) {
			destroy_view(view.data[i]);
			i--;
		}

	XGrabServer(dpy);
	sel = view2index(v);

	cext_strlcpy(def.tag, v->name, sizeof(def.tag));

	update_frame_selectors(v);

	/* gives all(!) clients proper geometry (for use of different tags) */
	for(i = 0; i < client.size; i++)
		if(client.data[i]->frame.size) {
			Frame *f = client.data[i]->frame.data[client.data[i]->sel];
			if(f->area->view == v) {
				XMoveWindow(dpy, client.data[i]->framewin, f->rect.x, f->rect.y);
				if(client.data[i]->frame.size > 1)
					resize_client(client.data[i], &f->rect, False);
				draw_client(client.data[i]);
			}
			else
				XMoveWindow(dpy, client.data[i]->framewin,
						2 * rect.width + f->rect.x, f->rect.y);
		}
	if((c = sel_client_of_view(v)))
		focus_client(c);
	update_bar_tags();
	XSync(dpy, False);
	XUngrabServer(dpy);
}

XRectangle *
rectangles(View *v, Bool isfloat, unsigned int *num)
{
	XRectangle *result = nil;
	unsigned int i;

	*num = 0;
	if(isfloat)
		*num = v->area.data[0]->frame.size;
	else {
		for(i = 1; i < v->area.size; i++)
			*num += v->area.data[i]->frame.size;
	}

	if(*num) {
		result = cext_emallocz(*num * sizeof(XRectangle));
		if(isfloat) {
			for(i = 0; i < *num; i++)
				result[i] = v->area.data[0]->frame.data[0]->rect;
		}
		else {
			unsigned int j, n = 0;
			for(i = 1; i < v->area.size; i++) {
				for(j = 0; j < v->area.data[i]->frame.size; j++)
					result[n++] = v->area.data[i]->frame.data[j]->rect;
			}
		}
	}
	return result;
}

int
vid2index(unsigned short id)
{
	int i;
	for(i = 0; i < view.size; i++)
		if(view.data[i]->id == id)
			return i;
	return -1;
}

View *
name2view(char *name)
{
	unsigned int i;

	for(i = 0; i < view.size; i++)
		if(!strncmp(view.data[i]->name, name, strlen(name)))
			return view.data[i];
	return nil;
}

View *
get_view(char *name)
{
{'fraction_non_alphanumeric': 0.20418163998693237, 'fraction_numerical': 0.0099640640313623, 'mean_word_length': 3.4726077428780133, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 97, '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': '5672783', 'n_tokens_mistral': 2842, 'n_tokens_neox': 2616, 'n_words': 552}