1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14:
15:
16: 17: 18: 19: 20: 21:
22:
23: #include "kbpd.h"
24:
25: 26: 27:
28: LOCAL ER setKpState( KPStat *state )
29: {
30: ER err;
31:
32: 33: 34: 35:
36: err = kpMovePointerAndNotify(state->xpos, state->ypos);
37:
38: return err;
39: }
40:
41: 42: 43:
44: LOCAL ER setKpMetaBut( MetaBut meta[2] )
45: {
46: const UW KBMET = ES_ALPH|ES_KANA
47: |ES_LSHFT|ES_RSHFT|ES_EXT|ES_CMD
48: |ES_LLSHFT|ES_LRSHFT|ES_LEXT|ES_LCMD
49: |ES_TLSHFT|ES_TRSHFT|ES_TEXT|ES_TCMD
50: |ES_HAN|ES_KBSEL;
51: const UW PDBUT = ES_BUT|ES_BUT2;
52: const UW PDMOV = ES_NODSP;
53: const UW PDSIM = ES_PDSIM;
54: UW stat, chg;
55: ER err, error = E_OK;
56: union {
57: MetaBut stat;
58: UW uw;
59: } u;
60:
61: u.stat = kpMgrInfo.kpState.stat;
62: stat = (u.uw & *(UW*)&meta[0]) | *(UW*)&meta[1];
63: chg = stat ^ u.uw;
64:
65: if ( (chg & KBMET) != 0 ) {
66:
67: u.stat = kpMgrInfo.kpState.stat;
68: u.uw ^= chg & KBMET;
69: kpMgrInfo.kpState.stat = u.stat;
70:
71: if ( (chg & (ES_ALPH|ES_KANA)) != 0 ) {
72:
73: err = kpChangeKbInputMode(kpMgrInfo.kpState.stat.mode);
74: if ( err < E_OK ) error = err;
75: }
76:
77: err = kpNotifyMetaEvent();
78: if ( err < E_OK ) error = err;
79: }
80:
81: if ( (chg & (PDBUT|PDMOV)) != 0 ) {
82:
83: u.stat = kpMgrInfo.kpState.stat;
84: u.uw ^= chg & (PDBUT|PDMOV);
85: kpMgrInfo.kpState.stat = u.stat;
86:
87: err = kpNotifyPdEvent(
88: ( (chg & PDBUT) != 0 )? TDE_PDBUT: TDE_PDMOVE,
89: kpMgrInfo.kpState.stat.nodsp);
90: if ( err < E_OK ) error = err;
91: }
92:
93: if ( (chg & PDSIM) != 0 ) {
94:
95: u.stat = kpMgrInfo.kpState.stat;
96: u.uw ^= chg & PDSIM;
97: kpMgrInfo.kpState.stat = u.stat;
98:
99: err = kpNotifyPdEvent(TDE_PDSTATE,
100: kpMgrInfo.kpState.stat.nodsp);
101: if ( err < E_OK ) error = err;
102: }
103:
104: return error;
105: }
106:
107: 108: 109: 110:
111: Inline BOOL SelKbDef( UW *kbsel, UW *kid, W datano )
112: {
113: if ( datano <= DN_KEYDEF_S && datano >= DN_KEYDEF_E ) {
114: *kbsel = 0;
115: *kid = DN_KEYDEF_S - datano;
116: return TRUE;
117: }
118: if ( datano <= DN_KEYDEF2_S && datano >= DN_KEYDEF2_E ) {
119: *kbsel = 1;
120: *kid = DN_KEYDEF2_S - datano;
121: return TRUE;
122: }
123: return FALSE;
124: }
125:
126: 127: 128: 129: 130:
131: LOCAL ER defineKeyboard( UW kbsel, UW kid,
132: W keytopofs, KeyTab *keytab, W keytabsz )
133: {
134: KbDef *kbdef = GetKbDef(kbsel, kid);
135: ER err;
136:
137: if ( keytabsz <= 0 ) {
138:
139: if ( kbdef == NULL ) { err = E_NOEXS; goto err_ret; }
140: } else {
141:
142: if ( keytabsz < offsetof(KeyTab, kct)
143: || !(keytab->keymax > 0 && keytab->keymax <= KEYMAX)
144: || !(keytab->kctmax > 0 && keytab->kctmax <= KCTSEL)
145: || !(keytabsz >= keytab->keymax * keytab->kctmax * sizeof(UH)
146: + offsetof(KeyTab, kct)) )
147: { err = E_PAR; goto err_ret; }
148:
149: 150:
151: if ( kbdef == NULL && keytopofs < 0 ) keytopofs = 0;
152: }
153:
154: if ( keytabsz > 0 ) {
155:
156: kbdef = Vrealloc(kbdef,
157: offsetof(KbDef, keyDef.keytab) + keytabsz);
158: if ( kbdef == NULL ) { err = E_NOMEM; goto err_ret; }
159: kbdef->size = offsetof(KeyDef, keytab) + keytabsz;
160: SetKbDef(kbsel, kid, kbdef);
161:
162: MEMCPY(&kbdef->keyDef.keytab, keytab, keytabsz);
163: }
164:
165: if ( keytopofs >= 0 ) {
166:
167: kbdef->keyDef.keytopofs = keytopofs;
168: }
169:
170: return E_OK;
171:
172: err_ret:
173: DEBUG_PRINT(("defineKeyboard err = %d\n", err));
174: return err;
175: }
176:
177: 178: 179:
180: LOCAL ER setKeyDef( KeyDef *keydef, UW kbsel, UW kid, W datacnt )
181: {
182: ER err;
183:
184: if ( datacnt >= offsetof(KeyDef, keytab.kctmax)
185: && keydef->keytab.keymax == 0 ) {
186:
187: KbDef *kbdef = GetKbDef(kbsel, kid);
188: if ( kbdef != NULL ) {
189: SetKbDef(kbsel, kid, NULL);
190: Vfree(kbdef);
191: }
192: } else {
193:
194: if ( (datacnt -= offsetof(KeyDef, keytab)) < 0 )
195: return E_PAR;
196: err = defineKeyboard(kbsel, kid,
197: keydef->keytopofs, &keydef->keytab, datacnt);
198: if ( err < E_OK ) return err;
199: }
200:
201: return E_OK;
202: }
203:
204: 205: 206:
207: LOCAL ER setKeyTab( KeyTab *table, W datacnt )
208: {
209: return defineKeyboard(0, kpMgrInfo.kb.keyID, -1, table, datacnt);
210: }
211:
212: 213: 214:
215: LOCAL ER setKeyID( UW kid )
216: {
217:
218: if ( kid < 0 || kid > MAX_KID ) return E_PAR;
219:
220: kpMgrInfo.kb.keyID = kid;
221: kpMgrInfo.kb.defKeyID = 2;
222:
223: return E_OK;
224: }
225:
226: 227: 228:
229: LOCAL ER setKeyMode( KeyMode *mode )
230: {
231: #define settime(time) \
232: if ( mode->time >= 0 ) { \
233: kpMgrInfo.kb.keyMode.time \
234: = min(mode->time, KB_MAXTIME); \
235: }
236:
237:
238: settime(ontime);
239: settime(offtime);
240: settime(invtime);
241: settime(contime);
242: settime(sclktime);
243: settime(dclktime);
244: kpMgrInfo.kb.keyMode.tslock = mode->tslock;
245:
246: return E_OK;
247:
248: #undef settime
249: }
250:
251: 252: 253:
254: LOCAL ER setPdMode( PdMode *mode )
255: {
256: #define settime(time) \
257: if ( mode->time >= 0 ) { \
258: kpMgrInfo.pd.pdMode.time \
259: = min(mode->time, PD_MAXTIME); \
260: }
261:
262: union {
263: PdAttr attr;
264: UW uw;
265: } u;
266:
267:
268: settime(ontime);
269: settime(offtime);
270: settime(invtime);
271: settime(timeout);
272: kpMgrInfo.pd.pdMode.attr = mode->attr;
273:
274:
275: kpChangePdScanRate(mode->attr.rate);
276:
277:
278: u.attr = mode->attr;
279: kpChangePdSense(u.uw & (PD_ACMSK|PD_ABS|PD_SNMSK));
280:
281: return E_OK;
282:
283: #undef settime
284: }
285:
286: 287: 288:
289: LOCAL ER setPdRange( PdRange *range )
290: {
291: BOOL move;
292: ER err = E_OK;
293:
294:
295: if ( range->xmax < 0 || range->ymax < 0 )
296: return E_PAR;
297:
298:
299: kpMgrInfo.pd.pdRange = *range;
300:
301:
302: move = kpMovePointer(kpMgrInfo.kpState.xpos, kpMgrInfo.kpState.ypos);
303:
304: if ( move ) {
305:
306: err = kpNotifyPdEvent(TDE_PDMOVE, 0);
307: }
308:
309: return err;
310: }
311:
312: 313: 314:
315: LOCAL ER setPdSimSpeed( W simSpeed )
316: {
317:
318: if ( !(simSpeed >= 0 && simSpeed <= 15) )
319: return E_PAR;
320:
321:
322: kpMgrInfo.pd.pdSimSpeed = simSpeed;
323:
324: if ( simSpeed == 0 ) {
325:
326: kpMgrInfo.kpState.stat.pdsim = 0;
327: }
328:
329: return E_OK;
330: }
331:
332: 333: 334:
335: LOCAL ER setPdSimInh( BOOL inhibit )
336: {
337:
338: kpMgrInfo.pd.pdSimInh = inhibit;
339:
340: return E_OK;
341: }
342:
343:
344:
345: 346: 347:
348: LOCAL INT readData( ID devid, INT datano, INT datacnt, void *buf, SDI sdi )
349: {
350: #define setAddrAndSize(var) addr = &(var); size = sizeof(var)
351:
352: void *addr;
353: W size;
354: KbDef *kbdef;
355: UW kbsel, kid;
356:
357:
358: if ( datacnt < 0 ) {
359: DEBUG_PRINT(("readData, datacnt(%d) err\n", datacnt));
360: return E_PAR;
361: }
362:
363: switch ( datano ) {
364: case DN_KPEVENT:
365: setAddrAndSize(kpMgrInfo.eventMbf);
366: break;
367: case DN_KPSTAT:
368: setAddrAndSize(kpMgrInfo.kpState);
369: break;
370: case DN_KEYMAP:
371: setAddrAndSize(kpMgrInfo.kb.keyMap);
372: break;
373: case DN_KEYTAB:
374: kbdef = GetKbDef(0, kpMgrInfo.kb.keyID);
375: if ( kbdef == NULL ) return E_NOEXS;
376: addr = &kbdef->keyDef.keytab;
377: size = kbdef->size - offsetof(KeyDef, keytab);
378: break;
379: case DN_KEYMODE:
380: setAddrAndSize(kpMgrInfo.kb.keyMode);
381: break;
382: case DN_PDMODE:
383: setAddrAndSize(kpMgrInfo.pd.pdMode);
384: break;
385: case DN_PDRANGE:
386: setAddrAndSize(kpMgrInfo.pd.pdRange);
387: break;
388: case DN_PDSIM:
389: setAddrAndSize(kpMgrInfo.pd.pdSimSpeed);
390: break;
391: case DN_PDSIMINH:
392: setAddrAndSize(kpMgrInfo.pd.pdSimInh);
393: break;
394: case DN_KPINPUT:
395: setAddrAndSize(kpMgrInfo.dataMbx);
396: break;
397: case DN_KEYID:
398: setAddrAndSize(kpMgrInfo.kb.keyID);
399: break;
400:
401: default:
402: if ( SelKbDef(&kbsel, &kid, datano) ) {
403:
404: kbdef = GetKbDef(kbsel, kid);
405: if ( kbdef == NULL ) return E_NOEXS;
406: addr = &kbdef->keyDef;
407: size = kbdef->size;
408: } else {
409:
410: DEBUG_PRINT(("readData, datano(%d) err\n", datano));
411: return E_PAR;
412: }
413: }
414:
415: if ( datacnt > 0 ) {
416:
417: if ( datacnt < size ) size = datacnt;
418: MEMCPY(buf, addr, size);
419: }
420: return size;
421:
422: #undef setAddrAndSize
423: }
424:
425: EXPORT INT kpReadFn( ID devid, INT datano, INT datacnt, void *buf, SDI sdi )
426: {
427: INT er;
428:
429: Lock(&kpMgrInfo.lock);
430: er = readData(devid, datano, datacnt, buf, sdi);
431: Unlock(&kpMgrInfo.lock);
432:
433: return er;
434: }
435:
436: 437: 438:
439: LOCAL INT writeData( ID devid, INT datano, INT datacnt, void *buf, SDI sdi )
440: {
441: W size;
442: KbDef *kbdef;
443: UW kbsel, kid;
444: ER err = E_OK;
445:
446:
447: if ( datacnt < 0 ) {
448: DEBUG_PRINT(("writeData, datacnt(%d) err\n", datacnt));
449: return E_PAR;
450: }
451:
452: kbsel = kid = 0;
453: switch ( datano ) {
454: case DN_KPEVENT:
455: size = sizeof(kpMgrInfo.eventMbf);
456: break;
457: case DN_KPSTAT:
458: size = sizeof(kpMgrInfo.kpState);
459: break;
460: case DN_KEYTAB:
461: kbdef = GetKbDef(0, kpMgrInfo.kb.keyID);
462: size = ( kbdef == NULL )?
463: 0: kbdef->size - offsetof(KeyDef, keytab);
464: break;
465: case DN_KEYMODE:
466: size = sizeof(kpMgrInfo.kb.keyMode);
467: break;
468: case DN_PDMODE:
469: size = sizeof(kpMgrInfo.pd.pdMode);
470: break;
471: case DN_PDRANGE:
472: size = sizeof(kpMgrInfo.pd.pdRange);
473: break;
474: case DN_PDSIM:
475: size = sizeof(kpMgrInfo.pd.pdSimSpeed);
476: break;
477: case DN_PDSIMINH:
478: size = sizeof(kpMgrInfo.pd.pdSimInh);
479: break;
480: case DN_KEYID:
481: size = sizeof(kpMgrInfo.kb.keyID);
482: break;
483: case DN_KPMETABUT:
484: size = sizeof(MetaBut) * 2;
485: break;
486:
487: case DN_KEYMAP:
488: case DN_KPINPUT:
489:
490: DEBUG_PRINT(("writeData, read only\n"));
491: return E_PAR;
492:
493: default:
494: if ( SelKbDef(&kbsel, &kid, datano) ) {
495:
496: kbdef = GetKbDef(kbsel, kid);
497: size = ( kbdef == NULL )? 0: kbdef->size;
498: } else {
499:
500: DEBUG_PRINT(("writeData, datano(%d) err\n", datano));
501: return E_PAR;
502: }
503: }
504:
505: if ( datacnt > 0 ) {
506: if ( datano != DN_KEYTAB
507: && !(datano <= DN_KEYDEF_S && datano >= DN_KEYDEF_E)
508: && !(datano <= DN_KEYDEF2_S && datano >= DN_KEYDEF2_E) ) {
509:
510: if ( datacnt < size ) return E_PAR;
511: }
512:
513:
514: switch ( datano ) {
515: case DN_KPEVENT:
516: kpMgrInfo.eventMbf = *(ID*)buf;
517: break;
518: case DN_KPSTAT:
519: err = setKpState((KPStat*)buf);
520: break;
521: case DN_KEYTAB:
522: err = setKeyTab((KeyTab*)buf, datacnt);
523: break;
524: case DN_KEYMODE:
525: err = setKeyMode((KeyMode*)buf);
526: break;
527: case DN_PDMODE:
528: err = setPdMode((PdMode*)buf);
529: break;
530: case DN_PDRANGE:
531: err = setPdRange((PdRange*)buf);
532: break;
533: case DN_PDSIM:
534: err = setPdSimSpeed(*(W*)buf);
535: break;
536: case DN_PDSIMINH:
537: err = setPdSimInh(*(BOOL*)buf);
538: break;
539: case DN_KEYID:
540: err = setKeyID(*(UW*)buf);
541: break;
542: case DN_KPMETABUT:
543: err = setKpMetaBut((MetaBut*)buf);
544: break;
545: default:
546: err = setKeyDef((KeyDef*)buf,
547: kbsel, kid, datacnt);
548: }
549: }
550: if ( err != E_OK ) {
551: DEBUG_PRINT(("writeData, write err = %d\n", err));
552: return err;
553: }
554: return size;
555: }
556:
557: EXPORT INT kpWriteFn( ID devid, INT datano, INT datacnt, void *buf, SDI sdi )
558: {
559: INT er;
560:
561: Lock(&kpMgrInfo.lock);
562: er = writeData(devid, datano, datacnt, buf, sdi);
563: Unlock(&kpMgrInfo.lock);
564:
565: return er;
566: }
567:
568:
569:
570: 571: 572:
573: LOCAL ER suspend( void )
574: {
575: ER err, error = E_OK;
576:
577:
578: err = kpSendDeviceCommand(SuspendKBPD);
579: if ( err < E_OK ) error = err;
580:
581:
582: err = kpKeyAndButtonForceUp();
583: if ( err < E_OK ) error = err;
584:
585: kpMgrInfo.suspended = TRUE;
586:
587: DO_DEBUG(
588: if ( error < E_OK ) DEBUG_PRINT(("suspend err = %d\n", error));
589: )
590: return error;
591: }
592:
593: 594: 595:
596: LOCAL ER resume( void )
597: {
598: ER err, error = E_OK;
599:
600:
601: if ( !kpMgrInfo.suspended ) return E_OK;
602:
603:
604: err = kpSendDeviceCommand(ResumeKBPD);
605: if ( err < E_OK ) error = err;
606:
607:
608: err = kpSetAllDeviceStatus();
609: if ( err < E_OK ) error = err;
610:
611: kpMgrInfo.suspended = FALSE;
612:
613: DO_DEBUG(
614: if ( error < E_OK ) DEBUG_PRINT(("resume err = %d\n", error));
615: )
616: return error;
617: }
618:
619:
620:
621: 622: 623:
624: EXPORT INT kpEventFn( INT evttyp, void *evtinf, SDI sdi )
625: {
626: ER err;
627:
628: Lock(&kpMgrInfo.lock);
629:
630: switch ( evttyp ) {
631: case TDV_SUSPEND:
632: err = suspend();
633: break;
634:
635: case TDV_RESUME:
636: err = resume();
637: break;
638:
639: default:
640:
641: err = E_PAR;
642: DEBUG_PRINT(("evttyp(%d) err\n", evttyp));
643: }
644:
645: Unlock(&kpMgrInfo.lock);
646: return err;
647: }
648: