-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
214 lines (179 loc) · 5.62 KB
/
main.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
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
/*
Copyright 08/02/2024 https://github.com/su8/doomy
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
MA 02110-1301, USA.
Compile with:
gcc -Wall -Wextra -O2 -I/usr/include/freetype2 -lX11 -lXft -o doomy doomy.c
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <argp.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xft/Xft.h>
static inline void drawString(const char *);
static error_t parse_opt(int , char *, struct argp_state *);
static XftColor brown;
static XftColor pink;
static XftFont *xftfont;
static XftDraw *xdraw = NULL;
static const char *BG_COLOR = "";
static const char *TEXT_COLOR = "";
static const char *USE_FONT = "";
static unsigned int BAR_HEIGHT = 18U;
static unsigned int TOP_BAR = 1U;
static char doc[] = "Dead simple x11 bar/statusline program.\v";
const char *argp_program_version = "1.0";
const char *argp_program_bug_address = "https://github.com/su8/doomy ";
static struct argp_option options[] =
{
{ .doc = "" },
{ .name = "bgcolor", .key = 'b', .arg = "#282a2e", .doc = "Set background color"},
{ .name = "textcolor", .key = 't', .arg = "#b294bb", .doc = "Set text color"},
{ .name = "top", .key = 's', .arg = "1", .doc = "Show the program on top == 1, else 0 bottom position"},
{ .name = "font", .key = 'f', .arg = "xft#DejaVu Sans:size=9:style=bold", .doc = "Use custom font size and bold type"},
{ .name = "barheight", .key = 'h', .arg = "15", .doc = "Set the height of the bar"},
{ .doc = NULL }
};
static error_t parse_opt(int key, char *arg, struct argp_state *state)
{
(void)state;
switch(key)
{
case 'b':
BG_COLOR = arg;
break;
case 't':
TEXT_COLOR = arg;
break;
case 'f':
USE_FONT = arg;
break;
case 'h':
BAR_HEIGHT = (unsigned int)strtoul(arg, (char **)NULL, 10);
break;
case 's':
TOP_BAR = (unsigned int)strtoul(arg, (char **)NULL, 10);
break;
default:
return ARGP_ERR_UNKNOWN;
}
return EXIT_SUCCESS;
}
static const struct argp arg_parser = {
.parser = parse_opt,
.options = options,
.doc = doc
};
int main(int argc, char *argv[])
{
Window win;
Display *display;
Screen *Screen;
int screen;
XSetWindowAttributes wa;
Colormap cmap;
Visual *visual;
FILE *fp = NULL;
unsigned int showToBottom = 0U;
char buf[1000] = {'\0'};
const char *use_font = *USE_FONT ? USE_FONT : "xft#DejaVu Sans:size=9:style=bold";
argp_parse(&arg_parser, argc, argv, ARGP_IN_ORDER, NULL, NULL);
display = XOpenDisplay(NULL);
if (NULL == display)
{
puts("Cannot open X display.");
return EXIT_FAILURE;
}
screen = DefaultScreen(display);
cmap = DefaultColormap(display, screen);
visual = DefaultVisual(display, screen);
Screen = ScreenOfDisplay(display, DefaultScreen(display));
showToBottom = (unsigned int)Screen->height - (unsigned int)BAR_HEIGHT;
wa.override_redirect = 1;
wa.background_pixmap = ParentRelative;
wa.event_mask = ExposureMask|KeyPressMask;
win = XCreateWindow(display, RootWindow(display, screen),
0, TOP_BAR ? 0 : showToBottom,
(unsigned int)Screen->width, BAR_HEIGHT ? BAR_HEIGHT : 15U, 0,
DefaultDepth(display, screen), CopyFromParent, visual,
CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa
);
XSelectInput(display, win, StructureNotifyMask);
Atom wm_delete_window = XInternAtom(display, "WM_DELETE_WINDOW", 0);
XSetWMProtocols(display, win, &wm_delete_window, 1);
XftColorAllocName(display, visual, cmap, *BG_COLOR ? BG_COLOR : "#282a2e", &brown);
XftColorAllocName(display, visual, cmap, *TEXT_COLOR ? TEXT_COLOR : "#b294bb", &pink);
xftfont = XftFontOpenXlfd(display, screen, use_font);
if (!xftfont)
{
xftfont = XftFontOpenName(display, screen, use_font);
if (!xftfont)
{
return EXIT_FAILURE;
}
}
XMapWindow(display, win);
XFlush(display);
while (1)
{
if (!(fp = fopen("/tmp/doomy.txt", "r")))
{
puts("Couldn't find /tmp/doomy.txt, exiting.");
goto err;
}
fscanf(fp, "%[^\n]\n", buf);
if ((fclose(fp)) == EOF)
{
puts("EOF in fclose(fp), exiting.");
goto err;
}
xdraw = XftDrawCreate(display, win, visual, cmap);
XftDrawRect(xdraw, &brown, 0, 0, (unsigned int)Screen->width, BAR_HEIGHT ? BAR_HEIGHT : 15U);
drawString(buf);
XFlush(display);
sleep(1);
XftDrawDestroy(xdraw);
}
err:
XftColorFree(display, visual, cmap, &brown);
XftColorFree(display, visual, cmap, &pink);
XftFontClose(display, xftfont);
if(xdraw != NULL)
{
XftDrawDestroy(xdraw);
}
if (display != NULL)
{
XDestroyWindow(display, win);
XCloseDisplay(display);
}
return EXIT_SUCCESS;
}
static inline void drawString(const char *str)
{
char buf[1000] = {'\0'};
char *bufPtr = buf;
snprintf(buf, sizeof(buf) - 1, "%s", str);
for (; *bufPtr; bufPtr++)
{
if (*bufPtr == '\n')
{
*bufPtr = '\0';
break;
}
}
XftDrawStringUtf8(xdraw, &pink, xftfont, 0, 1 + xftfont->ascent, (const FcChar8 *)buf, (int)strlen(buf));
}