// File: /workspace/github_c_makefile_star2_O2/baskerville[P]bspwm/src/bspwm.c
// Function: setup
void setup(void)
{
	init();
	ewmh_init();
	pointer_init();

	screen = xcb_setup_roots_iterator(xcb_get_setup(dpy)).data;

	if (screen == NULL) {
		err("Can't acquire the default screen.\n");
	}

	root = screen->root;
	register_events();

	screen_width = screen->width_in_pixels;
	screen_height = screen->height_in_pixels;

	meta_window = xcb_generate_id(dpy);
	xcb_create_window(dpy, XCB_COPY_FROM_PARENT, meta_window, root, -1, -1, 1, 1, 0, XCB_WINDOW_CLASS_INPUT_ONLY, XCB_COPY_FROM_PARENT, XCB_NONE, NULL);
	xcb_icccm_set_wm_class(dpy, meta_window, sizeof(META_WINDOW_IC), META_WINDOW_IC);

	motion_recorder.id = xcb_generate_id(dpy);
	motion_recorder.sequence = 0;
	motion_recorder.enabled = false;
	uint32_t values[] = {XCB_EVENT_MASK_STRUCTURE_NOTIFY | XCB_EVENT_MASK_POINTER_MOTION};
	xcb_create_window(dpy, XCB_COPY_FROM_PARENT, motion_recorder.id, root, 0, 0, 1, 1, 0,
	                  XCB_WINDOW_CLASS_INPUT_ONLY, XCB_COPY_FROM_PARENT, XCB_CW_EVENT_MASK, values);
	xcb_icccm_set_wm_class(dpy, motion_recorder.id, sizeof(MOTION_RECORDER_IC), MOTION_RECORDER_IC);

	xcb_atom_t net_atoms[] = {ewmh->_NET_SUPPORTED,
	                          ewmh->_NET_SUPPORTING_WM_CHECK,
	                          ewmh->_NET_DESKTOP_NAMES,
	                          ewmh->_NET_DESKTOP_VIEWPORT,
	                          ewmh->_NET_NUMBER_OF_DESKTOPS,
	                          ewmh->_NET_CURRENT_DESKTOP,
	                          ewmh->_NET_CLIENT_LIST,
	                          ewmh->_NET_ACTIVE_WINDOW,
	                          ewmh->_NET_CLOSE_WINDOW,
	                          ewmh->_NET_WM_STRUT_PARTIAL,
	                          ewmh->_NET_WM_DESKTOP,
	                          ewmh->_NET_WM_STATE,
	                          ewmh->_NET_WM_STATE_HIDDEN,
	                          ewmh->_NET_WM_STATE_FULLSCREEN,
	                          ewmh->_NET_WM_STATE_BELOW,
	                          ewmh->_NET_WM_STATE_ABOVE,
	                          ewmh->_NET_WM_STATE_STICKY,
	                          ewmh->_NET_WM_STATE_DEMANDS_ATTENTION,
	                          ewmh->_NET_WM_WINDOW_TYPE,
	                          ewmh->_NET_WM_WINDOW_TYPE_DOCK,
	                          ewmh->_NET_WM_WINDOW_TYPE_DESKTOP,
	                          ewmh->_NET_WM_WINDOW_TYPE_NOTIFICATION,
	                          ewmh->_NET_WM_WINDOW_TYPE_DIALOG,
	                          ewmh->_NET_WM_WINDOW_TYPE_UTILITY,
	                          ewmh->_NET_WM_WINDOW_TYPE_TOOLBAR};

	xcb_ewmh_set_supported(ewmh, default_screen, LENGTH(net_atoms), net_atoms);
	ewmh_set_supporting(meta_window);

#define GETATOM(a) \
	get_atom(#a, &a);
	GETATOM(WM_STATE)
	GETATOM(WM_DELETE_WINDOW)
	GETATOM(WM_TAKE_FOCUS)
#undef GETATOM

	const xcb_query_extension_reply_t *qep = xcb_get_extension_data(dpy, &xcb_randr_id);
	if (qep->present && update_monitors()) {
		randr = true;
		randr_base = qep->first_event;
		xcb_randr_select_input(dpy, root, XCB_RANDR_NOTIFY_MASK_SCREEN_CHANGE);
	} else {
		randr = false;
		warn("Couldn't retrieve monitors via RandR.\n");
		bool xinerama_is_active = false;

		if (xcb_get_extension_data(dpy, &xcb_xinerama_id)->present) {
			xcb_xinerama_is_active_reply_t *xia = xcb_xinerama_is_active_reply(dpy, xcb_xinerama_is_active(dpy), NULL);
			if (xia != NULL) {
				xinerama_is_active = xia->state;
				free(xia);
			}
		}

		if (xinerama_is_active) {
			xcb_xinerama_query_screens_reply_t *xsq = xcb_xinerama_query_screens_reply(dpy, xcb_xinerama_query_screens(dpy), NULL);
			xcb_xinerama_screen_info_t *xsi = xcb_xinerama_query_screens_screen_info(xsq);
			int n = xcb_xinerama_query_screens_screen_info_length(xsq);
			for (int i = 0; i < n; i++) {
				xcb_xinerama_screen_info_t info = xsi[i];
				xcb_rectangle_t rect = (xcb_rectangle_t) {info.x_org, info.y_org, info.width, info.height};
				monitor_t *m = make_monitor(NULL, &rect, XCB_NONE);
				add_monitor(m);
				add_desktop(m, make_desktop(NULL, XCB_NONE));
			}
			free(xsq);
		} else {
			warn("Xinerama is inactive.\n");
			xcb_rectangle_t rect = (xcb_rectangle_t) {0, 0, screen_width, screen_height};
			monitor_t *m = make_monitor(NULL, &rect, XCB_NONE);
			add_monitor(m);
			add_desktop(m, make_desktop(NULL, XCB_NONE));
		}
	}

	ewmh_update_number_of_desktops();
	ewmh_update_desktop_names();
	ewmh_update_desktop_viewport();
	ewmh_update_current_desktop();
	xcb_get_input_focus_reply_t *ifo = xcb_get_input_focus_reply(dpy, xcb_get_input_focus(dpy), NULL);
	if (ifo != NULL && (ifo->focus == XCB_INPUT_FOCUS_POINTER_ROOT || ifo->focus == XCB_NONE)) {
		clear_input_focus();
	}
	free(ifo);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
callq 0x59d1
callq 0x1c4bc
callq 0xe02c
movq 0x231ff(%rip), %rdi     # 0x286c8
callq 0x4520
movq %rax, %rdi
callq 0x43f0
movq %rax, 0x23293(%rip)     # 0x28770
testq %rax, %rax
jne 0x54f7
leaq 0x18c56(%rip), %rdi     # 0x1e13f
xorl %eax, %eax
callq 0x5be8
movq 0x23279(%rip), %rax     # 0x28770
movl (%rax), %eax
movl %eax, 0x23279(%rip)     # 0x28778
callq 0x5a83
movq 0x23265(%rip), %rax     # 0x28770
movzwl 0x14(%rax), %ecx
movl %ecx, 0x23267(%rip)     # 0x2877c
movzwl 0x16(%rax), %eax
movl %eax, 0x23261(%rip)     # 0x28780
movq 0x231a2(%rip), %rdi     # 0x286c8
callq 0x4920
movl %eax, 0x231b3(%rip)     # 0x286e4
movq 0x23190(%rip), %rdi     # 0x286c8
movl 0x2323a(%rip), %ecx     # 0x28778
subq $0x40, %rsp
pushq $0x2
popq %rbx
andq $0x0, 0x30(%rsp)
movl %ebx, 0x18(%rsp)
andl $0x0, 0x28(%rsp)
pushq $0x1
popq %r14
andl $0x0, 0x20(%rsp)
movl %r14d, 0x8(%rsp)
andl $0x0, 0x10(%rsp)
movl %r14d, (%rsp)
pushq $-0x1
popq %r8
xorl %esi, %esi
movl %eax, %edx
movl %r8d, %r9d
callq 0x4030
addq $0x40, %rsp
movq 0x23142(%rip), %rdi     # 0x286c8
movl 0x23158(%rip), %esi     # 0x286e4
leaq 0x18d65(%rip), %rcx     # 0x1e2f8
pushq $0x9
popq %rdx
callq 0x4710
movq 0x23126(%rip), %rdi     # 0x286c8
callq 0x4920
movl %eax, 0x2313b(%rip)     # 0x286e8
andw $0x0, 0x23137(%rip)     # 0x286ec
movb $0x0, 0x23132(%rip)     # 0x286ee
leaq 0xc(%rsp), %r10
movl $0x20040, (%r10)        # imm = 0x20040
movq 0x230f9(%rip), %rdi     # 0x286c8
movl 0x231a3(%rip), %ecx     # 0x28778
subq $0x8, %rsp
xorl %esi, %esi
movl %eax, %edx
xorl %r8d, %r8d
xorl %r9d, %r9d
pushq %r10
pushq $0x800                  # imm = 0x800
pushq $0x0
pushq %rbx
pushq $0x0
pushq %r14
pushq %r14
callq 0x4030
addq $0x40, %rsp
movq 0x230c5(%rip), %rdi     # 0x286c8
movl 0x230df(%rip), %esi     # 0x286e8
leaq 0x18cf1(%rip), %rcx     # 0x1e301
pushq $0x16
popq %rdx
callq 0x4710
leaq 0x23811(%rip), %rax     # 0x28e30
movq (%rax), %rdi
movl 0x20(%rdi), %eax
leaq 0x10(%rsp), %rcx
movl %eax, (%rcx)
movl 0x48(%rdi), %eax
movl %eax, 0x4(%rcx)
movl 0x3c(%rdi), %eax
movl %eax, 0x8(%rcx)
movl 0x34(%rdi), %eax
movl %eax, 0xc(%rcx)
movl 0x2c(%rdi), %eax
movl %eax, 0x10(%rcx)
movl 0x38(%rdi), %eax
movl %eax, 0x14(%rcx)
movl 0x24(%rdi), %eax
movl %eax, 0x18(%rcx)
movl 0x40(%rdi), %eax
movl %eax, 0x1c(%rcx)
movl 0x58(%rdi), %eax
movl %eax, 0x20(%rcx)
movl 0x90(%rdi), %eax
movl %eax, 0x24(%rcx)
movl 0x7c(%rdi), %eax
movl %eax, 0x28(%rcx)
movl 0x84(%rdi), %eax
movl %eax, 0x2c(%rcx)
movdqu 0x124(%rdi), %xmm0
pshufd $0xb4, %xmm0, %xmm0     # xmm0 = xmm0[0,1,3,2]
movdqa %xmm0, 0x30(%rcx)
movl 0x10c(%rdi), %eax
movl %eax, 0x40(%rcx)
movl 0x134(%rdi), %eax
movl %eax, 0x44(%rcx)
movl 0x80(%rdi), %eax
movl %eax, 0x48(%rcx)
movq 0xd0(%rdi), %xmm0
pshufd $0xe1, %xmm0, %xmm0     # xmm0 = xmm0[1,0,2,3]
movq %xmm0, 0x4c(%rcx)
movl 0xf8(%rdi), %eax
movl %eax, 0x54(%rcx)
movl 0xe8(%rdi), %eax
movl %eax, 0x58(%rcx)
movl 0xe0(%rdi), %eax
movl %eax, 0x5c(%rcx)
movl 0xd8(%rdi), %eax
movl %eax, 0x60(%rcx)
movl 0x22fe3(%rip), %esi     # 0x286c0
pushq $0x19
popq %rdx
callq 0x4890
movl 0x22ff9(%rip), %edi     # 0x286e4
callq 0x1ccbc
leaq 0x18a6b(%rip), %rdi     # 0x1e162
leaq 0x23086(%rip), %rsi     # 0x28784
callq 0x106ae
leaq 0x18a61(%rip), %rdi     # 0x1e16b
leaq 0x23077(%rip), %rsi     # 0x28788
callq 0x106ae
leaq 0x18a5f(%rip), %rdi     # 0x1e17c
leaq 0x23068(%rip), %rsi     # 0x2878c
callq 0x106ae
movq 0x22f98(%rip), %rdi     # 0x286c8
movq 0x22899(%rip), %rsi     # 0x27fd0
callq 0x42b0
cmpb $0x0, 0x8(%rax)
je 0x577b
movq %rax, %rbx
callq 0x7521
testb %al, %al
je 0x577b
movb $0x1, 0x2303b(%rip)     # 0x28790
movb 0xa(%rbx), %al
leaq 0x236b1(%rip), %rcx     # 0x28e10
movb %al, (%rcx)
movq 0x22f60(%rip), %rdi     # 0x286c8
movl 0x2300a(%rip), %esi     # 0x28778
pushq $0x1
popq %rdx
callq 0x4220
jmp 0x58c0
movb $0x0, 0x2300e(%rip)     # 0x28790
leaq 0x18a01(%rip), %rdi     # 0x1e18a
xorl %eax, %eax
callq 0x5b50
movq 0x22f31(%rip), %rdi     # 0x286c8
movq 0x2285a(%rip), %rsi     # 0x27ff8
callq 0x42b0
cmpb $0x0, 0x8(%rax)
je 0x5864
movq 0x22f14(%rip), %rbx     # 0x286c8
movq %rbx, %rdi
callq 0x46d0
movq %rbx, %rdi
movl %eax, %esi
xorl %edx, %edx
callq 0x4510
testq %rax, %rax
je 0x5864
movl 0x8(%rax), %ebx
movq %rax, %rdi
callq 0x4460
testl %ebx, %ebx
je 0x5864
movq 0x22edd(%rip), %rbx     # 0x286c8
movq %rbx, %rdi
callq 0x47c0
xorl %ebp, %ebp
movq %rbx, %rdi
movl %eax, %esi
xorl %edx, %edx
callq 0x4240
movq %rax, %rbx
movq %rax, %rdi
callq 0x4a90
movq %rax, %r14
movq %rbx, %rdi
callq 0x43c0
movl %eax, %r15d
testl %eax, %eax
cmovlel %ebp, %r15d
movq %rsp, %r12
cmpq %rbp, %r15
je 0x58b8
movq (%r14,%rbp,8), %rax
movq %rax, (%rsp)
xorl %edi, %edi
movq %r12, %rsi
xorl %edx, %edx
callq 0x6984
movq %rax, %r13
movq %rax, %rdi
callq 0x7056
xorl %edi, %edi
xorl %esi, %esi
callq 0x7efd
movq %r13, %rdi
movq %rax, %rsi
callq 0x7fe2
incq %rbp
jmp 0x5823
leaq 0x18946(%rip), %rdi     # 0x1e1b1
xorl %eax, %eax
callq 0x5b50
movq %rsp, %rsi
andl $0x0, (%rsi)
movzwl 0x22efd(%rip), %eax     # 0x2877c
movw %ax, 0x4(%rsi)
movzwl 0x22ef6(%rip), %eax     # 0x28780
movw %ax, 0x6(%rsi)
xorl %edi, %edi
xorl %edx, %edx
callq 0x6984
movq %rax, %rbx
movq %rax, %rdi
callq 0x7056
xorl %edi, %edi
xorl %esi, %esi
callq 0x7efd
movq %rbx, %rdi
movq %rax, %rsi
callq 0x7fe2
jmp 0x58c0
movq %rbx, %rdi
callq 0x4460
callq 0x1c53f
callq 0x1c6ea
callq 0x1c78f
callq 0x1c5f3
movq 0x22ded(%rip), %rbx     # 0x286c8
movq %rbx, %rdi
callq 0x4950
movq %rbx, %rdi
movl %eax, %esi
xorl %edx, %edx
callq 0x4b00
movq %rax, %rbx
testq %rax, %rax
je 0x5902
cmpl $0x1, 0x8(%rbx)
ja 0x5902
callq 0x105cd
movq %rbx, %rdi
callq 0x4460
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq