main
xd.c
1#include<X11/Xlib.h>
2#include<X11/keysym.h>
3
4#include<stdbool.h>
5#include<stdio.h>
6#include<stdlib.h>
7
8int main(int ac, char **av)
9{
10 int wi=640, hi=480;
11
12 Display *d=XOpenDisplay(NULL);
13 if(!d)
14 {
15 fprintf(stderr, "couldnt open display\n");
16 return 1;
17 }
18
19 int s=DefaultScreen(d);
20
21 XColor bg;
22 XParseColor(d, DefaultColormap(d, s), av[1] ? av[1] : "#ffffff", &bg);
23 XAllocColor(d, DefaultColormap(d, s), &bg);
24
25 Window w=XCreateSimpleWindow(d, RootWindow(d, s),
26 100, 100, wi, hi, 0, BlackPixel(d,s),
27 WhitePixel(d, s));
28 XStoreName(d, w, "xd");
29 XSelectInput(d, w, ExposureMask|KeyPressMask|StructureNotifyMask|
30 ButtonPressMask|ButtonReleaseMask|
31 PointerMotionMask);
32 XMapWindow(d, w);
33 GC gc=XCreateGC(d, w, 0, NULL);
34 Pixmap p=XCreatePixmap(d,w,wi,hi,DefaultDepth(d,s));
35 XSetForeground(d, gc, bg.pixel);
36 XFillRectangle(d, p,gc,0,0,wi,hi);
37 XSetForeground(d, gc, BlackPixel(d,s));
38
39 int z=20;
40 XSetLineAttributes(d, gc, z, LineSolid, CapRound, JoinRound);
41
42 bool dr=false, er=false;
43 int lx=0, ly=0;
44
45 while(1)
46 {
47 XEvent ev;
48 XNextEvent(d, &ev);
49 XSetLineAttributes(d, gc, z, LineSolid, CapRound, JoinRound);
50
51 switch(ev.type)
52 {
53 case Expose:
54 XCopyArea(d,p,w,gc,0,0,wi,hi,0,0);
55 break;
56 case ConfigureNotify:
57 if(ev.xconfigure.width>wi&&ev.xconfigure.height>hi)
58 {
59 Pixmap l=XCreatePixmap(d,w,
60 ev.xconfigure.width,ev.xconfigure.height,
61 DefaultDepth(d,s));
62
63 XSetForeground(d, gc, bg.pixel);
64 XFillRectangle(d, l,gc,0,0,ev.xconfigure.width,ev.xconfigure.height);
65
66 int cw = wi < ev.xconfigure.width ? wi : ev.xconfigure.width;
67 int ch = hi < ev.xconfigure.height ? hi : ev.xconfigure.height;
68
69 XCopyArea(d,p,l,gc,0,0,
70 cw,ch,
71 0,0);
72 XFreePixmap(d,p);
73 p=l;
74
75 wi=ev.xconfigure.width;
76 hi=ev.xconfigure.height;
77
78 XCopyArea(d,p,w,gc,0,0,wi,hi,0,0);
79 }
80 break;
81 case ButtonPress:
82 if(ev.xbutton.button==Button1)
83 {
84 dr=true;
85 lx=ev.xbutton.x;
86 ly=ev.xbutton.y;
87 }
88 break;
89 case ButtonRelease:
90 if(ev.xbutton.button==Button1)
91 {
92 dr=false;
93 }
94 break;
95 case MotionNotify:
96 if (dr)
97 {
98 XSetForeground(d, gc, er ? bg.pixel : BlackPixel(d,s));
99 XDrawLine(d,p,gc,lx,ly,
100 ev.xmotion.x,
101 ev.xmotion.y);
102 XCopyArea(d,p,w,gc,0,0,wi,hi,0,0);
103 lx=ev.xmotion.x;
104 ly=ev.xmotion.y;
105 }
106 break;
107 case KeyPress: {
108 KeySym k=XLookupKeysym(&ev.xkey, 0);
109 if (k==XK_q||k==XK_Escape) goto q;
110 if (k==XK_c) {
111 XSetForeground(d, gc, bg.pixel);
112 XFillRectangle(d, p,gc,0,0,wi,hi);
113 XCopyArea(d,p,w,gc,0,0,wi,hi,0,0);
114 XSetForeground(d, gc, BlackPixel(d,s));
115 }
116 if (k==XK_e) {
117 er=!er;
118 printf("er=%s\n", er ? "true" : "false");
119 }
120
121
122 if (k==XK_i) z= z<=1 ? z+4 : z+5;
123 if (k==XK_o) z= z>6 ? (z-5) : 1;
124 if (k==XK_p) z=20;
125
126 if (k==XK_i||k==XK_o||k==XK_p) printf("z=%d\n", z);
127
128 break;
129 }
130 }
131 }
132
133q:
134 XFreePixmap(d,p);
135 XFreeGC(d,gc);
136 XDestroyWindow(d,w);
137 XCloseDisplay(d);
138 return 0;
139
140}
141