#ifndef __GST_WESTONIMAGESRC_H__
#define __GST_WESTONIMAGESRC_H__

#include <gst/base/gstbasesrc.h>
#include <gst/gst.h>
#include <gst/video/video.h>

#include <stdint.h>
#include <errno.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <limits.h>
#include <sys/param.h>
#include <sys/mman.h>
#include <wayland-client.h>
#include <wayland-util.h>

#include "config.h"
#include "glib.h"
#include "weston-screenshooter-client-protocol.h"
#include "shared/os-compatibility.h"
#include "shared/xalloc.h"
#include "shared/file-util.h"

G_BEGIN_DECLS

#define GST_TYPE_WESTONIMAGESRC (gst_westonimagesrc_get_type())
#define GST_WESTONIMAGESRC(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj), GST_TYPE_WESTONIMAGESRC, GstWestonImageSrc))
#define GST_WESTONIMAGESRC_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass), GST_TYPE_WESTONIMAGESRC, GstWestonImageSrcClass))
#define GST_IS_WESTONIMAGESRC(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj), GST_TYPE_WESTONIMAGESRC))
#define GST_IS_WESTONIMAGESRC_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass), GST_TYPE_WESTONIMAGESRC))

typedef struct _GstWestonImageSrc GstWestonImageSrc;
typedef struct _GstWestonImageSrcClass GstWestonImageSrcClass;

struct _GstWestonImageSrc {
  GstBaseSrc element;

  // Video
  GstVideoInfo video_info;
  gint width;
  gint height;
  GstVideoFormat format;
  
  // Properties
  gint framerate_n, framerate_d;
  GstClockTime duration;

  // Image data
  gpointer image_data;
  gsize image_size;

  // Sync
  GMutex lock;
  GCond cond;
  gboolean running;
  guint timeout_id;
  guint64 offset;
  guint64 frame_number;
  gboolean do_timestamp;

  // Remote control
  gint uinput_fd, udp_socket_fd;
  GThread *listener_thread;
  
  // Stat
  GstClockTime last_timestamp;
};

struct _GstWestonImageSrcClass {
  GstBaseSrcClass parent_class;
};

GType gst_westonimagesrc_get_type(void);

G_END_DECLS

// Screenshoter logic related declarations and defines
struct screenshooter_output {
  struct wl_output *output;
  struct wl_buffer *buffer;
  int width, height, offset_x, offset_y;
  void *data;
  struct wl_list link;
};

struct buffer_size {
  int width, height;
  int min_x, min_y;
  int max_x, max_y;
};

struct screenshooter_data {
  struct wl_shm *shm;
  struct wl_list output_list;
  struct weston_screenshooter *screenshooter;
  int buffer_copy_done;
};

// Weston related structures
struct wl_display *display;
struct wl_registry *registry;
struct screenshooter_output *output;
struct buffer_size buff_size = {};
struct screenshooter_data sh_data = {};

#endif /* __GST_WESTONIMAGESRC_H__ */
