1 // App.cc
2 // Copyright (c) 2002 - 2006 Henrik Kinnunen (fluxgen at fluxbox dot org)
3 //
4 // Permission is hereby granted, free of charge, to any person obtaining a
5 // copy of this software and associated documentation files (the "Software"),
6 // to deal in the Software without restriction, including without limitation
7 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 // and/or sell copies of the Software, and to permit persons to whom the
9 // Software is furnished to do so, subject to the following conditions:
10 //
11 // The above copyright notice and this permission notice shall be included in
12 // all copies or substantial portions of the Software.
13 //
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20 // DEALINGS IN THE SOFTWARE.
21
22 #include "App.hh"
23 #include "FbString.hh"
24 #include "Font.hh"
25 #include "Image.hh"
26 #include "EventManager.hh"
27
28 #include <cstring>
29 #include <cstdlib>
30
31 #include <set>
32 #include <iostream>
33
34 namespace FbTk {
35
36 App *App::s_app = 0;
37
instance()38 App *App::instance() {
39 if (s_app == 0)
40 throw std::string("You must create an instance of FbTk::App first!");
41 return s_app;
42 }
43
App(const char * displayname)44 App::App(const char *displayname):m_done(false), m_display(0) {
45 if (s_app != 0)
46 throw std::string("Can't create more than one instance of FbTk::App");
47 s_app = this;
48 // this allows the use of std::string.c_str(), which returns
49 // a blank string, rather than a null string, so we make them equivalent
50 if (displayname != 0 && displayname[0] == '\0')
51 displayname = 0;
52 m_display = XOpenDisplay(displayname);
53 if (!m_display) {
54 if (displayname) {
55 throw std::string("Couldn't connect to XServer") + displayname;
56 } else {
57 throw std::string("Couldn't connect to XServer passing null display");
58 }
59 }
60
61 FbStringUtil::init();
62 }
63
~App()64 App::~App() {
65 if (m_display != 0) {
66
67 Font::shutdown();
68
69 XCloseDisplay(m_display);
70 m_display = 0;
71 }
72 s_app = 0;
73 }
74
sync(bool discard)75 void App::sync(bool discard) {
76 XSync(display(), discard);
77 }
78
eventLoop()79 void App::eventLoop() {
80 XEvent ev;
81 while (!m_done) {
82 XNextEvent(display(), &ev);
83 EventManager::instance()->handleEvent(ev);
84 }
85 }
86
87
end()88 void App::end() {
89 m_done = true; //end loop in App::eventLoop
90 }
91
setenv(const char * key,const char * value)92 bool App::setenv(const char* key, const char* value) {
93
94 if (!key || !*key)
95 return false;
96
97 static std::set<char*> stored;
98
99 const size_t key_size = strlen(key);
100 const size_t value_size = value ? strlen(value) : 0;
101 const size_t newenv_size = key_size + value_size + 2;
102
103 char* newenv = new char[newenv_size];
104 if (!newenv) {
105 return false;
106 }
107
108
109 // someone might have used putenv("key=value") (or setenv()) before.
110 // if getenv("key") succeeds, the returning value points to the address
111 // of "value" (right after the "="). this means, that that address
112 // minus "key=" points to the beginning of what was set. if fluxbox
113 // set that variable we have the pointer in "stored" and can dealloc it.
114 //
115 // we use putenv() to have control over the dealloction (valgrind and
116 // other complaint about it)
117
118 char* oldenv = getenv(key);
119 if (oldenv) {
120 oldenv = oldenv - (key_size + 1);
121 }
122 if (stored.find(oldenv) == stored.end()) {
123 oldenv = NULL;
124 }
125
126 // create the new environment
127 strncpy(newenv, key, key_size);
128 newenv[key_size] = '=';
129 if (value_size > 0) {
130 strncpy(newenv+key_size+1, value, value_size);
131 }
132 newenv[newenv_size-1] = 0;
133
134 if (putenv(newenv) == 0) {
135 if (oldenv) {
136 stored.erase(oldenv);
137 delete[] oldenv;
138 }
139 stored.insert(newenv);
140 }
141 return true;
142 }
143
144 } // end namespace FbTk
145