about summary refs log tree commit diff stats
path: root/event.c
blob: e4dfad1638c5edea51c2a8868782e0bc1d43be6e (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
/*
 * (C)opyright MMVI Anselm R. Garbe <garbeam at gmail dot com>
 * See LICENSE file for license details.
 */
#include "dwm.h"

#include <stdlib.h>
#include <X11/keysym.h>
#include <X11/Xatom.h>

#define ButtonMask      (ButtonPressMask | ButtonReleaseMask)
#define MouseMask       (ButtonMask | PointerMotionMask)

/* CUSTOMIZE */
const char *browse[] = { "firefox", NULL };
const char *gimp[] = { "gimp", NULL };
const char *term[] = { 
	"urxvtc", "-tr", "+sb", "-bg", "black", "-fg", "white", "-cr", "white",
	"-fn", "-*-terminus-medium-*-*-*-13-*-*-*-*-*-iso10646-*", NULL
};
const char *xlock[] = { "xlock", NULL };

Key key[] = {
	/* modifier				key			function	arguments */
	{ ControlMask,			XK_0,		appendtag,	{ .i = Tscratch } }, 
	{ ControlMask,			XK_1,		appendtag,	{ .i = Tdev } }, 
	{ ControlMask,			XK_2,		appendtag,	{ .i = Twww } }, 
	{ ControlMask,			XK_3,		appendtag,	{ .i = Twork } }, 
	{ Mod1Mask,				XK_0,		view,		{ .i = Tscratch } }, 
	{ Mod1Mask,				XK_1,		view,		{ .i = Tdev } }, 
	{ Mod1Mask,				XK_2,		view,		{ .i = Twww } }, 
	{ Mod1Mask,				XK_3,		view,		{ .i = Twork } }, 
	{ Mod1Mask,				XK_j,		focusnext,		{ 0 } }, 
	{ Mod1Mask,				XK_k,		focusprev,		{ 0 } },
	{ Mod1Mask,				XK_m,		maximize,		{ 0 } }, 
	{ Mod1Mask,				XK_space,	dotile,		{ 0 } }, 
	{ Mod1Mask,				XK_Return,	zoom,		{ 0 } },
	{ ControlMask|ShiftMask,XK_0,		heretag,	{ .i = Tscratch } }, 
	{ ControlMask|ShiftMask,XK_1,		heretag,	{ .i = Tdev } }, 
	{ ControlMask|ShiftMask,XK_2,		heretag,	{ .i = Twww } }, 
	{ ControlMask|ShiftMask,XK_3,		heretag,	{ .i = Twork } }, 
	{ Mod1Mask|ShiftMask,	XK_0,		replacetag,		{ .i = Tscratch } }, 
	{ Mod1Mask|ShiftMask,	XK_1,		replacetag,		{ .i = Tdev } }, 
	{ Mod1Mask|ShiftMask,	XK_2,		replacetag,		{ .i = Twww } }, 
	{ Mod1Mask|ShiftMask,	XK_3,		replacetag,		{ .i = Twork } }, 
	{ Mod1Mask|ShiftMask,	XK_c,		killclient,		{ 0 } }, 
	{ Mod1Mask|ShiftMask,	XK_g,		spawn,		{ .argv = gimp } },
	{ Mod1Mask|ShiftMask,	XK_l,		spawn,		{ .argv = xlock } },
	{ Mod1Mask|ShiftMask,	XK_q,		quit,		{ 0 } },
	{ Mod1Mask|ShiftMask,	XK_space,	dofloat,	{ 0 } }, 
	{ Mod1Mask|ShiftMask,	XK_w,		spawn,		{ .argv = browse } },
	{ Mod1Mask|ShiftMask,	XK_Return,	spawn,		{ .argv = term } },
};

/* static */

static void
movemouse(Client *c)
{
	XEvent ev;
	int x1, y1, ocx, ocy, di;
	unsigned int dui;
	Window dummy;

	ocx = *c->x;
	ocy = *c->y;
	if(XGrabPointer(dpy, root, False, MouseMask, GrabModeAsync, GrabModeAsync,
				None, cursor[CurMove], CurrentTime) != GrabSuccess)
		return;
	XQueryPointer(dpy, root, &dummy, &dummy, &x1, &y1, &di, &di, &dui);
	for(;;) {
		XMaskEvent(dpy, MouseMask | ExposureMask, &ev);
		switch (ev.type) {
		default: break;
		case Expose:
			handler[Expose](&ev);
			break;
		case MotionNotify:
			XSync(dpy, False);
			*c->x = ocx + (ev.xmotion.x - x1);
			*c->y = ocy + (ev.xmotion.y - y1);
			resize(c, False, TopLeft);
			break;
		case ButtonRelease:
			XUngrabPointer(dpy, CurrentTime);
			return;
		}
	}
}

static void
resizemouse(Client *c)
{
	XEvent ev;
	int ocx, ocy;
	Corner sticky;

	ocx = *c->x;
	ocy = *c->y;
	if(XGrabPointer(dpy, root, False, MouseMask, GrabModeAsync, GrabModeAsync,
				None, cursor[CurResize], CurrentTime) != GrabSuccess)
		return;
	XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, *c->w, *c->h);
	for(;;) {
		XMaskEvent(dpy, MouseMask | ExposureMask, &ev);
		switch(ev.type) {
		default: break;
		case Expose:
			handler[Expose](&ev);
			break;
		case MotionNotify:
			XSync(dpy, False);
			*c->w = abs(ocx - ev.xmotion.x);
			*c->h = abs(ocy - ev.xmotion.y);
			*c->x = (ocx <= ev.xmotion.x) ? ocx : ocx - *c->w;
			*c->y = (ocy <= ev.xmotion.y) ? ocy : ocy - *c->h;
			if(ocx <= ev.xmotion.x)
				sticky = (ocy <= ev.xmotion.y) ? TopLeft : BotLeft;
			else
				sticky = (ocy <= ev.xmotion.y) ? TopRight : BotRight;
			resize(c, True, sticky);
			break;
		case ButtonRelease:
			XUngrabPointer(dpy, CurrentTime);
			return;
		}
	}
}

static void
buttonpress(XEvent *e)
{
	int x;
	Arg a;
	XButtonPressedEvent *ev = &e->xbutton;
	Client *c;

	if(barwin == ev->window) {
		switch(ev->button) {
		default:
			x = 0;
			for(a.i = 0; a.i < TLast; a.i++) {
				x += textw(tags[a.i]);
				if(ev->x < x) {
					view(&a);
					break;
				}
			}
			break;
		case Button4:
			a.i = (tsel + 1 < TLast) ? tsel + 1 : 0;
			view(&a);
			break;
		case Button5:
			a.i = (tsel - 1 >= 0) ? tsel - 1 : TLast - 1;
			view(&a);
			break;
		}
	}
	else if((c = getclient(ev->window))) {
		switch(ev->button) {
		default:
			break;
		case Button1:
			if(arrange == dotile && !c->isfloat) {
				if((ev->state & ControlMask) && (ev->button == Button1))
					zoom(NULL);
			}
			else {
				higher(c);
				movemouse(c);
			}
			break;
		case Button2:
			lower(c);
			break;
		case Button3:
			if(arrange == dofloat || c->isfloat) {
				higher(c);
				resizemouse(c);
			}
			break;
		}
	}
}

static void
configurerequest(XEvent *e)
{
	XConfigureRequestEvent *ev = &e->xconfigurerequest;
	XWindowChanges wc;
	Client *c;

	ev->value_mask &= ~CWSibling;
	if((c = getclient(ev->window))) {
		gravitate(c, True);
		if(ev->value_mask & CWX)
			*c->x = ev->x;
		if(ev->value_mask & CWY)
			*c->y = ev->y;
		if(ev->value_mask & CWWidth)
			*c->w = ev->width;
		if(ev->value_mask & CWHeight)
			*c->h = ev->height;
		if(ev->value_mask & CWBorderWidth)
			c->border = 1;
		gravitate(c, False);
		resize(c, True, TopLeft);
	}

	wc.x = ev->x;
	wc.y = ev->y;
	wc.width = ev->width;
	wc.height = ev->height;
	wc.border_width = 1;
	wc.sibling = None;
	wc.stack_mode = Above;
	ev->value_mask &= ~CWStackMode;
	ev->value_mask |= CWBorderWidth;
	XConfigureWindow(dpy, ev->window, ev->value_mask, &wc);
	XSync(dpy, False);
}

static void
destroynotify(XEvent *e)
{
	Client *c;
	XDestroyWindowEvent *ev = &e->xdestroywindow;

	if((c = getclient(ev->window)))
		unmanage(c);
}

static void
enternotify(XEvent *e)
{
	XCrossingEvent *ev = &e->xcrossing;
	Client *c;

	if(ev->mode != NotifyNormal || ev->detail == NotifyInferior)
		return;

	if((c = getclient(ev->window)))
		focus(c);
	else if(ev->window == root)
		issel = True;
}

static void
expose(XEvent *e)
{
	XExposeEvent *ev = &e->xexpose;
	Client *c;

	if(ev->count == 0) {
		if(barwin == ev->window)
			drawstatus();
		else if((c = getctitle(ev->window)))
			drawtitle(c);
	}
}

static void
keypress(XEvent *e)
{
	XKeyEvent *ev = &e->xkey;
	static unsigned int len = key ? sizeof(key) / sizeof(key[0]) : 0;
	unsigned int i;
	KeySym keysym;

	keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
	for(i = 0; i < len; i++)
		if((keysym == key[i].keysym) && (key[i].mod == ev->state)) {
			if(key[i].func)
				key[i].func(&key[i].arg);
			return;
		}
}

static void
leavenotify(XEvent *e)
{
	XCrossingEvent *ev = &e->xcrossing;

	if((ev->window == root) && !ev->same_screen)
		issel = True;
}

static void
maprequest(XEvent *e)
{
	XMapRequestEvent *ev = &e->xmaprequest;
	static XWindowAttributes wa;

	if(!XGetWindowAttributes(dpy, ev->window, &wa))
		return;

	if(wa.override_redirect) {
		XSelectInput(dpy, ev->window,
				(StructureNotifyMask | PropertyChangeMask));
		return;
	}

	if(!getclient(ev->window))
		manage(ev->window, &wa);
}

static void
propertynotify(XEvent *e)
{
	XPropertyEvent *ev = &e->xproperty;
	Window trans;
	Client *c;

	if(ev->state == PropertyDelete)
		return; /* ignore */

	if((c = getclient(ev->window))) {
		if(ev->atom == wmatom[WMProtocols]) {
			c->proto = getproto(c->win);
			return;
		}
		switch (ev->atom) {
			default: break;
			case XA_WM_TRANSIENT_FOR:
				XGetTransientForHint(dpy, c->win, &trans);
				if(!c->isfloat && (c->isfloat = (trans != 0)))
					arrange(NULL);
				break;
			case XA_WM_NORMAL_HINTS:
				setsize(c);
				break;
		}
		if(ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
			settitle(c);
			drawtitle(c);
		}
	}
}

static void
unmapnotify(XEvent *e)
{
	Client *c;
	XUnmapEvent *ev = &e->xunmap;

	if((c = getclient(ev->window)))
		unmanage(c);
}

/* extern */

void (*handler[LASTEvent]) (XEvent *) = {
	[ButtonPress] = buttonpress,
	[ConfigureRequest] = configurerequest,
	[DestroyNotify] = destroynotify,
	[EnterNotify] = enternotify,
	[LeaveNotify] = leavenotify,
	[Expose] = expose,
	[KeyPress] = keypress,
	[MapRequest] = maprequest,
	[PropertyNotify] = propertynotify,
	[UnmapNotify] = unmapnotify
};

void
grabkeys()
{
	static unsigned int len = key ? sizeof(key) / sizeof(key[0]) : 0;
	unsigned int i;
	KeyCode code;

	for(i = 0; i < len; i++) {
		code = XKeysymToKeycode(dpy, key[i].keysym);
		XUngrabKey(dpy, code, key[i].mod, root);
		XGrabKey(dpy, code, key[i].mod, root, True,
				GrabModeAsync, GrabModeAsync);
	}
}
s="w"> newFileStream(c.infile, fmRead) if input != nil: open(p, input, c.infile) while true: var k = next(p) case k.kind of cfgEof: break of cfgSectionStart: section = normalize(k.section) of cfgKeyValuePair: var v = `%`(k.value, c.vars, {useEnvironment, useEmpty}) c.vars[k.key] = v case section of "project": case normalize(k.key) of "name": c.name = v of "displayname": c.displayName = v of "version": c.version = v of "os": c.oses = split(v, {';'}) hasCpuOs = true if c.explicitPlatforms: quit(errorStr(p, "you cannot have both 'platforms' and 'os'")) of "cpu": c.cpus = split(v, {';'}) hasCpuOs = true if c.explicitPlatforms: quit(errorStr(p, "you cannot have both 'platforms' and 'cpu'")) of "platforms": platforms(c, v) c.explicitPlatforms = true if hasCpuOs: quit(errorStr(p, "you cannot have both 'platforms' and 'os'")) of "authors": c.authors = split(v, {';'}) of "description": c.description = v of "app": case normalize(v) of "console": c.app = appConsole of "gui": c.app = appGUI else: quit(errorStr(p, "expected: console or gui")) of "license": c.license = unixToNativePath(k.value) else: quit(errorStr(p, "unknown variable: " & k.key)) of "var": discard of "winbin": filesOnly(p, k.key, v, c.cat[fcWinBin]) of "config": filesOnly(p, k.key, v, c.cat[fcConfig]) of "data": filesOnly(p, k.key, v, c.cat[fcData]) of "documentation": case normalize(k.key) of "files": addFiles(c.cat[fcDoc], split(v, {';'})) of "start": addFiles(c.cat[fcDocStart], split(v, {';'})) else: quit(errorStr(p, "unknown variable: " & k.key)) of "lib": filesOnly(p, k.key, v, c.cat[fcLib]) of "other": filesOnly(p, k.key, v, c.cat[fcOther]) of "windows": case normalize(k.key) of "files": addFiles(c.cat[fcWindows], split(v, {';'})) of "binpath": c.binPaths = split(v, {';'}) of "innosetup": c.innoSetupFlag = yesno(p, v) of "download": c.downloads.add(v) else: quit(errorStr(p, "unknown variable: " & k.key)) of "unix": case normalize(k.key) of "files": addFiles(c.cat[fcUnix], split(v, {';'})) of "installscript": c.installScript = yesno(p, v) of "uninstallscript": c.uninstallScript = yesno(p, v) else: quit(errorStr(p, "unknown variable: " & k.key)) of "unixbin": filesOnly(p, k.key, v, c.cat[fcUnixBin]) of "innosetup": pathFlags(p, k.key, v, c.innosetup) of "nsis": pathFlags(p, k.key, v, c.nsisSetup) of "ccompiler": pathFlags(p, k.key, v, c.ccompiler) of "linker": pathFlags(p, k.key, v, c.linker) of "deb": case normalize(k.key) of "builddepends": c.debOpts.buildDepends = v of "packagedepends", "pkgdepends": c.debOpts.pkgDepends = v of "shortdesc": c.debOpts.shortDesc = v of "licenses": # file,license;file,license; var i = 0 var file = "" var license = "" var afterComma = false while i < v.len(): case v[i] of ',': afterComma = true of ';': if file == "" or license == "": quit(errorStr(p, "Invalid `licenses` key.")) c.debOpts.licenses.add((file, license)) afterComma = false file = "" license = "" else: if afterComma: license.add(v[i]) else: file.add(v[i]) inc(i) else: quit(errorStr(p, "unknown variable: " & k.key)) of "nimble": case normalize(k.key) of "pkgname": c.nimblePkgName = v of "pkgfiles": addFiles(c.cat[fcNimble], split(v, {';'})) else: quit(errorStr(p, "invalid key: " & k.key)) else: quit(errorStr(p, "invalid section: " & section)) of cfgOption: quit(errorStr(p, "syntax error")) of cfgError: quit(errorStr(p, k.msg)) close(p) if c.name.len == 0: c.name = changeFileExt(extractFilename(c.mainfile), "") if c.displayName.len == 0: c.displayName = c.name else: quit("cannot open: " & c.infile) # ------------------------- generate source based installation --------------- proc readCFiles(c: var ConfigData, osA, cpuA: int) = var p: CfgParser var f = splitFile(c.infile).dir / "mapping.txt" c.cfiles[osA][cpuA] = @[] var input = newFileStream(f, fmRead) var section = "" if input != nil: open(p, input, f) while true: var k = next(p) case k.kind of cfgEof: break of cfgSectionStart: section = normalize(k.section) of cfgKeyValuePair: case section of "ccompiler": pathFlags(p, k.key, k.value, c.ccompiler) of "linker": pathFlags(p, k.key, k.value, c.linker) # HACK: we conditionally add ``-lm -ldl``, so remove them from the # linker flags: c.linker.flags = c.linker.flags.replaceWord("-lm").replaceWord( "-ldl").replaceWord("-lroot").replaceWord( "-lnetwork").strip else: if cmpIgnoreStyle(k.key, "libpath") == 0: c.libpath = k.value of cfgOption: if section == "cfiles" and cmpIgnoreStyle(k.key, "file") == 0: add(c.cfiles[osA][cpuA], k.value) of cfgError: quit(errorStr(p, k.msg)) close(p) else: quit("Cannot open: " & f) proc buildDir(os, cpu: int): string = return "c_code" / ($os & "_" & $cpu) proc getOutputDir(c: var ConfigData): string = if c.outdir.len > 0: c.outdir else: "build" proc writeFile(filename, content, newline: string) = var f: File if open(f, filename, fmWrite): for x in splitLines(content): write(f, x) write(f, newline) close(f) else: quit("Cannot open for writing: " & filename) proc removeDuplicateFiles(c: var ConfigData) = for osA in countdown(c.oses.len, 1): for cpuA in countdown(c.cpus.len, 1): if c.cfiles[osA][cpuA].isNil: c.cfiles[osA][cpuA] = @[] if c.explicitPlatforms and not c.platforms[osA][cpuA]: continue for i in 0..c.cfiles[osA][cpuA].len-1: var dup = c.cfiles[osA][cpuA][i] var f = extractFilename(dup) for osB in 1..c.oses.len: for cpuB in 1..c.cpus.len: if osB != osA or cpuB != cpuA: var orig = buildDir(osB, cpuB) / f if existsFile(orig) and existsFile(dup) and sameFileContent(orig, dup): # file is identical, so delete duplicate: removeFile(dup) c.cfiles[osA][cpuA][i] = orig proc writeInstallScripts(c: var ConfigData) = if c.installScript: writeFile(installShFile, generateInstallScript(c), "\10") inclFilePermissions(installShFile, {fpUserExec, fpGroupExec, fpOthersExec}) if c.uninstallScript: writeFile(deinstallShFile, generateDeinstallScript(c), "\10") inclFilePermissions(deinstallShFile, {fpUserExec, fpGroupExec, fpOthersExec}) proc srcdist(c: var ConfigData) = if not existsDir(getOutputDir(c) / "c_code"): createDir(getOutputDir(c) / "c_code") for x in walkFiles(c.libpath / "lib/*.h"): echo(getOutputDir(c) / "c_code" / extractFilename(x)) copyFile(dest=getOutputDir(c) / "c_code" / extractFilename(x), source=x) var winIndex = -1 var intel32Index = -1 var intel64Index = -1 for osA in 1..c.oses.len: let osname = c.oses[osA-1] if osname.cmpIgnoreStyle("windows") == 0: winIndex = osA for cpuA in 1..c.cpus.len: if c.explicitPlatforms and not c.platforms[osA][cpuA]: continue let cpuname = c.cpus[cpuA-1] if cpuname.cmpIgnoreStyle("i386") == 0: intel32Index = cpuA elif cpuname.cmpIgnoreStyle("amd64") == 0: intel64Index = cpuA var dir = getOutputDir(c) / buildDir(osA, cpuA) if existsDir(dir): removeDir(dir) createDir(dir) var cmd = ("nim compile -f --symbolfiles:off --compileonly " & "--gen_mapping --cc:gcc --skipUserCfg" & " --os:$# --cpu:$# $# $#") % [osname, cpuname, c.nimArgs, c.mainfile] echo(cmd) if execShellCmd(cmd) != 0: quit("Error: call to nim compiler failed") readCFiles(c, osA, cpuA) for i in 0 .. c.cfiles[osA][cpuA].len-1: let dest = dir / extractFilename(c.cfiles[osA][cpuA][i]) let relDest = buildDir(osA, cpuA) / extractFilename(c.cfiles[osA][cpuA][i]) copyFile(dest=dest, source=c.cfiles[osA][cpuA][i]) c.cfiles[osA][cpuA][i] = relDest # second pass: remove duplicate files removeDuplicateFiles(c) writeFile(getOutputDir(c) / buildShFile, generateBuildShellScript(c), "\10") inclFilePermissions(getOutputDir(c) / buildShFile, {fpUserExec, fpGroupExec, fpOthersExec}) writeFile(getOutputDir(c) / makeFile, generateMakefile(c), "\10") if winIndex >= 0: if intel32Index >= 0: writeFile(getOutputDir(c) / buildBatFile32, generateBuildBatchScript(c, winIndex, intel32Index), "\13\10") if intel64Index >= 0: writeFile(getOutputDir(c) / buildBatFile64, generateBuildBatchScript(c, winIndex, intel64Index), "\13\10") writeInstallScripts(c) # --------------------- generate inno setup ----------------------------------- proc setupDist(c: var ConfigData) = let scrpt = generateInnoSetup(c) let n = "build" / "install_$#_$#.iss" % [toLowerAscii(c.name), c.version] writeFile(n, scrpt, "\13\10") when defined(windows): if c.innosetup.path.len == 0: c.innosetup.path = "iscc.exe" let outcmd = if c.outdir.len == 0: "build" else: c.outdir let cmd = "$# $# /O$# $#" % [quoteShell(c.innosetup.path), c.innosetup.flags, outcmd, n] echo(cmd) if execShellCmd(cmd) == 0: removeFile(n) else: quit("External program failed") # --------------------- generate NSIS setup ----------------------------------- proc setupDist2(c: var ConfigData) = let scrpt = generateNsisSetup(c) let n = "build" / "install_$#_$#.nsi" % [toLowerAscii(c.name), c.version] writeFile(n, scrpt, "\13\10") when defined(windows): if c.nsisSetup.path.len == 0: c.nsisSetup.path = "makensis.exe" let outcmd = if c.outdir.len == 0: "build" else: c.outdir let cmd = "$# $# /O$# $#" % [quoteShell(c.nsisSetup.path), c.nsisSetup.flags, outcmd, n] echo(cmd) if execShellCmd(cmd) == 0: removeFile(n) else: quit("External program failed") # ------------------ generate ZIP file --------------------------------------- when haveZipLib: proc zipDist(c: var ConfigData) = var proj = toLowerAscii(c.name) & "-" & c.version var n = "$#.zip" % proj if c.outdir.len == 0: n = "build" / n else: n = c.outdir / n var z: ZipArchive if open(z, n, fmWrite): addFile(z, proj / buildBatFile32, "build" / buildBatFile32) addFile(z, proj / buildBatFile64, "build" / buildBatFile64) addFile(z, proj / buildShFile, "build" / buildShFile) addFile(z, proj / makeFile, "build" / makeFile) addFile(z, proj / installShFile, installShFile) addFile(z, proj / deinstallShFile, deinstallShFile) for f in walkFiles(c.libpath / "lib/*.h"): addFile(z, proj / "c_code" / extractFilename(f), f) for osA in 1..c.oses.len: for cpuA in 1..c.cpus.len: var dir = buildDir(osA, cpuA) for k, f in walkDir("build" / dir): if k == pcFile: addFile(z, proj / dir / extractFilename(f), f) for cat in items({fcConfig..fcOther, fcUnix, fcNimble}): for f in items(c.cat[cat]): addFile(z, proj / f, f) # Copy the .nimble file over let nimbleFile = c.nimblePkgName & ".nimble" processFile(z, proj / nimbleFile, nimbleFile) close(z) else: quit("Cannot open for writing: " & n) proc xzDist(c: var ConfigData; windowsZip=false) = let proj = toLowerAscii(c.name) & "-" & c.version let tmpDir = if c.outdir.len == 0: "build" else: c.outdir proc processFile(destFile, src: string) = let dest = tmpDir / destFile echo "Copying ", src, " to ", dest if not existsFile(src): echo "[Warning] Source file doesn't exist: ", src let destDir = dest.splitFile.dir if not dirExists(destDir): createDir(destDir) copyFileWithPermissions(src, dest) if not windowsZip and not existsFile("build" / buildBatFile32): quit("No C sources found in ./build/, please build by running " & "./koch csource -d:release.") if not windowsZip: processFile(proj / buildBatFile32, "build" / buildBatFile32) processFile(proj / buildBatFile64, "build" / buildBatFile64) processFile(proj / buildShFile, "build" / buildShFile) processFile(proj / makeFile, "build" / makeFile) processFile(proj / installShFile, installShFile) processFile(proj / deinstallShFile, deinstallShFile) for f in walkFiles(c.libpath / "lib/*.h"): processFile(proj / "c_code" / extractFilename(f), f) for osA in 1..c.oses.len: for cpuA in 1..c.cpus.len: var dir = buildDir(osA, cpuA) for k, f in walkDir("build" / dir): if k == pcFile: processFile(proj / dir / extractFilename(f), f) else: for f in items(c.cat[fcWinBin]): let filename = f.extractFilename processFile(proj / "bin" / filename, f) let osSpecific = if windowsZip: fcWindows else: fcUnix for cat in items({fcConfig..fcOther, osSpecific, fcNimble}): echo("Current category: ", cat) for f in items(c.cat[cat]): processFile(proj / f, f) # Copy the .nimble file over let nimbleFile = c.nimblePkgName & ".nimble" processFile(proj / nimbleFile, nimbleFile) when true: let oldDir = getCurrentDir() setCurrentDir(tmpDir) try: if windowsZip: if execShellCmd("7z a -tzip $1.zip $1" % proj) != 0: echo("External program failed") else: if execShellCmd("XZ_OPT=-9 gtar Jcf $1.tar.xz $1 --exclude=.DS_Store" % proj) != 0: # try old 'tar' without --exclude feature: if execShellCmd("XZ_OPT=-9 tar Jcf $1.tar.xz $1" % proj) != 0: echo("External program failed") finally: setCurrentDir(oldDir) # -- prepare build files for .deb creation proc debDist(c: var ConfigData) = if not existsFile(getOutputDir(c) / "build.sh"): quit("No build.sh found.") if not existsFile(getOutputDir(c) / "install.sh"): quit("No install.sh found.") if c.debOpts.shortDesc == "": quit("shortDesc must be set in the .ini file.") if c.debOpts.licenses.len == 0: echo("[Warning] No licenses specified for .deb creation.") # -- Copy files into /tmp/.. echo("Copying source to tmp/niminst/deb/") var currentSource = getCurrentDir() var workingDir = getTempDir() / "niminst" / "deb" var upstreamSource = (c.name.toLowerAscii() & "-" & c.version) createDir(workingDir / upstreamSource) template copyNimDist(f, dest: string) = createDir((workingDir / upstreamSource / dest).splitFile.dir) copyFile(currentSource / f, workingDir / upstreamSource / dest) # Don't copy all files, only the ones specified in the config: copyNimDist(buildShFile, buildShFile) copyNimDist(makeFile, makeFile) copyNimDist(installShFile, installShFile) createDir(workingDir / upstreamSource / "build") for f in walkFiles(c.libpath / "lib/*.h"): copyNimDist(f, "build" / extractFilename(f)) for osA in 1..c.oses.len: for cpuA in 1..c.cpus.len: var dir = buildDir(osA, cpuA) for k, f in walkDir(dir): if k == pcFile: copyNimDist(f, dir / extractFilename(f)) for cat in items({fcConfig..fcOther, fcUnix}): for f in items(c.cat[cat]): copyNimDist(f, f) # -- Create necessary build files for debhelper. let mtnName = c.vars["mtnname"] let mtnEmail = c.vars["mtnemail"] prepDeb(c.name, c.version, mtnName, mtnEmail, c.debOpts.shortDesc, c.description, c.debOpts.licenses, c.cat[fcUnixBin], c.cat[fcConfig], c.cat[fcDoc], c.cat[fcLib], c.debOpts.buildDepends, c.debOpts.pkgDepends) # ------------------- main ---------------------------------------------------- var c: ConfigData iniConfigData(c) parseCmdLine(c) parseIniFile(c) if actionInno in c.actions: setupDist(c) if actionNsis in c.actions: setupDist2(c) if actionCSource in c.actions: srcdist(c) if actionScripts in c.actions: writeInstallScripts(c) if actionZip in c.actions: xzDist(c, true) if actionXz in c.actions: xzDist(c) if actionDeb in c.actions: debDist(c)