History

Merging echoline's android and fbdev forks

Lorenzo Bivens committed 5 years ago

Commit 96209041 parent a130d4427 files · +2398 −0Browse files

Showing the changes to gui-fbdev/fbdev.c (1 of 27 files). Show all

Unified · Split

gui-fbdev/fbdev.c+673−0
@@ -0,0 +1,673 @@
1+#include "u.h"
2+#include "lib.h"
3+#include "dat.h"
4+#include "fns.h"
5+#include "error.h"
6+
7+#include <draw.h>
8+#include <memdraw.h>
9+#include <keyboard.h>
10+#include <cursor.h>
11+#include "screen.h"
12+
13+#undef long
14+#undef ulong
15+
16+#include <linux/fb.h>
17+#include <linux/input.h>
18+
19+uchar* fbp;
20+Memimage* screenimage;
21+Memimage* backbuf;
22+Rectangle screenr;
23+char* snarfbuf;
24+struct fb_fix_screeninfo finfo;
25+struct fb_var_screeninfo vinfo;
26+int *eventfds = NULL;
27+int neventfds;
28+Point mousexy;
29+char shift_state;
30+int ttyfd;
31+char* tty;
32+char hidden;
33+int devicesfd;
34+ulong chan;
35+int depth;
36+
37+#include <sys/ioctl.h>
38+#include <sys/mman.h>
39+#include <limits.h>
40+
41+#include <fcntl.h>
42+#include <termios.h>
43+#include <poll.h>
44+#include <sys/ioctl.h>
45+#include <linux/keyboard.h>
46+#include <signal.h>
47+
48+#include <termios.h>
49+
50+#define ulong p9_ulong
51+
52+int code2key[] = {
53+ Kesc, '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-', '=', '\x08',
54+ '\x09', 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', '[', ']', '\n',
55+ Kctl, 'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';', '\'', '`', Kshift,
56+ '\\', 'z', 'x', 'c', 'v', 'b', 'n', 'm', ',', '.', '/', Kshift, '*', Kalt,
57+ ' ', Kcaps, KF|1, KF|2, KF|3, KF|4, KF|5, KF|6, KF|7, KF|8, KF|9, KF|10,
58+ Knum, Kscroll,
59+ '7', '8', '9', '-', '4', '5', '6', '+', '1', '2', '3', '0', '.',
60+};
61+
62+int code2key_shift[] = {
63+ Kesc, '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '_', '+', '\x08',
64+ '\x09', 'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P', '{', '}', '\n',
65+ Kctl, 'A', 'S', 'D', 'F', 'G', 'H', 'J', 'K', 'L', ':', '\"', '~', Kshift,
66+ '|', 'Z', 'X', 'C', 'V', 'B', 'N', 'M', '<', '>', '?', Kshift, '*', Kalt,
67+ ' ', Kcaps, KF|1, KF|2, KF|3, KF|4, KF|5, KF|6, KF|7, KF|8, KF|9, KF|10,
68+ Knum, Kscroll,
69+ '7', '8', '9', '-', '4', '5', '6', '+', '1', '2', '3', '0', '.',
70+};
71+
72+Memimage *gscreen;
73+char *snarfbuf = nil;
74+
75+int onevent(struct input_event*);
76+void termctl(uint32_t o, int or);
77+void ctrlc(int sig);
78+
79+void
80+_fbput(Memimage *m, Rectangle r) {
81+ int y;
82+
83+ for (y = r.min.y; y < r.max.y; y++){
84+ long loc = y * finfo.line_length + r.min.x * depth;
85+ void *ptr = m->data->bdata + y * m->width * 4 + r.min.x * depth;
86+
87+ memcpy(fbp + loc, ptr, Dx(r) * depth);
88+ }
89+}
90+
91+Memimage*
92+fbattach(int fbdevidx)
93+{
94+ Rectangle r;
95+ char devname[64];
96+ size_t size;
97+ int fd;
98+
99+ /*
100+ * Connect to /dev/fb0
101+ */
102+ snprintf(devname, sizeof(devname) - 1, "/dev/fb%d", fbdevidx);
103+ if ((fd = open(devname, O_RDWR)) < 0)
104+ goto err;
105+
106+ if (ioctl(fd, FBIOGET_VSCREENINFO, &(vinfo)) < 0)
107+ goto err;
108+
109+ switch (vinfo.bits_per_pixel) {
110+ case 32:
111+ chan = XRGB32;
112+ depth = 4;
113+ break;
114+ case 16:
115+ chan = RGB16;
116+ depth = 2;
117+ break;
118+ default:
119+ goto err;
120+ }
121+
122+ if (ioctl(fd, FBIOGET_FSCREENINFO, &(finfo)) < 0)
123+ goto err;
124+
125+ size = vinfo.yres_virtual * finfo.line_length;
126+ if ((fbp = mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, (off_t)0)) < 0)
127+ goto err;
128+ /*
129+ * Figure out underlying screen format.
130+ */
131+ r = Rect(0, 0, vinfo.xres_virtual, vinfo.yres_virtual);
132+
133+ screenr = r;
134+
135+ screenimage = allocmemimage(r, chan);
136+ backbuf = allocmemimage(r, chan);
137+ return backbuf;
138+
139+err:
140+ return nil;
141+}
142+
143+int
144+eventattach()
145+{
146+ char eventfile[PATH_MAX] = "";
147+ char line[PATH_MAX];
148+ FILE *devices;
149+ char *ptr;
150+
151+ neventfds = 0;
152+ devices = fopen("/proc/bus/input/devices", "r");
153+ if (devices == NULL)
154+ return -1;
155+ while (fgets(line, sizeof(line)-1, devices) != NULL)
156+ if (line[0] == 'H') {
157+ ptr = strstr(line, "event");
158+ if (ptr == NULL)
159+ continue;
160+ ptr[strcspn(ptr, " \r\n")] = '\0';
161+ snprintf(eventfile, sizeof(eventfile)-1, "/dev/input/%s", ptr);
162+ neventfds++;
163+ eventfds = realloc(eventfds, neventfds * sizeof(int));
164+ eventfds[neventfds-1] = open(eventfile, O_RDONLY);
165+ if (eventfds[neventfds-1] < 0)
166+ neventfds--;
167+ }
168+ fclose(devices);
169+
170+ if (neventfds == 0)
171+ return -1;
172+ return 1;
173+}
174+
175+void
176+flushmemscreen(Rectangle r)
177+{
178+ int x, y, i;
179+ Point p;
180+ long fbloc;
181+ int x2, y2;
182+
183+ if (rectclip(&r, screenimage->r) == 0)
184+ return;
185+ if (Dx(r) == 0 || Dy(r) == 0)
186+ return;
187+
188+ assert(!canqlock(&drawlock));
189+
190+ memimagedraw(screenimage, r, backbuf, r.min, nil, r.min, S);
191+
192+ if (hidden != 0)
193+ return;
194+
195+ p = mousexy;
196+
197+ // draw cursor
198+ for (x = 0; x < 16; x++) {
199+ x2 = x + cursor.offset.x;
200+
201+ if ((p.x + x2) < 0)
202+ continue;
203+
204+ if ((p.x + x2) >= screenimage->r.max.x)
205+ break;
206+
207+ for (y = 0; y < 16; y++) {
208+ y2 = y + cursor.offset.y;
209+
210+ if ((p.y + y2) < 0)
211+ continue;
212+
213+ if ((p.y + y2) >= screenimage->r.max.y)
214+ break;
215+
216+ i = y * 2 + x / 8;
217+ fbloc = ((p.y+y2) * screenimage->r.max.x + (p.x+x2)) * depth;
218+
219+ if (cursor.clr[i] & (128 >> (x % 8))) {
220+ switch (depth) {
221+ case 2:
222+ *((uint16_t*)(screenimage->data->bdata + fbloc)) = 0xFFFF;
223+ break;
224+ case 4:
225+ *((uint32_t*)(screenimage->data->bdata + fbloc)) = 0xFFFFFFFF;
226+ break;
227+ }
228+ }
229+
230+ if (cursor.set[i] & (128 >> (x % 8))) {
231+ switch (depth) {
232+ case 2:
233+ *((uint16_t*)(screenimage->data->bdata + fbloc)) = 0x0000;
234+ break;
235+ case 4:
236+ *((uint32_t*)(screenimage->data->bdata + fbloc)) = 0xFF000000;
237+ break;
238+ }
239+ }
240+ }
241+ }
242+
243+ _fbput(screenimage, r);
244+}
245+
246+static void
247+fbproc(void *v)
248+{
249+ struct input_event data;
250+ char buf[32];
251+ struct pollfd *pfd;
252+ int r;
253+ int ioctlarg;
254+
255+ pfd = calloc(3, sizeof(struct pollfd));
256+ pfd[0].fd = ttyfd; // for virtual console switches
257+ pfd[0].events = POLLPRI;
258+ pfd[1].fd = 0; // stdin goes to nowhere
259+ pfd[1].events = POLLIN;
260+ pfd[2].fd = open("/proc/bus/input/devices", O_RDONLY); // input hotplug
261+ if (pfd[2].fd < 0)
262+ panic("cannot open /proc/bus/input/devices: %r");
263+ pfd[2].events = POLLIN;
264+
265+TOP:
266+ while(read(pfd[2].fd, buf, 31) > 0);
267+
268+ pfd = realloc(pfd, sizeof(struct pollfd) * (neventfds + 3));
269+ for (r = 0; r < neventfds; r++) {
270+ pfd[r+3].fd = eventfds[r];
271+ pfd[r+3].events = POLLIN;
272+ }
273+
274+ for(;;) {
275+ shift_state = 6;
276+ if (ioctl(0, TIOCLINUX, &shift_state) < 0)
277+ panic("ioctl TIOCLINUX 6: %r");
278+
279+ r = poll(pfd, 3+neventfds, -1);
280+ if (r < 0)
281+ oserror();
282+ if (pfd[0].revents & POLLPRI) {
283+ if ((r = read(ttyfd, buf, 31)) <= 0)
284+ panic("ttyfd read: %r");
285+ buf[r] = '\0';
286+ if (strcmp(buf, tty) == 0) {
287+ hidden = 0;
288+ printf("\e[?25l");
289+ fflush(stdout);
290+ qlock(&drawlock);
291+ flushmemscreen(gscreen->clipr);
292+ qunlock(&drawlock);
293+ }
294+ else
295+ hidden = 1;
296+ close(ttyfd);
297+ ttyfd = open("/sys/class/tty/tty0/active", O_RDONLY);
298+ if (ttyfd < 0)
299+ panic("cannot open tty active fd: %r");
300+ pfd[0].fd = ttyfd;
301+ read(ttyfd, buf, 0);
302+ }
303+ if (pfd[1].revents & POLLIN)
304+ read(pfd[1].fd, buf, 31);
305+ if (pfd[2].revents & POLLIN) {
306+ for (r = 0; r < neventfds; r++)
307+ close(eventfds[r]);
308+ if(eventattach() < 0) {
309+ panic("cannot open event files: %r");
310+ }
311+ goto TOP;
312+ }
313+ for (r = 0; r < neventfds; r++)
314+ if (pfd[r+3].revents & POLLIN) {
315+ if (read(pfd[r+3].fd, &data, sizeof(data)) != sizeof(data))
316+ panic("eventfd read: %r");
317+ if (onevent(&data) == 0) {
318+ ioctlarg = 15;
319+ if (ioctl(0, TIOCLINUX, &ioctlarg) != 0) {
320+ ioctlarg = 4;
321+ ioctl(0, TIOCLINUX, &ioctlarg);
322+ qlock(&drawlock);
323+ flushmemscreen(gscreen->clipr);
324+ qunlock(&drawlock);
325+ } else {
326+ write(1, "\033[9;30]", 7);
327+ }
328+ }
329+ }
330+ }
331+
332+ printf("\e[?25h");
333+ fflush(stdout);
334+ termctl(ECHO, 1);
335+ free(pfd);
336+}
337+
338+void
339+screensize(Rectangle r, ulong chan)
340+{
341+ gscreen = backbuf;
342+ gscreen->clipr = ZR;
343+}
344+
345+void
346+screeninit(void)
347+{
348+ int r;
349+ char buf[1];
350+
351+ // set up terminal
352+ printf("\e[?25l");
353+ fflush(stdout);
354+ termctl(~(ICANON|ECHO), 0);
355+ signal(SIGINT, ctrlc);
356+
357+ memimageinit();
358+
359+ // tty switching
360+ ttyfd = open("/sys/class/tty/tty0/active", O_RDONLY);
361+ if (ttyfd >= 0) {
362+ tty = malloc(32);
363+ r = read(ttyfd, tty, 31);
364+ if (r >= 0)
365+ tty[r] = '\0';
366+ else
367+ tty[0] = '\0';
368+ close(ttyfd);
369+ ttyfd = open("/sys/class/tty/tty0/active", O_RDONLY);
370+ }
371+ if (ttyfd < 0)
372+ panic("cannot open tty active fd: %r");
373+ read(ttyfd, buf, 0);
374+ hidden = 0;
375+
376+ if(fbattach(0) == nil) {
377+ panic("cannot open framebuffer: %r");
378+ }
379+
380+ if(eventattach() < 0) {
381+ panic("cannot open event files: %r");
382+ }
383+
384+ screensize(screenr, chan);
385+ if (gscreen == nil)
386+ panic("screensize failed");
387+
388+ gscreen->clipr = screenr;
389+ kproc("fbdev", fbproc, nil);
390+
391+ terminit();
392+
393+ qlock(&drawlock);
394+ flushmemscreen(gscreen->clipr);
395+ qunlock(&drawlock);
396+}
397+
398+Memdata*
399+attachscreen(Rectangle *r, ulong *chan, int *depth, int *width, int *softscreen)
400+{
401+ *r = gscreen->clipr;
402+ *chan = gscreen->chan;
403+ *depth = gscreen->depth;
404+ *width = gscreen->width;
405+ *softscreen = 1;
406+
407+ gscreen->data->ref++;
408+ return gscreen->data;
409+}
410+
411+void
412+getcolor(ulong i, ulong *r, ulong *g, ulong *b)
413+{
414+ ulong v;
415+
416+ v = cmap2rgb(i);
417+ *r = (v>>16)&0xFF;
418+ *g = (v>>8)&0xFF;
419+ *b = v&0xFF;
420+}
421+
422+void
423+setcolor(ulong i, ulong r, ulong g, ulong b)
424+{
425+ /* no-op */
426+ return;
427+}
428+
429+char*
430+clipread(void)
431+{
432+ if(snarfbuf)
433+ return strdup(snarfbuf);
434+ return nil;
435+}
436+
437+int
438+clipwrite(char *buf)
439+{
440+ if(snarfbuf)
441+ free(snarfbuf);
442+ snarfbuf = strdup(buf);
443+ return 0;
444+}
445+
446+void
447+guimain(void)
448+{
449+ cpubody();
450+}
451+
452+void
453+termctl(uint32_t o, int or)
454+{
455+ struct termios t;
456+
457+ tcgetattr(0, &t);
458+ if (or)
459+ t.c_lflag |= o;
460+ else
461+ t.c_lflag &= o;
462+ tcsetattr(0, TCSANOW, &t);
463+}
464+
465+void
466+ctrlc(int sig) {
467+}
468+
469+int
470+onevent(struct input_event *data)
471+{
472+ Rectangle old, new;
473+ ulong msec;
474+ static int buttons;
475+ static Point coord;
476+ static char touched;
477+ static Point startmousept;
478+ static Point startpt;
479+ int key;
480+ static ulong lastmsec = 0;
481+
482+ if (hidden != 0)
483+ return -1;
484+
485+ msec = ticks();
486+
487+ old.min = mousexy;
488+ old.max = addpt(old.min, Pt(16, 16));
489+
490+ buttons &= ~0x18;
491+
492+ switch(data->type) {
493+ case 3:
494+ switch(data->code) {
495+ case 0:
496+ coord.x = data->value;
497+ break;
498+ case 1:
499+ coord.y = data->value;
500+ break;
501+ case 0x18:
502+ case 0x1c:
503+ if (data->value == 0)
504+ touched = 0;
505+ else if (data->value > 24) {
506+ touched = 1;
507+ startmousept = coord;
508+ startpt = mousexy;
509+ }
510+ break;
511+ default:
512+ return -1;
513+ }
514+ if (touched)
515+ mousexy = addpt(startpt, divpt(subpt(coord, startmousept), 4));
516+ break;
517+ case 2:
518+ switch(data->code) {
519+ case 0:
520+ mousexy.x += data->value;
521+ break;
522+ case 1:
523+ mousexy.y += data->value;
524+ break;
525+ case 8:
526+ buttons |= data->value == 1? 8: 16;
527+ break;
528+ default:
529+ return -1;
530+ }
531+ break;
532+ case 1:
533+ switch(data->code) {
534+ case 0x110:
535+ if (data->value == 1)
536+ buttons |= 1;
537+ else
538+ buttons &= ~1;
539+ break;
540+ case 0x111:
541+ if (data->value == 1)
542+ buttons |= shift_state & (1 << KG_SHIFT)? 2: 4;
543+ else
544+ buttons &= ~(shift_state & (1 << KG_SHIFT)? 2: 4);
545+ break;
546+ case 0x112:
547+ if (data->value == 1)
548+ buttons |= 2;
549+ else
550+ buttons &= ~2;
551+ break;
552+ default:
553+ if (hidden)
554+ return 0;
555+ if (data->code > 0 && data->code <= nelem(code2key)) {
556+ if (shift_state & (1 << KG_SHIFT))
557+ key = code2key_shift[data->code-1];
558+ else
559+ key = code2key[data->code-1];
560+ if (key == Kshift)
561+ return -1;
562+ kbdkey(key, data->value);
563+ return 0;
564+ }
565+ switch(data->code) {
566+ case 87:
567+ kbdkey(KF|11, data->value);
568+ break;
569+ case 88:
570+ kbdkey(KF|12, data->value);
571+ break;
572+ case 96:
573+ kbdkey('\n', data->value);
574+ break;
575+ case 97:
576+ kbdkey(Kctl, data->value);
577+ break;
578+ case 98:
579+ kbdkey('/', data->value);
580+ break;
581+ case 100:
582+ kbdkey(Kalt, data->value);
583+ break;
584+ case 102:
585+ kbdkey(Khome, data->value);
586+ break;
587+ case 103:
588+ kbdkey(Kup, data->value);
589+ break;
590+ case 104:
591+ kbdkey(Kpgup, data->value);
592+ break;
593+ case 105:
594+ kbdkey(Kleft, data->value);
595+ break;
596+ case 106:
597+ kbdkey(Kright, data->value);
598+ break;
599+ case 107:
600+ kbdkey(Kend, data->value);
601+ break;
602+ case 108:
603+ kbdkey(Kdown, data->value);
604+ break;
605+ case 109:
606+ kbdkey(Kpgdown, data->value);
607+ break;
608+ case 110:
609+ kbdkey(Kins, data->value);
610+ break;
611+ case 111:
612+ kbdkey(Kdel, data->value);
613+ break;
614+ }
615+ return 0;
616+ }
617+ break;
618+ default:
619+ return -1;
620+ }
621+
622+ if (mousexy.x < screenimage->r.min.x)
623+ mousexy.x = screenimage->r.min.x;
624+ if (mousexy.y < screenimage->r.min.y)
625+ mousexy.y = screenimage->r.min.y;
626+ if (mousexy.x > screenimage->r.max.x)
627+ mousexy.x = screenimage->r.max.x;
628+ if (mousexy.y > screenimage->r.max.y)
629+ mousexy.y = screenimage->r.max.y;
630+
631+ new.min = mousexy;
632+ new.max = addpt(new.min, Pt(16, 16)); // size of cursor bitmap
633+
634+ combinerect(&new, old);
635+ new.min = subpt(new.min, Pt(16, 16)); // to encompass any cursor->offset
636+
637+ qlock(&drawlock);
638+ flushmemscreen(new);
639+ qunlock(&drawlock);
640+
641+ if ((msec - lastmsec) < 10)
642+ if (data->type != 1)
643+ return 0;
644+
645+ lastmsec = msec;
646+
647+ absmousetrack(mousexy.x, mousexy.y, buttons, msec);
648+
649+ return 0;
650+}
651+
652+void
653+mouseset(Point p)
654+{
655+ qlock(&drawlock);
656+ mousexy = p;
657+ flushmemscreen(screenr);
658+ qunlock(&drawlock);
659+}
660+
661+void
662+setcursor(void)
663+{
664+ qlock(&drawlock);
665+ flushmemscreen(screenr);
666+ qunlock(&drawlock);
667+}
668+
669+void
670+titlewrite(char* buf)
671+{
672+}
673+

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t