#include <daguerre/framebuffer.hpp>
#include <daguerre/ppm.hpp>
#include "renderer.hpp"
#include "socket.hpp"
#include "input.hpp"
#include "main.hpp"

//TODO: handle errors

renderer *r;

int main(int, char **) {

  euler::syscall::listener_handle listener;
  euler::syscall::create_socket_listener("hilbert.compositor", listener);

  r = new renderer(
    daguerre::get_hilbert_framebuffer(),
    *daguerre::try_load_ppm("/assets/background.ppm"),
    euler::syscall::encode_color(0x00, 0x00, 0x00),
    *daguerre::try_load_ppm("/assets/pointer.ppm"),
    euler::syscall::encode_color(0xff, 0x00, 0xff));

  euler::syscall::start_thread([]() { r->render_thread_main(); });
  euler::syscall::start_thread(input_thread_main);

  r->dispatch_render();

  euler::syscall::set_thread_name("socket listener thread");

  while (true) {
    euler::syscall::stream_handle socket;
    euler::syscall::accept_socket_connection(listener, socket);
    euler::syscall::start_thread(socket_thread_main, socket);
  }

}
