]> icculus.org git repositories - mikachu/openbox.git/blob - src/config.cc
make labels' parent relative actually work :>
[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 #include "otk/screeninfo.hh"
7 #include "otk/renderstyle.hh"
8 #include "otk/util.hh"
9 #include "otk/property.hh"
10 #include "otk/display.hh"
11
12 extern "C" {
13 #include <Python.h>
14
15 #include "gettext.h"
16 #define _(str) gettext(str)
17 }
18
19 #include <cstring>
20
21 namespace ob {
22
23 static PyObject *obdict = NULL;
24
25 bool python_get_long(const char *name, long *value)
26 {
27   PyObject *val = PyDict_GetItemString(obdict, const_cast<char*>(name));
28   if (!(val && PyInt_Check(val))) return false;
29   
30   *value = PyInt_AsLong(val);
31   return true;
32 }
33
34 bool python_get_string(const char *name, otk::ustring *value)
35 {
36   PyObject *val = PyDict_GetItemString(obdict, const_cast<char*>(name));
37   if (!(val && PyString_Check(val))) return false;
38
39   std::string temp(PyString_AsString(val), PyString_Size(val));
40   *value = temp;
41   return true;
42 }
43
44 bool python_get_stringlist(const char *name, std::vector<otk::ustring> *value)
45 {
46   PyObject *val = PyDict_GetItemString(obdict, const_cast<char*>(name));
47   if (!(val && PyList_Check(val))) return false;
48
49   value->clear();
50   
51   for (int i = 0, end = PyList_Size(val); i < end; ++i) {
52     PyObject *str = PyList_GetItem(val, i);
53     if (PyString_Check(str))
54       value->push_back(PyString_AsString(str));
55   }
56   return true;
57 }
58
59 void Config::load()
60 {
61   const otk::ScreenInfo *info = otk::display->screenInfo(_screen);
62   Window root = info->rootWindow();
63
64   // set up access to the python global variables
65   PyObject *obmodule = PyImport_ImportModule("config");
66   obdict = PyModule_GetDict(obmodule);
67   Py_DECREF(obmodule);
68
69   python_get_stringlist("DESKTOP_NAMES", &desktop_names);
70
71   python_get_string("THEME", &theme);
72   // initialize the screen's style
73   otk::RenderStyle::setStyle(_screen, theme);
74   // draw the root window
75   otk::bexec("obsetroot " + otk::RenderStyle::style(_screen)->rootArgs(),
76              info->displayString());
77
78
79   if (!python_get_string("TITLEBAR_LAYOUT", &titlebar_layout)) {
80     fprintf(stderr, _("Unable to load config.%s\n"), "TITLEBAR_LAYOUT");
81     ::exit(1);
82   }
83
84   if (!python_get_long("DOUBLE_CLICK_DELAY", &double_click_delay)) {
85     fprintf(stderr, _("Unable to load config.%s\n"), "DOUBLE_CLICK_DELAY");
86     ::exit(1);
87   }
88   if (!python_get_long("DRAG_THRESHOLD", &drag_threshold)) {
89     fprintf(stderr, _("Unable to load config.%s\n"), "DRAG_THRESHOLD");
90     ::exit(1);
91   }
92   if (!python_get_long("NUMBER_OF_DESKTOPS", (long*)&num_desktops)) {
93     fprintf(stderr, _("Unable to load config.%s\n"), "NUMBER_OF_DESKTOPS");
94     ::exit(1);
95   }
96
97   // Set the net_desktop_names property
98   otk::Property::set(root,
99                      otk::Property::atoms.net_desktop_names,
100                      otk::Property::utf8, desktop_names);
101   // the above set() will cause screen::updateDesktopNames to fire right away
102   // so we have a list of desktop names
103
104   XEvent ce;
105   ce.xclient.type = ClientMessage;
106   ce.xclient.message_type = otk::Property::atoms.net_number_of_desktops;
107   ce.xclient.display = **otk::display;
108   ce.xclient.window = root;
109   ce.xclient.format = 32;
110   ce.xclient.data.l[0] = num_desktops;
111   XSendEvent(**otk::display, root, False,
112              SubstructureNotifyMask | SubstructureRedirectMask, &ce);
113 }
114
115 Config::Config(int screen)
116   : _screen(screen)
117 {
118 }
119
120 Config::~Config()
121 {
122 }
123
124 }