The Cursor object

This commit is contained in:
Kovid Goyal
2016-10-31 22:48:36 +05:30
parent 1f55af6691
commit 3d13ad3411
4 changed files with 129 additions and 1 deletions

100
kitty/cursor.c Normal file
View File

@@ -0,0 +1,100 @@
/*
* cursor.c
* Copyright (C) 2016 Kovid Goyal <kovid at kovidgoyal.net>
*
* Distributed under terms of the GPL3 license.
*/
#include "data-types.h"
#include <structmember.h>
#define INIT_NONE(x) Py_INCREF(Py_None); x = Py_None;
static PyObject *
Cursor_new(PyTypeObject *type, PyObject UNUSED *args, PyObject UNUSED *kwds) {
Cursor *self;
self = (Cursor *)type->tp_alloc(type, 0);
if (self != NULL) {
INIT_NONE(self->shape);
INIT_NONE(self->blink);
INIT_NONE(self->color);
self->hidden = Py_False; Py_INCREF(Py_False);
self->bold = 0; self->italic = 0; self->reverse = 0; self->strikethrough = 0; self->decoration = 0;
self->fg = 0; self->bg = 0; self->decoration_fg = 0;
self->x = PyLong_FromLong(0); self->y = PyLong_FromLong(0);
if (self->x == NULL || self->y == NULL) { Py_DECREF(self); self = NULL; }
}
return (PyObject*) self;
}
static void
Cursor_dealloc(Cursor* self) {
Py_XDECREF(self->shape);
Py_XDECREF(self->blink);
Py_XDECREF(self->color);
Py_XDECREF(self->hidden);
Py_XDECREF(self->x);
Py_XDECREF(self->y);
Py_TYPE(self)->tp_free((PyObject*)self);
}
// Boilerplate {{{
static PyMemberDef Cursor_members[] = {
{"x", T_OBJECT_EX, offsetof(Cursor, x), 0, "x"},
{"y", T_OBJECT_EX, offsetof(Cursor, y), 0, "y"},
{"shape", T_OBJECT_EX, offsetof(Cursor, shape), 0, "shape"},
{"blink", T_OBJECT_EX, offsetof(Cursor, blink), 0, "blink"},
{"color", T_OBJECT_EX, offsetof(Cursor, color), 0, "color"},
{"hidden", T_OBJECT_EX, offsetof(Cursor, hidden), 0, "hidden"},
{NULL} /* Sentinel */
};
static PyMethodDef Cursor_methods[] = {
{NULL} /* Sentinel */
};
PyTypeObject Cursor_Type = {
PyVarObject_HEAD_INIT(NULL, 0)
"fast_data_types.Cursor",
sizeof(Cursor),
0, /* tp_itemsize */
(destructor)Cursor_dealloc, /* tp_dealloc */
0, /* tp_print */
0, /* tp_getattr */
0, /* tp_setattr */
0, /* tp_reserved */
0, /* tp_repr */
0, /* tp_as_number */
0, /* tp_as_sequence */
0, /* tp_as_mapping */
0, /* tp_hash */
0, /* tp_call */
0, /* tp_str */
0, /* tp_getattro */
0, /* tp_setattro */
0, /* tp_as_buffer */
Py_TPFLAGS_DEFAULT, /* tp_flags */
"Cursors", /* tp_doc */
0, /* tp_traverse */
0, /* tp_clear */
0, /* tp_richcompare */
0, /* tp_weaklistoffset */
0, /* tp_iter */
0, /* tp_iternext */
Cursor_methods, /* tp_methods */
Cursor_members, /* tp_members */
0, /* tp_getset */
0, /* tp_base */
0, /* tp_dict */
0, /* tp_descr_get */
0, /* tp_descr_set */
0, /* tp_dictoffset */
0, /* tp_init */
0, /* tp_alloc */
Cursor_new, /* tp_new */
};
// }}

View File

@@ -8,6 +8,7 @@
#include "data-types.h"
extern PyTypeObject LineBuf_Type;
extern PyTypeObject Cursor_Type;
static PyMethodDef module_methods[] = {
{NULL, NULL, 0, NULL} /* Sentinel */
@@ -27,12 +28,15 @@ PyInit_fast_data_types(void) {
if (PyType_Ready(&LineBuf_Type) < 0) return NULL;
if (PyType_Ready(&Cursor_Type) < 0) return NULL;
m = PyModule_Create(&module);
if (m == NULL) return NULL;
if (m != NULL) {
Py_INCREF(&LineBuf_Type);
PyModule_AddObject(m, "LineBuf", (PyObject *)&LineBuf_Type);
Py_INCREF(&Cursor_Type);
PyModule_AddObject(m, "Cursor", (PyObject *)&Cursor_Type);
}
return m;

View File

@@ -19,7 +19,24 @@ typedef uint32_t decoration_type;
typedef uint32_t combining_type;
typedef unsigned int index_type;
#define CELL_SIZE (sizeof(char_type) + sizeof(color_type) + sizeof(decoration_type) + sizeof(combining_type))
#define CHAR_MASK 0xFFFFFF
#define ATTRS_SHIFT 24
#define WIDTH_MASK 0xFF
#define DECORATION_SHIFT 2
#define BOLD_SHIFT 4
#define ITALIC_SHIFT 5
#define REVERSE_SHIFT 6
#define STRIKE_SHIFT 7
#define DECORATION_MASK (1 << DECORATION_SHIFT)
#define BOLD_MASK (1 << BOLD_SHIFT)
#define ITALIC_MASK (1 << ITALIC_SHIFT)
#define REVERSE_MASK (1 << REVERSE_SHIFT)
#define STRIKE_MASK (1 << STRIKE_SHIFT)
#define COL_MASK 0xFFFFFFFF
#define COL_SHIFT 32
#define HAS_BG_MASK (0xFF << COL_SHIFT)
typedef struct {
PyObject_HEAD
@@ -37,4 +54,11 @@ typedef struct {
} LineBuf;
typedef struct {
PyObject_HEAD
PyObject *x, *y, *shape, *blink, *hidden, *color;
uint8_t bold, italic, reverse, strikethrough, decoration;
uint32_t fg, bg, decoration_fg;
} Cursor;

View File

@@ -68,4 +68,4 @@ if __name__ == '__main__':
if sys.version_info < (3, 5):
raise SystemExit('python >= 3.5 required')
init_env()
compile_c_extension('kitty/fast_data_types', 'kitty/data-types.c', 'kitty/line-buf.c')
compile_c_extension('kitty/fast_data_types', 'kitty/data-types.c', 'kitty/line-buf.c', 'kitty/cursor.c')