/*
    This file is part of libtermui.

    libtermui is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    libtermui is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with libtermui.  If not, see <http://www.gnu.org/licenses/>.

    Copyright 2010, Alexandre Becoulet <alexandre.becoulet@free.fr>

*/

#ifndef EMU_PV_H_
#define EMU_PV_H_

#include <termui/types.h>

struct termui_vtemu_s;

#define TERMUI_VTEMU_STATE(n) void (n)(struct termui_vtemu_s *ctx, uint8_t c)
typedef TERMUI_VTEMU_STATE(termui_vtemu_state_t);

#define TERMUI_VTEMU_MAXPARAMS 2
#define TERMUI_VTEMU_INTERCHAR 2

struct termui_vtemu_s
{
  termui_vtemu_state_t *st;
  int_fast16_t         params[TERMUI_VTEMU_MAXPARAMS];
  termui_size_t        params_count;
  uint8_t              intermediate[TERMUI_VTEMU_INTERCHAR];
  termui_size_t        intermediate_count;

#ifdef CONFIG_LIBTERMUI_VTEMU_DEBUG
  const char           *func;
#endif
};

struct termui_emu_s;

#define TERMUI_EMU_FCN_PUSHCHAR(f)	\
  termui_err_t f(struct termui_emu_s *te, uint8_t c)

#define TERMUI_EMUIO_FCN_SETSIZE(f)	\
  termui_err_t f(struct termui_emu_s *te, termui_cursor_pos_t x, termui_cursor_pos_t y)

#define TERMUI_EMUIO_FCN_WRITE(f)	\
  void f(struct termui_emu_s *te, const void *data, termui_size_t size)

#define TERMUI_EMUIO_FCN_READ(f)	\
  int_fast16_t f(struct termui_emu_s *te)

#define TERMUI_EMUIO_FCN_CLEANUP(f)	\
  void f(struct termui_emu_s *te)

typedef TERMUI_EMU_FCN_PUSHCHAR(termui_emu_pushchar_t);
typedef TERMUI_EMUIO_FCN_SETSIZE(termui_emuio_setsize_t);
typedef TERMUI_EMUIO_FCN_WRITE(termui_emuio_write_t);
typedef TERMUI_EMUIO_FCN_READ(termui_emuio_read_t);
typedef TERMUI_EMUIO_FCN_CLEANUP(termui_emuio_cleanup_t);

struct termui_emu_s
{
  struct termui_term_s     *term;

  termui_emu_pushchar_t    *e_pushchar;

  termui_emuio_setsize_t   *c_setsize;
  termui_emuio_read_t      *c_read;
  termui_emuio_write_t     *c_write;
  termui_emuio_cleanup_t   *c_cleanup;

  union {
    struct termui_vtemu_s  vt;
  }			   emu;

  union {
    int t;
  }			   io;
};

termui_err_t termui_emu_vt_init(struct termui_emu_s *te, struct termui_term_s *term);

termui_err_t termui_emuio_pty_init(struct termui_emu_s *te, int32_t fd);

#endif

