/*
*Copyright(c)2014, Jeffrey Lee
*Allrightsreserved.
*
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met: 
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
*/

#ifndef VNCPROTO_H
#define VNCPROTO_H

#include <stdint.h>
#include <stdbool.h>

/* Definitions related to the VNC protocol */

typedef enum {
  S2C_FramebufferUpdate = 0,
  S2C_SetColourMapEntries = 1,
  S2C_Bell = 2,
  S2C_ServerCutText = 3,
} S2C;

typedef enum {
  C2S_SetPixelFormat = 0,
  C2S_FixColourMapEntries = 1,
  C2S_SetEncodings = 2,
  C2S_FramebufferUpdateRequest = 3,
  C2S_KeyEvent = 4,
  C2S_PointerEvent = 5,
  C2S_ClientCutText = 6,
} C2S;

typedef struct {
  uint8_t bits_per_pixel;
  uint8_t depth;
  uint8_t big_endian_flag;
  uint8_t true_colour_flag;
  uint16_t red_max;
  uint16_t green_max;
  uint16_t blue_max;
  uint8_t red_shift;
  uint8_t green_shift;
  uint8_t blue_shift;
  uint8_t padding[3];
} PIXEL_FORMAT;

typedef struct {
  uint16_t x;
  uint16_t y;
  uint16_t w;
  uint16_t h;
  uint32_t enc;
} rect_header;

typedef struct {
  uint8_t type;
  uint8_t pad[3];
  uint32_t len;
} cuttext_header;

static inline bool PIXEL_FORMAT_is_CPIXEL(const PIXEL_FORMAT *fmt)
{
  /* Check if fmt is a ZRLE 3-byte 'CPIXEL' type */
  if (!fmt->true_colour_flag || (fmt->bits_per_pixel != 32) || (fmt->depth > 24))
  {
    return false;
  }
  uint32_t full = (((uint32_t) fmt->red_max) << fmt->red_shift) | (((uint32_t) fmt->green_max) << fmt->green_shift) | (((uint32_t) fmt->blue_max) << fmt->blue_shift);
  return !(full & 0xff) || !(full & 0xff000000);
}

#endif
