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 <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <time.h>
|
|
|
|
|
2016-04-05 05:02:03 +00:00
|
|
|
#include "core/dive.h"
|
|
|
|
#include "core/qt-gui.h"
|
|
|
|
#include "core/subsurfacestartup.h"
|
2015-09-03 17:20:19 +00:00
|
|
|
#include "desktop-widgets/mainwindow.h"
|
2017-04-04 17:21:30 +00:00
|
|
|
#include "desktop-widgets/tab-widgets/maintab.h"
|
2015-11-05 17:32:19 +00:00
|
|
|
#include "profile-widget/profilewidget2.h"
|
2016-04-05 05:02:03 +00:00
|
|
|
#include "desktop-widgets/preferences/preferencesdialog.h"
|
2015-09-03 17:20:19 +00:00
|
|
|
#include "desktop-widgets/diveplanner.h"
|
2016-04-05 05:02:03 +00:00
|
|
|
#include "core/color.h"
|
|
|
|
#include "core/qthelper.h"
|
2017-04-27 18:24:14 +00:00
|
|
|
#include "core/downloadfromdcthread.h" // for fill_computer_list
|
2013-09-09 08:59:03 +00:00
|
|
|
|
|
|
|
#include <QStringList>
|
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 <QOpenGLContext>
|
|
|
|
#include <QOffscreenSurface>
|
|
|
|
#include <QOpenGLFunctions>
|
|
|
|
#include <QQuickWindow>
|
2014-03-31 20:37:41 +00:00
|
|
|
#include <git2.h>
|
2013-09-09 08:59:03 +00:00
|
|
|
|
2015-11-05 18:05:22 +00:00
|
|
|
static bool filesOnCommandLine = false;
|
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-09 08:59:03 +00:00
|
|
|
int i;
|
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"));
|
2015-06-16 13:52:06 +00:00
|
|
|
QApplication *application = new QApplication(argc, argv);
|
2015-11-07 20:18:47 +00:00
|
|
|
(void)application;
|
2013-09-09 08:59:03 +00:00
|
|
|
QStringList files;
|
|
|
|
QStringList 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();
|
|
|
|
const char *default_filename = system_default_filename();
|
2015-10-13 18:34:26 +00:00
|
|
|
subsurface_mkdir(default_directory);
|
2015-10-06 10:10:21 +00:00
|
|
|
|
2013-10-08 09:48:46 +00:00
|
|
|
for (i = 1; i < arguments.length(); i++) {
|
|
|
|
QString a = arguments.at(i);
|
2015-10-12 13:47:51 +00:00
|
|
|
if (a.isEmpty())
|
|
|
|
continue;
|
2013-10-08 09:48:46 +00:00
|
|
|
if (a.at(0) == '-') {
|
2018-02-25 12:51:41 +00:00
|
|
|
parse_argument(qPrintable(a));
|
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
|
2016-02-24 14:37:33 +00:00
|
|
|
/*
|
|
|
|
* Initialize the random number generator - not really secure as
|
|
|
|
* this is based only on current time, but it should not matter
|
|
|
|
* that much in our context. Moreover this is better than
|
|
|
|
* the constant numbers we used to get before.
|
|
|
|
*/
|
|
|
|
qsrand(time(NULL));
|
2014-03-19 16:23:43 +00:00
|
|
|
setup_system_prefs();
|
2015-10-06 09:15:38 +00:00
|
|
|
copy_prefs(&default_prefs, &prefs);
|
2014-03-19 16:23:43 +00:00
|
|
|
fill_profile_color();
|
2017-04-27 18:24:14 +00:00
|
|
|
fill_computer_list();
|
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) {
|
|
|
|
QString defaultFile(prefs.default_filename);
|
|
|
|
if (!defaultFile.isEmpty())
|
|
|
|
files.push_back(QString(prefs.default_filename));
|
|
|
|
} else if (prefs.default_file_behavior == CLOUD_DEFAULT_FILE) {
|
|
|
|
QString cloudURL;
|
|
|
|
if (getCloudURL(cloudURL) == 0)
|
|
|
|
files.push_back(cloudURL);
|
|
|
|
}
|
2014-03-20 12:56:04 +00:00
|
|
|
}
|
2014-03-11 21:54:28 +00:00
|
|
|
MainWindow *m = MainWindow::instance();
|
2015-11-05 18:05:22 +00:00
|
|
|
filesOnCommandLine = !files.isEmpty() || !importedFiles.isEmpty();
|
2017-10-26 11:43:00 +00:00
|
|
|
if (verbose && !files.isEmpty())
|
|
|
|
qDebug() << "loading dive data from" << files;
|
2014-03-11 21:54:28 +00:00
|
|
|
m->loadFiles(files);
|
2017-10-26 11:43:00 +00:00
|
|
|
if (verbose && !importedFiles.isEmpty())
|
|
|
|
qDebug() << "importing dive data from" << importedFiles;
|
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();
|
2014-12-18 07:47:55 +00:00
|
|
|
taglist_free(g_tag_list);
|
2014-03-19 16:23:43 +00:00
|
|
|
parse_xml_exit();
|
2015-10-06 10:10:21 +00:00
|
|
|
free((void *)default_directory);
|
|
|
|
free((void *)default_filename);
|
2014-03-25 14:55:56 +00:00
|
|
|
subsurface_console_exit();
|
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
|
|
|
|
|
|
|
bool haveFilesOnCommandLine()
|
|
|
|
{
|
|
|
|
return filesOnCommandLine;
|
|
|
|
}
|
2017-10-29 16:04:32 +00:00
|
|
|
|
2017-10-29 19:11:05 +00:00
|
|
|
#define VALIDATE_GL_PREFIX "validateGL(): "
|
|
|
|
|
2017-10-29 16:04:32 +00:00
|
|
|
void validateGL()
|
|
|
|
{
|
2017-10-30 21:03:59 +00:00
|
|
|
QString quickBackend = qgetenv("QT_QUICK_BACKEND");
|
|
|
|
/* 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
|
|
|
|
*/
|
|
|
|
if (!quickBackend.isEmpty()) {
|
|
|
|
if (verbose) {
|
|
|
|
qDebug() << QStringLiteral(VALIDATE_GL_PREFIX "'QT_QUICK_BACKEND' is set to '%1'. "
|
2018-02-25 12:51:41 +00:00
|
|
|
"Skipping validation.").arg(quickBackend);
|
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)
|
2018-02-25 12:51:41 +00:00
|
|
|
qDebug() << QStringLiteral(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)
|
2018-02-25 12:51:41 +00:00
|
|
|
qDebug() << QStringLiteral(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) {
|
|
|
|
qWarning() << QStringLiteral(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"
|
2018-02-25 12:51:41 +00:00
|
|
|
"before running Subsurface!\n");
|
2017-11-01 12:31:04 +00:00
|
|
|
return;
|
|
|
|
}
|
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)
|
2018-02-25 12:51:41 +00:00
|
|
|
qDebug() << QStringLiteral(VALIDATE_GL_PREFIX "detected OpenGL version %1.%2.").arg(verMajor).arg(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) {
|
|
|
|
#if QT_VERSION < QT_VERSION_CHECK(5, 8, 0)
|
2017-10-29 19:11:05 +00:00
|
|
|
qWarning() << QStringLiteral(VALIDATE_GL_PREFIX "ERROR: %1.\n"
|
2017-10-29 16:04:32 +00:00
|
|
|
"Cannot automatically fallback to a software renderer!\n"
|
|
|
|
"Set the environment variable 'QT_QUICK_BACKEND' with the value of 'software'\n"
|
2018-02-25 12:51:41 +00:00
|
|
|
"before running Subsurface!\n").arg(glError);
|
2017-10-29 16:04:32 +00:00
|
|
|
exit(0);
|
|
|
|
#else
|
2018-02-25 12:51:41 +00:00
|
|
|
qWarning() << QStringLiteral(VALIDATE_GL_PREFIX "WARNING: %1. Using a software renderer!").arg(glError);
|
2017-10-29 16:04:32 +00:00
|
|
|
QQuickWindow::setSceneGraphBackend(QSGRendererInterface::Software);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
2017-11-15 20:57:35 +00:00
|
|
|
|
|
|
|
// install this message handler primarily so that the Windows build can log to files
|
|
|
|
void messageHandler(QtMsgType type, const QMessageLogContext &ctx, const QString &msg)
|
|
|
|
{
|
|
|
|
Q_UNUSED(ctx);
|
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();
|
|
|
|
}
|
|
|
|
}
|