File: blast.c

package info (click to toggle)
blast 1.1-22
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 120 kB
  • ctags: 23
  • sloc: ansic: 263; makefile: 43
file content (325 lines) | stat: -rw-r--r-- 7,919 bytes parent folder | download | duplicates (8)
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
#ifndef lint
static char SccsId[] = "@(#)blast.c    V1.1    9/9/92";
#endif

/* blast.c
   Written by Daniel Dee  7/23/92

   Copyright (c) 1992 Daniel Dee
   This program is free software; you may
   redistribute it and/or modify it under the terms of
   the GNU General Public License as published by the
   Free Software Foundation; either version, or
   any later version.

   email: ddee@vicorp.com
   V.I. Corporation
   47 Pleasant Street
   Northampton, MA 01060
*/

#include <stdio.h>

#ifndef VMS
/* Header files for Unix version */
#include <X11/X.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Intrinsic.h>
#include <X11/Xatom.h>
#include <X11/cursorfont.h>
#include <X11/keysym.h>
#include <X11/extensions/shape.h>

#include <errno.h>
#include <ctype.h>
#include <stdlib.h>
#include <fcntl.h>
#else
/* Header files for VMS version */
#include <decw$include/Xlib.h>
#include <decw$include/Xutil.h>
#include <decw$include/Intrinsic.h>
#include <decw$include/Xatom.h>
#include <decw$include/keysym.h>
#endif /* VMS */

#define INITIAL_MAXWIN 100
#define AUDIO_DEVICE "/dev/audio"
#define FILENAME "shooting.au"

static void BlastWindow();
static void SaveWindow();
static Window GetWindowToBlast();
static void RepairWindows();
static void MakeNoise();
static FILE* OpenSoundFile();
static void CloseSoundFile();

typedef struct 
{
  Window window;
  Bool  bshaped;  
  XRectangle *bound;
  int bcount;
  int border;  
  Bool cshaped;  
  XRectangle *clip;
  int ccount;
  int corder;  
}
WinList;

main( argc, argv )
int argc;
char *argv[];
{
  Display *display;
  Window window, root;
  long screen;  
  GC gc;
  XEvent event;
  XGCValues gcvalues;   
  Pixmap shape_mask;
  WinList *winlist;
  int numwin = 0;  
  int maxwin = INITIAL_MAXWIN;
  FILE *fp, *afp;  

  /* Open display connection and create a window */
  if( (display = XOpenDisplay( "" )) == (Display*)NULL ) 
  {
    fprintf( stderr, "Invalid display\n" );
    exit(0);
  }

  winlist = (WinList*)malloc( maxwin * sizeof(WinList) );
  screen = DefaultScreen(display);
  root = RootWindow( display, screen );  

  gcvalues.foreground = 1;
  gcvalues.background = 0;
  shape_mask = XCreatePixmap( display, root,
			     DisplayWidth(display,screen), 
			     DisplayHeight(display,screen), 1);
  gc = XCreateGC(display, shape_mask, GCForeground | GCBackground, &gcvalues);

  afp = OpenSoundFile();  

  /* Main event loop */
  for(;;)
  {
    window = GetWindowToBlast(display, screen, &event, winlist, 
			      &numwin, &maxwin);
    switch( event.xbutton.button )
    {
    case Button1:
      BlastWindow( display, window, gc, screen, shape_mask, afp );
      break;
    case Button2:
      RepairWindows( display, root, winlist, numwin );
      break;
    case Button3:
      CloseSoundFile( afp );
      exit(0);      
    }
  }
}


static void BlastWindow( display, window, gc, screen, shape_mask, afp )
Display *display;
Window window;
GC gc;
long screen;
Pixmap shape_mask;
FILE *afp;
{
  int x, y, root_x, root_y;
  unsigned int bwidth, dummy;
  unsigned int key_buttons;
  Window root, child;

  XGetGeometry( display, window, &root, &x, &y,
	       &dummy, &dummy, &bwidth, &dummy );  

#if 0
  window = XmuClientWindow (display, window);
#endif

  XSetForeground( display, gc, 1 );
  XFillRectangle( display, shape_mask, gc, 0, 0, 
		 DisplayWidth(display,screen),
		 DisplayHeight(display,screen) );
  
  XQueryPointer(display, window, &root, &child, &root_x, 
		&root_y, &x, &y, &key_buttons );

  XSetForeground( display, gc, 0 );
  XFillArc( display, shape_mask, gc, x-25, y-25, 50, 50, 0, 360 * 64);
  XShapeCombineMask(display,window,ShapeBounding, -bwidth, -bwidth, 
		    shape_mask, 
		    ShapeIntersect );
  MakeNoise(afp, FILENAME);  
}


static Window GetWindowToBlast(display, screen, event, winlist, numwin, maxwin)
Display *display;
long screen;
XEvent *event;
WinList *winlist;
int *numwin, *maxwin;
{
  int status;
  Cursor cursor;
  Window target_win = None, root = RootWindow(display,screen);
  int buttons = 0;

  /* Make the target cursor */
  cursor = XCreateFontCursor(display, XC_crosshair);

  /* Grab the pointer using target cursor, letting it room all over */
  status = XGrabPointer(display, root, False,
			ButtonPressMask|ButtonReleaseMask, GrabModeSync,
			GrabModeAsync, root, cursor, CurrentTime);
#if 0
  if (status != GrabSuccess) Fatal_Error("Can't grab the mouse.");
#endif

  /* Let the user select a window... */
  while ((target_win == None) || (buttons != 0)) {
    /* allow one more event */
    XAllowEvents(display, SyncPointer, CurrentTime);
    XWindowEvent(display, root, ButtonPressMask|ButtonReleaseMask, event);
    switch (event->type) 
    {
    case ButtonPress:
      if (target_win == None) 
      {
	target_win = event->xbutton.subwindow; /* window selected */
	if (target_win == None) target_win = root;
      }
      buttons++;
      break;
    case ButtonRelease:
      if (buttons > 0) /* there may have been some down before we started */
	buttons--;
       break;
    }
  } 

  XUngrabPointer(display, CurrentTime);      /* Done with pointer */
  SaveWindow( display, target_win, winlist, numwin, maxwin );  
  return(target_win);
}

static void RepairWindows( display, window, winlist, numwin )
Display *display;
Window window;
WinList *winlist;
int numwin;
{
  Window root, parent, *children;
  int i, num_children;  
#ifdef REPAIR_EVERYTHING_IN_SIGHT
  XQueryTree( display, window, &root, &parent, &children, &num_children );
  for( i=0; i < num_children; i++ )
    XShapeCombineMask(display, children[i], ShapeBounding, 0, 0, 
		      None, ShapeSet );
  XFree( *children );
#endif
  for( i=0; i < numwin; i++ )
  {    
    if( winlist[i].bshaped )
      XShapeCombineRectangles( display, winlist[i].window, ShapeBounding,
			    0, 0, winlist[i].bound, winlist[i].bcount,
			    ShapeSet, winlist[i].border );
    else
      XShapeCombineMask(display, winlist[i].window, ShapeBounding, 0, 0, 
			None, ShapeSet );
      
    if( winlist[i].cshaped )
      XShapeCombineRectangles( display, winlist[i].window, ShapeClip,
			    0, 0, winlist[i].clip, winlist[i].ccount,
			    ShapeSet, winlist[i].corder );
    else
      XShapeCombineMask(display, winlist[i].window, ShapeClip, 0, 0, 
			None, ShapeSet );
  } 
}

static void SaveWindow( display, window, winlist, numwin, maxwin )
Display *display;
Window window;
WinList *winlist;
int *numwin, *maxwin;
{
  int i, count, order, dummy;  
  Bool bshaped, cshaped;
  unsigned int udummy;
  
  for( i=0; i < *numwin; i++ )
    if( window == winlist[i].window )
      return;
  winlist[*numwin].window = window;  

  XShapeQueryExtents(display, window, &bshaped, 
		     &dummy, &dummy, &udummy, &udummy,
		     &cshaped, &dummy, &dummy, &udummy, &udummy);
  
  if( winlist[*numwin].bshaped = bshaped )
  {
    winlist[*numwin].bound = XShapeGetRectangles( display, window, 
					     ShapeBounding, &count, &order );
    winlist[*numwin].bcount = count;
    winlist[*numwin].border = order;  
  }
  
  if( winlist[*numwin].cshaped = cshaped )
  {    
    winlist[*numwin].clip = XShapeGetRectangles( display, window, ShapeClip, 
					    &count, &order );
    winlist[*numwin].ccount = count;
    winlist[*numwin].corder = order;  
  }
  
  (*numwin)++;

  if( *numwin >= *maxwin )
  {    
    winlist = (WinList*)realloc( winlist, 2 * (*maxwin) * sizeof(WinList) );
    *maxwin *= 2;
  }  
  
}

static void MakeNoise( afp, filename )
FILE *afp;
String filename;
{
  unsigned char buf[1024*64];
  int cnt;
  FILE *fp;  
#ifdef sun4  
  fp = (FILE*)open( filename, O_RDONLY, 0 );  
  while( (cnt = read(fp, (char*)buf, sizeof(buf))) > 0 )
    write( afp, (char*)buf, cnt );
  close(fp);  
#endif
}

static FILE* OpenSoundFile()
{
#ifdef sun4
  return (FILE*)open( AUDIO_DEVICE, O_WRONLY | O_NDELAY );  
#endif
}

static void CloseSoundFile( afp )
FILE *afp;
{
#ifdef sun4
  close( afp );
#endif
}