demotool/utilities.cc

158 lines
3.4 KiB
C++
Raw Normal View History

2017-09-30 10:37:45 +02:00
#include "externals.hh"
#include "utilities.hh"
void disableButton( )
{
ImGui::PushStyleColor( ImGuiCol_Button , ImColor( .3f , .3f , .3f ) );
ImGui::PushStyleColor( ImGuiCol_ButtonHovered , ImColor( .3f , .3f , .3f ) );
ImGui::PushStyleColor( ImGuiCol_ButtonActive , ImColor( .3f , .3f , .3f ) );
}
/*----------------------------------------------------------------------------*/
void updateAngle(
__rw__ float& initial ,
__rd__ const float delta
)
{
initial = fmod( initial + delta + 540 , 360 ) - 180;
}
void anglesToMatrix(
__rd__ float const* angles ,
__wr__ float* matrix )
{
float c[3] , s[3];
for ( int i = 0 ; i < 3 ; i ++ ) {
const float a = M_PI * angles[ i ] / 180;
c[i] = cos( a );
s[i] = sin( a );
}
matrix[0] = c[1]*c[2];
matrix[1] = s[0]*s[1]*c[2] - c[0]*s[2];
matrix[2] = s[0]*s[2] + c[0]*s[1]*c[2];
matrix[3] = c[1]*s[2];
matrix[4] = c[0]*c[2] + s[0]*s[1]*s[2];
matrix[5] = c[0]*s[1]*s[2] - s[0]*c[2];
matrix[6] = -s[1];
matrix[7] = s[0]*c[1];
matrix[8] = c[0]*c[1];
}
/*= T_FilesWatcher ===========================================================*/
T_FilesWatcher::T_FilesWatcher( )
: fd( inotify_init1( O_NONBLOCK ) )
{ }
T_FilesWatcher::T_FilesWatcher( T_FilesWatcher&& other ) noexcept
: fd( 0 ) , watched( std::move( other.watched ) )
{
std::swap( fd , other.fd );
other.watched.clear( );
for ( T_WatchedFiles* wf : watched ) {
if ( wf ) {
wf->watcher = this;
}
}
}
T_FilesWatcher::~T_FilesWatcher( )
{
if ( fd ) {
close( fd );
}
for ( T_WatchedFiles* wf : watched ) {
if ( wf ) {
wf->watcher = nullptr;
}
}
}
void T_FilesWatcher::check( )
{
for ( T_WatchedFiles* wf : watched ) {
if ( wf ) {
wf->triggered = false;
}
}
inotify_event ie;
while ( read( fd , &ie , sizeof( ie ) ) == sizeof( ie ) ) {
if ( ( ie.mask & ( IN_CLOSE_WRITE | IN_DELETE_SELF ) ) == 0 ) {
continue;
}
for ( T_WatchedFiles* wf : watched ) {
if ( !wf || wf->triggered ) {
continue;
}
auto const& idl( wf->identifiers );
if ( find( idl , ie.wd ) != idl.end( ) ) {
wf->triggered = true;
wf->callback( );
}
}
}
}
/*= T_WatchedFiles ===========================================================*/
T_WatchedFiles::T_WatchedFiles( T_WatchedFiles&& other ) noexcept
: watcher( other.watcher ) , callback( other.callback ) ,
triggered( other.triggered ) ,
identifiers( std::move( other.identifiers ) )
{
if ( watcher ) {
other.watcher = nullptr;
*( find( watcher->watched , &other ) ) = this;
}
}
T_WatchedFiles::T_WatchedFiles(
__rw__ T_FilesWatcher& watcher ,
__rd__ const F_OnFileChanges callback )
: watcher( &watcher ) , callback( callback ) , triggered( false )
{
watcher.watched.push_back( this );
}
T_WatchedFiles::~T_WatchedFiles( )
{
clear( );
if ( watcher ) {
watcher->watched.erase( find( watcher->watched , this ) );
}
}
void T_WatchedFiles::clear( )
{
if ( watcher ) {
const auto fd( watcher->fd );
for ( int wd : identifiers ) {
inotify_rm_watch( fd , wd );
}
}
identifiers.clear( );
}
bool T_WatchedFiles::watch(
__rd__ std::string const& file )
{
static constexpr auto inFlags( IN_CLOSE_WRITE | IN_DELETE_SELF );
if ( watcher ) {
const auto wd( inotify_add_watch( watcher->fd ,
file.c_str( ) , inFlags ) );
if ( wd == -1 ) {
return false;
}
if ( find( identifiers , wd ) == identifiers.end( ) ) {
identifiers.push_back( wd );
}
return true;
}
return false;
}