Skip to content

Commit

Permalink
hack up direct_mode_player #1342
Browse files Browse the repository at this point in the history
  • Loading branch information
dankamongmen committed Feb 15, 2021
1 parent f275da2 commit 9ed8b95
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 9 deletions.
13 changes: 10 additions & 3 deletions doc/man/man1/ncplayer.1.md
Expand Up @@ -21,7 +21,7 @@ area, and the **sexblitter** blitter is used (where known to work well) for a
# OPTIONS

**-d** ***delaymult***: Apply a non-negative rational multiplier to the delayscale.
Only applies to multiframe media such as video and animated images.
Only applies to multiframe media such as video and animated images. Not supported with **-k**.

**-t** ***seconds***: Delay **seconds** after each file. If this option is used,
the "press any key to continue" prompt will not be displayed. **seconds** may
Expand All @@ -36,9 +36,9 @@ be any non-negative number.

**-m margins**: Define rendering margins (see below).

**-L**: Loop frames until a key is pressed.
**-L**: Loop frames until a key is pressed. Not supported with **-k**.

**-k**: Use direct mode (see **notcurses_direct(3)**). This will have the effect of leaving the output on-screen after program exit, and generating it inline (rather than clearing the screen and placing it at the top).
**-k**: Use direct mode (see **notcurses_direct(3)**). This will have the effect of leaving the output on-screen after program exit, and generating it inline (rather than clearing the screen and placing it at the top). Not supported with **-L** or **-d**.

**-q**: Print neither frame/timing information along the top of the screen, nor the output summary on exit.

Expand Down Expand Up @@ -76,6 +76,13 @@ capability, or that the environment variable **COLORTERM** is defined to
**24bit** (and that the terminal honors this variable), along with a
fixed-width font with good coverage of the Unicode Block Drawing Characters.

# BUGS

Direct mode (**-k**) does not yet support multiframe media. It'll read them
just fine, but only show the first frame. This might or might not change in
the future. Direct mode is kinda fundamentally suboptimal for multiframe
media. Until that time, **-k** is exclusive with **-d** and **-L**.

# SEE ALSO

**notcurses(3)**,
Expand Down
75 changes: 69 additions & 6 deletions src/player/play.cpp
Expand Up @@ -8,6 +8,7 @@
#include <libgen.h>
#include <unistd.h>
#include <iostream>
#include <ncpp/Direct.hh>
#include <ncpp/Visual.hh>
#include <ncpp/NotCurses.hh>
#include "compat/compat.h"
Expand All @@ -22,7 +23,7 @@ void usage(std::ostream& o, const char* name, int exitcode){
o << " -h: display help and exit with success\n";
o << " -V: print program name and version\n";
o << " -q: be quiet (no frame/timing information along top of screen)\n";
o << " -k: use direct mode rather than rendered mode\n";
o << " -k: use direct mode (cannot be used with -L or -d)\n";
o << " -L: loop frames\n";
o << " -t seconds: delay t seconds after each file\n";
o << " -l loglevel: integer between 0 and 9, goes to stderr'\n";
Expand Down Expand Up @@ -195,10 +196,18 @@ auto handle_opts(int argc, char** argv, notcurses_options& opts, bool* quiet,
usage(std::cerr, argv[0], EXIT_FAILURE);
}
break;
case 'k':{
case 'k':{ // actually engages direct mode
opts.flags |= NCOPTION_NO_ALTERNATE_SCREEN;
if(*loop || *timescale != 1.0){
std::cerr << "-k cannot be used with -L or -d" << std::endl;
usage(std::cerr, argv[0], EXIT_FAILURE);
}
break;
}case 'L':{
if(opts.flags & NCOPTION_NO_ALTERNATE_SCREEN){
std::cerr << "-L cannot be used with -k" << std::endl;
usage(std::cerr, argv[0], EXIT_FAILURE);
}
*loop = true;
break;
}case 'm':{
Expand Down Expand Up @@ -231,6 +240,10 @@ auto handle_opts(int argc, char** argv, notcurses_options& opts, bool* quiet,
usage(std::cerr, argv[0], EXIT_FAILURE);
}
*timescale = ts;
if(opts.flags & NCOPTION_NO_ALTERNATE_SCREEN){
std::cerr << "-d cannot be used with -k" << std::endl;
usage(std::cerr, argv[0], EXIT_FAILURE);
}
break;
}case 'l':{
std::stringstream ss;
Expand Down Expand Up @@ -259,11 +272,60 @@ auto handle_opts(int argc, char** argv, notcurses_options& opts, bool* quiet,
return optind;
}

int direct_mode_player(int argc, char** argv, bool quiet, float timescale,
// argc/argv ought already be reduced to only the media arguments
int direct_mode_player(int argc, char** argv, float timescale,
ncscale_e scalemode, ncblitter_e blitter,
float displaytime, bool loop){
// FIXME
return 0;
Direct dm{};
if(!dm.canopen_images()){
std::cerr << "Notcurses was compiled without multimedia support\n";
return -1;
}
bool failed = false;
{
for(auto i = 0 ; i < argc ; ++i){
std::unique_ptr<Visual> ncv;
try{
ncv = std::make_unique<Visual>(argv[i]);
}catch(std::exception& e){
// FIXME want to stop nc first :/ can't due to stdn, ugh
std::cerr << argv[i] << ": " << e.what() << "\n";
failed = true;
break;
}
struct ncvisual_options vopts{};
int r;
vopts.scaling = scalemode;
vopts.blitter = blitter;
do{
// FIXME just display, please
if(r == 0){
//vopts.blitter = marsh.blitter;
if(displaytime < 0){
/*
if(!nc.render()){
failed = true;
break;
}
*/
}else{
// FIXME do we still want to honor keybindings when timing out?
struct timespec ts;
ts.tv_sec = displaytime;
ts.tv_nsec = (displaytime - ts.tv_sec) * NANOSECS_IN_SEC;
clock_nanosleep(CLOCK_MONOTONIC, 0, &ts, NULL);
}
}
}while(loop && r == 0);
if(r < 0){ // positive is intentional abort
std::cerr << "Error decoding " << argv[i] << std::endl;
failed = true;
break;
}
}
}
done:
return failed ? -1 : 0;
}

auto main(int argc, char** argv) -> int {
Expand All @@ -282,7 +344,8 @@ auto main(int argc, char** argv) -> int {
// if -k was provided, we now use direct mode rather than simply not using the
// alternate screen, so that output is inline with the shell.
if(ncopts.flags & NCOPTION_NO_ALTERNATE_SCREEN){
if(direct_mode_player(argc, argv, quiet, timescale, scalemode, blitter, displaytime, loop)){
if(direct_mode_player(argc - nonopt, argv + nonopt, timescale,
scalemode, blitter, displaytime, loop)){
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
Expand Down

0 comments on commit 9ed8b95

Please sign in to comment.