2
// Copyright (c) 2002 - 2005 Henrik Kinnunen (fluxgen at fluxbox dot org)
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:
11
// The above copyright notice and this permission notice shall be included in
12
// all copies or substantial portions of the Software.
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.
22
// $Id: FbRun.cc 4001 2005-05-09 07:20:17Z mathias $
27
#include "EventManager.hh"
30
#include "FileUtil.hh"
34
#endif // HAVE_CONFIG_H
42
#include <X11/keysym.h>
43
#include <X11/Xutil.h>
44
#include <X11/cursorfont.h>
58
FbRun::FbRun(int x, int y, size_t width):
59
FbTk::TextBox(DefaultScreen(FbTk::App::instance()->display()),
62
m_display(FbTk::App::instance()->display()),
66
m_current_history_item(0),
67
m_current_apps_item(0),
68
m_cursor(XCreateFontCursor(FbTk::App::instance()->display(), XC_xterm)) {
72
// setting nomaximize in local resize
73
resize(width, font().height() + m_bevel);
76
XClassHint *class_hint = XAllocClassHint();
78
throw string("Out of memory");
79
class_hint->res_name = "fbrun";
80
class_hint->res_class = "FbRun";
81
XSetClassHint(m_display, window(), class_hint);
87
XpmCreatePixmapFromData(m_display,
94
XFreePixmap(m_display, mask);
99
if (m_pixmap.drawable()) {
101
wmhints.flags = IconPixmapHint;
102
wmhints.icon_pixmap = m_pixmap.drawable();
103
XSetWMHints(m_display, window(), &wmhints);
113
void FbRun::run(const std::string &command) {
114
FbTk::App::instance()->end(); // end application
115
m_end = true; // mark end of processing
117
// fork and execute program
120
execl("/bin/sh", "/bin/sh", "-c", command.c_str(), static_cast<void*>(NULL));
121
exit(0); //exit child
126
// save command history to file
127
if (text().size() != 0) { // no need to save empty command
129
// don't allow duplicates into the history file, first
130
// look for a duplicate
131
if (m_current_history_item < m_history.size()
132
&& text() == m_history[m_current_history_item]) {
133
// m_current_history_item is the duplicate
135
m_current_history_item = 0;
136
for (; m_current_history_item < m_history.size();
137
++m_current_history_item) {
138
if (m_history[m_current_history_item] == text())
143
fstream inoutfile(m_history_file.c_str(), ios::in|ios::out);
145
// now m_current_history_item points at the duplicate, or
146
// at m_history.size() if no duplicate
147
if (m_current_history_item != m_history.size()) {
149
// read past history items before current
150
for (; inoutfile.good() && i < m_current_history_item; i++)
151
inoutfile.ignore(1, '\n');
153
// write the history items that come after current
154
for (i++; i < m_history.size(); i++)
155
inoutfile<<m_history[i]<<endl;
158
// set put-pointer at end of file
159
inoutfile.seekp(0, ios::end);
161
// append current command to the file
162
inoutfile<<command<<endl;
165
cerr<<"FbRun Warning: Can't write command history to file: "<<m_history_file<<endl;
170
bool FbRun::loadHistory(const char *filename) {
173
ifstream infile(filename);
175
//even though we fail to load file, we should try save to it
176
ofstream outfile(filename);
178
m_history_file = filename;
183
// clear old history and load new one from file
185
// each line is a command
187
while (!infile.eof()) {
188
getline(infile, line);
189
if (line.size()) // don't add empty lines
190
m_history.push_back(line);
192
// set no current histor to display
193
m_current_history_item = m_history.size();
195
m_history_file = filename;
199
bool FbRun::loadFont(const string &fontname) {
200
if (!m_font.load(fontname.c_str()))
203
// resize to fit new font height
204
resize(width(), font().height() + m_bevel);
208
void FbRun::setForegroundColor(const FbTk::Color &color) {
209
m_gc.setForeground(color);
212
void FbRun::setTitle(const string &title) {
213
setName(title.c_str());
216
void FbRun::resize(unsigned int width, unsigned int height) {
217
FbTk::TextBox::resize(width, height);
221
void FbRun::redrawLabel() {
225
void FbRun::keyPressEvent(XKeyEvent &ke) {
227
ke.state = FbTk::KeyUtil::instance().cleanMods(ke.state);
229
int cp= cursorPosition();
230
FbTk::TextBox::keyPressEvent(ke);
233
XLookupString(&ke, keychar, 1, &ks, 0);
234
// a modifier key by itself doesn't do anything
235
if (IsModifierKey(ks))
238
if (FbTk::KeyUtil::instance().isolateModifierMask(ke.state)) { // a modifier key is down
239
if ((ke.state & ControlMask) == ControlMask) {
248
tabCompleteHistory();
249
setCursorPosition(cp);
252
} else if ((ke.state & (Mod1Mask|ShiftMask)) == (Mod1Mask | ShiftMask)) {
262
} else { // no modifier key
267
FbTk::App::instance()->end(); // end program
281
setCursorPosition(cp);
288
void FbRun::setNoMaximize() {
289
// we don't need to maximize this window
291
sh.flags = PMaxSize | PMinSize;
292
sh.max_width = width();
293
sh.max_height = height();
294
sh.min_width = width();
295
sh.min_height = height();
296
XSetWMNormalHints(m_display, window(), &sh);
299
void FbRun::prevHistoryItem() {
300
if (m_history.size() == 0 || m_current_history_item == 0) {
303
m_current_history_item--;
304
setText(m_history[m_current_history_item]);
308
void FbRun::nextHistoryItem() {
309
if (m_current_history_item == m_history.size()) {
312
m_current_history_item++;
313
if (m_current_history_item == m_history.size()) {
314
m_current_history_item = m_history.size();
317
setText(m_history[m_current_history_item]);
321
void FbRun::firstHistoryItem() {
322
if (m_history.size() == 0 || m_current_history_item == 0) {
325
m_current_history_item = 0;
326
setText(m_history[m_current_history_item]);
330
void FbRun::lastHistoryItem() {
331
// actually one past the end
332
if (m_history.size() == 0) {
335
m_current_history_item = m_history.size();
340
void FbRun::tabCompleteHistory() {
341
if (m_current_history_item == 0 || m_history.empty() ) {
345
int history_item = m_current_history_item - 1;
346
string prefix = text().substr(0, textStartPos() + cursorPosition());
347
while (history_item != m_current_history_item && nr++ < m_history.size()) {
348
if (history_item <= -1 )
349
history_item= m_history.size() - 1;
350
if (m_history[history_item].find(prefix) == 0) {
351
m_current_history_item = history_item;
352
setText(m_history[m_current_history_item]);
357
if (history_item == m_current_history_item) XBell(m_display, 0);
361
void FbRun::tabCompleteApps() {
363
static bool first_run= true;
364
static string saved_prefix= "";
365
string prefix= text().substr(0, textStartPos() + cursorPosition());
368
bool add_dirs= false;
369
bool changed_prefix= false;
371
// (re)build m_apps-container
372
if (first_run || saved_prefix != prefix) {
377
if(!prefix.empty() &&
378
string("/.~").find_first_of(prefix[0]) != string::npos) {
379
size_t rseparator= prefix.find_last_of("/");
380
path= prefix.substr(0, rseparator + 1) + ":";
383
path= getenv("PATH");
390
for(l= 0, r= 0; r < path.size(); r++) {
391
if ((path[r]==':' || r == path.size() - 1) && r - l > 0) {
394
dir.open(path.substr(l, r - l).c_str());
395
int n= dir.entries();
398
filename= dir.readFilename();
399
fncomplete= dir.name() +
400
(*dir.name().rbegin() != '/' ? "/" : "") +
403
// directories in dirmode ?
404
if (add_dirs && FbTk::FileUtil::isDirectory(fncomplete.c_str()) &&
405
filename != ".." && filename != ".") {
406
m_apps.push_back(fncomplete);
407
// executables in dirmode ?
408
} else if (add_dirs && FbTk::FileUtil::isRegularFile(fncomplete.c_str()) &&
409
FbTk::FileUtil::isExecutable(fncomplete.c_str()) &&
411
fncomplete.substr(0, prefix.size()) == prefix)) {
412
m_apps.push_back(fncomplete);
413
// executables in $PATH ?
414
} else if (FbTk::FileUtil::isRegularFile(fncomplete.c_str()) &&
415
FbTk::FileUtil::isExecutable(fncomplete.c_str()) &&
417
filename.substr(0, prefix.size()) == prefix)) {
418
m_apps.push_back(filename);
426
sort(m_apps.begin(), m_apps.end());
427
unique(m_apps.begin(), m_apps.end());
429
saved_prefix= prefix;
430
changed_prefix= true;
431
m_current_apps_item= 0;
434
if (m_apps.empty() ) {
437
size_t apps_item = m_current_apps_item + (changed_prefix ? 0 : 1);
441
if (apps_item >= m_apps.size() ) {
446
if ((!changed_prefix || loop) && apps_item == m_current_apps_item) {
449
if (m_apps[apps_item].find(prefix) == 0) {
450
m_current_apps_item = apps_item;
451
if (add_dirs && FbTk::FileUtil::isDirectory(m_apps[m_current_apps_item].c_str()))
452
setText(m_apps[m_current_apps_item] + "/");
454
setText(m_apps[m_current_apps_item]);
459
if (!changed_prefix && apps_item == m_current_apps_item)
464
void FbRun::insertCharacter(char keychar) {
465
char val[2] = {keychar, 0};