/* Network-Integrated Multimedia Middleware (NMM)
 * http://www.motama.com/nmm.html
 *  
 * Copyright (C) 2006-2007
 * Motama GmbH, Saarbruecken, Germany
 * http://www.motama.com
 *  
 * This file is part of NMM. 
 * 
 * NMM 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; only version 2 of the License.      
 * 
 * NMM 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 NMM; if not, write to the Free Software Foundation,    
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 */

#ifndef NMM_X_DISPLAY_NODE_HPP
#define NMM_X_DISPLAY_NODE_HPP NMM_X_DISPLAY_NODE_HPP

#include "nmm/nmm_prefix.hpp"

#include "nmm/multimedia/sync/GenericUSyncSinkNode.hpp"

#include "nmm/plugins/video/display/interfaces/IVideoDisplayImpl.hpp"
#include "nmm/plugins/video/display/interfaces/IXDisplayEventSenderImpl.hpp"
#include "nmm/plugins/video/display/interfaces/IXWindowIDImpl.hpp"
#include "nmm/plugins/video/display/interfaces/IMouseCursorImpl.hpp"

#include "nmm/plugins/interfaces/IStreamJumpImpl.hpp"

#include "nmm/utils/time/Benchmark.hpp"

namespace NMM {
  
  class XVirtualImg;
  class ShmBufferManager;
  
  class XDisplayNode
    : public GenericUSyncSinkNode,
      public IVideoDisplayImpl,
      public IXDisplayEventSenderImpl,
      public IXWindowIDImpl,
      public IStreamJumpImpl,
      public IMouseCursorImpl
  {
  public:
    /** Create an %XDisplayNode.
     *
     * @param name name of the node
     * @param queue_mode the way how buffers are queued (or discarded)
     * @param queue_size size of the queue
     */
    XDisplayNode(const char* name = "XDisplayNode",
		 StreamQueue::Mode queue_mode = StreamQueue::MODE_SUSPEND,
		 int queue_size = 20);
    
    virtual ~XDisplayNode();
    
    Result getDepth( int& );
    
    /**
     * Refresh display.
     * I don't know what this does but I comment it anyway to be consistent.
     */
    void refresh();
    
    /**
     * \param shared_pages if value > 1 then a ShmBufferManager is used with the given number of display pages
     */
    void useSharedMemoryManager( unsigned int shared_pages );
    
  protected:
    virtual Result doInit();
    virtual Result doInitOutput();
    virtual Result doStart();
    virtual Result doStop();
    virtual Result doDeinit();
    virtual Result doDeactivate();
    virtual Result doFlush();
    
    virtual void prepareBuffer(Buffer *in_buffer);
    virtual Message* presentBuffer(Buffer *in_buffer);
    
    /** IDisplayEventSender interface. */
    virtual void enableDisplayEvents(const string& config_file);
    virtual void disableDisplayEvents();

    /** IXDisplayEventSender interface. */
    virtual void setUsingKeySymKeyEvents(const nmm_bool& ukke);
    virtual nmm_bool isUsingKeySymKeyEvents();
    virtual void setSendingMouseEventsDirect(const nmm_bool& smed);
    virtual nmm_bool isSendingMouseEventsDirect();
    virtual void setXAutoRepeat(const nmm_bool& on);
    virtual nmm_bool isXAutoRepeat();
    
    /** IXWindowID interface. */
    virtual nmm_uint32 getWindowID(); 

    /** IVideoDisplay interface. */
    virtual void setBenchmarkingEnabled(const nmm_bool& value);
    virtual void setDisplayPosition(const nmm_int32& value, const nmm_int32& value);
    virtual void setFullscreen( const nmm_bool& fullscreen );
    virtual nmm_bool getFullscreen() const;
    virtual nmm_bool hasFullscreen() const;
    virtual nmm_bool isFullscreenOnly() const;
    virtual nmm_bool makeScreenshot(const string &) const;
    virtual void setLeftWindowAlignment();
    virtual void setCenterWindowAlignment();
    virtual void setRightWindowAlignment();

   /** IStreamJump interface. */
    virtual Result fastForward( const nmm_int32& value );
    virtual Result streamJump( const nmm_int32& value );
    virtual Result sendIFramesOnly( const nmm_int32& count );
    
    /** IMouseCursor interface. */
    virtual void useDotCursor(const nmm_bool& value);
    virtual void useTriangleCursor(const nmm_bool& value);
    virtual void useStandardCursor();
    
    virtual Result setFormat(const Format& format);

  private:
    int xPos;
    int yPos;
    string colorspace;
    int width, height;
    int depth;
    
    char *frame_title;
    
    int title_update_rate;
    
    bool fullscreen;
    int  ratio_x, ratio_y;
    bool xshm_supported;
    bool display_local;

    XVirtualImg *image;
    Buffer *last_in_buffer;
    /* holds the shared memory buffer manager, NULL if default buffer manager is used */
    ShmBufferManager* buffermanager;
    
    /* number of (display) pages for shared memory, <=1 if ShmBufferManager is not used */
    unsigned int      shared_memory_pages;
    
    Benchmark benchmark;

    ///! flag to store if display events were enabled when
    ///  deactivating, to enable the on activation again.
    bool display_events_enabled;
    ///! if display events were enabled last time, the Node was activ,
    ///  this string stores the config file parameter given on
    ///  enabling. Its needed to reenable display events on
    ///  reactivation.
    string display_events_config;

    enum WindowAlignment {
      LEFT = 0,
      CENTER = 1,
      RIGHT = 2,
      CUSTOM = 3
    } align;

    bool benchmarking_enabled;
    Result allocate();
    Result setResolution(int x, int y);
    void setRatio(int x, int y);
  };
  
}

#include "nmm/nmm_suffix.hpp"

#endif
