Fex

Check-in [6ba49fe548]

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:code cleanup
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk | master
Files: files | file ages | folders
SHA3-256: 6ba49fe548a885f66f3ffc8f06835a9224b30d13949d26ce7d258d8987bd5ae7
User & Date: jesse@mccluresk9.com 2014-10-07 17:37:11
Context
2015-01-07
20:10
frequency trace exporing to work with WarpZone check-in: 2cc45f4303 user: jesse@mccluresk9.com tags: trunk, master
2014-10-07
17:37
code cleanup check-in: 6ba49fe548 user: jesse@mccluresk9.com tags: trunk, master
17:07
strict -Wall -Werror patching up check-in: 9d6841b701 user: jesse@mccluresk9.com tags: trunk, master
Changes

Changes to Makefile.

9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
MODULES  =  config fex fft spectro wave xlib
HEADERS  =  fex.h
MANPAGES =  fex.1 fex-help.1
VPATH    =  src:doc

${PROG}: ${MODULES:%=%.o}

xlib.o: xlib.c xlib_toolwin.c ${HEADERS}

config.o: config.c config.h ${HEADERS}

%.o: %.c ${HEADERS}

install: ${PROG}
	@install -Dm755 ${PROG} ${DESTDIR}${PREFIX}/bin/${PROG}







|







9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
MODULES  =  config fex fft spectro wave xlib
HEADERS  =  fex.h
MANPAGES =  fex.1 fex-help.1
VPATH    =  src:doc

${PROG}: ${MODULES:%=%.o}

xlib.o: xlib.c xlib_toolwin.c xlib_events.c xlib_actions.c ${HEADERS}

config.o: config.c config.h ${HEADERS}

%.o: %.c ${HEADERS}

install: ${PROG}
	@install -Dm755 ${PROG} ${DESTDIR}${PREFIX}/bin/${PROG}

Changes to src/config.c.

163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
		conf.help_cmd[i+1] = NULL;
		help_arg = strtok(NULL," ");
	}
	/* return audio file name */
	return fname;
}

int deconfig() {
	/* clean up, free data */
	int i;
	for (i = 0; conf.help_cmd[i]; i++) free(conf.help_cmd[i]);
	free(conf.help_cmd);
	cairo_font_face_destroy(conf.font);
	cairo_font_face_destroy(conf.bfont);
	return 0;
}







|








163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
		conf.help_cmd[i+1] = NULL;
		help_arg = strtok(NULL," ");
	}
	/* return audio file name */
	return fname;
}

int deconfigure() {
	/* clean up, free data */
	int i;
	for (i = 0; conf.help_cmd[i]; i++) free(conf.help_cmd[i]);
	free(conf.help_cmd);
	cairo_font_face_destroy(conf.font);
	cairo_font_face_destroy(conf.bfont);
	return 0;
}

Changes to src/fex.c.

47
48
49
50
51
52
53

54
55
	else if (conf.long_out)
		fprintf(stdout,"NA\tNA\tNA\n");
	else
		fprintf(stdout,"NA\n");

	free_spectro();
	free_fft(&fft);

	return 0;
}







>


47
48
49
50
51
52
53
54
55
56
	else if (conf.long_out)
		fprintf(stdout,"NA\tNA\tNA\n");
	else
		fprintf(stdout,"NA\n");

	free_spectro();
	free_fft(&fft);
	deconfigure();
	return 0;
}

Changes to src/fex.h.

104
105
106
107
108
109
110

111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
	Bool long_out, layers;
} Config;

/* main.c */
extern int die(const char *, ...);
/* config.c */
extern const char *configure(int, const char **);

/* fft.c */
extern FFT *create_fft(Wave *);
extern int free_fft(FFT **);
/* spectro.c */
extern int create_spectro(FFT *, const char *);
extern int free_spectro();
extern int spectro_spec();
extern int spectro_thresh();
extern int spectro_points();
extern int spectro_draw();
/* wave.c */
extern Wave *create_wave(const char *);
extern int free_wave(Wave **);
/* xlib.c */
extern int create_xlib();
extern int free_xlib();
extern cairo_t *xlib_context();







>






<
<
<
<







104
105
106
107
108
109
110
111
112
113
114
115
116
117




118
119
120
121
122
123
124
	Bool long_out, layers;
} Config;

/* main.c */
extern int die(const char *, ...);
/* config.c */
extern const char *configure(int, const char **);
extern int deconfigure();
/* fft.c */
extern FFT *create_fft(Wave *);
extern int free_fft(FFT **);
/* spectro.c */
extern int create_spectro(FFT *, const char *);
extern int free_spectro();




/* wave.c */
extern Wave *create_wave(const char *);
extern int free_wave(Wave **);
/* xlib.c */
extern int create_xlib();
extern int free_xlib();
extern cairo_t *xlib_context();

Changes to src/spectro.c.

23
24
25
26
27
28
29












































30
31
32
33
34
35
36
..
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
...
207
208
209
210
211
212
213









































#include "fex.h"

#define set_color(x,n)	{	\
	cairo_set_source_rgba(x, conf.col[n].r, conf.col[n].g,	\
			conf.col[n].b, conf.col[n].a);							\
	cairo_set_line_width(x, conf.col[n].w);						\
}













































int img_draw() {
	cairo_surface_t *img;
	img = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, spect->fft_w*conf.scale, spect->fft_h*conf.scale * 4);
	cairo_t *ctx;
	ctx = cairo_create(img);
	cairo_scale(ctx,conf.scale,-4.0 * conf.scale);
................................................................................
		cairo_set_source_surface(c,spect->s_points,0,0);
		cairo_paint(c);
	}
	cairo_destroy(c);
	return 0;
}

int create_spectro(FFT *fft, const char *fname) {
	spect = (Spectro *) calloc(1, sizeof(Spectro));
	/* names */
	spect->fname = fname;
	char *c = strrchr(spect->fname,'/');
	if (c && (++c)) spect->name = strdup(c);
	else spect->name = strdup(fname);
	if ( (c=strrchr(spect->name,'.')) ) *c = '\0';
	/* fft */
	spect->fft = fft;
	spect->fft_w = fft->ntime;
	spect->fft_h = fft->nfreq;
	/* set bounds */
	int i;
	for (i = 0; i < spect->fft->nfreq &&
			spect->fft->freq[i] < conf.hipass; i++);
	spect->fft_lo = spect->fft_y = i;
	for (i++; i < spect->fft->nfreq &&
			spect->fft->freq[i] < conf.lopass; i++);
	spect->fft_hi = i;
	spect->fft_h = i - spect->fft_y;
	/* cairo_surfaces */
	spectro_spec();
	spectro_thresh();
	spectro_points();
	/* xlib */
	create_xlib();
	return 0;
}

int free_spectro() {
	cairo_surface_destroy(spect->m_spec);
	cairo_surface_destroy(spect->m_thresh);
	cairo_surface_destroy(spect->s_points);
	free_xlib();
	free(spect->name);
	free(spect);
	return 0;
}

int spectro_spec() {
	if (spect->m_spec) cairo_surface_destroy(spect->m_spec);
	if (spect->a_spec) free(spect->a_spec);
	int i, j, stride;
	stride = cairo_format_stride_for_width(CAIRO_FORMAT_A8, spect->fft_w);
	spect->a_spec = (unsigned char *) calloc(stride,spect->fft_h);
	unsigned char *a = NULL;
	for (j = spect->fft_y; j < spect->fft_h + spect->fft_y; j++) {
		a = spect->a_spec + (j - spect->fft_y) * stride;
		for (i = spect->fft_x; i < spect->fft_w + spect->fft_x; i++, a++) {
			*a = 255  - (unsigned char) 255 * (
					( (spect->fft->amp[i][j] - conf.spect_floor) >= 0) ?
						spect->fft->amp[i][j] / conf.spect_floor : 1);
		}
	}
	spect->m_spec = cairo_image_surface_create_for_data(spect->a_spec,
			CAIRO_FORMAT_A8, spect->fft_w, spect->fft_h, stride);
	return 0;
}

int spectro_thresh() {
	if (spect->m_thresh) cairo_surface_destroy(spect->m_thresh);
	if (spect->a_thresh) free(spect->a_thresh);
	int i, j, stride;
	stride = cairo_format_stride_for_width(CAIRO_FORMAT_A8, spect->fft_w);
	spect->a_thresh = (unsigned char *) calloc(stride, spect->fft_h);
	unsigned char *a = NULL;
	for (j = spect->fft_y; j < spect->fft_h + spect->fft_y; j++) {
		a = spect->a_thresh + (j - spect->fft_y) * stride;
		for (i = spect->fft_x; i < spect->fft_w + spect->fft_x; i++, a++) {
			if (spect->fft->mask[i][j]) *a = 0;
			else if (spect->fft->amp[i][j] > conf.thresh) *a = 255;
			else *a = 0;
		}
	}
	spect->m_thresh = cairo_image_surface_create_for_data(spect->a_thresh,
			CAIRO_FORMAT_A8, spect->fft_w, spect->fft_h, stride);
	return 0;
}

int spectro_points() {
	if (spect->s_points) cairo_surface_destroy(spect->s_points);
	spect->s_points = cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
			//spect->fft_w, spect->fft_h);
			spect->fft_w * conf.scale, spect->fft_h * conf.scale);
	cairo_t *p = cairo_create(spect->s_points);
	cairo_t *l = cairo_create(spect->s_points);
................................................................................
	}
	cairo_fill(p);
	cairo_stroke(l);
	cairo_destroy(p);
	cairo_destroy(l);
	return 0;
}
















































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







 







<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<







 







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
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
...
123
124
125
126
127
128
129
















































































130
131
132
133
134
135
136
...
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
#include "fex.h"

#define set_color(x,n)	{	\
	cairo_set_source_rgba(x, conf.col[n].r, conf.col[n].g,	\
			conf.col[n].b, conf.col[n].a);							\
	cairo_set_line_width(x, conf.col[n].w);						\
}

int create_spectro(FFT *fft, const char *fname) {
	spect = (Spectro *) calloc(1, sizeof(Spectro));
	/* names */
	spect->fname = fname;
	char *c = strrchr(spect->fname,'/');
	if (c && (++c)) spect->name = strdup(c);
	else spect->name = strdup(fname);
	if ( (c=strrchr(spect->name,'.')) ) *c = '\0';
	/* fft */
	spect->fft = fft;
	spect->fft_w = fft->ntime;
	spect->fft_h = fft->nfreq;
	/* set bounds */
	int i;
	for (i = 0; i < spect->fft->nfreq &&
			spect->fft->freq[i] < conf.hipass; i++);
	spect->fft_lo = spect->fft_y = i;
	for (i++; i < spect->fft->nfreq &&
			spect->fft->freq[i] < conf.lopass; i++);
	spect->fft_hi = i;
	spect->fft_h = i - spect->fft_y;
	/* cairo_surfaces */
	spectro_spec();
	spectro_thresh();
	spectro_points();
	/* xlib */
	create_xlib();
	return 0;
}

int free_spectro() {
	cairo_surface_destroy(spect->m_spec);
	cairo_surface_destroy(spect->m_thresh);
	cairo_surface_destroy(spect->s_points);
	free_xlib();
	free(spect->name);
	free(spect);
	return 0;
}





int img_draw() {
	cairo_surface_t *img;
	img = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, spect->fft_w*conf.scale, spect->fft_h*conf.scale * 4);
	cairo_t *ctx;
	ctx = cairo_create(img);
	cairo_scale(ctx,conf.scale,-4.0 * conf.scale);
................................................................................
		cairo_set_source_surface(c,spect->s_points,0,0);
		cairo_paint(c);
	}
	cairo_destroy(c);
	return 0;
}

















































































int spectro_points() {
	if (spect->s_points) cairo_surface_destroy(spect->s_points);
	spect->s_points = cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
			//spect->fft_w, spect->fft_h);
			spect->fft_w * conf.scale, spect->fft_h * conf.scale);
	cairo_t *p = cairo_create(spect->s_points);
	cairo_t *l = cairo_create(spect->s_points);
................................................................................
	}
	cairo_fill(p);
	cairo_stroke(l);
	cairo_destroy(p);
	cairo_destroy(l);
	return 0;
}

int spectro_spec() {
	if (spect->m_spec) cairo_surface_destroy(spect->m_spec);
	if (spect->a_spec) free(spect->a_spec);
	int i, j, stride;
	stride = cairo_format_stride_for_width(CAIRO_FORMAT_A8, spect->fft_w);
	spect->a_spec = (unsigned char *) calloc(stride,spect->fft_h);
	unsigned char *a = NULL;
	for (j = spect->fft_y; j < spect->fft_h + spect->fft_y; j++) {
		a = spect->a_spec + (j - spect->fft_y) * stride;
		for (i = spect->fft_x; i < spect->fft_w + spect->fft_x; i++, a++) {
			*a = 255  - (unsigned char) 255 * (
					( (spect->fft->amp[i][j] - conf.spect_floor) >= 0) ?
						spect->fft->amp[i][j] / conf.spect_floor : 1);
		}
	}
	spect->m_spec = cairo_image_surface_create_for_data(spect->a_spec,
			CAIRO_FORMAT_A8, spect->fft_w, spect->fft_h, stride);
	return 0;
}

int spectro_thresh() {
	if (spect->m_thresh) cairo_surface_destroy(spect->m_thresh);
	if (spect->a_thresh) free(spect->a_thresh);
	int i, j, stride;
	stride = cairo_format_stride_for_width(CAIRO_FORMAT_A8, spect->fft_w);
	spect->a_thresh = (unsigned char *) calloc(stride, spect->fft_h);
	unsigned char *a = NULL;
	for (j = spect->fft_y; j < spect->fft_h + spect->fft_y; j++) {
		a = spect->a_thresh + (j - spect->fft_y) * stride;
		for (i = spect->fft_x; i < spect->fft_w + spect->fft_x; i++, a++) {
			if (spect->fft->mask[i][j]) *a = 0;
			else if (spect->fft->amp[i][j] > conf.thresh) *a = 255;
			else *a = 0;
		}
	}
	spect->m_thresh = cairo_image_surface_create_for_data(spect->a_thresh,
			CAIRO_FORMAT_A8, spect->fft_w, spect->fft_h, stride);
	return 0;
}

Changes to src/xlib.c.

25
26
27
28
29
30
31

32




33
34
35
36
37
38
39
..
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
...
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
#define MODE_NULL		0x00
#define MODE_ERASE	0x01
#define MODE_CROP		0x02

#define EVENT_MASK	ExposureMask | ButtonPressMask | KeyPressMask | \
							StructureNotifyMask


extern int img_draw();





static void buttonpress(XEvent *);
static void clientmessage(XEvent *);
static void configurenotify(XEvent *);
static void expose(XEvent *);
static void keypress(XEvent *);
static void motionnotify(XEvent *);
................................................................................
	[ConfigureNotify]		= configurenotify,
	[Expose]					= expose,
	[KeyPress]				= keypress,
	[MotionNotify]			= motionnotify,
};

#include "xlib_toolwin.c"


#define CtrlMask	ControlMask
void buttonpress(XEvent *ev) {
	XButtonEvent *e = &ev->xbutton;
	if (e->window == info->win) info->button(info, e);
	else if (e->state == (ControlMask | ShiftMask)) {
		if (e->button == 4) threshold(0.1);
		else if (e->button == 5) threshold(-0.1);
		else if (e->button == 6) sp_floor(-0.1);
		else if (e->button == 7) sp_floor(0.1);
	}
	else if (e->state == ControlMask) {
		if (e->button == 4) zoom(0.025);
		else if (e->button == 5) zoom(-0.025);
		else if (e->button == 6) return;
		else if (e->button == 7) return;
	}
	else if (e->state == Mod1Mask) {
		if (e->button == 4) eraser_cursor(1,1);
		else if (e->button == 5) eraser_cursor(-1,-1);
		else if (e->button == 6) eraser_cursor(-1,1);
		else if (e->button == 7) eraser_cursor(1,-1);
	}
	else if (e->state == ShiftMask) {
		if (e->button == 4) pt_line(0.2,0);
		else if (e->button == 5) pt_line(-0.2,0);
		else if (e->button == 6) pt_line(0,-0.2);
		else if (e->button == 7) pt_line(0,0.2);
	}
	else if (e->button == 4) move(0,-0.02);
	else if (e->button == 5) move(0,0.02);
	else if (e->button == 6) move(0.02,0);
	else if (e->button == 7) move(-0.02,0);
	else if (mode == MODE_ERASE && e->button == 1) erase(e->x, e->y);
	else if (mode == MODE_ERASE && e->button == 3) erase(-1,-1);
	else if (mode == MODE_CROP && e->button == 1) crop(e->x, e->y);
	while(XCheckMaskEvent(dpy, ButtonPressMask, ev));
}

void clientmessage(XEvent *ev) {
	XClientMessageEvent *e = &ev->xclient;
	if ( (Atom)e->data.l[0] == WM_DELETE_WINDOW ) {
		if (e->window == info->win) {
			info->vis = False;
			XUnmapWindow(dpy, info->win);
		}
		else if (e->window == win) {
			running = False;
		}
	}
}

void configurenotify(XEvent *ev) {
	while (XCheckTypedEvent(dpy,ConfigureNotify, ev));
	XConfigureEvent *e = &ev->xconfigure;
	if (e->window == win) {
		/* The XServer is not storing the new/current values for width +
		 * height.  These must be retrieved directly.
		 * Is this an X11 bug? */
		Window wig;
		int ig;
		unsigned int uig;
		XGetGeometry(dpy, win, &wig, &ig, &ig, &ww, &wh, &uig, &uig);
		//ww = e->width; wh = e->height;
		spectro_draw();
		XSetWindowBackgroundPixmap(dpy, win, buf);
		XClearWindow(dpy,win);
	}
}

void expose(XEvent *ev) {
	XExposeEvent *e = &ev->xexpose;
	if (e->window == info->win) info->draw(info);
	else {
		XSetWindowBackgroundPixmap(dpy, win, buf);
		XClearWindow(dpy,win);
	}
}

void keypress(XEvent *ev) {
	XKeyEvent *e = &ev->xkey;
	KeySym sym = XkbKeycodeToKeysym(dpy, (KeyCode)e->keycode, 0, 0);
	int mod = ((e->state & ~Mod2Mask) & ~LockMask);
	if (mod == (ControlMask | ShiftMask)) {
		if (sym == XK_q) {
			spect->fex = 0;
			running = False;
		}
		if (sym == XK_j || sym == XK_Down) threshold(-0.05);
		else if (sym == XK_k || sym == XK_Up) threshold(0.05);
		else if (sym == XK_h || sym == XK_Left) sp_floor(-0.05);
		else if (sym == XK_l || sym == XK_Right) sp_floor(0.05);
		else if (sym == XK_p) play(0.1666);
	}
	else if (mod == (Mod1Mask | ShiftMask)) {
		if (sym == XK_p) play(0.08333);
	}
	else if (mod == ControlMask) {
		if (sym == XK_q) running = False;
		else if (sym == XK_s) screenshot();
		else if (sym == XK_i) img_draw();
		else if (sym == XK_j || sym == XK_Down) zoom(-0.025);
		else if (sym == XK_k || sym == XK_Up) zoom(0.025);
		else if (sym == XK_h || sym == XK_Left) return;
		else if (sym == XK_l || sym == XK_Right) return;
		else if (sym == XK_p) play(0.33);
	}
	else if (mod == Mod1Mask) {
		if (sym == XK_j || sym == XK_Down) eraser_cursor(-1,-1);
		else if (sym == XK_k || sym == XK_Up) eraser_cursor(1,1);
		else if (sym == XK_h || sym == XK_Left) eraser_cursor(-1,1);
		else if (sym == XK_l || sym == XK_Right) eraser_cursor(1,-1);
		else if (sym == XK_p) play(0.25);
	}
	else if (mod == ShiftMask) {
		if (sym == XK_j || sym == XK_Down) pt_line(-0.2,0);
		else if (sym == XK_k || sym == XK_Up) pt_line(0.2,0);
		else if (sym == XK_h || sym == XK_Left) pt_line(0,-0.2);
		else if (sym == XK_l || sym == XK_Right) pt_line(0,0.2);
		else if (sym == XK_p) play(0.5);
	}
	else if (sym == XK_j || sym == XK_Down) move(0,0.02);
	else if (sym == XK_k || sym == XK_Up) move(0,-0.02);
	else if (sym == XK_h || sym == XK_Left) move(0.02,0);
	else if (sym == XK_l || sym == XK_Right) move(-0.02,0);
	else if (sym == XK_F1) {
		if (fork() == 0)
			execvp(conf.help_cmd[0],(char * const *)conf.help_cmd);
	}
	else if (sym == XK_F2) {
		if ( (info->vis = !info->vis) ) XMapRaised(dpy,info->win);
		else XUnmapWindow(dpy,info->win);
		XFlush(dpy);
	}
	else if (sym == XK_e) {
		//mode = MODE_ERASE & (mode ^= MODE_ERASE);
		mode = (mode & MODE_ERASE ? 0 : MODE_ERASE);
		eraser_cursor(0,0);
		info->draw(info);
	}
	else if (sym == XK_c) {
		//mode = MODE_CROP & (mode ^= MODE_CROP);
		mode = (mode & MODE_CROP ? 0 : MODE_CROP);
		if (!(mode & MODE_CROP)) XDefineCursor(dpy, win, None);
		else XDefineCursor(dpy, win, XCreateFontCursor(dpy, 34));
		info->draw(info);
	}
	else if (sym == XK_Escape) {
		mode = MODE_NULL;
		XDefineCursor(dpy, win, None);
		info->draw(info);
	}
	else if (sym == XK_p) play(1.0);
	else if (sym == XK_t) {
		conf.layers = !conf.layers;
		spectro_draw();
		XCopyArea(dpy, buf, win, gc, 0, 0, ww, wh, 0, 0);
	}
	else if (sym == XK_u && mode & (MODE_ERASE)) erase(-1,-1);
	while(XCheckMaskEvent(dpy, KeyPressMask, ev));
}

void motionnotify(XEvent *ev) {
	//static int px, py;
	int x = ev->xmotion.x, y = ev->xmotion.y;
	mx = spect->fft_w * x / (xsc * ww) - 1.0 * xoff + spect->fft_x;
	my = spect->fft_h * (1.0 - y / (ysc * wh)) - 1.0 * yoff + spect->fft_y;
	info->draw(info);
	if (mode == MODE_CROP) {
		XCopyArea(dpy, buf, win, gc, 0, 0, ww, wh, 0, 0);
		XDrawLine(dpy, win, gc, x, 0, x, wh);
		XDrawLine(dpy, win, gc, 0, y, ww, y);
	}
}


int crop(int x, int y) {
	int x1 = x, y1 = y, x2, y2, t;
	XCopyArea(dpy, win, buf, gc, 0, 0, ww, wh, 0, 0);
	XEvent e;
	XGrabPointer(dpy, win, True, PointerMotionMask | ButtonPressMask,
			GrabModeAsync, GrabModeAsync, None, None, CurrentTime);
	while (e.type != ButtonPress) {
		XMaskEvent(dpy, PointerMotionMask | ButtonPressMask |
				KeyPressMask, &e);
		if (e.type == KeyPress) break;
		x2 = e.xmotion.x;
		y2 = e.xmotion.y;
		if (x2 > ww) x2 = ww;
		if (y2 > wh) y2 = wh;
		mx = spect->fft_w * x2/(ww*xsc) - 1.0 * xoff + spect->fft_x;
		my = spect->fft_h * (1.0-y2/(wh*ysc)) - 1.0 * yoff + spect->fft_y;
		XCopyArea(dpy, buf, win, gc, 0, 0, ww, wh, 0, 0);
		XDrawLine(dpy, win, gc, x2, 0, x2, wh);
		XDrawLine(dpy, win, gc, 0, y2, ww, y2);
		info_draw(info);
		if (e.type == ButtonPress) break;
		while(XCheckMaskEvent(dpy, PointerMotionMask, &e));
	}
	XUngrabPointer(dpy, CurrentTime);
	if (x2 < x1) { t = x1; x1 = x2; x2 = t; }
	if (y2 > y1) { t = y1; y1 = y2; y2 = t; }
	mode = MODE_NULL;
	info_draw(info);
	if (e.type == KeyPress) {
		spectro_draw();
		XCopyArea(dpy, buf, win, gc, 0, 0, ww, wh, 0, 0);
		return 0;
	}
	int oldy = spect->fft_y;
	spect->fft_x = spect->fft_w * x1/(ww*xsc) - 1.0*xoff + spect->fft_x;
	spect->fft_y = spect->fft_h*(1.0-y1/(wh*ysc))-1.0*yoff+spect->fft_y;
	spect->fft_w = spect->fft_w * x2/(ww*xsc) - 1.0*xoff - spect->fft_x;
	spect->fft_h = spect->fft_h*(1.0-y2/(wh*ysc))-1.0*yoff +
			oldy - spect->fft_y;
	spectro_spec();
	spectro_thresh();
	spectro_points();
	spectro_draw();
	XCopyArea(dpy, buf, win, gc, 0, 0, ww, wh, 0, 0);
	return 0;
}

int erase(int x, int y) {
	int x1, x2, y1, y2, i, j;
	double sew = spect->fft_w * ew / (ww * xsc);
	double seh = spect->fft_h * eh / (wh * ysc);
	if (x == -1 && y == -1) { /* UNDO LAST ERASE */
		for (i = 0; i < spect->fft->ntime; i++)
			for (j = 0; j < spect->fft->nfreq; j++)
				spect->fft->mask[i][j] = ( (spect->fft->mask[i][j] >> 7) ?
						(spect->fft->mask[i][j] >> 1) | 0x40 :
						(spect->fft->mask[i][j] >> 1) );
		spectro_points();
		spectro_draw();
		XCopyArea(dpy, buf, win, gc, 0, 0, ww, wh, 0, 0);
		return 0;
	} /* ELSE START NEW ERASE */
	XEvent e;
	XGrabPointer(dpy, win, True, PointerMotionMask | ButtonReleaseMask,
			GrabModeAsync, GrabModeAsync, None, None, CurrentTime);
	/* rotate bits for 7 undo levels */
	for (i = 0; i < spect->fft->ntime; i++)
		for (j = 0; j < spect->fft->nfreq; j++)
			spect->fft->mask[i][j] = ( (spect->fft->mask[i][j] >> 7) ?
					(spect->fft->mask[i][j] << 1) | 0x40 :
					(spect->fft->mask[i][j] << 1) );
	while (e.type != ButtonRelease) {
		XMaskEvent(dpy, PointerMotionMask | ButtonReleaseMask, &e);
		/* set erase bit mask */
		mx = spect->fft_w*e.xbutton.x/(ww*xsc) - 1.0*xoff + spect->fft_x;
		my = spect->fft_h*(1.0-e.xbutton.y/(wh*ysc)) -1.0*yoff+spect->fft_y;
		x1 = mx - sew / 2.0; y1 = my - seh / 2.0;
		x2 = mx + sew / 2.0; y2 = my + seh / 2.0;
		if (x1 < 0) x1 = 0;
		if (y1 < 0) y1 = 0;
		if (x2 >= spect->fft->ntime) x2 = spect->fft->ntime - 1;
		if (y2 >= spect->fft->nfreq) y2 = spect->fft->nfreq - 1;
		for (j = y1; j <= y2; j++)
			for (i = x1; i <= x2; i++)
				spect->fft->mask[i][j] |= 0x01;
		/* redraw */
		spectro_thresh();
		spectro_points();
		spectro_draw();
		XCopyArea(dpy, buf, win, gc, 0, 0, ww, wh, 0, 0);
		info_draw(info);
		if (e.type == ButtonRelease) break;
		while(XCheckMaskEvent(dpy, PointerMotionMask, &e));
	}
	XUngrabPointer(dpy, CurrentTime);
	return 0;
}

int eraser_cursor(int w, int h) {
	XUndefineCursor(dpy, win);
	if ( !(mode & MODE_ERASE) ) return 0;
	if ( (ew+=w) < 3 ) ew = 3;
	if ( (eh+=h) < 3 ) eh = 3;
	if ( ew > ww/4 ) ew = ww / 4;
	if ( eh > wh/2 ) eh = wh / 2;
	int i, stride = ew/ 8 + 1;
	char data[stride * eh];
	char mask[stride * eh];
	XColor bg, fg;
	XAllocNamedColor(dpy,DefaultColormap(dpy,scr),e_col[0],&bg,&bg);
	XAllocNamedColor(dpy,DefaultColormap(dpy,scr),e_col[1],&fg,&fg);
	memset(mask, 0xAA, stride * eh);
	memset(data, 0xAA, stride * eh);
	for (i = 0; i < eh; i+= 2) memset(&mask[stride * i], 0x55, stride);
	Pixmap cd = XCreateBitmapFromData(dpy, win, data, ew, eh);
	Pixmap cm = XCreateBitmapFromData(dpy, win, mask, ew, eh);
	Cursor c = XCreatePixmapCursor(dpy, cd, cm, &fg, &bg, ew/2, eh/2);
	XDefineCursor(dpy, win, c);
	XFreePixmap(dpy, cd);
	XFreePixmap(dpy, cm);
	XFlush(dpy);
	return 0;
}

int move(double x, double y) {
	xoff += x / xsc * spect->fft_w; yoff += y / ysc * spect->fft_h;
	if ( yoff > spect->fft_h * (1.0 - 1.0/ysc) )
		yoff = spect->fft_h * (1.0 - 1.0/ysc);
	if ( yoff < 0) yoff = 0;
	if ( xoff < spect->fft_w * (1.0/xsc - 1.0) )
		xoff = spect->fft_w * (1.0/xsc - 1.0);
	if ( xoff > 0) xoff = 0;
	if (x || y) { /* don't redraw for offset checks */
		spectro_draw();
		XCopyArea(dpy, buf, win, gc, 0, 0, ww, wh, 0, 0);
	}
	return 0;
}

int play(float speed) {
	/* calculate fft coordinates of current view window */
	int tw = spect->fft_w / xsc;
	int th = spect->fft_h / ysc;
	int tx = spect->fft_x - xoff;
	int ty = spect->fft_y - yoff + spect->fft_h - th;
	if (tx + tw >= spect->fft->ntime) tw = spect->fft->ntime - tx - 1;
	if (ty + th >= spect->fft->nfreq) th = spect->fft->nfreq - ty - 1;
	/* get time and frequency values of current view window */
	double _start = spect->fft->time[tx];
	double _end = spect->fft->time[tx+tw];
	double _low = spect->fft->freq[ty];
	double _hi = spect->fft->freq[ty+th];
	double _mid = (_hi+_low)/2.0;
	double _wid = (_hi-_low)/2.0;
	/* fork and play */
	if (!fork()==0) {
		close(ConnectionNumber(dpy));
		char start[12], end[12], mid[12], wid[12], sp[8];
		snprintf(sp,7,"%3f",speed);
		sprintf(start,"=%lf",_start); sprintf(end,"=%lf",_end);
		sprintf(mid,"%lfk",_mid); sprintf(wid,"%lfk",_wid);
		execl("/usr/bin/play", "play", "-q", spect->fname,
				"trim", start, end, "bandpass", mid, wid,
				"speed", sp, NULL);
		perror("Fex Play");
		exit(1);
	}
	/* draw cursor while playing */
	int i, step = 12;
	double dur = _end - _start;
	useconds_t usec = dur * 1000000.0 * step / (double) ww;
	step = speed * step + 0.5;
	for (i = 0; i < ww; i+=step) {
		XCopyArea(dpy, buf, win, gc, 0, 0, ww, wh, 0, 0);
		XDrawLine(dpy, win, gc, i, 0, i, wh);
		XDrawLine(dpy, win, gc, i+step, 0, i+step, wh);
		XFlush(dpy);
		usleep(usec);
	}
	XCopyArea(dpy, buf, win, gc, 0, 0, ww, wh, 0, 0);
	XFlush(dpy);
	return 0;
}

int pt_line(double p, double l) {
	conf.col[RGBA_POINTS].w += p;
	conf.col[RGBA_LINES].w += l;
	if (conf.col[RGBA_POINTS].w < 0) conf.col[RGBA_POINTS].w = 0.0;
	if (conf.col[RGBA_LINES].w < 0) conf.col[RGBA_LINES].w = 0.0;
	spectro_points();
	spectro_draw();
	XCopyArea(dpy, buf, win, gc, 0, 0, ww, wh, 0, 0);
	return 0;
}

int screenshot() {
	char fname[256];
	static int n = 0;
	snprintf(fname, 256, "%s-%d.png", spect->name, n++);
	cairo_surface_t *t = cairo_xlib_surface_create(dpy, buf,
			DefaultVisual(dpy,scr), ww, wh);
	cairo_surface_write_to_png(t, fname);
	cairo_surface_destroy(t);
	return 0;
}

int sp_floor(double f) {
	conf.spect_floor += f;
	spectro_spec();
	spectro_draw();
	XCopyArea(dpy, buf, win, gc, 0, 0, ww, wh, 0, 0);
	if (info->vis) info->draw(info);
	return 0;
}

int threshold(double f) {
	conf.thresh += f;
	spectro_thresh();
	spectro_points();
	spectro_draw();
	XCopyArea(dpy, buf, win, gc, 0, 0, ww, wh, 0, 0);
	if (info->vis) info->draw(info);
	return 0;
}

int zoom(double f) {
	double px = xsc, py = ysc;
	xsc += f; ysc += f;
	if (xsc < 1.0) xsc = 1.0;
	if (ysc < 1.0) ysc = 1.0;
	xoff += (spect->fft_w/xsc - spect->fft_w/px) / 2.0;
	yoff += (spect->fft_h/py - spect->fft_h/ysc) / 2.0;
	if (f < 0) move(0,0); /* check offsets for bounds */
	spectro_draw();
	XCopyArea(dpy, buf, win, gc, 0, 0, ww, wh, 0, 0);
	return 0;
}

int create_xlib() {
	if (!setlocale(LC_CTYPE,"")) die("unable to set locale");
	if (!XSupportsLocale()) die("unsupported locale");
	if (!XSetLocaleModifiers("")) die("unable to set locale modifiers");
	if ( !(dpy=XOpenDisplay(0x0)) ) die("unable to open display");
	scr = DefaultScreen(dpy);
................................................................................
	XStoreName(dpy, win, "FEX: Frequency Excursion Calculator");
	XClassHint *hint = XAllocClassHint();
	hint->res_name = "FEX";
	hint->res_class = "FEX";
	XSetClassHint(dpy, win, hint);
	WM_DELETE_WINDOW = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
	XSetWMProtocols(dpy, win, &WM_DELETE_WINDOW, 1);
#include "icon.xpm"
Pixmap image, shape;
XWMHints *hints;
XpmCreatePixmapFromData(dpy, win, icon, &image, &shape, NULL);
hints = XAllocWMHints();
hints->flags = IconPixmapHint | IconMaskHint;
hints->icon_pixmap = image;
hints->icon_mask = shape;
XSetWMHints(dpy, win, hints);
XFree(hints);
	/* set up eraser / crop colors */
	sprintf(e_col[0],"#%02hhX%02hhX%02hhX",
		(unsigned char) conf.col[RGBA_ERASE1].r * 255,
		(unsigned char) conf.col[RGBA_ERASE1].g * 255,
		(unsigned char) conf.col[RGBA_ERASE1].b * 255);
	sprintf(e_col[1],"#%02hhX%02hhX%02hhX",
		(unsigned char) conf.col[RGBA_ERASE2].r * 255,







>

>
>
>
>







 







<
>
|
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<







 







|
|
|
|
|
|
|
|
|
|







25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
..
72
73
74
75
76
77
78

79
80



























































































































































































































































































































































































































81
82
83
84
85
86
87
..
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#define MODE_NULL		0x00
#define MODE_ERASE	0x01
#define MODE_CROP		0x02

#define EVENT_MASK	ExposureMask | ButtonPressMask | KeyPressMask | \
							StructureNotifyMask

/* spectro.c */
extern int img_draw();
extern int spectro_draw();
extern int spectro_points();
extern int spectro_spec();
extern int spectro_thresh();

static void buttonpress(XEvent *);
static void clientmessage(XEvent *);
static void configurenotify(XEvent *);
static void expose(XEvent *);
static void keypress(XEvent *);
static void motionnotify(XEvent *);
................................................................................
	[ConfigureNotify]		= configurenotify,
	[Expose]					= expose,
	[KeyPress]				= keypress,
	[MotionNotify]			= motionnotify,
};

#include "xlib_toolwin.c"

#include "xlib_actions.c"
#include "xlib_events.c"




























































































































































































































































































































































































































int create_xlib() {
	if (!setlocale(LC_CTYPE,"")) die("unable to set locale");
	if (!XSupportsLocale()) die("unsupported locale");
	if (!XSetLocaleModifiers("")) die("unable to set locale modifiers");
	if ( !(dpy=XOpenDisplay(0x0)) ) die("unable to open display");
	scr = DefaultScreen(dpy);
................................................................................
	XStoreName(dpy, win, "FEX: Frequency Excursion Calculator");
	XClassHint *hint = XAllocClassHint();
	hint->res_name = "FEX";
	hint->res_class = "FEX";
	XSetClassHint(dpy, win, hint);
	WM_DELETE_WINDOW = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
	XSetWMProtocols(dpy, win, &WM_DELETE_WINDOW, 1);
	#include "icon.xpm"
	Pixmap image, shape;
	XWMHints *hints;
	XpmCreatePixmapFromData(dpy, win, icon, &image, &shape, NULL);
	hints = XAllocWMHints();
	hints->flags = IconPixmapHint | IconMaskHint;
	hints->icon_pixmap = image;
	hints->icon_mask = shape;
	XSetWMHints(dpy, win, hints);
	XFree(hints);
	/* set up eraser / crop colors */
	sprintf(e_col[0],"#%02hhX%02hhX%02hhX",
		(unsigned char) conf.col[RGBA_ERASE1].r * 255,
		(unsigned char) conf.col[RGBA_ERASE1].g * 255,
		(unsigned char) conf.col[RGBA_ERASE1].b * 255);
	sprintf(e_col[1],"#%02hhX%02hhX%02hhX",
		(unsigned char) conf.col[RGBA_ERASE2].r * 255,

Added src/xlib_actions.c.











































































































































































































































































































































































































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
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
/**********************************************************************\
* FEX - The Frequency Excursion Calculator
*
* Author: Jesse McClure, copyright 2013-2014
* License: GPL3
*
*    This program 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 of the
*    License, or (at your option) any later version.
*
*    This program 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/>.
*
\**********************************************************************/


int crop(int x, int y) {
	int x1 = x, y1 = y, x2, y2, t;
	XCopyArea(dpy, win, buf, gc, 0, 0, ww, wh, 0, 0);
	XEvent e;
	XGrabPointer(dpy, win, True, PointerMotionMask | ButtonPressMask,
			GrabModeAsync, GrabModeAsync, None, None, CurrentTime);
	while (e.type != ButtonPress) {
		XMaskEvent(dpy, PointerMotionMask | ButtonPressMask |
				KeyPressMask, &e);
		if (e.type == KeyPress) break;
		x2 = e.xmotion.x;
		y2 = e.xmotion.y;
		if (x2 > ww) x2 = ww;
		if (y2 > wh) y2 = wh;
		mx = spect->fft_w * x2/(ww*xsc) - 1.0 * xoff + spect->fft_x;
		my = spect->fft_h * (1.0-y2/(wh*ysc)) - 1.0 * yoff + spect->fft_y;
		XCopyArea(dpy, buf, win, gc, 0, 0, ww, wh, 0, 0);
		XDrawLine(dpy, win, gc, x2, 0, x2, wh);
		XDrawLine(dpy, win, gc, 0, y2, ww, y2);
		info_draw(info);
		if (e.type == ButtonPress) break;
		while(XCheckMaskEvent(dpy, PointerMotionMask, &e));
	}
	XUngrabPointer(dpy, CurrentTime);
	if (x2 < x1) { t = x1; x1 = x2; x2 = t; }
	if (y2 > y1) { t = y1; y1 = y2; y2 = t; }
	mode = MODE_NULL;
	info_draw(info);
	if (e.type == KeyPress) {
		spectro_draw();
		XCopyArea(dpy, buf, win, gc, 0, 0, ww, wh, 0, 0);
		return 0;
	}
	int oldy = spect->fft_y;
	spect->fft_x = spect->fft_w * x1/(ww*xsc) - 1.0*xoff + spect->fft_x;
	spect->fft_y = spect->fft_h*(1.0-y1/(wh*ysc))-1.0*yoff+spect->fft_y;
	spect->fft_w = spect->fft_w * x2/(ww*xsc) - 1.0*xoff - spect->fft_x;
	spect->fft_h = spect->fft_h*(1.0-y2/(wh*ysc))-1.0*yoff +
			oldy - spect->fft_y;
	spectro_spec();
	spectro_thresh();
	spectro_points();
	spectro_draw();
	XCopyArea(dpy, buf, win, gc, 0, 0, ww, wh, 0, 0);
	return 0;
}

int erase(int x, int y) {
	int x1, x2, y1, y2, i, j;
	double sew = spect->fft_w * ew / (ww * xsc);
	double seh = spect->fft_h * eh / (wh * ysc);
	if (x == -1 && y == -1) { /* UNDO LAST ERASE */
		for (i = 0; i < spect->fft->ntime; i++)
			for (j = 0; j < spect->fft->nfreq; j++)
				spect->fft->mask[i][j] = ( (spect->fft->mask[i][j] >> 7) ?
						(spect->fft->mask[i][j] >> 1) | 0x40 :
						(spect->fft->mask[i][j] >> 1) );
		spectro_points();
		spectro_draw();
		XCopyArea(dpy, buf, win, gc, 0, 0, ww, wh, 0, 0);
		return 0;
	} /* ELSE START NEW ERASE */
	XEvent e;
	XGrabPointer(dpy, win, True, PointerMotionMask | ButtonReleaseMask,
			GrabModeAsync, GrabModeAsync, None, None, CurrentTime);
	/* rotate bits for 7 undo levels */
	for (i = 0; i < spect->fft->ntime; i++)
		for (j = 0; j < spect->fft->nfreq; j++)
			spect->fft->mask[i][j] = ( (spect->fft->mask[i][j] >> 7) ?
					(spect->fft->mask[i][j] << 1) | 0x40 :
					(spect->fft->mask[i][j] << 1) );
	while (e.type != ButtonRelease) {
		XMaskEvent(dpy, PointerMotionMask | ButtonReleaseMask, &e);
		/* set erase bit mask */
		mx = spect->fft_w*e.xbutton.x/(ww*xsc) - 1.0*xoff + spect->fft_x;
		my = spect->fft_h*(1.0-e.xbutton.y/(wh*ysc)) -1.0*yoff+spect->fft_y;
		x1 = mx - sew / 2.0; y1 = my - seh / 2.0;
		x2 = mx + sew / 2.0; y2 = my + seh / 2.0;
		if (x1 < 0) x1 = 0;
		if (y1 < 0) y1 = 0;
		if (x2 >= spect->fft->ntime) x2 = spect->fft->ntime - 1;
		if (y2 >= spect->fft->nfreq) y2 = spect->fft->nfreq - 1;
		for (j = y1; j <= y2; j++)
			for (i = x1; i <= x2; i++)
				spect->fft->mask[i][j] |= 0x01;
		/* redraw */
		spectro_thresh();
		spectro_points();
		spectro_draw();
		XCopyArea(dpy, buf, win, gc, 0, 0, ww, wh, 0, 0);
		info_draw(info);
		if (e.type == ButtonRelease) break;
		while(XCheckMaskEvent(dpy, PointerMotionMask, &e));
	}
	XUngrabPointer(dpy, CurrentTime);
	return 0;
}

int eraser_cursor(int w, int h) {
	XUndefineCursor(dpy, win);
	if ( !(mode & MODE_ERASE) ) return 0;
	if ( (ew+=w) < 3 ) ew = 3;
	if ( (eh+=h) < 3 ) eh = 3;
	if ( ew > ww/4 ) ew = ww / 4;
	if ( eh > wh/2 ) eh = wh / 2;
	int i, stride = ew/ 8 + 1;
	char data[stride * eh];
	char mask[stride * eh];
	XColor bg, fg;
	XAllocNamedColor(dpy,DefaultColormap(dpy,scr),e_col[0],&bg,&bg);
	XAllocNamedColor(dpy,DefaultColormap(dpy,scr),e_col[1],&fg,&fg);
	memset(mask, 0xAA, stride * eh);
	memset(data, 0xAA, stride * eh);
	for (i = 0; i < eh; i+= 2) memset(&mask[stride * i], 0x55, stride);
	Pixmap cd = XCreateBitmapFromData(dpy, win, data, ew, eh);
	Pixmap cm = XCreateBitmapFromData(dpy, win, mask, ew, eh);
	Cursor c = XCreatePixmapCursor(dpy, cd, cm, &fg, &bg, ew/2, eh/2);
	XDefineCursor(dpy, win, c);
	XFreePixmap(dpy, cd);
	XFreePixmap(dpy, cm);
	XFlush(dpy);
	return 0;
}

int move(double x, double y) {
	xoff += x / xsc * spect->fft_w; yoff += y / ysc * spect->fft_h;
	if ( yoff > spect->fft_h * (1.0 - 1.0/ysc) )
		yoff = spect->fft_h * (1.0 - 1.0/ysc);
	if ( yoff < 0) yoff = 0;
	if ( xoff < spect->fft_w * (1.0/xsc - 1.0) )
		xoff = spect->fft_w * (1.0/xsc - 1.0);
	if ( xoff > 0) xoff = 0;
	if (x || y) { /* don't redraw for offset checks */
		spectro_draw();
		XCopyArea(dpy, buf, win, gc, 0, 0, ww, wh, 0, 0);
	}
	return 0;
}

int play(float speed) {
	/* calculate fft coordinates of current view window */
	int tw = spect->fft_w / xsc;
	int th = spect->fft_h / ysc;
	int tx = spect->fft_x - xoff;
	int ty = spect->fft_y - yoff + spect->fft_h - th;
	if (tx + tw >= spect->fft->ntime) tw = spect->fft->ntime - tx - 1;
	if (ty + th >= spect->fft->nfreq) th = spect->fft->nfreq - ty - 1;
	/* get time and frequency values of current view window */
	double _start = spect->fft->time[tx];
	double _end = spect->fft->time[tx+tw];
	double _low = spect->fft->freq[ty];
	double _hi = spect->fft->freq[ty+th];
	double _mid = (_hi+_low)/2.0;
	double _wid = (_hi-_low)/2.0;
	/* fork and play */
	if (!fork()==0) {
		close(ConnectionNumber(dpy));
		char start[12], end[12], mid[12], wid[12], sp[8];
		snprintf(sp,7,"%3f",speed);
		sprintf(start,"=%lf",_start); sprintf(end,"=%lf",_end);
		sprintf(mid,"%lfk",_mid); sprintf(wid,"%lfk",_wid);
		execl("/usr/bin/play", "play", "-q", spect->fname,
				"trim", start, end, "bandpass", mid, wid,
				"speed", sp, NULL);
		perror("Fex Play");
		exit(1);
	}
	/* draw cursor while playing */
	int i, step = 12;
	double dur = _end - _start;
	useconds_t usec = dur * 1000000.0 * step / (double) ww;
	step = speed * step + 0.5;
	for (i = 0; i < ww; i+=step) {
		XCopyArea(dpy, buf, win, gc, 0, 0, ww, wh, 0, 0);
		XDrawLine(dpy, win, gc, i, 0, i, wh);
		XDrawLine(dpy, win, gc, i+step, 0, i+step, wh);
		XFlush(dpy);
		usleep(usec);
	}
	XCopyArea(dpy, buf, win, gc, 0, 0, ww, wh, 0, 0);
	XFlush(dpy);
	return 0;
}

int pt_line(double p, double l) {
	conf.col[RGBA_POINTS].w += p;
	conf.col[RGBA_LINES].w += l;
	if (conf.col[RGBA_POINTS].w < 0) conf.col[RGBA_POINTS].w = 0.0;
	if (conf.col[RGBA_LINES].w < 0) conf.col[RGBA_LINES].w = 0.0;
	spectro_points();
	spectro_draw();
	XCopyArea(dpy, buf, win, gc, 0, 0, ww, wh, 0, 0);
	return 0;
}

int screenshot() {
	char fname[256];
	static int n = 0;
	snprintf(fname, 256, "%s-%d.png", spect->name, n++);
	cairo_surface_t *t = cairo_xlib_surface_create(dpy, buf,
			DefaultVisual(dpy,scr), ww, wh);
	cairo_surface_write_to_png(t, fname);
	cairo_surface_destroy(t);
	return 0;
}

int sp_floor(double f) {
	conf.spect_floor += f;
	spectro_spec();
	spectro_draw();
	XCopyArea(dpy, buf, win, gc, 0, 0, ww, wh, 0, 0);
	if (info->vis) info->draw(info);
	return 0;
}

int threshold(double f) {
	conf.thresh += f;
	spectro_thresh();
	spectro_points();
	spectro_draw();
	XCopyArea(dpy, buf, win, gc, 0, 0, ww, wh, 0, 0);
	if (info->vis) info->draw(info);
	return 0;
}

int zoom(double f) {
	double px = xsc, py = ysc;
	xsc += f; ysc += f;
	if (xsc < 1.0) xsc = 1.0;
	if (ysc < 1.0) ysc = 1.0;
	xoff += (spect->fft_w/xsc - spect->fft_w/px) / 2.0;
	yoff += (spect->fft_h/py - spect->fft_h/ysc) / 2.0;
	if (f < 0) move(0,0); /* check offsets for bounds */
	spectro_draw();
	XCopyArea(dpy, buf, win, gc, 0, 0, ww, wh, 0, 0);
	return 0;
}

Added src/xlib_events.c.











































































































































































































































































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
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
/**********************************************************************\
* FEX - The Frequency Excursion Calculator
*
* Author: Jesse McClure, copyright 2013-2014
* License: GPL3
*
*    This program 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 of the
*    License, or (at your option) any later version.
*
*    This program 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/>.
*
\**********************************************************************/


#define CtrlMask	ControlMask
void buttonpress(XEvent *ev) {
	XButtonEvent *e = &ev->xbutton;
	if (e->window == info->win) info->button(info, e);
	else if (e->state == (ControlMask | ShiftMask)) {
		if (e->button == 4) threshold(0.1);
		else if (e->button == 5) threshold(-0.1);
		else if (e->button == 6) sp_floor(-0.1);
		else if (e->button == 7) sp_floor(0.1);
	}
	else if (e->state == ControlMask) {
		if (e->button == 4) zoom(0.025);
		else if (e->button == 5) zoom(-0.025);
		else if (e->button == 6) return;
		else if (e->button == 7) return;
	}
	else if (e->state == Mod1Mask) {
		if (e->button == 4) eraser_cursor(1,1);
		else if (e->button == 5) eraser_cursor(-1,-1);
		else if (e->button == 6) eraser_cursor(-1,1);
		else if (e->button == 7) eraser_cursor(1,-1);
	}
	else if (e->state == ShiftMask) {
		if (e->button == 4) pt_line(0.2,0);
		else if (e->button == 5) pt_line(-0.2,0);
		else if (e->button == 6) pt_line(0,-0.2);
		else if (e->button == 7) pt_line(0,0.2);
	}
	else if (e->button == 4) move(0,-0.02);
	else if (e->button == 5) move(0,0.02);
	else if (e->button == 6) move(0.02,0);
	else if (e->button == 7) move(-0.02,0);
	else if (mode == MODE_ERASE && e->button == 1) erase(e->x, e->y);
	else if (mode == MODE_ERASE && e->button == 3) erase(-1,-1);
	else if (mode == MODE_CROP && e->button == 1) crop(e->x, e->y);
	while(XCheckMaskEvent(dpy, ButtonPressMask, ev));
}

void clientmessage(XEvent *ev) {
	XClientMessageEvent *e = &ev->xclient;
	if ( (Atom)e->data.l[0] == WM_DELETE_WINDOW ) {
		if (e->window == info->win) {
			info->vis = False;
			XUnmapWindow(dpy, info->win);
		}
		else if (e->window == win) {
			running = False;
		}
	}
}

void configurenotify(XEvent *ev) {
	while (XCheckTypedEvent(dpy,ConfigureNotify, ev));
	XConfigureEvent *e = &ev->xconfigure;
	if (e->window == win) {
		/* The XServer is not storing the new/current values for width +
		 * height.  These must be retrieved directly.
		 * Is this an X11 bug? */
		Window wig;
		int ig;
		unsigned int uig;
		XGetGeometry(dpy, win, &wig, &ig, &ig, &ww, &wh, &uig, &uig);
		//ww = e->width; wh = e->height;
		spectro_draw();
		XSetWindowBackgroundPixmap(dpy, win, buf);
		XClearWindow(dpy,win);
	}
}

void expose(XEvent *ev) {
	XExposeEvent *e = &ev->xexpose;
	if (e->window == info->win) info->draw(info);
	else {
		XSetWindowBackgroundPixmap(dpy, win, buf);
		XClearWindow(dpy,win);
	}
}

void keypress(XEvent *ev) {
	XKeyEvent *e = &ev->xkey;
	KeySym sym = XkbKeycodeToKeysym(dpy, (KeyCode)e->keycode, 0, 0);
	int mod = ((e->state & ~Mod2Mask) & ~LockMask);
	if (mod == (ControlMask | ShiftMask)) {
		if (sym == XK_q) {
			spect->fex = 0;
			running = False;
		}
		if (sym == XK_j || sym == XK_Down) threshold(-0.05);
		else if (sym == XK_k || sym == XK_Up) threshold(0.05);
		else if (sym == XK_h || sym == XK_Left) sp_floor(-0.05);
		else if (sym == XK_l || sym == XK_Right) sp_floor(0.05);
		else if (sym == XK_p) play(0.1666);
	}
	else if (mod == (Mod1Mask | ShiftMask)) {
		if (sym == XK_p) play(0.08333);
	}
	else if (mod == ControlMask) {
		if (sym == XK_q) running = False;
		else if (sym == XK_s) screenshot();
		else if (sym == XK_i) img_draw();
		else if (sym == XK_j || sym == XK_Down) zoom(-0.025);
		else if (sym == XK_k || sym == XK_Up) zoom(0.025);
		else if (sym == XK_h || sym == XK_Left) return;
		else if (sym == XK_l || sym == XK_Right) return;
		else if (sym == XK_p) play(0.33);
	}
	else if (mod == Mod1Mask) {
		if (sym == XK_j || sym == XK_Down) eraser_cursor(-1,-1);
		else if (sym == XK_k || sym == XK_Up) eraser_cursor(1,1);
		else if (sym == XK_h || sym == XK_Left) eraser_cursor(-1,1);
		else if (sym == XK_l || sym == XK_Right) eraser_cursor(1,-1);
		else if (sym == XK_p) play(0.25);
	}
	else if (mod == ShiftMask) {
		if (sym == XK_j || sym == XK_Down) pt_line(-0.2,0);
		else if (sym == XK_k || sym == XK_Up) pt_line(0.2,0);
		else if (sym == XK_h || sym == XK_Left) pt_line(0,-0.2);
		else if (sym == XK_l || sym == XK_Right) pt_line(0,0.2);
		else if (sym == XK_p) play(0.5);
	}
	else if (sym == XK_j || sym == XK_Down) move(0,0.02);
	else if (sym == XK_k || sym == XK_Up) move(0,-0.02);
	else if (sym == XK_h || sym == XK_Left) move(0.02,0);
	else if (sym == XK_l || sym == XK_Right) move(-0.02,0);
	else if (sym == XK_F1) {
		if (fork() == 0)
			execvp(conf.help_cmd[0],(char * const *)conf.help_cmd);
	}
	else if (sym == XK_F2) {
		if ( (info->vis = !info->vis) ) XMapRaised(dpy,info->win);
		else XUnmapWindow(dpy,info->win);
		XFlush(dpy);
	}
	else if (sym == XK_e) {
		//mode = MODE_ERASE & (mode ^= MODE_ERASE);
		mode = (mode & MODE_ERASE ? 0 : MODE_ERASE);
		eraser_cursor(0,0);
		info->draw(info);
	}
	else if (sym == XK_c) {
		//mode = MODE_CROP & (mode ^= MODE_CROP);
		mode = (mode & MODE_CROP ? 0 : MODE_CROP);
		if (!(mode & MODE_CROP)) XDefineCursor(dpy, win, None);
		else XDefineCursor(dpy, win, XCreateFontCursor(dpy, 34));
		info->draw(info);
	}
	else if (sym == XK_Escape) {
		mode = MODE_NULL;
		XDefineCursor(dpy, win, None);
		info->draw(info);
	}
	else if (sym == XK_p) play(1.0);
	else if (sym == XK_t) {
		conf.layers = !conf.layers;
		spectro_draw();
		XCopyArea(dpy, buf, win, gc, 0, 0, ww, wh, 0, 0);
	}
	else if (sym == XK_u && mode & (MODE_ERASE)) erase(-1,-1);
	while(XCheckMaskEvent(dpy, KeyPressMask, ev));
}

void motionnotify(XEvent *ev) {
	//static int px, py;
	int x = ev->xmotion.x, y = ev->xmotion.y;
	mx = spect->fft_w * x / (xsc * ww) - 1.0 * xoff + spect->fft_x;
	my = spect->fft_h * (1.0 - y / (ysc * wh)) - 1.0 * yoff + spect->fft_y;
	info->draw(info);
	if (mode == MODE_CROP) {
		XCopyArea(dpy, buf, win, gc, 0, 0, ww, wh, 0, 0);
		XDrawLine(dpy, win, gc, x, 0, x, wh);
		XDrawLine(dpy, win, gc, 0, y, ww, y);
	}
}