3 #include "doomdef.h" // printf
5 #include "v_video.h" // screens[]
6 #include "d_main.h" // D_PostEvent
13 static int mouseactive;
15 static Rectangle grabout;
18 static void kbdproc(void);
19 static void mouseproc(void);
21 static uchar cmap[3*256];
23 static int kbdpid = -1;
24 static int mousepid = -1;
27 catch(void *, char *msg)
29 /* in case we crash, disable mouse grab */
30 if(strncmp(msg, "sys:", 4) == 0)
35 void I_InitGraphics(void)
41 if(initdraw(nil, nil, "doom") < 0)
42 I_Error("I_InitGraphics failed");
44 draw(screen, screen->r, display->black, nil, ZP);
46 center = addpt(screen->r.min, Pt(Dx(screen->r)/2, Dy(screen->r)/2));
47 grabout = insetrect(screen->r, Dx(screen->r)/8);
49 if((pid = rfork(RFPROC|RFMEM|RFFDG)) == 0){
55 if((pid = rfork(RFPROC|RFMEM|RFFDG)) == 0){
62 void I_ShutdownGraphics(void)
65 postnote(PNPROC, kbdpid, "shutdown");
69 postnote(PNPROC, mousepid, "shutdown");
74 void I_SetPalette(byte *palette)
76 memcpy(cmap, palette, 3*256);
79 void I_UpdateNoBlit(void)
84 void I_FinishUpdate(void)
90 uchar buf[SCREENWIDTH*3*4];
94 if(getwindow(display, Refnone) < 0)
95 sysfatal("getwindow failed: %r");
97 /* make black background */
98 draw(screen, screen->r, display->black, nil, ZP);
100 center = addpt(screen->r.min, Pt(Dx(screen->r)/2, Dy(screen->r)/2));
101 grabout = insetrect(screen->r, Dx(screen->r)/8);
104 scale = Dx(screen->r)/SCREENWIDTH;
110 /* where to draw the scaled row */
111 r = rectsubpt(rectaddpt(Rect(0, 0, scale*SCREENWIDTH, scale), center),
112 Pt(scale*SCREENWIDTH/2, scale*SCREENHEIGHT/2));
114 /* the row image, y-axis gets scaled with repl flag */
115 rowimg = allocimage(display, Rect(0, 0, scale*SCREENWIDTH, 1), RGB24, 1, DNofill);
118 for(y = 0; y < SCREENHEIGHT; y++){
142 loadimage(rowimg, rowimg->r, buf, d - buf);
143 draw(screen, r, rowimg, nil, ZP);
149 flushimage(display, 1);
152 void I_MouseEnable(int on)
154 static char nocurs[2*4+2*2*16];
157 if(mouseactive == on)
159 if(mouseactive = on){
160 if((fd = open("/dev/cursor", ORDWR|OCEXEC)) < 0)
162 write(fd, nocurs, sizeof(nocurs));
169 void I_ReadScreen(byte *scr)
171 memcpy (scr, screens[0], SCREENWIDTH*SCREENHEIGHT);
174 void I_BeginRead(void)
176 I_Error("PORTME i_video.c I_BeginRead");
181 I_Error("PORTME i_video.c I_EndRead");
184 void I_StartTic(void)
198 return KEY_LEFTARROW;
200 return KEY_RIGHTARROW;
204 return KEY_DOWNARROW;
216 return KEY_BACKSPACE;
232 return KEY_F1+(r-(KF|1));
244 char buf[128], buf2[128], *s;
249 if((kfd = open("/dev/kbd", OREAD)) < 0)
250 sysfatal("can't open kbd: %r");
254 while((n = read(kfd, buf, sizeof(buf))) > 0){
263 chartorune(&r, buf+1);
275 s += chartorune(&r, s);
276 if(utfrune(buf2+1, r) == nil){
277 if(e.data1 = runetokey(r)){
287 s += chartorune(&r, s);
288 if(utfrune(buf+1, r) == nil){
289 if(e.data1 = runetokey(r)){
310 if((fd = open("/dev/mouse", ORDWR)) < 0)
311 sysfatal("can't open mouse: %r");
313 memset(&m, 0, sizeof m);
314 memset(&om, 0, sizeof om);
317 n = read(fd, buf, sizeof buf);
319 fprint(2, "mouse: bad count %d not 49: %r\n", n);
333 m.xy.x = atoi(buf+1+0*12);
334 m.xy.y = atoi(buf+1+1*12);
335 m.buttons = atoi(buf+1+2*12);
336 m.msec = atoi(buf+1+3*12);
338 if(!ptinrect(m.xy, grabout)){
339 fprint(fd, "m%d %d", center.x, center.y);
347 e.data2 = 10*(m.xy.x - om.xy.x);
348 e.data3 = 10*(om.xy.y - m.xy.y);