// File: /workspace/github_cpp_makefile_star2_O1/rfotino[P]consolite-emulator/src/window.cpp
// Function: EmuWindow::EmuWindow(EmuVideoMemory*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&)
EmuWindow::EmuWindow(EmuVideoMemory *vid_mem,
                     const std::string& keymap_filename)
                    : _vidMem(vid_mem),
                      _error(false),
                      _width(DEFAULT_WINDOW_WIDTH),
                      _height(DEFAULT_WINDOW_HEIGHT) {
  // Read key mapping into memory
  _loadKeyMap(keymap_filename);

  // Open a connection to the X server.
  _display = XOpenDisplay(nullptr);
  if (nullptr == _display) {
    std::cerr << "Error: Cannot open connection to the X server."
              << std::endl;
    _error = true;
    return;
  }

  // Create a window on the default screen.
  int screen_num = DefaultScreen(_display);
  Window window = XCreateSimpleWindow(
      _display,                         // Connection to the X server
      DefaultRootWindow(_display),      // Parent window
      0,                                // X position
      0,                                // Y position
      _width,                           // Width
      _height,                          // Height
      0,                                // Border width
      BlackPixel(_display, screen_num), // Border color
      BlackPixel(_display, screen_num)  // Background color
  );

  // Create the cairo surface we will use for drawing to the window
  Visual *visual = DefaultVisual(_display, screen_num);
  _surface = cairo_xlib_surface_create(
      _display,     // Connection to the X server
      window,       // A Drawable, the window in this case
      visual,       // A Visual, the screen in this case
      _width,       // The surface width
      _height       // The surface height
  );
  if (nullptr == _surface) {
    _error = true;
    std::cerr << "Error: Cannot get cairo Xlib surface." << std::endl;
    return;
  }

  // Create the cairo object from the window surface
  _cairo = cairo_create(_surface);
  
  // Intercept the WM_DELETE_WINDOW message from the window
  // manager so that we can close the window when the user hits
  // the exit button.
  _wmDeleteMessage = XInternAtom(_display, "WM_DELETE_WINDOW", false);
  XSetWMProtocols(_display, window, &_wmDeleteMessage, 1);
  // Select the kind of window events we want to intercept.
  // StructureNotifyMask lets us intercept window resize events.
  XSelectInput(_display, window, ExposureMask | StructureNotifyMask |
                                 KeyPressMask | KeyReleaseMask);
  // "Map" (show) the window.
  XMapWindow(_display, window);
  // Suppress auto-repeating KeyRelease events
  XkbSetDetectableAutoRepeat(_display, 1, 0);
  // Flush any buffered commands to the X server
  XFlush(_display);
}

// Assembly:
pushq %r15
pushq %r14
pushq %rbx
movq %rdi, %rbx
movq %rsi, 0x20(%rdi)
movb $0x0, 0x28(%rdi)
movabsq $0x24000000300, %rax    # imm = 0x24000000300
movq %rax, 0x2c(%rdi)
leaq 0x40(%rdi), %rax
xorl %ecx, %ecx
movl %ecx, 0x40(%rdi)
movq %rcx, 0x48(%rdi)
movq %rax, 0x50(%rdi)
movq %rax, 0x58(%rdi)
movq %rcx, 0x60(%rdi)
leaq 0x70(%rdi), %rax
movl %ecx, 0x70(%rdi)
movq %rcx, 0x78(%rdi)
movq %rax, 0x80(%rdi)
movq %rax, 0x88(%rdi)
movq %rcx, 0x90(%rdi)
movq %rdx, %rsi
callq 0x3e5a
xorl %edi, %edi
callq 0x32f0
movq %rax, 0x10(%rbx)
testq %rax, %rax
je 0x3d99
movslq 0xe0(%rax), %r15
movq 0xe8(%rax), %rcx
shlq $0x7, %r15
movl 0x2c(%rbx), %r8d
movl 0x30(%rbx), %r9d
movq 0x10(%rcx,%r15), %rsi
movq 0x60(%rcx,%r15), %r10
subq $0x8, %rsp
movq %rax, %rdi
xorl %edx, %edx
xorl %ecx, %ecx
pushq %r10
pushq %r10
pushq $0x0
callq 0x3090
addq $0x20, %rsp
movq %rax, %r14
movq 0x10(%rbx), %rdi
movq 0xe8(%rdi), %rax
movq 0x40(%rax,%r15), %rdx
movl 0x2c(%rbx), %ecx
movl 0x30(%rbx), %r8d
movq %r14, %rsi
callq 0x31a0
movq %rax, (%rbx)
testq %rax, %rax
je 0x3de5
movq %rax, %rdi
callq 0x30b0
movq %rax, 0x8(%rbx)
movq 0x10(%rbx), %rdi
leaq 0x241f(%rip), %rsi      # 0x615d
xorl %edx, %edx
callq 0x31c0
leaq 0x18(%rbx), %rdx
movq %rax, 0x18(%rbx)
movq 0x10(%rbx), %rdi
movq %r14, %rsi
movl $0x1, %ecx
callq 0x3120
movq 0x10(%rbx), %rdi
movl $0x28003, %edx          # imm = 0x28003
movq %r14, %rsi
callq 0x3150
movq 0x10(%rbx), %rdi
movq %r14, %rsi
callq 0x3290
movq 0x10(%rbx), %rdi
movl $0x1, %esi
xorl %edx, %edx
callq 0x3440
movq 0x10(%rbx), %rdi
callq 0x3380
jmp 0x3e2f
movq 0x5250(%rip), %r14      # 0x8ff0
leaq 0x2361(%rip), %rsi      # 0x6108
movl $0x2e, %edx
movq %r14, %rdi
callq 0x32a0
movq (%r14), %rax
addq -0x18(%rax), %r14
movq %r14, %rdi
movl $0xa, %esi
callq 0x3240
movsbl %al, %esi
movq 0x521e(%rip), %rdi      # 0x8ff0
callq 0x3030
movq %rax, %rdi
callq 0x3180
movb $0x1, 0x28(%rbx)
jmp 0x3e2f
movb $0x1, 0x28(%rbx)
movq 0x5200(%rip), %r14      # 0x8ff0
leaq 0x2340(%rip), %rsi      # 0x6137
movl $0x25, %edx
movq %r14, %rdi
callq 0x32a0
movq (%r14), %rax
addq -0x18(%rax), %r14
movq %r14, %rdi
movl $0xa, %esi
callq 0x3240
movsbl %al, %esi
movq 0x51ce(%rip), %rdi      # 0x8ff0
callq 0x3030
movq %rax, %rdi
callq 0x3180
popq %rbx
popq %r14
popq %r15
retq
jmp 0x3e39
jmp 0x3e39
movq %rax, %r14
leaq 0x68(%rbx), %rdi
addq $0x38, %rbx
callq 0x45fe
movq %rbx, %rdi
callq 0x464e
movq %r14, %rdi
callq 0x3430
nop