Mercurial > pidgin.yaz
diff finch/libgnt/gntwm.c @ 21800:73aab5bdd830
Allow loading python scripts at runtime.
Things can go wrong if a script starts its own mainloop, or blocks on something.
For now, a script can check for a positive return value from 'gobject.main_depth()'
to determine that it's being loaded at runtime, and not start its own mainloop.
author | Sadrul Habib Chowdhury <imadil@gmail.com> |
---|---|
date | Sun, 09 Dec 2007 00:56:29 +0000 |
parents | 9487a26750d8 |
children | a86c035be695 c38d72677c8a |
line wrap: on
line diff
--- a/finch/libgnt/gntwm.c Fri Dec 07 22:14:23 2007 +0000 +++ b/finch/libgnt/gntwm.c Sun Dec 09 00:56:29 2007 +0000 @@ -20,12 +20,16 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA */ +#include "config.h" + +#ifdef USE_PYTHON +#include <Python.h> +#else #define _GNU_SOURCE #if (defined(__APPLE__) || defined(__unix__)) && !defined(__FreeBSD__) #define _XOPEN_SOURCE_EXTENDED #endif - -#include "config.h" +#endif #include <glib.h> #include <glib/gstdio.h> @@ -1198,12 +1202,50 @@ return ignore_keys ? !(ignore_keys = FALSE) : FALSE; } +#ifdef USE_PYTHON +static void +python_script_selected(GntFileSel *fs, const char *path, const char *f, gpointer n) +{ + char *dir = g_path_get_dirname(path); + FILE *file = fopen(path, "r"); + PyObject *pp = PySys_GetObject("path"), *dirobj = PyString_FromString(dir); + + PyList_Insert(pp, 0, dirobj); + Py_DECREF(dirobj); + PyRun_SimpleFile(file, path); + fclose(file); + + if (PyErr_Occurred()) { + PyErr_Print(); + } + g_free(dir); + + gnt_widget_destroy(GNT_WIDGET(fs)); +} + +static gboolean +run_python(GntBindable *bindable, GList *n) +{ + GntWidget *window = gnt_file_sel_new(); + GntFileSel *sel = GNT_FILE_SEL(window); + + g_object_set(G_OBJECT(window), "vertical", TRUE, NULL); + gnt_box_add_widget(GNT_BOX(window), gnt_label_new("Please select the python script you want to run.")); + gnt_box_set_title(GNT_BOX(window), "Select Python Script..."); + + g_signal_connect(G_OBJECT(sel), "file_selected", G_CALLBACK(python_script_selected), NULL); + g_signal_connect_swapped(G_OBJECT(sel->cancel), "activate", G_CALLBACK(gnt_widget_destroy), sel); + gnt_widget_show(window); + return TRUE; +} +#endif /* USE_PYTHON */ + static gboolean help_for_bindable(GntWM *wm, GntBindable *bindable) { gboolean ret = TRUE; GntBindableClass *klass = GNT_BINDABLE_GET_CLASS(bindable); - + if (klass->help_window) { gnt_wm_raise_window(wm, GNT_WIDGET(klass->help_window)); } else { @@ -1271,6 +1313,9 @@ g_object_unref(wm->workspaces->data); wm->workspaces = g_list_delete_link(wm->workspaces, wm->workspaces); } +#ifdef USE_PYTHON + Py_Finalize(); +#endif } static void @@ -1441,6 +1486,12 @@ GNT_KEY_CTRL_G, NULL); gnt_bindable_class_register_action(GNT_BINDABLE_CLASS(klass), "ignore-keys-end", ignore_keys_end, "\033" GNT_KEY_CTRL_G, NULL); +#ifdef USE_PYTHON + gnt_bindable_class_register_action(GNT_BINDABLE_CLASS(klass), "run-python", run_python, + GNT_KEY_F3, NULL); + Py_SetProgramName("gnt"); + Py_Initialize(); +#endif gnt_style_read_actions(G_OBJECT_CLASS_TYPE(klass), GNT_BINDABLE_CLASS(klass));