Commit 262d40e60c033c8e2a822ed94d7e2973c7d3c2e8
1 parent
a0ae785b
Working version but 30s is needed by pic
Showing
1 changed file
with
169 additions
and
117 deletions
Show diff stats
1 | #include <X11/Xlib.h> | 1 | #include <X11/Xlib.h> |
2 | -#include <X11/Xutil.h> | ||
3 | -#include <X11/Xos.h> | ||
4 | #include <stdio.h> | 2 | #include <stdio.h> |
5 | -#include <stdlib.h> | 3 | +#include <stdlib.h> /* getenv(), etc. */ |
4 | +#include <unistd.h> /* sleep(), etc. */ | ||
6 | 5 | ||
7 | -#define AWESOME 7 | 6 | +Window create_simple_window(Display* display, int width, int height, int x, int y) |
7 | +{ | ||
8 | + int screen_num = DefaultScreen(display); | ||
9 | + int win_border_width = 2; | ||
10 | + Window win; | ||
11 | + | ||
12 | + win = XCreateSimpleWindow(display, RootWindow(display, screen_num), x, y, width, height, win_border_width, BlackPixel(display, screen_num), WhitePixel(display, screen_num)); | ||
8 | 13 | ||
9 | -Display *dis; | ||
10 | -int screen; | ||
11 | -Window win; | ||
12 | -GC gc; | ||
13 | -int width=320; | ||
14 | -int height=240; | ||
15 | -int i=0; | 14 | + XMapWindow(display, win); |
15 | + XFlush(display); | ||
16 | 16 | ||
17 | -void init_x(); | ||
18 | -void close_x(); | ||
19 | -void redraw(); | 17 | + return win; |
18 | +} | ||
20 | 19 | ||
21 | -int main () | 20 | +GC create_gc(Display* display, Window win, int reverse_video) |
22 | { | 21 | { |
23 | - XEvent event; /* the XEvent declaration !!! */ | ||
24 | - KeySym key; /* a dealie-bob to handle KeyPress Events */ | ||
25 | - char text[255]; /* a char buffer for KeyPress Events */ | ||
26 | - XColor col; | ||
27 | - int initOffsetBMP; | 22 | + GC gc; |
23 | + unsigned long valuemask = 0; /* which values in 'values' to */ | ||
24 | + /* check when creating the GC. */ | ||
25 | + XGCValues values; /* initial values for the GC. */ | ||
26 | + unsigned int line_width = 2; /* line width for the GC. */ | ||
27 | + int line_style = LineSolid; /* style for lines drawing and */ | ||
28 | + int cap_style = CapButt; /* style of the line's edje and */ | ||
29 | + int join_style = JoinBevel; /* joined lines. */ | ||
30 | + int screen_num = DefaultScreen(display); | ||
31 | + | ||
32 | + gc = XCreateGC(display, win, valuemask, &values); | ||
33 | + if (gc < 0) { | ||
34 | + fprintf(stderr, "XCreateGC: \n"); | ||
35 | + } | ||
36 | + | ||
37 | + if (reverse_video) | ||
38 | + { | ||
39 | + XSetForeground(display, gc, WhitePixel(display, screen_num)); | ||
40 | + XSetBackground(display, gc, BlackPixel(display, screen_num)); | ||
41 | + } | ||
42 | + else | ||
43 | + { | ||
44 | + XSetForeground(display, gc, BlackPixel(display, screen_num)); | ||
45 | + XSetBackground(display, gc, WhitePixel(display, screen_num)); | ||
46 | + } | ||
47 | + | ||
48 | + XSetLineAttributes(display, gc, line_width, line_style, cap_style, join_style); | ||
49 | + XSetFillStyle(display, gc, FillSolid); | ||
50 | + | ||
51 | + return gc; | ||
52 | +} | ||
28 | 53 | ||
29 | - init_x(); | 54 | +int main(int argc, char* argv[]) |
55 | +{ | ||
56 | + Display* display; /* pointer to X Display structure. */ | ||
57 | + int screen_num; /* number of screen to place the window on. */ | ||
58 | + Window win; /* pointer to the newly created window. */ | ||
59 | + unsigned int display_width, | ||
60 | + display_height; /* height and width of the X display. */ | ||
61 | + unsigned int width, height; /* height and width for the new window. */ | ||
62 | + char *display_name = getenv("DISPLAY"); /* address of the X display. */ | ||
63 | + GC gc; /* GC (graphics context) used for drawing */ | ||
64 | + /* in our window. */ | ||
65 | + Colormap screen_colormap; /* color map to use for allocating colors. */ | ||
66 | + XColor red, brown, blue, yellow, green; | ||
67 | + /* used for allocation of the given color */ | ||
68 | + /* map entries. */ | ||
69 | + Status rc; /* return status of various Xlib functions. */ | ||
70 | + int initOffsetBMP; | ||
71 | + XColor col; | ||
30 | 72 | ||
31 | - printf("Patte de canard\n"); | ||
32 | - Colormap cmap = DefaultColormap(dis, DefaultScreen(dis)); | ||
33 | - printf("?\n"); | 73 | + display = XOpenDisplay(display_name); |
74 | + if (display == NULL) { | ||
75 | + fprintf(stderr, "%s: cannot connect to X server '%s'\n", | ||
76 | + argv[0], display_name); | ||
77 | + exit(1); | ||
78 | + } | ||
79 | + | ||
80 | + screen_num = DefaultScreen(display); | ||
81 | + display_width = DisplayWidth(display, screen_num); | ||
82 | + display_height = DisplayHeight(display, screen_num); | ||
83 | + | ||
84 | + width = display_width; | ||
85 | + height = display_height; | ||
86 | + printf("window width - '%d'; height - '%d'\n", width, height); | ||
87 | + | ||
88 | + win = create_simple_window(display, width, height, 0, 0); | ||
89 | + | ||
90 | + gc = create_gc(display, win, 0); | ||
91 | + XSync(display, False); | ||
92 | + screen_colormap = DefaultColormap(display, DefaultScreen(display)); | ||
93 | + | ||
94 | + rc = XAllocNamedColor(display, screen_colormap, "red", &red, &red); | ||
95 | + if (rc == 0) { | ||
96 | + fprintf(stderr, "XAllocNamedColor - failed to allocated 'red' color.\n"); | ||
97 | + exit(1); | ||
98 | + } | ||
99 | + rc = XAllocNamedColor(display, screen_colormap, "brown", &brown, &brown); | ||
100 | + if (rc == 0) { | ||
101 | + fprintf(stderr, "XAllocNamedColor - failed to allocated 'brown' color.\n"); | ||
102 | + exit(1); | ||
103 | + } | ||
104 | + rc = XAllocNamedColor(display, screen_colormap, "blue", &blue, &blue); | ||
105 | + if (rc == 0) { | ||
106 | + fprintf(stderr, "XAllocNamedColor - failed to allocated 'blue' color.\n"); | ||
107 | + exit(1); | ||
108 | + } | ||
109 | + rc = XAllocNamedColor(display, screen_colormap, "yellow", &yellow, &yellow); | ||
110 | + if (rc == 0) { | ||
111 | + fprintf(stderr, "XAllocNamedColor - failed to allocated 'yellow' color.\n"); | ||
112 | + exit(1); | ||
113 | + } | ||
114 | + rc = XAllocNamedColor(display, screen_colormap, "green", &green, &green); | ||
115 | + if (rc == 0) { | ||
116 | + fprintf(stderr, "XAllocNamedColor - failed to allocated 'green' color.\n"); | ||
117 | + exit(1); | ||
118 | + } | ||
119 | + | ||
120 | + /* draw one pixel near each corner of the window */ | ||
121 | + /* draw the pixels in a red color. */ | ||
122 | + XSetForeground(display, gc, red.pixel); | ||
123 | + XDrawPoint(display, win, gc, 5, 5); | ||
124 | + XDrawPoint(display, win, gc, 5, height-5); | ||
125 | + XDrawPoint(display, win, gc, width-5, 5); | ||
126 | + XDrawPoint(display, win, gc, width-5, height-5); | ||
34 | 127 | ||
35 | FILE* home = NULL; | 128 | FILE* home = NULL; |
36 | char buffer[4]; | 129 | char buffer[4]; |
@@ -47,23 +140,23 @@ int main () | @@ -47,23 +140,23 @@ int main () | ||
47 | initOffsetBMP = buffer[0] + (buffer[1] >> 8) + (buffer[2] >> 16) + (buffer[3] >> 24); | 140 | initOffsetBMP = buffer[0] + (buffer[1] >> 8) + (buffer[2] >> 16) + (buffer[3] >> 24); |
48 | 141 | ||
49 | fseek(home, 0x12, SEEK_SET); | 142 | fseek(home, 0x12, SEEK_SET); |
50 | - buffer[0] = getc(home); | ||
51 | - buffer[1] = getc(home); | ||
52 | - buffer[2] = getc(home); | ||
53 | - buffer[3] = getc(home); | ||
54 | - printf("Width %02x %02x %02x %02x\n", buffer[0], buffer[1], buffer[2], buffer[3]); | 143 | + buffer[0] = getc(home); |
144 | + buffer[1] = getc(home); | ||
145 | + buffer[2] = getc(home); | ||
146 | + buffer[3] = getc(home); | ||
147 | + printf("Width %02x %02x %02x %02x\n", buffer[0], buffer[1], buffer[2], buffer[3]); | ||
55 | 148 | ||
56 | //fseek(home, 0x12, SEEK_SET); | 149 | //fseek(home, 0x12, SEEK_SET); |
57 | - buffer[0] = getc(home); | ||
58 | - buffer[1] = getc(home); | ||
59 | - buffer[2] = getc(home); | ||
60 | - buffer[3] = getc(home); | ||
61 | - printf("Height %02x %02x %02x %02x\n", buffer[0], buffer[1], buffer[2], buffer[3]); | 150 | + buffer[0] = getc(home); |
151 | + buffer[1] = getc(home); | ||
152 | + buffer[2] = getc(home); | ||
153 | + buffer[3] = getc(home); | ||
154 | + printf("Height %02x %02x %02x %02x\n", buffer[0], buffer[1], buffer[2], buffer[3]); | ||
62 | 155 | ||
63 | fseek(home, 0x1C, SEEK_SET); | 156 | fseek(home, 0x1C, SEEK_SET); |
64 | - buffer[0] = getc(home); | ||
65 | - buffer[1] = getc(home); | ||
66 | - printf("Bits Per Pixel %02x %02x\n", buffer[0], buffer[1]); | 157 | + buffer[0] = getc(home); |
158 | + buffer[1] = getc(home); | ||
159 | + printf("Bits Per Pixel %02x %02x\n", buffer[0], buffer[1]); | ||
67 | 160 | ||
68 | if(buffer[1] != 0 || buffer[0] != 0x18) | 161 | if(buffer[1] != 0 || buffer[0] != 0x18) |
69 | { | 162 | { |
@@ -74,86 +167,45 @@ int main () | @@ -74,86 +167,45 @@ int main () | ||
74 | 167 | ||
75 | fseek(home, initOffsetBMP, SEEK_SET); | 168 | fseek(home, initOffsetBMP, SEEK_SET); |
76 | 169 | ||
77 | - while(1) | ||
78 | - { | ||
79 | - XNextEvent(dis, &event); | 170 | + col.flags= DoRed | DoGreen | DoBlue; |
80 | 171 | ||
81 | - if (event.type==Expose && event.xexpose.count==0) | ||
82 | - redraw(); | ||
83 | - | ||
84 | - if (event.type==KeyPress&& XLookupString(&event.xkey,text,255,&key,0)==1) | ||
85 | - { | ||
86 | - if (text[0]=='q') | ||
87 | - close_x(); | ||
88 | - printf("You pressed the %c key!\n",text[0]); | ||
89 | - } | ||
90 | - if (event.type==ButtonPress) | ||
91 | - { | ||
92 | - //int x=event.xbutton.x, | ||
93 | - //y=event.xbutton.y; | ||
94 | - | ||
95 | - //strcpy(text,"X is FUN!"); | ||
96 | - //XSetForeground(dis,gc,rand()%event.xbutton.x%255); | ||
97 | - //XDrawString(dis,win,gc,x,y, text, strlen(text)); | ||
98 | - | ||
99 | - buffer[0] = getc(home); | ||
100 | - buffer[1] = getc(home); | ||
101 | - buffer[2] = getc(home); | ||
102 | - | ||
103 | - int j; | ||
104 | - | ||
105 | - for(j=0; j<AWESOME; j++) | ||
106 | - { | ||
107 | - col.red = buffer[2]; | ||
108 | - col.green = buffer[1]; | ||
109 | - col.blue = buffer[0]; | ||
110 | - | ||
111 | - | ||
112 | - | ||
113 | - | ||
114 | - | ||
115 | - if(XAllocColor(dis, cmap, &col) == 0) | ||
116 | - printf("Failed to allocate color\n"); | ||
117 | - if(XSetForeground(dis, gc, col.pixel) == 0) | ||
118 | - printf("Failed to set foreground\n"); | ||
119 | - if(XDrawPoint(dis,win,gc,i%width,i/height) == 0) | ||
120 | - printf("Failed to draw point\n"); | ||
121 | - buffer[0] = getc(home); | ||
122 | - buffer[1] = getc(home); | ||
123 | - buffer[2] = getc(home); | ||
124 | - printf("X %03d Y %03d R %02x G %02x B %02x\n", i%width, i/height, buffer[0], buffer[1], buffer[2]); | ||
125 | - i++; | ||
126 | - } | ||
127 | - } | 172 | + int i; |
173 | + for(i=0; i<76800; i++) | ||
174 | + { | ||
175 | + col.red = buffer[2] * 257; | ||
176 | + col.green = buffer[1] * 257; | ||
177 | + col.blue = buffer[0] * 257; | ||
178 | + | ||
179 | + if(XAllocColor(display, screen_colormap, &col) == 0) | ||
180 | + printf("Failed to allocate color\n"); | ||
181 | + if(XSetForeground(display, gc, col.pixel) == 0) | ||
182 | + printf("Failed to set foreground\n"); | ||
183 | + if(XDrawPoint(display,win,gc,i%width,height-1-(i/width)) == 0) | ||
184 | + printf("Failed to draw point\n"); | ||
185 | + buffer[0] = getc(home); | ||
186 | + buffer[1] = getc(home); | ||
187 | + buffer[2] = getc(home); | ||
188 | + //printf("X %03d Y %03d R %02x G %02x B %02x\n", i%width, i/width, buffer[0], buffer[1], buffer[2]); | ||
128 | } | 189 | } |
129 | - printf("End of display\n"); | ||
130 | - return 0; | ||
131 | -} | ||
132 | 190 | ||
133 | -void init_x() { | ||
134 | - unsigned long black,white; | ||
135 | - | ||
136 | - dis=XOpenDisplay((char *)0); | ||
137 | - screen=DefaultScreen(dis); | ||
138 | - black=BlackPixel(dis,screen), | ||
139 | - white=WhitePixel(dis, screen); | ||
140 | - win=XCreateSimpleWindow(dis,DefaultRootWindow(dis),0,0,width,height, 0,black, white); | ||
141 | - XSetStandardProperties(dis,win,"Howdy","Hi",None,NULL,0,NULL); | ||
142 | - XSelectInput(dis, win, ExposureMask|ButtonPressMask|KeyPressMask); | ||
143 | - gc=XCreateGC(dis, win, 0,0); | ||
144 | - XSetBackground(dis,gc,white); | ||
145 | - XSetForeground(dis,gc,black); | ||
146 | - XClearWindow(dis, win); | ||
147 | - XMapRaised(dis, win); | ||
148 | -}; | ||
149 | - | ||
150 | -void close_x() { | ||
151 | - XFreeGC(dis, gc); | ||
152 | - XDestroyWindow(dis,win); | ||
153 | - XCloseDisplay(dis); | ||
154 | - exit(1); | ||
155 | -}; | ||
156 | - | ||
157 | -void redraw() { | ||
158 | - XClearWindow(dis, win); | ||
159 | -}; | 191 | + |
192 | + | ||
193 | + | ||
194 | + | ||
195 | + /* draw two intersecting lines, one horizontal and one vertical, */ | ||
196 | + /* which intersect at point "50,100". */ | ||
197 | + /* draw the line in a brown color. */ | ||
198 | + //XSetForeground(display, gc, brown.pixel); | ||
199 | + //XDrawLine(display, win, gc, 50, 0, 50, 200); | ||
200 | + //XDrawLine(display, win, gc, 0, 100, 200, 100); | ||
201 | + | ||
202 | + /* flush all pending requests to the X server. */ | ||
203 | + XFlush(display); | ||
204 | + | ||
205 | + /* make a delay for a short period. */ | ||
206 | + sleep(4); | ||
207 | + | ||
208 | + /* close the connection to the X server. */ | ||
209 | + XCloseDisplay(display); | ||
210 | + return(0); | ||
211 | +} |