/
xmarquee.c
102 lines (79 loc) · 1.81 KB
/
xmarquee.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <unistd.h>
#include <X11/Xlib.h>
Display *display;
Window window;
GC gc;
int x, y;
int width, height;
void clearScreen(){
XClearArea(display, window, 0, 0, width, height, 0);
}
void cleanup(int sig){
clearScreen();
XFlush(display);
XFreeGC(display, gc);
XCloseDisplay(display);
exit(EXIT_SUCCESS);
}
int main(int argc, char *argv[]){
XTextItem textItem;
int whiteColor, blackColor, color;
int speed = 5;
char opt;
textItem.delta = 0;
textItem.font = None;
display = XOpenDisplay(NULL);
if(display == NULL){
fprintf(stderr, "Failed to open display.\n");
exit(EXIT_FAILURE);
}
width = DisplayWidth(display, DefaultScreen(display));
height = DisplayHeight(display, DefaultScreen(display));
whiteColor = WhitePixel(display, DefaultScreen(display));
blackColor = BlackPixel(display, DefaultScreen(display));
color = whiteColor;
while((opt = getopt(argc, argv, "wbs:")) != -1){
switch(opt){
case 'w':
color = whiteColor;
break;
case 'b':
color = blackColor;
break;
case 's':
speed = atoi(optarg);
break;
default:
fprintf(stderr, "Unrecognized option: %c", opt);
XCloseDisplay(display);
exit(EXIT_FAILURE);
}
}
if(optind >= argc){
fprintf(stderr, "Usage: %s [options] command\n", argv[0]);
XCloseDisplay(display);
exit(EXIT_FAILURE);
}
window = DefaultRootWindow(display);
signal(SIGINT, cleanup);
gc = XCreateGC(display, window, 0, NULL);
XSetForeground(display, gc, color);
x = 0;
y = height / 2;
for(;;){
clearScreen();
textItem.chars = argv[optind];
textItem.nchars = strlen(argv[optind]);
XDrawText(display, window, gc, x, y, &textItem, 1);
x += speed;
if(x > width) x = 0;
XFlush(display);
usleep(10000);
}
cleanup(SIGINT);
return 0;
}