about summary refs log tree commit diff
path: root/xwin.c
blob: 5ded8851001c5e09e7ee7c18c09f7a22508e6f37 (plain) (blame)
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
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
/*
 * XDU - X Window System Interface.
 *
 * We hide all of the X hieroglyphics inside of this module.
 *
 * Phillip C. Dykstra
 * <phil@arl.mil>
 * 4 Sep 1991.
 * 
 * Copyright (c)	Phillip C. Dykstra	1991, 1993, 1994
 * The X Consortium, and any party obtaining a copy of these files from
 * the X Consortium, directly or indirectly, is granted, free of charge, a
 * full and unrestricted irrevocable, world-wide, paid up, royalty-free,
 * nonexclusive right and license to deal in this software and
 * documentation files (the "Software"), including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons who receive
 * copies from any such party to do so.  This license includes without
 * limitation a license to do the foregoing actions under any patents of
 * the party supplying this software to the X Consortium.
 */
#include <X11/Intrinsic.h>
#include <X11/StringDefs.h>
#include <X11/Shell.h>

#include <X11/Xaw/AsciiText.h>
#include <X11/Xaw/AsciiSrc.h>
#include <X11/Xaw/Form.h>
#include <X11/Xaw/Label.h>

#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>	/* for exit() */

/* IMPORTS: routines that this module vectors out to */
int press(int x, int y);
int reset(void);
int repaint(int width, int height);
int setorder(char *op);
int reorder(char *op);
int nodeinfo(void);
int helpinfo(void);
extern int ncols;

/* internal routines */
static void help_popup();
static void help_popdown();
void xrepaint(void);
void xrepaint_noclear(void);

static String fallback_resources[] = {
"*window.width:		600",
"*window.height:	480",
"*help.width:		500",
"*help.height:		330",
"*order:		first",
NULL
};

/* Application Resources */
typedef struct {
	Pixel	foreground;
	Pixel	background;
	XFontStruct *font;
	int	ncol;
	Boolean	showsize;
	Boolean	human;
	char	*order;
} res_data, *res_data_ptr;
static res_data res;

static XtResource application_resources[] = {
	{ XtNforeground, XtCForeground, XtRPixel, sizeof(Pixel),
		XtOffset(res_data_ptr,foreground), XtRString, XtDefaultForeground},
	{ XtNbackground, XtCBackground, XtRPixel, sizeof(Pixel),
		XtOffset(res_data_ptr,background), XtRString, XtDefaultBackground},
	{ XtNfont, XtCFont, XtRFontStruct, sizeof(XFontStruct *),
		XtOffset(res_data_ptr,font), XtRString, XtDefaultFont },
	{ "ncol", "Ncol", XtRInt, sizeof(int),
		XtOffset(res_data_ptr,ncol), XtRString, "5"},
	{ "showsize", "ShowSize", XtRBoolean, sizeof(Boolean),
		XtOffset(res_data_ptr,showsize), XtRString, "True"},
	{ "human", "Human", XtRBoolean, sizeof(Boolean),
		XtOffset(res_data_ptr,human), XtRString, "True"},
	{ "order", "Order", XtRString, sizeof(String),
		XtOffset(res_data_ptr,order), XtRString, "first"}
};

/* Command Line Options */
static XrmOptionDescRec options[] = {
	{"-c",		"*ncol",	XrmoptionSepArg,	NULL},
	{"+s",		"*showsize",	XrmoptionNoArg,		"True"},
	{"-s",		"*showsize",	XrmoptionNoArg,		"False"},
	{"+h",		"*human",	XrmoptionNoArg,		"True"},
	{"-h",		"*human",	XrmoptionNoArg,		"False"},
	{"-n",		"*order",	XrmoptionNoArg,		"size"},
	{"-rn",		"*order",	XrmoptionNoArg,		"rsize"},
	{"-a",		"*order",	XrmoptionNoArg,		"alpha"},
	{"-ra",		"*order",	XrmoptionNoArg,		"ralpha"}
};

/* action routines */
static void a_goto();
static void a_reset();
static void a_quit();
static void a_reorder();
static void a_size();
static void a_human();
static void a_ncol();
static void a_info();
static void a_help();
static void a_removehelp();

static XtActionsRec actionsTable[] = {
	{ "reset",	a_reset },
	{ "goto",	a_goto },
	{ "quit",	a_quit },
	{ "reorder",	a_reorder },
	{ "size",	a_size },
	{ "human",	a_human },
	{ "ncol",	a_ncol },
	{ "info",	a_info },
	{ "help",	a_help },
	{ "RemoveHelp",	a_removehelp }
};

static char defaultTranslations[] = "\
<Key>Q:	quit()\n\
<Key>Escape: quit()\n\
:<Key>/: reset()\n\
<Key>S:	size()\n\
<Key>H:	human()\n\
<Key>I:	info()\n\
<Key>Help: help()\n\
:<Key>?: help()\n\
<Key>A:	reorder(alpha)\n\
<Key>N:	reorder(size)\n\
<Key>F:	reorder(first)\n\
<Key>L:	reorder(last)\n\
<Key>R:	reorder(reverse)\n\
<Key>1:	ncol(1)\n\
<Key>2:	ncol(2)\n\
<Key>3:	ncol(3)\n\
<Key>4:	ncol(4)\n\
<Key>5:	ncol(5)\n\
<Key>6:	ncol(6)\n\
<Key>7:	ncol(7)\n\
<Key>8:	ncol(8)\n\
<Key>9:	ncol(9)\n\
<Key>0:	ncol(10)\n\
<Btn1Down>: goto()\n\
<Btn2Down>: reset()\n\
<Btn3Down>: quit()\n\
";

/*  action routines  */

static void
a_quit(Widget w, XEvent *event, String *params, Cardinal *num_params)
{
	XtDestroyApplicationContext(XtWidgetToApplicationContext(w));
	exit(0);
}

static void
a_goto(Widget w, XEvent *event, String *params, Cardinal *num_params)
{
	press(event->xbutton.x, event->xbutton.y);
}

static void
a_reset(Widget w, XEvent *event, String *params, Cardinal *num_params)
{
	reset();
}

static void
a_reorder(Widget w, XEvent *event, String *params, Cardinal *num_params)
{
	if (*num_params != 1) {
		fprintf(stderr, "xdu: bad number of params to reorder action\n");
	} else {
		reorder(*params);
	}
}

static void
a_size(Widget w, XEvent *event, String *params, Cardinal *num_params)
{
	if (res.showsize)
		res.showsize = 0;
	else
		res.showsize = 1;
	xrepaint();
}

static void
a_human(Widget w, XEvent *event, String *params, Cardinal *num_params)
{
	if (res.human)
		res.human = 0;
	else
		res.human = 1;
	xrepaint();
}

static void
a_ncol(Widget w, XEvent *event, String *params, Cardinal *num_params)
{
	int	n;

	if (*num_params != 1) {
		fprintf(stderr, "xdu: bad number of params to ncol action\n");
		return;
	}
	n = atoi(*params);
	if (n < 1 || n > 1000) {
		fprintf(stderr, "xdu: bad value to ncol action\n");
		return;
	}
	ncols = res.ncol = n;
	xrepaint();
}

static void
a_info(Widget w, XEvent *event, String *params, Cardinal *num_params)
{
	nodeinfo();
}

static void
a_help(Widget w, XEvent *event, String *params, Cardinal *num_params)
{
	/*helpinfo();*/
	help_popup();
}

static void
a_removehelp(Widget w, XEvent *event, String *params, Cardinal *num_params)
{
	help_popdown();
}

/* callback routines */

static void
c_resize(Widget w, XtPointer data, XEvent *event, Boolean *continue_to_dispatch)
{
	/*printf("Resize\n");*/
	xrepaint();
}

static void
c_repaint(Widget w, XtPointer data, XEvent *event, Boolean *continue_to_dispatch)
{
	/*printf("Expose\n");*/
	xrepaint_noclear();
}

/* X Window related variables */
static Display *dpy;
static int screen;
static Visual *vis;
static Window win;
static GC gc;
static XtAppContext app_con;

Widget toplevel;

/*  External Functions  */

void
xsetup(int *argcp, char **argv)
{
	XtTranslations trans_table;
	Widget w;
	XGCValues gcv;
	int n;
	Arg args[5];

	/* Create the top level Widget */
	n = 0;
	XtSetArg(args[n], XtNtitle, "XDU Disk Usage Display ('?' for help)"); n++;
	toplevel = XtAppInitialize(&app_con, "XDu",
			options, XtNumber(options),
			argcp, argv,
			fallback_resources, args, n);

	XtGetApplicationResources(toplevel, (XtPointer)&res,
		application_resources, XtNumber(application_resources),
		NULL, 0 );

	XtAppAddActions(app_con, actionsTable, XtNumber(actionsTable));
	trans_table = XtParseTranslationTable(defaultTranslations);

	/* Create a simple Label class widget to draw in */
	n = 0;
	XtSetArg(args[n], XtNlabel, ""); n++;
	w = XtCreateManagedWidget("window", labelWidgetClass, toplevel,
		args, n);

	/* events */
	XtAddEventHandler(w, ExposureMask, False, c_repaint, NULL);
	XtAddEventHandler(w, StructureNotifyMask, False, c_resize, NULL);
	XtAugmentTranslations(w, trans_table);

	XtRealizeWidget(toplevel);

	/* We need these for the raw Xlib calls */
	win = XtWindow(w);
	dpy = XtDisplay(w);
	screen = DefaultScreen(dpy);
	vis = DefaultVisual(dpy,screen);

	gcv.foreground = res.foreground;
	gcv.background = res.background;
	gcv.font = res.font->fid;
	gc = XCreateGC(dpy, win, (GCFont|GCForeground|GCBackground), &gcv);

	setorder(res.order);
	ncols = res.ncol;
}

int
xmainloop(void)
{
	XtAppMainLoop(app_con);
	return(0);
}

void
xclear(void)
{
	XClearWindow(dpy, win);
}

void
xrepaint(void)
{
	XWindowAttributes xwa;

	XClearWindow(dpy, win);
	XGetWindowAttributes(dpy, win, &xwa);
	repaint(xwa.width, xwa.height);
}

void
xrepaint_noclear(void)
{
	XWindowAttributes xwa;

	XGetWindowAttributes(dpy, win, &xwa);
	repaint(xwa.width, xwa.height);
}

void
xdrawrect(char *name, off_t size, int x, int y, int width, int height)
{
	int	textx, texty;
	char	label[1024];
	XCharStruct overall;
	int	ascent, descent, direction;
	int	cheight;

	/*printf("draw(%d,%d,%d,%d)\n", x, y, width, height );*/
	XDrawRectangle(dpy, win, gc, x, y, width, height);

	if (res.showsize) {
		if (res.human) {
			double d = size;
			const char *u = "KMGTPEZY";

			while (d >= 1024) {
				u++;
				d /= 1024.0;
			}
			sprintf(label,"%s (%.1f%c)", name, d, *u);
		} else {
			sprintf(label,"%s (%jd)", name, (intmax_t)size);
		}

		name = label;
	}

	XTextExtents(res.font, name, strlen(name), &direction,
		&ascent, &descent, &overall);
	cheight = overall.ascent + overall.descent;
	if (height < (cheight + 2))
		return;

	/* print label */
	textx = x + 4;
	texty = y + height/2.0 + (overall.ascent - overall.descent)/2.0 + 1.5;
	XDrawString(dpy, win, gc, textx, texty, name, strlen(name));
}

static Widget popup;

static void
help_popup(void)
{
	Widget form, text, src;
	Arg args[15];
	int n;
	Atom wm_delete_window;
	XtTranslations trans_table;

	if (popup != NULL) {
		XtPopup(popup, XtGrabNone);
		return;
	}

	/* popup shell */
	n = 0;
	XtSetArg(args[n], XtNtitle, "XDU Help"); n++;
	popup = XtCreatePopupShell("helpPopup", transientShellWidgetClass,
		toplevel, args, n);

	/* form container */
	n = 0;
	XtSetArg(args[n], XtNborderWidth, 0); n++;
	XtSetArg(args[n], XtNdefaultDistance, 0); n++;
	form = XtCreateManagedWidget("form", formWidgetClass,
		popup, args, n);

	/* text widget in form */
	n = 0;
	XtSetArg(args[n], XtNborderWidth, 0); n++;
	XtSetArg(args[n], XtNresize, XawtextResizeBoth); n++;
	/* fallback resources weren't working here on the Sun */
	XtSetArg(args[n], XtNwidth, 500); n++;
	XtSetArg(args[n], XtNheight, 330); n++;
	text = XtCreateManagedWidget("help", asciiTextWidgetClass,
		form, args, n);

	/* create text source */
	n = 0;
	XtSetArg(args[n], XtNtype, XawAsciiString); n++;
	XtSetArg(args[n], XtNeditType, XawtextRead); n++;
	XtSetArg(args[n], XtNstring, "\
XDU Version 3.0 - Phil Dykstra <phil@arl.mil>\n\
\n\
Keyboard Commands\n\
  a  sort alphabetically\n\
  n  sort numerically (largest first)\n\
  f  sort first-in-first-out\n\
  l  sort last-in-first-out\n\
  r  reverse sort\n\
  s  toggle size display\n\
  h  toggle human readable sizes\n\
  /  goto the root\n\
  i  node info to standard out\n\
  ?  this help message\n\
  q  quit (also Escape)\n\
0-9  set number of columns (0=10)\n\
\n\
Mouse Commands\n\
  Left   Goto node (goto parent if leftmost box)\n\
  Middle Back to root\n\
  Right  Quit\n\
"); n++;
	src = XtCreateWidget("textSource", asciiSrcObjectClass,
		text, args, n);
	/* set text source */
	XawTextSetSource(text, src, 0);

	XtRealizeWidget(popup);
	XtPopup(popup, XtGrabNone);

	trans_table = XtParseTranslationTable("<Key>Q: RemoveHelp()");
	XtAugmentTranslations(form, trans_table);
	XtOverrideTranslations(text, trans_table);

	/* Set up ICCCM delete window */
	wm_delete_window = XInternAtom(XtDisplay(popup), "WM_DELETE_WINDOW", False);
	XtOverrideTranslations(popup, XtParseTranslationTable("<Message>WM_PROTOCOLS: RemoveHelp()"));
	XSetWMProtocols(XtDisplay(popup), XtWindow(popup), &wm_delete_window, 1);
}

static void
help_popdown(void)
{
	XtPopdown(popup);
}