| 1 |
// lv-tool - Libvisual commandline tool |
| 2 |
// |
| 3 |
// Copyright (C) 2012-2023 Libvisual team |
| 4 |
// 2004-2006 Dennis Smit |
| 5 |
// |
| 6 |
// Authors: Daniel Hiepler <[email protected]> |
| 7 |
// Chong Kai Xiong <[email protected]> |
| 8 |
// Dennis Smit <[email protected]> |
| 9 |
// Scott Sibley <[email protected]> |
| 10 |
// Sebastian Pipping <[email protected]> |
| 11 |
// |
| 12 |
// This file is part of lv-tool. |
| 13 |
// |
| 14 |
// lv-tool is free software: you can redistribute it and/or modify |
| 15 |
// it under the terms of the GNU General Public License as published by |
| 16 |
// the Free Software Foundation, either version 2 of the License, or |
| 17 |
// (at your option) any later version. |
| 18 |
// |
| 19 |
// lv-tool is distributed in the hope that it will be useful, |
| 20 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 21 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 22 |
// GNU General Public License for more details. |
| 23 |
// |
| 24 |
// You should have received a copy of the GNU General Public License |
| 25 |
// along with lv-tool. If not, see <http://www.gnu.org/licenses/>. |
| 26 |
|
| 27 |
#include "config.h" |
| 28 |
#include "gettext.h" |
| 29 |
#include <libvisual/libvisual.h> |
| 30 |
#include <SDL.h> |
| 31 |
#include <algorithm> |
| 32 |
#include <array> |
| 33 |
#include <stdexcept> |
| 34 |
#include <iostream> |
| 35 |
#include <unordered_set> |
| 36 |
#include <string> |
| 37 |
#include <cstdio> |
| 38 |
#include <cstdlib> |
| 39 |
#include <csignal> |
| 40 |
#include <getopt.h> |
| 41 |
|
| 42 |
// Defaults |
| 43 |
#define DEFAULT_ACTOR_GL "lv_gltest" |
| 44 |
#define DEFAULT_ACTOR_NONGL "lv_scope" |
| 45 |
#define DEFAULT_INPUT "debug" |
| 46 |
#define DEFAULT_WIDTH 640 |
| 47 |
#define DEFAULT_HEIGHT 480 |
| 48 |
#define DEFAULT_FPS 60 |
| 49 |
#define DEFAULT_COLOR_DEPTH 0 |
| 50 |
|
| 51 |
// A thin C++ compatibility layer to reduce the diff to the master branch |
| 52 |
namespace LV { |
| 53 |
class Bin { |
| 54 |
VisBin * m_bin; |
| 55 |
|
| 56 |
public: |
| 57 |
Bin() : m_bin(visual_bin_new()) { |
| 58 |
visual_log_return_if_fail( m_bin != nullptr ); |
| 59 |
} |
| 60 |
|
| 61 |
bool actor_available(std::string const& actor_name) { |
| 62 |
return visual_actor_valid_by_name(actor_name.c_str()); |
| 63 |
} |
| 64 |
|
| 65 |
bool connect (std::string const& actor_name, std::string const& input_name) { |
| 66 |
if (! visual_input_valid_by_name(input_name.c_str())) |
| 67 |
return false; |
| 68 |
|
| 69 |
if (! actor_available(actor_name)) |
| 70 |
return false; |
| 71 |
|
| 72 |
return visual_bin_connect_by_names( |
| 73 |
m_bin, |
| 74 |
const_cast<char *>(actor_name.c_str()), |
| 75 |
const_cast<char *>(input_name.c_str())) >= 0; |
| 76 |
} |
| 77 |
|
| 78 |
VisActor * get_actor () const { |
| 79 |
return visual_bin_get_actor(m_bin); |
| 80 |
} |
| 81 |
|
| 82 |
void set_supported_depth(VisVideoDepth depthflag) { |
| 83 |
visual_bin_set_supported_depth(m_bin, depthflag); |
| 84 |
}; |
| 85 |
|
| 86 |
int get_depth () const { |
| 87 |
return visual_bin_get_depth(m_bin); |
| 88 |
} |
| 89 |
|
| 90 |
void set_depth (VisVideoDepth depth) { |
| 91 |
visual_bin_set_depth(m_bin, depth); |
| 92 |
} |
| 93 |
|
| 94 |
void set_video(VisVideo * video) { |
| 95 |
visual_bin_set_video(m_bin, video); |
| 96 |
} |
| 97 |
|
| 98 |
void realize() { |
| 99 |
visual_bin_realize(m_bin); |
| 100 |
} |
| 101 |
|
| 102 |
void sync(bool noevent) { |
| 103 |
visual_bin_sync(m_bin, noevent); |
| 104 |
} |
| 105 |
|
| 106 |
bool depth_changed() { |
| 107 |
return visual_bin_depth_changed(m_bin) == TRUE; |
| 108 |
} |
| 109 |
|
| 110 |
void run() { |
| 111 |
visual_bin_run(m_bin); |
| 112 |
}; |
| 113 |
|
| 114 |
void switch_actor (std::string const& actname) { |
| 115 |
visual_bin_switch_actor_by_name(m_bin, const_cast<char*>(actname.c_str())); |
| 116 |
} |
| 117 |
}; |
| 118 |
|
| 119 |
class Time { |
| 120 |
Uint32 m_moment; |
| 121 |
|
| 122 |
Time(Uint32 moment) : m_moment(moment) { |
| 123 |
} |
| 124 |
|
| 125 |
public: |
| 126 |
Time() : m_moment(0) { |
| 127 |
} |
| 128 |
|
| 129 |
static Time now() { |
| 130 |
return Time(SDL_GetTicks()); |
| 131 |
} |
| 132 |
|
| 133 |
friend Time operator-(Time const& lhs, Time const& rhs) { |
| 134 |
return Time(lhs.m_moment - rhs.m_moment); |
| 135 |
} |
| 136 |
|
| 137 |
friend bool operator>=(Time const& lhs, Time const& rhs) { |
| 138 |
return lhs.m_moment >= rhs.m_moment; |
| 139 |
} |
| 140 |
|
| 141 |
//! Converts the time to seconds |
| 142 |
double to_secs() const |
| 143 |
{ |
| 144 |
return this->m_moment / 1000.0; |
| 145 |
} |
| 146 |
|
| 147 |
//! Converts the time to microseconds |
| 148 |
uint64_t to_usecs() const { |
| 149 |
return this->m_moment * 1000; |
| 150 |
}; |
| 151 |
}; |
| 152 |
} |
| 153 |
|
| 154 |
namespace { |
| 155 |
|
| 156 |
const std::unordered_set<std::string> actors_to_skip { |
| 157 |
"gdkpixbuf", |
| 158 |
"gstreamer", |
| 159 |
"lv_analyzer" |
| 160 |
}; |
| 161 |
|
| 162 |
std::string actor_name; |
| 163 |
std::string input_name = DEFAULT_INPUT; |
| 164 |
std::string exclude_actors; |
| 165 |
|
| 166 |
unsigned int width = DEFAULT_WIDTH; |
| 167 |
unsigned int height = DEFAULT_HEIGHT; |
| 168 |
unsigned int color_depth = DEFAULT_COLOR_DEPTH; |
| 169 |
|
| 170 |
unsigned int frame_rate = DEFAULT_FPS; |
| 171 |
unsigned int frame_count = 0; |
| 172 |
unsigned int actor_switch_after_frames = 0; |
| 173 |
unsigned int actor_switch_framecount = 0; |
| 174 |
|
| 175 |
bool have_seed = 0; |
| 176 |
uint32_t seed = 0; |
| 177 |
|
| 178 |
volatile std::sig_atomic_t terminate_process = false; |
| 179 |
|
| 180 |
enum class CycleDir |
| 181 |
{ |
| 182 |
PREV, |
| 183 |
NEXT |
| 184 |
}; |
| 185 |
|
| 186 |
/** Class to manage LV's lifecycle */ |
| 187 |
class Libvisual |
| 188 |
{ |
| 189 |
public: |
| 190 |
|
| 191 |
Libvisual (int& argc, char**& argv) |
| 192 |
{ |
| 193 |
visual_init (&argc, &argv); |
| 194 |
} |
| 195 |
|
| 196 |
~Libvisual () |
| 197 |
{ |
| 198 |
visual_quit (); |
| 199 |
} |
| 200 |
}; |
| 201 |
|
| 202 |
class Display |
| 203 |
{ |
| 204 |
SDL_Surface * m_screen; |
| 205 |
VisVideoDepth m_requested_depth; |
| 206 |
VisVideo * m_screen_video; |
| 207 |
bool m_resizable; |
| 208 |
unsigned int m_last_width; |
| 209 |
unsigned int m_last_height; |
| 210 |
|
| 211 |
void destroy() { |
| 212 |
if (m_screen_video) { |
| 213 |
visual_object_unref(VISUAL_OBJECT(m_screen_video)); |
| 214 |
m_screen_video = nullptr; |
| 215 |
} |
| 216 |
if (m_screen) { |
| 217 |
// NOTE: The surface returned by SDL_SetVideoMode is either |
| 218 |
// freed by the next call to SDL_SetVideoMode or |
| 219 |
// when calling SDL_QUIT. So nothing to free here. |
| 220 |
// https://www.libsdl.org/release/SDL-1.2.15/docs/html/sdlsetvideomode.html |
| 221 |
} |
| 222 |
} |
| 223 |
|
| 224 |
public: |
| 225 |
Display() : m_screen(nullptr), m_requested_depth(VISUAL_VIDEO_DEPTH_NONE), m_screen_video(nullptr), |
| 226 |
m_resizable(false), m_last_width(0), m_last_height(0) { |
| 227 |
} |
| 228 |
|
| 229 |
~Display() { |
| 230 |
destroy(); |
| 231 |
|
| 232 |
if (SDL_WasInit (SDL_INIT_VIDEO)) { |
| 233 |
SDL_Quit(); |
| 234 |
} |
| 235 |
} |
| 236 |
|
| 237 |
VisVideo * create (VisVideoDepth depth, |
| 238 |
VisVideoAttributeOptions const* vidoptions, |
| 239 |
unsigned int width, |
| 240 |
unsigned int height, |
| 241 |
bool resizable) { |
| 242 |
int videoflags = 0; |
| 243 |
|
| 244 |
if (resizable) |
| 245 |
videoflags |= SDL_RESIZABLE; |
| 246 |
|
| 247 |
if (!SDL_WasInit (SDL_INIT_VIDEO)) { |
| 248 |
if (SDL_Init (SDL_INIT_VIDEO) == -1) { |
| 249 |
visual_log (VISUAL_LOG_ERROR, "Unable to init SDL VIDEO: %s", SDL_GetError ()); |
| 250 |
return nullptr; |
| 251 |
} |
| 252 |
} |
| 253 |
|
| 254 |
m_resizable = resizable; |
| 255 |
m_requested_depth = depth; |
| 256 |
|
| 257 |
destroy(); |
| 258 |
|
| 259 |
int bpp; |
| 260 |
|
| 261 |
if (depth == VISUAL_VIDEO_DEPTH_GL) { |
| 262 |
SDL_VideoInfo const* videoinfo = SDL_GetVideoInfo (); |
| 263 |
|
| 264 |
if (!videoinfo) { |
| 265 |
return nullptr; |
| 266 |
} |
| 267 |
|
| 268 |
videoflags |= SDL_OPENGL; |
| 269 |
|
| 270 |
if (videoinfo->hw_available) |
| 271 |
videoflags |= SDL_HWSURFACE; |
| 272 |
|
| 273 |
// TODO apply vidoptions here |
| 274 |
|
| 275 |
bpp = videoinfo->vfmt->BitsPerPixel; |
| 276 |
|
| 277 |
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); |
| 278 |
} |
| 279 |
else |
| 280 |
{ |
| 281 |
bpp = 8 * visual_video_bpp_from_depth (depth); |
| 282 |
} |
| 283 |
|
| 284 |
// Create surface |
| 285 |
m_screen = SDL_SetVideoMode (width, height, bpp, videoflags); |
| 286 |
visual_log_return_val_if_fail( m_screen != nullptr, nullptr ); |
| 287 |
|
| 288 |
// Recreate video object |
| 289 |
m_screen_video = visual_video_new(); |
| 290 |
visual_log_return_val_if_fail( m_screen_video != nullptr, nullptr ); |
| 291 |
visual_video_set_attributes(m_screen_video, m_screen->w, m_screen->h, m_screen->pitch, depth); |
| 292 |
visual_video_allocate_buffer(m_screen_video); |
| 293 |
|
| 294 |
SDL_EnableKeyRepeat (SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL); |
| 295 |
|
| 296 |
return m_screen_video; |
| 297 |
} |
| 298 |
|
| 299 |
void drain_events(VisEventQueue & eventqueue, VisVideo * video) { |
| 300 |
SDL_Event event; |
| 301 |
while (SDL_PollEvent (&event)) { |
| 302 |
switch (event.type) { |
| 303 |
case SDL_KEYUP: |
| 304 |
visual_event_queue_add_keyboard (&eventqueue, |
| 305 |
VisKey(event.key.keysym.sym), |
| 306 |
VisKeyMod(event.key.keysym.mod), |
| 307 |
VISUAL_KEY_UP); |
| 308 |
break; |
| 309 |
|
| 310 |
case SDL_KEYDOWN: |
| 311 |
visual_event_queue_add_keyboard (&eventqueue, VisKey(event.key.keysym.sym), |
| 312 |
VisKeyMod(event.key.keysym.mod), |
| 313 |
VISUAL_KEY_DOWN); |
| 314 |
break; |
| 315 |
|
| 316 |
case SDL_VIDEORESIZE: |
| 317 |
visual_event_queue_add_resize (&eventqueue, video, event.resize.w, event.resize.h); |
| 318 |
break; |
| 319 |
|
| 320 |
case SDL_MOUSEMOTION: |
| 321 |
visual_event_queue_add_mousemotion (&eventqueue, event.motion.x, |
| 322 |
event.motion.y); |
| 323 |
break; |
| 324 |
|
| 325 |
case SDL_MOUSEBUTTONDOWN: |
| 326 |
visual_event_queue_add_mousebutton (&eventqueue, event.button.button, |
| 327 |
VISUAL_MOUSE_DOWN, |
| 328 |
event.button.x, |
| 329 |
event.button.y); |
| 330 |
break; |
| 331 |
|
| 332 |
case SDL_MOUSEBUTTONUP: |
| 333 |
visual_event_queue_add_mousebutton (&eventqueue, event.button.button, |
| 334 |
VISUAL_MOUSE_UP, |
| 335 |
event.button.x, |
| 336 |
event.button.y); |
| 337 |
break; |
| 338 |
|
| 339 |
case SDL_QUIT: |
| 340 |
visual_event_queue_add_quit (&eventqueue, 0); |
| 341 |
break; |
| 342 |
|
| 343 |
default: |
| 344 |
break; |
| 345 |
} |
| 346 |
} |
| 347 |
} |
| 348 |
|
| 349 |
VisVideo * get_video() const { |
| 350 |
visual_log_return_val_if_fail( m_screen_video != nullptr, nullptr ); |
| 351 |
return m_screen_video; |
| 352 |
} |
| 353 |
|
| 354 |
static void set_title(std::string const& title) { |
| 355 |
SDL_WM_SetCaption (title.c_str(), nullptr); |
| 356 |
} |
| 357 |
|
| 358 |
bool is_fullscreen () const { |
| 359 |
return m_screen->flags & SDL_FULLSCREEN; |
| 360 |
} |
| 361 |
|
| 362 |
void set_fullscreen (bool fullscreen, bool autoscale) { |
| 363 |
if (fullscreen) { |
| 364 |
if (!is_fullscreen()) { |
| 365 |
if (autoscale) { |
| 366 |
unsigned int width = m_screen->w; |
| 367 |
unsigned int height = m_screen->h; |
| 368 |
|
| 369 |
m_last_width = width; |
| 370 |
m_last_height = height; |
| 371 |
get_nearest_resolution (width, height); |
| 372 |
|
| 373 |
// TODO non-NULL vidoptions here? |
| 374 |
create (m_requested_depth, nullptr, width, height, m_resizable); |
| 375 |
} |
| 376 |
|
| 377 |
SDL_ShowCursor (SDL_FALSE); |
| 378 |
SDL_WM_ToggleFullScreen (m_screen); |
| 379 |
} |
| 380 |
} else { |
| 381 |
if (is_fullscreen()) { |
| 382 |
SDL_ShowCursor (SDL_TRUE); |
| 383 |
SDL_WM_ToggleFullScreen (m_screen); |
| 384 |
|
| 385 |
if (autoscale) { |
| 386 |
// TODO non-NULL vidoptions here? |
| 387 |
create (m_requested_depth, nullptr, m_last_width, m_last_height, m_resizable); |
| 388 |
} |
| 389 |
} |
| 390 |
} |
| 391 |
} |
| 392 |
|
| 393 |
void get_nearest_resolution (unsigned int& width, unsigned int& height) { |
| 394 |
auto modelist = SDL_ListModes (nullptr, SDL_FULLSCREEN); |
| 395 |
if (!modelist) |
| 396 |
return; |
| 397 |
|
| 398 |
// Window is bigger than highest resolution |
| 399 |
if (modelist[0]->w <= width || modelist[0]->h <= height) { |
| 400 |
width = modelist[0]->w; |
| 401 |
height = modelist[0]->h; |
| 402 |
return; |
| 403 |
} |
| 404 |
|
| 405 |
for (unsigned int i = 0; modelist[i]; i++) { |
| 406 |
if (modelist[i]->w >= width && modelist[i]->h >= height) { |
| 407 |
width = modelist[i]->w; |
| 408 |
height = modelist[i]->h; |
| 409 |
return; |
| 410 |
} |
| 411 |
} |
| 412 |
} |
| 413 |
|
| 414 |
void update_all() { |
| 415 |
visual_log_return_if_fail( m_screen != nullptr ); |
| 416 |
|
| 417 |
static VisPalette * prev_pal = nullptr; |
| 418 |
if (m_screen->format->BitsPerPixel == 8) { |
| 419 |
visual_log_return_if_fail( m_screen_video != nullptr ); |
| 420 |
VisPalette * const pal = m_screen_video->pal; |
| 421 |
|
| 422 |
if (pal != prev_pal) { |
| 423 |
std::array<SDL_Color, 256> colors {}; |
| 424 |
|
| 425 |
if (pal) { |
| 426 |
const std::size_t source_color_count = std::min(colors.size(), |
| 427 |
static_cast<visual_size_t>(pal->ncolors)); |
| 428 |
for (std::size_t i = 0; i < source_color_count; i++) { |
| 429 |
colors[i].r = pal->colors[i].r; |
| 430 |
colors[i].g = pal->colors[i].g; |
| 431 |
colors[i].b = pal->colors[i].b; |
| 432 |
} |
| 433 |
} |
| 434 |
|
| 435 |
SDL_SetColors (m_screen, colors.data(), 0, colors.size()); |
| 436 |
prev_pal = pal; |
| 437 |
} |
| 438 |
} else { |
| 439 |
prev_pal = nullptr; |
| 440 |
} |
| 441 |
|
| 442 |
if (m_requested_depth == VISUAL_VIDEO_DEPTH_GL) { |
| 443 |
SDL_GL_SwapBuffers(); |
| 444 |
} else { |
| 445 |
visual_mem_copy(m_screen->pixels, |
| 446 |
visual_video_get_pixels(m_screen_video), |
| 447 |
visual_video_get_size(m_screen_video)); |
| 448 |
SDL_Flip(m_screen); |
| 449 |
} |
| 450 |
} |
| 451 |
|
| 452 |
void lock () { |
| 453 |
visual_log_return_if_fail( m_screen != nullptr ); |
| 454 |
if (SDL_MUSTLOCK (m_screen)) |
| 455 |
SDL_LockSurface (m_screen); |
| 456 |
} |
| 457 |
|
| 458 |
void unlock () { |
| 459 |
visual_log_return_if_fail( m_screen != nullptr ); |
| 460 |
if (SDL_MUSTLOCK (m_screen)) |
| 461 |
SDL_UnlockSurface (m_screen); |
| 462 |
} |
| 463 |
}; |
| 464 |
|
| 465 |
class DisplayLock |
| 466 |
{ |
| 467 |
Display * m_display; |
| 468 |
|
| 469 |
public: |
| 470 |
DisplayLock(Display & display) : m_display(&display) { |
| 471 |
visual_log_return_if_fail( m_display != nullptr ); |
| 472 |
m_display->lock(); |
| 473 |
} |
| 474 |
|
| 475 |
~DisplayLock() { |
| 476 |
m_display->unlock(); |
| 477 |
} |
| 478 |
}; |
| 479 |
|
| 480 |
class ResizeRequest |
| 481 |
{ |
| 482 |
unsigned int m_width; |
| 483 |
unsigned int m_height; |
| 484 |
LV::Time m_received_at; |
| 485 |
LV::Time m_applied_at; |
| 486 |
|
| 487 |
public: |
| 488 |
ResizeRequest() : m_width(0), m_height(0) { |
| 489 |
} |
| 490 |
|
| 491 |
unsigned int width() const { return m_width; } |
| 492 |
unsigned int height() const { return m_height; } |
| 493 |
|
| 494 |
bool is_due() const { |
| 495 |
if (m_applied_at >= m_received_at) { |
| 496 |
return false; |
| 497 |
} |
| 498 |
const double seconds_passed = (LV::Time::now() - m_received_at).to_secs(); |
| 499 |
return seconds_passed >= 0.2; // >=300ms is known to feel slow to humans |
| 500 |
} |
| 501 |
|
| 502 |
void store(unsigned int width, unsigned int height) { |
| 503 |
m_width = width; |
| 504 |
m_height = height; |
| 505 |
m_received_at = LV::Time::now(); |
| 506 |
} |
| 507 |
|
| 508 |
void mark_as_applied() { |
| 509 |
m_applied_at = LV::Time::now(); |
| 510 |
} |
| 511 |
}; |
| 512 |
|
| 513 |
/** print info about libvisual plugin */ |
| 514 |
void print_plugin_info(VisPluginInfo const& info) |
| 515 |
{ |
| 516 |
printf("Plugin: \"%s\" (%s)\n" |
| 517 |
"\tAuthor: %s\n\tVersion: %s\t\n" |
| 518 |
"\t%s - %s\n\n", |
| 519 |
info.name, info.plugname, |
| 520 |
info.author, info.version, |
| 521 |
info.about, info.help); |
| 522 |
} |
| 523 |
|
| 524 |
|
| 525 |
/** print help for plugins */ |
| 526 |
void print_plugin_help() |
| 527 |
{ |
| 528 |
VisList * const all_plugins = visual_plugin_get_registry(); |
| 529 |
|
| 530 |
VisListEntry *le = NULL; |
| 531 |
VisPluginRef *ref = NULL; |
| 532 |
|
| 533 |
printf("===== INPUTS =====\n"); |
| 534 |
VisList * const inputs = visual_plugin_registry_filter(all_plugins, "Libvisual:core:input"); |
| 535 |
ref = static_cast<VisPluginRef*>(visual_list_next (inputs, &le)); |
| 536 |
|
| 537 |
// print inputs |
| 538 |
if(! ref) |
| 539 |
{ |
| 540 |
std::cerr << "No input plugins found\n"; |
| 541 |
} |
| 542 |
else |
| 543 |
{ |
| 544 |
do { |
| 545 |
visual_plugin_load(ref); |
| 546 |
print_plugin_info(*(ref->info)); |
| 547 |
ref = static_cast<VisPluginRef*>(visual_list_next (inputs, &le)); |
| 548 |
} while (ref != nullptr); |
| 549 |
} |
| 550 |
|
| 551 |
printf("===== ACTORS =====\n"); |
| 552 |
VisList * const actors = visual_plugin_registry_filter(all_plugins, "Libvisual:core:actor"); |
| 553 |
ref = static_cast<VisPluginRef*>(visual_list_next (actors, &le)); |
| 554 |
|
| 555 |
// print actors |
| 556 |
if(! ref) |
| 557 |
{ |
| 558 |
std::cerr << "No actor plugins found\n"; |
| 559 |
} |
| 560 |
else |
| 561 |
{ |
| 562 |
do { |
| 563 |
visual_plugin_load(ref); |
| 564 |
print_plugin_info(*(ref->info)); |
| 565 |
ref = static_cast<VisPluginRef*>(visual_list_next (inputs, &le)); |
| 566 |
} while (ref != nullptr); |
| 567 |
} |
| 568 |
} |
| 569 |
|
| 570 |
/** print commandline version */ |
| 571 |
void print_version(std::string const& name) |
| 572 |
{ |
| 573 |
printf("%s %s\n", name.c_str (), PACKAGE_VERSION); |
| 574 |
} |
| 575 |
|
| 576 |
/** print commandline help */ |
| 577 |
void print_help(std::string const& name) |
| 578 |
{ |
| 579 |
printf("Usage: %s [options]\n" |
| 580 |
"\n" |
| 581 |
"Valid options:\n" |
| 582 |
" --help, -h This help text\n" |
| 583 |
" --plugin-help, -p List of installed plugins + information\n" |
| 584 |
" --version, -V Print version and exit\n" |
| 585 |
" --verbose, -v Increase log verbosity (may be used multiple times)\n" |
| 586 |
" --dimensions <wxh>, -D <wxh> Request dimensions from display driver (no guarantee) [%dx%d]\n" |
| 587 |
" --depth <depth>, -c <depth> Set output colour depth (automatic by default)\n" |
| 588 |
" --input <input>, -i <input> Use this input plugin [%s]\n" |
| 589 |
" --actor <actor>, -a <actor> Use this actor plugin [%s/%s]\n" |
| 590 |
" --seed <seed>, -s <seed> Set random seed\n" |
| 591 |
" --fps <n>, -f <n> Limit output to n frames per second (if display driver supports it) [%d]\n" |
| 592 |
" --framecount <n>, -F <n> Output n frames, then exit.\n" |
| 593 |
" --switch <n>, -S <n> Switch actor after n frames.\n" |
| 594 |
" --exclude <actors>, -x <actors> Provide a list of actors to exclude.\n" |
| 595 |
"\n", |
| 596 |
name.c_str (), |
| 597 |
width, height, |
| 598 |
input_name.c_str (), |
| 599 |
DEFAULT_ACTOR_GL, |
| 600 |
DEFAULT_ACTOR_NONGL, |
| 601 |
frame_rate); |
| 602 |
} |
| 603 |
|
| 604 |
void print_welcome() |
| 605 |
{ |
| 606 |
// print warm welcome |
| 607 |
std::cerr << "lv-tool" << " - " |
| 608 |
<< "Libvisual " << PACKAGE_VERSION |
| 609 |
<< " commandline tool - " |
| 610 |
<< PACKAGE_URL << "\n"; |
| 611 |
} |
| 612 |
|
| 613 |
/** |
| 614 |
* parse commandline arguments |
| 615 |
* |
| 616 |
* @param argc from main() |
| 617 |
* @param argv from main() |
| 618 |
* @result 0 upon success, <0 upon failure, >0 if app should exit without error */ |
| 619 |
int parse_args(int argc, char *argv[]) |
| 620 |
{ |
| 621 |
static struct option loptions[] = { |
| 622 |
{"help", no_argument, 0, 'h'}, |
| 623 |
{"plugin-help", no_argument, 0, 'p'}, |
| 624 |
{"version", no_argument, 0, 'V'}, |
| 625 |
{"verbose", no_argument, 0, 'v'}, |
| 626 |
{"dimensions", required_argument, 0, 'D'}, |
| 627 |
{"input", required_argument, 0, 'i'}, |
| 628 |
{"actor", required_argument, 0, 'a'}, |
| 629 |
{"fps", required_argument, 0, 'f'}, |
| 630 |
{"seed", required_argument, 0, 's'}, |
| 631 |
{"exclude", required_argument, 0, 'x'}, |
| 632 |
{"framecount", required_argument, 0, 'F'}, |
| 633 |
{"switch", required_argument, 0, 'S'}, |
| 634 |
{"depth", required_argument, 0, 'c'}, |
| 635 |
{0, 0, 0, 0 } |
| 636 |
}; |
| 637 |
|
| 638 |
int index, argument; |
| 639 |
|
| 640 |
while ((argument = getopt_long(argc, argv, "hpvD:i:a:f:s:F:S:x:c:", loptions, &index)) >= 0) { |
| 641 |
|
| 642 |
switch(argument) { |
| 643 |
// --help |
| 644 |
case 'h': { |
| 645 |
print_welcome(); |
| 646 |
print_help(argv[0]); |
| 647 |
return 1; |
| 648 |
} |
| 649 |
|
| 650 |
// --plugin-help |
| 651 |
case 'p': { |
| 652 |
print_welcome(); |
| 653 |
print_plugin_help(); |
| 654 |
return 1; |
| 655 |
} |
| 656 |
|
| 657 |
// --version |
| 658 |
case 'V': { |
| 659 |
print_version(argv[0]); |
| 660 |
return 1; |
| 661 |
} |
| 662 |
|
| 663 |
// --verbose |
| 664 |
case 'v': { |
| 665 |
VisLogVerboseness level = visual_log_get_verboseness (); |
| 666 |
level = VisLogVerboseness (int (level) + 1); |
| 667 |
if (int (level) > VISUAL_LOG_VERBOSENESS_HIGH) { |
| 668 |
visual_log_set_verboseness (VISUAL_LOG_VERBOSENESS_HIGH); |
| 669 |
} |
| 670 |
else { |
| 671 |
visual_log_set_verboseness (level); |
| 672 |
} |
| 673 |
break; |
| 674 |
} |
| 675 |
|
| 676 |
// --dimensions |
| 677 |
case 'D': { |
| 678 |
if (std::sscanf (optarg, "%ux%u", &width, &height) != 2) |
| 679 |
{ |
| 680 |
std::cerr << "Invalid dimensions: '" << optarg << "'. Use <width>x<height> (e.g. 640x480)\n"; |
| 681 |
return -1; |
| 682 |
} |
| 683 |
break; |
| 684 |
} |
| 685 |
|
| 686 |
// --depth |
| 687 |
case 'c': { |
| 688 |
if (std::sscanf (optarg, "%u", &color_depth) != 1 || |
| 689 |
visual_video_depth_enum_from_value(color_depth) == VISUAL_VIDEO_DEPTH_ERROR) |
| 690 |
{ |
| 691 |
std::cerr << "Invalid depth: '" << optarg << "'. Use integer value (e.g. 24)\n"; |
| 692 |
return -1; |
| 693 |
} |
| 694 |
break; |
| 695 |
} |
| 696 |
|
| 697 |
// --input |
| 698 |
case 'i': { |
| 699 |
// save name for later |
| 700 |
input_name = optarg; |
| 701 |
break; |
| 702 |
} |
| 703 |
|
| 704 |
// --actor |
| 705 |
case 'a': { |
| 706 |
// save name for later |
| 707 |
actor_name = optarg; |
| 708 |
break; |
| 709 |
} |
| 710 |
|
| 711 |
// --fps |
| 712 |
case 'f': { |
| 713 |
// set frame_rate |
| 714 |
std::sscanf(optarg, "%u", &frame_rate); |
| 715 |
break; |
| 716 |
} |
| 717 |
|
| 718 |
// --seed |
| 719 |
case 's': { |
| 720 |
have_seed = true; |
| 721 |
std::sscanf(optarg, "%d", &seed); |
| 722 |
break; |
| 723 |
} |
| 724 |
|
| 725 |
// --framecount |
| 726 |
case 'F': { |
| 727 |
// set framecount |
| 728 |
std::sscanf(optarg, "%u", &frame_count); |
| 729 |
break; |
| 730 |
} |
| 731 |
|
| 732 |
// --switch |
| 733 |
case 'S': { |
| 734 |
// switch actor after n frames |
| 735 |
std::sscanf(optarg, "%u", &actor_switch_after_frames); |
| 736 |
break; |
| 737 |
} |
| 738 |
|
| 739 |
// --exclude |
| 740 |
case 'x': { |
| 741 |
exclude_actors = optarg; |
| 742 |
break; |
| 743 |
} |
| 744 |
|
| 745 |
// invalid argument |
| 746 |
case '?': { |
| 747 |
print_help(argv[0]); |
| 748 |
return -1; |
| 749 |
} |
| 750 |
|
| 751 |
// unhandled arguments |
| 752 |
default: |
| 753 |
std::abort (); |
| 754 |
} |
| 755 |
} |
| 756 |
|
| 757 |
return 0; |
| 758 |
} |
| 759 |
|
| 760 |
void handle_termination_signal (int signal) |
| 761 |
{ |
| 762 |
(void)signal; |
| 763 |
|
| 764 |
terminate_process = true; |
| 765 |
} |
| 766 |
|
| 767 |
/** |
| 768 |
* Handle process termination signals. |
| 769 |
*/ |
| 770 |
void setup_signal_handlers () |
| 771 |
{ |
| 772 |
std::signal (SIGINT, handle_termination_signal); |
| 773 |
std::signal (SIGTERM, handle_termination_signal); |
| 774 |
} |
| 775 |
|
| 776 |
std::string cycle_actor_name (std::string const& name, CycleDir dir) |
| 777 |
{ |
| 778 |
auto cycler = (dir == CycleDir::NEXT) ? visual_actor_get_next_by_name |
| 779 |
: visual_actor_get_prev_by_name; |
| 780 |
|
| 781 |
auto new_name = cycler (name.c_str ()); |
| 782 |
if (!new_name) { |
| 783 |
new_name = cycler (nullptr); |
| 784 |
} |
| 785 |
|
| 786 |
// Always skip actors that are of little interest to end users, |
| 787 |
// while allowing explicit "--actor (gdkpixbuf|gstreamer)". |
| 788 |
const bool found = actors_to_skip.find (new_name) != actors_to_skip.end (); |
| 789 |
if (found) { |
| 790 |
return cycle_actor_name (new_name, dir); |
| 791 |
} |
| 792 |
|
| 793 |
// FIXME: this won't work if an actor's name is used as part of |
| 794 |
// another actor's name |
| 795 |
if (exclude_actors.find (new_name) != std::string::npos) { |
| 796 |
return cycle_actor_name (new_name, dir); |
| 797 |
} |
| 798 |
|
| 799 |
return new_name; |
| 800 |
} |
| 801 |
|
| 802 |
} // anonymous namespace |
| 803 |
|
| 804 |
|
| 805 |
int main (int argc, char **argv) |
| 806 |
{ |
| 807 |
try { |
| 808 |
// setup signal handlers |
| 809 |
setup_signal_handlers (); |
| 810 |
|
| 811 |
// default loglevel |
| 812 |
visual_log_set_verboseness (VISUAL_LOG_VERBOSENESS_LOW); |
| 813 |
|
| 814 |
// initialize LV |
| 815 |
Libvisual main {argc, argv}; |
| 816 |
|
| 817 |
// parse commandline arguments |
| 818 |
int parse_result = parse_args (argc, argv); |
| 819 |
if (parse_result < 0) { |
| 820 |
throw std::runtime_error ("Failed to parse arguments"); |
| 821 |
} |
| 822 |
if (parse_result > 0) { |
| 823 |
return EXIT_SUCCESS; |
| 824 |
} |
| 825 |
|
| 826 |
print_welcome(); |
| 827 |
|
| 828 |
// Set system-wide random seed |
| 829 |
if (have_seed) { |
| 830 |
visual_random_context_set_seed(&__lv_internal_random_context, seed); |
| 831 |
} |
| 832 |
|
| 833 |
// create new VisBin for video output |
| 834 |
LV::Bin bin; |
| 835 |
bin.set_supported_depth(VISUAL_VIDEO_DEPTH_ALL); |
| 836 |
|
| 837 |
// Apply dynamic actor default, as needed |
| 838 |
if (actor_name.empty()) { |
| 839 |
actor_name = DEFAULT_ACTOR_NONGL; |
| 840 |
|
| 841 |
// Upgrade default to a more appealing OpenGL actor if available |
| 842 |
if (bin.actor_available(DEFAULT_ACTOR_GL)) { |
| 843 |
actor_name = DEFAULT_ACTOR_GL; |
| 844 |
} |
| 845 |
} |
| 846 |
|
| 847 |
// Let the bin manage plugins. There's a bug otherwise. |
| 848 |
if (!bin.connect(actor_name, input_name)) { |
| 849 |
throw std::runtime_error ("Failed to start pipeline with actor '" + actor_name + "' and input '" + input_name + "'"); |
| 850 |
} |
| 851 |
|
| 852 |
auto actor = bin.get_actor(); |
| 853 |
|
| 854 |
// Select output colour depth |
| 855 |
|
| 856 |
VisVideoDepth depth; |
| 857 |
int depthflag = visual_actor_get_supported_depth(actor); |
| 858 |
|
| 859 |
// Pick the best display depth directly supported by non GL actor |
| 860 |
if(depthflag != VISUAL_VIDEO_DEPTH_GL) |
| 861 |
{ |
| 862 |
if (color_depth == 0) |
| 863 |
{ |
| 864 |
depth = visual_video_depth_get_highest_nogl (depthflag); |
| 865 |
} |
| 866 |
// Pick user chosen colordepth |
| 867 |
else |
| 868 |
{ |
| 869 |
depth = visual_video_depth_enum_from_value (color_depth); |
| 870 |
} |
| 871 |
} |
| 872 |
/* GL actor */ |
| 873 |
else |
| 874 |
{ |
| 875 |
depth = visual_video_depth_get_highest (depthflag); |
| 876 |
} |
| 877 |
|
| 878 |
bin.set_depth (depth); |
| 879 |
|
| 880 |
auto vidoptions = visual_actor_get_video_attribute_options(actor); |
| 881 |
|
| 882 |
// initialize display |
| 883 |
Display display; |
| 884 |
|
| 885 |
// create display |
| 886 |
auto video = display.create(depth, vidoptions, width, height, true); |
| 887 |
if(!video) { |
| 888 |
throw std::runtime_error("Failed to setup display for rendering"); |
| 889 |
} |
| 890 |
|
| 891 |
// Set the display title |
| 892 |
display.set_title(_("lv-tool")); |
| 893 |
|
| 894 |
// put it all together |
| 895 |
bin.set_video(video); |
| 896 |
bin.realize(); |
| 897 |
bin.sync(false); |
| 898 |
bin.depth_changed(); |
| 899 |
|
| 900 |
// get a queue to handle events |
| 901 |
VisEventQueue * const localqueue = visual_event_queue_new(); |
| 902 |
visual_log_return_val_if_fail( localqueue != nullptr, EXIT_FAILURE ); |
| 903 |
|
| 904 |
// rendering statistics |
| 905 |
uint64_t frames_drawn = 0; |
| 906 |
|
| 907 |
// frame rate control state |
| 908 |
uint64_t const frame_period_us = frame_rate > 0 ? VISUAL_USEC_PER_SEC / frame_rate : 0; |
| 909 |
LV::Time last_frame_time; |
| 910 |
bool draw_frame = true; |
| 911 |
|
| 912 |
// main loop |
| 913 |
bool running = true; |
| 914 |
ResizeRequest resize_request; |
| 915 |
//bool visible = true; |
| 916 |
|
| 917 |
while (running) |
| 918 |
{ |
| 919 |
// Check if process termination was signaled |
| 920 |
if (terminate_process) { |
| 921 |
std::cerr << "Received signal to terminate process, exiting..\n"; |
| 922 |
return EXIT_SUCCESS; |
| 923 |
} |
| 924 |
|
| 925 |
// Control frame rate |
| 926 |
if (frame_rate > 0) { |
| 927 |
if (frames_drawn > 0) { |
| 928 |
draw_frame = (LV::Time::now () - last_frame_time).to_usecs () >= frame_period_us; |
| 929 |
} |
| 930 |
} |
| 931 |
|
| 932 |
if (draw_frame) { |
| 933 |
DisplayLock lock {display}; |
| 934 |
|
| 935 |
// Draw audio data and render |
| 936 |
bin.run(); |
| 937 |
|
| 938 |
// Display rendering |
| 939 |
display.update_all (); |
| 940 |
|
| 941 |
// Record frame time |
| 942 |
last_frame_time = LV::Time::now (); |
| 943 |
|
| 944 |
// All frames rendered? |
| 945 |
frames_drawn++; |
| 946 |
if (frame_count > 0 && frames_drawn >= frame_count) { |
| 947 |
break; |
| 948 |
} |
| 949 |
|
| 950 |
// switch actor? |
| 951 |
if (actor_switch_after_frames > 0 && |
| 952 |
frames_drawn >= actor_switch_after_frames + actor_switch_framecount) |
| 953 |
{ |
| 954 |
actor_switch_framecount += actor_switch_after_frames; |
| 955 |
|
| 956 |
actor_name = cycle_actor_name (actor_name, CycleDir::NEXT); |
| 957 |
std::cerr << "Switching to actor '" << actor_name << "'...\n"; |
| 958 |
bin.switch_actor (actor_name); |
| 959 |
} |
| 960 |
} |
| 961 |
|
| 962 |
// Note: The static is to avoid stack-use-after-scope |
| 963 |
static VisEvent ev; |
| 964 |
|
| 965 |
// Handle all events |
| 966 |
display.drain_events(*localqueue, video); |
| 967 |
|
| 968 |
auto pluginqueue = visual_plugin_get_eventqueue (visual_actor_get_plugin(bin.get_actor())); |
| 969 |
|
| 970 |
while (visual_event_queue_poll(localqueue, &ev)) |
| 971 |
{ |
| 972 |
if(ev.type != VISUAL_EVENT_RESIZE) |
| 973 |
visual_event_queue_add(pluginqueue, &ev); |
| 974 |
|
| 975 |
switch (ev.type) |
| 976 |
{ |
| 977 |
case VISUAL_EVENT_PARAM: |
| 978 |
{ |
| 979 |
break; |
| 980 |
} |
| 981 |
|
| 982 |
case VISUAL_EVENT_RESIZE: |
| 983 |
{ |
| 984 |
resize_request.store(ev.event.resize.width, ev.event.resize.height); |
| 985 |
break; |
| 986 |
} |
| 987 |
|
| 988 |
case VISUAL_EVENT_MOUSEMOTION: |
| 989 |
{ |
| 990 |
break; |
| 991 |
} |
| 992 |
|
| 993 |
case VISUAL_EVENT_MOUSEBUTTONDOWN: |
| 994 |
{ |
| 995 |
// switch to next actor |
| 996 |
actor_name = cycle_actor_name (actor_name, CycleDir::NEXT); |
| 997 |
|
| 998 |
std::cerr << "Switching to actor '" << actor_name << "'...\n"; |
| 999 |
bin.switch_actor (actor_name); |
| 1000 |
|
| 1001 |
break; |
| 1002 |
} |
| 1003 |
|
| 1004 |
case VISUAL_EVENT_MOUSEBUTTONUP: |
| 1005 |
{ |
| 1006 |
break; |
| 1007 |
} |
| 1008 |
|
| 1009 |
case VISUAL_EVENT_KEYDOWN: |
| 1010 |
{ |
| 1011 |
switch(ev.event.keyboard.keysym.sym) |
| 1012 |
{ |
| 1013 |
case VKEY_ESCAPE: |
| 1014 |
{ |
| 1015 |
running = false; |
| 1016 |
break; |
| 1017 |
} |
| 1018 |
|
| 1019 |
case VKEY_TAB: |
| 1020 |
{ |
| 1021 |
break; |
| 1022 |
} |
| 1023 |
|
| 1024 |
case VKEY_F11: |
| 1025 |
{ |
| 1026 |
const bool currently_fullscreen = display.is_fullscreen(); |
| 1027 |
display.set_fullscreen (!currently_fullscreen, true); |
| 1028 |
|
| 1029 |
video = display.get_video(); |
| 1030 |
bin.set_video(video); |
| 1031 |
bin.sync(false); |
| 1032 |
break; |
| 1033 |
} |
| 1034 |
|
| 1035 |
default: |
| 1036 |
break; |
| 1037 |
} |
| 1038 |
|
| 1039 |
break; |
| 1040 |
} |
| 1041 |
|
| 1042 |
case VISUAL_EVENT_KEYUP: |
| 1043 |
{ |
| 1044 |
break; |
| 1045 |
} |
| 1046 |
|
| 1047 |
case VISUAL_EVENT_QUIT: |
| 1048 |
{ |
| 1049 |
running = false; |
| 1050 |
break; |
| 1051 |
} |
| 1052 |
|
| 1053 |
case VISUAL_EVENT_VISIBILITY: |
| 1054 |
{ |
| 1055 |
//visible = ev.event.visibility.is_visible; |
| 1056 |
break; |
| 1057 |
} |
| 1058 |
|
| 1059 |
default: |
| 1060 |
{ |
| 1061 |
break; |
| 1062 |
} |
| 1063 |
} |
| 1064 |
} |
| 1065 |
|
| 1066 |
if (resize_request.is_due ()) { |
| 1067 |
DisplayLock lock {display}; |
| 1068 |
|
| 1069 |
width = resize_request.width(); |
| 1070 |
height = resize_request.height(); |
| 1071 |
|
| 1072 |
video = display.create(depth, vidoptions, width, height, true); |
| 1073 |
display.set_title(_("lv-tool")); |
| 1074 |
|
| 1075 |
bin.set_video (video); |
| 1076 |
bin.sync(false); |
| 1077 |
|
| 1078 |
resize_request.mark_as_applied(); |
| 1079 |
} |
| 1080 |
|
| 1081 |
if (bin.depth_changed()) |
| 1082 |
{ |
| 1083 |
DisplayLock lock {display}; |
| 1084 |
|
| 1085 |
int depthflag = bin.get_depth(); |
| 1086 |
VisVideoDepth depth = visual_video_depth_get_highest(depthflag); |
| 1087 |
|
| 1088 |
display.create(depth, vidoptions, width, height, true); |
| 1089 |
|
| 1090 |
video = display.get_video(); |
| 1091 |
bin.set_video(video); |
| 1092 |
|
| 1093 |
bin.sync(true); |
| 1094 |
} |
| 1095 |
} |
| 1096 |
|
| 1097 |
return EXIT_SUCCESS; |
| 1098 |
} |
| 1099 |
catch (std::exception& error) { |
| 1100 |
std::cerr << error.what () << std::endl; |
| 1101 |
|
| 1102 |
return EXIT_FAILURE; |
| 1103 |
} |
| 1104 |
} |