pointer's variables are config vars
[mikachu/openbox.git] / openbox / configwrap.c
1 #include <Python.h>
2 #include <glib.h>
3
4 /* This simply wraps the config.py module so that it can be accessed from the
5    C code.
6 */
7
8 static PyObject *add, *get, *set, *reset;
9
10 void configwrap_startup()
11 {
12     PyObject *c, *cdict;
13
14     /* get the ob module/dict */
15     c = PyImport_ImportModule("config"); /* new */
16     g_assert(c != NULL);
17     cdict = PyModule_GetDict(c); /* borrowed */
18     g_assert(cdict != NULL);
19
20     /* get the functions */
21     add = PyDict_GetItemString(cdict, "add");
22     g_assert(add != NULL);
23     get = PyDict_GetItemString(cdict, "get");
24     g_assert(get != NULL);
25     set = PyDict_GetItemString(cdict, "set");
26     g_assert(set != NULL);
27     reset = PyDict_GetItemString(cdict, "reset");
28     g_assert(reset != NULL);
29
30     Py_DECREF(c);
31 }
32
33 void configwrap_shutdown()
34 {
35     Py_DECREF(get);
36     Py_DECREF(set);
37     Py_DECREF(reset);
38     Py_DECREF(add);
39 }
40
41 void configwrap_add_int(char *modname, char *varname, char *friendname,
42                              char *description, int defvalue)
43 {
44     PyObject *r;
45
46     r= PyObject_CallFunction(add, "sssssi", modname, varname,
47                              friendname, description, "integer", defvalue);
48     g_assert(r != NULL);
49     Py_DECREF(r);
50 }
51
52 int configwrap_get_int(char *modname, char *varname)
53 {
54     PyObject *r;
55     int i;
56
57     r = PyObject_CallFunction(get, "ss", modname, varname);
58     g_assert(r != NULL);
59     i = PyInt_AsLong(r);
60     Py_DECREF(r);
61     return i;
62 }
63
64 void configwrap_set_int(char *modname, char *varname, int value)
65 {
66     PyObject *r;
67
68     r = PyObject_CallFunction(set, "ssi", modname, varname, value);
69     g_assert(r != NULL);
70     Py_DECREF(r);
71 }
72
73 void configwrap_reset(char *modname, char *varname)
74 {
75     PyObject *r;
76
77     r = PyObject_CallFunction(reset, "ss", modname, varname);
78     g_assert(r != NULL);
79     Py_DECREF(r);
80 }