File: xduplic-copier.c

package info (click to toggle)
chiark-utils 8.0.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,084 kB
  • sloc: ansic: 4,640; perl: 4,281; sh: 671; python: 465; makefile: 286; tcl: 228
file content (285 lines) | stat: -rw-r--r-- 7,405 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/*
 * Copyright (C) 2002,2013 Ian Jackson <ian@chiark.greenend.org.uk>
 *
 * This is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 3,
 * or (at your option) any later version.
 *
 * This is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <X11/Xlib.h>
#include <X11/keysym.h>
#include <X11/cursorfont.h>
#include <X11/cursorfont.h>
#include <X11/Xmu/WinUtil.h>
#include <X11/XKBlib.h>

#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include <string.h>
#include <unistd.h>

static Display *display;
static int selecting, l1_x, l1_y;
static char stringbuf[20];
static unsigned long c_black, c_white, c_red, c_yellow;
static Window w, root;
static Cursor cursor;
static GC gc;
static Colormap cmap;

struct wnode {
  struct wnode *next;
  Window w;
} *headwn;

static unsigned long getcolour(const char *name, int def) {
  Status st;
  XColor screen_def, exact_def;
  st= XAllocNamedColor(display,cmap,name,&screen_def,&exact_def);
  fprintf(stdout,"name %s pixel %lu\n",name,screen_def.pixel);
  return st ? screen_def.pixel : def;
}

static void beep(void) { XBell(display,100); }

/*
 *  While selecting:
 *                   Left                         Right
 *    application     select                       deselect
 *    root            start typing                 deselect all
 *    xduplic         start typing                 quit
 *
 *  While typing:
 *                   Left                         Right
 *    xduplic         start selecting              quit
 *
 *  Colours:
 *
 *    While typing:                   yellow on black
 *    While selecting:                white on black
 *    Idle (typing into nowhere):     red on black
 */

static void redisplay(void) {
  XClearWindow(display,w);
  XDrawString(display,w,gc, l1_x,l1_y, stringbuf,strlen(stringbuf));
}

static void restatus(void) {
  XGCValues v;
  int count;
  struct wnode *own;

  v.foreground= (selecting ? c_white :
		 headwn ? c_yellow :
		 c_red);
  
  XChangeGC(display,gc,
	    GCForeground,
	    &v);
  XClearWindow(display,w);

  for (count=0, own=headwn; own; own=own->next) count++;
  snprintf(stringbuf,sizeof(stringbuf),
	   "%c %d",
	   selecting ? 'S' :
	   headwn ? 'T' : 'i',
	   count);
  redisplay();
}

static void stopselecting(void) {
  XUngrabPointer(display,CurrentTime);
  selecting= 0;
  restatus();
}

static void startselecting(void) {
  Status st;
  st= XGrabPointer(display,root,True,
		   ButtonPressMask,GrabModeAsync,
		   GrabModeAsync,None,cursor,CurrentTime);
  if (st != Success) beep();
  else selecting= 1;
  restatus();
}

static void buttonpress(XButtonEvent *e) {
  struct wnode *own, **ownp, *ownn;
  int rightbutton;
  Window sw;

  switch (e->button) {
  case Button1: rightbutton=0; break;
  case Button3: rightbutton=1; break;
  default: return;
  }

  fprintf(stdout,"button right=%d in=%lx sub=%lx (w=%lx root=%lx)\n",
	  rightbutton, (unsigned long)e->window, (unsigned long)e->subwindow,
	  (unsigned long)w, (unsigned long)e->root);

  if (e->window == w) {
    if (rightbutton) _exit(0);
    if (selecting) {
      stopselecting();
      /* move pointer to where it already is, just in case wm is confused */
      XWarpPointer(display,None,root, 0,0,0,0, e->x_root,e->y_root);
    } else {
      startselecting();
    }
    return;
  }

  if (!selecting) return;

  if (e->window != e->root) return;

  if (!e->subwindow) {
    if (!rightbutton) {
      stopselecting();
    } else {
      if (!headwn) { beep(); return; }
      for (own=headwn; own; own=ownn) {
	ownn= own->next;
	free(own);
      }
      headwn= 0;
      restatus();
    }
    return;
  }

  sw= XmuClientWindow(display, e->subwindow);

  if (sw == w) { beep(); return; }

  for (ownp=&headwn;
       (own=(*ownp)) && own->w != sw;
       ownp= &(*ownp)->next);
  
  if (!rightbutton) {
    
    if (own) { beep(); return; }
    own= malloc(sizeof(*own)); if (!own) { perror("malloc"); exit(-1); }
    own->w= sw;
    own->next= headwn;
    headwn= own;

  } else {

    if (!own) { beep(); return; }
    *ownp= own->next;
    free(own);

  }

  restatus();
}

static void keypress(XKeyEvent *e) {
  Status st;
  struct wnode *own;
  unsigned long mask;
  
  if (selecting) {
    fprintf(stdout,"key type %d serial %lu (send %d) "
	    "window %lx root %lx sub %lx time %lx @%dx%d (%dx%dabs) "
	    "state %x keycode %u same %d\n",
	    e->type, e->serial, (int)e->send_event,
	    (unsigned long)e->window,
	    (unsigned long)e->root,
	    (unsigned long)e->subwindow,
	    (unsigned long)e->time,
	    e->x,e->y, e->x_root,e->y_root,
	    e->state, e->keycode, (int)e->same_screen);
    if (XkbKeycodeToKeysym(display, e->keycode, 0, 0) == XK_q) _exit(1);
    beep(); return;
  }
  for (own=headwn; own; own=own->next) {
    mask= (e->type == KeyPress ? KeyPressMask :
	   e->type == KeyRelease ? KeyReleaseMask :
	   KeyPressMask|KeyReleaseMask);
    e->window= own->w;
    e->subwindow= None;
    e->send_event= True;
    st= XSendEvent(display,own->w,True,mask,(XEvent*)e);
    if (st != Success) {
      fprintf(stdout,"sendevent to %lx %d mask %lx\n",
	      (unsigned long)own->w, st, mask);
    }
  }
}

static void expose(XExposeEvent *e) {
  if (e->count) return;
  redisplay();
}

int main(int argc, const char **argv) {
  XEvent e;
  XGCValues gcv;
  XSetWindowAttributes wv;
  int screen, direction, ascent, descent, l1_width, l1_height;
  XCharStruct overall;
  Font font;

  display= XOpenDisplay(0);
  if (!display) { fputs("XOpenDisplay failed\n",stderr); exit(-1); }
  screen= DefaultScreen(display);
  cmap= DefaultColormap(display,screen);
  root= DefaultRootWindow(display);
  
  c_black=   getcolour("black",  0);
  c_white=   getcolour("white",  1);
  c_yellow=  getcolour("yellow", c_white);
  c_red=     getcolour("red",    c_white);

  cursor= XCreateFontCursor(display,XC_crosshair);

  wv.event_mask= KeyPressMask|KeyReleaseMask|ButtonPressMask|ExposureMask;
  w= XCreateWindow(display, root,
		   0,0, 50,21, 0,DefaultDepth(display,screen),
		   InputOutput, DefaultVisual(display,screen),
		   CWEventMask, &wv);

  font= XLoadFont(display,"fixed");

  gcv.background= c_black;
  gcv.font=       font;
  gc= XCreateGC(display,w,GCBackground|GCFont,&gcv);

  XQueryTextExtents(display,font, "SIT 0689", 8,
		    &direction,&ascent,&descent,&overall);
  l1_width= overall.lbearing + overall.rbearing;
  l1_x= overall.lbearing;
  l1_y= ascent;
  l1_height= descent+ascent;

  XResizeWindow(display,w, l1_width,l1_height);
  XSetWindowBackground(display,w,c_black);
  
  XMapWindow(display,w);
  restatus();
  
  for (;;) {
    XNextEvent(display,&e);
    fprintf(stdout,"selecting = %d; event type = %lu\n",
	    selecting, (unsigned long)e.type);
    switch (e.type) {
    case Expose:                     expose(&e.xexpose);       break;
    case ButtonPress:                buttonpress(&e.xbutton);  break;
    case KeyPress: case KeyRelease:  keypress(&e.xkey);        break;
    }
  }
}