This repository has been archived by the owner on Jun 10, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathxosdvol.c
129 lines (110 loc) · 2.94 KB
/
xosdvol.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
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <unistd.h>
#include <errno.h>
#include <xosd.h>
#include "voleventd.h"
struct volume {
int status;
int percent;
};
void
parse(struct volume *vol, char *msg, int index)
{
char sp[5], ss[1];
int i, j, p, s;
i = 0; j = index;
while ((sp[i++] = msg[j++]));
printf("sp: |%s|\n", sp);
p = atoi(sp);
printf("p: %i\n", p);
while (msg[j++] != ' ');
ss[0] = msg[j];
printf("ss: |%s|\n", ss);
s = atoi(ss);
printf("s: %i\n", s);
}
// could work out the index dynamically but it seems pointless
int
parse_percent(char *msg, int index)
{
char sp[5];
int i, j, p;
i = 0; j = index;
while ((sp[i++] = msg[j++]));
//printf("sp: |%s|\n", sp);
p = atoi(sp);
//printf("p: %i\n", p);
return p;
}
int
parse_mute(char *msg, int index)
{
char sp[5];
int i, j, p;
i = 0; j = index;
while (msg[j++] != ' ');
sp[0] = msg[j];
//printf("sp: |%s|\n", sp);
p = atoi(sp);
//printf("p: %i\n", p);
return p;
}
void
volume_display(xosd *osd, int percent, int muted)
{
char vol_percent[25];
if (muted)
snprintf(vol_percent, 22, "Volume - %i%% (Muted)", percent);
else
snprintf(vol_percent, 14, "Volume - %i%%", percent);
xosd_set_timeout(osd, 5);
xosd_display(osd, 0, XOSD_string, vol_percent);
xosd_display(osd, 1, XOSD_slider, percent);
}
int
main(void)
{
int s_fd;
struct sockaddr_un server;
char msg[20];
xosd *osd;
if ((s_fd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
fprintf(stderr, "Couldn't create socket: %s\n", strerror(errno));
return EXIT_FAILURE;
}
memset(&server, 0, sizeof(struct sockaddr_un));
server.sun_family = AF_UNIX;
strcpy(server.sun_path, VOLEVENTD_SOCKET);
if (connect(s_fd, (struct sockaddr *) &server,
sizeof(struct sockaddr_un)) < 0) {
fprintf(stderr, "Couldn't connect to server: %s\n", strerror(errno));
return EXIT_FAILURE;
}
memset(&msg, 0, sizeof(msg));
/* set volume display options */
osd = xosd_create(2);
xosd_set_bar_length(osd, 30);
xosd_set_pos(osd, XOSD_middle);
xosd_set_align(osd, XOSD_center);
while (read(s_fd, msg, 20)) {
if (strncmp(msg, MSG_MUTE, 4) == 0) {
volume_display(osd, parse_percent(msg, 5), !parse_mute(msg, 5));
}
else if (strncmp(msg, MSG_UNMUTE, 6) == 0) {
volume_display(osd, parse_percent(msg, 7), !parse_mute(msg, 7));
}
else if (strncmp(msg, MSG_VOL_UP, 6) == 0) {
volume_display(osd, parse_percent(msg, 7), !parse_mute(msg, 7));
}
else if (strncmp(msg, MSG_VOL_DOWN, 8) == 0) {
volume_display(osd, parse_percent(msg, 9), !parse_mute(msg, 9));
}
memset(msg, 0, 20);
}
close(s_fd);
return 0;
}