~ubuntu-branches/ubuntu/trusty/fluxbox/trusty-proposed

« back to all changes in this revision

Viewing changes to util/fbrun/main.cc

  • Committer: Bazaar Package Importer
  • Author(s): Dmitry E. Oboukhov
  • Date: 2008-07-01 10:38:14 UTC
  • mfrom: (2.1.12 intrepid)
  • Revision ID: james.westby@ubuntu.com-20080701103814-khx2b6il152x9p93
Tags: 1.0.0+deb1-8
* x-dev has been removed from build-depends (out-of-date package).
* Standards-Version bumped to 3.8.0.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
// main.cc for FbRun
2
 
// Copyright (c) 2002 - 2005 Henrik Kinnunen (fluxgen at fluxbox dot 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 4043 2005-06-03 07:25:48Z mathias $
23
 
 
24
 
#include "FbRun.hh"
25
 
#include "App.hh"
26
 
#include "StringUtil.hh"
27
 
#include "Color.hh"
28
 
 
29
 
#ifdef XINERAMA
30
 
extern  "C" {
31
 
#include <X11/extensions/Xinerama.h>
32
 
}
33
 
#endif // XINERAMA
34
 
 
35
 
#include <string>
36
 
#include <iostream>
37
 
 
38
 
using namespace std;
39
 
 
40
 
void showUsage(const char *progname) {
41
 
    cerr<<"fbrun 1.5 : (c) 2002-2004 Henrik Kinnunen"<<endl;
42
 
    cerr<<"Usage: "<<
43
 
        progname<<" [arguments]"<<endl<<
44
 
        "Arguments: "<<endl<<
45
 
        "   -font [font name]           Text font"<<endl<<
46
 
        "   -title [title name]         Set title"<<endl<<
47
 
        "   -text [text]                Text input"<<endl<<
48
 
        "   -w [width]                  Window width in pixels"<<endl<<
49
 
        "   -h [height]                 Window height in pixels"<<endl<<
50
 
        "   -display [display string]   Display name"<<endl<<
51
 
        "   -pos [x] [y]                Window position in pixels"<<endl<<
52
 
        "   -nearmouse                  Window position near mouse"<<endl<<
53
 
        "   -fg [color name]            Foreground text color"<<endl<<
54
 
        "   -bg [color name]            Background color"<<endl<<
55
 
        "   -na                         Disable antialias"<<endl<<
56
 
        "   -hf [history file]          History file to load (default ~/.fluxbox/fbrun_history)"<<endl<<
57
 
        "   -help                       Show this help"<<endl<<endl<<
58
 
        "Example: fbrun -fg black -bg white -text xterm -title \"run xterm\""<<endl;
59
 
}
60
 
 
61
 
int main(int argc, char **argv) {
62
 
    int x = 0, y = 0; // default pos of window
63
 
    size_t width = 200, height = 32; // default size of window
64
 
    bool set_height = false, set_width=false; // use height/width of font by default
65
 
    bool set_pos = false; // set position
66
 
    bool near_mouse = false; // popup near mouse
67
 
    bool antialias = true; // antialias text
68
 
    string fontname; // font name
69
 
    string title("Run program"); // default title
70
 
    string text;         // default input text
71
 
    string foreground("black");   // text color
72
 
    string background("white");   // text background color
73
 
    string display_name; // name of the display connection
74
 
    string history_file("~/.fluxbox/fbrun_history"); // command history file
75
 
    // parse arguments
76
 
    for (int i=1; i<argc; i++) {
77
 
        if (strcmp(argv[i], "-font") == 0 && i+1 < argc) {
78
 
            fontname = argv[++i];
79
 
        } else if (strcmp(argv[i], "-title") == 0 && i+1 < argc) {
80
 
            title = argv[++i];
81
 
        } else if (strcmp(argv[i], "-text") == 0 && i+1 < argc) {
82
 
            text = argv[++i];
83
 
        } else if (strcmp(argv[i], "-w") == 0 && i+1 < argc) {
84
 
            width = atoi(argv[++i]);                    
85
 
            set_width = true;
86
 
        } else if (strcmp(argv[i], "-h") == 0 && i+1 < argc) {
87
 
            height = atoi(argv[++i]);
88
 
            set_height = true; // mark true else the height of font will be used
89
 
        } else if (strcmp(argv[i], "-display") == 0 && i+1 < argc) {
90
 
            display_name = argv[++i];
91
 
        } else if (strcmp(argv[i], "-pos") == 0 && i+2 < argc) {
92
 
            x = atoi(argv[++i]);
93
 
            y = atoi(argv[++i]);
94
 
            set_pos = true;
95
 
        } else if (strcmp(argv[i], "-nearmouse") == 0) {
96
 
            set_pos = true;
97
 
            near_mouse = true;
98
 
        } else if (strcmp(argv[i], "-fg") == 0 && i+1 < argc) {
99
 
            foreground = argv[++i];
100
 
        } else if (strcmp(argv[i], "-bg") == 0 && i+1 < argc) {
101
 
            background = argv[++i];
102
 
        } else if (strcmp(argv[i], "-na") == 0) {
103
 
            antialias = false;
104
 
        } else if (strcmp(argv[i], "-hf") == 0 && i+1 < argc) {
105
 
            history_file = argv[++i];
106
 
        } else if (strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "-help") == 0) {
107
 
            showUsage(argv[0]);
108
 
            exit(0);
109
 
        } else {
110
 
            cerr<<"Invalid argument: "<<argv[i]<<endl;
111
 
            showUsage(argv[0]);
112
 
            exit(0);
113
 
        }
114
 
 
115
 
    }
116
 
 
117
 
    try {
118
 
                
119
 
        FbTk::App application(display_name.c_str());
120
 
        FbRun fbrun;
121
 
 
122
 
        //fbrun.setAntialias(antialias);
123
 
 
124
 
        if (fontname.size() != 0) {
125
 
            if (!fbrun.loadFont(fontname.c_str())) {
126
 
                cerr<<"Failed to load font: "<<fontname<<endl;
127
 
                cerr<<"Falling back to \"fixed\""<<endl;
128
 
            }
129
 
        }
130
 
 
131
 
        // get color
132
 
        XColor xc_foreground, xc_background;
133
 
        FbTk::Color fg_color(foreground.c_str(), 0);
134
 
        FbTk::Color bg_color(background.c_str(), 0);
135
 
                
136
 
        fbrun.setForegroundColor(fg_color);
137
 
        fbrun.setBackgroundColor(bg_color);
138
 
 
139
 
        if (set_height)
140
 
            fbrun.resize(fbrun.width(), height);
141
 
        if (set_width)
142
 
            fbrun.resize(width, fbrun.height());
143
 
        //if (antialias)
144
 
        //    fbrun.setAntialias(antialias);
145
 
        // expand and load command history
146
 
        string expanded_filename = FbTk::StringUtil::expandFilename(history_file);
147
 
        if (!fbrun.loadHistory(expanded_filename.c_str()))
148
 
            cerr<<"FbRun Warning: Failed to load history file: "<<expanded_filename<<endl;
149
 
 
150
 
        fbrun.setTitle(title);
151
 
        fbrun.setText(text);
152
 
        
153
 
        if (near_mouse) {
154
 
            
155
 
            int wx, wy;
156
 
            unsigned int mask;
157
 
            Window ret_win;
158
 
            Window child_win;
159
 
            
160
 
            Display* dpy = FbTk::App::instance()->display();
161
 
            
162
 
            if (XQueryPointer(dpy, DefaultRootWindow(dpy), 
163
 
                              &ret_win, &child_win,
164
 
                              &x, &y, &wx, &wy, &mask)) {
165
 
 
166
 
                int root_x = 0;
167
 
                int root_y = 0;
168
 
                unsigned int root_w = WidthOfScreen(DefaultScreenOfDisplay(dpy));
169
 
                unsigned int root_h = HeightOfScreen(DefaultScreenOfDisplay(dpy));
170
 
#ifdef XINERAMA
171
 
                if(XineramaIsActive(dpy)) {
172
 
                    XineramaScreenInfo* screen_info = 0;
173
 
                    int number = 0;
174
 
                    screen_info = XineramaQueryScreens(dpy, &number);
175
 
                    if (screen_info) {
176
 
                        for(int i= 0; i < number; i++) {
177
 
                            if (x >= screen_info[i].x_org &&
178
 
                                x <  screen_info[i].x_org + screen_info[i].width &&
179
 
                                y >= screen_info[i].y_org &&
180
 
                                y <  screen_info[i].y_org + screen_info[i].height) {
181
 
                                root_x = screen_info[i].x_org;
182
 
                                root_y = screen_info[i].y_org;
183
 
                                root_w = screen_info[i].width;
184
 
                                root_h = screen_info[i].height;
185
 
                                break;
186
 
                            }
187
 
                        }
188
 
 
189
 
                        XFree(screen_info);
190
 
                    }
191
 
                }
192
 
#endif // XINERAMA
193
 
                x-= fbrun.width()/2;
194
 
                y-= fbrun.height()/2;
195
 
 
196
 
                if (x < root_x)
197
 
                    x = root_x;
198
 
                if (x + fbrun.width() > root_x + root_w)
199
 
                    x = root_x + root_w - fbrun.width();
200
 
                if (y < root_y)
201
 
                    y = root_y;
202
 
                if (y + fbrun.height() > root_y + root_h)
203
 
                    y = root_y + root_h - fbrun.height();
204
 
            }
205
 
        }
206
 
        
207
 
        if (set_pos)
208
 
            fbrun.move(x, y);
209
 
                
210
 
        fbrun.show();
211
 
 
212
 
        application.eventLoop();
213
 
 
214
 
    } catch (string errstr) {
215
 
        cerr<<"Error: "<<errstr<<endl;
216
 
    }
217
 
}