Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

dev.H

Go to the documentation of this file.
00001 #ifndef DEV_H
00002 #define DEV_H
00003 
00004 #include "std/support.H"
00005 #include "mlib/points.H"
00006 
00007 
00008 //---------------------------------------------
00009 //
00010 // DEV: 
00011 //    This package is used to access all devices 
00012 // used by an application.  The general idea is
00013 // that there are a number of generic subclasses
00014 // from an abstract device :
00015 //
00016 //                  DEVice
00017 //
00018 // These subclasses include :
00019 //   DEVice_buttons   - any button device 
00020 //                (including buttons on a mouse!)
00021 //   DEVice_2d        - a 2D valuator device 
00022 //                (just the 2D stuff, no buttons)
00023 //   DEVice_2d_absrel - " + supports abs/rel
00024 //   DEVice_6d        - a 6D valuator device 
00025 //                (once again, no buttons)
00026 //
00027 // The abstract DEVice stores a list of device
00028 // observers.  These observers are all called
00029 // whenever an event occurs on the DEVice.
00030 // The information sent to the observers is
00031 // packaged up in a structure :
00032 // 
00033 //                  Evd
00034 //
00035 // This Evd structure contains 3 pieces of
00036 // information :
00037 //     DEVice *  - a pointer to the device that
00038 //                 generated the event         
00039 //     DEVact    - an enumeration describing 
00040 //                 the type of event that occured
00041 //     DEVmod    - a bit flag of all of the
00042 //                 modifiers active when the 
00043 //                 event occured.
00044 //
00045 // Each observer must be a subclass of a :
00046 //                
00047 //                DEVhandler
00048 //
00049 // A DEVhandler must implement the 'handle_event'
00050 // virtual function, which just takes an Evd
00051 // as input.
00052 //
00053 // Details (coordinate systems, etc):
00054 // ---
00055 //   When a DEVice_2d's event() function is 
00056 // called, the valuator data is mapped and scaled 
00057 // by the DEVice_2d's map and scale parameters.
00058 // Typically, these are setup so that the DEVice
00059 // will report values in the -1 to 1 canonical 
00060 // coordinate system.  (For a tablet, this would
00061 // be from the bottom left to the upper right of
00062 // the tablet).
00063 //   The value of a DEVice_6d is a transformation
00064 // that maps from the tracker coordinate system
00065 // to the world coordinate system.  The tracker
00066 // coordinate system is supposed to be calibrated
00067 // to a display-dependent working volume where
00068 // the boundary of the display map from -1 to 1
00069 // The third coordinate also ranges from -1 to 1
00070 // with -1 being at the display surface and 1 
00071 // being an distance in front of the display
00072 // approximately equal to the width of the display.
00073 //   Since DEVice_buttons usually want to know
00074 // where the cursor is, each DEVice_buttons can
00075 // store a pointer to a DEVice_6d and a DEVice_2d
00076 // object.  Thus a device like a mouse will 
00077 // construct two devices, a DEVice_2d and 
00078 // DEVice_button with the latter also getting a
00079 // pointer to the former.
00080 //
00081 // Connecting generic DEVice_xxx's to real devices
00082 // ----
00083 //   In order for the generic DEVice_xxx objects 
00084 // to get triggered with data, someone must call 
00085 // the 'event()' function on the DEVice_xxx.  This
00086 // event() function is different for each 
00087 // DEVice_xxx subclass, but roughly takes the
00088 // new value of the device and a bit flag of the
00089 // modifiers that are currently active.
00090 //   In practice, the generic DEVice_xxx's event() 
00091 // functions get called by first registering the 
00092 // file descriptor for the real device with the 
00093 // application's highest level FD_MANAGER (see tty.H).  
00094 // Then, when the file descriptor has available data, 
00095 // the device specific callback function is invoked.
00096 // This callback will then read the data from
00097 // the file descriptor (or wherever it gets it)
00098 // and will package it up in the canonical
00099 // coordinate system of the device and finally
00100 // will call the 'event()' function for the 
00101 // appropriate DEVice_xxx.
00102 //
00103 //---------------------------------------------
00104 
00105 //----------------------------------------------
00106 // 
00107 //   FORWARD DECLARATIONS
00108 // 
00109 //----------------------------------------------
00110 
00111 class DEVice;
00112 
00113 //----------------------------------------------
00114 // 
00115 //   DEVact - enumerations of all possible device
00116 //            events.  
00117 //            
00118 //            Perhaps this should be more cleanly
00119 //            separated into individual device 
00120 //            classes, but go blow for now.
00121 // 
00122 //----------------------------------------------
00123 
00124 enum DEVact {
00125    B1U = 0,  // these first events are generated
00126    B1D,      // by button devices.
00127    B2U,
00128    B2D,
00129    B3U,
00130    B3D,
00131    B4U,
00132    B4D,
00133    B5U,
00134    B5D,
00135    B6U,
00136    B6D,
00137    B7U,
00138    B7D,
00139    B8U,
00140    B8D,
00141    B9U,
00142    B9D,
00143    B10U,
00144    B10D,
00145    B11U,
00146    B11D,
00147    B12U,
00148    B12D,
00149    B13U,
00150    B13D,
00151    B14U,
00152    B14D,
00153    B15U,
00154    B15D,
00155    B16U,
00156    B16D,
00157    B17U,
00158    B17D,
00159    B18U,
00160    B18D,
00161    B19U,
00162    B19D,
00163    B20U,
00164    B20D,
00165    B21U,
00166    B21D,
00167    B22U,
00168    B22D,
00169    B23U,
00170    B23D,
00171    B24U,
00172    B24D,
00173    B25U,
00174    B25D,
00175    ACTIVE,    // button 9 - is the active/inactive
00176    INACTIVE,  //            flag for tablet devices
00177    MOV,       // MOTION - generated by 2d & 3d devices
00178    KEYU,      // KEYBOARD - generated by a NULL device
00179    KEYD,      //     
00180    SHIFT_U,   //     NULL Device Also generates
00181    SHIFT_D,   //          events when SHIFT or CONTROL
00182    CTL_U,     //          go up or down
00183    CTL_D
00184 } ;
00185 
00186 //------------------------------------------------------
00187 //
00188 //  Evd - 
00189 //     Event Description. Identifies device events.
00190 //  All device events consist of a DEVice descriptor,
00191 //  and an action.
00192 //
00193 //------------------------------------------------------
00194 #define CEvd const Evd
00195 class Evd {
00196    public :
00197 
00198      enum DEVmod {
00199         EMPTY          = 0,
00200         NONE           = 1,
00201         SHIFT          = (1 << 1),
00202         CONTROL        = (1 << 2),
00203         MOD1           = (1 << 3),
00204         MOD2           = (1 << 4),
00205         MOD3           = (1 << 5),
00206         MOD4           = (1 << 6),
00207         MOD5           = (1 << 7),
00208         MOD6           = (1 << 8),
00209         SHIFT_MOD1     = (1 << 1) | (1 << 3),
00210         CONTROL_MOD2   = (1 << 2) | (1 << 4),
00211         NONE_MOD1_MOD2 = 1 | (1 << 3) | (1 << 4),
00212         NONE_MOD2      = 1 | (1 << 4),
00213         ANY            = (1 << 9)-1
00214      } ;
00215 
00216      DEVice *_d;
00217      DEVact  _a;
00218      DEVmod  _m;
00219      char    _c;
00220 
00221         Evd():_d(0)                                                        { }
00222         Evd(DEVice *d, DEVact a=MOV, DEVmod m=NONE):_d(d),_a(a),_m(m),_c(0){ }
00223         Evd(char   c,  DEVact a=KEYD):_d(0),_a(a),_m(ANY),_c(c){}
00224         Evd(DEVact a) :_d(0),_a(a),_m(ANY),_c(0){}
00225 
00226    Evd &operator = (CEvd &e) { _d=e._d;_a=e._a;_m=e._m;_c=e._c;return *this; }
00227    int operator == (CEvd &e) const       
00228                              { return e._d==_d && e._a==_a && e._c==_c && 
00229                                       (( ((int)_m & (int)(e._m)) != EMPTY) || 
00230                                       _m   == ANY || 
00231                                       e._m == ANY); }
00232 };
00233 
00234 
00235 //------------------------------------------------------
00236 //
00237 //  DEVhandler -
00238 //     Defines an interface for handling Evd events
00239 //
00240 //------------------------------------------------------
00241 class DEVhandler {
00242   public:
00243    DEVhandler() {}
00244    virtual ~DEVhandler() {}
00245 
00246    virtual void handle_event( CEvd & ) = 0;
00247 };
00248 
00249 
00250 //----------------------------------------------
00251 // 
00252 //   DEVice -
00253 //      Abstract device class. Contains a pointer
00254 //   to an event handler object.
00255 //
00256 //----------------------------------------------
00257 
00258 class DEVice {
00259   protected:
00260    ARRAY<DEVhandler *>      _handlers;   // Pointer to the event handler
00261                                          // for this device
00262   public:
00263 //    static mlib::Wtransf booth_to_room;
00264 //    static mlib::Wtransf room_to_booth;
00265 
00266 //    static void set_room_to_booth( mlib::CWtransf &xf )
00267 //       { room_to_booth = xf; booth_to_room = xf.inverse();}
00268 //    static void set_booth_to_room( mlib::CWtransf &xf )
00269 //       { booth_to_room = xf; room_to_booth = xf.inverse();}
00270 
00271                  DEVice()  {}
00272    virtual      ~DEVice()  {}
00273 
00274    virtual void  add_handler( DEVhandler *h ) { _handlers += h; }
00275    virtual void  rem_handler( DEVhandler *h ) { _handlers -= h; }
00276 };
00277 
00278 //----------------------------------------------
00279 // 
00280 //   DEVice_2d -
00281 //      Generic 2d valuator class.
00282 //
00283 //----------------------------------------------
00284 class DEVice_2d : public DEVice {
00285   protected:
00286    mlib::XYpt      _cur;
00287    mlib::XYpt      _old;
00288    mlib::XYvec     _offset;
00289    mlib::XYvec     _scale;
00290    double    _pressure;
00291 
00292    virtual void _event(mlib::CXYpt &p, Evd::DEVmod mod)
00293                                             { set_val(map(p));
00294                                               for(int i=0;i<_handlers.num();i++)
00295                                             _handlers[i]->handle_event(
00296                                                           Evd(this,MOV,mod));
00297                                             }
00298 
00299   public :
00300  static DEVice_2d  *last;
00301                 DEVice_2d(): _scale(1,1), _pressure(1) { }
00302    virtual     ~DEVice_2d()                 { }
00303 
00304    void         offset (mlib::CXYvec &v)          { _offset = v; }
00305    void         scale  (mlib::CXYvec &s)          { _scale = s;  }
00306 
00307    mlib::XYpt         cur    ()                   { return _cur; }
00308    mlib::XYpt         old    ()                   { return _old; }
00309    mlib::XYvec        delta  ()                   { return (_cur - _old); }
00310    void         set_cur(mlib::CXYpt    &p)        { _cur = p; }
00311    void         set_old(mlib::CXYpt    &p)        { _old = p; }
00312    void         set_val(mlib::CXYpt    &p)        { _old = _cur; _cur = p; }
00313    virtual mlib::XYpt map    (mlib::CXYpt    &p) 
00314       { return (mlib::XYpt(_scale[0]*p[0], _scale[1]*p[1]) + _offset); }
00315 
00316    // XXX - should be passed along w/ event() parameters.
00317    // as-is, pressure has to get set *before* event() is called
00318    void   set_pressure(double p)            { _pressure = p; }
00319    double pressure()                const   { return _pressure; }
00320 
00321    virtual void event_delta(mlib::CXYvec &v, Evd::DEVmod mod)
00322       { event(_cur+v, mod); }
00323    virtual void event      (mlib::CXYpt &p,  Evd::DEVmod mod)
00324       { last = this; if (map(p)!=_cur) _event(p,mod); }
00325 };
00326 
00327 class DEVice_2d_absrel : public DEVice_2d
00328 {
00329  protected:
00330   int         _rel_flag;
00331 
00332   int         _down_flag, _first_down;
00333   mlib::XYpt        _old_abs_pos;
00334   mlib::XYpt        _cur_abs_pos;
00335   mlib::XYpt        _logical_pos;
00336 
00337   virtual void    _event(mlib::CXYpt  &p, Evd::DEVmod mod);
00338 
00339  public:
00340   virtual        ~DEVice_2d_absrel() {}
00341                   DEVice_2d_absrel() : _rel_flag(false), _down_flag(true), 
00342                                        _first_down(true), 
00343                                        _old_abs_pos(0,0), _logical_pos(0,0)
00344                                        {}
00345 
00346           void    set_as_abs_device() { _rel_flag = false; }
00347           void    set_as_rel_device() { _rel_flag = true;  }
00348 
00349   virtual void    up();
00350   virtual void    down();
00351 };
00352 
00353 //----------------------------------------------
00354 // 
00355 //   DEVice_buttons -
00356 //      Generic button device
00357 //
00358 //----------------------------------------------
00359 class DEVice_buttons : public DEVice {
00360   protected:
00361    int _state;
00362    DEVice_2d *_dev2;
00363 
00364   public:
00365    enum        DEVbutton_action { UP, DOWN };
00366 
00367                DEVice_buttons()              : _state(0),_dev2(0) /*,_dev6(0)*/ {}
00368                DEVice_buttons(DEVice_2d *d2) : _state(0),_dev2(d2)/*,_dev6(0)*/ {}
00369    virtual    ~DEVice_buttons()             {}
00370 
00371    void        set_ptr(DEVice_2d *d)  { _dev2 = d; }
00372    DEVice_2d  *ptr2d()                { return _dev2; }
00373    int         get  (int i)           { return ((_state & (1<<i)) ? 1 : 0); }
00374    void        set  (int i, int b)    { if (b) _state |= (1<<i); 
00375                                          else   _state &= (~(1<<i)); }
00376 
00377    virtual void event(int num, DEVbutton_action action, Evd::DEVmod mod) {
00378         set(num,action);
00379         for (int i=0; i<_handlers.num(); i++)
00380           _handlers[i]->handle_event(Evd(this,
00381                               (DEVact)(num*2 + ((action == UP) ? 0 : 1)),mod));
00382    }
00383 };
00384 
00385 
00386 
00387 
00388 //------------------------------------------------------
00389 //
00390 //  DEVmod_desc - 
00391 //     Description of buttons states that are equivalent
00392 //  to a modifier key being pressed.  e.g., its the way to
00393 //  turn a button on a serial mouse (etc) into a control
00394 //  key.
00395 //
00396 //------------------------------------------------------
00397 #define CDEVmod_desc_list const DEVmod_desc_list
00398 #define CDEVmod_desc      const DEVmod_desc
00399 class DEVmod_desc {
00400    protected :
00401      Evd::DEVmod     _mod;
00402      DEVice_buttons *_ice;
00403      int             _ind;
00404    public :
00405      DEVmod_desc():_mod(Evd::NONE),_ice(0),_ind(-1)    { }
00406      DEVmod_desc(DEVice_buttons *ice, int btn_ind, Evd::DEVmod mod):
00407                   _mod(mod), _ice(ice), _ind(btn_ind)  { }
00408      DEVice_buttons *ice()                             { return _ice; }
00409      int             btn_ind()                         { return _ind; }
00410      Evd::DEVmod     mod()                             { return _mod; }
00411      int             operator == (CDEVmod_desc &d) const
00412           { return d._mod==_mod && d._ice==_ice && d._ind==_ind; }
00413 };
00414 typedef ARRAYptrs<DEVmod_desc> DEVmod_desc_list;
00415 
00416 
00417 //------------------------------------------------------
00418 //
00419 //  DEVmod_gen -
00420 //    Virtual base class for the object that determines 
00421 //  which window system modifiers are currently active.
00422 //
00423 //------------------------------------------------------
00424 class DEVmod_gen {
00425    static DEVmod_desc_list  _mods;
00426    static DEVmod_gen       *_gen;
00427    static Evd::DEVmod _forced_mods;
00428 
00429   public :
00430    virtual ~DEVmod_gen() {}
00431    static  void         set_gen(DEVmod_gen     *gen)  { _gen = gen; }
00432    static  void         force_mods(Evd::DEVmod  mods) { _forced_mods = mods; }
00433    static  Evd::DEVmod  force_mods()                  { return _forced_mods; }
00434    static  void         add_mod(CDEVmod_desc &desc)   { _mods += desc; }
00435    virtual Evd::DEVmod  gen_mods()                    { return Evd::EMPTY; }
00436 
00437    static  Evd::DEVmod  mods();
00438 };
00439 
00440 //
00441 // Base class for mice
00442 // 
00443 //
00444 //
00445 class Mouse {
00446  protected:
00447    int                 _stereo_view_flag;
00448 
00449    DEVice_2d           _pointer;
00450    DEVice_buttons      _buttons;
00451 
00452  public:
00453    virtual ~Mouse() {}
00454             Mouse() : _stereo_view_flag(0), _buttons(&_pointer) {}
00455 
00456    DEVice_2d        &pointer()                   { return _pointer; }
00457    DEVice_buttons   &buttons()                   { return _buttons; }
00458    virtual void      set_size(int, int) = 0;
00459 
00460    virtual void      stereo_view(int b)          { _stereo_view_flag = b; }
00461    virtual int       stereo_view()         const { return _stereo_view_flag; }
00462 
00463            void      add_handler(DEVhandler *h)  { _pointer.add_handler(h); 
00464                                                    _buttons.add_handler(h); }
00465 };
00466 
00467 #ifdef macosx
00468 #   define DEV_DFLT_SERIAL_PORT "placeholder"
00469 #elif sgi
00470 #   define DEV_DFLT_SERIAL_PORT "/dev/ttyd2"
00471 #elif linux
00472 #   define DEV_DFLT_SERIAL_PORT "/dev/ttyS1"
00473 #elif WIN32
00474 #   define DEV_DFLT_SERIAL_PORT "/dev/com1"
00475 #elif defined(_AIX) || defined(sun)
00476 #   define DEV_DFLT_SERIAL_PORT "/dev/ttya"
00477 #endif
00478 
00479 #endif

Generated on Mon Sep 18 11:39:29 2006 for jot by  doxygen 1.4.4