changelog shortlog tags branches changeset files revisions annotate raw help

Mercurial > hg > plan9front / sys/src/games/blit/blit.c

changeset 6228: f4fa0b9d0397
parent: b2240f44b103
child: 16b731de1df0
author: cinap_lenrek@felloff.net
date: Sun, 26 Nov 2017 17:11:01 +0100
permissions: -rw-r--r--
description: cga: capture cga console contents on boot, make sure cgapos is in range

to capture bios and bootloader messages, convert the contents
on the screen to kmesg.

on machines without legacy cga, the cga registers read out as
0xFF, resuting in out of bounds cgapos. so set cgapos to 0 in
that case.
1 #include <u.h>
2 #include <libc.h>
3 #include <thread.h>
4 #include <draw.h>
5 #include <keyboard.h>
6 #include <mouse.h>
7 #include <cursor.h>
8 #include "dat.h"
9 #include "fns.h"
10 
11 int baud = 40000;
12 int scale = 1;
13 Rectangle picr;
14 Image *tmp, *bg;
15 Channel *keych, *uartrxch, *uarttxch;
16 Mousectl *mc;
17 int daddr;
18 u16int dstat;
19 u8int invert;
20 int vblctr, uartrxctr;
21 Rectangle updated;
22 u32int colbgv, colfgv;
23 Image *colbg, *colfg;
24 int realcolors;
25 
26 static void
27 screeninit(void)
28 {
29  Point p;
30 
31  p = divpt(addpt(screen->r.min, screen->r.max), 2);
32  picr = (Rectangle){subpt(p, Pt(scale * SX/2, scale * SY/2)), addpt(p, Pt(scale * SX/2, scale * SY/2))};
33  if(picr.min.x < screen->r.min.x){
34  picr.max.x += screen->r.min.x - picr.min.x;
35  picr.min.x = screen->r.min.x;
36  }
37  if(picr.min.y < screen->r.min.y){
38  picr.max.y += screen->r.min.y - picr.min.y;
39  picr.min.y = screen->r.min.y;
40  }
41  if(tmp != nil) freeimage(tmp);
42  tmp = allocimage(display, Rect(0, 0, scale * SX, scale > 1 ? 1 : scale * SY), CHAN1(CMap, 1), scale > 1, 0);
43  if(bg != nil) freeimage(bg);
44  bg = allocimage(display, Rect(0, 0, 1, 1), screen->chan, 1, 0xCCCCCCFF);
45  colbg = allocimage(display, Rect(0, 0, 1, 1), screen->chan, 1, colbgv);
46  colfg = allocimage(display, Rect(0, 0, 1, 1), screen->chan, 1, colfgv);
47  draw(screen, screen->r, bg, nil, ZP);
48  updated = Rect(0, 0, SX, SY);
49 }
50 
51 static void
52 redraw(void)
53 {
54  static uchar pic[SX*SY/8];
55  ushort *p;
56  uchar *q;
57  int o, n;
58  Mouse m;
59  Rectangle r;
60 
61  if(nbrecvul(mc->resizec) > 0){
62  if(getwindow(display, Refnone) < 0)
63  sysfatal("resize failed: %r");
64  screeninit();
65  }
66  while(nbrecv(mc->c, &m) > 0){
67  if(ptinrect(m.xy, picr)){
68  mousex = picr.max.x - m.xy.x - 1;
69  mousey = picr.max.y - m.xy.y - 1;
70  }
71  n = m.buttons >> 2 & 1 | m.buttons & 2 | m.buttons << 2 & 4;
72  if(n != mousebut){
73  mousebut = n;
74  irq |= INTMOUSE;
75  }
76  }
77 
78  if(Dy(updated) <= 0 || Dx(updated) <= 0)
79  return;
80 
81  assert(daddr + sizeof(pic) <= sizeof(ram));
82 
83  r = tmp->r;
84  if(updated.min.y > r.min.y)
85  r.min.y = updated.min.y;
86  if(updated.max.y < r.max.y)
87  r.max.y = updated.max.y;
88 
89  o = r.min.y*(SX/8);
90  p = ram + (daddr + o) / 2;
91  q = pic + o;
92  for(n = Dy(r)*(SX/16); --n >= 0; ){
93  *q++ = invert ^ *p >> 8;
94  *q++ = invert ^ *p++;
95  }
96 
97  loadimage(tmp, r, pic+o, Dy(r)*(SX/8));
98  if(realcolors){
99  draw(screen, rectaddpt(r, picr.min), colfg, nil, r.min);
100  draw(screen, rectaddpt(r, picr.min), colbg, tmp, r.min);
101  }else
102  draw(screen, rectaddpt(r, picr.min), tmp, nil, r.min);
103  updated = Rect(SX, SY, 0, 0);
104  flushimage(display, 1);
105 }
106 
107 static uchar
108 keymap[] = {
109  [Kup-KF] 0xf1,
110  [Kdown-KF] 0xf2,
111  [Kleft-KF] 0xf3,
112  [Kright-KF] 0xf4,
113  [1] 0xf6, /* PF1 */
114  [2] 0xf7, /* PF2 */
115  [3] 0xf8, /* PF3 */
116  [4] 0xf9, /* PF4 */
117  [12] 0xfe, /* SET-UP */
118  [Kpgdown-KF] 0xb0, /* SCROLL */
119  [Kins-KF] 0xe0, /* BREAK */
120 };
121 
122 static void
123 keyproc(void *)
124 {
125  int fd, cfd, ch, rc;
126  static char buf[256];
127  char *p;
128  Rune r;
129 
130  fd = open("/dev/cons", OREAD);
131  if(fd < 0)
132  sysfatal("open: %r");
133  cfd = open("/dev/consctl", OWRITE);
134  if(cfd < 0)
135  sysfatal("open: %r");
136  fprint(cfd, "rawon");
137  for(;;){
138  rc = read(fd, buf, sizeof(buf) - 1);
139  if(rc <= 0)
140  sysfatal("read /dev/cons: %r");
141  for(p = buf; p < buf + rc && (p += chartorune(&r, p)); ){
142  if(r == Kend){
143  close(fd);
144  threadexitsall(nil);
145  }
146  ch = r;
147  if(ch == '\n') ch = '\r';
148  else if(ch >= KF){
149  if(ch >= KF + nelem(keymap)) continue;
150  ch = keymap[ch - KF];
151  if(ch == 0) continue;
152  }else if(ch >= 0x80) continue;
153  send(keych, &ch);
154  }
155  }
156 }
157 
158 void
159 usage(void)
160 {
161  fprint(2, "usage: %s [-b baud] [-C bg,fg] [-d] [-t [net!]host[!service]]\n", argv0);
162  threadexitsall("usage");
163 }
164 
165 void
166 threadmain(int argc, char **argv)
167 {
168  int n, ms;
169  static Cursor blank;
170  char *telnet;
171  char *p;
172  extern int diag;
173 
174  ms = 0;
175  telnet = nil;
176  ARGBEGIN{
177  case 'b':
178  baud = strtol(EARGF(usage()), &p, 0);
179  if(*p != 0) usage();
180  break;
181  case 't':
182  telnet = EARGF(usage());
183  break;
184  case 'C':
185  if(realcolors) usage();
186  realcolors++;
187  p = EARGF(usage());
188  colbgv = strtol(p, &p, 16) << 8 | 0xff;
189  if(*p++ != ',') usage();
190  colfgv = strtol(p, &p, 16) << 8 | 0xff;
191  if(*p != 0) usage();
192  break;
193  case 'd':
194  diag++;
195  break;
196  case 'm':
197  ms++;
198  break;
199  default: usage();
200  }ARGEND;
201  if(argc != 0) usage();
202 
203  keych = chancreate(sizeof(int), 64);
204  uartrxch = chancreate(sizeof(int), 128);
205  uarttxch = chancreate(sizeof(int), 128);
206  if(telnet != nil) telnetinit(telnet);
207  meminit();
208  if(initdraw(nil, nil, nil) < 0)
209  sysfatal("initdraw: %r");
210 
211  screeninit();
212  proccreate(keyproc, nil, mainstacksize);
213  mc = initmouse(nil, screen);
214  if(mc == nil)
215  sysfatal("initmouse: %r");
216  if(ms == 0)
217  setcursor(mc, &blank);
218 
219  cpureset();
220  for(;;){
221  keycheck();
222  n = step();
223  vblctr += n;
224  if(vblctr >= VBLDIV){
225  irq |= INTVBL;
226  redraw();
227  vblctr -= VBLDIV;
228  }
229  if(uartrxctr > 0)
230  uartrxctr -= n;
231  }
232 }