/* This file is part of cardwords
   (c) 1999 Tobias Peters
   see file COPYING for the copyright terms.
   
   This program 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 2 of the License, or
   (at your option) any later version.

   This program 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 this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

// cardwords_textclient.hh

#ifndef CARDWORDS_NCURSESAPP_HH
#define CARDWORDS_NCURSESAPP_HH

// This file contains the class for accessing the text screen via ncurses.

#include <map>
#include "cardwords_select.hh"

class CardWords_NCursesApp : CardWords_Selectible
{
public:
  CardWords_NCursesApp(CardWords_Selector *);
  ~CardWords_NCursesApp();

  size_t
  get_screen_width() const;
  size_t
  get_screen_height() const;

  bool
  has_colors() const;

  // Return a pointer to the CardWords_NCursesApp instance (only one allowed).
  // If no instance exists, returns 0.
  static
  CardWords_NCursesApp *
  instance(void);

  // Register a function to be notified when SIGWINCH occurs
  typedef void(*SIGWINCH_Handler)(void *);
  
  int //tag
  register_sigwinch_handler(SIGWINCH_Handler, void *);
  void
  deregister_sigwinch_handler(int tag);

  virtual
  void
  being_selected (int fd, int mode);

  virtual
  void
  timed_out (int fd, int mode) {};

private:
  // SIGWINCH must be handled by an ncurses app:
  void
  SIGWINCH_marshaller();

  static
  void
  SIGWINCH_handler(int);

  static
  CardWords_NCursesApp * _instance;

  map<int/*tag*/,pair<SIGWINCH_Handler, void*> > sigwinch_handlers;
  int last_tag_in_use;

  static
  int sighandler_pipe[2];

  bool currently_marshalling_sigwinch;
  map<int/*tag*/,pair<SIGWINCH_Handler, void*> >::iterator current_handler;
  map<int/*tag*/,pair<SIGWINCH_Handler, void*> >::iterator next_handler;

  CardWords_Selector * selector;
};
#endif
