2 * Copyright (C) 2002,2013 Ian Jackson <ian@chiark.greenend.org.uk>
4 * This is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 3,
7 * or (at your option) any later version.
9 * This is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 #include <X11/keysym.h>
20 #include <X11/cursorfont.h>
21 #include <X11/cursorfont.h>
22 #include <X11/Xmu/WinUtil.h>
28 static Display *display;
29 static int selecting, l1_x, l1_y;
30 static char stringbuf[20];
31 static unsigned long c_black, c_white, c_red, c_yellow;
32 static Window w, root;
42 static unsigned long getcolour(const char *name, int def) {
44 XColor screen_def, exact_def;
45 st= XAllocNamedColor(display,cmap,name,&screen_def,&exact_def);
46 fprintf(stdout,"name %s pixel %lu\n",name,screen_def.pixel);
47 return st ? screen_def.pixel : def;
50 static void beep(void) { XBell(display,100); }
55 * application select deselect
56 * root start typing deselect all
57 * xduplic start typing quit
61 * xduplic start selecting quit
65 * While typing: yellow on black
66 * While selecting: white on black
67 * Idle (typing into nowhere): red on black
70 static void redisplay(void) {
71 XClearWindow(display,w);
72 XDrawString(display,w,gc, l1_x,l1_y, stringbuf,strlen(stringbuf));
75 static void restatus(void) {
80 v.foreground= (selecting ? c_white :
87 XClearWindow(display,w);
89 for (count=0, own=headwn; own; own=own->next) count++;
90 snprintf(stringbuf,sizeof(stringbuf),
98 static void stopselecting(void) {
99 XUngrabPointer(display,CurrentTime);
104 static void startselecting(void) {
106 st= XGrabPointer(display,root,True,
107 ButtonPressMask,GrabModeAsync,
108 GrabModeAsync,None,cursor,CurrentTime);
109 if (st != Success) beep();
114 static void buttonpress(XButtonEvent *e) {
115 struct wnode *own, **ownp, *ownn;
120 case Button1: rightbutton=0; break;
121 case Button3: rightbutton=1; break;
125 fprintf(stdout,"button right=%d in=%lx sub=%lx (w=%lx root=%lx)\n",
126 rightbutton, (unsigned long)e->window, (unsigned long)e->subwindow,
127 (unsigned long)w, (unsigned long)e->root);
129 if (e->window == w) {
130 if (rightbutton) _exit(0);
133 /* move pointer to where it already is, just in case wm is confused */
134 XWarpPointer(display,None,root, 0,0,0,0, e->x_root,e->y_root);
141 if (!selecting) return;
143 if (e->window != e->root) return;
145 sw= XmuClientWindow(display, e->subwindow);
150 if (!headwn) { beep(); return; }
151 for (own=headwn; own; own=ownn) {
161 if (sw == w) { beep(); return; }
164 (own=(*ownp)) && own->w != sw;
165 ownp= &(*ownp)->next);
169 if (own) { beep(); return; }
170 own= malloc(sizeof(*own)); if (!own) { perror("malloc"); exit(-1); }
177 if (!own) { beep(); return; }
186 static void keypress(XKeyEvent *e) {
192 fprintf(stdout,"key type %d serial %lu (send %d) "
193 "window %lx root %lx sub %lx time %lx @%dx%d (%dx%dabs) "
194 "state %x keycode %u same %d\n",
195 e->type, e->serial, (int)e->send_event,
196 (unsigned long)e->window,
197 (unsigned long)e->root,
198 (unsigned long)e->subwindow,
199 (unsigned long)e->time,
200 e->x,e->y, e->x_root,e->y_root,
201 e->state, e->keycode, (int)e->same_screen);
202 if (XKeycodeToKeysym(display, e->keycode, 0) == XK_q) _exit(1);
205 for (own=headwn; own; own=own->next) {
206 mask= (e->type == KeyPress ? KeyPressMask :
207 e->type == KeyRelease ? KeyReleaseMask :
208 KeyPressMask|KeyReleaseMask);
212 st= XSendEvent(display,own->w,True,mask,(XEvent*)e);
214 fprintf(stdout,"sendevent to %lx %d mask %lx\n",
215 (unsigned long)own->w, st, mask);
220 static void expose(XExposeEvent *e) {
221 if (e->count) return;
225 int main(int argc, const char **argv) {
228 XSetWindowAttributes wv;
229 int screen, direction, ascent, descent, l1_width, l1_height;
233 display= XOpenDisplay(0);
234 screen= DefaultScreen(display);
235 cmap= DefaultColormap(display,screen);
236 root= DefaultRootWindow(display);
238 c_black= getcolour("black", 0);
239 c_white= getcolour("white", 1);
240 c_yellow= getcolour("yellow", c_white);
241 c_red= getcolour("red", c_white);
243 cursor= XCreateFontCursor(display,XC_crosshair);
245 wv.event_mask= KeyPressMask|KeyReleaseMask|ButtonPressMask|ExposureMask;
246 w= XCreateWindow(display, root,
247 0,0, 50,21, 0,DefaultDepth(display,screen),
248 InputOutput, DefaultVisual(display,screen),
251 font= XLoadFont(display,"fixed");
253 gcv.background= c_black;
255 gc= XCreateGC(display,w,GCBackground|GCFont,&gcv);
257 XQueryTextExtents(display,font, "SIT 0689", 8,
258 &direction,&ascent,&descent,&overall);
259 l1_width= overall.lbearing + overall.rbearing;
260 l1_x= overall.lbearing;
262 l1_height= descent+ascent;
264 XResizeWindow(display,w, l1_width,l1_height);
265 XSetWindowBackground(display,w,c_black);
267 XMapWindow(display,w);
271 XNextEvent(display,&e);
272 fprintf(stdout,"selecting = %d; event type = %lu\n",
273 selecting, (unsigned long)e.type);
275 case Expose: expose(&e.xexpose); break;
276 case ButtonPress: buttonpress(&e.xbutton); break;
277 case KeyPress: case KeyRelease: keypress(&e.xkey); break;