diff options
author | fluxgen <fluxgen> | 2002-08-20 02:05:17 (GMT) |
---|---|---|
committer | fluxgen <fluxgen> | 2002-08-20 02:05:17 (GMT) |
commit | 952d7717cd1a2a00d873719dd104277f674db15b (patch) | |
tree | 57ebbcc03b21368a21877d7f3fba17070a35ebf7 /util/fbrun/FbRun.cc | |
parent | 4d3ee5364af5ceccdee89809dbe13a41720bf781 (diff) | |
download | fluxbox-952d7717cd1a2a00d873719dd104277f674db15b.zip fluxbox-952d7717cd1a2a00d873719dd104277f674db15b.tar.bz2 |
first
Diffstat (limited to 'util/fbrun/FbRun.cc')
-rw-r--r-- | util/fbrun/FbRun.cc | 226 |
1 files changed, 226 insertions, 0 deletions
diff --git a/util/fbrun/FbRun.cc b/util/fbrun/FbRun.cc new file mode 100644 index 0000000..b023a56 --- /dev/null +++ b/util/fbrun/FbRun.cc | |||
@@ -0,0 +1,226 @@ | |||
1 | // FbRun.hh | ||
2 | // Copyright (c) 2002 Henrik Kinnunen (fluxgen@linuxmail.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 | // $Id: FbRun.cc,v 1.1 2002/08/20 02:04:34 fluxgen Exp $ | ||
23 | |||
24 | #include "FbRun.hh" | ||
25 | |||
26 | #include <X11/Xlib.h> | ||
27 | #include <X11/keysym.h> | ||
28 | #include <X11/Xutil.h> | ||
29 | |||
30 | #include <unistd.h> | ||
31 | |||
32 | #include <iostream> | ||
33 | |||
34 | using namespace std; | ||
35 | FbRun::FbRun(Display *disp, int x, int y, size_t width): | ||
36 | m_font(disp, "fixed"), | ||
37 | m_win(None), | ||
38 | m_display(disp), | ||
39 | m_bevel(4), | ||
40 | m_gc(None), | ||
41 | m_end(false) { | ||
42 | |||
43 | createWindow(x, y, width, m_font.height() + m_bevel); | ||
44 | // create GC | ||
45 | XGCValues gcv; | ||
46 | |||
47 | if (m_font.fontStruct()) | ||
48 | gcv.font = m_font.fontStruct()->fid; | ||
49 | |||
50 | m_gc = XCreateGC(m_display, m_win, GCFont, &gcv); | ||
51 | } | ||
52 | |||
53 | FbRun::~FbRun() { | ||
54 | hide(); | ||
55 | XDestroyWindow(m_display, m_win); | ||
56 | } | ||
57 | |||
58 | void FbRun::run(const std::string &command) { | ||
59 | //fork and execute program | ||
60 | if (!fork()) { | ||
61 | setsid(); | ||
62 | execl("/bin/sh", "/bin/sh", "-c", command.c_str(), 0); | ||
63 | exit(0); //exit fork | ||
64 | } | ||
65 | |||
66 | hide(); | ||
67 | |||
68 | m_end = true; // mark end of processing | ||
69 | } | ||
70 | |||
71 | bool FbRun::loadFont(const string &fontname) { | ||
72 | if (!m_font.load(fontname.c_str())) | ||
73 | return false; | ||
74 | |||
75 | // reconfigure m_gc for the new font | ||
76 | XGCValues gcv; | ||
77 | if (m_font.fontStruct()) | ||
78 | gcv.font = m_font.fontStruct()->fid; | ||
79 | XChangeGC(m_display, m_gc, GCFont, &gcv); | ||
80 | |||
81 | // resize to fit new font height | ||
82 | resize(m_width, m_font.height() + m_bevel); | ||
83 | return true; | ||
84 | } | ||
85 | |||
86 | void FbRun::setForeground(const XColor &color) { | ||
87 | XSetForeground(m_display, m_gc, color.pixel); | ||
88 | redrawLabel(); | ||
89 | } | ||
90 | |||
91 | void FbRun::setBackground(const XColor &color) { | ||
92 | XSetWindowBackground(m_display, m_win, color.pixel); | ||
93 | redrawLabel(); | ||
94 | } | ||
95 | |||
96 | |||
97 | void FbRun::setText(const string &text) { | ||
98 | m_runtext = text; | ||
99 | redrawLabel(); | ||
100 | } | ||
101 | |||
102 | void FbRun::setTitle(const string &title) { | ||
103 | assert(m_win); | ||
104 | XStoreName(m_display, m_win, const_cast<char *>(title.c_str())); | ||
105 | } | ||
106 | |||
107 | void FbRun::move(int x, int y) { | ||
108 | XMoveWindow(m_display, m_win, x, y); | ||
109 | } | ||
110 | |||
111 | void FbRun::resize(size_t width, size_t height) { | ||
112 | assert(m_win); | ||
113 | XResizeWindow(m_display, m_win, width, height + m_bevel); | ||
114 | m_width = width; | ||
115 | m_height = height + m_bevel; | ||
116 | setNoMaximize(); | ||
117 | } | ||
118 | |||
119 | void FbRun::show() { | ||
120 | assert(m_win); | ||
121 | XMapWindow(m_display, m_win); | ||
122 | } | ||
123 | |||
124 | void FbRun::hide() { | ||
125 | assert(m_win); | ||
126 | XUnmapWindow(m_display, m_win); | ||
127 | } | ||
128 | |||
129 | void FbRun::redrawLabel() { | ||
130 | assert(m_win); | ||
131 | |||
132 | XClearWindow(m_display, m_win); | ||
133 | drawString(0, m_height - m_bevel/2, | ||
134 | m_runtext.c_str(), m_runtext.size()); | ||
135 | |||
136 | } | ||
137 | |||
138 | void FbRun::drawString(int x, int y, | ||
139 | const char *text, size_t len) { | ||
140 | assert(m_win); | ||
141 | assert(m_gc); | ||
142 | |||
143 | if (FbTk::Font::multibyte()) { | ||
144 | XmbDrawString(m_display, m_win, m_font.fontSet(), | ||
145 | m_gc, x, y, | ||
146 | text, len); | ||
147 | } else { | ||
148 | XDrawString(m_display, m_win, | ||
149 | m_gc, x, y, | ||
150 | text, len); | ||
151 | } | ||
152 | } | ||
153 | |||
154 | |||
155 | void FbRun::createWindow(int x, int y, size_t width, size_t height) { | ||
156 | m_win = XCreateSimpleWindow(m_display, // display | ||
157 | DefaultRootWindow(m_display), // parent windows | ||
158 | x, y, | ||
159 | width, height, | ||
160 | 1, // border_width | ||
161 | 0, // border | ||
162 | WhitePixel(m_display, DefaultScreen(m_display))); // background | ||
163 | |||
164 | if (m_win == None) | ||
165 | throw string("Failed to create FbRun window!"); | ||
166 | |||
167 | XSelectInput(m_display, m_win, KeyPressMask|ExposureMask); | ||
168 | |||
169 | setNoMaximize(); | ||
170 | |||
171 | m_width = width; | ||
172 | m_height = height; | ||
173 | |||
174 | } | ||
175 | |||
176 | void FbRun::handleEvent(XEvent * const xev) { | ||
177 | switch (xev->type) { | ||
178 | case KeyPress: { | ||
179 | KeySym ks; | ||
180 | char keychar[1]; | ||
181 | XLookupString(&xev->xkey, keychar, 1, &ks, 0); | ||
182 | if (ks == XK_Escape) { | ||
183 | m_end = true; | ||
184 | hide(); | ||
185 | return; // no more processing | ||
186 | } else if (ks == XK_Return) { | ||
187 | run(m_runtext); | ||
188 | m_runtext = ""; // clear text | ||
189 | } else if (ks == XK_BackSpace && m_runtext.size() != 0) { | ||
190 | m_runtext.erase(m_runtext.size()-1); | ||
191 | redrawLabel(); | ||
192 | } else if (! IsModifierKey(ks) && !IsCursorKey(ks)) { | ||
193 | m_runtext+=keychar[0]; // append character | ||
194 | redrawLabel(); | ||
195 | } | ||
196 | } break; | ||
197 | case Expose: | ||
198 | redrawLabel(); | ||
199 | break; | ||
200 | default: | ||
201 | break; | ||
202 | } | ||
203 | } | ||
204 | |||
205 | void FbRun::getSize(size_t &width, size_t &height) { | ||
206 | XWindowAttributes attr; | ||
207 | XGetWindowAttributes(m_display, m_win, &attr); | ||
208 | width = attr.width; | ||
209 | height = attr.height; | ||
210 | } | ||
211 | |||
212 | void FbRun::setNoMaximize() { | ||
213 | |||
214 | size_t width, height; | ||
215 | |||
216 | getSize(width, height); | ||
217 | |||
218 | // we don't need to maximize this window | ||
219 | XSizeHints sh; | ||
220 | sh.flags = PMaxSize | PMinSize; | ||
221 | sh.max_width = width; | ||
222 | sh.max_height = height; | ||
223 | sh.min_width = width; | ||
224 | sh.min_height = height; | ||
225 | XSetWMNormalHints(m_display, m_win, &sh); | ||
226 | } | ||