/*
 * Copyright (C) 2019 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#pragma once

#include <atomic>
#include <cstdint>
#include <functional>
#include <future>
#include <memory>
#include <string>
#include <thread>

#include "host/libs/wayland/wayland_server_callbacks.h"
#include "host/libs/wayland/wayland_surfaces.h"

namespace wayland {

namespace internal {
struct WaylandServerState;
}  // namespace internal

// A Wayland compositing server that provides an interface for receiving frame
// updates from a connected client.
class WaylandServer {
  public:
    // Creates a Wayland compositing server. If specified, uses the given
    // socket file descriptor to connect with clients. If provided, this
    // server will close the file descriptor upon exit.
   WaylandServer(int wayland_socket_fd = -1,
                 bool wayland_frames_are_rgba = true);
   virtual ~WaylandServer();

   WaylandServer(const WaylandServer& rhs) = delete;
   WaylandServer& operator=(const WaylandServer& rhs) = delete;

   WaylandServer(WaylandServer&& rhs) = delete;
   WaylandServer& operator=(WaylandServer&& rhs) = delete;

   // Registers the callback that will be run whenever a new frame is
   // available.
   void SetFrameCallback(Surfaces::FrameCallback callback);

   void SetDisplayEventCallback(DisplayEventCallback callback);

  private:
   void ServerLoop(int wayland_socket_fd, bool wayland_frames_are_rgba);

   bool server_ready_ = false;
   std::mutex server_ready_mutex_;
   std::condition_variable server_ready_cv_;

   std::thread server_thread_;
   std::unique_ptr<internal::WaylandServerState> server_state_;
};

}  // namespace wayland