forked from xyproto/wallutils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
xwallpaper.h
64 lines (56 loc) · 1.77 KB
/
xwallpaper.h
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
#include "X11/bitmaps/gray"
#include <X11/Xatom.h>
#include <X11/Xcursor/Xcursor.h>
#include <X11/Xlib.h>
#include <X11/Xmu/CurUtil.h>
#include <X11/Xutil.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static Display* dpy;
static int screen;
static Window root;
static Pixmap ReadBitmapFile(
char* filename, unsigned int* width, unsigned int* height, int* x_hot, int* y_hot)
{
Pixmap bitmap;
int status = XReadBitmapFile(dpy, root, filename, width, height, &bitmap, x_hot, y_hot);
if (status == BitmapSuccess) {
return (bitmap);
} else if (status == BitmapOpenFailed) {
fprintf(stderr, "can't open file: %s\n", filename);
} else if (status == BitmapFileInvalid) {
fprintf(stderr, "bad bitmap format file: %s\n", filename);
}
fprintf(stderr, "insufficient memory for bitmap: %s", filename);
exit(1);
}
static void SetBackgroundToBitmap(Pixmap bitmap, unsigned int width, unsigned int height)
{
XGCValues gc_init;
GC gc = XCreateGC(dpy, root, GCForeground | GCBackground, &gc_init);
Pixmap pix = XCreatePixmap(dpy, root, width, height, (unsigned int)DefaultDepth(dpy, screen));
XCopyPlane(dpy, bitmap, pix, gc, 0, 0, width, height, 0, 0, (unsigned long)1);
XSetWindowBackgroundPixmap(dpy, root, pix);
XFreeGC(dpy, gc);
XFreePixmap(dpy, bitmap);
XFreePixmap(dpy, pix);
XClearWindow(dpy, root);
}
int SetBackground(char* filename)
{
unsigned int ww;
unsigned int hh;
dpy = XOpenDisplay("");
if (!dpy) {
return -1;
}
screen = DefaultScreen(dpy);
root = RootWindow(dpy, screen);
Pixmap bitmap = ReadBitmapFile(filename, &ww, &hh, (int*)NULL, (int*)NULL);
SetBackgroundToBitmap(bitmap, ww, hh);
if (dpy) {
XCloseDisplay(dpy);
}
return 0;
}