Skip to content

Commit

Permalink
Port vpKeyboard on windows
Browse files Browse the repository at this point in the history
  • Loading branch information
fspindle committed Jul 6, 2023
1 parent 3702933 commit 5a0c67f
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 20 deletions.
11 changes: 0 additions & 11 deletions example/tools/keyboard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
* Description:
* Example of keyboard management.
*
*
*****************************************************************************/

/*!
Expand All @@ -43,10 +42,7 @@
#include <visp3/core/vpConfig.h>
#include <visp3/core/vpDebug.h>

#if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
#include <iostream>
#include <signal.h>
#include <stdio.h>

#include <visp3/io/vpKeyboard.h>

Expand Down Expand Up @@ -77,10 +73,3 @@ int main()

return EXIT_SUCCESS;
}
#else
int main()
{
std::cout << "vpKeyboard class works only on unix..." << std::endl;
return EXIT_SUCCESS;
}
#endif
9 changes: 5 additions & 4 deletions modules/io/include/visp3/io/vpKeyboard.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,13 @@

#include <visp3/core/vpConfig.h>

#if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))

#include <iostream>

#if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
#include <stdlib.h>
#include <termios.h>
#include <unistd.h>
#endif // defined UNIX

/*!
Expand Down Expand Up @@ -88,15 +89,15 @@ class VISP_EXPORT vpKeyboard
int kbhit();
int getchar();

#if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
private:
void init();
void end();

void setRawMode(bool active);

struct termios initial_settings, new_settings;
};

#endif // defined UNIX
};

#endif
50 changes: 45 additions & 5 deletions modules/io/src/tools/vpKeyboard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,13 @@
*
*****************************************************************************/

#if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
#include <stdio.h>

#if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
#include <sys/select.h>
#elif defined(_WIN32)
#include <conio.h>
#endif
#include <visp3/io/vpKeyboard.h>

/*!
Expand All @@ -46,22 +50,44 @@
/*!
Activates the raw mode to read keys in an non blocking way.
*/
vpKeyboard::vpKeyboard() : initial_settings(), new_settings() { init(); }
vpKeyboard::vpKeyboard()
#if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
: initial_settings(), new_settings()
{
init();
}
#else
{}
#endif



/*!
Stops the raw mode.
*/
vpKeyboard::~vpKeyboard() { end(); }
vpKeyboard::~vpKeyboard()
{
#if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
end();
#endif
}

/*!
Get the hit key. kbhit() indicates if a key was hitten.
*/
int vpKeyboard::getchar()
{
#if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
int c;
c = ::getchar();
return c;
#elif defined(_WIN32)
return _getch();
#else
std::cout << "vpKeyboard class is not supported by your system!" << std::endl;
return 0;
#endif
}

/*!
Expand All @@ -71,26 +97,40 @@ int vpKeyboard::getchar()
*/
int vpKeyboard::kbhit()
{
#if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
struct timeval tv = {0, 0};
fd_set readfds;

FD_ZERO(&readfds);
FD_SET(STDIN_FILENO, &readfds);

return select(STDIN_FILENO + 1, &readfds, NULL, NULL, &tv) == 1;
#elif defined(_WIN32)
return _kbhit();
#else
std::cout << "vpKeyboard class is not supported by your system!" << std::endl;
return 0;
#endif
}

#if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
/*!
Activates the raw mode to read keys in an non blocking way.
*/
void vpKeyboard::init() { setRawMode(true); }
void vpKeyboard::init()
{
setRawMode(true);
}

/*!
Stops the raw mode.
*/
void vpKeyboard::end() { setRawMode(false); }
void vpKeyboard::end()
{
setRawMode(false);
}

/*!
Turns on/off the 'raw' mode.
Expand Down

0 comments on commit 5a0c67f

Please sign in to comment.