chiark / gitweb /
xduplic-copier: as found on chiark
[chiark-utils.git] / cprogs / xduplic-copier.c
1 /*
2  * Copyright (C) 2002 Ian Jackson <ian@chiark.greenend.org.uk>
3  *
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 2,
7  * or (at your option) any later version.
8  *
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.
13  *
14  * You should have received a copy of the GNU General Public
15  * License along with this file; if not, write to the Free Software
16  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17  */
18
19 #include <X11/Xlib.h>
20 #include <X11/keysym.h>
21 #include <X11/cursorfont.h>
22 #include <X11/cursorfont.h>
23 #include <X11/Xmu/WinUtil.h>
24
25 #include <stdlib.h>
26 #include <stdio.h>
27 #include <assert.h>
28
29 static Display *display;
30 static int selecting, l1_x, l1_y;
31 static char stringbuf[20];
32 static unsigned long c_black, c_white, c_red, c_yellow;
33 static Window w, root;
34 static Cursor cursor;
35 static GC gc;
36 static Colormap cmap;
37
38 struct wnode {
39   struct wnode *next;
40   Window w;
41 } *headwn;
42
43 static unsigned long getcolour(const char *name, int def) {
44   Status st;
45   XColor screen_def, exact_def;
46   st= XAllocNamedColor(display,cmap,name,&screen_def,&exact_def);
47   fprintf(stdout,"name %s pixel %lu\n",name,screen_def.pixel);
48   return st ? screen_def.pixel : def;
49 }
50
51 static void beep(void) { XBell(display,100); }
52
53 /*
54  *  While selecting:
55  *                   Left                         Right
56  *    application     select                       deselect
57  *    root            start typing                 deselect all
58  *    xduplic         start typing                 quit
59  *
60  *  While typing:
61  *                   Left                         Right
62  *    xduplic         start selecting              quit
63  *
64  *  Colours:
65  *
66  *    While typing:                   yellow on black
67  *    While selecting:                white on black
68  *    Idle (typing into nowhere):     red on black
69  */
70
71 static void redisplay(void) {
72   XClearWindow(display,w);
73   XDrawString(display,w,gc, l1_x,l1_y, stringbuf,strlen(stringbuf));
74 }
75
76 static void restatus(void) {
77   XGCValues v;
78   int count;
79   struct wnode *own;
80
81   v.foreground= (selecting ? c_white :
82                  headwn ? c_yellow :
83                  c_red);
84   
85   XChangeGC(display,gc,
86             GCForeground,
87             &v);
88   XClearWindow(display,w);
89
90   for (count=0, own=headwn; own; own=own->next) count++;
91   snprintf(stringbuf,sizeof(stringbuf),
92            "%c %d",
93            selecting ? 'S' :
94            headwn ? 'T' : 'i',
95            count);
96   redisplay();
97 }
98
99 static void stopselecting(void) {
100   XUngrabPointer(display,CurrentTime);
101   selecting= 0;
102   restatus();
103 }
104
105 static void startselecting(void) {
106   Status st;
107   st= XGrabPointer(display,root,True,
108                    ButtonPressMask,GrabModeAsync,
109                    GrabModeAsync,None,cursor,CurrentTime);
110   if (st != Success) beep();
111   else selecting= 1;
112   restatus();
113 }
114
115 static void buttonpress(XButtonEvent *e) {
116   struct wnode *own, **ownp, *ownn;
117   int rightbutton;
118   Window sw;
119
120   switch (e->button) {
121   case Button1: rightbutton=0; break;
122   case Button3: rightbutton=1; break;
123   default: return;
124   }
125
126   fprintf(stdout,"button right=%d in=%lx sub=%lx (w=%lx root=%lx)\n",
127           rightbutton, (unsigned long)e->window, (unsigned long)e->subwindow,
128           (unsigned long)w, (unsigned long)e->root);
129
130   if (e->window == w) {
131     if (rightbutton) _exit(0);
132     if (selecting) {
133       stopselecting();
134       /* move pointer to where it already is, just in case wm is confused */
135       XWarpPointer(display,None,root, 0,0,0,0, e->x_root,e->y_root);
136     } else {
137       startselecting();
138     }
139     return;
140   }
141
142   if (!selecting) return;
143
144   if (e->window != e->root) return;
145
146   sw= XmuClientWindow(display, e->subwindow);
147   if (!sw) {
148     if (!rightbutton) {
149       stopselecting();
150     } else {
151       if (!headwn) { beep(); return; }
152       for (own=headwn; own; own=ownn) {
153         ownn= own->next;
154         free(own);
155       }
156       headwn= 0;
157       restatus();
158     }
159     return;
160   }
161
162   if (sw == w) { beep(); return; }
163
164   for (ownp=&headwn;
165        (own=(*ownp)) && own->w != sw;
166        ownp= &(*ownp)->next);
167   
168   if (!rightbutton) {
169     
170     if (own) { beep(); return; }
171     own= malloc(sizeof(*own)); if (!own) { perror("malloc"); exit(-1); }
172     own->w= sw;
173     own->next= headwn;
174     headwn= own;
175
176   } else {
177
178     if (!own) { beep(); return; }
179     *ownp= own->next;
180     free(own);
181
182   }
183
184   restatus();
185 }
186
187 static void keypress(XKeyEvent *e) {
188   Status st;
189   struct wnode *own;
190   unsigned long mask;
191   
192   if (selecting) {
193     fprintf(stdout,"key type %d serial %lu (send %d) "
194             "window %lx root %lx sub %lx time %lx @%dx%d (%dx%dabs) "
195             "state %x keycode %u same %d\n",
196             e->type, e->serial, (int)e->send_event,
197             (unsigned long)e->window,
198             (unsigned long)e->root,
199             (unsigned long)e->subwindow,
200             (unsigned long)e->time,
201             e->x,e->y, e->x_root,e->y_root,
202             e->state, e->keycode, (int)e->same_screen);
203     if (XKeycodeToKeysym(display, e->keycode, 0) == XK_q) _exit(1);
204     beep(); return;
205   }
206   for (own=headwn; own; own=own->next) {
207     mask= (e->type == KeyPress ? KeyPressMask :
208            e->type == KeyRelease ? KeyReleaseMask :
209            KeyPressMask|KeyReleaseMask);
210     e->window= own->w;
211     e->subwindow= None;
212     e->send_event= True;
213     st= XSendEvent(display,own->w,True,mask,(XEvent*)e);
214     if (st != Success) {
215       fprintf(stdout,"sendevent to %lx %d mask %lx\n",
216               (unsigned long)own->w, st, mask);
217     }
218   }
219 }
220
221 static void expose(XExposeEvent *e) {
222   if (e->count) return;
223   redisplay();
224 }
225
226 int main(int argc, const char **argv) {
227   XEvent e;
228   XGCValues gcv;
229   XSetWindowAttributes wv;
230   int screen, direction, ascent, descent, l1_width, l1_height;
231   XCharStruct overall;
232   Font font;
233
234   display= XOpenDisplay(0);
235   screen= DefaultScreen(display);
236   cmap= DefaultColormap(display,screen);
237   root= DefaultRootWindow(display);
238   
239   c_black=   getcolour("black",  0);
240   c_white=   getcolour("white",  1);
241   c_yellow=  getcolour("yellow", c_white);
242   c_red=     getcolour("red",    c_white);
243
244   cursor= XCreateFontCursor(display,XC_crosshair);
245
246   wv.event_mask= KeyPressMask|KeyReleaseMask|ButtonPressMask|ExposureMask;
247   w= XCreateWindow(display, root,
248                    0,0, 50,21, 0,DefaultDepth(display,screen),
249                    InputOutput, DefaultVisual(display,screen),
250                    CWEventMask, &wv);
251
252   font= XLoadFont(display,"fixed");
253
254   gcv.background= c_black;
255   gcv.font=       font;
256   gc= XCreateGC(display,w,GCBackground|GCFont,&gcv);
257
258   XQueryTextExtents(display,font, "SIT 0689", 8,
259                     &direction,&ascent,&descent,&overall);
260   l1_width= overall.lbearing + overall.rbearing;
261   l1_x= overall.lbearing;
262   l1_y= ascent;
263   l1_height= descent+ascent;
264
265   XResizeWindow(display,w, l1_width,l1_height);
266   XSetWindowBackground(display,w,c_black);
267   
268   XMapWindow(display,w);
269   restatus();
270   
271   for (;;) {
272     XNextEvent(display,&e);
273     fprintf(stdout,"selecting = %d; event type = %lu\n",
274             selecting, (unsigned long)e.type);
275     switch (e.type) {
276     case Expose:                     expose(&e.xexpose);       break;
277     case ButtonPress:                buttonpress(&e.xbutton);  break;
278     case KeyPress: case KeyRelease:  keypress(&e.xkey);        break;
279     }
280   }
281 }