// File: /workspace/github_cpp_makefile_star2_O3/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 0x3e3e
xorl %edi, %edi
callq 0x32f0
movq %rax, 0x10(%rbx)
testq %rax, %rax
je 0x3d7d
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 0x3dc9
movq %rax, %rdi
callq 0x30b0
movq %rax, 0x8(%rbx)
movq 0x10(%rbx), %rdi
leaq 0x243b(%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 0x3e13
movq 0x526c(%rip), %r14      # 0x8ff0
leaq 0x237d(%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 0x523a(%rip), %rdi      # 0x8ff0
callq 0x3030
movq %rax, %rdi
callq 0x3180
movb $0x1, 0x28(%rbx)
jmp 0x3e13
movb $0x1, 0x28(%rbx)
movq 0x521c(%rip), %r14      # 0x8ff0
leaq 0x235c(%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 0x51ea(%rip), %rdi      # 0x8ff0
callq 0x3030
movq %rax, %rdi
callq 0x3180
popq %rbx
popq %r14
popq %r15
retq
jmp 0x3e1d
jmp 0x3e1d
movq %rax, %r14
leaq 0x68(%rbx), %rdi
addq $0x38, %rbx
callq 0x45a6
movq %rbx, %rdi
callq 0x45f6
movq %r14, %rdi
callq 0x3430
nop