-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconsole.c
More file actions
70 lines (55 loc) · 1.52 KB
/
Copy pathconsole.c
File metadata and controls
70 lines (55 loc) · 1.52 KB
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
#include "console.h"
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <poll.h>
static const char *inpath = "/tmp/dwm.in";
static const char *cmdpath = "/tmp/dwm.cmd";
static const char *outpath = "/tmp/dwm.out";
Console init_console(Action_msg msgact, Action_cmd cmdact) {
mkfifo(inpath, 0660);
mkfifo(outpath, 0660);
mkfifo(cmdpath, 0660);
return (Console) {
.in = open (inpath, O_RDWR, 0),
.out = open (outpath, O_RDWR, 0),
.cmd = open (cmdpath, O_RDWR, 0),
.msgact = msgact,
.cmdact = cmdact
};
}
void close_console (Console *data) {
close (data->in);
unlink(inpath);
close (data->out);
unlink(outpath);
close (data->cmd);
unlink(cmdpath);
}
void console_job (Console *data) {
struct pollfd polldat[] = {
{.fd = data->in, .events = POLLIN},
{.fd = data->cmd, .events = POLLIN}
};
if (poll(polldat, 2, 0) > 0) {
if (polldat[0].revents & POLLIN) {
char line[81];
int len = read (data->in, line, 81) - 1;
data->msgact(line, len);
}
if (polldat[1].revents & POLLIN) {
char cmd;
read (data->cmd, &cmd, 1);
data->cmdact(cmd, data->cmd, data->out);
}
}
}
void console_log(Console *console, Cstr fmt, ...) {
char buff[256];
va_list args;
va_start(args, fmt);
int l = vsnprintf(buff, 256, fmt, args);
va_end(args);
write(console->out, buff, l);
}