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.cc277
1 files changed, 277 insertions, 0 deletions
diff --git a/util/fbrun/FbRun.cc b/util/fbrun/FbRun.cc
new file mode 100644
index 0000000..992255f
--- /dev/null
+++ b/util/fbrun/FbRun.cc
@@ -0,0 +1,277 @@
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.11 2003/04/27 02:26:21 rathnor Exp $
23
24#include "FbRun.hh"
25
26#include "App.hh"
27#include "EventManager.hh"
28#include "Color.hh"
29
30#include <X11/Xlib.h>
31#include <X11/keysym.h>
32#include <X11/Xutil.h>
33#include <X11/cursorfont.h>
34
35#include <unistd.h>
36
37#include <iostream>
38#include <fstream>
39#include <cassert>
40
41using namespace std;
42FbRun::FbRun(int x, int y, size_t width):
43 m_font("fixed"),
44 m_win((int)0, x, y, //screen num and position
45 width + m_bevel, m_font.height() + 2, // size
46 KeyPressMask|ExposureMask), // eventmask
47 m_display(FbTk::App::instance()->display()),
48 m_bevel(4),
49 m_gc(DefaultGC(m_display, DefaultScreen(m_display))),
50 m_end(false),
51 m_current_history_item(0),
52 m_cursor(XCreateFontCursor(FbTk::App::instance()->display(), XC_xterm)),
53 m_cursor_pos(0) {
54 XDefineCursor(FbTk::App::instance()->display(), m_win.window(), m_cursor);
55 // setting nomaximize in local resize
56 resize(width, m_font.height());
57 FbTk::EventManager::instance()->registerEventHandler(*this, m_win.window());
58}
59
60FbRun::~FbRun() {
61 hide();
62 FbTk::EventManager::instance()->unregisterEventHandler(m_win.window());
63}
64
65void FbRun::run(const std::string &command) {
66 //fork and execute program
67 if (!fork()) {
68 setsid();
69 execl("/bin/sh", "/bin/sh", "-c", command.c_str(), 0);
70 exit(0); //exit child
71 }
72
73 hide(); // hide gui
74
75 // save command history to file
76 if (m_runtext.size() != 0) { // no need to save empty command
77 // open file in append mode
78 ofstream outfile(m_history_file.c_str(), ios::app);
79 if (outfile)
80 outfile<<m_runtext<<endl;
81 else
82 cerr<<"FbRun Warning: Can't write command history to file: "<<m_history_file<<endl;
83 }
84 FbTk::App::instance()->end(); // end application
85 m_end = true; // mark end of processing
86}
87
88bool FbRun::loadHistory(const char *filename) {
89 if (filename == 0)
90 return false;
91 ifstream infile(filename);
92 if (!infile) {
93 //even though we fail to load file, we should try save to it
94 m_history_file = filename;
95 return false;
96 }
97 // clear old history and load new one from file
98 m_history.clear();
99 // each line is a command
100 string line;
101 while (!infile.eof()) {
102 getline(infile, line);
103 if (line.size()) // don't add empty lines
104 m_history.push_back(line);
105 }
106 // set no current histor to display
107 m_current_history_item = m_history.size();
108 // set history file
109 m_history_file = filename;
110 return true;
111}
112
113bool FbRun::loadFont(const string &fontname) {
114 if (!m_font.load(fontname.c_str()))
115 return false;
116
117 // resize to fit new font height
118 resize(m_win.width(), m_font.height() + m_bevel);
119 return true;
120}
121
122void FbRun::setForeground(const FbTk::Color &color) {
123 XSetForeground(m_display, m_gc, color.pixel());
124 redrawLabel();
125}
126
127void FbRun::setBackground(const FbTk::Color &color) {
128 m_win.setBackgroundColor(color);
129 redrawLabel();
130}
131
132
133void FbRun::setText(const string &text) {
134 m_runtext = text;
135 redrawLabel();
136}
137
138void FbRun::setTitle(const string &title) {
139 m_win.setName(title.c_str());
140}
141
142void FbRun::move(int x, int y) {
143 m_win.move(x, y);
144}
145
146void FbRun::resize(size_t width, size_t height) {
147 m_win.resize(width, height);
148 setNoMaximize();
149}
150
151void FbRun::show() {
152 m_win.show();
153}
154
155void FbRun::hide() {
156 m_win.hide();
157}
158
159void FbRun::redrawLabel() {
160 m_win.clear();
161 drawString(m_bevel/2, m_font.ascent() + m_bevel/2,
162 m_runtext.c_str(), m_runtext.size());
163
164}
165
166void FbRun::drawString(int x, int y,
167 const char *text, size_t len) {
168 assert(m_gc);
169
170 // check right boundary and adjust text drawing
171 size_t text_width = m_font.textWidth(text, len);
172 size_t startpos = 0;
173 if (text_width > m_win.width()) {
174 for (; startpos < len; ++startpos) {
175 if (m_font.textWidth(text+startpos, len-startpos) < m_win.width())
176 break;
177 }
178 }
179
180 m_font.drawText(m_win.window(), DefaultScreen(m_display), m_gc, text + startpos, len-startpos, x, y - 2);
181 int cursor_pos = m_font.textWidth(text + m_cursor_pos, len - startpos) + 1;
182 // draw cursor position
183 XDrawLine(FbTk::App::instance()->display(), m_win.window(), m_gc,
184 cursor_pos, 0,
185 cursor_pos, m_font.height());
186}
187
188void FbRun::keyPressEvent(XKeyEvent &ke) {
189 KeySym ks;
190 char keychar[1];
191 XLookupString(&ke, keychar, 1, &ks, 0);
192 if (ks == XK_Escape) {
193 m_end = true;
194 hide();
195 FbTk::App::instance()->end(); // end program
196 } else if (ks == XK_Return) {
197 run(m_runtext);
198 m_runtext = ""; // clear text
199 } else if (ks == XK_BackSpace) {
200 if (m_runtext.size() != 0) { // we can't erase what we don't have ;)
201 m_runtext.erase(m_runtext.size()-1);
202 redrawLabel();
203 m_cursor_pos--;
204 }
205 } else if (! IsModifierKey(ks) && !IsCursorKey(ks)) { // insert normal character at cursor pos
206 char in_char[2] = {keychar[0], 0};
207 m_runtext.insert(m_cursor_pos, in_char);
208 m_cursor_pos++;
209 redrawLabel();
210 } else if (IsCursorKey(ks)) {
211
212 switch (ks) {
213 case XK_Up:
214 prevHistoryItem();
215 break;
216 case XK_Down:
217 nextHistoryItem();
218 break;
219 case XK_Left:
220 cursorLeft();
221 break;
222 case XK_Right:
223 cursorRight();
224 break;
225 }
226 redrawLabel();
227 } else if (ks == XK_End) {
228 m_cursor_pos = m_runtext.size() - 1;
229 }
230}
231
232void FbRun::exposeEvent(XExposeEvent &ev) {
233 redrawLabel();
234}
235
236
237void FbRun::setNoMaximize() {
238 // we don't need to maximize this window
239 XSizeHints sh;
240 sh.flags = PMaxSize | PMinSize;
241 sh.max_width = m_win.width();
242 sh.max_height = m_win.height();
243 sh.min_width = m_win.width();
244 sh.min_height = m_win.height();
245 XSetWMNormalHints(m_display, m_win.window(), &sh);
246}
247
248void FbRun::prevHistoryItem() {
249
250 if (m_current_history_item > 0 && m_history.size() > 0)
251 m_current_history_item--;
252 if (m_current_history_item < m_history.size())
253 m_runtext = m_history[m_current_history_item];
254}
255
256void FbRun::nextHistoryItem() {
257 m_current_history_item++;
258 if (m_current_history_item >= m_history.size()) {
259 m_current_history_item = m_history.size();
260 m_runtext = "";
261 return;
262 } else
263 m_runtext = m_history[m_current_history_item];
264
265}
266
267
268void FbRun::cursorLeft() {
269 if (m_cursor_pos > 0)
270 m_cursor_pos--;
271}
272
273void FbRun::cursorRight() {
274 m_cursor_pos++;
275 if (m_cursor_pos >= m_runtext.size())
276 m_cursor_pos = m_runtext.size() - 1;
277}