Skip to content

Commit

Permalink
loop through dir with arrow keys
Browse files Browse the repository at this point in the history
  • Loading branch information
Like4Schnitzel committed Feb 15, 2024
1 parent c8911ee commit ebdec95
Show file tree
Hide file tree
Showing 11 changed files with 280 additions and 53 deletions.
5 changes: 4 additions & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,13 @@ add_executable( transcoder transcoder/main.cpp transcoder/videotranscoder.cpp li
#target_link_options( transcoder PRIVATE -pg )
target_link_libraries( transcoder stdc++fs ${OpenCV_LIBS} )

add_executable( decoder decoder/main.cpp decoder/vtdidecoder.cpp libs/binaryutils.cpp libs/variousutils.cpp )
add_executable( decoder decoder/main.cpp decoder/vtdidecoder.cpp libs/binaryutils.cpp libs/variousutils.cpp libs/termutils.cpp )
#target_compile_options( decoder PRIVATE -pg -fsanitize=address )
#target_link_options( decoder PRIVATE -pg -fsanitize=address )
target_link_libraries( decoder stdc++fs )

add_executable( img-viewer img-viewer/main.cpp img-viewer/imgviewer.cpp libs/variousutils.cpp )
target_link_libraries( img-viewer stdc++fs ${OpenCV_LIBS} )

add_executable( media-viewer media-viewer/main.cpp libs/termutils.cpp libs/variousutils.cpp libs/binaryutils.cpp )
target_link_libraries( media-viewer stdc++fs ${OpenCV_LIBS} )
2 changes: 1 addition & 1 deletion src/decoder/vtdidecoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ void VTDIDecoder::playVideo()
throw std::runtime_error("It seems static info hasn't been initialized yet. Try running VTDIDecoder.getStaticInfo()");
}

auto terminalDimensions = VariousUtils::getTerminalDimensions();
auto terminalDimensions = TermUtils::getTerminalDimensions();
this->terminalWidth = terminalDimensions[0];
this->terminalHeight = terminalDimensions[1];

Expand Down
1 change: 1 addition & 0 deletions src/decoder/vtdidecoder.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "../libs/charinfo.hpp"
#include "../libs/variousutils.hpp"
#include "../libs/binaryutils.hpp"
#include "../libs/termutils.hpp"

namespace vtt {

Expand Down
40 changes: 40 additions & 0 deletions src/libs/_kbhit.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#pragma once

#ifdef WIN32

// _kbhit() is originally from this library but it is windows only
#include <conio.h>

#else
// taken from https://www.flipcode.com/archives/_kbhit_for_Linux.shtml
// very slightly modified (#include <strops.h> to #include <sys/ioctl.h>)

/**
Linux (POSIX) implementation of _kbhit().
Morgan McGuire, [email protected]
*/

#include <stdio.h>
#include <sys/select.h>
#include <termios.h>
#include <sys/ioctl.h>

int _kbhit() {
static const int STDIN = 0;
static bool initialized = false;

if (! initialized) {
// Use termios to turn off line buffering
termios term;
tcgetattr(STDIN, &term);
term.c_lflag &= ~ICANON;
tcsetattr(STDIN, TCSANOW, &term);
setbuf(stdin, NULL);
initialized = true;
}

int bytesWaiting;
ioctl(STDIN, FIONREAD, &bytesWaiting);
return bytesWaiting;
}
#endif
84 changes: 84 additions & 0 deletions src/libs/termutils.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
#include "termutils.hpp"

namespace vtt {

TermUtils::TermUtils()
{
hidden = false;
}

// taken from https://stackoverflow.com/a/23370070
#ifdef WIN32

std::array<int, 2> TermUtils::getTerminalDimensions()
{
CONSOLE_SCREEN_BUFFER_INFO csbi;
int columns, rows;

GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
columns = csbi.srWindow.Right - csbi.srWindow.Left + 1;
rows = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;

std::array<int, 2> dimensions;
dimensions[0] = columns;
dimensions[1] rows;

return dimensions;
}

void TermUtils::hideInput()
{
if (!hidden)
{
this->hStdin = GetStdHandle(STD_INPUT_HANDLE);
this->mode = 0;
GetConsoleMode(hStdin, &mode);
SetConsoleMode(hStdin, mode & (~ENABLE_ECHO_INPUT));
hidden = true;
}
}

void TermUtils::showInput()
{
if (hidden)
{
SetConsoleMode(hStdin, mode);
hidden = false;
}
}
#else

std::array<int, 2> TermUtils::getTerminalDimensions()
{
struct winsize w;
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
std::array<int, 2> dimensions;
dimensions[0] = w.ws_col;
dimensions[1] = w.ws_row;

return dimensions;
}

void TermUtils::hideInput()
{
if (!hidden)
{
tcgetattr(STDIN_FILENO, &oldt);
newt = oldt;
newt.c_lflag &= ~ECHO;
tcsetattr(STDIN_FILENO, TCSANOW, &newt);
hidden = true;
}
}

void TermUtils::showInput()
{
if (hidden)
{
tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
hidden = false;
}
}
#endif

}
32 changes: 32 additions & 0 deletions src/libs/termutils.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#pragma once

#ifdef WIN32
#include <windows.h>
#else
#include <sys/ioctl.h>
#include <stdio.h>
#include <unistd.h>
#include <termios.h>
#endif
#include <array>

namespace vtt {

class TermUtils {
private:
bool hidden;
#ifdef WIN32
HANDLE hStdin;
DWORD mode;
#else
termios oldt;
termios newt;
#endif
public:
TermUtils();
static std::array<int, 2> getTerminalDimensions();
void hideInput();
void showInput();
};

}
71 changes: 24 additions & 47 deletions src/libs/variousutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,62 +38,39 @@ bool VariousUtils::fileExists(std::string fileName)
return (stat (fileName.c_str(), &buffer) == 0);
}


// taken from https://stackoverflow.com/a/23370070
#ifdef WIN32
#include <windows.h>

std::array<int, 2> VariousUtils::getTerminalDimensions()
{
CONSOLE_SCREEN_BUFFER_INFO csbi;
int columns, rows;

GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
columns = csbi.srWindow.Right - csbi.srWindow.Left + 1;
rows = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;

std::array<int, 2> dimensions;
dimensions[0] = columns;
dimensions[1] rows;

return dimensions;
}
#else
#include <sys/ioctl.h>
#include <stdio.h>
#include <unistd.h>

std::array<int, 2> VariousUtils::getTerminalDimensions()
std::string VariousUtils::numToUnicodeBlockChar(int num)
{
struct winsize w;
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
std::array<int, 2> dimensions;
dimensions[0] = w.ws_col;
dimensions[1] = w.ws_row;

return dimensions;
char buf[3];
buf[0] = 0xe2;
buf[1] = 0x96;
buf[2] = 0x80 + num;
return std::string(reinterpret_cast<char*>(buf), 3);
}
#endif

char VariousUtils::toLower(char c)
std::vector<dirent> VariousUtils::getFilesInDir(std::filesystem::path path)
{
if (c >= 'A' && c <= 'Z')
if (!std::filesystem::is_directory(path))
{
return c - 32;
std::stringstream errorMsg;
errorMsg << path.c_str() << " is not a directory.";
throw std::runtime_error(errorMsg.str());
}
else

std::vector<dirent> files;
DIR* dp = nullptr;
dirent* entry = nullptr;
dp = opendir(path.c_str());

while ((entry = readdir(dp)))
{
return c;
// d_type 8 is file. 4 is directory.
if (entry->d_type == 8)
{
files.push_back(*entry);
}
}
}

std::string VariousUtils::numToUnicodeBlockChar(int num)
{
char buf[3];
buf[0] = 0xe2;
buf[1] = 0x96;
buf[2] = 0x80 + num;
return std::string(reinterpret_cast<char*>(buf), 3);
return files;
}

}
7 changes: 4 additions & 3 deletions src/libs/variousutils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
#include <vector>
#include <array>
#include <memory>
#include <filesystem>
#include <dirent.h>

namespace vtt {

Expand All @@ -27,9 +29,6 @@ class VariousUtils {
return sub;
}

static std::array<int, 2> getTerminalDimensions();
static char toLower(char c);

template <typename T>
static void pushArrayToVector(const T* inputArr, int inputArrLen, std::vector<T>& vec)
{
Expand All @@ -40,6 +39,8 @@ class VariousUtils {
}

static std::string numToUnicodeBlockChar(int num);

static std::vector<dirent> getFilesInDir(std::filesystem::path path);
};

}
67 changes: 67 additions & 0 deletions src/media-viewer/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#include <iostream>
#include <termio.h>
#include "../libs/variousutils.hpp"
#include "../libs/termutils.hpp"
#include "../libs/binaryutils.hpp"
#include "../libs/_kbhit.h"

using namespace std;
using namespace vtt;

int main(int argc, char** argv)
{
filesystem::path path;
if (argc > 1)
{
path = filesystem::absolute(argv[1]);
if (!filesystem::is_directory(path))
path = path.parent_path();
}
else
path = filesystem::current_path();

auto files = VariousUtils::getFilesInDir(path);

TermUtils tu;
int fileIndex = 0;
cout << "Selected file: " << files[fileIndex].d_name << "\n";
unsigned char keyValue;
cout << "waiting for a keypress...\npress ESC to close the program.\n" << flush;
tu.hideInput();
while (true)
{
if (_kbhit())
{
std::vector<char> vals;
while (_kbhit())
{
cin >> keyValue;
vals.push_back(keyValue);
}
int combinedKeyCode = BinaryUtils::byteArrayToUint((Byte*)vals.data(), vals.size());

// right arrow
if (combinedKeyCode == 1792835)
{
fileIndex = (fileIndex + 1) % files.size();
cout << "Selected file: " << files[fileIndex].d_name << "\n";
}

// left arrow
if (combinedKeyCode == 1792836)
{
fileIndex--;
if (fileIndex < 0) fileIndex += files.size();
cout << "Selected file: " << files[fileIndex].d_name << "\n";
}

// ESC
if (combinedKeyCode == 27)
break;
}
}
tu.showInput();
cout << "done\n";

return 0;
}
22 changes: 22 additions & 0 deletions src/media-viewer/mediaviewer.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#pragma once

#include "../transcoder/videotranscoder.hpp"
#include "../decoder/vtdidecoder.hpp"
#include "../img-viewer/imgviewer.hpp"

namespace vtt {

enum FileType { image, video };

struct File {
std::string path;
FileType type;
bool readyToView;
};

class MediaViewer {
private:
std::vector<File> files;
};

}
Loading

0 comments on commit ebdec95

Please sign in to comment.