// File: /workspace/github_c_makefile_star2_O1/tudurom[P]windowchef/wm.c
// Function: ipc_window_cardinal_focus
static void
cardinal_focus(uint32_t dir)
{
	/* Don't focus if we don't have a current focus! */
	if (focused_win == NULL)
		return;

	struct list_item *valid_windows = NULL;
	struct list_item *desired_window = NULL;
	struct list_item *valid_window;
	struct list_item *win;

	struct win_position focus_win_pos = get_window_position(CENTER, focused_win);

	float closest_distance = -1;

	win = win_list;

	while(win != NULL) {
		/* Skip focused window */
		if (((struct client *)win->data)->window == focused_win->window) {
			win = win->next;
			continue;
		}

		/* Skip unmapped windows */
		if (!((struct client *)win->data)->mapped) {
			win = win->next;
			continue;
		}

		struct win_position win_pos = get_window_position(CENTER, (struct client *)win->data);

		valid_window = NULL;

		switch (dir) {
			case NORTH:
				if (win_pos.y < focus_win_pos.y)
					valid_window = list_add_item(&valid_windows);
				break;
			case SOUTH:
				if (win_pos.y >= focus_win_pos.y)
					valid_window = list_add_item(&valid_windows);
				break;
			case WEST:
				if (win_pos.x < focus_win_pos.x)
					valid_window = list_add_item(&valid_windows);
				break;
			case EAST:
				if (win_pos.x >= focus_win_pos.x)
					valid_window = list_add_item(&valid_windows);
				break;
		}

		if (valid_window != NULL)
			valid_window->data = win->data;

		win = win->next;
	}

	win = valid_windows;
	while(win != NULL) {
		float cur_distance;
		float cur_angle;

		cur_distance = get_distance_between_windows(focused_win, (struct client *)win->data);
		cur_angle = get_angle_between_windows(focused_win, (struct client *)win->data);

		if (is_in_valid_direction(dir, cur_angle, 10)) {
			if (is_overlapping(focused_win, (struct client *)win->data))
				cur_distance = cur_distance * 0.1;
			cur_distance = cur_distance * 0.80;
		}
		else if (is_in_valid_direction(dir, cur_angle, 25)) {
			if (is_overlapping(focused_win, (struct client *)win->data))
				cur_distance = cur_distance * 0.1;
			cur_distance = cur_distance * 0.85;
		}
		else if (is_in_valid_direction(dir, cur_angle, 35)) {
			if (is_overlapping(focused_win, (struct client *)win->data))
				cur_distance = cur_distance * 0.1;
			cur_distance = cur_distance * 0.9;
		}
		else if (is_in_valid_direction(dir, cur_angle, 50)) {
			if (is_overlapping(focused_win, (struct client *)win->data))
				cur_distance = cur_distance * 0.1;
			cur_distance = cur_distance * 3;
		}
		else {
			win = win->next;
			continue;
		}

		if (is_in_cardinal_direction(dir, focused_win, (struct client *)win->data))
			cur_distance = cur_distance * 0.9;


		if (closest_distance == -1 || (cur_distance < closest_distance)) {
			closest_distance = cur_distance;
			desired_window = win;
		}

		win = win->next;
	}

	if (desired_window != NULL)
		set_focused(desired_window->data);

	if (valid_windows != NULL)
		list_delete_all_items(&valid_windows, false);
}

// Assembly:
movq 0x513b(%rip), %rax      # 0xc7e0
testq %rax, %rax
je 0x7aee
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movl (%rdi), %ebx
movq $0x0, 0x8(%rsp)
movq 0x4cfa(%rip), %r15      # 0xc3c8
testq %r15, %r15
je 0x7779
movzwl 0xa(%rax), %ebp
shrl %ebp
addw 0x6(%rax), %bp
movzwl 0x8(%rax), %r12d
shrl %r12d
addw 0x4(%rax), %r12w
shll $0x10, %r12d
movl %ebx, %r13d
leaq 0x1950(%rip), %r14      # 0x904c
movq (%r15), %rax
movl (%rax), %ecx
movq 0x50d8(%rip), %rdx      # 0xc7e0
cmpl (%rdx), %ecx
je 0x7767
cmpb $0x1, 0x54(%rax)
jne 0x7767
cmpl $0x3, %ebx
ja 0x7746
movzwl 0x8(%rax), %edx
shrl %edx
addw 0x4(%rax), %dx
movzwl 0xa(%rax), %ecx
shrl %ecx
addw 0x6(%rax), %cx
movzwl %dx, %eax
movslq (%r14,%r13,4), %rdx
addq %r14, %rdx
jmpq *%rdx
cmpw %bp, %cx
jge 0x7746
jmp 0x7752
shll $0x10, %eax
cmpl %r12d, %eax
jge 0x7752
xorl %eax, %eax
jmp 0x775c
shll $0x10, %eax
cmpl %r12d, %eax
jge 0x7746
leaq 0x8(%rsp), %rdi
callq 0x85d2
testq %rax, %rax
je 0x7767
movq (%r15), %rcx
movq %rcx, (%rax)
movq 0x10(%r15), %r15
testq %r15, %r15
jne 0x76fc
jmp 0x7779
cmpw %bp, %cx
jl 0x7746
jmp 0x7752
movq 0x8(%rsp), %r15
testq %r15, %r15
je 0x7a9b
movss 0x194d(%rip), %xmm0     # 0x90dc
xorl %r14d, %r14d
leal -0x2(%rbx), %ebp
movaps %xmm0, 0x10(%rsp)
movq 0x503f(%rip), %rax      # 0xc7e0
movq (%r15), %rcx
movzwl 0x8(%rax), %edx
shrl %edx
addw 0x4(%rax), %dx
movzwl 0xa(%rax), %esi
shrl %esi
addw 0x6(%rax), %si
movzwl 0x8(%rcx), %eax
shrl %eax
addw 0x4(%rcx), %ax
movzwl 0xa(%rcx), %edi
shrl %edi
cwtl
movswl %dx, %edx
subl %edx, %eax
xorps %xmm0, %xmm0
cvtsi2ss %eax, %xmm0
addw 0x6(%rcx), %di
movswl %di, %eax
movswl %si, %ecx
subl %ecx, %eax
xorps %xmm1, %xmm1
cvtsi2ss %eax, %xmm1
callq 0x3670
movaps %xmm0, 0x20(%rsp)
movq 0x4fe7(%rip), %rdx      # 0xc7e0
movq (%r15), %rdi
movzwl 0x8(%rdx), %eax
shrl %eax
addw 0x4(%rdx), %ax
movzwl 0xa(%rdx), %ecx
shrl %ecx
addw 0x6(%rdx), %cx
movzwl 0x8(%rdi), %edx
shrl %edx
addw 0x4(%rdi), %dx
movzwl 0xa(%rdi), %esi
shrl %esi
addw 0x6(%rdi), %si
cmpw %ax, %dx
jne 0x7831
xorps %xmm0, %xmm0
cmpw %cx, %si
je 0x7862
movswl %cx, %ecx
movswl %si, %esi
subl %ecx, %esi
cwtl
movswl %dx, %ecx
xorps %xmm1, %xmm1
cvtsi2ss %esi, %xmm1
subl %eax, %ecx
xorps %xmm0, %xmm0
cvtsi2ss %ecx, %xmm0
callq 0x3270
cvtss2sd %xmm0, %xmm0
mulsd 0x18aa(%rip), %xmm0     # 0x9108
cvtsd2ss %xmm0, %xmm0
movl %ebx, %edi
movss %xmm0, 0x4(%rsp)
movss 0x186e(%rip), %xmm1     # 0x90e0
callq 0x8125
testb %al, %al
je 0x78c2
movq 0x4f5e(%rip), %rdi      # 0xc7e0
movq (%r15), %rsi
callq 0x81ab
movaps 0x20(%rsp), %xmm1
xorps %xmm0, %xmm0
cvtss2sd %xmm1, %xmm0
mulsd 0x1872(%rip), %xmm0     # 0x9110
testb %al, %al
je 0x78a9
xorps %xmm1, %xmm1
cvtsd2ss %xmm0, %xmm1
movaps 0x10(%rsp), %xmm2
xorps %xmm0, %xmm0
cvtss2sd %xmm1, %xmm0
mulsd 0x186b(%rip), %xmm0     # 0x9128
jmp 0x797e
movl %ebx, %edi
movss 0x4(%rsp), %xmm0
movss 0x1812(%rip), %xmm1     # 0x90e4
callq 0x8125
testb %al, %al
je 0x791f
movq 0x4efe(%rip), %rdi      # 0xc7e0
movq (%r15), %rsi
callq 0x81ab
movaps 0x20(%rsp), %xmm1
xorps %xmm0, %xmm0
cvtss2sd %xmm1, %xmm0
mulsd 0x1812(%rip), %xmm0     # 0x9110
testb %al, %al
je 0x7909
xorps %xmm1, %xmm1
cvtsd2ss %xmm0, %xmm1
movaps 0x10(%rsp), %xmm2
xorps %xmm0, %xmm0
cvtss2sd %xmm1, %xmm0
mulsd 0x1803(%rip), %xmm0     # 0x9120
jmp 0x797e
movl %ebx, %edi
movss 0x4(%rsp), %xmm0
movss 0x17b9(%rip), %xmm1     # 0x90e8
callq 0x8125
testb %al, %al
je 0x7a3b
movq 0x4e9d(%rip), %rdi      # 0xc7e0
movq (%r15), %rsi
callq 0x81ab
movaps 0x20(%rsp), %xmm1
xorps %xmm0, %xmm0
cvtss2sd %xmm1, %xmm0
mulsd 0x17b1(%rip), %xmm0     # 0x9110
testb %al, %al
je 0x796a
xorps %xmm1, %xmm1
cvtsd2ss %xmm0, %xmm1
movaps 0x10(%rsp), %xmm2
xorps %xmm0, %xmm0
cvtss2sd %xmm1, %xmm0
mulsd 0x179a(%rip), %xmm0     # 0x9118
cvtsd2ss %xmm0, %xmm4
movq 0x4e57(%rip), %rax      # 0xc7e0
movq (%r15), %rcx
cmpl $0x2, %ebp
jb 0x79c0
cmpl $0x1, %ebx
ja 0x7a37
movl 0x4(%rax), %edx
movzwl 0x8(%rcx), %esi
shrl %esi
addw 0x4(%rcx), %si
movzwl 0x8(%rax), %eax
addw %dx, %ax
shll $0x10, %edx
shll $0x10, %esi
cmpl %esi, %edx
setle %cl
shll $0x10, %eax
cmpl %esi, %eax
jmp 0x79de
movzwl 0xa(%rcx), %edx
shrl %edx
addw 0x6(%rcx), %dx
movzwl 0x6(%rax), %ecx
movzwl 0xa(%rax), %eax
addw %cx, %ax
cmpw %dx, %cx
setle %cl
cmpw %dx, %ax
setge %al
andb %cl, %al
xorps %xmm0, %xmm0
cvtss2sd %xmm4, %xmm0
mulsd 0x1726(%rip), %xmm0     # 0x9118
testb %al, %al
je 0x79fd
xorps %xmm4, %xmm4
cvtsd2ss %xmm0, %xmm4
movaps %xmm4, %xmm0
cmpltps %xmm2, %xmm0
movaps %xmm2, %xmm1
movss 0x16cd(%rip), %xmm3     # 0x90dc
cmpeqps %xmm3, %xmm1
orps %xmm0, %xmm1
movd %xmm1, %eax
testb $0x1, %al
jne 0x7a21
movaps %xmm2, %xmm4
cmovneq %r15, %r14
movaps %xmm4, %xmm0
movq 0x10(%r15), %r15
testq %r15, %r15
jne 0x7795
jmp 0x7a9e
xorl %eax, %eax
jmp 0x79e3
movl %ebx, %edi
movss 0x4(%rsp), %xmm0
movss 0x16a1(%rip), %xmm1     # 0x90ec
callq 0x8125
testb %al, %al
je 0x7a94
movq 0x4d85(%rip), %rdi      # 0xc7e0
movq (%r15), %rsi
callq 0x81ab
movaps 0x20(%rsp), %xmm4
xorps %xmm0, %xmm0
cvtss2sd %xmm4, %xmm0
mulsd 0x1699(%rip), %xmm0     # 0x9110
testb %al, %al
je 0x7a82
xorps %xmm4, %xmm4
cvtsd2ss %xmm0, %xmm4
mulss 0x1666(%rip), %xmm4     # 0x90f0
movaps 0x10(%rsp), %xmm2
jmp 0x7982
movaps 0x10(%rsp), %xmm0
jmp 0x7a28
xorl %r14d, %r14d
testq %r14, %r14
je 0x7acc
movq (%r14), %rbx
movq %rbx, %rdi
callq 0x5045
movl (%rbx), %esi
leaq 0x34(%rsp), %rcx
movl $0x0, (%rcx)
movq 0x48f6(%rip), %rdi      # 0xc3b8
movl $0x40, %edx
callq 0x3200
cmpq $0x0, 0x8(%rsp)
je 0x7ae0
leaq 0x8(%rsp), %rdi
xorl %esi, %esi
callq 0x8660
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq