// File: /workspace/github_c_makefile_star2_O1/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 0x5afa
callq 0x1dda4
callq 0xebf8
movq 0x240d0(%rip), %rdi     # 0x296c8
callq 0x4520
movq %rax, %rdi
callq 0x43f0
movq %rax, 0x24164(%rip)     # 0x29770
testq %rax, %rax
jne 0x561f
leaq 0x19b27(%rip), %rdi     # 0x1f13f
xorl %eax, %eax
callq 0x5c74
movq 0x2414a(%rip), %rax     # 0x29770
movl (%rax), %eax
movl %eax, 0x2414a(%rip)     # 0x29778
callq 0x5b9b
movq 0x24136(%rip), %rax     # 0x29770
movzwl 0x14(%rax), %ecx
movl %ecx, 0x24138(%rip)     # 0x2977c
movzwl 0x16(%rax), %eax
movl %eax, 0x24132(%rip)     # 0x29780
movq 0x24073(%rip), %rdi     # 0x296c8
callq 0x4920
movl %eax, 0x24084(%rip)     # 0x296e4
movq 0x24061(%rip), %rdi     # 0x296c8
movl 0x2410b(%rip), %ecx     # 0x29778
subq $0x8, %rsp
xorl %r14d, %r14d
movl $0x2, %r15d
movl $0x1, %ebx
xorl %esi, %esi
movl %eax, %edx
movl $0xffffffff, %r8d       # imm = 0xFFFFFFFF
movl $0xffffffff, %r9d       # imm = 0xFFFFFFFF
pushq $0x0
pushq %r14
pushq %r14
pushq %r15
pushq %r14
pushq %rbx
pushq %rbx
callq 0x4030
addq $0x40, %rsp
movq 0x2401d(%rip), %rdi     # 0x296c8
movl 0x24033(%rip), %esi     # 0x296e4
leaq 0x19c40(%rip), %rcx     # 0x1f2f8
movl $0x9, %edx
callq 0x4710
movq 0x23fff(%rip), %rdi     # 0x296c8
callq 0x4920
movl %eax, 0x24014(%rip)     # 0x296e8
movw $0x0, 0x2400f(%rip)     # 0x296ec
movb $0x0, 0x2400a(%rip)     # 0x296ee
leaq 0xc(%rsp), %r10
movl $0x20040, (%r10)        # imm = 0x20040
movq 0x23fd1(%rip), %rdi     # 0x296c8
movl 0x2407b(%rip), %ecx     # 0x29778
subq $0x8, %rsp
xorl %esi, %esi
movl %eax, %edx
xorl %r8d, %r8d
xorl %r9d, %r9d
pushq %r10
pushq $0x800                  # imm = 0x800
pushq %r14
pushq %r15
pushq %r14
pushq %rbx
pushq %rbx
callq 0x4030
addq $0x40, %rsp
movq 0x23f9e(%rip), %rdi     # 0x296c8
movl 0x23fb8(%rip), %esi     # 0x296e8
leaq 0x19bca(%rip), %rcx     # 0x1f301
movl $0x16, %edx
callq 0x4710
leaq 0x246e8(%rip), %rax     # 0x29e30
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)
movl 0x124(%rdi), %eax
movl %eax, 0x30(%rcx)
movl 0x128(%rdi), %eax
movl %eax, 0x34(%rcx)
movl 0x130(%rdi), %eax
movl %eax, 0x38(%rcx)
movl 0x12c(%rdi), %eax
movl %eax, 0x3c(%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)
movl 0xd4(%rdi), %eax
movl %eax, 0x4c(%rcx)
movl 0xd0(%rdi), %eax
movl %eax, 0x50(%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 0x23ea8(%rip), %esi     # 0x296c0
movl $0x19, %edx
callq 0x4890
movl 0x23ebc(%rip), %edi     # 0x296e4
callq 0x1e724
leaq 0x1992e(%rip), %rdi     # 0x1f162
leaq 0x23f49(%rip), %rsi     # 0x29784
callq 0x115f0
leaq 0x19924(%rip), %rdi     # 0x1f16b
leaq 0x23f3a(%rip), %rsi     # 0x29788
callq 0x115f0
leaq 0x19922(%rip), %rdi     # 0x1f17c
leaq 0x23f2b(%rip), %rsi     # 0x2978c
callq 0x115f0
movq 0x23e5b(%rip), %rdi     # 0x296c8
movq 0x2375c(%rip), %rsi     # 0x28fd0
callq 0x42b0
cmpb $0x0, 0x8(%rax)
je 0x58ba
movq %rax, %rbx
callq 0x77db
testb %al, %al
je 0x58ba
movb $0x1, 0x23efe(%rip)     # 0x29790
movb 0xa(%rbx), %al
leaq 0x24574(%rip), %rcx     # 0x29e10
movb %al, (%rcx)
movq 0x23e23(%rip), %rdi     # 0x296c8
movl 0x23ecd(%rip), %esi     # 0x29778
movl $0x1, %edx
callq 0x4220
jmp 0x59ff
movb $0x0, 0x23ecf(%rip)     # 0x29790
leaq 0x198c2(%rip), %rdi     # 0x1f18a
xorl %eax, %eax
callq 0x4b50
movq 0x23df2(%rip), %rdi     # 0x296c8
movq 0x2371b(%rip), %rsi     # 0x28ff8
callq 0x42b0
cmpb $0x0, 0x8(%rax)
je 0x59a8
movq 0x23dd5(%rip), %rbx     # 0x296c8
movq %rbx, %rdi
callq 0x46d0
movq %rbx, %rdi
movl %eax, %esi
xorl %edx, %edx
callq 0x4510
testq %rax, %rax
je 0x59a8
movl 0x8(%rax), %ebx
movq %rax, %rdi
callq 0x4460
testl %ebx, %ebx
je 0x59a8
movq 0x23d9e(%rip), %rbx     # 0x296c8
movq %rbx, %rdi
callq 0x47c0
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
testl %eax, %eax
jle 0x599e
movl %eax, %r13d
xorl %ebp, %ebp
leaq 0x4(%rsp), %r15
movq (%r14,%rbp,8), %rax
movq %rax, 0x4(%rsp)
xorl %edi, %edi
movq %r15, %rsi
xorl %edx, %edx
callq 0x6b68
movq %rax, %r12
movq %rax, %rdi
callq 0x7261
xorl %edi, %edi
xorl %esi, %esi
callq 0x8297
movq %r12, %rdi
movq %rax, %rsi
callq 0x8389
incq %rbp
cmpq %rbp, %r13
jne 0x5962
movq %rbx, %rdi
callq 0x4460
jmp 0x59ff
leaq 0x19802(%rip), %rdi     # 0x1f1b1
xorl %eax, %eax
callq 0x4b50
leaq 0x4(%rsp), %rsi
movl $0x0, (%rsi)
movzwl 0x23db4(%rip), %eax     # 0x2977c
movw %ax, 0x4(%rsi)
movzwl 0x23dad(%rip), %eax     # 0x29780
movw %ax, 0x6(%rsi)
xorl %edi, %edi
xorl %edx, %edx
callq 0x6b68
movq %rax, %rbx
movq %rax, %rdi
callq 0x7261
xorl %edi, %edi
xorl %esi, %esi
callq 0x8297
movq %rbx, %rdi
movq %rax, %rsi
callq 0x8389
callq 0x1de29
callq 0x1e0b4
callq 0x1e17a
callq 0x1df11
movq 0x23cae(%rip), %rbx     # 0x296c8
movq %rbx, %rdi
callq 0x4950
movq %rbx, %rdi
movl %eax, %esi
xorl %edx, %edx
callq 0x4b10
movq %rax, %rbx
testq %rax, %rax
je 0x5a41
cmpl $0x1, 0x8(%rbx)
ja 0x5a41
callq 0x11504
movq %rbx, %rdi
callq 0x4460
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq