// File: /workspace/github_c_makefile_star2_O2/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 0x566f(%rip), %rsi      # 0xc7e0
testq %rsi, %rsi
je 0x7552
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movl (%rdi), %ebx
leaq 0x28(%rsp), %r14
andq $0x0, (%r14)
pushq $0x4
popq %rdi
callq 0x7acd
movl %eax, %r15d
shll $0x10, %eax
movl %eax, 0x10(%rsp)
sarl $0x10, %r15d
leaq 0x5218(%rip), %r13      # 0xc3c8
movl %ebx, %ebp
leaq 0x1edf(%rip), %r12      # 0x9098
movq (%r13), %r13
testq %r13, %r13
je 0x7231
movq (%r13), %rsi
movl (%rsi), %eax
movq 0x5611(%rip), %rcx      # 0xc7e0
cmpl (%rcx), %eax
je 0x722b
cmpb $0x1, 0x54(%rsi)
jne 0x722b
pushq $0x4
popq %rdi
callq 0x7acd
cmpl $0x3, %ebx
ja 0x722b
movslq (%r12,%rbp,4), %rcx
addq %r12, %rcx
jmpq *%rcx
sarl $0x10, %eax
cmpl %r15d, %eax
jl 0x7217
jmp 0x722b
shll $0x10, %eax
cmpl 0x10(%rsp), %eax
jge 0x7217
jmp 0x722b
shll $0x10, %eax
cmpl 0x10(%rsp), %eax
jl 0x7217
jmp 0x722b
sarl $0x10, %eax
cmpl %r15d, %eax
jl 0x722b
movq %r14, %rdi
callq 0x7f71
testq %rax, %rax
je 0x722b
movq (%r13), %rcx
movq %rcx, (%rax)
addq $0x10, %r13
jmp 0x71b9
movss 0x1f03(%rip), %xmm0     # 0x913c
xorl %r13d, %r13d
pushq $0x4
popq %rbp
leal -0x2(%rbx), %eax
movl %eax, 0x24(%rsp)
movq (%r14), %r14
testq %r14, %r14
je 0x7522
movaps %xmm0, 0x30(%rsp)
movq 0x5582(%rip), %rsi      # 0xc7e0
movq (%r14), %r15
movl %ebp, %edi
callq 0x7acd
movl %eax, %r12d
movl %ebp, %edi
movq %r15, %rsi
callq 0x7acd
movswl %ax, %ecx
movswl %r12w, %edx
subl %edx, %ecx
cvtsi2ss %ecx, %xmm0
sarl $0x10, %eax
sarl $0x10, %r12d
subl %r12d, %eax
cvtsi2ss %eax, %xmm1
callq 0x3660
movaps %xmm0, 0x10(%rsp)
movq 0x553f(%rip), %rsi      # 0xc7e0
movq (%r14), %r12
movl %ebp, %edi
callq 0x7acd
movl %eax, %r15d
movl %ebp, %edi
movq %r12, %rsi
callq 0x7acd
movswl %ax, %ecx
movswl %r15w, %edx
sarl $0x10, %eax
sarl $0x10, %r15d
movl %ecx, %esi
xorl %edx, %esi
movl %eax, %edi
xorl %r15d, %edi
xorps %xmm0, %xmm0
orl %esi, %edi
je 0x72f8
subl %r15d, %eax
cvtsi2ss %eax, %xmm1
subl %edx, %ecx
cvtsi2ss %ecx, %xmm0
callq 0x3260
cvtss2sd %xmm0, %xmm0
mulsd 0x1e74(%rip), %xmm0     # 0x9168
cvtsd2ss %xmm0, %xmm0
movl %ebx, %edi
movss %xmm0, 0xc(%rsp)
movss 0x1e38(%rip), %xmm1     # 0x9140
callq 0x7b12
testb %al, %al
je 0x7350
movq 0x54c8(%rip), %r15      # 0xc7e0
movq (%r14), %r12
movq %r15, %rdi
movq %r12, %rsi
callq 0x7b98
movaps 0x10(%rsp), %xmm1
cvtss2sd %xmm1, %xmm0
mulsd 0x1e39(%rip), %xmm0     # 0x9170
testb %al, %al
je 0x733f
cvtsd2ss %xmm0, %xmm1
cvtss2sd %xmm1, %xmm0
mulsd 0x1e3d(%rip), %xmm0     # 0x9188
jmp 0x73fc
movl %ebx, %edi
movss 0xc(%rsp), %xmm0
movss 0x1de4(%rip), %xmm1     # 0x9144
callq 0x7b12
testb %al, %al
je 0x73a5
movq 0x5470(%rip), %r15      # 0xc7e0
movq (%r14), %r12
movq %r15, %rdi
movq %r12, %rsi
callq 0x7b98
movaps 0x10(%rsp), %xmm1
cvtss2sd %xmm1, %xmm0
mulsd 0x1de1(%rip), %xmm0     # 0x9170
testb %al, %al
je 0x7397
cvtsd2ss %xmm0, %xmm1
cvtss2sd %xmm1, %xmm0
mulsd 0x1ddd(%rip), %xmm0     # 0x9180
jmp 0x73fc
movl %ebx, %edi
movss 0xc(%rsp), %xmm0
movss 0x1d93(%rip), %xmm1     # 0x9148
callq 0x7b12
testb %al, %al
je 0x74c2
movq 0x5417(%rip), %r15      # 0xc7e0
movq (%r14), %r12
movq %r15, %rdi
movq %r12, %rsi
callq 0x7b98
movaps 0x10(%rsp), %xmm1
cvtss2sd %xmm1, %xmm0
mulsd 0x1d88(%rip), %xmm0     # 0x9170
testb %al, %al
je 0x73f0
cvtsd2ss %xmm0, %xmm1
cvtss2sd %xmm1, %xmm0
mulsd 0x1d7c(%rip), %xmm0     # 0x9178
cvtsd2ss %xmm0, %xmm0
movaps %xmm0, 0x10(%rsp)
movl %ebp, %edi
movq %r12, %rsi
callq 0x7acd
cmpl $0x2, %ebx
jb 0x7448
cmpl $0x1, 0x24(%rsp)
movaps 0x30(%rsp), %xmm2
ja 0x7489
movswl 0x6(%r15), %ecx
movzwl 0xa(%r15), %edx
addw %cx, %dx
sarl $0x10, %eax
cmpl %ecx, %eax
setge %cl
movswl %dx, %edx
cmpl %edx, %eax
setle %al
testb %al, %cl
movaps 0x10(%rsp), %xmm4
jne 0x7477
jmp 0x748e
movl 0x4(%r15), %ecx
movzwl 0x8(%r15), %edx
addw %cx, %dx
shll $0x10, %ecx
shll $0x10, %eax
cmpl %eax, %ecx
setle %cl
shll $0x10, %edx
cmpl %eax, %edx
setge %al
andb %cl, %al
cmpb $0x1, %al
movaps 0x30(%rsp), %xmm2
movaps 0x10(%rsp), %xmm4
jne 0x748e
cvtss2sd %xmm4, %xmm0
mulsd 0x1cf5(%rip), %xmm0     # 0x9178
cvtsd2ss %xmm0, %xmm4
jmp 0x748e
movaps 0x10(%rsp), %xmm4
movaps %xmm4, %xmm0
cmpltps %xmm2, %xmm0
movaps %xmm2, %xmm1
movss 0x1c9c(%rip), %xmm3     # 0x913c
cmpeqps %xmm3, %xmm1
orps %xmm0, %xmm1
movd %xmm1, %eax
testb $0x1, %al
jne 0x74b2
movaps %xmm2, %xmm4
cmovneq %r14, %r13
movaps %xmm4, %xmm0
addq $0x10, %r14
jmp 0x7246
movl %ebx, %edi
movss 0xc(%rsp), %xmm0
movss 0x1c7a(%rip), %xmm1     # 0x914c
callq 0x7b12
testb %al, %al
je 0x751b
movq 0x52fe(%rip), %r15      # 0xc7e0
movq (%r14), %r12
movq %r15, %rdi
movq %r12, %rsi
callq 0x7b98
movaps 0x10(%rsp), %xmm1
cvtss2sd %xmm1, %xmm0
mulsd 0x1c6f(%rip), %xmm0     # 0x9170
testb %al, %al
je 0x7509
cvtsd2ss %xmm0, %xmm1
mulss 0x1c3f(%rip), %xmm1     # 0x9150
movaps %xmm1, 0x10(%rsp)
jmp 0x7405
movaps 0x30(%rsp), %xmm0
jmp 0x74b9
testq %r13, %r13
je 0x7530
movq (%r13), %rdi
callq 0x5936
cmpq $0x0, 0x28(%rsp)
je 0x7544
leaq 0x28(%rsp), %rdi
xorl %esi, %esi
callq 0x7fe3
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq