// File: /workspace/github_cpp_makefile_star2_O2/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
subq $0x20, %rsp
movq %rdi, %rbx
movq %rsi, 0x20(%rdi)
movb $0x0, 0x28(%rdi)
movabsq $0x24000000300, %rax    # imm = 0x24000000300
movq %rax, 0x2c(%rdi)
leaq 0x40(%rdi), %rax
andl $0x0, 0x40(%rdi)
andq $0x0, 0x48(%rdi)
movq %rax, 0x50(%rdi)
movq %rax, 0x58(%rdi)
andq $0x0, 0x60(%rdi)
leaq 0x70(%rdi), %rax
andl $0x0, 0x70(%rdi)
andq $0x0, 0x78(%rdi)
movq %rax, 0x80(%rdi)
movq %rax, 0x88(%rdi)
andq $0x0, 0x90(%rdi)
movq %rdx, %rsi
callq 0x3d28
xorl %edi, %edi
callq 0x3320
movq %rax, 0x10(%rbx)
testq %rax, %rax
je 0x3cba
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), %rcx
andl $0x0, (%rsp)
movq %rcx, 0x10(%rsp)
movq %rcx, 0x8(%rsp)
movq %rax, %rdi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x3090
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 0x31c0
movq %rax, (%rbx)
testq %rax, %rax
je 0x3cdb
movq %rax, %rdi
callq 0x30a0
movq %rax, 0x8(%rbx)
movq 0x10(%rbx), %rdi
leaq 0x14f7(%rip), %rsi      # 0x515d
xorl %edx, %edx
callq 0x31e0
leaq 0x18(%rbx), %rdx
movq %rax, 0x18(%rbx)
movq 0x10(%rbx), %rdi
pushq $0x1
popq %rcx
movq %r14, %rsi
callq 0x3150
movq 0x10(%rbx), %rdi
movl $0x28003, %edx          # imm = 0x28003
movq %r14, %rsi
callq 0x3180
movq 0x10(%rbx), %rdi
movq %r14, %rsi
callq 0x32d0
movq 0x10(%rbx), %rdi
pushq $0x1
popq %rsi
xorl %edx, %edx
callq 0x3450
movq 0x10(%rbx), %rdi
callq 0x33a0
jmp 0x3cfa
movq 0x432f(%rip), %rdi      # 0x7ff0
leaq 0x1440(%rip), %rsi      # 0x5108
callq 0x3280
movq %rax, %rdi
callq 0x3120
movb $0x1, 0x28(%rbx)
jmp 0x3cfa
movb $0x1, 0x28(%rbx)
movq 0x430a(%rip), %rdi      # 0x7ff0
leaq 0x144a(%rip), %rsi      # 0x5137
callq 0x3280
movq %rax, %rdi
callq 0x3120
addq $0x20, %rsp
popq %rbx
popq %r14
popq %r15
retq
jmp 0x3d08
jmp 0x3d08
movq %rax, %r14
leaq 0x68(%rbx), %rdi
addq $0x38, %rbx
callq 0x42c8
movq %rbx, %rdi
callq 0x4320
movq %r14, %rdi
callq 0x3440