]> git.sesse.net Git - nageru/blob - main.cpp
Implement basic support for CEF.
[nageru] / main.cpp
1 extern "C" {
2 #include <libavformat/avformat.h>
3 }
4 #include <stdbool.h>
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <sys/mman.h>
8 #include <epoxy/gl.h>  // IWYU pragma: keep
9 #include <QApplication>
10 #include <QCoreApplication>
11 #include <QGL>
12 #include <QSize>
13 #include <QSurfaceFormat>
14 #include <string>
15
16 #include <cef_app.h>
17 #include <cef_browser.h>
18 #include <cef_client.h>
19 #include <cef_version.h>
20
21 #include "basic_stats.h"
22 #include "nageru_cef_app.h"
23 #include "context.h"
24 #include "flags.h"
25 #include "image_input.h"
26 #include "mainwindow.h"
27 #include "mixer.h"
28
29 CefRefPtr<NageruCefApp> cef_app;
30
31 int main(int argc, char *argv[])
32 {
33 #ifdef HAVE_CEF
34         // Let CEF have first priority on parsing the command line, because we might be
35         // launched as a CEF sub-process.
36         CefMainArgs main_args(argc, argv);
37         cef_app = CefRefPtr<NageruCefApp>(new NageruCefApp());
38         int err = CefExecuteProcess(main_args, cef_app.get(), nullptr);
39         if (err >= 0) {
40                 return err;
41         }
42
43         // CEF wants to use GLib for its main loop, which interferes with Qt's use of it.
44         // The alternative is trying to integrate CEF into Qt's main loop, but that requires
45         // fairly extensive cross-thread communication and that parts of CEF runs on Qt's UI
46         // thread.
47         setenv("QT_NO_GLIB", "1", 0);
48 #endif
49
50         parse_flags(PROGRAM_NAGERU, argc, argv);
51
52         if (global_flags.va_display.empty() ||
53             global_flags.va_display[0] != '/') {
54                 // We normally use EGL for zerocopy, but if we use VA against DRM
55                 // instead of against X11, we turn it off, and then don't need EGL.
56                 setenv("QT_XCB_GL_INTEGRATION", "xcb_egl", 0);
57                 using_egl = true;
58         }
59         setlinebuf(stdout);
60         av_register_all();
61
62         QCoreApplication::setAttribute(Qt::AA_ShareOpenGLContexts, true);
63
64         QSurfaceFormat fmt;
65         fmt.setDepthBufferSize(0);
66         fmt.setStencilBufferSize(0);
67         fmt.setProfile(QSurfaceFormat::CoreProfile);
68         fmt.setMajorVersion(3);
69         fmt.setMinorVersion(1);
70
71         // Turn off vsync, since Qt generally gives us at most frame rate
72         // (display frequency) / (number of QGLWidgets active).
73         fmt.setSwapInterval(0);
74
75         QSurfaceFormat::setDefaultFormat(fmt);
76
77         QGLFormat::setDefaultFormat(QGLFormat::fromSurfaceFormat(fmt));
78
79         QApplication app(argc, argv);
80         global_share_widget = new QGLWidget();
81         if (!global_share_widget->isValid()) {
82                 fprintf(stderr, "Failed to initialize OpenGL. Nageru needs at least OpenGL 3.1 to function properly.\n");
83                 exit(1);
84         }
85
86         MainWindow mainWindow;
87         mainWindow.resize(QSize(1500, 850));
88         mainWindow.show();
89
90         app.installEventFilter(&mainWindow);  // For white balance color picking.
91
92         // Even on an otherwise unloaded system, it would seem writing the recording
93         // to disk (potentially terabytes of data as time goes by) causes Nageru
94         // to be pushed out of RAM. If we have the right privileges, simply lock us
95         // into memory for better realtime behavior.
96         if (mlockall(MCL_CURRENT | MCL_FUTURE) == -1) {
97                 perror("mlockall()");
98                 fprintf(stderr, "Failed to lock Nageru into RAM. You probably want to\n");
99                 fprintf(stderr, "increase \"memlock\" for your user in limits.conf\n");
100                 fprintf(stderr, "for better realtime behavior.\n");
101                 uses_mlock = false;
102         } else {
103                 uses_mlock = true;
104         }
105
106         int rc = app.exec();
107         global_mixer->quit();
108         mainWindow.mixer_shutting_down();
109         delete global_mixer;
110         ImageInput::shutdown_updaters();
111         return rc;
112 }