x11-kernel.c - vx32 - Local 9vx git repository for patches.
(HTM) git clone git://r-36.net/vx32
(DIR) Log
(DIR) Files
(DIR) Refs
---
x11-kernel.c (3586B)
---
1 #include "u.h"
2 #include "lib.h"
3 #include "mem.h"
4 #include "dat.h"
5 #include "fns.h"
6 #include "error.h"
7 #define Image IMAGE /* kernel has its own Image */
8 #include <draw.h>
9 #include <memdraw.h>
10 #include <keyboard.h>
11 #include <cursor.h>
12 #include "screen.h"
13 #include "mouse.h"
14 #include "x11-inc.h"
15
16 #define MouseMask (\
17 ButtonPressMask|\
18 ButtonReleaseMask|\
19 PointerMotionMask|\
20 Button1MotionMask|\
21 Button2MotionMask|\
22 Button3MotionMask)
23
24 #define Mask MouseMask|ExposureMask|StructureNotifyMask|KeyPressMask|EnterWindowMask|LeaveWindowMask
25
26 Rectangle windowrect;
27 Rectangle screenrect;
28 int fullscreen;
29
30 /*
31 * Handle an incoming X event.
32 */
33 void
34 runxevent(XEvent *xev)
35 {
36 KeySym k;
37 static Mouse m;
38
39 #ifdef SHOWEVENT
40 static int first = 1;
41 if(first){
42 dup(create("/tmp/devdraw.out", OWRITE, 0666), 1);
43 setbuf(stdout, 0);
44 first = 0;
45 }
46 #endif
47
48 if(xev == 0)
49 return;
50
51 #ifdef SHOWEVENT
52 print("\n");
53 ShowEvent(xev);
54 #endif
55
56 switch(xev->type){
57 case Expose:
58 drawqlock();
59 _xexpose(xev);
60 drawqunlock();
61 break;
62
63 case DestroyNotify:
64 if(_xdestroy(xev)){
65 restoretty();
66 bye(0);
67 }
68 break;
69
70 case ConfigureNotify:
71 drawqlock();
72 if(_xconfigure(xev)){
73 drawqunlock();
74 _xreplacescreenimage();
75 }else
76 drawqunlock();
77 break;
78
79 case ButtonPress:
80 case ButtonRelease:
81 case MotionNotify:
82 if(_xtoplan9mouse(xev, &m) < 0)
83 return;
84 mousetrack(m.xy.x, m.xy.y, m.buttons, m.msec);
85 break;
86
87 case KeyPress:
88 XLookupString((XKeyEvent*)xev, NULL, 0, &k, NULL);
89 if(k == XK_F11){
90 fullscreen = !fullscreen;
91 if(1){
92 /* The old way: send a move request */
93 XWindowChanges e;
94 int mask;
95 Rectangle r;
96
97 memset(&e, 0, sizeof e);
98 mask = CWX|CWY|CWWidth|CWHeight;
99 if(fullscreen)
100 r = screenrect;
101 else
102 r = windowrect;
103 e.x = r.min.x;
104 e.y = r.min.y;
105 e.width = Dx(r);
106 e.height = Dy(r);
107 XConfigureWindow(_x.kmcon, _x.drawable, mask, &e);
108 XFlush(_x.kmcon);
109 }else{
110 /* The "right" way, but not supported by rio. */
111 XClientMessageEvent e;
112
113 memset(&e, 0, sizeof e);
114 e.type = ClientMessage;
115 e.send_event = True;
116 e.window = _x.drawable;
117 e.message_type = _x.wmstate;
118 e.format = 32;
119 e.data.l[0] = fullscreen; // 0 off, 1 on, 2 is toggle
120 e.data.l[1] = _x.wmfullscreen;
121 e.data.l[2] = 0;
122 XSendEvent(_x.kmcon, DefaultRootWindow(_x.kmcon), False,
123 SubstructureRedirectMask|SubstructureNotifyMask, (XEvent*)&e);
124 }
125 return;
126 }
127 _xtoplan9kbd(xev);
128 break;
129
130 case SelectionRequest:
131 _xselect(xev);
132 break;
133 }
134 }
135
136 static void
137 _xproc(void *v)
138 {
139 XEvent event;
140
141 XSelectInput(_x.kmcon, _x.drawable, Mask);
142 for(;;){
143 XNextEvent(_x.kmcon, &event);
144 runxevent(&event);
145 }
146 }
147
148 void
149 screeninit(void)
150 {
151 }
152
153 uchar*
154 attachscreen(Rectangle *r, ulong *chan, int *depth,
155 int *width, int *softscreen, void **X)
156 {
157 Memimage *m;
158
159 if(_x.screenimage == nil){
160 _memimageinit();
161 if(_xattach("9vx", nil) == nil)
162 panic("cannot connect to X display: %r");
163 kproc("*x11*", _xproc, nil);
164 }
165 m = _x.screenimage;
166 *r = m->r;
167 *chan = m->chan;
168 *depth = m->depth;
169 *width = m->width;
170 *X = m->x;
171 *softscreen = 1;
172 return m->data->bdata;
173 }
174
175 int
176 hwdraw(Memdrawparam *p)
177 {
178 return 0;
179 }
180
181 void
182 getcolor(ulong i, ulong *r, ulong *g, ulong *b)
183 {
184 ulong v;
185
186 v = cmap2rgb(i);
187 *r = (v>>16)&0xFF;
188 *g = (v>>8)&0xFF;
189 *b = v&0xFF;
190 }
191
192 int
193 setcolor(ulong i, ulong r, ulong g, ulong b)
194 {
195 /* no-op */
196 return 0;
197 }
198
199 char*
200 getsnarf(void)
201 {
202 return _xgetsnarf();
203 }
204
205 void
206 putsnarf(char *data)
207 {
208 _xputsnarf(data);
209 }
210
211 void
212 setmouse(Point p)
213 {
214 drawqlock();
215 _xmoveto(p);
216 drawqunlock();
217 }
218