//
// Signal handling
//
// Copyright 2026, Jakob Østergaard Hegelund <joe@unthought.net>
// -----------------------------------------------------------------------
// This file is part of FanMan.
//
// FanMan is free software: you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// FanMan is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with FanMan. If not, see <https://www.gnu.org/licenses/>.
// -----------------------------------------------------------------------
//

#include "sigh.hh"
#include <signal.h>

SigHandler::SigHandler(int signal, std::condition_variable &cv)
  : m_cv(cv)
{
  m_sigmap[signal] = this;
  struct sigaction sa{};
  sa.sa_handler = &sighandler;
  if (sigaction(signal, &sa, 0) == -1)
    throw std::runtime_error("Unable to set up signal handler");
}

void SigHandler::handler()
{
  m_triggered = true;
  m_cv.notify_all();
}


std::unordered_map<int,SigHandler*> SigHandler:: m_sigmap;

void SigHandler::sighandler(int sig)
{
  SigHandler::m_sigmap[sig]->handler();
}
