aboutsummaryrefslogtreecommitdiff
path: root/util/fbrun/FbRun.cc
diff options
context:
space:
mode:
Diffstat (limited to 'util/fbrun/FbRun.cc')
-rw-r--r--util/fbrun/FbRun.cc336
1 files changed, 336 insertions, 0 deletions
diff --git a/util/fbrun/FbRun.cc b/util/fbrun/FbRun.cc
new file mode 100644
index 0000000..27270d5
--- /dev/null
+++ b/util/fbrun/FbRun.cc
@@ -0,0 +1,336 @@
1// FbRun.cc
2// Copyright (c) 2002-2003 Henrik Kinnunen (fluxgen<at>users.sourceforge.net)
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.23 2003/12/31 01:34:33 fluxgen Exp $
23
24#include "FbRun.hh"
25
26#include "App.hh"
27#include "EventManager.hh"
28#include "Color.hh"
29#include "KeyUtil.hh"
30
31#ifdef HAVE_CONFIG_H
32#include "config.h"
33#endif // HAVE_CONFIG_H
34
35#ifdef HAVE_XPM
36#include <X11/xpm.h>
37#include "fbrun.xpm"
38#endif // HAVE_XPM
39
40#include <X11/Xlib.h>
41#include <X11/keysym.h>
42#include <X11/Xutil.h>
43#include <X11/cursorfont.h>
44
45#include <unistd.h>
46
47#include <iostream>
48#include <iterator>
49#include <fstream>
50#include <cassert>
51
52using namespace std;
53FbRun::FbRun(int x, int y, size_t width):
54 FbTk::TextBox(DefaultScreen(FbTk::App::instance()->display()),
55 m_font, ""),
56 m_font("fixed"),
57 m_display(FbTk::App::instance()->display()),
58 m_bevel(4),
59 m_gc(*this),
60 m_end(false),
61 m_current_history_item(0),
62 m_cursor(XCreateFontCursor(FbTk::App::instance()->display(), XC_xterm)) {
63
64 setGC(m_gc.gc());
65 setCursor(m_cursor);
66 // setting nomaximize in local resize
67 resize(width, font().height() + m_bevel);
68
69 // setup class name
70 XClassHint *class_hint = XAllocClassHint();
71 if (class_hint == 0)
72 throw string("Out of memory");
73 class_hint->res_name = "fbrun";
74 class_hint->res_class = "FbRun";
75 XSetClassHint(m_display, window(), class_hint);
76
77 XFree(class_hint);
78#ifdef HAVE_XPM
79 Pixmap mask = 0;
80 Pixmap pm;
81 XpmCreatePixmapFromData(m_display,
82 window(),
83 fbrun_xpm,
84 &pm,
85 &mask,
86 0); // attribs
87 if (mask != 0)
88 XFreePixmap(m_display, mask);
89
90 m_pixmap = pm;
91#endif // HAVE_XPM
92
93 if (m_pixmap.drawable()) {
94 XWMHints wmhints;
95 wmhints.flags = IconPixmapHint;
96 wmhints.icon_pixmap = m_pixmap.drawable();
97 XSetWMHints(m_display, window(), &wmhints);
98 }
99}
100
101
102FbRun::~FbRun() {
103 hide();
104}
105
106void FbRun::run(const std::string &command) {
107 FbTk::App::instance()->end(); // end application
108 m_end = true; // mark end of processing
109
110 // fork and execute program
111 if (!fork()) {
112 setsid();
113 execl("/bin/sh", "/bin/sh", "-c", command.c_str(), 0);
114 exit(0); //exit child
115 }
116
117 hide(); // hide gui
118
119 // save command history to file
120 if (text().size() != 0) { // no need to save empty command
121
122 // don't allow duplicates into the history file, first
123 // look for a duplicate
124 if (m_current_history_item < m_history.size()
125 && text() == m_history[m_current_history_item]) {
126 // m_current_history_item is the duplicate
127 } else {
128 m_current_history_item = 0;
129 for (; m_current_history_item < m_history.size();
130 ++m_current_history_item) {
131 if (m_history[m_current_history_item] == text())
132 break;
133 }
134 }
135
136 // now m_current_history_item points at the duplicate, or
137 // at m_history.size() if no duplicate
138 fstream inoutfile(m_history_file.c_str(), ios::in|ios::out);
139 if (inoutfile) {
140 int i = 0;
141 // read past history items before current
142 for (string line; !inoutfile.eof() && i < m_current_history_item; i++)
143 getline(inoutfile, line);
144 // write the history items that come after current
145 for (i++; i < m_history.size(); i++)
146 inoutfile<<m_history[i]<<endl;
147
148 // and append the current one back to the end
149 inoutfile<<text()<<endl;
150 } else
151 cerr<<"FbRun Warning: Can't write command history to file: "<<m_history_file<<endl;
152 }
153
154}
155
156bool FbRun::loadHistory(const char *filename) {
157 if (filename == 0)
158 return false;
159 ifstream infile(filename);
160 if (!infile) {
161 //even though we fail to load file, we should try save to it
162 m_history_file = filename;
163 return false;
164 }
165 // clear old history and load new one from file
166 m_history.clear();
167 // each line is a command
168 string line;
169 while (!infile.eof()) {
170 getline(infile, line);
171 if (line.size()) // don't add empty lines
172 m_history.push_back(line);
173 }
174 // set no current histor to display
175 m_current_history_item = m_history.size();
176 // set history file
177 m_history_file = filename;
178 return true;
179}
180
181bool FbRun::loadFont(const string &fontname) {
182 if (!m_font.load(fontname.c_str()))
183 return false;
184
185 // resize to fit new font height
186 resize(width(), font().height() + m_bevel);
187 return true;
188}
189
190void FbRun::setForegroundColor(const FbTk::Color &color) {
191 m_gc.setForeground(color);
192}
193
194void FbRun::setTitle(const string &title) {
195 setName(title.c_str());
196}
197
198void FbRun::resize(unsigned int width, unsigned int height) {
199 FbTk::TextBox::resize(width, height);
200 setNoMaximize();
201}
202
203void FbRun::redrawLabel() {
204 clear();
205}
206
207void FbRun::keyPressEvent(XKeyEvent &ke) {
208 // strip numlock, capslock and scrolllock mask
209 ke.state = FbTk::KeyUtil::instance().cleanMods(ke.state);
210
211 FbTk::TextBox::keyPressEvent(ke);
212 KeySym ks;
213 char keychar[1];
214 XLookupString(&ke, keychar, 1, &ks, 0);
215 // a modifier key by itself doesn't do anything
216 if (IsModifierKey(ks)) return;
217
218 if (ke.state) { // a modifier key is down
219 if (ke.state == ControlMask) {
220 switch (ks) {
221 case XK_p:
222 prevHistoryItem();
223 break;
224 case XK_n:
225 nextHistoryItem();
226 break;
227 }
228 } else if (ke.state == (Mod1Mask | ShiftMask)) {
229 switch (ks) {
230 case XK_less:
231 firstHistoryItem();
232 break;
233 case XK_greater:
234 lastHistoryItem();
235 break;
236 }
237 }
238 } else { // no modifier key
239 switch (ks) {
240 case XK_Escape:
241 m_end = true;
242 hide();
243 FbTk::App::instance()->end(); // end program
244 break;
245 case XK_Return:
246 run(text());
247 break;
248 case XK_Up:
249 prevHistoryItem();
250 break;
251 case XK_Down:
252 nextHistoryItem();
253 break;
254 case XK_Tab:
255 tabCompleteHistory();
256 break;
257 }
258 }
259 clear();
260}
261
262void FbRun::setNoMaximize() {
263 // we don't need to maximize this window
264 XSizeHints sh;
265 sh.flags = PMaxSize | PMinSize;
266 sh.max_width = width();
267 sh.max_height = height();
268 sh.min_width = width();
269 sh.min_height = height();
270 XSetWMNormalHints(m_display, window(), &sh);
271}
272
273void FbRun::prevHistoryItem() {
274 if (m_history.size() == 0 || m_current_history_item == 0) {
275 XBell(m_display, 0);
276 } else {
277 m_current_history_item--;
278 setText(m_history[m_current_history_item]);
279 }
280}
281
282void FbRun::nextHistoryItem() {
283 if (m_current_history_item == m_history.size()) {
284 XBell(m_display, 0);
285 } else {
286 m_current_history_item++;
287 if (m_current_history_item == m_history.size()) {
288 m_current_history_item = m_history.size();
289 setText("");
290 } else
291 setText(m_history[m_current_history_item]);
292 }
293}
294
295void FbRun::firstHistoryItem() {
296 if (m_history.size() == 0 || m_current_history_item == 0) {
297 XBell(m_display, 0);
298 } else {
299 m_current_history_item = 0;
300 setText(m_history[m_current_history_item]);
301 }
302}
303
304void FbRun::lastHistoryItem() {
305 // actually one past the end
306 if (m_history.size() == 0) {
307 XBell(m_display, 0);
308 } else {
309 m_current_history_item = m_history.size();
310 setText("");
311 }
312}
313
314void FbRun::tabCompleteHistory() {
315 if (m_current_history_item == 0) {
316 XBell(m_display, 0);
317 } else {
318 int history_item = m_current_history_item - 1;
319 string prefix = text().substr(0, cursorPosition());
320 while (history_item > - 1) {
321 if (m_history[history_item].find(prefix) == 0) {
322 m_current_history_item = history_item;
323 setText(m_history[m_current_history_item]);
324 break;
325 }
326 history_item--;
327 }
328 if (history_item == -1) XBell(m_display, 0);
329 }
330}
331
332void FbRun::insertCharacter(char keychar) {
333 char val[2] = {keychar, 0};
334 insertText(val);
335}
336