fluxbox/util/fbrun/FbRun.cc

338 lines
9.5 KiB
C++
Raw Normal View History

// FbRun.cc
// Copyright (c) 2002-2003 Henrik Kinnunen (fluxgen<at>users.sourceforge.net)
2002-08-20 02:05:17 +00:00
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
2003-09-06 15:50:25 +00:00
// $Id: FbRun.cc,v 1.20 2003/09/06 15:50:25 fluxgen Exp $
2002-08-20 02:05:17 +00:00
#include "FbRun.hh"
2002-11-26 17:13:36 +00:00
#include "App.hh"
#include "EventManager.hh"
2002-12-05 00:07:39 +00:00
#include "Color.hh"
2003-09-06 15:50:25 +00:00
#include "KeyUtil.hh"
2003-08-25 01:18:00 +00:00
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif // HAVE_CONFIG_H
#ifdef HAVE_XPM
#include <X11/xpm.h>
#include "fbrun.xpm"
#endif // HAVE_XPM
2002-08-20 02:05:17 +00:00
#include <X11/Xlib.h>
#include <X11/keysym.h>
#include <X11/Xutil.h>
2003-03-22 11:33:04 +00:00
#include <X11/cursorfont.h>
2002-08-20 02:05:17 +00:00
#include <unistd.h>
#include <iostream>
2003-08-25 01:18:00 +00:00
#include <iterator>
2002-11-12 19:20:31 +00:00
#include <fstream>
2003-04-27 02:26:21 +00:00
#include <cassert>
2002-08-20 02:05:17 +00:00
using namespace std;
FbRun::FbRun(int x, int y, size_t width):
2003-08-27 00:20:19 +00:00
FbTk::TextBox(DefaultScreen(FbTk::App::instance()->display()),
m_font, ""),
2002-12-05 00:07:39 +00:00
m_font("fixed"),
2003-07-25 11:17:41 +00:00
m_display(FbTk::App::instance()->display()),
2002-12-05 00:07:39 +00:00
m_bevel(4),
2003-08-27 14:04:12 +00:00
m_gc(*this),
2002-12-05 00:07:39 +00:00
m_end(false),
2003-03-22 11:33:04 +00:00
m_current_history_item(0),
2003-08-27 14:04:12 +00:00
m_cursor(XCreateFontCursor(FbTk::App::instance()->display(), XC_xterm)) {
setGC(m_gc.gc());
2003-08-25 01:18:00 +00:00
setCursor(m_cursor);
2002-12-05 00:07:39 +00:00
// setting nomaximize in local resize
2003-08-27 00:20:19 +00:00
resize(width, font().height() + m_bevel);
2003-06-25 12:01:23 +00:00
// setup class name
XClassHint *class_hint = XAllocClassHint();
if (class_hint == 0)
throw string("Out of memory");
class_hint->res_name = "fbrun";
2003-08-25 01:18:00 +00:00
class_hint->res_class = "FbRun";
XSetClassHint(m_display, window(), class_hint);
2003-06-25 12:01:23 +00:00
XFree(class_hint);
2003-08-25 01:18:00 +00:00
#ifdef HAVE_XPM
Pixmap mask = 0;
2003-08-27 14:04:12 +00:00
Pixmap pm;
2003-08-25 01:18:00 +00:00
XpmCreatePixmapFromData(m_display,
window(),
fbrun_xpm,
2003-08-27 14:04:12 +00:00
&pm,
2003-08-25 01:18:00 +00:00
&mask,
0); // attribs
if (mask != 0)
XFreePixmap(m_display, mask);
2003-08-27 14:04:12 +00:00
m_pixmap = pm;
2003-08-25 01:18:00 +00:00
#endif // HAVE_XPM
2003-08-27 14:04:12 +00:00
if (m_pixmap.drawable()) {
2003-08-27 00:20:19 +00:00
XWMHints wmhints;
wmhints.flags = IconPixmapHint;
2003-08-27 14:04:12 +00:00
wmhints.icon_pixmap = m_pixmap.drawable();
2003-08-27 00:20:19 +00:00
XSetWMHints(m_display, window(), &wmhints);
}
2002-08-20 02:05:17 +00:00
}
2002-08-20 02:05:17 +00:00
FbRun::~FbRun() {
2002-12-05 00:07:39 +00:00
hide();
2002-08-20 02:05:17 +00:00
}
void FbRun::run(const std::string &command) {
2003-08-25 01:18:00 +00:00
FbTk::App::instance()->end(); // end application
m_end = true; // mark end of processing
// fork and execute program
2002-12-05 00:07:39 +00:00
if (!fork()) {
setsid();
execl("/bin/sh", "/bin/sh", "-c", command.c_str(), 0);
exit(0); //exit child
}
hide(); // hide gui
2002-12-05 00:07:39 +00:00
// save command history to file
2003-08-27 00:20:19 +00:00
if (text().size() != 0) { // no need to save empty command
2003-08-24 23:47:31 +00:00
// don't allow duplicates into the history file, first
// look for a duplicate
2003-08-24 23:47:31 +00:00
if (m_current_history_item < m_history.size()
2003-08-27 00:20:19 +00:00
&& text() == m_history[m_current_history_item]) {
2003-08-24 23:47:31 +00:00
// m_current_history_item is the duplicate
} else {
2003-08-25 01:18:00 +00:00
m_current_history_item = 0;
for (; m_current_history_item < m_history.size();
++m_current_history_item) {
2003-08-27 00:20:19 +00:00
if (m_history[m_current_history_item] == text())
2003-08-25 01:18:00 +00:00
break;
}
2003-08-24 23:47:31 +00:00
}
2003-08-24 23:47:31 +00:00
// now m_current_history_item points at the duplicate, or
// at m_history.size() if no duplicate
fstream inoutfile(m_history_file.c_str(), ios::in|ios::out);
if (inoutfile) {
int i = 0;
// read past history items before current
for (string line; !inoutfile.eof() && i < m_current_history_item; i++)
getline(inoutfile, line);
// write the history items that come after current
for (i++; i < m_history.size(); i++)
inoutfile<<m_history[i]<<endl;
2003-08-25 01:18:00 +00:00
2003-08-24 23:47:31 +00:00
// and append the current one back to the end
2003-08-27 00:20:19 +00:00
inoutfile<<text()<<endl;
2003-08-24 23:47:31 +00:00
} else
cerr<<"FbRun Warning: Can't write command history to file: "<<m_history_file<<endl;
2002-12-05 00:07:39 +00:00
}
2003-08-25 01:18:00 +00:00
2002-08-20 02:05:17 +00:00
}
2002-11-12 19:20:31 +00:00
bool FbRun::loadHistory(const char *filename) {
2002-12-05 00:07:39 +00:00
if (filename == 0)
return false;
ifstream infile(filename);
if (!infile) {
//even though we fail to load file, we should try save to it
m_history_file = filename;
return false;
}
// clear old history and load new one from file
m_history.clear();
// each line is a command
string line;
while (!infile.eof()) {
getline(infile, line);
if (line.size()) // don't add empty lines
m_history.push_back(line);
}
// set no current histor to display
m_current_history_item = m_history.size();
// set history file
m_history_file = filename;
return true;
2002-11-12 19:20:31 +00:00
}
2002-08-20 02:05:17 +00:00
bool FbRun::loadFont(const string &fontname) {
2002-12-05 00:07:39 +00:00
if (!m_font.load(fontname.c_str()))
return false;
2002-08-20 02:05:17 +00:00
2002-12-05 00:07:39 +00:00
// resize to fit new font height
2003-08-27 00:20:19 +00:00
resize(width(), font().height() + m_bevel);
2002-12-05 00:07:39 +00:00
return true;
2002-08-20 02:05:17 +00:00
}
2003-08-25 01:18:00 +00:00
void FbRun::setForegroundColor(const FbTk::Color &color) {
2003-08-27 14:04:12 +00:00
m_gc.setForeground(color);
2002-08-20 02:05:17 +00:00
}
void FbRun::setTitle(const string &title) {
2003-08-25 01:18:00 +00:00
setName(title.c_str());
2002-08-20 02:05:17 +00:00
}
void FbRun::resize(size_t width, size_t height) {
2003-08-27 00:20:19 +00:00
FbTk::TextBox::resize(width, height);
2002-12-05 00:07:39 +00:00
setNoMaximize();
2002-08-20 02:05:17 +00:00
}
void FbRun::redrawLabel() {
2003-08-25 01:18:00 +00:00
clear();
2002-08-20 02:05:17 +00:00
}
void FbRun::keyPressEvent(XKeyEvent &ke) {
2003-09-06 15:50:25 +00:00
// strip numlock and scrolllock mask
ke.state &= ~FbTk::KeyUtil::numlockMod();
ke.state &= ~FbTk::KeyUtil::scrolllockMod();
2003-08-27 00:20:19 +00:00
FbTk::TextBox::keyPressEvent(ke);
2002-12-05 00:07:39 +00:00
KeySym ks;
char keychar[1];
XLookupString(&ke, keychar, 1, &ks, 0);
// a modifier key by itself doesn't do anything
if (IsModifierKey(ks)) return;
if (ke.state) { // a modifier key is down
if (ke.state == ControlMask) {
switch (ks) {
case XK_p:
prevHistoryItem();
break;
case XK_n:
nextHistoryItem();
break;
}
} else if (ke.state == (Mod1Mask | ShiftMask)) {
switch (ks) {
case XK_less:
firstHistoryItem();
break;
case XK_greater:
lastHistoryItem();
break;
}
2002-12-05 00:07:39 +00:00
}
} else { // no modifier key
2002-12-05 00:07:39 +00:00
switch (ks) {
case XK_Escape:
m_end = true;
hide();
FbTk::App::instance()->end(); // end program
break;
case XK_Return:
2003-08-27 00:20:19 +00:00
run(text());
break;
2002-12-05 00:07:39 +00:00
case XK_Up:
prevHistoryItem();
break;
case XK_Down:
nextHistoryItem();
break;
case XK_Tab:
tabCompleteHistory();
break;
2002-12-05 00:07:39 +00:00
}
}
2003-08-27 00:20:19 +00:00
clear();
}
2002-08-20 02:05:17 +00:00
void FbRun::setNoMaximize() {
2002-12-05 00:07:39 +00:00
// we don't need to maximize this window
XSizeHints sh;
sh.flags = PMaxSize | PMinSize;
2003-08-25 01:18:00 +00:00
sh.max_width = width();
sh.max_height = height();
sh.min_width = width();
sh.min_height = height();
XSetWMNormalHints(m_display, window(), &sh);
2002-08-20 02:05:17 +00:00
}
2002-11-12 19:20:31 +00:00
void FbRun::prevHistoryItem() {
if (m_history.size() == 0 || m_current_history_item == 0) {
XBell(m_display, 0);
} else {
2002-12-05 00:07:39 +00:00
m_current_history_item--;
2003-08-27 00:20:19 +00:00
setText(m_history[m_current_history_item]);
}
2002-11-12 19:20:31 +00:00
}
void FbRun::nextHistoryItem() {
if (m_current_history_item == m_history.size()) {
XBell(m_display, 0);
} else {
m_current_history_item++;
if (m_current_history_item == m_history.size()) {
m_current_history_item = m_history.size();
2003-08-27 00:20:19 +00:00
setText("");
} else
setText(m_history[m_current_history_item]);
}
}
void FbRun::firstHistoryItem() {
if (m_history.size() == 0 || m_current_history_item == 0) {
XBell(m_display, 0);
} else {
m_current_history_item = 0;
2003-08-27 00:20:19 +00:00
setText(m_history[m_current_history_item]);
}
}
2002-11-12 19:20:31 +00:00
void FbRun::lastHistoryItem() {
// actually one past the end
if (m_history.size() == 0) {
XBell(m_display, 0);
} else {
m_current_history_item = m_history.size();
2003-08-27 00:20:19 +00:00
setText("");
}
2002-11-12 19:20:31 +00:00
}
2003-03-22 11:33:04 +00:00
void FbRun::tabCompleteHistory() {
if (m_current_history_item == 0) {
XBell(m_display, 0);
} else {
int history_item = m_current_history_item - 1;
2003-08-27 00:20:19 +00:00
string prefix = text().substr(0, cursorPosition());
while (history_item > - 1) {
if (m_history[history_item].find(prefix) == 0) {
m_current_history_item = history_item;
2003-08-27 00:20:19 +00:00
setText(m_history[m_current_history_item]);
break;
}
history_item--;
}
if (history_item == -1) XBell(m_display, 0);
}
}
2003-03-22 11:33:04 +00:00
2003-08-27 00:20:19 +00:00
void FbRun::insertCharacter(char keychar) {
char val[2] = {keychar, 0};
insertText(val);
}