Skip to content

Commit 5769b07

Browse files
committed
Use CreateProcess for running commands
1 parent e7230c3 commit 5769b07

File tree

2 files changed

+79
-27
lines changed

2 files changed

+79
-27
lines changed

src/lib.mjs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,27 @@ globalThis.setInterval = (fn, interval) => {
1515
setInterval(__wbc.yieldToC, 10);
1616
setInterval(__wbc.checkBarSize, 100);
1717

18+
// TODO: colored info, warn, error
19+
console.error = (...args) => std.err.printf('%s\n', args.join(' '));;
20+
1821
// Load all scripts within the `blocks` dir
1922
const [files, err] = os.readdir('./blocks');
2023
if (err) {
2124
console.log('Failed to open directory "blocks", does it exist?');
2225
std.exit(1);
2326
}
2427
files.filter(f => !f.startsWith('.')).sort().forEach(script => {
25-
std.loadScript('./blocks/' + script);
28+
std.out.printf('Loading %s... ', script);
29+
const data = std.loadFile('./blocks/' + script);
30+
if (!data) {
31+
throw 'Failed to load ' + data;
32+
}
33+
try {
34+
(() => {
35+
eval(data);
36+
})();
37+
std.out.printf('OK!\n');
38+
} catch (ex) {
39+
console.error(`Error running script '${script}':`, ex);
40+
}
2641
});

src/main.c

Lines changed: 63 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -135,16 +135,18 @@ void updateBlocks(HWND wnd)
135135
SetBkMode(wb.hdc, TRANSPARENT);
136136
wblock_t *block = headBlock;
137137
while (block) {
138-
// Draw text
139-
SetTextColor(wb.hdc, block->color),
140-
SelectObject(wb.hdc, block->font->handle);
141-
rect.right -= block->padRight;
142-
DrawTextW(wb.hdc, block->text, block->textLen, &rect,
143-
DT_NOCLIP | DT_NOPREFIX | DT_SINGLELINE | DT_RIGHT | DT_VCENTER);
144-
RECT rectCalc = { .right = sz.cx, .bottom = sz.cy };
145-
DrawTextW(wb.hdc, block->text, block->textLen, &rectCalc,
146-
DT_NOCLIP | DT_NOPREFIX | DT_SINGLELINE | DT_RIGHT | DT_VCENTER | DT_CALCRECT);
147-
rect.right -= rectCalc.right + block->padLeft;
138+
if (block->visible) {
139+
// Draw text
140+
SetTextColor(wb.hdc, block->color),
141+
SelectObject(wb.hdc, block->font->handle);
142+
rect.right -= block->padRight;
143+
DrawTextW(wb.hdc, block->text, block->textLen, &rect,
144+
DT_NOCLIP | DT_NOPREFIX | DT_SINGLELINE | DT_RIGHT | DT_VCENTER);
145+
RECT rectCalc = { .right = sz.cx, .bottom = sz.cy };
146+
DrawTextW(wb.hdc, block->text, block->textLen, &rectCalc,
147+
DT_NOCLIP | DT_NOPREFIX | DT_SINGLELINE | DT_RIGHT | DT_VCENTER | DT_CALCRECT);
148+
rect.right -= rectCalc.right + block->padLeft;
149+
}
148150

149151
block = block->tail; // Next
150152
}
@@ -290,6 +292,7 @@ static inline wblock_t *getBlockThis(JSValueConst this)
290292

291293
JSValue jsBlockSetFont(JSContext *ctx, JSValueConst this, int argc, JSValueConst *argv)
292294
{
295+
// TODO: allow name only, using default size
293296
if (argc != 2 || !JS_IsString(argv[0]) || !JS_IsNumber(argv[1])) {
294297
return JS_ThrowTypeError(ctx, "Invalid argument");
295298
}
@@ -399,28 +402,62 @@ void jsShellResolve(void *data)
399402
free(td);
400403
}
401404

405+
char *runProcess(char *cmd)
406+
{
407+
// Create pipes for menu
408+
SECURITY_ATTRIBUTES sa = {
409+
.nLength = sizeof(SECURITY_ATTRIBUTES),
410+
.bInheritHandle = TRUE,
411+
};
412+
HANDLE stdoutR, stdoutW;
413+
CreatePipe(&stdoutR, &stdoutW, &sa, 0);
414+
assert(SetHandleInformation(stdoutR, HANDLE_FLAG_INHERIT, 0));
415+
416+
// Open menu
417+
PROCESS_INFORMATION pi;
418+
STARTUPINFO si = {
419+
.cb = sizeof(STARTUPINFO),
420+
.hStdOutput = stdoutW,
421+
.hStdError = stdoutW,
422+
.dwFlags = STARTF_USESTDHANDLES,
423+
};
424+
if (!CreateProcessA(NULL, cmd, NULL, NULL, TRUE, 0, NULL, NULL, &si, &pi)) {
425+
CloseHandle(stdoutR);
426+
CloseHandle(stdoutW);
427+
return NULL;
428+
}
429+
CloseHandle(stdoutW);
430+
431+
// Read output
432+
char *buf = xmalloc(4096);
433+
size_t len = 0;
434+
DWORD bread;
435+
while (ReadFile(stdoutR, buf + len, 4096, &bread, NULL)) {
436+
len += bread;
437+
xrealloc((void**)&buf, len + 4096);
438+
}
439+
buf[len] = 0;
440+
441+
// Clean up
442+
CloseHandle(stdoutR);
443+
CloseHandle(pi.hProcess);
444+
CloseHandle(pi.hThread);
445+
446+
return buf;
447+
}
448+
402449
// The command runner for `jsShell`, ran on a different thread
403450
DWORD CALLBACK jsShellThread(LPVOID param)
404451
{
405452
js_shell_thread_data_t *td = param;
406-
FILE *f = popen(td->cmd, "rb");
407-
if (!f) {
453+
char *res = runProcess(td->cmd);
454+
if (res) {
455+
td->success = true;
456+
td->result = res;
457+
} else {
408458
td->success = false;
409459
td->result = strdup("failed to run command");
410-
return 0;
411460
}
412-
char *buf = xmalloc(4096);
413-
size_t len = 0, bread;
414-
while ((bread = fread(buf, 1, 4096, f)) > 0) {
415-
len += bread;
416-
if (bread == 4096) {
417-
xrealloc((void**)&buf, len + 4096);
418-
}
419-
}
420-
fclose(f);
421-
buf[len] = 0;
422-
td->success = true;
423-
td->result = buf;
424461
mainQueueAppend(jsShellResolve, td);
425462
return 0;
426463
}
@@ -468,7 +505,7 @@ int CALLBACK WinMain(HINSTANCE inst, HINSTANCE prevInst, LPSTR cmdLine, int cmdS
468505
defaultBlock.font = xmalloc(sizeof(fontref_t));
469506
defaultBlock.font->handle = CreateFont(22, 0, 0, 0, FW_NORMAL, 0, 0, 0, 0, 0, 0, 0, 0, "Courier New");
470507
defaultBlock.font->refCount = 1;
471-
assert(defaultBlock.font);
508+
assert(defaultBlock.font->handle);
472509

473510
// Create mutex
474511
mainQueueMutex = CreateMutex(NULL, false, NULL);

0 commit comments

Comments
 (0)