fluxbox/util/fbrun/FbRun.cc

539 lines
16 KiB
C++
Raw Permalink Normal View History

// FbRun.cc
2006-02-16 06:53:05 +00:00
// Copyright (c) 2002 - 2006 Henrik Kinnunen (fluxgen at fluxbox dot org)
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.
#include "FbRun.hh"
2009-05-25 06:04:41 +00:00
#include "FbTk/App.hh"
#include "FbTk/EventManager.hh"
#include "FbTk/Color.hh"
#include "FbTk/KeyUtil.hh"
#include "FbTk/FileUtil.hh"
2003-08-25 01:18:00 +00:00
#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>
2004-04-21 14:58:44 +00:00
#include <unistd.h>
2002-08-20 02:05:17 +00:00
#include <iostream>
2003-08-25 01:18:00 +00:00
#include <iterator>
2002-11-12 19:20:31 +00:00
#include <fstream>
#include <algorithm>
2002-08-20 02:05:17 +00:00
#ifdef _WIN32
#include <cstring>
#endif
2006-10-30 19:31:15 +00:00
using std::cerr;
using std::endl;
using std::string;
using std::fstream;
using std::ifstream;
using std::ofstream;
using std::ios;
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, ""),
m_print(false),
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),
2018-03-04 10:17:31 +00:00
m_padding(0),
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),
m_current_files_item(-1),
m_last_completion_path(""),
m_current_apps_item(-1),
m_completion_pos(std::string::npos),
2003-08-27 14:04:12 +00:00
m_cursor(XCreateFontCursor(FbTk::App::instance()->display(), XC_xterm)) {
2003-08-27 14:04:12 +00:00
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 ch;
ch.res_name = const_cast<char *>("fbrun");
ch.res_class = const_cast<char *>("FbRun");
XSetClassHint(m_display, window(), &ch);
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(),
const_cast<char **>(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
hide(); // hide gui
if (m_print) {
std::cout << command;
return;
}
if (command.empty()) {
return;
}
#ifdef HAVE_FORK
2003-08-25 01:18:00 +00:00
// fork and execute program
2002-12-05 00:07:39 +00:00
if (!fork()) {
const char *shell = getenv("SHELL");
if (!shell)
shell = "/bin/sh";
2002-12-05 00:07:39 +00:00
setsid();
execl(shell, shell, "-c", command.c_str(), static_cast<void*>(NULL));
2002-12-05 00:07:39 +00:00
exit(0); //exit child
}
#elif defined(_WIN32)
/// @todo - unduplicate from FbCommands.cc
#ifndef PATH_MAX
#define PATH_MAX 1024
#endif
char comspec[PATH_MAX] = {0};
char * env_var = getenv("COMSPEC");
if (env_var != NULL) {
strncpy(comspec, env_var, PATH_MAX - 1);
comspec[PATH_MAX - 1] = '\0';
} else {
strncpy(comspec, "cmd.exe", 7);
comspec[7] = '\0';
}
spawnlp(P_NOWAIT, comspec, comspec, "/c", command.c_str(), static_cast<void*>(NULL));
#else
#error "Can't build FbRun - don't know how to launch without fork on your platform"
#endif
2002-12-05 00:07:39 +00:00
ofstream outfile(m_history_file.c_str());
if (!outfile) {
cerr<<"FbRun Warning: Can't write command history to file: "<<m_history_file<<endl;
return;
}
int n = 1024;
char *a = getenv("FBRUN_HISTORY_SIZE");
if (a)
n = atoi(a);
int j = m_history.size();
--n; // NOTICE: this should be "-=2", but a duplicate entry in the late
// (good) section would wait "too" long
// (we'd wait until 3 items are left and then still skip one for being a dupe)
// IOW: the limit is either n or n+1, depending in the history structure
for (unsigned int i = 0; i != m_history.size(); ++i) {
// don't allow duplicates into the history file
if (--j > n || m_history[i] == command)
continue;
outfile<<m_history[i]<<endl;
2002-12-05 00:07:39 +00:00
}
if (++n > 0) // n was decremented for the loop
outfile << command << endl;
outfile.close();
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
2004-09-29 00:51:29 +00:00
ofstream outfile(filename);
if (outfile) {
m_history_file = filename;
return true;
}
2002-12-05 00:07:39 +00:00
return false;
}
2002-12-05 00:07:39 +00:00
m_history.clear();
string line;
while (getline(infile, line)) {
if (!line.empty()) // don't add empty lines
2002-12-05 00:07:39 +00:00
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
}
bool FbRun::loadCompletion(const char *filename) {
if (!filename)
return false;
ifstream infile(filename);
if (!infile)
return false;
m_apps.clear();
string line;
while (getline(infile, line)) {
if (!line.empty()) // don't add empty lines
m_apps.push_back(line);
}
return true;
}
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
}
2003-09-16 14:49:49 +00:00
void FbRun::resize(unsigned int width, unsigned int height) {
FbTk::TextBox::resize(width, height);
2002-08-20 02:05:17 +00:00
}
2018-03-04 10:17:31 +00:00
void FbRun::setPadding(int padding) {
m_padding = padding;
FbTk::TextBox::setPadding(padding);
}
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) {
// reset last completion prefix if we don't do a tab completion thing
bool did_tab_complete = false;
2003-12-31 01:34:33 +00:00
ke.state = FbTk::KeyUtil::instance().cleanMods(ke.state);
2003-09-06 15:50:25 +00:00
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;
2016-07-23 06:18:26 +00:00
if (m_autocomplete && isprint(keychar[0])) {
did_tab_complete = true;
if (m_completion_pos == std::string::npos) {
m_completion_pos = cursorPosition();
} else {
++m_completion_pos;
}
tabCompleteApps();
} else if (FbTk::KeyUtil::instance().isolateModifierMask(ke.state)) {
// a modifier key is down
if ((ke.state & ControlMask) == ControlMask) {
switch (ks) {
case XK_p:
did_tab_complete = true;
prevHistoryItem();
break;
case XK_n:
did_tab_complete = true;
nextHistoryItem();
break;
case XK_Tab:
did_tab_complete = true;
tabComplete(m_history, m_current_history_item, true); // reverse
break;
}
} else if ((ke.state & (Mod1Mask|ShiftMask)) == (Mod1Mask | ShiftMask)) {
switch (ks) {
case XK_less:
did_tab_complete = true;
firstHistoryItem();
break;
case XK_greater:
did_tab_complete = true;
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_KP_Enter:
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:
did_tab_complete = true;
tabCompleteApps();
break;
2002-12-05 00:07:39 +00:00
}
}
if (!did_tab_complete)
m_completion_pos = std::string::npos;
clear();
}
void FbRun::lockPosition(bool size_too) {
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();
if (size_too) {
sh.flags |= USPosition;
sh.x = x();
sh.y = y();
}
2003-08-25 01:18:00 +00:00
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.empty() || 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++;
FbTk::BiDiString text("");
if (m_current_history_item == m_history.size()) {
m_current_history_item = m_history.size();
} else
text.setLogical((m_history[m_current_history_item]));
setText(text);
}
}
void FbRun::firstHistoryItem() {
if (m_history.empty() || m_current_history_item == 0) {
XBell(m_display, 0);
} else {
m_current_history_item = 0;
setText(FbTk::BiDiString(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.empty()) {
XBell(m_display, 0);
} else {
m_current_history_item = m_history.size();
setText(FbTk::BiDiString(""));
}
2002-11-12 19:20:31 +00:00
}
2003-03-22 11:33:04 +00:00
void FbRun::tabComplete(const std::vector<std::string> &list, int &currentItem, bool reverse) {
if (list.empty()) {
XBell(m_display, 0);
return;
}
if (m_completion_pos == std::string::npos)
m_completion_pos = textStartPos() + cursorPosition();
size_t split = text().find_last_of(' ', m_completion_pos);
if (split == std::string::npos)
split = 0;
else
++split; // skip space
std::string prefix = text().substr(split, m_completion_pos - split);
if (currentItem < 0)
currentItem = 0;
else if (currentItem >= list.size())
currentItem = list.size() - 1;
int item = currentItem;
while (true) {
if (reverse) {
if (--item < 0)
item = list.size() - 1;
} else {
if (++item >= list.size())
item = 0;
}
if (list.at(item).find(prefix) == 0) {
setText(FbTk::BiDiString(text().substr(0, split) + list.at(item)));
if (item == currentItem) {
cursorEnd();
m_completion_pos = std::string::npos;
} else {
select(split + prefix.size(), text().size() - (prefix.size() + split));
}
currentItem = item;
return;
}
if (item == currentItem) {
cursorEnd();
m_completion_pos = std::string::npos;
return;
}
}
// found nothing
XBell(m_display, 0);
}
2003-03-22 11:33:04 +00:00
void FbRun::tabCompleteApps() {
if (m_completion_pos == std::string::npos)
m_completion_pos = textStartPos() + cursorPosition();
size_t split = text().find_last_of(' ', m_completion_pos);
if (split == std::string::npos)
split = 0;
else
++split; // skip the space
std::string prefix = text().substr(split, m_completion_pos - split);
if (prefix.empty()) {
XBell(m_display, 0);
return;
}
if (prefix.at(0) == '/' || prefix.at(0) == '.' || prefix.at(0) == '~') {
// we're completing a directory, find subdirs
split = prefix.find_last_of('/');
if (split == std::string::npos) {
split = prefix.size();
prefix.append("/");
}
prefix = prefix.substr(0, split+1);
if (prefix != m_last_completion_path) {
m_files.clear();
m_current_files_item = -1;
m_last_completion_path = prefix;
FbTk::Directory dir;
std::string path = prefix;
if (path.at(0) == '~')
path.replace(0,1,getenv("HOME"));
dir.open(path.c_str());
int n = dir.entries();
while (--n > -1) {
std::string entry = dir.readFilename();
if (entry == "." || entry == "..")
continue;
// escape special characters
std::string needle(" !\"$&'()*,:;<=>?@[\\]^`{|}");
std::size_t pos = 0;
while ((pos = entry.find_first_of(needle, pos)) != std::string::npos) {
entry.insert(pos, "\\");
pos += 2;
}
if (FbTk::FileUtil::isDirectory(std::string(path + entry).c_str()))
m_files.push_back(prefix + entry + "/");
else
m_files.push_back(prefix + entry);
}
dir.close();
sort(m_files.begin(), m_files.end());
}
tabComplete(m_files, m_current_files_item);
} else {
static bool first_run = true;
if (first_run && m_apps.empty()) {
first_run = false;
std::string path = getenv("PATH");
FbTk::Directory dir;
for (unsigned int l = 0, r = 0; r <= path.size(); ++r) {
if ((r == path.size() || path.at(r) == ':') && r - l > 1) {
dir.open(path.substr(l, r - l).c_str());
prefix = dir.name() + (*dir.name().rbegin() == '/' ? "" : "/");
int n = dir.entries();
while (--n > -1) {
std::string entry = dir.readFilename();
std::string file = prefix + entry;
if (FbTk::FileUtil::isExecutable(file.c_str()) &&
!FbTk::FileUtil::isDirectory(file.c_str())) {
m_apps.push_back(entry);
}
}
dir.close();
l = r + 1;
}
}
sort(m_apps.begin(), m_apps.end());
unique(m_apps.begin(), m_apps.end());
}
tabComplete(m_apps, m_current_apps_item);
}
}
2003-08-27 00:20:19 +00:00
void FbRun::insertCharacter(char keychar) {
char val[2] = {keychar, 0};
insertText(val);
}