/*
*Copyright(c)2016, 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.
*/

#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <oslib/filter.h>
#include <oslib/wimp.h>
#include "filter.h"
#include "header.h"
#include "debug.h"
#include "proto2.h"
#include "areas.h"
#include "dirtyrect.h"

static bool init = false;
static dirtyrect_buffer dirty;
static area pending_rect;
static area old_caret;

#define CARET_WIDTH 8

static void get_caret(area *a)
{
  reset_area(a);
  wimp_caret caret;
  if (xwimp_get_caret_position(&caret))
  {
    return;
  }
  if ((caret.w == wimp_NO_ICON) || (caret.height == -1) || ((caret.height & 0xffff) == 0))
  {
    return;
  }
  a->w = CARET_WIDTH >> os_state.myscreen.xeig;
  a->h = ((caret.height & 0xffff) >> os_state.myscreen.yeig) + 1;
  wimp_window_state win;
  win.w = caret.w;
  if (xwimp_get_window_state(&win))
  {
    return;
  }
  a->x = (caret.pos.x + win.visible.x0 - win.xscroll - (CARET_WIDTH/2)) >> os_state.myscreen.xeig;
  a->y = os_state.myscreen.height - ((caret.pos.y + win.visible.y1 - win.yscroll) >> os_state.myscreen.yeig);
  a->y -= a->h;
}

void filter_init()
{
  if (init)
  {
    return;
  }
  init = true;
  dirtyrect_init(&dirty);
  reset_area(&pending_rect);
  if (xfilter_register_rect_filter(Module_Title, rect_filter_entry, private_word, filter_ALL_TASKS)
   || xfilter_register_copy_filter(Module_Title, copy_filter_entry, private_word)
   || xfilter_register_pre_filter(Module_Title, poll_filter_entry, private_word, filter_ALL_TASKS))
  {
    filter_shutdown();
  }
}

void filter_shutdown()
{
  if (!init)
  {
    return;
  }
  xfilter_de_register_rect_filter(Module_Title, rect_filter_entry, private_word, filter_ALL_TASKS);
  xfilter_de_register_copy_filter(Module_Title, copy_filter_entry, private_word);
  xfilter_de_register_pre_filter(Module_Title, poll_filter_entry, private_word, filter_ALL_TASKS);
  init = false;
}

void filter_update(vnc_client_t *client)
{
  area a;
  dirtyrect_next(&dirty, &a);
  while (!area_is_empty(&a))
  {
    server_framebuffer_changed(client, &a, false);
    dirtyrect_next(&dirty, &a);
  }
  /* Track Wimp caret movements (caret redraws aren't notified by filter manager) */
  get_caret(&a);
  if (!area_is_empty(&a))
  {
    server_framebuffer_changed(client, &a, true);
  }
  if (memcmp(&a, &old_caret, sizeof(area)))
  {
    if (!area_is_empty(&old_caret))
    {
      server_framebuffer_changed(client, &old_caret, false);
    }
    old_caret = a;
//    dprintf("new caret: (%d,%d) (%d,%d)\n",a.x,a.y,a.w,a.h);
  }
}

bool filter_is_init()
{
  return init;
}

/* There is no true 'post' filter for redraws, so we remember the most recent rectangle and only add it to the dirty list once we receive the next call (or poll event for the case of the last rect in a redraw sequence) */

static void flush_pending()
{
  if (!area_is_empty(&pending_rect))
  {
    dirtyrect_add(&dirty,&pending_rect);
    reset_area(&pending_rect);
  }
}

int rect_filter_handler(_kernel_swi_regs *r, void *pw)
{
  flush_pending();
//  dprintf("rect filter: %08x (%d,%d) (%d,%d)\n",r->r[2],r->r[6],r->r[7],r->r[8],r->r[9]);
  pending_rect.x = r->r[6] >> os_state.myscreen.xeig;
  pending_rect.w = (r->r[8] - r->r[6]) >> os_state.myscreen.xeig;
  pending_rect.y = os_state.myscreen.height - (r->r[9] >> os_state.myscreen.yeig);
  pending_rect.h = (r->r[9] - r->r[7]) >> os_state.myscreen.yeig;
  return 1;
}

int copy_filter_handler(_kernel_swi_regs *r, void *pw)
{
  flush_pending();
//  dprintf("copy filter: %08x (%d,%d) (%d,%d) (%d,%d) (%d,%d)\n",r->r[0],r->r[2],r->r[3],r->r[4],r->r[5],r->r[6],r->r[7],r->r[8],r->r[9]);
  pending_rect.x = r->r[2] >> os_state.myscreen.xeig;
  pending_rect.w = (r->r[4] - r->r[2]) >> os_state.myscreen.xeig;
  pending_rect.y = os_state.myscreen.height - (r->r[5] >> os_state.myscreen.yeig);
  pending_rect.h = (r->r[5] - r->r[3]) >> os_state.myscreen.yeig;
  return 1;
}

int poll_filter_handler(_kernel_swi_regs *r, void *pw)
{
  flush_pending();
  return 1;
}
