/*
 * UPIWIN - Micro Pi Windowing Framework Kernel
 * Copyright (C) 2019 Amy Bowersox/Erbosoft Metaverse Design Solutions
 *
 * This program 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 2 of the License, or
 * (at your option) any later version.
 *
 * This program 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 this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *-------------------------------------------------------------------------
 */
#define PY_SSIZE_T_CLEAN
#include <Python.h>
#include "scode.h"
#include "msg.h"
#include "gpio.h"
#include "ep_init.h"
#include "ep_upiwin.h"
#include "ep_util.h"
#include "devctxt.h"

static PyMethodDef UPIWINMethods[] = {
  /* Backlight control functions */
  {"get_backlight", Epython_get_backlight, METH_VARARGS,
   "Returns the current status of the backlight (True=on, False=off)."},
  {"set_backlight", Epython_set_backlight, METH_VARARGS,
   "Sets the current status of the backlight (True=on, False=off). Returns a SCODE."},
  {"get_backlight_level", Epython_get_backlight_level, METH_VARARGS,
   "Returns the current intensity level of the backlight."},
  {"set_backlight_level", Epython_set_backlight_level, METH_VARARGS,
   "Sets the current intensity level of the backlight. Returns a SCODE."},
  /* Message functions */
  {"get_message", Epython_get_message, METH_VARARGS,
   "Retrieves a message from the message queue, blocking if necessary."},
  {"post_quit_message", Epython_post_quit_message, METH_VARARGS,
   "Posts a WM_QUIT message to the message queue, with an exit code."},
  /* Graphics functions */
  {"rgb", Epython_rgb, METH_VARARGS,
   "Creates a color value from separate red, green, and blue indexes."},
  {NULL, NULL, 0, NULL}
};

static PyModuleDef DefUPIWIN = {
  PyModuleDef_HEAD_INIT,     /* standard garbage */
  MOD_NAME_UPIWIN,           /* module name */
  NULL,                      /* no doc string */
  sizeof(UPIWIN_STATE),      /* per-module memory */
  UPIWINMethods,             /* method defs */
  NULL,                      /* no slots for multi-phase init */
  NULL,                      /* no traversal proc */
  NULL,                      /* no clear function */
  NULL                       /* no free function */
};

BEGIN_CONSTANT_TABLE(UPIWINConstants)
  /* Message constants */
  CONSTANT_INT_MACRO(WM_NULL)
  CONSTANT_INT_MACRO(WM_QUIT)
  CONSTANT_INT_MACRO(WM_HWBUTTONDOWN)
  CONSTANT_INT_MACRO(WM_HWBUTTONUP)
  CONSTANT_INT_MACRO(WM_HWBUTTONCLICK)
  CONSTANT_INT_MACRO(WM_TOUCHDOWN)
  CONSTANT_INT_MACRO(WM_TOUCHMOVE)
  CONSTANT_INT_MACRO(WM_TOUCHUP)
  CONSTANT_INT_MACRO(WM_TOUCHCLICK)
  /* Raster op constants */
  CONSTANT_INT_MACRO(R2_BLACK)
  CONSTANT_INT_MACRO(R2_NOTMERGEPEN)
  CONSTANT_INT_MACRO(R2_MASKNOTPEN)
  CONSTANT_INT_MACRO(R2_NOTCOPYPEN)
  CONSTANT_INT_MACRO(R2_MASKPENNOT)
  CONSTANT_INT_MACRO(R2_NOT)
  CONSTANT_INT_MACRO(R2_XORPEN)
  CONSTANT_INT_MACRO(R2_NOTMASKPEN)
  CONSTANT_INT_MACRO(R2_MASKPEN)
  CONSTANT_INT_MACRO(R2_NOTXORPEN)
  CONSTANT_INT_MACRO(R2_NOP)
  CONSTANT_INT_MACRO(R2_MERGENOTPEN)
  CONSTANT_INT_MACRO(R2_COPYPEN)
  CONSTANT_INT_MACRO(R2_MERGEPENNOT)
  CONSTANT_INT_MACRO(R2_MERGEPEN)
  CONSTANT_INT_MACRO(R2_WHITE)
END_CONSTANT_TABLE

PyObject *Epython_init_upiwin_module(void)
{
  PyObject *module;
  PUPIWIN_STATE pstate;

  module = PyModule_Create(&DefUPIWIN);
  if (!module)
    return NULL;

  if (FAILED(Epython_register_constants(module, UPIWINConstants)))
  {
    Py_DECREF(module);
    return NULL;
  }

  if (FAILED(Epython_register_bitmap(module)))
  {
    Py_DECREF(module);
    return NULL;
  }

  if (FAILED(Epython_register_devctxt(module)))
  {
    Py_DECREF(module);
    return NULL;
  }

  /* set up the module state */
  pstate = (PUPIWIN_STATE)PyModule_GetState(module);
  pstate->backlight_on = TRUE;
  pstate->backlight_level = GSB_BACKLIGHT_DEFAULT;

  return module;
}
