1 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
2 // main.cc for Blackbox - an X11 Window manager
3 // Copyright (c) 2001 - 2005 Sean 'Shaleh' Perry <shaleh@debian.org>
4 // Copyright (c) 1997 - 2000, 2002 - 2005
5 //         Bradley T Hughes <bhughes at trolltech.com>
6 //
7 // Permission is hereby granted, free of charge, to any person obtaining a
8 // copy of this software and associated documentation files (the "Software"),
9 // to deal in the Software without restriction, including without limitation
10 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 // and/or sell copies of the Software, and to permit persons to whom the
12 // Software is furnished to do so, subject to the following conditions:
13 //
14 // The above copyright notice and this permission notice shall be included in
15 // all copies or substantial portions of the Software.
16 //
17 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
20 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23 // DEALINGS IN THE SOFTWARE.
24 
25 // #define PRINT_SIZES
26 
27 #include <cstdlib>
28 
29 #if defined(PRINT_SIZES)
30 #  include "Screen.hh"
31 #  include "Slit.hh"
32 #  include "Toolbar.hh"
33 #  include "Window.hh"
34 #endif
35 
36 #include "blackbox.hh"
37 #include "../version.h"
38 
39 #include <cstring>
40 #include <stdio.h>
41 
42 
showHelp(int exitval)43 static void showHelp(int exitval) {
44   // print version - this should not be localized!
45   printf("Blackbox %s\n"
46          "Copyright (c) 2001 - 2005 Sean 'Shaleh' Perry\n"
47          "Copyright (c) 1997 - 2000, 2002 - 2005 Bradley T Hughes\n",
48          __blackbox_version);
49 
50   // print program usage and command line options
51   printf("  -display <string>\t\tuse display connection.\n"
52          "  -single <string>\t\tmanage the default screen only\n"
53          "  -rc <string>\t\t\tuse alternate resource file.\n"
54          "  -version\t\t\tdisplay version and exit.\n"
55          "  -help\t\t\t\tdisplay this help text and exit.\n\n");
56 
57   // some people have requested that we print out compile options
58   // as well
59   printf("Compile time options:\n"
60          "  Debugging:\t\t\t%s\n"
61          "  Shape:\t\t\t%s\n\n",
62 #ifdef    DEBUG
63          "yes",
64 #else // !DEBUG
65          "no",
66 #endif // DEBUG
67 
68 #ifdef    SHAPE
69          "yes"
70 #else // !SHAPE
71          "no"
72 #endif // SHAPE
73          );
74 
75   std::exit(exitval);
76 }
77 
main(int argc,char ** argv)78 int main(int argc, char **argv) {
79   const char *dpy_name = 0;
80   std::string rc_file;
81   bool multi_head = true;
82 
83   for (int i = 1; i < argc; ++i) {
84     if (! strcmp(argv[i], "-help")) {
85       showHelp(0);
86     } else if (! strcmp(argv[i], "-version")) {
87       // print current version string, this should not be localized!
88       printf("Blackbox %s\n"
89              "Copyright (c) 2001 - 2005 Sean 'Shaleh' Perry\n"
90              "Copyright (c) 1997 - 2000, 2002 - 2005 Bradley T Hughes\n",
91              __blackbox_version);
92 
93       std::exit(0);
94     } else if (! strcmp(argv[i], "-rc")) {
95       // look for alternative rc file to use
96 
97       if ((++i) >= argc) {
98         fprintf(stderr, "error: '-rc' requires and argument\n");
99         std::exit(1);
100       }
101 
102       rc_file = argv[i];
103     } else if (! strcmp(argv[i], "-display")) {
104       // check for -display option... to run on a display other than the one
105       // set by the environment variable DISPLAY
106 
107       if ((++i) >= argc) {
108         fprintf(stderr, "error: '-display' requires an argument\n");
109 	std::exit(1);
110       }
111 
112       dpy_name = argv[i];
113       std::string dtmp = "DISPLAY=";
114       dtmp += dpy_name;
115 
116       if (putenv(const_cast<char*>(dtmp.c_str()))) {
117         fprintf(stderr,
118                 "warning: couldn't set environment variable 'DISPLAY'\n");
119         perror("putenv()");
120       }
121     } else if (! strcmp(argv[i], "-single")) {
122       multi_head = false;
123     } else { // invalid command line option
124       showHelp(-1);
125     }
126   }
127 
128 #ifdef    __EMX__
129   _chdir2(getenv("X11ROOT"));
130 #endif // __EMX__
131 
132   if (rc_file.empty())
133     rc_file = "~/.blackboxrc";
134   rc_file = bt::expandTilde(rc_file);
135 
136 #if defined(PRINT_SIZES)
137   printf("Blackbox      : %4d bytes\n"
138          "BScreen       : %4d bytes\n"
139          "BlackboxWindow: %4d bytes\n"
140          "Toolbar       : %4d bytes\n"
141          "Slit          : %4d bytes\n",
142          sizeof(Blackbox),
143          sizeof(BScreen),
144          sizeof(BlackboxWindow),
145          sizeof(Toolbar),
146          sizeof(Slit));
147 #endif
148 
149   Blackbox blackbox(argv, dpy_name, rc_file, multi_head);
150   blackbox.run();
151   return 0;
152 }
153