-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprintprogram.c
156 lines (126 loc) · 3.32 KB
/
printprogram.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
// from http://www.dis.uniroma1.it/~liberato/screensaver/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <limits.h>
#include <X11/Xlib.h>
#include <X11/Intrinsic.h>
#include <X11/IntrinsicP.h>
#include <X11/CoreP.h>
#include <X11/Shell.h>
#include <X11/StringDefs.h>
#include <X11/Xutil.h>
#include <X11/keysym.h>
#include <X11/Xatom.h>
#include <X11/Xlib.h>
#include <X11/Xos.h>
#include <X11/Xproto.h>
#include "vroot.h"
#define NCOLORS 3
#define NUMPROGRAMS 28
int getWinFromEnv() {
char *s, *end;
long w;
// Read environment var
s = getenv("XSCREENSAVER_WINDOW");
if (!s || !*s) {
printf("No env\n");
return -1;
}
// Scan as int
w = strtol(s, &end, 0);
if (*end) {
printf("Bad value: %s\n", s);
return -1;
}
return w > INT_MAX ? -1 : (int)w;
}
int main(int argc, char **argv) {
srand(time(NULL));
Display *dpy;
Window root;
XWindowAttributes wa;
GC g;
char *progclass = "java screensaver";
Font f;
XFontStruct *fs;
XGCValues v;
// from http://stackoverflow.com/questions/7684359/using-nanosleep-in-c
const struct timespec *tim = (const struct timespec[]) { {0, 100000000L} };
const struct timespec *endtim = (const struct timespec[]) { {2, 0} };
char *colors[NCOLORS] = { "red", "green", "blue" };
XColor xcolors[NCOLORS];
XColor xc, sc;
int x, y;
int c;
/* open the display (connect to the X server) */
XtAppContext app;
Widget app_App;
app_App = XtAppInitialize(&app, progclass, NULL, 0, &argc, argv, NULL, 0, 0);
dpy = XtDisplay(app_App);
/* get the root window */
root = getWinFromEnv();
printf("envroot: %d\n", root);
if (root == -1)
root = RootWindowOfScreen(XtScreen(app_App));
printf("root: %d\n", root);
/* get attributes of the root window */
XGetWindowAttributes(dpy, root, &wa);\
/* create a GC for drawing in the window */
g = XCreateGC(dpy, root, 0, NULL);
/* load a font */
f = XLoadFont(dpy, "-*-helvetica-bold-r-*-*-20-*-*-*-*-*-*-*");
XSetFont(dpy, g, f);
/* get font metrics */
XGetGCValues(dpy, g, GCFont, &v);
fs = XQueryFont(dpy, v.font);
/* allocate colors */
for (c = 0; c < NCOLORS; c++) {
XAllocNamedColor(dpy, DefaultColormapOfScreen(DefaultScreenOfDisplay (dpy)), colors[c], &sc, &xc);
xcolors[c] = sc;
}
/* draw something */
x = 0;
y = fs->ascent;
/* set a random foreground color */
XSetForeground(dpy, g, xcolors[random() % NCOLORS].pixel);
char s;
while ((s = fgetc(stdin)) != EOF) {
char *string;
char buf[2];
buf[1] = '\0';
// check for newline
if (s == '\n') {
x = 0;
y = y + fs->ascent + fs->descent;
continue;
}
// check for tab character
if (s == '\t') {
string = " ";
} else {
string = buf;
string[0] = s;
}
/* draw the string */
XDrawString(dpy, root, g, x, y, string, strlen(string));
/* increase x and y */
x = x + XTextWidth(fs, string, strlen(string));
if (x > wa.width) {
x = 0;
y = y + fs->ascent + fs->descent;
}
if (y - fs->ascent > wa.height) {
XClearWindow(dpy, root);
x = 0;
y = fs->ascent;
}
/* flush changes and sleep */
XFlush(dpy);
nanosleep(tim, NULL);
}
nanosleep(endtim, NULL);
XClearWindow(dpy, root);
XCloseDisplay(dpy);
}