From 7567f96a347475db4202454acc3c9f262ff8c4a4 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Wed, 27 Sep 2017 20:18:41 +0530 Subject: [PATCH] Python API to get image data from gr. manager for tests --- kitty/graphics.c | 31 +++++++++++++++++++++++++++++++ kitty/screen.c | 1 + 2 files changed, 32 insertions(+) diff --git a/kitty/graphics.c b/kitty/graphics.c index 49036f07a..75791bc6b 100644 --- a/kitty/graphics.c +++ b/kitty/graphics.c @@ -413,6 +413,36 @@ dealloc(GraphicsManager* self) { grman_free(self); } +static inline PyObject* +image_as_dict(Image *img) { +#define U(x) #x, img->x + return Py_BuildValue("{sI sI sI sI sI sI sH sH sN}", + U(texture_id), U(client_id), U(width), U(height), U(internal_id), U(refcnt), U(data_loaded), + "is_4byte_aligned", img->load_data.is_4byte_aligned, + "data", Py_BuildValue("y#", img->load_data.data, img->load_data.data_sz) + ); +#undef U + +} + +#define W(x) static PyObject* py##x(GraphicsManager *self, PyObject *args) +#define PA(fmt, ...) if(!PyArg_ParseTuple(args, fmt, __VA_ARGS__)) return NULL; + +W(image_for_client_id) { + unsigned long id = PyLong_AsUnsignedLong(args); + bool existing = false; + Image *img = find_or_create_image(self, id, &existing); + if (existing) { Py_RETURN_NONE; } + return image_as_dict(img); +} + +#define M(x, va) {#x, (PyCFunction)py##x, va, ""} + +static PyMethodDef methods[] = { + M(image_for_client_id, METH_O), + {NULL} /* Sentinel */ +}; + PyTypeObject GraphicsManager_Type = { PyVarObject_HEAD_INIT(NULL, 0) @@ -422,6 +452,7 @@ PyTypeObject GraphicsManager_Type = { .tp_flags = Py_TPFLAGS_DEFAULT, .tp_doc = "GraphicsManager", .tp_new = new, + .tp_methods = methods, }; bool diff --git a/kitty/screen.c b/kitty/screen.c index 82a55df7e..f890fe196 100644 --- a/kitty/screen.c +++ b/kitty/screen.c @@ -1573,6 +1573,7 @@ static PyGetSetDef getsetters[] = { static PyMemberDef members[] = { {"callbacks", T_OBJECT_EX, offsetof(Screen, callbacks), 0, "callbacks"}, {"cursor", T_OBJECT_EX, offsetof(Screen, cursor), READONLY, "cursor"}, + {"grman", T_OBJECT_EX, offsetof(Screen, grman), READONLY, "grman"}, {"color_profile", T_OBJECT_EX, offsetof(Screen, color_profile), READONLY, "color_profile"}, {"linebuf", T_OBJECT_EX, offsetof(Screen, linebuf), READONLY, "linebuf"}, {"historybuf", T_OBJECT_EX, offsetof(Screen, historybuf), READONLY, "historybuf"},