|
|
1.1 root 1: #define NEED_EVENTS
2: #include "sun.h"
3: #include <stdio.h>
4: #include <sys/ioctl.h>
5: #include "Xproto.h"
6: #include "keysym.h"
7:
8: extern CARD8 *sunModMap[];
9: extern KeySymsRec sunKeySyms[];
10:
11: static void sunBell();
12: static void sunKbdCtrl();
13:
14: int sunKbdfd = -1;
15: static struct sgttyb sttymodes;
16: static struct ttydevb devmodes;
17: static struct tchars tcharmodes;
18: #define MAXNLINED 10
19: static int sunnld, linedstack[MAXNLINED];
20:
21: static void
22: sunsavemodes()
23: {
24: char filename[256], *p;
25: char *getenv();
26: int fd;
27:
28: if((p=getenv("HOME")) == 0)
29: return;
30: strcpy(filename, p);
31: strcat(filename, "/.sttymodes");
32: if ((fd = creat(filename, 0644)) < 0)
33: return;
34: ioctl(sunKbdfd, TIOCGETP, &sttymodes);
35: ioctl(sunKbdfd, TIOCGDEV, &devmodes);
36: ioctl(sunKbdfd, TIOCGETC, &tcharmodes);
37: write(fd, &sttymodes, sizeof(struct sgttyb));
38: write(fd, &devmodes, sizeof(struct ttydevb));
39: write(fd, &tcharmodes, sizeof(struct tchars));
40: close(fd);
41: sunnld = 0;
42: while ((linedstack[sunnld] = ioctl(sunKbdfd, FIOLOOKLD, 0)) >= 0) {
43: ioctl(sunKbdfd, FIOPOPLD, 0);
44: sunnld++;
45: }
46: }
47:
48: static void
49: sunresetmodes()
50: {
51: for (sunnld--; sunnld >= 0; sunnld--)
52: ioctl(sunKbdfd, FIOPUSHLD, &linedstack[sunnld]);
53: ioctl(sunKbdfd, TIOCSETP, &sttymodes);
54: ioctl(sunKbdfd, TIOCSDEV, &devmodes);
55: ioctl(sunKbdfd, TIOCSETC, &tcharmodes);
56: }
57:
58: int
59: sunKbdProc (pKeyboard, what)
60: DevicePtr pKeyboard; /* Keyboard to manipulate */
61: int what; /* What to do to it */
62: {
63: register int kbdFd;
64:
65: switch (what) {
66: case DEVICE_INIT:
67: if (pKeyboard != LookupKeyboardDevice()) {
68: ErrorF ("Cannot open non-system keyboard");
69: return (!Success);
70: }
71: if (sunKbdfd >= 0)
72: kbdFd = sunKbdfd;
73: else {
74: kbdFd = open ("/dev/console", 2);
75: if (kbdFd < 0) {
76: Error ("Opening /dev/console");
77: return (!Success);
78: }
79: sunKbdfd = kbdFd;
80: sunsavemodes();
81: }
82:
83: pKeyboard->on = FALSE;
84: InitKeyboardDeviceStruct(
85: pKeyboard, sunKeySyms, sunModMap[0], sunBell, sunKbdCtrl);
86: break;
87:
88: case DEVICE_ON:
89: AddEnabledDevice(sunKbdfd);
90: pKeyboard->on = TRUE;
91: break;
92:
93: case DEVICE_CLOSE:
94: case DEVICE_OFF:
95: sunresetmodes();
96: RemoveEnabledDevice(sunKbdfd);
97: pKeyboard->on = FALSE;
98: break;
99: }
100: return (Success);
101: }
102:
103: static void
104: sunBell (loudness, pKeyboard)
105: int loudness; /* Percentage of full volume */
106: DevicePtr pKeyboard; /* Keyboard to ring */
107: {
108: #define KBD_CMD_BELL 0x02 /* Turn on the bell */
109: #define KBD_CMD_NOBELL 0x03 /* Turn off the bell */
110: static char c[2] = { KBD_CMD_BELL, KBD_CMD_NOBELL};
111:
112: if (loudness)
113: write(sunKbdfd, c, 2);
114: }
115:
116: void
117: sunKbdEvent(pKeyboard)
118: DevicePtr pKeyboard;
119: {
1.1.1.2 ! root 120: register char c, *cp, *cp2;
1.1 root 121: register i;
122: char *tail;
1.1.1.2 ! root 123: int up;
1.1 root 124: static char rbuf[512];
1.1.1.2 ! root 125: static char keystate[128];
! 126: static int ndown;
1.1 root 127:
128: if ((i = read (sunKbdfd, rbuf, sizeof(rbuf))) < 0)
129: FatalError ("Could not read from keyboard");
130:
131: tail = &rbuf[i];
132: for (cp = rbuf; cp != tail; ) {
133: c = *cp++;
134: /*
1.1.1.2 ! root 135: * If idle, all keys should be up
1.1 root 136: */
1.1.1.2 ! root 137: if (c == 0x7f) {
! 138: if (ndown) {
! 139: for(cp2 = keystate; cp2 < &keystate[128]; cp2++)
! 140: if (*cp2) {
! 141: sunmkX(pKeyboard, KeyRelease, cp2 - keystate);
! 142: *cp2 = 0;
! 143: }
! 144: ndown = 0;
! 145: }
1.1 root 146: continue;
1.1.1.2 ! root 147: }
! 148: i = c & 0x7f;
! 149: up = c & 0x80;
! 150: if (up) {
! 151: if (keystate[i]) {
! 152: sunmkX(pKeyboard, KeyRelease, i);
! 153: keystate[i] = 0;
! 154: ndown--;
! 155: } else {
! 156: sunmkX(pKeyboard, KeyPress, i);
! 157: sunmkX(pKeyboard, KeyRelease, i);
! 158: }
! 159: } else {
! 160: if (keystate[i]) {
! 161: sunmkX(pKeyboard, KeyRelease, i);
! 162: sunmkX(pKeyboard, KeyPress, i);
! 163: } else {
! 164: sunmkX(pKeyboard, KeyPress, i);
! 165: keystate[i] = 1;
! 166: ndown++;
! 167: }
! 168: }
1.1 root 169: }
170:
171: }
172:
173: static void
174: sunKbdCtrl (pKeyboard, ctrl)
175: DevicePtr pKeyboard; /* Keyboard to alter */
176: KeybdCtrl *ctrl;
177: {
178: char c;
179:
180: #define KBD_CMD_CLICK 0x0A /* Turn on the click annunciator */
181: #define KBD_CMD_NOCLICK 0x0B /* Turn off the click annunciator */
182: if (ctrl->click == 0) { /* turn click off */
183: c = KBD_CMD_NOCLICK;
184: write(sunKbdfd, &c, 1);
185: } else {
186: c = KBD_CMD_CLICK;
187: write(sunKbdfd, &c, 1);
188: }
189: }
190:
191: Bool
192: LegalModifier(key)
193: {
194: return (TRUE);
195: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.