YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
main.cpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2006-2021 Istituto Italiano di Tecnologia (IIT)
3 * SPDX-License-Identifier: LGPL-2.1-or-later
4 */
5
6#include "mainwindow.h"
7#include <QApplication>
8
9#include <yarp/os/Network.h>
10#include <yarp/os/Property.h>
11#include <yarp/os/Os.h>
12#include <yarp/os/Log.h>
14#include <string>
16
17
18#define HELP_MESSAGE "\
19Usage:\n\
20 yarpmanager [option]\n\n\
21Options:\n\
22 --load_application Path to application to load in the main window\n\
23 --from Configuration file name\n\
24 --application Path to application to open\n\
25 --ymanagerini_dir \n\
26 --apppath \n\
27 --modpath \n\
28 --respath \n\
29 --templpath \n\
30 --load_subfolders \n\
31 --watchdog \n\
32 --module_failure \n\
33 --connection_failure \n\
34 --auto_connect \n\
35 --auto_dependency \n\
36 --add_current_dir add the current dir to the search path\n\
37 --elapsed_time if present the time stamp for log messages will be relative to the GUI start time\n\
38"
39
40#define DEF_CONFIG_FILE "ymanager.ini"
41
42
43#if defined(_WIN32)
44//#include <csignal>
45#include <windows.h>
46
47#else
48
49#include <cerrno>
50#include <sys/types.h>
51#include <csignal>
52#endif
53
54void onSignal(int signum);
55
56int main(int argc, char *argv[])
57{
58
59#if defined(_WIN32)
60 // We create a console. This is inherited by console processes created by the localhost broker.
61 // It is useful because new processes can receive ctrl+brk signals and shutdown cleanly.
62 // This console is not actually needed for printing so we hide it. In principle we could
63 // redirect the output of all processes to this console, in practice this would be end up
64 // soon in a big mess.
65 AllocConsole();
66 HWND hwnd = GetConsoleWindow();
67 HWND hide = FindWindowA("ConsoleWindowClass",NULL);
68 ShowWindow(hide, 0);
69#endif
70 QApplication a(argc, argv);
71
72 // Setup resource finder
73
75 rf.setDefaultContext("yarpmanager");
77 rf.configure(argc, argv);
78
80
81 yarp::os::Property config;
82 config.fromString(rf.toString());
83
84 if(config.check("help"))
85 {
86 yInfo("%s",HELP_MESSAGE);
87 return 0;
88 }
89
93 bool add_curr_dir = false;
94 if(config.check("add_current_dir"))
95 {
96 add_curr_dir=true;
97 }
98 const int cur_dir_max_size=512;
99 char current_dir[cur_dir_max_size]; current_dir[0]=0;
100 yarp::os::getcwd(current_dir,cur_dir_max_size);
101 config.put("current_dir", current_dir);
102
103 std::string inifile=rf.findFile("from");
104 std::string inipath;
105 size_t lastSlash=inifile.rfind('/');
106 if (lastSlash!=std::string::npos){
107 inipath=inifile.substr(0, lastSlash+1);
108 }else{
109 lastSlash=inifile.rfind('\\');
110 if (lastSlash!=std::string::npos){
111 inipath=inifile.substr(0, lastSlash+1);
112 }
113 }
114
115 if(!config.check("ymanagerini_dir")){
116 config.put("ymanagerini_dir", inipath);
117 }
118
119 yarp::os::Bottle appPaths;
120 if(!config.check("apppath")){
121
122 appPaths= rf.findPaths("applications");
123
124 yarp::os::ResourceFinderOptions findRobotScripts;
126 yarp::os::Bottle appPaths2=rf.findPaths("scripts", findRobotScripts);
127// yarp::os::Bottle appPaths2=rf.findPaths("scripts");
128// std::cout << "app path : " << appPaths.toString()<< std::endl;
129 QString appPathsStr="";
130 for (size_t ind=0; ind < appPaths.size(); ++ind){
131 appPathsStr += (appPaths.get(ind).asString() + ";").c_str();
132 }
133 for (size_t ind=0; ind < appPaths2.size(); ++ind){
134 appPathsStr += (appPaths2.get(ind).asString() + ";").c_str();
135 }
136 if (add_curr_dir)
137 {
138 appPathsStr += (current_dir + std::string(";")).c_str();
139 }
140 std::string sss= appPathsStr.toLatin1().data();
141 config.put("apppath", appPathsStr.toLatin1().data());
142 }
143
144 if(!config.check("modpath")){
145 appPaths=rf.findPaths("modules");
146 //std::cout << "mod path : " << appPaths.toString()<< std::endl;
147 QString modPathsStr="";
148 for (size_t ind=0; ind < appPaths.size(); ++ind){
149 modPathsStr += (appPaths.get(ind).asString() + ";").c_str();
150 }
151 if (add_curr_dir)
152 {
153 modPathsStr += (current_dir + std::string(";")).c_str();
154 }
155 config.put("modpath", modPathsStr.toLatin1().data());
156 }
157
158 if(!config.check("respath")){
159 appPaths=rf.findPaths("resources");
160 //std::cout << "res path : " << appPaths.toString()<< std::endl;
161 QString resPathsStr="";
162 for (size_t ind=0; ind < appPaths.size(); ++ind){
163 resPathsStr += (appPaths.get(ind).asString() + ";").c_str();
164 }
165 if (add_curr_dir)
166 {
167 resPathsStr += (current_dir + std::string(";")).c_str();
168 }
169 config.put("respath", resPathsStr.toLatin1().data());
170 }
171
172 if(!config.check("templpath")){
173 appPaths=rf.findPaths("templates/applications");
174 // std::cout << "templ path : " << appPaths.toString()<< std::endl;
175 QString templPathsStr="";
176 for (size_t ind=0; ind < appPaths.size(); ++ind){
177 templPathsStr += (appPaths.get(ind).asString() + ";").c_str();
178 }
179 if (add_curr_dir)
180 {
181 templPathsStr += (current_dir + std::string(";")).c_str();
182 }
183 config.put("templpath", templPathsStr.toLatin1().data());
184
185 }
186
187 if(!config.check("load_subfolders")){
188 config.put("load_subfolders", "yes");
189 }
190
191 if(!config.check("watchdog")){
192 config.put("watchdog", "no");
193 }
194
195 if(!config.check("module_failure")){
196 config.put("module_failure", "prompt");
197 }
198
199 if(!config.check("connection_failure")){
200 config.put("connection_failure", "prompt");
201 }
202
203 if(!config.check("auto_connect")){
204 config.put("auto_connect", "no");
205 }
206
207 if(!config.check("auto_dependency")){
208 config.put("auto_dependency", "no");
209 }
210
211 if(!config.check("elapsed_time"))
212 {
214 clock.setStartTime("00:00:00");
215 }
216
217#if defined(_WIN32)
218 //setup signal handler for windows
219// signal(SIGINT, onSignal);
220// signal(SIGBREAK, onSignal);
221// signal(SIGTERM, onSignal);
222
223#else
224 // Set up the structure to specify the new action.
225// struct sigaction new_action, old_action;
226// new_action.sa_handler = onSignal;
227// sigemptyset (&new_action.sa_mask);
228// new_action.sa_flags = 0;
229// sigaction (SIGINT, NULL, &old_action);
230// if (old_action.sa_handler != SIG_IGN)
231// sigaction (SIGINT, &new_action, NULL);
232// sigaction (SIGHUP, NULL, &old_action);
233// if (old_action.sa_handler != SIG_IGN)
234// sigaction (SIGHUP, &new_action, NULL);
235// sigaction (SIGTERM, NULL, &old_action);
236// if (old_action.sa_handler != SIG_IGN)
237// sigaction (SIGTERM, &new_action, NULL);
238#endif
239
240 MainWindow w;
241 w.init(config);
242 w.show();
243
244 return a.exec();
245}
246
247// void onSignal(int signum)
248// {
249// qDebug("Use <quit> menu to exit!");
250// }
#define yInfo(...)
Definition Log.h:319
MainWindow class.
Definition display.h:22
void init(yarp::os::Property config)
Init the application with the current configuration.
Singleton class For storing execution start time.
Definition utility.h:86
void setStartTime(std::string startTimeStr)
Set the starting time from a string in HH:MM:SS format.
Definition utility.h:110
static ClockStart & getInstance()
Definition utility.h:88
A simple collection of objects that can be described and transmitted in a portable way.
Definition Bottle.h:64
size_type size() const
Gets the number of elements in the bottle.
Definition Bottle.cpp:251
Value & get(size_type index) const
Reads a Value v from a certain part of the list.
Definition Bottle.cpp:246
Utilities for manipulating the YARP network, including initialization and shutdown.
Definition Network.h:706
A class for storing options and configuration information.
Definition Property.h:33
void fromString(const std::string &txt, bool wipe=true)
Interprets a string as a list of properties.
void put(const std::string &key, const std::string &value)
Associate the given key with the given string.
Definition Property.cpp:987
bool check(const std::string &key) const override
Check if there exists a property of the given name.
These options are loosely based on http://wiki.icub.org/wiki/YARP_ResourceFinder.
Helper class for finding config files and other external resources.
bool setDefaultContext(const std::string &contextName)
Sets the context for the current ResourceFinder object.
bool configure(int argc, char *argv[], bool skipFirstArgument=true)
Sets up the ResourceFinder.
std::string toString() const override
Return a standard text representation of the content of the object.
static ResourceFinder & getResourceFinderSingleton()
Access a ResourceFinder singleton whose lifetime will match that of the YARP library.
yarp::os::Bottle findPaths(const std::string &name)
Expand a partial path to a list of paths.
std::string findFile(const std::string &name)
Find the full path to a file.
bool setDefaultConfigFile(const std::string &fname)
Provide a default value for the configuration file (can be overridden from command line with the –fro...
virtual std::string asString() const
Get string value.
Definition Value.cpp:234
int main(int argc, char *argv[])
Definition main.cpp:385
#define HELP_MESSAGE
Definition main.cpp:18
void onSignal(int signum)
#define DEF_CONFIG_FILE
Definition main.cpp:40
char * getcwd(char *buf, size_t size)
Portable wrapper for the getcwd() function.
Definition Os.cpp:105
@ YARP_CLOCK_SYSTEM
Definition Time.h:28
The main, catch-all namespace for YARP.
Definition dirs.h:16