main.c
3.86 KB
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
157
158
159
160
/*
Remember to compile try:
1) gcc hi.c -o hi -lX11
2) gcc hi.c -I /usr/include/X11 -L /usr/X11/lib -lX11
3) gcc hi.c -I /where/ever -L /who/knows/where -l X11
Brian Hammond 2/9/96. Feel free to do with this as you will!
*/
/* include the X library headers */
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xos.h>
/* include some silly stuff */
#include <stdio.h>
#include <stdlib.h>
/* here are our X variables */
Display *dis;
int screen;
Window win;
GC gc;
int width=320;
int height=240;
/* here are our X routines declared! */
void init_x();
void close_x();
void redraw();
main () {
XEvent event; /* the XEvent declaration !!! */
KeySym key; /* a dealie-bob to handle KeyPress Events */
char text[255]; /* a char buffer for KeyPress Events */
Pixmap pmap;
XColor col;
init_x();
printf("Patte de canard\n");
Colormap cmap = DefaultColormap(dis, DefaultScreen(dis));
printf("?\n");
pmap = XCreatePixmap(dis, win, width, height, DefaultDepth(dis, DefaultScreen(dis)));
FILE* home = NULL;
char buffer[4];
home = fopen("home.bmp", "r");
if(home == NULL)
return 0;
fseek(home, 0x0A, SEEK_SET);
buffer[0] = getc(home);
buffer[1] = getc(home);
buffer[2] = getc(home);
buffer[3] = getc(home);
printf("Pixel array offset %02x %02x %02x %02x\n", buffer[0], buffer[1], buffer[2], buffer[3]);
fseek(home, 0x12, SEEK_SET);
buffer[0] = getc(home);
buffer[1] = getc(home);
buffer[2] = getc(home);
buffer[3] = getc(home);
printf("Width %02x %02x %02x %02x\n", buffer[0], buffer[1], buffer[2], buffer[3]);
//fseek(home, 0x12, SEEK_SET);
buffer[0] = getc(home);
buffer[1] = getc(home);
buffer[2] = getc(home);
buffer[3] = getc(home);
printf("Height %02x %02x %02x %02x\n", buffer[0], buffer[1], buffer[2], buffer[3]);
fseek(home, 0x1C, SEEK_SET);
buffer[0] = getc(home);
buffer[1] = getc(home);
printf("Bits Per Pixel %02x %02x\n", buffer[0], buffer[1]);
if(buffer[1] != 0 || buffer[0] != 0x18)
{
printf("Could not read BMP\n");
return 1;
}
while(1);
/* look for events forever... */
while(1) {
/* get the next event and stuff it into our event variable.
Note: only events we set the mask for are detected!
*/
XNextEvent(dis, &event);
if (event.type==Expose && event.xexpose.count==0) {
/* the window was exposed redraw it! */
redraw();
}
if (event.type==KeyPress&&
XLookupString(&event.xkey,text,255,&key,0)==1) {
/* use the XLookupString routine to convert the invent
KeyPress data into regular text. Weird but necessary...
*/
if (text[0]=='q') {
close_x();
}
printf("You pressed the %c key!\n",text[0]);
}
if (event.type==ButtonPress) {
/* tell where the mouse Button was Pressed */
int x=event.xbutton.x,
y=event.xbutton.y;
//strcpy(text,"X is FUN!");
//XSetForeground(dis,gc,rand()%event.xbutton.x%255);
//XDrawString(dis,win,gc,x,y, text, strlen(text));
col.red = 128;
col.green = 128;
col.blue = 0;
XAllocColor(dis, cmap, &col);
XSetForeground(dis, gc, col.pixel);
XDrawPoint(dis,win,gc,x,y);
}
}
}
void init_x() {
/* get the colors black and white (see section for details) */
unsigned long black,white;
dis=XOpenDisplay((char *)0);
screen=DefaultScreen(dis);
black=BlackPixel(dis,screen),
white=WhitePixel(dis, screen);
win=XCreateSimpleWindow(dis,DefaultRootWindow(dis),0,0,height,width, 0,black, white);
XSetStandardProperties(dis,win,"Howdy","Hi",None,NULL,0,NULL);
XSelectInput(dis, win, ExposureMask|ButtonPressMask|KeyPressMask);
gc=XCreateGC(dis, win, 0,0);
XSetBackground(dis,gc,white);
XSetForeground(dis,gc,black);
XClearWindow(dis, win);
XMapRaised(dis, win);
};
void close_x() {
XFreeGC(dis, gc);
XDestroyWindow(dis,win);
XCloseDisplay(dis);
exit(1);
};
void redraw() {
XClearWindow(dis, win);
};