From e64bcd5a81149c7ad5b5c39b7c1140d5f47ccc6b Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Tue, 16 Sep 2014 20:21:58 -0400 Subject: reimplement the api --- setup.py | 3 +- vt100.py | 119 ------------------------------------------------------ vt100/__init__.py | 78 +++++++++++++++++++++++++++++++++++ vt100module.c | 18 ++++----- 4 files changed, 89 insertions(+), 129 deletions(-) delete mode 100644 vt100.py create mode 100644 vt100/__init__.py diff --git a/setup.py b/setup.py index f80af6f..e1641a0 100644 --- a/setup.py +++ b/setup.py @@ -18,9 +18,10 @@ setup( author="Jesse Luehrs", author_email="doy@tozt.net", url="https://github.com/doy/vt100/", + packages=["vt100"], ext_modules=[ Extension( - name="vt100", + name="vt100_raw", sources=[ "vt100module.c", "libvt100/src/screen.c", diff --git a/vt100.py b/vt100.py deleted file mode 100644 index aef7b49..0000000 --- a/vt100.py +++ /dev/null @@ -1,119 +0,0 @@ -from ctypes import * - -libvt100 = CDLL("libvt100.so") - -class vt100_loc(Structure): - _fields_ = [ - ("row", c_int), - ("col", c_int), - ] - -class vt100_rgb_color(Union): - _fields_ = [ - ("r", c_ubyte), - ("g", c_ubyte), - ("b", c_ubyte), - ] - -class vt100_color(Structure): - _anonymous_ = ("rgb",) - _fields_ = [ - ("rgb", vt100_rgb_color), - ("type", c_ubyte), - ] - -class vt100_named_attrs(Structure): - _fields_ = [ - ("bold", c_ubyte, 1), - ("italic", c_ubyte, 1), - ("underline", c_ubyte, 1), - ("inverse", c_ubyte, 1), - ] - -class vt100_attrs(Union): - _anonymous_ = ("named",) - _fields_ = [ - ("named", vt100_named_attrs), - ("attrs", c_ubyte), - ] - -class vt100_cell_attrs(Structure): - _anonymous_ = ("cell_attrs",) - _fields_ = [ - ("fgcolor", vt100_color), - ("bgcolor", vt100_color), - ("cell_attrs", vt100_attrs), - ] - -class vt100_cell(Structure): - _fields_ = [ - ("_contents", c_char * 8), - ("len", c_size_t), - ("attrs", vt100_cell_attrs), - ("is_wide", c_ubyte, 1), - ] - - def contents(self): - return self._contents[:self.len].decode('utf-8') - -new_prototype = CFUNCTYPE(c_void_p, c_int, c_int) -vt100_new = new_prototype(("vt100_screen_new", libvt100)) - -set_window_size_prototype = CFUNCTYPE(None, c_void_p, c_int, c_int) -vt100_set_window_size = set_window_size_prototype(("vt100_screen_set_window_size", libvt100)) - -process_string_prototype = CFUNCTYPE(c_int, c_void_p, c_char_p, c_size_t) -vt100_process_string = process_string_prototype(("vt100_screen_process_string", libvt100)) - -cell_at_prototype = CFUNCTYPE(POINTER(vt100_cell), c_void_p, c_int, c_int) -vt100_cell_at = cell_at_prototype(("vt100_screen_cell_at", libvt100)) - -get_string_formatted_prototype = CFUNCTYPE(None, c_void_p, POINTER(vt100_loc), POINTER(vt100_loc), POINTER(c_char_p), POINTER(c_size_t)) -vt100_get_string_formatted = get_string_formatted_prototype(("vt100_screen_get_string_formatted", libvt100)) - -get_string_plaintext_prototype = CFUNCTYPE(None, c_void_p, POINTER(vt100_loc), POINTER(vt100_loc), POINTER(c_char_p), POINTER(c_size_t)) -vt100_get_string_plaintext = get_string_plaintext_prototype(("vt100_screen_get_string_plaintext", libvt100)) - -delete_prototype = CFUNCTYPE(None, c_void_p) -vt100_delete = delete_prototype(("vt100_screen_delete", libvt100)) - -# XXX process/cell need mutexes -class vt100(object): - def __init__(self, rows, cols): - self.vt = vt100_new(rows, cols) - - def __del__(self): - vt100_delete(self.vt) - - def set_window_size(self, rows, cols): - vt100_set_window_size(self.vt, rows, cols) - - def process(self, string): - return vt100_process_string(self.vt, string, len(string)) - - def get_string_formatted(self, row_start, col_start, row_end, col_end): - outstr = c_char_p() - outlen = c_size_t() - vt100_get_string_formatted( - self.vt, - byref(vt100_loc(row_start, col_start)), - byref(vt100_loc(row_end, col_end)), - byref(outstr), - byref(outlen), - ) - return string_at(outstr)[:outlen.value] - - def get_string_plaintext(self, row_start, col_start, row_end, col_end): - outstr = c_char_p() - outlen = c_size_t() - vt100_get_string_plaintext( - self.vt, - byref(vt100_loc(row_start, col_start)), - byref(vt100_loc(row_end, col_end)), - byref(outstr), - byref(outlen), - ) - return string_at(outstr)[:outlen.value] - - def cell(self, x, y): - return vt100_cell_at(self.vt, x, y).contents diff --git a/vt100/__init__.py b/vt100/__init__.py new file mode 100644 index 0000000..4e1a192 --- /dev/null +++ b/vt100/__init__.py @@ -0,0 +1,78 @@ +from ctypes import * + +import vt100_raw + +class vt100_rgb_color(Union): + _fields_ = [ + ("r", c_ubyte), + ("g", c_ubyte), + ("b", c_ubyte), + ] + +class vt100_color(Structure): + _anonymous_ = ("rgb",) + _fields_ = [ + ("rgb", vt100_rgb_color), + ("type", c_ubyte), + ] + +class vt100_named_attrs(Structure): + _fields_ = [ + ("bold", c_ubyte, 1), + ("italic", c_ubyte, 1), + ("underline", c_ubyte, 1), + ("inverse", c_ubyte, 1), + ] + +class vt100_attrs(Union): + _anonymous_ = ("named",) + _fields_ = [ + ("named", vt100_named_attrs), + ("attrs", c_ubyte), + ] + +class vt100_cell_attrs(Structure): + _anonymous_ = ("cell_attrs",) + _fields_ = [ + ("fgcolor", vt100_color), + ("bgcolor", vt100_color), + ("cell_attrs", vt100_attrs), + ] + +class vt100_cell(Structure): + _fields_ = [ + ("_contents", c_char * 8), + ("len", c_size_t), + ("attrs", vt100_cell_attrs), + ("is_wide", c_ubyte, 1), + ] + + def contents(self): + return self._contents[:self.len].decode('utf-8') + +# XXX process/cell need mutexes +class vt100(object): + def __init__(self, rows, cols): + self.vt = vt100_raw.new(rows, cols) + + def __del__(self): + vt100_raw.delete(self.vt) + + def set_window_size(self, rows, cols): + vt100_raw.set_window_size(self.vt, rows, cols) + + def process(self, string): + return vt100_raw.process_string(self.vt, string) + + def get_string_formatted(self, row_start, col_start, row_end, col_end): + return vt100_raw.get_string_formatted( + self.vt, row_start, col_start, row_end, col_end + ) + + def get_string_plaintext(self, row_start, col_start, row_end, col_end): + return vt100_raw.get_string_plaintext( + self.vt, row_start, col_start, row_end, col_end + ) + + def cell(self, x, y): + return cast(vt100_raw.cell_at(self.vt, x, y), vt100_cell) diff --git a/vt100module.c b/vt100module.c index b30f5f5..f564b87 100644 --- a/vt100module.c +++ b/vt100module.c @@ -108,25 +108,25 @@ static PyObject *py_vt100_delete(PyObject *self, PyObject *args) } static PyMethodDef vt100_methods[] = { - { "vt100_new", py_vt100_new, METH_VARARGS, "create a new vt100 object" }, - { "vt100_set_window_size", py_vt100_set_window_size, METH_VARARGS, "create a new vt100 object" }, - { "vt100_process_string", py_vt100_process_string, METH_VARARGS, "create a new vt100 object" }, - { "vt100_cell_at", py_vt100_cell_at, METH_VARARGS, "create a new vt100 object" }, - { "vt100_get_string_formatted", py_vt100_get_string_formatted, METH_VARARGS, "create a new vt100 object" }, - { "vt100_get_string_plaintext", py_vt100_get_string_plaintext, METH_VARARGS, "create a new vt100 object" }, - { "vt100_delete", py_vt100_delete, METH_VARARGS, "create a new vt100 object" }, + { "new", py_vt100_new, METH_VARARGS, "create a new vt100 object" }, + { "set_window_size", py_vt100_set_window_size, METH_VARARGS, "create a new vt100 object" }, + { "process_string", py_vt100_process_string, METH_VARARGS, "create a new vt100 object" }, + { "cell_at", py_vt100_cell_at, METH_VARARGS, "create a new vt100 object" }, + { "get_string_formatted", py_vt100_get_string_formatted, METH_VARARGS, "create a new vt100 object" }, + { "get_string_plaintext", py_vt100_get_string_plaintext, METH_VARARGS, "create a new vt100 object" }, + { "delete", py_vt100_delete, METH_VARARGS, "create a new vt100 object" }, { NULL, NULL, 0, NULL } }; static struct PyModuleDef vt100module = { PyModuleDef_HEAD_INIT, - "vt100", + "vt100_raw", NULL, -1, vt100_methods }; -PyMODINIT_FUNC PyInit_vt100() +PyMODINIT_FUNC PyInit_vt100_raw() { return PyModule_Create(&vt100module); } -- cgit v1.2.3-54-g00ecf