summaryrefslogtreecommitdiff
path: root/util/fbrun/FbRun.cc
blob: 7aa7938155e0148b038ec94b84a20272fbe30c4a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
// FbRun.hh
// Copyright (c) 2002 Henrik Kinnunen (fluxgen@linuxmail.org)
//
// 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.

// $Id: FbRun.cc,v 1.7 2002/11/26 17:13:36 fluxgen Exp $

#include "FbRun.hh"

#include "App.hh"

#include <X11/Xlib.h>
#include <X11/keysym.h>
#include <X11/Xutil.h>

#include <unistd.h>

#include <iostream>
#include <fstream>

using namespace std;
FbRun::FbRun(int x, int y, size_t width):
m_font("fixed"),
m_win(None),
m_display(FbTk::App::instance()->display()),
m_bevel(4),
m_gc(DefaultGC(m_display, DefaultScreen(m_display))),
m_end(false),
m_current_history_item(0) {
	createWindow(x, y, width + m_bevel, m_font.height());
}

FbRun::~FbRun() {
	hide();
	XDestroyWindow(m_display, m_win);
}

void FbRun::run(const std::string &command) {
	//fork and execute program
	if (!fork()) {
		setsid();
		execl("/bin/sh", "/bin/sh", "-c", command.c_str(), 0);
		exit(0); //exit fork
	}

	hide(); // hide gui
	
	// save command history to file
	if (m_runtext.size() != 0) { // no need to save empty command
		// open file in append mode
		ofstream outfile(m_history_file.c_str(), ios::app);
		if (outfile)
			outfile<<m_runtext<<endl;
		else 
			cerr<<"FbRun Warning: Can't write command history to file: "<<m_history_file<<endl;
	}

	m_end = true; // mark end of processing
}

bool FbRun::loadHistory(const char *filename) {
	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;
}

bool FbRun::loadFont(const string &fontname) {
	if (!m_font.load(fontname.c_str()))
		return false;

	// resize to fit new font height
	resize(m_width, m_font.height() + m_bevel);
	return true;
}

void FbRun::setForeground(const XColor &color) {
	XSetForeground(m_display, m_gc, color.pixel);
	redrawLabel();
}

void FbRun::setBackground(const XColor &color) {
	XSetWindowBackground(m_display, m_win, color.pixel);
	redrawLabel();
}


void FbRun::setText(const string &text) {
	m_runtext = text;
	redrawLabel();
}

void FbRun::setTitle(const string &title) {
	assert(m_win);
	XStoreName(m_display, m_win, const_cast<char *>(title.c_str()));	
}

void FbRun::move(int x, int y) {
	XMoveWindow(m_display, m_win, x, y);
}

void FbRun::resize(size_t width, size_t height) {
	assert(m_win);
	XResizeWindow(m_display, m_win, width, height);
	m_width = width;
	m_height = height;
	setNoMaximize();
}

void FbRun::show() {
	assert(m_win);
	XMapWindow(m_display, m_win);
}

void FbRun::hide() {
	assert(m_win);
	XUnmapWindow(m_display, m_win);
}

void FbRun::redrawLabel() {
	assert(m_win);

	XClearWindow(m_display, m_win);
	drawString(m_bevel/2, m_font.ascent() + m_bevel/2,
		m_runtext.c_str(), m_runtext.size());

}

void FbRun::drawString(int x, int y,
	const char *text, size_t len) {
	assert(m_win);
	assert(m_gc);
	// check right boundary
	// and adjust text drawing
	size_t text_width = m_font.textWidth(text, len);
	size_t startpos = 0;
	if (text_width > m_width) {
		for (; startpos < len; ++startpos) {
			if (m_font.textWidth(text+startpos, len-startpos) < m_width)
				break;
		}		
	}

	m_font.drawText(m_win, DefaultScreen(m_display), m_gc, text + startpos, len-startpos, x, y);
}


void FbRun::createWindow(int x, int y, size_t width, size_t height) {
	m_win = XCreateSimpleWindow(m_display, // display
		DefaultRootWindow(m_display), // parent windows
		x, y,
		width, height,
		1,  // border_width
		0,  // border
		WhitePixel(m_display, DefaultScreen(m_display))); // background

	if (m_win == None)
		throw string("Failed to create FbRun window!");

	XSelectInput(m_display, m_win, KeyPressMask|ExposureMask);

	setNoMaximize();

	m_width = width;
	m_height = height;

}

void FbRun::handleEvent(XEvent * const xev) {
	switch (xev->type) {
		case KeyPress: {
			KeySym ks;
			char keychar[1];
			XLookupString(&xev->xkey, keychar, 1, &ks, 0);
			if (ks == XK_Escape) {
				m_end = true;
				hide();
				return; // no more processing
			} else if (ks == XK_Return) {
				run(m_runtext);
				m_runtext = ""; // clear text
			} else if (ks == XK_BackSpace) {
				if (m_runtext.size() != 0) { // we can't erase what we don't have ;)
					m_runtext.erase(m_runtext.size()-1);
					redrawLabel();
				}
			} else if (! IsModifierKey(ks) && !IsCursorKey(ks)) {
				m_runtext+=keychar[0]; // append character
				redrawLabel(); 
			} else if (IsCursorKey(ks)) {
				
				switch (ks) {
				case XK_Up:
					prevHistoryItem();
				break;
				case XK_Down:
					nextHistoryItem();
				break;
				}
				redrawLabel();
			}
		} break;
		case Expose:
			redrawLabel();
		break;
		default:
		break;
	}
}

void FbRun::getSize(size_t &width, size_t &height) {
	XWindowAttributes attr;
	XGetWindowAttributes(m_display, m_win, &attr);
	width = attr.width;
	height = attr.height;
}

void FbRun::setNoMaximize() {

	size_t width, height;

	getSize(width, height);

	// we don't need to maximize this window
	XSizeHints sh;
	sh.flags = PMaxSize | PMinSize;
	sh.max_width = width;
	sh.max_height = height;
	sh.min_width = width;
	sh.min_height = height;
	XSetWMNormalHints(m_display, m_win, &sh);
}

void FbRun::prevHistoryItem() {

	if (m_current_history_item > 0 && m_history.size() > 0)
		m_current_history_item--;
	if (m_current_history_item < m_history.size())
		m_runtext = m_history[m_current_history_item];
}

void FbRun::nextHistoryItem() {
	m_current_history_item++;
	if (m_current_history_item >= m_history.size()) {
		m_current_history_item = m_history.size();
		m_runtext = "";
		return;
	} else 
		m_runtext = m_history[m_current_history_item];

}