00001 #ifndef DEV_H
00002 #define DEV_H
00003
00004 #include "std/support.H"
00005 #include "mlib/points.H"
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111 class DEVice;
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124 enum DEVact {
00125 B1U = 0,
00126 B1D,
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,
00176 INACTIVE,
00177 MOV,
00178 KEYU,
00179 KEYD,
00180 SHIFT_U,
00181 SHIFT_D,
00182 CTL_U,
00183 CTL_D
00184 } ;
00185
00186
00187
00188
00189
00190
00191
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
00238
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
00253
00254
00255
00256
00257
00258 class DEVice {
00259 protected:
00260 ARRAY<DEVhandler *> _handlers;
00261
00262 public:
00263
00264
00265
00266
00267
00268
00269
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
00281
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
00317
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
00356
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) {}
00368 DEVice_buttons(DEVice_2d *d2) : _state(0),_dev2(d2) {}
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
00391
00392
00393
00394
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
00420
00421
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
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