From 700984bd150d98a6876c117c2e1b1b3c72cffce1 Mon Sep 17 00:00:00 2001 From: Dana Jansens Date: Thu, 19 Dec 2002 23:54:41 +0000 Subject: [PATCH] python has begun! --- configure.ac | 3 +- src/Makefile.am | 4 +-- src/main.cc | 2 +- src/openbox.cc | 24 ++++++++++++++- src/openbox.hh | 4 +++ src/python.cc | 45 +++++++++++++++++++++++++++++ src/python.hh | 20 +++++++++++++ src/python_client.cc | 69 ++++++++++++++++++++++++++++++++++++++++++++ src/python_client.hh | 29 +++++++++++++++++++ 9 files changed, 194 insertions(+), 6 deletions(-) create mode 100644 src/python.cc create mode 100644 src/python.hh create mode 100644 src/python_client.cc create mode 100644 src/python_client.hh diff --git a/configure.ac b/configure.ac index 7f631a4d..08b6c1c2 100644 --- a/configure.ac +++ b/configure.ac @@ -75,7 +75,7 @@ CFLAGS="$CFLAGS $DEBUG" dnl Check for Python AC_CHECK_HEADER([python2.2/Python.h], - PYTHON_CFLAGS="-Ipython2.2", + , AC_MSG_ERROR([Openbox requires the use of Python 2.2. This is its secret special formula for extreme sexiness. See http://www.python.org ])) @@ -84,7 +84,6 @@ AC_CHECK_LIB([python2.2], [Py_Initialize], AC_MSG_ERROR([Openbox requires the use of Python 2.2. This is its secret special formula for extreme sexiness. See http://www.python.org ])) -AC_SUBST([PYTHON_CFLAGS]) AC_SUBST([PYTHON_LDFLAGS]) dnl Check for X headers and libraries diff --git a/src/Makefile.am b/src/Makefile.am index d2119f90..1cc23cf4 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -2,7 +2,7 @@ localedir=$(datadir)/locale DEFAULT_MENU=$(pkgdatadir)/menu DEFAULT_STYLE=$(pkgdatadir)/styles/mbdtex -CPPFLAGS=$(XFT_CFLAGS) $(PYTHON_CFLAGS) @CPPFLAGS@ \ +CPPFLAGS=$(XFT_CFLAGS) @CPPFLAGS@ \ -DDEFAULTMENU=\"$(DEFAULT_MENU)\" \ -DDEFAULTSTYLE=\"$(DEFAULT_STYLE)\" \ -DLOCALEDIR=\"$(localedir)\" @@ -17,7 +17,7 @@ openbox3_LDADD=../otk/libotk.a @LIBINTL@ openbox3_SOURCES= actions.cc client.cc frame.cc openbox.cc screen.cc \ main.cc rootwindow.cc backgroundwidget.cc labelwidget.cc \ - buttonwidget.cc + buttonwidget.cc python.cc python_client.cc MAINTAINERCLEANFILES= Makefile.in diff --git a/src/main.cc b/src/main.cc index 5b6c7885..1d6ce0b1 100644 --- a/src/main.cc +++ b/src/main.cc @@ -30,7 +30,7 @@ int main(int argc, char **argv) { ob::Openbox openbox(argc, argv); //ob::Blackbox blackbox(argc, argv, 0); - + //Blackbox blackbox(argv, session_display, rc_file); openbox.eventLoop(); diff --git a/src/openbox.cc b/src/openbox.cc index 18c4a5ca..f16d3464 100644 --- a/src/openbox.cc +++ b/src/openbox.cc @@ -9,6 +9,7 @@ #include "client.hh" #include "screen.hh" #include "actions.hh" +#include "python_client.hh" #include "otk/property.hh" #include "otk/display.hh" #include "otk/assassin.hh" @@ -90,6 +91,9 @@ Openbox::Openbox(int argc, char **argv) _doshutdown = false; _rcfilepath = otk::expandTilde("~/.openbox/rc3"); + _pyclients = PyDict_New(); + assert(_pyclients); + parseCommandLine(argc, argv); // TEMPORARY: using the xrdb rc3 @@ -149,7 +153,19 @@ Openbox::Openbox(int argc, char **argv) printf(_("No screens were found without a window manager. Exiting.\n")); ::exit(1); } - + + // initialize the python interface + Py_SetProgramName(argv[0]); + Py_Initialize(); + initopenbox(); // initialize the static 'openbox' module + FILE *rcpyfd = fopen("/home/natas/.openbox/user.py", "r"); + if (!rcpyfd) { + printf("failed to load python file /home/natas/.openbox/user.py\n"); + } else { + PyRun_SimpleFile(rcpyfd, "/home/natas/.openbox/user.py"); + fclose(rcpyfd); + } + _state = State_Normal; // done starting } @@ -261,6 +277,12 @@ void Openbox::eventLoop() void Openbox::addClient(Window window, OBClient *client) { _clients[window] = client; + + // maintain the python list here too + PyClientObject* pyclient = PyObject_New(PyClientObject, &PyClient_Type); + pyclient->window = window; + pyclient->client = client; + PyDict_SetItem(_pyclients, PyLong_FromLong(window), (PyObject*)pyclient); } diff --git a/src/openbox.hh b/src/openbox.hh index abdcfef3..83891828 100644 --- a/src/openbox.hh +++ b/src/openbox.hh @@ -18,6 +18,7 @@ extern "C" { #include #include +#include "python.hh" #include "otk/screeninfo.hh" #include "otk/timerqueuemanager.hh" #include "otk/property.hh" @@ -93,6 +94,7 @@ private: //! A list of all managed clients ClientMap _clients; + PyObject *_pyclients; // PyDictObject //! A list of all the managed screens ScreenList _screens; @@ -169,6 +171,8 @@ public: //! Returns the mouse cursors used throughout Openbox inline const Cursors &cursors() const { return _cursors; } + inline PyObject *pyclients() const { return _pyclients; } + //! The main function of the Openbox class /*! This function should be called after instantiating the Openbox class. diff --git a/src/python.cc b/src/python.cc new file mode 100644 index 00000000..e338b02c --- /dev/null +++ b/src/python.cc @@ -0,0 +1,45 @@ +// -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*- + +#ifdef HAVE_CONFIG_H +# include "../config.h" +#endif + +#include "python.hh" +#include "python_client.hh" +#include "openbox.hh" + +namespace ob { + +extern "C" { + +static PyObject *shit(PyObject *self, PyObject *args) +{ + if (!PyArg_ParseTuple(args, ":shit")) + return NULL; + + printf("SHIT CALLED!@!\n"); + + return Py_None; +} + + + +static PyMethodDef OBMethods[] = { + {"shit", shit, METH_VARARGS, + "Do some shit, yo!"}, + + {"get_client_dict", get_client_dict, METH_VARARGS, + "Get the list of all clients"}, + + {NULL, NULL, 0, NULL} +}; + +void initopenbox() +{ + PyClient_Type.ob_type = &PyType_Type; + + Py_InitModule("openbox", OBMethods); +} +} + +} diff --git a/src/python.hh b/src/python.hh new file mode 100644 index 00000000..61272a81 --- /dev/null +++ b/src/python.hh @@ -0,0 +1,20 @@ +// -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*- +#ifndef __python_hh +#define __python_hh + +/*! @file python.hh + @brief Python stuff +*/ + +#include + +namespace ob { + +extern "C" { + +void initopenbox(); + +} +} + +#endif // __python_hh diff --git a/src/python_client.cc b/src/python_client.cc new file mode 100644 index 00000000..3758e427 --- /dev/null +++ b/src/python_client.cc @@ -0,0 +1,69 @@ +// -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*- + +#ifdef HAVE_CONFIG_H +# include "../config.h" +#endif + +#include "python_client.hh" +#include "openbox.hh" + +namespace ob { + +extern "C" { + +PyObject *get_client_dict(PyObject* self, PyObject* args) +{ + if (!PyArg_ParseTuple(args, ":get_client_dict")) + return NULL; + return PyDictProxy_New(Openbox::instance->pyclients()); +} + + + +PyObject *getWindow(PyObject* self, PyObject* args) +{ + if (!PyArg_ParseTuple(args, ":getWindow")) + return NULL; + return PyLong_FromLong(((PyClientObject*)self)->window); +} + + + +static PyMethodDef attr_methods[] = { + {"getWindow", getWindow, METH_VARARGS, + "Return the window id."}, + {NULL, NULL, 0, NULL} /* sentinel */ +}; + +static PyObject *getattr(PyObject *obj, char *name) +{ + return Py_FindMethod(attr_methods, obj, name); +} + + + +static void client_dealloc(PyObject* self) +{ + PyObject_Del(self); +} + +PyTypeObject PyClient_Type = { + PyObject_HEAD_INIT(NULL) + 0, + "Client", + sizeof(PyClientObject), + 0, + client_dealloc, /*tp_dealloc*/ + 0, /*tp_print*/ + getattr, /*tp_getattr*/ + 0, /*tp_setattr*/ + 0, /*tp_compare*/ + 0, /*tp_repr*/ + 0, /*tp_as_number*/ + 0, /*tp_as_sequence*/ + 0, /*tp_as_mapping*/ + 0, /*tp_hash */ +}; + +} +} diff --git a/src/python_client.hh b/src/python_client.hh new file mode 100644 index 00000000..32b0c45b --- /dev/null +++ b/src/python_client.hh @@ -0,0 +1,29 @@ +// -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*- +#ifndef __pythonclient_hh +#define __pythonclient_hh + +/*! @file python_client.hh + @brief Python stuff +*/ + +#include "python.hh" +#include "client.hh" + +namespace ob { + +extern "C" { + +typedef struct { + PyObject_HEAD + Window window; + OBClient *client; +} PyClientObject; + +extern PyTypeObject PyClient_Type; + +PyObject *get_client_dict(PyObject* self, PyObject* args); + +} +} + +#endif // __pythonclient_hh -- 2.45.2