]> icculus.org git repositories - mikachu/openbox.git/blob - src/config.cc
execute files such that i can track if an exception was thrown in it
[mikachu/openbox.git] / src / config.cc
1 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
2
3 #include "config.h"
4
5 #include "config.hh"
6
7 extern "C" {
8 #include <Python.h>
9
10 #include "gettext.h"
11 #define _(str) gettext(str)
12 }
13
14 #include <cstring>
15
16 namespace ob {
17
18 static PyObject *obdict = NULL;
19
20 bool python_get_long(const char *name, long *value)
21 {
22   PyObject *val = PyDict_GetItemString(obdict, const_cast<char*>(name));
23   if (!(val && PyInt_Check(val))) return false;
24   
25   *value = PyInt_AsLong(val);
26   return true;
27 }
28
29 bool python_get_string(const char *name, otk::ustring *value)
30 {
31   PyObject *val = PyDict_GetItemString(obdict, const_cast<char*>(name));
32   if (!(val && PyString_Check(val))) return false;
33
34   std::string temp(PyString_AsString(val), PyString_Size(val));
35   *value = temp;
36   return true;
37 }
38
39 bool python_get_stringlist(const char *name, std::vector<otk::ustring> *value)
40 {
41   PyObject *val = PyDict_GetItemString(obdict, const_cast<char*>(name));
42   if (!(val && PyList_Check(val))) return false;
43
44   value->clear();
45   
46   for (int i = 0, end = PyList_Size(val); i < end; ++i) {
47     PyObject *str = PyList_GetItem(val, i);
48     if (PyString_Check(str))
49       value->push_back(PyString_AsString(str));
50   }
51   return true;
52 }
53
54 Config::Config()
55 {
56   // set up access to the python global variables
57   PyObject *obmodule = PyImport_ImportModule("config");
58   obdict = PyModule_GetDict(obmodule);
59   Py_DECREF(obmodule);
60
61   python_get_stringlist("DESKTOP_NAMES", &desktop_names);
62
63   python_get_string("THEME", &theme);
64
65   if (!python_get_string("TITLEBAR_LAYOUT", &titlebar_layout)) {
66     fprintf(stderr, _("Unable to load config.%s\n"), "TITLEBAR_LAYOUT");
67     ::exit(1);
68   }
69
70   if (!python_get_long("DOUBLE_CLICK_DELAY", &double_click_delay)) {
71     fprintf(stderr, _("Unable to load config.%s\n"), "DOUBLE_CLICK_DELAY");
72     ::exit(1);
73   }
74   if (!python_get_long("DRAG_THRESHOLD", &drag_threshold)) {
75     fprintf(stderr, _("Unable to load config.%s\n"), "DRAG_THRESHOLD");
76     ::exit(1);
77   }
78   if (!python_get_long("NUMBER_OF_DESKTOPS", (long*)&num_desktops)) {
79     fprintf(stderr, _("Unable to load config.%s\n"), "NUMBER_OF_DESKTOPS");
80     ::exit(1);
81   }
82 }
83
84 Config::~Config()
85 {
86 }
87
88 }