RTXI  2.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
main.cpp
Go to the documentation of this file.
1 /*
2  The Real-Time eXperiment Interface (RTXI)
3  Copyright (C) 2011 Georgia Institute of Technology, University of Utah, Weill Cornell Medical College
4 
5  This program is free software: you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation, either version 3 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17 
18  */
19 
20 #include <QApplication>
21 #include <QtWidgets>
22 
23 #include <iostream>
24 #include <cstdlib>
25 #include <dirent.h>
26 #include <errno.h>
27 #include <getopt.h>
28 #include <signal.h>
29 #include <string>
30 #include <unistd.h>
31 #include <cmdline.h>
32 #include <ctype.h>
33 #include <daq.h>
34 #include <debug.h>
35 #include <main_window.h>
36 #include <plugin.h>
37 
38 #if XENOMAI
39 #include <rtdk.h>
40 #endif
41 
42 static pid_t parentThread;
43 
45 {
46  std::string config_file;
47  std::string plugins_path;
48 };
49 
50 static bool parse_cli_options(int,char *[],cli_options_t *);
51 static void signal_handler(int);
52 
53 int main(int argc,char *argv[])
54 {
55  int retval = 0;
56 
57 #if XENOMAI
58  /* Initialize rtdk */
59  rt_print_auto_init(1);
60 #endif
61 
62  /* Try to Exit Cleanly on Signals */
63  parentThread = getpid();
64  signal(SIGINT,signal_handler);
65  signal(SIGABRT,signal_handler);
66  signal(SIGSEGV,signal_handler);
67 
68  /* Handle Command-Line Options */
69  cli_options_t cli_options;
70  if (!parse_cli_options(argc,argv,&cli_options))
71  return -EINVAL;
72 
73  /* Find Configuration File */
74  std::string config_file;
75  if (cli_options.config_file.length())
76  config_file = cli_options.config_file;
77  else if (getenv("RTXI_CONF"))
78  config_file = getenv("RTXI_CONF");
79  else
80  config_file = "/usr/local/share/rtxi/rtxi.conf";
81 
82  /************************************************************
83  * Create Main System Components *
84  * *
85  * These need to be created early because they should have *
86  * Settings::IDs of 0 and 1. *
87  ************************************************************/
88 
89  /* Create GUI Objects */
90  QApplication::setDesktopSettingsAware(false);
91  QApplication *app = new QApplication(argc,argv);
92  app->connect(app,SIGNAL(lastWindowClosed()),app,SLOT(quit()));
94 
98 
99  /* Bootstrap the System */
100  Settings::Manager::getInstance()->load(config_file);
101  retval = app->exec();
102 
104  return retval;
105 }
106 
107 static void error_msg(const std::string &self)
108 {
109  std::cout << "Try \'" << self << " --help\' for more information.\n";
110 }
111 
112 static void help_msg(const std::string &self)
113 {
114  std::cout << "Usage: " << self << " [options]\n";
115  std::cout << " where options include:\n";
116  std::cout << " --help, -h - Displays this message\n";
117  std::cout << " --config-file, -c - Pick a custom configuration file\n";
118  std::cout << " --plugins-path, -p - Specify a plugins directory\n";
119 }
120 
121 static bool parse_cli_options(int argc,char *argv[],cli_options_t *cli_options)
122 {
123  int opt, index;
124 
125  struct option options[] =
126  {
127  { "help", no_argument, 0, 'h' },
128  { "config-file", required_argument, 0, 'c' },
129  { "plugins-path", required_argument, 0, 'p' },
130  { "models-path", required_argument, 0, 'm' },
131  { 0,0,0,0 }
132  };
133 
134  for (;;)
135  {
136  opt = getopt_long(argc,argv,"hc:p:m:",options,&index);
137 
138  if (opt < 0) break;
139 
140  switch (opt)
141  {
142  case 'c':
143  cli_options->config_file = optarg;
144  break;
145  case 'h':
146  help_msg(argv[0]);
147  return false;
148  case 'p':
149  cli_options->plugins_path = optarg;
150  break;
151  default:
152  error_msg(argv[0]);
153  return false;
154  }
155  }
156 
157  return true;
158 }
159 
160 static void signal_handler(int signum)
161 {
162  static int count = 0;
163 
164  /* Only handler handle signals in the parent */
165  if (getpid() != parentThread) return;
166 
167  DEBUG_MSG("signal_handler : signal received\n");
168  PRINT_BACKTRACE();
169 
170  if (count++) _exit(-EFAULT);
171 
172  DEBUG_MSG("signal_handler : finished\n");
173  exit(0);
174 }
static MainWindow * getInstance(void)
std::string plugins_path
Definition: main.cpp:47
int load(const std::string &)
Definition: settings.cpp:230
std::string config_file
Definition: main.cpp:46
static CmdLine * getInstance(void)
Definition: cmdline.cpp:79
void loadWindow(void)
static Connector * getInstance(void)
Definition: io.cpp:421
void unloadAll(void)
Definition: plugin.cpp:116
static Manager * getInstance(void)
Definition: plugin.cpp:186
static System * getInstance(void)
Definition: rt.cpp:361
static Manager * getInstance(void)
Definition: settings.cpp:534
int main(int argc, char *argv[])
Definition: main.cpp:53
#define DEBUG_MSG(fmt, args...)
Prints debug messages to standard error.
Definition: debug.h:53