check for valid callback in bind()
[mikachu/openbox.git] / src / openbox.i
1 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
2
3 %module openbox
4
5 %{
6 #ifdef HAVE_CONFIG_H
7 #  include "../config.h"
8 #endif
9
10 #include "openbox.hh"
11 #include "screen.hh"
12 #include "client.hh"
13 #include "bindings.hh"
14 #include "actions.hh"
15 %}
16
17 %include stl.i
18 %include exception.i
19 //%include std_list.i
20 //%template(ClientList) std::list<OBClient*>;
21
22 %ignore ob::Openbox::instance;
23 %inline %{
24   ob::Openbox *Openbox_instance() { return ob::Openbox::instance; }
25 %};
26
27 // stuff for scripting callbacks!
28 %inline %{
29   enum ActionType {
30     Action_ButtonPress,
31     Action_ButtonRelease,
32     Action_Click,
33     Action_DoubleClick,
34     Action_EnterWindow,
35     Action_LeaveWindow,
36     Action_KeyPress,
37     Action_MouseMotion,
38     Action_NewWindow,
39     Action_CloseWindow
40   };
41   enum WidgetType {
42     Type_Frame,
43     Type_Titlebar,
44     Type_Handle,
45     Type_Plate,
46     Type_Label,
47     Type_MaximizeButton,
48     Type_CloseButton,
49     Type_IconifyButton,
50     Type_StickyButton,
51     Type_LeftGrip,
52     Type_RightGrip,
53     Type_Client,
54     Type_Root
55   };
56 %}
57 %rename(register) python_register;
58 %inline %{
59 PyObject * python_register(int action, PyObject *func, bool infront = false)
60 {
61   if (!PyCallable_Check(func)) {
62     PyErr_SetString(PyExc_TypeError, "Invalid callback function.");
63     return NULL;
64   }
65
66   if (!ob::Openbox::instance->actions()->registerCallback(
67         (ob::OBActions::ActionType)action, func, infront)) {
68     PyErr_SetString(PyExc_RuntimeError, "Unable to register action callback.");
69     return NULL;
70   }
71   Py_INCREF(Py_None); return Py_None;
72 }
73
74 PyObject * unregister(int action, PyObject *func)
75 {
76   if (!PyCallable_Check(func)) {
77     PyErr_SetString(PyExc_TypeError, "Invalid callback function.");
78     return NULL;
79   }
80
81   if (!ob::Openbox::instance->actions()->unregisterCallback(
82         (ob::OBActions::ActionType)action, func)) {
83     PyErr_SetString(PyExc_RuntimeError, "Unable to unregister action callback.");
84     return NULL;
85   }
86   Py_INCREF(Py_None); return Py_None;
87 }
88
89 PyObject * unregister_all(int action)
90 {
91   if (!ob::Openbox::instance->actions()->unregisterAllCallbacks(
92         (ob::OBActions::ActionType)action)) {
93     PyErr_SetString(PyExc_RuntimeError,
94                     "Unable to unregister action callbacks.");
95     return NULL;
96   }
97   Py_INCREF(Py_None); return Py_None;
98 }
99
100 PyObject * bind(PyObject *keylist, PyObject *func)
101 {
102   if (!PyCallable_Check(func)) {
103     PyErr_SetString(PyExc_TypeError, "Invalid callback function.");
104     return NULL;
105   }
106   if (!PyList_Check(keylist)) {
107     PyErr_SetString(PyExc_TypeError, "Invalid keylist. Not a list.");
108     return NULL;
109   }
110
111   ob::OBBindings::StringVect vectkeylist;
112   for (int i = 0, end = PyList_Size(keylist); i < end; ++i) {
113     PyObject *str = PyList_GetItem(keylist, i);
114     if (!PyString_Check(str)) {
115       PyErr_SetString(PyExc_TypeError,
116                      "Invalid keylist. It must contain only strings.");
117       return NULL;
118     }
119     vectkeylist.push_back(PyString_AsString(str));
120   }
121
122   if (!ob::Openbox::instance->bindings()->add(vectkeylist, func)) {
123     PyErr_SetString(PyExc_RuntimeError,"Unable to add binding.");
124     return NULL;
125   }
126   Py_INCREF(Py_None); return Py_None;
127 }
128
129 PyObject * unbind(PyObject *keylist)
130 {
131   if (!PyList_Check(keylist)) {
132     PyErr_SetString(PyExc_TypeError, "Invalid keylist. Not a list.");
133     return NULL;
134   }
135
136   ob::OBBindings::StringVect vectkeylist;
137   for (int i = 0, end = PyList_Size(keylist); i < end; ++i) {
138     PyObject *str = PyList_GetItem(keylist, i);
139     if (!PyString_Check(str)) {
140       PyErr_SetString(PyExc_TypeError,
141                      "Invalid keylist. It must contain only strings.");
142       return NULL;
143     }
144     vectkeylist.push_back(PyString_AsString(str));
145   }
146
147   ob::Openbox::instance->bindings()->remove(vectkeylist);
148   Py_INCREF(Py_None); return Py_None;
149 }
150
151 void unbind_all()
152 {
153   ob::Openbox::instance->bindings()->removeAll();
154 }
155
156 void set_reset_key(const std::string &key)
157 {
158   ob::Openbox::instance->bindings()->setResetKey(key);
159 }
160 %}
161
162 %ignore ob::OBScreen::clients;
163 %{
164   #include <iterator>
165 %}
166 %extend ob::OBScreen {
167   OBClient *client(int i) {
168     if (i >= (int)self->clients.size())
169       return NULL;
170     ob::OBScreen::ClientList::iterator it = self->clients.begin();
171     std::advance(it,i);
172     return *it;
173   }
174   int clientCount() const {
175     return (int) self->clients.size();
176   }
177 };
178
179 %import "../otk/eventdispatcher.hh"
180 %import "../otk/eventhandler.hh"
181 %import "widget.hh"
182 %import "actions.hh"
183
184 %include "openbox.hh"
185 %include "screen.hh"
186 %include "client.hh"
187
188 // for Mod1Mask etc
189 %include "X11/X.h"