MirDisplayConnection::MirDisplayConnection()
    : connection_(nullptr),
      output_id_(-1),
      vertical_resolution_(0),
      horizontal_resolution_(0) {
  auto xdg_runtime_dir = ::getenv("XDG_RUNTIME_DIR");
  if (!xdg_runtime_dir)
    BOOST_THROW_EXCEPTION(std::runtime_error("Failed to find XDG_RUNTIME_DIR"));

  std::string socket_path = xdg_runtime_dir;
  socket_path += "/mir_socket";

  connection_ = mir_connect_sync(socket_path.c_str(), "anbox");
  if (!mir_connection_is_valid(connection_)) {
    std::string msg;
    msg += "Failed to connect with Mir server: ";
    msg += mir_connection_get_error_message(connection_);
    BOOST_THROW_EXCEPTION(std::runtime_error(msg.c_str()));
  }

  mir_connection_set_display_config_change_callback(
      connection_,
      [](MirConnection *connection, void *context) {
        auto thiz = reinterpret_cast<MirDisplayConnection *>(context);
        DEBUG("");
      },
      this);

  MirDisplayConfiguration *display_config =
      mir_connection_create_display_config(connection_);

  const MirDisplayOutput *output = find_active_output(display_config);
  if (!output)
    BOOST_THROW_EXCEPTION(
        std::runtime_error("Failed to find active output display"));

  DEBUG("Selecting output id %d", output->output_id);

  output_id_ = output->output_id;

  const MirDisplayMode *mode = &output->modes[output->current_mode];

  vertical_resolution_ = mode->vertical_resolution;
  horizontal_resolution_ = mode->horizontal_resolution;

  mir_display_config_destroy(display_config);
}
// FIXME Make sure we clean everything up, Also:
// we get a crash (call of virtual function) when releasing the connection :(
void Mir_VideoQuit(_THIS)
{
    Mir_DeleteQueue(this->hidden->buffer_queue);

    if (this->hidden->buffer_queue)
    {
        SDL_free(this->hidden->buffer_queue);
    }

    if (this->hidden->surface)
    {
        mir_surface_release_sync(this->hidden->surface);
        this->hidden->surface = NULL;
    }

#if SDL_VIDEO_OPENGL
    if (this->gl_config.driver_loaded != 0)
    {
        Mir_GL_DeleteContext(this);
        Mir_GL_UnloadLibrary(this);
    }
#endif // SDL_VIDEO_OPENGL

    if (mir_connection_is_valid(this->hidden->connection))
    {
        mir_connection_set_display_config_change_callback(this->hidden->connection,
                                                          NULL, NULL);
    }

    if (this->hidden->connection)
    {
        mir_connection_release(this->hidden->connection);
        this->hidden->connection = NULL;
    }

    Mir_ModeListFree(this);
}
int Mir_VideoInit(_THIS, SDL_PixelFormat *vformat)
{
    this->hidden->connection = mir_connect_sync(NULL, __PRETTY_FUNCTION__);

    if (!mir_connection_is_valid(this->hidden->connection))
    {
        SDL_SetError("Failed to connect to the Mir Server: %s",
                     mir_connection_get_error_message(this->hidden->connection));
        mir_connection_release(this->hidden->connection);
        return -1;
    }

    MirPixelFormat formats[mir_pixel_formats];
    Uint32 n_formats;

    mir_connection_get_available_surface_formats (this->hidden->connection, formats,
                                                  mir_pixel_formats, &n_formats);

    if (n_formats == 0 || formats[0] == mir_pixel_format_invalid)
    {
        SDL_SetError("No valid pixel formats found");
        mir_connection_release(this->hidden->connection);
        return -1;
    }

    this->hidden->pixel_format = formats[0];
    vformat->BitsPerPixel = MIR_BYTES_PER_PIXEL(this->hidden->pixel_format) * 8;

    Mir_InitQueue(this->hidden->buffer_queue);
    Mir_ModeListUpdate(this);
    mir_connection_set_display_config_change_callback(this->hidden->connection,
                                                      Mir_DisplayConfigChanged, this);

    this->info.wm_available = 1;

    return 0;
}