aboutsummaryrefslogtreecommitdiff
path: root/util/fbrun
diff options
context:
space:
mode:
authorfluxgen <fluxgen>2002-08-20 02:05:17 (GMT)
committerfluxgen <fluxgen>2002-08-20 02:05:17 (GMT)
commit952d7717cd1a2a00d873719dd104277f674db15b (patch)
tree57ebbcc03b21368a21877d7f3fba17070a35ebf7 /util/fbrun
parent4d3ee5364af5ceccdee89809dbe13a41720bf781 (diff)
downloadfluxbox_pavel-952d7717cd1a2a00d873719dd104277f674db15b.zip
fluxbox_pavel-952d7717cd1a2a00d873719dd104277f674db15b.tar.bz2
first
Diffstat (limited to 'util/fbrun')
-rw-r--r--util/fbrun/FbRun.cc226
-rw-r--r--util/fbrun/FbRun.hh76
-rw-r--r--util/fbrun/Makefile.am7
-rw-r--r--util/fbrun/main.cc163
4 files changed, 472 insertions, 0 deletions
diff --git a/util/fbrun/FbRun.cc b/util/fbrun/FbRun.cc
new file mode 100644
index 0000000..b023a56
--- /dev/null
+++ b/util/fbrun/FbRun.cc
@@ -0,0 +1,226 @@
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.1 2002/08/20 02:04:34 fluxgen Exp $
23
24#include "FbRun.hh"
25
26#include <X11/Xlib.h>
27#include <X11/keysym.h>
28#include <X11/Xutil.h>
29
30#include <unistd.h>
31
32#include <iostream>
33
34using namespace std;
35FbRun::FbRun(Display *disp, int x, int y, size_t width):
36m_font(disp, "fixed"),
37m_win(None),
38m_display(disp),
39m_bevel(4),
40m_gc(None),
41m_end(false) {
42
43 createWindow(x, y, width, m_font.height() + m_bevel);
44 // create GC
45 XGCValues gcv;
46
47 if (m_font.fontStruct())
48 gcv.font = m_font.fontStruct()->fid;
49
50 m_gc = XCreateGC(m_display, m_win, GCFont, &gcv);
51}
52
53FbRun::~FbRun() {
54 hide();
55 XDestroyWindow(m_display, m_win);
56}
57
58void FbRun::run(const std::string &command) {
59 //fork and execute program
60 if (!fork()) {
61 setsid();
62 execl("/bin/sh", "/bin/sh", "-c", command.c_str(), 0);
63 exit(0); //exit fork
64 }
65
66 hide();
67
68 m_end = true; // mark end of processing
69}
70
71bool FbRun::loadFont(const string &fontname) {
72 if (!m_font.load(fontname.c_str()))
73 return false;
74
75 // reconfigure m_gc for the new font
76 XGCValues gcv;
77 if (m_font.fontStruct())
78 gcv.font = m_font.fontStruct()->fid;
79 XChangeGC(m_display, m_gc, GCFont, &gcv);
80
81 // resize to fit new font height
82 resize(m_width, m_font.height() + m_bevel);
83 return true;
84}
85
86void FbRun::setForeground(const XColor &color) {
87 XSetForeground(m_display, m_gc, color.pixel);
88 redrawLabel();
89}
90
91void FbRun::setBackground(const XColor &color) {
92 XSetWindowBackground(m_display, m_win, color.pixel);
93 redrawLabel();
94}
95
96
97void FbRun::setText(const string &text) {
98 m_runtext = text;
99 redrawLabel();
100}
101
102void FbRun::setTitle(const string &title) {
103 assert(m_win);
104 XStoreName(m_display, m_win, const_cast<char *>(title.c_str()));
105}
106
107void FbRun::move(int x, int y) {
108 XMoveWindow(m_display, m_win, x, y);
109}
110
111void FbRun::resize(size_t width, size_t height) {
112 assert(m_win);
113 XResizeWindow(m_display, m_win, width, height + m_bevel);
114 m_width = width;
115 m_height = height + m_bevel;
116 setNoMaximize();
117}
118
119void FbRun::show() {
120 assert(m_win);
121 XMapWindow(m_display, m_win);
122}
123
124void FbRun::hide() {
125 assert(m_win);
126 XUnmapWindow(m_display, m_win);
127}
128
129void FbRun::redrawLabel() {
130 assert(m_win);
131
132 XClearWindow(m_display, m_win);
133 drawString(0, m_height - m_bevel/2,
134 m_runtext.c_str(), m_runtext.size());
135
136}
137
138void FbRun::drawString(int x, int y,
139 const char *text, size_t len) {
140 assert(m_win);
141 assert(m_gc);
142
143 if (FbTk::Font::multibyte()) {
144 XmbDrawString(m_display, m_win, m_font.fontSet(),
145 m_gc, x, y,
146 text, len);
147 } else {
148 XDrawString(m_display, m_win,
149 m_gc, x, y,
150 text, len);
151 }
152}
153
154
155void FbRun::createWindow(int x, int y, size_t width, size_t height) {
156 m_win = XCreateSimpleWindow(m_display, // display
157 DefaultRootWindow(m_display), // parent windows
158 x, y,
159 width, height,
160 1, // border_width
161 0, // border
162 WhitePixel(m_display, DefaultScreen(m_display))); // background
163
164 if (m_win == None)
165 throw string("Failed to create FbRun window!");
166
167 XSelectInput(m_display, m_win, KeyPressMask|ExposureMask);
168
169 setNoMaximize();
170
171 m_width = width;
172 m_height = height;
173
174}
175
176void FbRun::handleEvent(XEvent * const xev) {
177 switch (xev->type) {
178 case KeyPress: {
179 KeySym ks;
180 char keychar[1];
181 XLookupString(&xev->xkey, keychar, 1, &ks, 0);
182 if (ks == XK_Escape) {
183 m_end = true;
184 hide();
185 return; // no more processing
186 } else if (ks == XK_Return) {
187 run(m_runtext);
188 m_runtext = ""; // clear text
189 } else if (ks == XK_BackSpace && m_runtext.size() != 0) {
190 m_runtext.erase(m_runtext.size()-1);
191 redrawLabel();
192 } else if (! IsModifierKey(ks) && !IsCursorKey(ks)) {
193 m_runtext+=keychar[0]; // append character
194 redrawLabel();
195 }
196 } break;
197 case Expose:
198 redrawLabel();
199 break;
200 default:
201 break;
202 }
203}
204
205void FbRun::getSize(size_t &width, size_t &height) {
206 XWindowAttributes attr;
207 XGetWindowAttributes(m_display, m_win, &attr);
208 width = attr.width;
209 height = attr.height;
210}
211
212void FbRun::setNoMaximize() {
213
214 size_t width, height;
215
216 getSize(width, height);
217
218 // we don't need to maximize this window
219 XSizeHints sh;
220 sh.flags = PMaxSize | PMinSize;
221 sh.max_width = width;
222 sh.max_height = height;
223 sh.min_width = width;
224 sh.min_height = height;
225 XSetWMNormalHints(m_display, m_win, &sh);
226}
diff --git a/util/fbrun/FbRun.hh b/util/fbrun/FbRun.hh
new file mode 100644
index 0000000..d15e009
--- /dev/null
+++ b/util/fbrun/FbRun.hh
@@ -0,0 +1,76 @@
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.hh,v 1.1 2002/08/20 02:04:34 fluxgen Exp $
23
24#ifndef FBRUN_HH
25#define FBRUN_HH
26
27#include "EventHandler.hh"
28#include "Font.hh"
29
30#include <string>
31
32/**
33 Creates and managed a run window
34 TODO: a command history
35*/
36class FbRun: public FbTk::EventHandler<XEvent> {
37public:
38 FbRun(Display *disp, int x = 0, int y = 0, size_t width = 200);
39 ~FbRun();
40 void handleEvent(XEvent * const ev);
41 void setText(const std::string &text);
42 void setTitle(const std::string &title);
43 void move(int x, int y);
44 void resize(size_t width, size_t height);
45 /// hide window
46 void hide();
47 /// show window
48 void show();
49 /// load and reconfigure for new font
50 bool loadFont(const std::string &fontname);
51 void setForeground(const XColor &color);
52 void setBackground(const XColor &color);
53 const FbTk::Font &font() const { return m_font; }
54 /// execute command and exit
55 void run(const std::string &execstring);
56 /// is this object done?
57 bool end() const { return m_end; }
58private:
59 void drawString(int x, int y, const char *text, size_t len);
60 void getSize(size_t &width, size_t &height);
61 void createWindow(int x, int y, size_t width, size_t height);
62 void redrawLabel();
63 /// set no maximizable for this window
64 void setNoMaximize();
65
66 FbTk::Font m_font; ///< font used to draw command text
67 Window m_win; ///< toplevel window
68 Display *m_display; ///< display connection
69 std::string m_runtext; ///< command to execute
70 size_t m_width, m_height; ///< size of window
71 int m_bevel; ///< distance to window edge from font in pixels
72 GC m_gc;
73 bool m_end;
74};
75
76#endif // FBRUN_HH
diff --git a/util/fbrun/Makefile.am b/util/fbrun/Makefile.am
new file mode 100644
index 0000000..a74b5d6
--- /dev/null
+++ b/util/fbrun/Makefile.am
@@ -0,0 +1,7 @@
1FLUXBOX_SRC_DIR=../../src/
2INCLUDES = -I${FLUXBOX_SRC_DIR}
3bin_PROGRAMS = fbrun
4fbrun_SOURCES = FbRun.hh FbRun.cc main.cc
5fbrun_LDADD = ${FLUXBOX_SRC_DIR}Font.o
6
7fbrun.o: ${FLUXBOX_SRC_DIR}/Font.hh
diff --git a/util/fbrun/main.cc b/util/fbrun/main.cc
new file mode 100644
index 0000000..d04b4a0
--- /dev/null
+++ b/util/fbrun/main.cc
@@ -0,0 +1,163 @@
1// main.cc for FbRun
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: main.cc,v 1.1 2002/08/20 02:04:34 fluxgen Exp $
23
24#include "FbRun.hh"
25
26#include <string>
27#include <iostream>
28using namespace std;
29
30void showUsage(const char *progname) {
31 cerr<<"fbrun 1.1.0 : (c) 2002 Henrik Kinnunen"<<endl;
32 cerr<<"Usage: "<<
33 progname<<" [arguments]"<<endl<<
34 "Arguments: "<<endl<<
35 " -font [font name] Text font"<<endl<<
36 " -title [title name] Set title"<<endl<<
37 " -text [text] Text input"<<endl<<
38 " -w [width] Window width in pixels"<<endl<<
39 " -h [height] Window height in pixels"<<endl<<
40 " -display [display string] Display name"<<endl<<
41 " -pos [x] [y] Window position in pixels"<<endl<<
42 " -fg [color name] Foreground text color"<<endl<<
43 " -bg [color name] Background color"<<endl<<
44 " -help Show this help"<<endl<<endl<<
45 "Example: fbrun -fg black -bg white -text xterm -title \"run xterm\""<<endl;
46}
47
48int main(int argc, char **argv) {
49 int x = 0, y = 0; // default pos of window
50 size_t width = 200, height = 32; // default size of window
51 bool set_height = false; // use height of font by default
52 bool set_pos = false; // set position
53 string fontname; // font name
54 string title("Run program"); // default title
55 string text; // default input text
56 string foreground("black"); // text color
57 string background("white"); // text background color
58 string display_name; // name of the display connection
59
60 // parse arguments
61 for (int i=1; i<argc; i++) {
62 if (strcmp(argv[i], "-font") == 0 && i+1 < argc) {
63 ++i;
64 fontname = argv[i];
65 } else if (strcmp(argv[i], "-title") == 0 && i+1 < argc) {
66 ++i;
67 title = argv[i];
68 } else if (strcmp(argv[i], "-text") == 0 && i+1 < argc) {
69 ++i;
70 text = argv[i];
71 } else if (strcmp(argv[i], "-w") == 0 && i+1 < argc) {
72 ++i;
73 width = atoi(argv[i]);
74 } else if (strcmp(argv[i], "-h") == 0 && i+1 < argc) {
75 ++i;
76 height = atoi(argv[i]);
77 set_height = true; // mark true else the height of font will be used
78 } else if (strcmp(argv[i], "-display") == 0 && i+1 < argc) {
79 ++i;
80 display_name = argv[i];
81 } else if (strcmp(argv[i], "-pos") == 0 && i+2 < argc) {
82 ++i;
83 x = atoi(argv[i]);
84 ++i;
85 y = atoi(argv[i]);
86 set_pos = true;
87 } else if (strcmp(argv[i], "-fg") == 0 && i+1 < argc) {
88 ++i;
89 foreground = argv[i];
90 } else if (strcmp(argv[i], "-bg") == 0 && i+1 < argc) {
91 ++i;
92 background = argv[i];
93 } else if (strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "-help") == 0) {
94 showUsage(argv[0]);
95 exit(0);
96 } else {
97 cerr<<"Invalid argument: "<<argv[i]<<endl;
98 showUsage(argv[0]);
99 exit(0);
100 }
101
102 }
103
104 try {
105
106 Display *disp = 0;
107
108 // establish display connection
109 disp = XOpenDisplay(display_name.c_str());
110 if (disp == 0)
111 throw string("Can't open display: " + display_name);
112
113 FbRun fbrun(disp);
114 if (fontname.size() != 0) {
115 if (!fbrun.loadFont(fontname.c_str())) {
116 cerr<<"Failed to load font: "<<fontname<<endl;
117 cerr<<"Falling back to \"fixed\""<<endl;
118 }
119 }
120
121 // get color
122 XColor xc_foreground, xc_background;
123 if (XParseColor(disp, DefaultColormap(disp, DefaultScreen(disp)),
124 foreground.c_str(),
125 &xc_foreground) == 0) {
126 cerr<<"Faild to lookup color: "<<foreground<<endl;
127 }
128
129 if (XParseColor(disp, DefaultColormap(disp, DefaultScreen(disp)),
130 background.c_str(),
131 &xc_background) == 0) {
132 cerr<<"Faild to lookup color: "<<background<<endl;
133 }
134
135 XAllocColor(disp, DefaultColormap(disp, DefaultScreen(disp)),
136 &xc_foreground);
137 XAllocColor(disp, DefaultColormap(disp, DefaultScreen(disp)),
138 &xc_background);
139
140 fbrun.setForeground(xc_foreground);
141 fbrun.setBackground(xc_background);
142
143 if (set_height)
144 fbrun.resize(width, height);
145
146 fbrun.setTitle(title);
147 fbrun.setText(text);
148 fbrun.show();
149
150 if (set_pos)
151 fbrun.move(x, y);
152
153 XEvent event;
154 // main loop
155 while (!fbrun.end()) {
156 XNextEvent(disp, &event);
157 fbrun.handleEvent(&event);
158 }
159
160 } catch (string errstr) {
161 cerr<<"Error: "<<errstr<<endl;
162 }
163}