2002-08-20 02:05:17 +00:00
|
|
|
// main.cc for FbRun
|
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/StringUtil.hh"
|
|
|
|
#include "FbTk/Color.hh"
|
2002-08-20 02:05:17 +00:00
|
|
|
|
2004-11-22 20:10:26 +00:00
|
|
|
#ifdef XINERAMA
|
|
|
|
extern "C" {
|
|
|
|
#include <X11/extensions/Xinerama.h>
|
|
|
|
}
|
|
|
|
#endif // XINERAMA
|
|
|
|
|
2002-08-20 02:05:17 +00:00
|
|
|
#include <string>
|
|
|
|
#include <iostream>
|
2007-06-29 17:25:24 +00:00
|
|
|
#ifdef HAVE_CSTRING
|
|
|
|
#include <cstring>
|
|
|
|
#else
|
|
|
|
#include <string.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_CSTDLIB
|
|
|
|
#include <cstdlib>
|
|
|
|
#else
|
|
|
|
#include <stdlib.h>
|
|
|
|
#endif
|
2002-11-12 16:46:17 +00:00
|
|
|
|
2006-10-30 19:31:15 +00:00
|
|
|
using std::cerr;
|
|
|
|
using std::endl;
|
|
|
|
using std::string;
|
2002-08-20 02:05:17 +00:00
|
|
|
|
|
|
|
void showUsage(const char *progname) {
|
2015-01-03 13:07:34 +00:00
|
|
|
cerr<<"fbrun 1.5 : (c) 2002-2015 Henrik Kinnunen"<<endl;
|
2002-12-05 00:07:39 +00:00
|
|
|
cerr<<"Usage: "<<
|
|
|
|
progname<<" [arguments]"<<endl<<
|
|
|
|
"Arguments: "<<endl<<
|
|
|
|
" -font [font name] Text font"<<endl<<
|
|
|
|
" -title [title name] Set title"<<endl<<
|
|
|
|
" -text [text] Text input"<<endl<<
|
2008-10-04 21:01:31 +00:00
|
|
|
" -print Print result to stdout"<<endl<<
|
2002-12-05 00:07:39 +00:00
|
|
|
" -w [width] Window width in pixels"<<endl<<
|
|
|
|
" -h [height] Window height in pixels"<<endl<<
|
2018-03-04 10:17:31 +00:00
|
|
|
" -pad [size] Padding size in pixels"<<endl<<
|
2002-12-05 00:07:39 +00:00
|
|
|
" -display [display string] Display name"<<endl<<
|
|
|
|
" -pos [x] [y] Window position in pixels"<<endl<<
|
2004-09-12 09:42:43 +00:00
|
|
|
" -nearmouse Window position near mouse"<<endl<<
|
2016-07-23 12:35:09 +00:00
|
|
|
" -center Window position on screen center"<<endl<<
|
2002-12-05 00:07:39 +00:00
|
|
|
" -fg [color name] Foreground text color"<<endl<<
|
|
|
|
" -bg [color name] Background color"<<endl<<
|
|
|
|
" -hf [history file] History file to load (default ~/.fluxbox/fbrun_history)"<<endl<<
|
2016-07-23 14:39:44 +00:00
|
|
|
" -cf [completion file] Complete contents of this file instead of $PATH binaries"<<endl<<
|
2016-07-23 06:18:26 +00:00
|
|
|
" -autocomplete Complete on typing"<<endl<<
|
2016-04-24 20:49:51 +00:00
|
|
|
" -preselect Select preset text"<<endl<<
|
2002-12-05 00:07:39 +00:00
|
|
|
" -help Show this help"<<endl<<endl<<
|
|
|
|
"Example: fbrun -fg black -bg white -text xterm -title \"run xterm\""<<endl;
|
2002-08-20 02:05:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char **argv) {
|
2002-12-05 00:07:39 +00:00
|
|
|
int x = 0, y = 0; // default pos of window
|
|
|
|
size_t width = 200, height = 32; // default size of window
|
|
|
|
bool set_height = false, set_width=false; // use height/width of font by default
|
2018-03-04 10:17:31 +00:00
|
|
|
int padding = 0; // default horizontal padding for text
|
2002-12-05 00:07:39 +00:00
|
|
|
bool set_pos = false; // set position
|
2004-09-03 14:18:48 +00:00
|
|
|
bool near_mouse = false; // popup near mouse
|
2016-07-23 12:35:09 +00:00
|
|
|
bool center = false;
|
2008-10-04 21:01:31 +00:00
|
|
|
bool print = false;
|
2016-04-24 20:49:51 +00:00
|
|
|
bool preselect = false;
|
2016-07-23 06:18:26 +00:00
|
|
|
bool autocomplete = getenv("FBRUN_AUTOCOMPLETE");
|
2002-12-05 00:07:39 +00:00
|
|
|
string fontname; // font name
|
|
|
|
string title("Run program"); // default title
|
|
|
|
string text; // default input text
|
|
|
|
string foreground("black"); // text color
|
|
|
|
string background("white"); // text background color
|
|
|
|
string display_name; // name of the display connection
|
|
|
|
string history_file("~/.fluxbox/fbrun_history"); // command history file
|
2016-07-23 14:39:44 +00:00
|
|
|
string completion_file; // command history file
|
2002-12-05 00:07:39 +00:00
|
|
|
// parse arguments
|
|
|
|
for (int i=1; i<argc; i++) {
|
2008-10-04 21:32:20 +00:00
|
|
|
string arg = argv[i];
|
|
|
|
if ((arg == "-font" || arg == "--font") && i+1 < argc) {
|
2002-12-05 00:07:39 +00:00
|
|
|
fontname = argv[++i];
|
2008-10-04 21:32:20 +00:00
|
|
|
} else if (arg == "-print" || arg == "--print") {
|
2008-10-04 21:01:31 +00:00
|
|
|
print = true;
|
2008-10-04 21:32:20 +00:00
|
|
|
} else if ((arg == "-title" || arg == "--title") && i+1 < argc) {
|
2002-12-05 00:07:39 +00:00
|
|
|
title = argv[++i];
|
2008-10-04 21:32:20 +00:00
|
|
|
} else if ((arg == "-text" || arg == "--text") && i+1 < argc) {
|
2002-12-05 00:07:39 +00:00
|
|
|
text = argv[++i];
|
2008-10-04 21:32:20 +00:00
|
|
|
} else if (arg == "-w" && i+1 < argc) {
|
2006-10-30 19:31:15 +00:00
|
|
|
width = atoi(argv[++i]);
|
2002-12-05 00:07:39 +00:00
|
|
|
set_width = true;
|
2008-10-04 21:32:20 +00:00
|
|
|
} else if (arg == "-h" && i+1 < argc) {
|
2002-12-05 00:07:39 +00:00
|
|
|
height = atoi(argv[++i]);
|
|
|
|
set_height = true; // mark true else the height of font will be used
|
2018-03-04 10:17:31 +00:00
|
|
|
} else if (arg == "-pad" && i+1 < argc) {
|
|
|
|
padding = atoi(argv[++i]);
|
2008-10-04 21:32:20 +00:00
|
|
|
} else if ((arg == "-display" || arg == "--display") && i+1 < argc) {
|
2002-12-05 00:07:39 +00:00
|
|
|
display_name = argv[++i];
|
2008-10-04 21:32:20 +00:00
|
|
|
} else if ((arg == "-pos" || arg == "--pos") && i+2 < argc) {
|
2002-12-05 00:07:39 +00:00
|
|
|
x = atoi(argv[++i]);
|
|
|
|
y = atoi(argv[++i]);
|
|
|
|
set_pos = true;
|
2008-10-04 21:32:20 +00:00
|
|
|
} else if (arg == "-nearmouse" || arg == "--nearmouse") {
|
2004-09-03 14:18:48 +00:00
|
|
|
set_pos = true;
|
|
|
|
near_mouse = true;
|
2016-07-23 12:35:09 +00:00
|
|
|
} else if (arg == "-center" || arg == "--center") {
|
|
|
|
set_pos = true;
|
|
|
|
center = true;
|
2002-12-05 00:07:39 +00:00
|
|
|
} else if (strcmp(argv[i], "-fg") == 0 && i+1 < argc) {
|
|
|
|
foreground = argv[++i];
|
|
|
|
} else if (strcmp(argv[i], "-bg") == 0 && i+1 < argc) {
|
|
|
|
background = argv[++i];
|
|
|
|
} else if (strcmp(argv[i], "-hf") == 0 && i+1 < argc) {
|
|
|
|
history_file = argv[++i];
|
2016-07-23 14:39:44 +00:00
|
|
|
} else if (strcmp(argv[i], "-cf") == 0 && i+1 < argc) {
|
|
|
|
completion_file = argv[++i];
|
2016-04-24 20:49:51 +00:00
|
|
|
} else if (strcmp(argv[i], "-preselect") == 0) {
|
|
|
|
preselect = true;
|
2016-07-23 06:18:26 +00:00
|
|
|
} else if (strcmp(argv[i], "-autocomplete") == 0) {
|
|
|
|
autocomplete = true;
|
2008-10-04 21:32:20 +00:00
|
|
|
} else if (arg == "-h" || arg == "-help" || arg == "--help") {
|
2002-12-05 00:07:39 +00:00
|
|
|
showUsage(argv[0]);
|
|
|
|
exit(0);
|
|
|
|
} else {
|
|
|
|
cerr<<"Invalid argument: "<<argv[i]<<endl;
|
|
|
|
showUsage(argv[0]);
|
|
|
|
exit(0);
|
|
|
|
}
|
2002-08-20 02:05:17 +00:00
|
|
|
|
2002-12-05 00:07:39 +00:00
|
|
|
}
|
2002-08-20 02:05:17 +00:00
|
|
|
|
2002-12-05 00:07:39 +00:00
|
|
|
try {
|
2006-10-30 19:31:15 +00:00
|
|
|
|
2002-12-05 00:07:39 +00:00
|
|
|
FbTk::App application(display_name.c_str());
|
|
|
|
FbRun fbrun;
|
2002-11-26 17:04:23 +00:00
|
|
|
|
2008-10-04 21:01:31 +00:00
|
|
|
fbrun.setPrint(print);
|
2016-07-23 06:18:26 +00:00
|
|
|
fbrun.setAutocomplete(autocomplete);
|
2003-12-01 18:58:53 +00:00
|
|
|
|
2002-12-05 00:07:39 +00:00
|
|
|
if (fontname.size() != 0) {
|
|
|
|
if (!fbrun.loadFont(fontname.c_str())) {
|
|
|
|
cerr<<"Failed to load font: "<<fontname<<endl;
|
|
|
|
cerr<<"Falling back to \"fixed\""<<endl;
|
|
|
|
}
|
|
|
|
}
|
2002-08-20 02:05:17 +00:00
|
|
|
|
2002-12-05 00:07:39 +00:00
|
|
|
// get color
|
|
|
|
FbTk::Color fg_color(foreground.c_str(), 0);
|
|
|
|
FbTk::Color bg_color(background.c_str(), 0);
|
2006-10-30 19:31:15 +00:00
|
|
|
|
2003-08-25 01:31:14 +00:00
|
|
|
fbrun.setForegroundColor(fg_color);
|
|
|
|
fbrun.setBackgroundColor(bg_color);
|
2002-08-20 02:05:17 +00:00
|
|
|
|
2002-12-05 00:07:39 +00:00
|
|
|
if (set_height)
|
|
|
|
fbrun.resize(fbrun.width(), height);
|
|
|
|
if (set_width)
|
|
|
|
fbrun.resize(width, fbrun.height());
|
2015-01-03 17:39:46 +00:00
|
|
|
|
2002-12-05 00:07:39 +00:00
|
|
|
// expand and load command history
|
2003-04-27 01:04:39 +00:00
|
|
|
string expanded_filename = FbTk::StringUtil::expandFilename(history_file);
|
2002-12-05 00:07:39 +00:00
|
|
|
if (!fbrun.loadHistory(expanded_filename.c_str()))
|
|
|
|
cerr<<"FbRun Warning: Failed to load history file: "<<expanded_filename<<endl;
|
2002-11-12 19:20:31 +00:00
|
|
|
|
2016-07-23 14:39:44 +00:00
|
|
|
if (!completion_file.empty()) {
|
|
|
|
expanded_filename = FbTk::StringUtil::expandFilename(completion_file);
|
|
|
|
if (!fbrun.loadCompletion(expanded_filename.c_str()))
|
|
|
|
cerr<<"FbRun Warning: Failed to load completion file: "<<expanded_filename<<endl;
|
|
|
|
}
|
|
|
|
|
2018-03-04 10:17:31 +00:00
|
|
|
fbrun.setPadding(padding);
|
2002-12-05 00:07:39 +00:00
|
|
|
fbrun.setTitle(title);
|
|
|
|
fbrun.setText(text);
|
2018-03-04 10:17:31 +00:00
|
|
|
|
2016-04-24 20:49:51 +00:00
|
|
|
if (preselect)
|
|
|
|
fbrun.selectAll();
|
2006-10-30 19:31:15 +00:00
|
|
|
|
2016-07-23 12:35:09 +00:00
|
|
|
if (near_mouse || center) {
|
2006-10-30 19:31:15 +00:00
|
|
|
|
2004-09-03 14:18:48 +00:00
|
|
|
int wx, wy;
|
|
|
|
unsigned int mask;
|
|
|
|
Window ret_win;
|
|
|
|
Window child_win;
|
2006-10-30 19:31:15 +00:00
|
|
|
|
2004-09-03 14:18:48 +00:00
|
|
|
Display* dpy = FbTk::App::instance()->display();
|
2016-07-23 12:35:09 +00:00
|
|
|
int root_x = 0;
|
|
|
|
int root_y = 0;
|
|
|
|
unsigned int root_w = WidthOfScreen(DefaultScreenOfDisplay(dpy));
|
|
|
|
unsigned int root_h = HeightOfScreen(DefaultScreenOfDisplay(dpy));
|
2006-10-30 19:31:15 +00:00
|
|
|
|
|
|
|
if (XQueryPointer(dpy, DefaultRootWindow(dpy),
|
2004-09-03 14:18:48 +00:00
|
|
|
&ret_win, &child_win,
|
|
|
|
&x, &y, &wx, &wy, &mask)) {
|
|
|
|
|
2004-11-22 20:10:26 +00:00
|
|
|
#ifdef XINERAMA
|
|
|
|
if(XineramaIsActive(dpy)) {
|
|
|
|
XineramaScreenInfo* screen_info = 0;
|
|
|
|
int number = 0;
|
|
|
|
screen_info = XineramaQueryScreens(dpy, &number);
|
|
|
|
if (screen_info) {
|
|
|
|
for(int i= 0; i < number; i++) {
|
|
|
|
if (x >= screen_info[i].x_org &&
|
|
|
|
x < screen_info[i].x_org + screen_info[i].width &&
|
|
|
|
y >= screen_info[i].y_org &&
|
|
|
|
y < screen_info[i].y_org + screen_info[i].height) {
|
|
|
|
root_x = screen_info[i].x_org;
|
|
|
|
root_y = screen_info[i].y_org;
|
|
|
|
root_w = screen_info[i].width;
|
|
|
|
root_h = screen_info[i].height;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
XFree(screen_info);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif // XINERAMA
|
2016-07-23 12:35:09 +00:00
|
|
|
} else if (!center) {
|
|
|
|
set_pos = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (center) {
|
|
|
|
x = root_x + root_w/2;
|
|
|
|
y = root_y + root_h/2;
|
2004-09-03 14:18:48 +00:00
|
|
|
}
|
2016-07-23 12:35:09 +00:00
|
|
|
|
|
|
|
x-= fbrun.width()/2;
|
|
|
|
y-= fbrun.height()/2;
|
|
|
|
|
|
|
|
if (x < root_x)
|
|
|
|
x = root_x;
|
|
|
|
if (x + fbrun.width() > root_x + root_w)
|
|
|
|
x = root_x + root_w - fbrun.width();
|
|
|
|
if (y < root_y)
|
|
|
|
y = root_y;
|
|
|
|
if (y + fbrun.height() > root_y + root_h)
|
|
|
|
y = root_y + root_h - fbrun.height();
|
2004-09-03 14:18:48 +00:00
|
|
|
}
|
2006-10-30 19:31:15 +00:00
|
|
|
|
2002-12-05 00:07:39 +00:00
|
|
|
if (set_pos)
|
|
|
|
fbrun.move(x, y);
|
2006-04-17 14:00:28 +00:00
|
|
|
|
|
|
|
fbrun.lockPosition(set_pos);
|
2006-10-30 19:31:15 +00:00
|
|
|
|
2004-12-21 16:10:57 +00:00
|
|
|
fbrun.show();
|
|
|
|
|
2002-12-05 00:07:39 +00:00
|
|
|
application.eventLoop();
|
2002-08-20 02:05:17 +00:00
|
|
|
|
2011-11-02 17:33:37 +00:00
|
|
|
} catch (string & errstr) {
|
2002-12-05 00:07:39 +00:00
|
|
|
cerr<<"Error: "<<errstr<<endl;
|
|
|
|
}
|
2002-08-20 02:05:17 +00:00
|
|
|
}
|