/* * Copyright (C) 2007 Kevin Cody Jr. <kcody@vegaresearch.com> * All rights reserved. * * Note, my email systems are broken right now, I can be reached * on FreeNode IRC as user "kevincody". * * This is an early API targeted for eventual inclusion * in the G* suite of programming libraries. Permission to * distribute is granted solely for developmental and * experimental purposes to that end. * * The author offers an open binding contract, that upon completion * of the interface specification, all work will be released under * the LGPL, version is a subject for debate. Until then, however, * it is required that any further development or derivative work * be submitted back to me, and furthermore, only be circulated * within the community of developers actively working the project. * * Final public licensing must account for the licensing requirements * of glib-2 and the current ncurses library. Someone should probably * ask the EFF or whoever, but curses is part of most UNIXes one way * or another, so I can't imagine why LGPL would be a problem. * * --- * * GCurses: * * A GObject-based wrapper around the venerable curses library * for character-cell terminal manipulation. * * This class intends to expose the full functionality of curses * through a purely glib/gobject interface, while reducing the * overwhelming redundancy of the curses API. * * Major Changes: * * * The notion of a default screen is deprecated, replaced by * a constructor that binds to the controlling terminal. * * * Character attributes (including color) and cursor position * can be controlled via GObject properties, as well as normal * set/get/etc. method calls. * * * Character set translation is organic to the object. * * * scanf and friends are omitted as being a bad idea for stack * integrity, and as being ill suited to terminal applications * * * Prefers to use 32-bit unicode internally. * */#ifndef __G_CURSES_H__#define __G_CURSES_H__#include <ncursesw/curses.h>#include <glib-object.h>G_BEGIN_DECLS/* * GObject boilerplate */GType g_curses_get_type (void);#define G_CURSES_TYPE (g_curses_get_type ())#define G_CURSES(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), G_CURSES_TYPE, GCurses))#define G_CURSES_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), G_CURSES_TYPE, GCursesClass))#define IS_G_CURSES(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), G_CURSES_TYPE))#define IS_G_CURSES_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), G_CURSES_TYPE))#define GET_G_CURSES_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), G_CURSES_TYPE, GCursesClass))/* * Colors */typedef enum { G_CURSES_COLOR_BLACK, G_CURSES_COLOR_RED, G_CURSES_COLOR_GREEN, G_CURSES_COLOR_YELLOW, G_CURSES_COLOR_BLUE, G_CURSES_COLOR_MAGENTA, G_CURSES_COLOR_CYAN, G_CURSES_COLOR_WHITE} GCursesColor;typedef guint8 GCursesColorPair;/* * Supported Attributes */typedef enum { G_CURSES_ATTR_NORMAL = 0x00000000, G_CURSES_ATTR_STANDOUT = 0x00000100, G_CURSES_ATTR_UNDERLINE = 0x00000200, G_CURSES_ATTR_REVERSE = 0x00000400, G_CURSES_ATTR_BLINK = 0x00000800, G_CURSES_ATTR_DIM = 0x00001000, G_CURSES_ATTR_BOLD = 0x00002000,} GCursesAttr;/* * Cursor modes */typedef enum { G_CURSES_CURSOR_NONE, G_CURSES_CURSOR_NORMAL, G_CURSES_CURSOR_BRIGHT} GCursesCursor;/* * Event masks */typedef enum { G_CURSES_MOUSE_NONE = 0, G_CURSES_MOUSE_CLICK = 1, G_CURSES_MOUSE_DOUBLE = 2, G_CURSES_MOUSE_TRIPLE = 4, G_CURSES_MOUSE_DRAGDROP = 8, G_CURSES_MOUSE_MOVEMENT = 16} GCursesMouseEventType;typedef enum { G_CURSES_KEY_MOD_NONE = 0, G_CURSES_KEY_MOD_SHIFT = 1, G_CURSES_KEY_MOD_CTRL = 2, G_CURSES_KEY_MOD_ALT = 4} GCursesKeyModifier;/* * Event structures */typedef struct { GCursesMouseEventType type; GCursesKeyModifier modifier; guint button; guint y; guint x;} GCursesMouseEvent;typedef struct { gunichar key; guchar ascii; GCursesKeyModifier modifier;} GCursesKeyEvent;/* * Callback Prototypes *//* Mouse events */typedef void (*GCursesMouseEventFunc) (GCurses *self, GCursesMouseEvent *ev, gpointer user_data);/* Keyboard events */typedef void (*GCursesKeyEventFunc) (GCurses *self, GCursesKeyEvent *ev, gpointer user_data);/* * ncurses wrapper object */typedef struct { GObject parent; /* constructor properties */ gint _outfd; /* display file descriptor */ gint _infd; /* keyboard file descriptor */ /* read-write properties */ gchar *charset; /* output stream charset */ gchar *termset; /* physical terminal charset */ gchar *keyset; /* physical keyboard type */ /* private members */ FILE *outfd; /* display file handle */ FILE *infd; /* keyboard file handle */ SCREEN *screen; /* native curses screen handle */ GConv *gconv; /* charset translation handle */} GCurses;typedef struct { GObjectClass parent; /* ncurses can only translate into the "current" charset */ gchar *current_locale; /* ncurses can only draw to the "current" screen */ SCREEN *current_screen;} GCursesClass;/* * Constructors *//* base case constructor takes a read and write file descriptor * note these are integer fd's, not the FILE structs used by curses */GCurses *g_curses_new_fds (gint out_fd, gint in_fd);/* wrapper to open a single bidirectional file descriptor */static inline GCurses *g_curses_new_fd (gint fd) { return g_curses_new_fds( fd, fd );}/* wrapper to open the controlling terminal */static inline GCurses *g_curses_new (void) { return g_curses_new_fds( 1, 0 );}/* * Simple Unicode I/O Methods * * Also used for single-byte character modes. * Upper bytes always NULL if keypad is FALSE. *//* print a single character */gbooleang_curses_putch (GCurses *self, gunichar ch, GError **error);/* read a single keystroke */gunicharg_curses_getch (GCurses *self, GError **error);/* stuff a single keystroke */gbooleang_curses_ungetch (GCurses *self, gunichar ch, GError **error);/* * String Unicode Output Methods *//* print a null-terminated unicode string */gbooleang_curses_putustr (GCurses *self, const gunichar *str, GError **error);/* print an unterminated unicode string */gbooleang_curses_putunstr (GCurses *self, const gunichar *str, gsize num, GError **error);/* * String Single-Byte Output Methods *//* print a null-terminatedstring */gbooleang_curses_putstr (GCurses *self, const gchar *str, GError **error);/* print an unterminatedstring */gbooleang_curses_putnstr (GCurses *self, const gchar *str, gsize num, GError **error);/* * Character normalization methods *//* specifies the encoding type for subsequent put* * any mismatch with the underlying terminal will be translated */gbooleang_curses_set_charset (GCurses *self, const gchar *charset, GError **error);/* informs curses of the charset used by the terminal */gbooleang_curses_set_termset (GCurses *self, const gchar *termset, GError **error);/* informs curses of the underlying terminal emulation */gbooleang_curses_set_termtype (GCurses *self, const gchar *termtype, GError **error);/* informs curses of the underlying keyboard type */gbooleang_curses_set_keyboard (GCurses *self, const gchar *keyboard, GError **error);/* sets up asynchronous keyboard i/o, requires glib mainloop */gbooleang_curses_set_callback (GCurses *self, GCursesKeyEventFunc *func, gpointer user_data, GError **error);/* * Attribute Methods *//* add the argument bits to the current attribute */gbooleang_curses_attr_on (GCurses *self, GCursesAttr attr);/* remove the argument bits from the current attribute */gbooleang_curses_attr_off (GCurses *self, GCursesAttr attr);/* set the current attribute exactly equal to the argument bits */gbooleang_curses_attr_set (GCurses *self, GCursesAttr attr);/* query the current attribute value */GCursesAttrg_curses_attr_get (GCurses *self);/* set the attribute value for a series of pre-displayed characters */gbooleang_curses_attr_change (GCurses *self, GCurses attr, gsize n);/* * High-Level Color Methods *//* sets up color map for _fore, _back, _line, _blink */gbooleang_curses_color_init (GCurses *self);/* sets foreground color */gbooleang_curses_color_set_fore (GCurses *self, GCursesColor f);/* sets background color */gbooleang_curses_color_set_back (GCurses *self, GCursesColor b);/* sets blinking characters (i know...) */gbooleang_curses_color_set_blink (GCurses *self, gboolean blink);/* sets underlined characters */gbooleang_curses_color_set_underline (GCurses *self, gboolean uline);/* * Low-Level Color Methods *//* sets the RGB values for a color */gbooleang_curses_color_value_set (GCurses *self, GCursesColor color, guint16 red, guint16 green, guint16 blue);/* gets the RGB values for a color */gbooleang_curses_color_value_get (GCurses *self, GCursesColor color, guint16 *red, guint16 *green, guint16 *blue);/* manually set up a color pair, unnecessary with _init above */gbooleang_curses_color_pair_set (GCurses *self, GCursesColorPair pair, GCursesColor fore, GCursesColor back);/* query the fore+back values of a color pair */gbooleang_curses_color_pair_get (GCurses *self, GCursesColorPair pair, GCursesColor *fore, GCursesColor *back);/* * Cursor Control Methods */gbooleang_curses_cursor_getyx (GCurses *self, guint *y, guint *x);gbooleang_curses_cursor_move (GCurses *self, guint y, guint x);gbooleang_curses_cursor_set (GCurses *self, GCursesCursor state);/* * Key Management Methods *//* for getch; -1 = blocking, 0 = nonblocking, >0 = milliseconds delay */gbooleang_curses_set_delay_time (GCurses *self, gint delay);/* sets whether curses interprets non-ascii keys, see KEY_* in curses.h */gbooleang_curses_set_keypad (GCurses *self, gboolean keypad);/* sets the terminal to character-at-a-time mode */gbooleang_curses_set_cbreak (GCurses *self, gboolean cbreak);/* sets whether curses echoes the keypresses */gbooleang_curses_set_echo (GCurses *self, gboolean echo);/* similar to cbreak, except -all- keys are seen by the application */gbooleang_curses_set_raw (GCurses *self, gboolean raw);/* * Mouse Methods */gbooleang_curses_mouse_set_callback (GCurses *self, GCursesMouseEventFunc *func, gpointer user_data);gbooleang_curses_mouse_set_events (GCurses *self, GCursesMouseEventType mask);GCursesMouseEventTypeg_curses_mouse_get_events (GCurses *self);/* * Capability Methods */gbooleang_curses_has_key (GCurses *self, gint key);gbooleang_curses_has_color (GCurses *self);gbooleang_curses_has_color_set (GCurses *self);gbooleang_curses_has_insdel_char (GCurses *self);gbooleang_curses_has_insdel_line (GCurses *self);G_END_DECLS#endif /* __G_CURSES_H__ */