-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
294 lines (244 loc) · 7.9 KB
/
main.cpp
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
/*
* File: main.cpp
* Author: David Young
*
* Created on November 6, 2015, 3:33 PM
*/
#include <cstdlib>
#include <unistd.h>
#include <signal.h>
#include <getopt.h>
#include "Main.h"
using namespace std;
bool terminate_process;
config_vals ParseArguments(int argc, char** argv)
{
enum OPTION_VAL {SWAP, SCALE, HELP};
int opt = 0;
config_vals cfg;
cfg.NoScale = false;
cfg.PrintHelp = false;
cfg.SwapRate = DEFAULT_SWAP_RATE;
cfg.NoSwap = false;
int opt_indx = -1;
option option_a[4];
option_a[0].name = "help";
option_a[0].has_arg = no_argument;
option_a[0].flag = NULL;
option_a[0].val = OPTION_VAL::HELP;
option_a[1].name = "noscale";
option_a[1].has_arg = no_argument;
option_a[1].flag = NULL;
option_a[1].val = OPTION_VAL::SCALE;
option_a[2].name = "swaprate";
option_a[2].flag = NULL;
option_a[2].has_arg = required_argument;
option_a[2].val = OPTION_VAL::SWAP;
option_a[3].name = NULL;
option_a[3].flag = 0;
option_a[3].has_arg = 0;
option_a[3].val = 0;
while((opt = getopt_long(argc, argv, "hn:r:", option_a, &opt_indx)) != -1)
{
switch(opt)
{
case OPTION_VAL::SCALE:
case 'n':
cfg.NoScale = true;
break;
case OPTION_VAL::HELP:
case 'h':
cfg.PrintHelp = true;
break;
case OPTION_VAL::SWAP:
case 'r':
cfg.SwapRate = ::atoi(optarg);
if(cfg.SwapRate == 0)
{
ERROR_SYSTEM("Option -r [--swaprate] must be a non-zero integer.");
exit(-1);
}
break;
case 'p':
cfg.NoSwap = true;
break;
case '?':
default:
exit(-1);
}
}
return cfg;
}
void PrintHelp()
{
cout<<"This program can be used to set the background of the root window "
"in your XClient.\nRotates backgrounds from the `~/.wallpapers folder'.\n"
"\t-n [--noscale] Do not scale background images.\n"
"\t-r [--swaprate] The rate in seconds that the background rotates.\n"
"\t-p Do not swap background images (random pick).\n"
"\t-h [--help] Print information on program switches."<<endl;
}
void HandleTermiSignals(int signo)
{
cout << "Terminating process..." << endl;
terminate_process = true;
}
void RegisterSignalHandler()
{
struct sigaction sa;
memset(&sa, 0, sizeof(sa));
sa.sa_handler = HandleTermiSignals;
sa.sa_flags = SA_NOCLDWAIT;
if(sigaction(SIGINT, &sa, NULL) ||
sigaction(SIGQUIT, &sa, NULL) ||
sigaction(SIGHUP, &sa, NULL))
{
ERROR_SYSTEM("Failed to register signal handler...");
exit(-1);
}
}
int main(int argc, char** argv) {
std::locale::global(std::locale(""));
RegisterSignalHandler();
terminate_process = false;
config_vals cfg = ParseArguments(argc, argv);
if(cfg.PrintHelp)
{
PrintHelp();
exit(0);
}
RootWindowContainer rwc;
deque<string> files;
if(PickFilesToLoad(files) != 0)
exit(-1);
InitImlib(rwc);
std::string select_f;
//Early exit -- we got our random file.
if(cfg.NoSwap)
{
select_f = WALLPAPER_DIR + files.front();
if(ProcessBackground(rwc, select_f, cfg) != 0)
{
ERROR_SYSTEM("Failed to load a background image. Check for locks.");
exit(-1);
}
exit(0);
}
chrono::time_point<chrono::system_clock> start, now;
chrono::duration<long> bg_swap_t = std::chrono::seconds(cfg.SwapRate);
start = chrono::system_clock::now();
while(!terminate_process)
{
//Instigate background change
now = chrono::system_clock::now();
auto delta = chrono::duration_cast<chrono::seconds>(now - start);
if(delta.count() > bg_swap_t.count())
{
start = now;
std::string t_file = files.front();
files.pop_front();
select_f = WALLPAPER_DIR + t_file;
files.push_back(t_file);
cout<<"Loading '"<<select_f<<"'..."<<endl;
if(ProcessBackground(rwc, select_f, cfg) != 0)
ERROR_SYSTEM("Failed to swap background...");
}
//Check for change in wallpaper directory
deque<string> n_file_set;
if(PickFilesToLoad(n_file_set) == 0 &&
n_file_set.size() != files.size())
files = n_file_set;
//Poll process
usleep(TIMER_POLLING_RATE * 1000);
}
cout << "Exiting..." << endl;
return 0;
}
int InitImlib(RootWindowContainer& rwc)
{
const Window root_win = rwc.GetRootWindow();
Display* display = rwc.GetDisplay();
::imlib_set_cache_size(MEGABYTE * 2);
::imlib_set_color_usage(128);
::imlib_context_set_dither(1);
::imlib_context_set_display(display);
::imlib_context_set_visual(DefaultVisual(display, DefaultScreen(display)));
::imlib_context_set_colormap(DefaultColormap(display, DefaultScreen(display)));
::imlib_context_set_drawable(root_win);
}
int PickFilesToLoad(deque<string>& files)
{
files = extrautility::ListFiles(WALLPAPER_DIR);
if(extrautility::GetLastError() == extrautility::BAD_DIRECTORY)
{
ERROR_SYSTEM("The ~/.wallpapers/ directory does not exist."
" Please create and populate it.");
return -1;
}
//Inverse the set of all files with the following extensions and filter them
//out.
extrautility::RegexFilter filter(".+\\.jpg|.+\\.jpeg|.+\\.tiff|.+\\.gif|.+\\.bmp|.+\\.png");
filter.invert();
files.erase(std::remove_if(files.begin(), files.end(), filter), files.end());
if(files.empty())
{
ERROR_SYSTEM("The ~/.wallpapers/ directory is empty.");
return -1;
}
std::shuffle(files.begin(), files.end(),
std::default_random_engine());
return 0;
}
int ProcessBackground(RootWindowContainer& rwc, string& file, config_vals cfg)
{
const Window root_win = rwc.GetRootWindow();
Display* display = rwc.GetDisplay();
//Get Window Attributes
XWindowAttributes win_attr;
XGetWindowAttributes(display, root_win, &win_attr);
int try_again = 0;
Imlib_Image buffer;
//Load buffer image and free it after creating a pixmap version
while((buffer = imlib_load_image(file.c_str())) == NULL)
{
if(3 <= try_again++)
{
ERROR_SYSTEM("Failed to load image.");
return -1;
}
usleep(1000*5000);
}
static Pixmap bg_pixm = -1;
static Pixmap pixm_mask = -1;
Pixmap temp_pixm = bg_pixm;
imlib_context_set_image(buffer);
imlib_context_set_dither(0);
if(cfg.NoScale)
{
imlib_render_pixmaps_for_whole_image(&bg_pixm, &pixm_mask);
}
else
{
imlib_render_pixmaps_for_whole_image_at_size(&bg_pixm, &pixm_mask,
win_attr.width, win_attr.height);
}
switch(XSetWindowBackgroundPixmap(display, root_win, bg_pixm))
{
case BadMatch:
ERROR_INTERNAL("Malformed arguments passed to"
" XSetWindowBackgroundPixmap in ProcessBackground.");
return -1;
case BadPixmap:
ERROR_INTERNAL("Invalid Pixmap passed to"
" XSetWindowBackgroundPixmap in ProcessBackground.");
return -1;
case BadWindow:
ERROR_INTERNAL("Invalid Window passed to"
" XSetWindowBackgroundPixmap in ProcessBackground.");
return -1;
}
if(temp_pixm != -1)
::XFreePixmap(display, temp_pixm);
imlib_free_image();
return 0;
}