/* -*- Mode: C; c-basic-offset: 4 -*- */ %% headers #include #define NO_IMPORT_PYGTK #include #include #include #include "../src/vte.h" void pyvte_add_constants(PyObject *module, const gchar *strip_prefix); void pyvte_register_classes(PyObject *d); %% modulename vte %% import gtk.gdk.Pixbuf as PyGdkPixbuf_Type import gtk.MenuShell as PyGtkMenuShell_Type import gtk.Widget as PyGtkWidget_Type %% override vte_terminal_feed kwargs static PyObject * _wrap_vte_terminal_feed(PyGObject *self, PyObject *args, PyObject *kwargs) { static char *kwlist[] = { "data", "length", NULL }; char *data; int length; PyObject *length_obj = NULL; if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s#|O:VteTerminal.feed", kwlist, &data, &length, &length_obj)) { return NULL; } if ((length_obj != NULL) && PyNumber_Check(length_obj)) { PyObject *intobj; intobj = PyNumber_Int(length_obj); if (intobj) { if (PyInt_AsLong(intobj) != -1) { length = PyInt_AsLong(intobj); } Py_DECREF(intobj); } } vte_terminal_feed(VTE_TERMINAL(self->obj), data, length); Py_INCREF(Py_None); return Py_None; } %% override vte_terminal_feed_child kwargs static PyObject * _wrap_vte_terminal_feed_child(PyGObject *self, PyObject *args, PyObject *kwargs) { static char *kwlist[] = { "data", "length", NULL }; char *data; int length; PyObject *length_obj = NULL; if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s#|O:VteTerminal.feed_child", kwlist, &data, &length, &length_obj)) { return NULL; } if ((length_obj != NULL) && PyNumber_Check(length_obj)) { PyObject *intobj; intobj = PyNumber_Int(length_obj); if (intobj) { if (PyInt_AsLong(intobj) != -1) { length = PyInt_AsLong(intobj); } Py_DECREF(intobj); } } vte_terminal_feed_child(VTE_TERMINAL(self->obj), data, length); Py_INCREF(Py_None); return Py_None; } %% override vte_terminal_feed_child_binary kwargs static PyObject * _wrap_vte_terminal_feed_child_binary(PyGObject *self, PyObject *args, PyObject *kwargs) { static char *kwlist[] = { "data", "length", NULL }; char *data; int length; PyObject *length_obj = NULL; if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s#|O:VteTerminal.feed_child_binary", kwlist, &data, &length, &length_obj)) { return NULL; } if ((length_obj != NULL) && PyNumber_Check(length_obj)) { PyObject *intobj; intobj = PyNumber_Int(length_obj); if (intobj) { if (PyInt_AsLong(intobj) != -1) { length = PyInt_AsLong(intobj); } Py_DECREF(intobj); } } vte_terminal_feed_child_binary(VTE_TERMINAL(self->obj), data, length); Py_INCREF(Py_None); return Py_None; } %% override vte_terminal_fork_command kwargs static int _build_envv(PyObject *py_envv, char ***envv) { int i, pos, n_envs; PyObject *py_dict_k, *py_dict_v; if (py_envv == NULL || py_envv == Py_None) { return 0; } if (PyDict_Check(py_envv)) { n_envs = PyMapping_Length(py_envv); *envv = g_new(gchar *, n_envs + 1); pos = 0; i = 0; while (PyDict_Next(py_envv, &pos, &py_dict_k, &py_dict_v)) { (*envv)[i++] = g_strdup_printf("%s=%s", PyString_AsString(py_dict_k), PyString_AsString(py_dict_v)); } (*envv)[n_envs] = NULL; } else if (PySequence_Check(py_envv)) { n_envs = PySequence_Length(py_envv); *envv = g_new(gchar *, n_envs + 1); for (i = 0; i < n_envs; i++) { PyObject *item = PySequence_GetItem(py_envv, i); Py_DECREF(item); /* PySequence_GetItem INCREF's */ (*envv)[i] = PyString_AsString(item); } (*envv)[n_envs] = NULL; } else { PyErr_SetString(PyExc_TypeError, "envv must be a sequence or a dictionary"); return -1; } return n_envs; } static void _free_envv(PyObject *py_envv, char **envv) { if (PyDict_Check(py_envv)) { g_strfreev(envv); } else { g_free(envv); } } static PyObject * _wrap_vte_terminal_fork_command(PyGObject * self, PyObject * args, PyObject * kwargs) { gchar **argv = NULL, **envv = NULL; gchar *command = NULL, *directory = NULL; static char *kwlist[] = { "command", "argv", "envv", "directory", "loglastlog", "logutmp", "logwtmp", NULL }; PyObject *py_argv = NULL, *py_envv = NULL, *loglastlog = NULL, *logutmp = NULL, *logwtmp = NULL; int i, n_args, n_envs; pid_t pid; if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|zOOzOOO:fork_command", kwlist, &command, &py_argv, &py_envv, &directory, &loglastlog, &logutmp, &logwtmp)) { return NULL; } if (py_argv != NULL && py_argv != Py_None) { if (!PySequence_Check(py_argv)) { PyErr_SetString(PyExc_TypeError, "argv must be a sequence"); return NULL; } n_args = PySequence_Length(py_argv); argv = g_new(gchar *, n_args + 1); for (i = 0; i < n_args; i++) { PyObject *item = PySequence_GetItem(py_argv, i); Py_DECREF(item); /* PySequence_GetItem INCREF's */ argv[i] = PyString_AsString(item); } argv[n_args] = NULL; } n_envs = _build_envv(py_envv, &envv); if (n_envs == -1) { g_free(argv); return NULL; } pid = vte_terminal_fork_command(VTE_TERMINAL(self->obj), command, argv, envv, directory, (loglastlog != NULL) && PyObject_IsTrue(loglastlog), (logutmp != NULL) && PyObject_IsTrue(logutmp), (logwtmp != NULL) && PyObject_IsTrue(logwtmp)); if (envv) { _free_envv(py_envv, envv); } if (argv) { g_free(argv); } return PyInt_FromLong(pid); } %% override vte_terminal_forkpty kwargs static PyObject * _wrap_vte_terminal_forkpty(PyGObject * self, PyObject * args, PyObject * kwargs) { gchar **envv = NULL, *directory = NULL; static char *kwlist[] = { "envv", "directory", "loglastlog", "logutmp", "logwtmp", NULL }; PyObject *py_envv = NULL, *loglastlog = NULL, *logutmp = NULL, *logwtmp = NULL; int n_envs; pid_t pid; if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|OzOOO:forkpty", kwlist, &py_envv, &directory, &loglastlog, &logutmp, &logwtmp)) { return NULL; } n_envs = _build_envv(py_envv, &envv); if (n_envs == -1) { return NULL; } pid = vte_terminal_forkpty(VTE_TERMINAL(self->obj), envv, directory, (loglastlog != NULL) && PyObject_IsTrue(loglastlog), (logutmp != NULL) && PyObject_IsTrue(logutmp), (logwtmp != NULL) && PyObject_IsTrue(logwtmp)); if (envv) { _free_envv(py_envv, envv); } return PyInt_FromLong(pid); } %% override vte_terminal_get_cursor_position noargs static PyObject * _wrap_vte_terminal_get_cursor_position(PyGObject *self) { PyObject *ret; glong column, row; vte_terminal_get_cursor_position(VTE_TERMINAL(self->obj), &column, &row); ret = PyTuple_New(2); PyTuple_SetItem(ret, 0, PyInt_FromLong(column)); PyTuple_SetItem(ret, 1, PyInt_FromLong(row)); return ret; } %% override vte_terminal_get_padding noargs static PyObject * _wrap_vte_terminal_get_padding(PyGObject *self) { PyObject *ret; int xpad, ypad; vte_terminal_get_padding(VTE_TERMINAL(self->obj), &xpad, &ypad); ret = PyTuple_New(2); PyTuple_SetItem(ret, 0, PyInt_FromLong(xpad)); PyTuple_SetItem(ret, 1, PyInt_FromLong(ypad)); return ret; } %% override vte_terminal_get_text kwargs static gboolean call_callback(VteTerminal *terminal, glong column, glong row, gpointer data) { PyObject *cb, *args, *result; gboolean ret; if (!PySequence_Check(data)) { PyErr_SetString(PyExc_TypeError, "expected argument list in a tuple"); return FALSE; } cb = PySequence_GetItem(data, 0); /* INCREFs */ Py_XDECREF(cb); if (!PyCallable_Check(cb)) { PyErr_SetString(PyExc_TypeError, "callback is not a callable object"); return FALSE; } args = PyTuple_New(4); PyTuple_SetItem(args, 0, PySequence_GetItem(data, 1)); PyTuple_SetItem(args, 1, PyInt_FromLong(column)); PyTuple_SetItem(args, 2, PyInt_FromLong(row)); PyTuple_SetItem(args, 3, PySequence_GetItem(data, 2)); result = PyObject_CallObject(cb, args); Py_DECREF(args); ret = (result && PyObject_IsTrue(result)); Py_XDECREF(result); return ret; } static PyObject* build_attributes(GArray *attrs) { PyObject *py_attrs = PyTuple_New(attrs->len); int count; PyObject *row = PyString_FromString("row"); PyObject *column = PyString_FromString("column"); PyObject *fore = PyString_FromString("fore"); PyObject *back = PyString_FromString("back"); PyObject *underline = PyString_FromString("underline"); PyObject *striketrough = PyString_FromString("striketrough"); for (count = 0; count < attrs->len; count++) { VteCharAttributes *cht; PyObject *py_char_attr; py_char_attr = Py_BuildValue("{S:l,S:l,S:N,S:N,S:I,S:I}", row, cht->row, column, cht->column, fore, pyg_boxed_new(GDK_TYPE_COLOR, &cht->fore, TRUE, TRUE), back, pyg_boxed_new(GDK_TYPE_COLOR, &cht->back, TRUE, TRUE), underline, (unsigned int) cht->underline, striketrough, (unsigned int) cht->strikethrough); PyTuple_SetItem(py_attrs, count, py_char_attr); } Py_DECREF(row); Py_DECREF(column); Py_DECREF(fore); Py_DECREF(back); Py_DECREF(underline); Py_DECREF(striketrough); return py_attrs; } static gboolean always_true(VteTerminal *terminal, glong row, glong column, gpointer data) { return TRUE; } static PyObject * _wrap_vte_terminal_get_text(PyGObject *self, PyObject *args, PyObject *kwargs) { static char *kwlist[] = { "callback", "get_attributes", "data", NULL }; PyObject *callback = NULL, *do_attr = NULL, *data = NULL; PyObject *callback_and_args = NULL; PyObject *result = NULL; GArray *attrs = NULL; char *text; PyObject *py_attrs; int count; long length; if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|OO:terminal_get_text", kwlist, &callback, &do_attr, &data)) { return NULL; } if ((do_attr != NULL) && (PyObject_IsTrue(do_attr))) { attrs = g_array_new(FALSE, TRUE, sizeof(VteCharAttributes)); } else { attrs = NULL; } if (callback != NULL) { if (!PyCallable_Check(callback)) { PyErr_SetString(PyExc_TypeError, "1st argument not callable."); if (attrs) { g_array_free(attrs, TRUE); } return NULL; } callback_and_args = PyTuple_New(3); Py_INCREF(callback); /* PyTuple_SetItem will assume ownership. */ PyTuple_SetItem(callback_and_args, 0, callback); Py_INCREF(self); /* PyTuple_SetItem will assume ownership. */ PyTuple_SetItem(callback_and_args, 1, (PyObject*) self); if (data != NULL) { Py_INCREF(data); /* PyTuple_SetItem will assume ownership. */ PyTuple_SetItem(callback_and_args, 2, data); } else { PyTuple_SetItem(callback_and_args, 2, PyTuple_New(0)); } text = vte_terminal_get_text(VTE_TERMINAL(self->obj), call_callback, callback_and_args, attrs); Py_DECREF(callback_and_args); } else { text = vte_terminal_get_text(VTE_TERMINAL(self->obj), always_true, NULL, attrs); } if (attrs) { py_attrs = build_attributes(attrs); length = attrs->len; g_array_free(attrs, TRUE); result = Py_BuildValue("(s#N)", text, length, py_attrs); } else { result = Py_BuildValue("s", text); } g_free (text); return result; } %% override vte_terminal_get_text_include_trailing_spaces kwargs static PyObject * _wrap_vte_terminal_get_text_include_trailing_spaces(PyGObject *self, PyObject *args, PyObject *kwargs) { static char *kwlist[] = { "callback", "get_attributes", "data", NULL }; PyObject *callback = NULL, *do_attr = NULL, *data = NULL; PyObject *callback_and_args = NULL; PyObject *result = NULL; GArray *attrs = NULL; char *text; PyObject *py_attrs; int count; long length; if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|OO:terminal_get_text_include_trailing_spaces", kwlist, &callback, &do_attr, &data)) { return NULL; } if ((do_attr != NULL) && (PyObject_IsTrue(do_attr))) { attrs = g_array_new(FALSE, TRUE, sizeof(VteCharAttributes)); } else { attrs = NULL; } if (callback != NULL) { if (!PyCallable_Check(callback)) { PyErr_SetString(PyExc_TypeError, "1st argument not callable."); if (attrs) { g_array_free(attrs, TRUE); } return NULL; } callback_and_args = PyTuple_New(3); Py_INCREF(callback); /* PyTuple_SetItem will assume ownership. */ PyTuple_SetItem(callback_and_args, 0, callback); Py_INCREF(self); /* PyTuple_SetItem will assume ownership. */ PyTuple_SetItem(callback_and_args, 1, (PyObject*) self); if (data != NULL) { Py_INCREF(data); /* PyTuple_SetItem will assume ownership. */ PyTuple_SetItem(callback_and_args, 2, data); } else { PyTuple_SetItem(callback_and_args, 2, PyTuple_New(0)); } text = vte_terminal_get_text_include_trailing_spaces(VTE_TERMINAL(self->obj), call_callback, callback_and_args, attrs); Py_DECREF(callback_and_args); } else { text = vte_terminal_get_text_include_trailing_spaces(VTE_TERMINAL(self->obj), always_true, NULL, attrs); } if (attrs) { py_attrs = build_attributes(attrs); length = attrs->len; g_array_free(attrs, TRUE); result = Py_BuildValue("(s#N)", text, length, py_attrs); } else { result = Py_BuildValue("s", text); } g_free (text); return result; } %% override vte_terminal_get_text_range kwargs static PyObject * _wrap_vte_terminal_get_text_range(PyGObject *self, PyObject *args, PyObject *kwargs) { static char *kwlist[] = { "start_row", "start_col", "end_row", "end_col", "callback", "get_attributes", "data", NULL }; PyObject *callback = NULL, *do_attr = NULL, *data = NULL; glong start_row, start_col, end_row, end_col; PyObject *callback_and_args = NULL; PyObject *result = NULL; GArray *attrs = NULL; char *text; PyObject *py_attrs; int count; long length; if (!PyArg_ParseTupleAndKeywords(args, kwargs, "llllO|OO:terminal_get_text_range", kwlist, &start_row, &start_col, &end_row, &end_col, &callback, &do_attr, &data)) { return NULL; } if ((do_attr != NULL) && (PyObject_IsTrue(do_attr))) { attrs = g_array_new(FALSE, TRUE, sizeof(VteCharAttributes)); } else { attrs = NULL; } if (callback != NULL) { if (!PyCallable_Check(callback)) { PyErr_SetString(PyExc_TypeError, "1st argument not callable."); if (attrs) { g_array_free(attrs, TRUE); } return NULL; } callback_and_args = PyTuple_New(3); Py_INCREF(callback); /* PyTuple_SetItem will assume ownership. */ PyTuple_SetItem(callback_and_args, 0, callback); Py_INCREF(self); /* PyTuple_SetItem will assume ownership. */ PyTuple_SetItem(callback_and_args, 1, (PyObject*) self); if (data != NULL) { Py_INCREF(data); /* PyTuple_SetItem will assume ownership. */ PyTuple_SetItem(callback_and_args, 2, data); } else { PyTuple_SetItem(callback_and_args, 2, PyTuple_New(0)); } text = vte_terminal_get_text_range(VTE_TERMINAL(self->obj), start_row, start_col, end_row, end_col, call_callback, callback_and_args, attrs); Py_DECREF(callback_and_args); } else { text = vte_terminal_get_text_range(VTE_TERMINAL(self->obj), start_row, start_col, end_row, end_col, always_true, NULL, attrs); } if (attrs) { py_attrs = build_attributes(attrs); length = attrs->len; g_array_free(attrs, TRUE); result = Py_BuildValue("(s#N)", text, length, py_attrs); } else { result = Py_BuildValue("s", text); } g_free (text); return result; } %% override vte_terminal_set_colors kwargs static PyObject * _wrap_vte_terminal_set_colors(PyGObject *self, PyObject *args, PyObject *kwargs) { static char *kwlist[] = { "foreground", "background", "palette", NULL }; PyObject *py_foreground, *py_background, *py_palette, *item; int palette_size, i; GdkColor *foreground = NULL, *background = NULL, *palette = NULL; if (!PyArg_ParseTupleAndKeywords(args, kwargs, "OOO:VteTerminal.set_colors", kwlist, &py_foreground, &py_background, &py_palette, &palette_size)) return NULL; if (pyg_boxed_check(py_foreground, GDK_TYPE_COLOR)) foreground = pyg_boxed_get(py_foreground, GdkColor); else { PyErr_SetString(PyExc_TypeError, "foreground should be a GdkColor"); return NULL; } if (pyg_boxed_check(py_background, GDK_TYPE_COLOR)) background = pyg_boxed_get(py_background, GdkColor); else { PyErr_SetString(PyExc_TypeError, "background should be a GdkColor"); return NULL; } if (PySequence_Check(py_palette)) { palette_size = PySequence_Length(py_palette); palette = g_malloc(sizeof(GdkColor) * palette_size); for (i = 0; i < palette_size; i++) { item = PySequence_GetItem(py_palette, i); /* INCREFs */ if (!pyg_boxed_check(item, GDK_TYPE_COLOR)) { g_free(palette); PyErr_SetString(PyExc_TypeError, "palette should be a list of GdkColors"); return NULL; } palette[i] = *((GdkColor*)pyg_boxed_get(item, GdkColor)); Py_DECREF(item); } } else { PyErr_SetString(PyExc_TypeError, "palette should be a list of GdkColors"); return NULL; } vte_terminal_set_colors(VTE_TERMINAL(self->obj), foreground, background, palette, palette_size); g_free(palette); Py_INCREF(Py_None); return Py_None; } %% override vte_terminal_match_check kwargs static PyObject * _wrap_vte_terminal_match_check(PyGObject *self, PyObject *args, PyObject *kwargs) { static char *kwlist[] = { "column", "row", NULL }; gchar *ret; glong column, row; int *tag; if (!PyArg_ParseTupleAndKeywords(args, kwargs, "ll:VteTerminal.match_check", kwlist, &column, &row)) return NULL; ret = vte_terminal_match_check(VTE_TERMINAL(self->obj), column, row, &tag); if (ret) { PyObject *py_ret = Py_BuildValue("si", ret, tag); g_free(ret); return py_ret; } Py_INCREF(Py_None); return Py_None; } %%