-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathmain.c
403 lines (363 loc) · 12.4 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
#include "qvk/qvk.h"
#include "pipe/graph.h"
#include "pipe/graph-io.h"
#include "pipe/global.h"
#include "pipe/modules/api.h"
#include "db/thumbnails.h"
#include "core/log.h"
#include "core/signal.h"
#include "core/version.h"
#include "core/tools.h"
#include "gui/gui.h"
#include "gui/render.h"
#include "gui/view.h"
#include "db/db.h"
#include "nk.h"
#define GLFW_INCLUDE_VULKAN
#include <GLFW/glfw3.h>
#include <stdio.h>
#include <time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdlib.h>
#include <math.h>
dt_gui_t vkdt = {0};
int g_fullscreen = 0;
// from a stackoverflow answer. get the monitor that currently covers most of
// the window area.
static GLFWmonitor*
get_current_monitor(GLFWwindow *window)
{
int bestoverlap = 0;
GLFWmonitor *bestmonitor = NULL;
int wx, wy, ww, wh;
glfwGetWindowPos(window, &wx, &wy);
glfwGetFramebufferSize(window, &ww, &wh);
int nmonitors;
GLFWmonitor **monitors = glfwGetMonitors(&nmonitors);
for (int i = 0; i < nmonitors; i++)
{
const GLFWvidmode *mode = glfwGetVideoMode(monitors[i]);
int mx, my;
glfwGetMonitorPos(monitors[i], &mx, &my);
int mw = mode->width;
int mh = mode->height;
int overlap =
MAX(0, MIN(wx + ww, mx + mw) - MAX(wx, mx)) *
MAX(0, MIN(wy + wh, my + mh) - MAX(wy, my));
if (bestoverlap < overlap)
{
bestoverlap = overlap;
bestmonitor = monitors[i];
}
}
return bestmonitor;
}
static inline int
gamepad_changed(
GLFWgamepadstate *last,
GLFWgamepadstate *curr)
{
for(int i=0;i<sizeof(curr->buttons)/sizeof(curr->buttons[0]);i++)
if(curr->buttons[i] != last->buttons[i])
return 1;
for(int i=0;i<sizeof(curr->axes)/sizeof(curr->axes[0]);i++)
if(fabsf(curr->axes[i] - last->axes[i]) > 0.1)
return 1;
return 0;
}
// since in glfw, joysticks can only be polled and have no event interface
// (see this pull request: https://github.com/glfw/glfw/pull/1590)
// we need to look for changes in a busy loop in this dedicated thread.
// once we find activity, we'll step outside the glfwWaitEvents call by
// posting an empty event from here.
// note that the actual joystick callbacks will be handled there, in
// the gui thread. here, we only detect change and raise the flag.
static void*
joystick_active(void *unused)
{
static GLFWgamepadstate last = {0};
GLFWgamepadstate curr;
while(!glfwWindowShouldClose(vkdt.win.window))
{
if(!glfwGetGamepadState(GLFW_JOYSTICK_1, &curr)) break; // no more joystick?
if(gamepad_changed(&last, &curr))
{
last = curr;
vkdt.wstate.busy = 20; // make sure we'll stay awake for a few frames
glfwPostEmptyEvent();
}
struct timespec req = { .tv_sec = 0, .tv_nsec = 16000 }, rem;
nanosleep(&req, &rem);
}
return 0;
}
static void
toggle_fullscreen()
{
GLFWmonitor* monitor = get_current_monitor(vkdt.win.window);
const GLFWvidmode* mode = glfwGetVideoMode(monitor);
if(g_fullscreen)
{
glfwWindowHint(GLFW_DECORATED, GLFW_TRUE);
glfwSetWindowMonitor(vkdt.win.window, 0, mode->width/8, mode->height/8, 3*mode->width/4, 3*mode->height/4, mode->refreshRate);
g_fullscreen = 0;
}
else
{
glfwSetWindowMonitor(vkdt.win.window, monitor, 0, 0, mode->width, mode->height, mode->refreshRate);
g_fullscreen = 1;
}
}
static void
key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
{
dt_view_keyboard(window, key, scancode, action, mods);
if(!vkdt.wstate.grabbed)
nk_glfw3_keyboard_callback(window, key, scancode, action, mods);
if(key == GLFW_KEY_ESCAPE) // TODO: or gamepad equivalent
{
if(vkdt.wstate.popup) vkdt.wstate.popup = 0; // close any popup
}
else if(key == GLFW_KEY_X && action == GLFW_PRESS && mods == GLFW_MOD_CONTROL)
{
glfwSetWindowShouldClose(vkdt.win.window, GLFW_TRUE);
}
else if(key == GLFW_KEY_F11 && action == GLFW_PRESS)
{
toggle_fullscreen();
}
}
static void
mouse_button_callback(GLFWwindow* window, int button, int action, int mods)
{
dt_view_mouse_button(window, button, action, mods);
if(!vkdt.wstate.grabbed)
nk_glfw3_mouse_button_callback(window, button, action, mods);
}
static void
mouse_position_callback(GLFWwindow* window, double x, double y)
{
float xscale, yscale;
dt_gui_content_scale(window, &xscale, &yscale);
dt_view_mouse_position(window, x*xscale, y*yscale);
if(!vkdt.wstate.grabbed)
nk_glfw3_mouse_position_callback(window, x*xscale, y*yscale);
}
static void
window_close_callback(GLFWwindow* window)
{
glfwSetWindowShouldClose(vkdt.win.window, GLFW_TRUE);
}
static void
char_callback(GLFWwindow* window, unsigned int c)
{
if(!vkdt.wstate.grabbed)
nk_glfw3_char_callback(window, c);
}
static void
scroll_callback(GLFWwindow *window, double xoff, double yoff)
{
float xscale, yscale;
dt_gui_content_scale(window, &xscale, &yscale);
xoff *= xscale;
yoff *= yscale;
dt_view_mouse_scrolled(window, xoff, yoff);
if(!vkdt.wstate.grabbed)
nk_glfw3_scroll_callback(window, xoff, yoff);
}
#if VKDT_USE_PENTABLET==1
static void
pentablet_data_callback(double x, double y, double z, double pressure, double pitch, double yaw, double roll)
{
float xscale, yscale;
dt_gui_content_scale(vkdt.win.window, &xscale, &yscale);
x *= xscale; y *= yscale;
dt_view_pentablet_data(x, y, z, pressure, pitch, yaw, roll);
}
static void
pentablet_proximity_callback(int entering)
{
dt_view_pentablet_proximity(entering);
}
static void
pentablet_cursor_callback(unsigned int cursor)
{ }
#endif
static void
dt_gui_print_usage()
{
printf("gui usage: vkdt [options] [file | directory]\n"
" options:\n"
" -d <log layer> switch on log layer\n"
" -D <log layer> mute log layer\n"
" log layers:\n"
" none qvk pipe gui db cli snd perf mem err all\n");
}
int main(int argc, char *argv[])
{
if(argc > 1)
{
if(!strcmp(argv[1], "--version"))
{
printf("vkdt "VKDT_VERSION" (c) 2020--2025 johannes hanika\n");
exit(0);
}
else if(!strcmp(argv[1], "--help"))
{
printf("vkdt "VKDT_VERSION" (c) 2020--2025 johannes hanika\n");
dt_tool_print_usage();
dt_gui_print_usage();
exit(0);
}
dt_tool_dispatch(argc, argv);
}
// init global things, log and pipeline:
dt_log_init(s_log_err|s_log_gui);
int lastarg = dt_log_init_arg(argc, argv);
dt_log(s_log_gui, "vkdt "VKDT_VERSION" (c) 2020--2025 johannes hanika");
dt_pipe_global_init();
threads_global_init();
dt_set_signal_handlers();
if(dt_gui_init())
{
dt_log(s_log_gui|s_log_err, "failed to init gui/swapchain");
exit(1);
}
// start un-fullscreen on current monitor, we only know which one is that after
// we created the window in dt_gui_init(). maybe should be a config option:
g_fullscreen = 1;
toggle_fullscreen();
dt_gui_recreate_swapchain(&vkdt.win);
nk_glfw3_resize(vkdt.win.window, vkdt.win.width, vkdt.win.height);
dt_gui_init_fonts();
glfwSetKeyCallback(vkdt.win.window, key_callback);
glfwSetMouseButtonCallback(vkdt.win.window, mouse_button_callback);
glfwSetCursorPosCallback(vkdt.win.window, mouse_position_callback);
glfwSetCharCallback(vkdt.win.window, char_callback);
glfwSetScrollCallback(vkdt.win.window, scroll_callback);
glfwSetWindowCloseCallback(vkdt.win.window, window_close_callback);
#if VKDT_USE_PENTABLET==1
glfwSetPenTabletDataCallback(pentablet_data_callback);
glfwSetPenTabletCursorCallback(pentablet_cursor_callback);
glfwSetPenTabletProximityCallback(pentablet_proximity_callback);
#endif
vkdt.view_mode = s_view_cnt;
// this is done so we can mess with the thumbnails struct in
// an asynchronous manner in a background thread. it will know
// it is not serving thumbnails but it only writes bc1 to disk.
// also we have a temporary thumbnails struct and background threads
// to create thumbnails, if necessary.
// only width/height will matter here
dt_thumbnails_init(&vkdt.thumbnail_gen, 400, 400, 0, 0);
dt_thumbnails_init(&vkdt.thumbnails, 400, 400, 3000, 1ul<<30);
dt_db_init(&vkdt.db);
char *filename = 0;
{
char defpath[1024];
const char *mru = dt_rc_get(&vkdt.rc, "gui/ruc_entry00", "null");
if(strcmp(mru, "null"))
{
snprintf(defpath, sizeof(defpath), "%s", mru);
for(int i=0;i<sizeof(defpath) && defpath[i];i++) if(defpath[i] == '&') { defpath[i] = 0; break; }
}
else fs_picturesdir(defpath, sizeof(defpath));
if(argc > lastarg+1) filename = fs_realpath(argv[lastarg+1], 0);
else filename = fs_realpath(defpath, 0);
}
if(!filename || fs_isdir_file(filename))
{
vkdt.view_mode = s_view_lighttable;
dt_db_load_directory(&vkdt.db, &vkdt.thumbnails, filename);
dt_view_switch(s_view_lighttable);
dt_thumbnails_cache_collection(&vkdt.thumbnail_gen, &vkdt.db, &glfwPostEmptyEvent);
}
else
{
if(dt_db_load_image(&vkdt.db, &vkdt.thumbnails, filename))
{
dt_log(s_log_err, "image `%s' could not be loaded!", filename);
dt_gui_notification("image `%s' could not be loaded!", filename);
dt_view_switch(s_view_files);
}
else
{
dt_db_selection_add(&vkdt.db, 0);
dt_view_switch(s_view_darkroom);
}
}
dt_gui_read_tags();
// joystick
pthread_t joystick_thread;
const int joystick_present = vkdt.wstate.have_joystick;
if(joystick_present) pthread_create(&joystick_thread, 0, joystick_active, 0);
// main loop
double beg_rf = dt_time();
const int frame_limiter = dt_rc_get_int(&vkdt.rc, "gui/frame_limiter", 6); // default: cap ui at 160fps
vkdt.wstate.busy = 3;
vkdt.graph_dev.frame = vkdt.state.anim_frame = 0;
GLFWgamepadstate gamepad_last = {0};
while(!glfwWindowShouldClose(vkdt.win.window))
{
// block and wait for one event instead of polling all the time to save on
// gpu workload. might need an interrupt for "render finished" etc. we might
// do that via glfwPostEmptyEvent()
if(vkdt.wstate.busy > 0) vkdt.wstate.busy--;
// vkdt.wstate.busy = 100; // do these two lines instead if profiling in nvidia gfx insight or so.
// vkdt.graph_dev.runflags = s_graph_run_record_cmd_buf;
if(vkdt.state.anim_playing) // should redraw because animation is playing?
vkdt.wstate.busy = vkdt.state.anim_max_frame == -1 ? 3 : vkdt.state.anim_max_frame - vkdt.state.anim_frame + 1;
if(vkdt.wstate.busy > 0) glfwPostEmptyEvent();
else vkdt.wstate.busy = 3;
if(frame_limiter || (dt_log_global.mask & s_log_perf))
{ // artificially limit frames rate to frame_limiter milliseconds/frame as minimum.
double end_rf = dt_time();
if(frame_limiter && end_rf - beg_rf < frame_limiter / 1000.0)
{
usleep(frame_limiter * 1000);
continue;
}
// dt_log(s_log_perf, "fps %.2g", 1.0/(end_rf - beg_rf));
beg_rf = end_rf;
}
// collect input from windows for our contexts.
// enable mouse grab only on wayland (is buggy on x11 and windows for wacom, see https://github.com/hanatos/vkdt/issues/144)
nk_glfw3_input_begin(&vkdt.ctx, vkdt.win.window, vkdt.session_type == 1);
if(vkdt.win1.window)
nk_glfw3_input_begin(&vkdt.ctx1, vkdt.win1.window, vkdt.session_type == 1);
glfwWaitEvents();
nk_glfw3_input_end(&vkdt.ctx, vkdt.win.window, vkdt.session_type == 1);
if(vkdt.win1.window)
nk_glfw3_input_end(&vkdt.ctx1, vkdt.win1.window, vkdt.session_type == 1);
if(vkdt.wstate.have_joystick)
{
GLFWgamepadstate gamepad_curr;
if (!glfwGetGamepadState(GLFW_JOYSTICK_1, &gamepad_curr)) vkdt.wstate.have_joystick = 0;
else if(gamepad_changed(&gamepad_last, &gamepad_curr))
{
dt_view_gamepad(vkdt.win.window, &gamepad_last, &gamepad_curr);
gamepad_last = gamepad_curr;
}
}
dt_view_process(); // process before render/preset because this might swap the output image backbuffers
if(vkdt.graph_dev.gui_msg && vkdt.graph_dev.gui_msg[0]) dt_gui_notification(vkdt.graph_dev.gui_msg);
if(dt_gui_render() == VK_SUCCESS)
dt_gui_present();
}
if(joystick_present) pthread_join(joystick_thread, 0);
for(int q=0;q<s_queue_cnt;q++)
if(qvk.qid[q] == q)
QVKL(&qvk.queue[qvk.qid[q]].mutex, vkQueueWaitIdle(qvk.queue[qvk.qid[q]].queue));
// leave whatever view we're still in:
dt_view_switch(s_view_cnt);
threads_shutdown();
threads_global_cleanup(); // join worker threads before killing their resources
dt_thumbnails_cleanup(&vkdt.thumbnails);
dt_thumbnails_cleanup(&vkdt.thumbnail_gen);
dt_gui_cleanup();
dt_db_cleanup(&vkdt.db);
dt_pipe_global_cleanup();
free(filename);
exit(0);
}