2017-04-27 18:27:59 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
2013-09-09 08:59:03 +00:00
|
|
|
/* main.c */
|
|
|
|
#include <locale.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2018-07-26 01:36:40 +00:00
|
|
|
#include <string.h>
|
2013-09-09 08:59:03 +00:00
|
|
|
#include <time.h>
|
|
|
|
|
2018-07-26 01:36:40 +00:00
|
|
|
#include "core/downloadfromdcthread.h" // for fill_computer_list
|
core: introduce divelog structure
The parser API was very annoying, as a number of tables
to-be-filled were passed in as pointers. The goal of this
commit is to collect all these tables in a single struct.
This should make it (more or less) clear what is actually
written into the divelog files.
Moreover, it should now be rather easy to search for
instances, where the global logfile is accessed (and it
turns out that there are many!).
The divelog struct does not contain the tables as substructs,
but only collects pointers. The idea is that the "divelog.h"
file can be included without all the other files describing
the numerous tables.
To make it easier to use from C++ parts of the code, the
struct implements a constructor and a destructor. Sadly,
we can't use smart pointers, since the pointers are accessed
from C code. Therfore the constructor and destructor are
quite complex.
The whole commit is large, but was mostly an automatic
conversion.
One oddity of note: the divelog structure also contains
the "autogroup" flag, since that is saved in the divelog.
This actually fixes a bug: Before, when importing dives
from a different log, the autogroup flag was overwritten.
This was probably not intended and does not happen anymore.
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2022-11-08 20:31:08 +00:00
|
|
|
#include "core/divelog.h"
|
2019-08-05 17:41:15 +00:00
|
|
|
#include "core/errorhelper.h"
|
2020-06-14 09:55:06 +00:00
|
|
|
#include "core/parse.h"
|
2016-04-05 05:02:03 +00:00
|
|
|
#include "core/qt-gui.h"
|
2018-07-26 01:36:40 +00:00
|
|
|
#include "core/qthelper.h"
|
2016-04-05 05:02:03 +00:00
|
|
|
#include "core/subsurfacestartup.h"
|
2024-03-25 09:58:27 +00:00
|
|
|
#include "core/subsurface-string.h"
|
2018-08-14 08:12:32 +00:00
|
|
|
#include "core/settings/qPref.h"
|
2019-05-30 16:29:36 +00:00
|
|
|
#include "core/tag.h"
|
2015-09-03 17:20:19 +00:00
|
|
|
#include "desktop-widgets/mainwindow.h"
|
cloudstorage: try to pick between multiple cloud servers
The backend infrastructure will soon be able to support more than one
cloud server which automagically stay in sync with each other.
One critical requirement for that to work is that once a session was
started with one of the servers, the complete session happens with that
server - we must not switch from server to server while doing a git
transaction. To make sure that's the case, we aren't trying to use DNS
tricks to make this load balancing scheme work, but instead try to
determine at program start which server is the best one to use.
Right now this is super simplistic. Two servers, one in the US, one in
Europe. By default we use the European server (most of our users appear
to be in Europe), but if we can figure out that the client is actually
in the Americas, use the US server. We might improve that heuristic over
time, but as a first attempt it seems not entirely bogus.
The way this is implemented is a simple combination of two free
webservices that together appear to give us a very reliable estimate
which continent the user is located on.
api.ipify.org gives us our external IP address
ip-api.com gives us the continent that IP address is on
If any of this fails or takes too long to respond, we simply ignore it
since either server will work. One oddity is that if we decide to change
servers we only change the settings that are stored on disk, not the
runtime preferences. This goes back to the comment above that we have to
avoid changing servers in mid sync.
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2021-04-11 01:03:08 +00:00
|
|
|
#include "core/checkcloudconnection.h"
|
2013-09-09 08:59:03 +00:00
|
|
|
|
2015-06-16 13:52:06 +00:00
|
|
|
#include <QApplication>
|
2015-07-06 14:11:02 +00:00
|
|
|
#include <QLoggingCategory>
|
2017-10-29 16:04:32 +00:00
|
|
|
#include <QOffscreenSurface>
|
2018-07-26 01:36:40 +00:00
|
|
|
#include <QOpenGLContext>
|
2017-10-29 16:04:32 +00:00
|
|
|
#include <QOpenGLFunctions>
|
|
|
|
#include <QQuickWindow>
|
2018-07-26 01:36:40 +00:00
|
|
|
#include <QStringList>
|
2014-03-31 20:37:41 +00:00
|
|
|
#include <git2.h>
|
2013-09-09 08:59:03 +00:00
|
|
|
|
2017-10-29 16:04:32 +00:00
|
|
|
static void validateGL();
|
2017-11-15 20:57:35 +00:00
|
|
|
static void messageHandler(QtMsgType type, const QMessageLogContext &ctx, const QString &msg);
|
2015-11-05 18:05:22 +00:00
|
|
|
|
2013-09-09 08:59:03 +00:00
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
2017-11-15 21:55:42 +00:00
|
|
|
subsurface_console_init();
|
|
|
|
qInstallMessageHandler(messageHandler);
|
|
|
|
if (verbose) /* print the version if the Win32 console_init() code enabled verbose. */
|
|
|
|
print_version();
|
|
|
|
|
2013-09-22 15:30:25 +00:00
|
|
|
bool no_filenames = true;
|
2015-07-06 14:11:02 +00:00
|
|
|
QLoggingCategory::setFilterRules(QStringLiteral("qt.bluetooth* = true"));
|
2019-09-29 11:21:12 +00:00
|
|
|
std::unique_ptr<QApplication> app(new QApplication(argc, argv));
|
2024-03-25 09:58:27 +00:00
|
|
|
std::vector<std::string> files;
|
|
|
|
std::vector<std::string> importedFiles;
|
2013-10-08 09:48:46 +00:00
|
|
|
QStringList arguments = QCoreApplication::arguments();
|
2014-03-25 14:55:56 +00:00
|
|
|
|
2015-10-06 10:10:21 +00:00
|
|
|
const char *default_directory = system_default_directory();
|
2015-10-13 18:34:26 +00:00
|
|
|
subsurface_mkdir(default_directory);
|
2015-10-06 10:10:21 +00:00
|
|
|
|
2024-03-25 09:58:27 +00:00
|
|
|
for (int i = 1; i < arguments.length(); i++) {
|
|
|
|
std::string a = arguments[i].toStdString();
|
|
|
|
if (a.empty())
|
2015-10-12 13:47:51 +00:00
|
|
|
continue;
|
2024-03-25 09:58:27 +00:00
|
|
|
if (a[0] == '-') {
|
|
|
|
parse_argument(a.c_str());
|
2013-09-09 08:59:03 +00:00
|
|
|
continue;
|
|
|
|
}
|
2013-09-22 15:30:25 +00:00
|
|
|
if (imported) {
|
2013-10-08 09:48:46 +00:00
|
|
|
importedFiles.push_back(a);
|
2013-09-22 15:30:25 +00:00
|
|
|
} else {
|
|
|
|
no_filenames = false;
|
2013-10-08 09:48:46 +00:00
|
|
|
files.push_back(a);
|
2013-09-22 15:30:25 +00:00
|
|
|
}
|
2013-09-09 08:59:03 +00:00
|
|
|
}
|
2016-03-25 08:21:45 +00:00
|
|
|
if (subsurface_user_is_root() && !force_root) {
|
|
|
|
printf("You are running Subsurface as root. This is not recommended.\n");
|
|
|
|
printf("If you insist to do so, run with option --allow_run_as_root.\n");
|
|
|
|
exit(0);
|
|
|
|
}
|
2017-10-29 19:11:05 +00:00
|
|
|
validateGL();
|
2014-12-07 19:14:58 +00:00
|
|
|
#if !LIBGIT2_VER_MAJOR && LIBGIT2_VER_MINOR < 22
|
2014-03-31 20:37:41 +00:00
|
|
|
git_threads_init();
|
2014-12-06 13:49:57 +00:00
|
|
|
#else
|
|
|
|
git_libgit2_init();
|
|
|
|
#endif
|
2014-03-19 16:23:43 +00:00
|
|
|
setup_system_prefs();
|
2015-10-06 09:15:38 +00:00
|
|
|
copy_prefs(&default_prefs, &prefs);
|
cloudstorage: try to pick between multiple cloud servers
The backend infrastructure will soon be able to support more than one
cloud server which automagically stay in sync with each other.
One critical requirement for that to work is that once a session was
started with one of the servers, the complete session happens with that
server - we must not switch from server to server while doing a git
transaction. To make sure that's the case, we aren't trying to use DNS
tricks to make this load balancing scheme work, but instead try to
determine at program start which server is the best one to use.
Right now this is super simplistic. Two servers, one in the US, one in
Europe. By default we use the European server (most of our users appear
to be in Europe), but if we can figure out that the client is actually
in the Americas, use the US server. We might improve that heuristic over
time, but as a first attempt it seems not entirely bogus.
The way this is implemented is a simple combination of two free
webservices that together appear to give us a very reliable estimate
which continent the user is located on.
api.ipify.org gives us our external IP address
ip-api.com gives us the continent that IP address is on
If any of this fails or takes too long to respond, we simply ignore it
since either server will work. One oddity is that if we decide to change
servers we only change the settings that are stored on disk, not the
runtime preferences. This goes back to the comment above that we have to
avoid changing servers in mid sync.
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2021-04-11 01:03:08 +00:00
|
|
|
CheckCloudConnection ccc;
|
|
|
|
ccc.pickServer();
|
2017-04-27 18:24:14 +00:00
|
|
|
fill_computer_list();
|
2024-05-03 21:18:45 +00:00
|
|
|
reset_tank_info_table(tank_info_table);
|
2014-03-19 16:23:43 +00:00
|
|
|
parse_xml_init();
|
|
|
|
taglist_init_global();
|
|
|
|
init_ui();
|
2014-03-20 12:56:04 +00:00
|
|
|
if (no_filenames) {
|
2015-06-14 15:32:47 +00:00
|
|
|
if (prefs.default_file_behavior == LOCAL_DEFAULT_FILE) {
|
2024-03-25 09:58:27 +00:00
|
|
|
if (!empty_string(prefs.default_filename))
|
|
|
|
files.emplace_back(prefs.default_filename ? prefs.default_filename : "");
|
2015-06-14 15:32:47 +00:00
|
|
|
} else if (prefs.default_file_behavior == CLOUD_DEFAULT_FILE) {
|
2024-03-24 21:05:28 +00:00
|
|
|
auto cloudURL = getCloudURL();
|
|
|
|
if (cloudURL)
|
2024-03-25 09:58:27 +00:00
|
|
|
files.push_back(*cloudURL);
|
2015-06-14 15:32:47 +00:00
|
|
|
}
|
2014-03-20 12:56:04 +00:00
|
|
|
}
|
2014-03-11 21:54:28 +00:00
|
|
|
MainWindow *m = MainWindow::instance();
|
2024-03-25 09:58:27 +00:00
|
|
|
if (verbose && !files.empty())
|
|
|
|
report_info("loading dive data from: %s", join(files, std::string(", ")).c_str());
|
2014-03-11 21:54:28 +00:00
|
|
|
m->loadFiles(files);
|
2024-03-25 09:58:27 +00:00
|
|
|
if (verbose && !importedFiles.empty())
|
|
|
|
report_info("importing dive data from %s", join(importedFiles, std::string(", ")).c_str());
|
2014-03-11 21:54:28 +00:00
|
|
|
m->importFiles(importedFiles);
|
2015-10-05 22:52:39 +00:00
|
|
|
|
2017-11-09 14:43:38 +00:00
|
|
|
if (verbose > 0)
|
2015-09-23 09:13:07 +00:00
|
|
|
print_files();
|
2013-11-02 19:00:16 +00:00
|
|
|
if (!quit)
|
|
|
|
run_ui();
|
2013-09-09 08:59:03 +00:00
|
|
|
exit_ui();
|
core: introduce divelog structure
The parser API was very annoying, as a number of tables
to-be-filled were passed in as pointers. The goal of this
commit is to collect all these tables in a single struct.
This should make it (more or less) clear what is actually
written into the divelog files.
Moreover, it should now be rather easy to search for
instances, where the global logfile is accessed (and it
turns out that there are many!).
The divelog struct does not contain the tables as substructs,
but only collects pointers. The idea is that the "divelog.h"
file can be included without all the other files describing
the numerous tables.
To make it easier to use from C++ parts of the code, the
struct implements a constructor and a destructor. Sadly,
we can't use smart pointers, since the pointers are accessed
from C code. Therfore the constructor and destructor are
quite complex.
The whole commit is large, but was mostly an automatic
conversion.
One oddity of note: the divelog structure also contains
the "autogroup" flag, since that is saved in the divelog.
This actually fixes a bug: Before, when importing dives
from a different log, the autogroup flag was overwritten.
This was probably not intended and does not happen anymore.
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2022-11-08 20:31:08 +00:00
|
|
|
clear_divelog(&divelog);
|
2014-03-19 16:23:43 +00:00
|
|
|
parse_xml_exit();
|
2014-03-25 14:55:56 +00:00
|
|
|
subsurface_console_exit();
|
2018-08-14 08:12:32 +00:00
|
|
|
|
|
|
|
// Sync struct preferences to disk
|
|
|
|
qPref::sync();
|
|
|
|
|
2014-12-18 07:47:42 +00:00
|
|
|
free_prefs();
|
2013-09-09 08:59:03 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2015-11-05 18:05:22 +00:00
|
|
|
|
2018-07-26 01:36:40 +00:00
|
|
|
#define VALIDATE_GL_PREFIX "validateGL(): "
|
2017-10-29 19:11:05 +00:00
|
|
|
|
2017-10-29 16:04:32 +00:00
|
|
|
void validateGL()
|
|
|
|
{
|
2024-03-25 19:33:30 +00:00
|
|
|
std::string quickBackend = qgetenv("QT_QUICK_BACKEND").toStdString();
|
2022-04-10 23:47:38 +00:00
|
|
|
/* on macOS with Qt6 (maybe others), things only work with the software backend */
|
|
|
|
#if defined(Q_OS_MACOS) && QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
|
2024-03-25 19:33:30 +00:00
|
|
|
if (quickBackend.empty()) {
|
|
|
|
quickBackend = "software";
|
2022-04-10 23:47:38 +00:00
|
|
|
qputenv("QT_QUICK_BACKEND", "software");
|
|
|
|
}
|
|
|
|
#endif
|
2017-10-30 21:03:59 +00:00
|
|
|
/* an empty QT_QUICK_BACKEND env. variable means OpenGL (default).
|
|
|
|
* only validate OpenGL; for everything else print out and return.
|
|
|
|
* https://doc.qt.io/qt-5/qtquick-visualcanvas-adaptations.html
|
|
|
|
*/
|
2024-03-25 19:33:30 +00:00
|
|
|
if (!quickBackend.empty()) {
|
2017-10-30 21:03:59 +00:00
|
|
|
if (verbose) {
|
2024-03-25 19:33:30 +00:00
|
|
|
report_info(VALIDATE_GL_PREFIX
|
|
|
|
"'QT_QUICK_BACKEND' is set to '%s'. "
|
|
|
|
"Skipping validation.", quickBackend.c_str());
|
2017-10-30 21:03:59 +00:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2017-11-01 12:31:04 +00:00
|
|
|
GLint verMajor = -1, verMinor;
|
2017-10-29 16:04:32 +00:00
|
|
|
const char *glError = NULL;
|
|
|
|
QOpenGLContext ctx;
|
|
|
|
QOffscreenSurface surface;
|
|
|
|
QOpenGLFunctions *func;
|
2017-11-01 12:31:04 +00:00
|
|
|
const char *verChar;
|
2017-10-29 16:04:32 +00:00
|
|
|
|
|
|
|
surface.setFormat(ctx.format());
|
|
|
|
surface.create();
|
|
|
|
if (!ctx.create()) {
|
|
|
|
glError = "Cannot create OpenGL context";
|
|
|
|
goto exit;
|
|
|
|
}
|
2017-10-29 19:11:05 +00:00
|
|
|
if (verbose)
|
2024-03-25 19:33:30 +00:00
|
|
|
report_info(VALIDATE_GL_PREFIX "created OpenGLContext.");
|
2017-10-29 16:04:32 +00:00
|
|
|
ctx.makeCurrent(&surface);
|
|
|
|
func = ctx.functions();
|
|
|
|
if (!func) {
|
|
|
|
glError = "Cannot obtain QOpenGLFunctions";
|
|
|
|
goto exit;
|
|
|
|
}
|
2017-10-29 19:11:05 +00:00
|
|
|
if (verbose)
|
2024-03-25 19:33:30 +00:00
|
|
|
report_info(VALIDATE_GL_PREFIX "obtained QOpenGLFunctions.");
|
2017-11-01 12:31:04 +00:00
|
|
|
// detect version for legacy profiles
|
|
|
|
verChar = (const char *)func->glGetString(GL_VERSION);
|
|
|
|
if (verChar) {
|
|
|
|
// detect GLES, show a warning and return early as we don't handle it's versioning
|
|
|
|
if (strstr(verChar, " ES ") != NULL) {
|
2024-03-25 19:33:30 +00:00
|
|
|
report_error(VALIDATE_GL_PREFIX
|
|
|
|
"WARNING: Detected OpenGL ES!\n"
|
|
|
|
"Attempting to run with the available profile!\n"
|
|
|
|
"If this fails try manually setting the environment variable\n"
|
|
|
|
"'QT_QUICK_BACKEND' with the value of 'software'\n"
|
|
|
|
"before running Subsurface!\n");
|
2018-07-26 01:36:40 +00:00
|
|
|
return;
|
2017-11-01 12:31:04 +00:00
|
|
|
}
|
Unify float calulations: use double
Internal floating point (FP) calculations should be performed using double
unless there is a very good reason. This avoids headaches with conversions.
Indeed, the vast majority of FP calculations were already done using double.
This patch adapts most remaining calculations. Not converted where things
that were based on binary representations and variables which weren't used
anyway.
An analysis of all instances follows:
core/plannernotes.c, l.404:
This was a comparison between two floats. On the left side, first an integer
was cast to float then multiplied with and integer and divided by a constant
double. The right hand side was an integer cast to a float. Simply divide by
1000.0 first to convert to double and continue with calculations. On the right
hand side, remove the cast, because the integer will be implicitely cast to
double for comparison. This conversion actually emits less instructions,
because no conversion to double and back is performed.
core/planner.c, l.613:
Same analysis as previous case.
subsurface-desktop-main.cpp, l.155:
A local variable representing the version OpenGL version. Turn this into
integer logic. Not only does this avoid dreaded FP rounding issues, it also
works correctly for minor version > 10 (not that such a thing is to be
expected anytime soon).
abstractpreferenceswidget.[h/cpp]:
A widget where the position is described as a float. Turn into double.
desktop-widgets/divelogexportdialog.cpp, l.313:
total_weight is described as float. Use double arithmetics instead. This
instance fixes a truncation warning emitted by gcc.
2017-11-20 20:39:50 +00:00
|
|
|
int min, maj;
|
|
|
|
if (sscanf(verChar, "%d.%d", &maj, &min) == 2) {
|
|
|
|
verMajor = (GLint)maj;
|
|
|
|
verMinor = (GLint)min;
|
2017-11-01 12:31:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// attempt to detect version using the newer API
|
|
|
|
if (verMajor == -1) {
|
|
|
|
func->glGetIntegerv(GL_MAJOR_VERSION, &verMajor);
|
|
|
|
func->glGetIntegerv(GL_MINOR_VERSION, &verMinor);
|
|
|
|
}
|
|
|
|
if (verMajor == -1) {
|
|
|
|
glError = "Cannot detect OpenGL version";
|
|
|
|
goto exit;
|
|
|
|
}
|
2017-10-29 19:11:05 +00:00
|
|
|
if (verbose)
|
2024-03-25 19:33:30 +00:00
|
|
|
report_info(VALIDATE_GL_PREFIX "detected OpenGL version %d.%d.", verMajor, verMinor);
|
2017-10-29 19:11:05 +00:00
|
|
|
if (verMajor * 10 + verMinor < 21) { // set 2.1 as the minimal version
|
2017-10-29 16:04:32 +00:00
|
|
|
glError = "OpenGL 2.1 or later is required";
|
2017-10-29 19:11:05 +00:00
|
|
|
goto exit;
|
|
|
|
}
|
2017-10-29 16:04:32 +00:00
|
|
|
|
|
|
|
exit:
|
|
|
|
ctx.makeCurrent(NULL);
|
|
|
|
surface.destroy();
|
|
|
|
if (glError) {
|
2024-03-25 19:33:30 +00:00
|
|
|
report_error(VALIDATE_GL_PREFIX "WARNING: %s. Using a software renderer!", glError);
|
2022-02-10 01:13:03 +00:00
|
|
|
QQuickWindow::setSceneGraphBackend("software");
|
2017-10-29 16:04:32 +00:00
|
|
|
}
|
|
|
|
}
|
2017-11-15 20:57:35 +00:00
|
|
|
|
|
|
|
// install this message handler primarily so that the Windows build can log to files
|
2018-07-26 01:36:40 +00:00
|
|
|
void messageHandler(QtMsgType type, const QMessageLogContext &, const QString &msg)
|
2017-11-15 20:57:35 +00:00
|
|
|
{
|
2018-03-02 19:29:50 +00:00
|
|
|
QByteArray localMsg = msg.toUtf8();
|
2017-11-15 20:57:35 +00:00
|
|
|
switch (type) {
|
|
|
|
case QtDebugMsg:
|
|
|
|
fprintf(stdout, "%s\n", localMsg.constData());
|
|
|
|
break;
|
|
|
|
case QtInfoMsg:
|
|
|
|
fprintf(stdout, "%s\n", localMsg.constData());
|
|
|
|
break;
|
|
|
|
case QtWarningMsg:
|
|
|
|
fprintf(stderr, "%s\n", localMsg.constData());
|
|
|
|
break;
|
|
|
|
case QtCriticalMsg:
|
|
|
|
fprintf(stderr, "%s\n", localMsg.constData());
|
|
|
|
break;
|
|
|
|
case QtFatalMsg:
|
|
|
|
fprintf(stderr, "%s\n", localMsg.constData());
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
}
|