11 #include <X11/Xutil.h>
31 #define BORDER_WIDTH 4
37 char * x_display_name
;
43 int FONT_X_SIZE
,FONT_Y_SIZE
;
47 Atom x_delete_window_atom
,x_wm_protocols_atom
;
49 char * x_color_name
[16]={
70 int x_current_x
,x_current_y
;
71 int x_current_color
=0;
72 int x_current_bgcolor
=0;
73 int x_width
=DEFAULT_X_SIZE
,x_height
=DEFAULT_Y_SIZE
;
74 int display_height
,display_width
;
80 Pixmap icon_pixmap
,icon_mask
;
89 /* initialize console */
90 void c_init(int w
,int h
)
93 XSizeHints size_hints
;
99 char *fontname
=x_font_name
?x_font_name
:DEFAULT_FONT_NAME
;
103 if (!x_display_name
) x_display_name
= getenv("DISPLAY");
105 /* initiate connection with the X server */
106 display
=XOpenDisplay(x_display_name
);
107 if (!display
){fprintf(stderr
,"Error: Can't open display %s\n",x_display_name
);exit(1);}
109 x_screen
=DefaultScreen(display
);
111 display_height
=DisplayHeight(display
,x_screen
);
112 display_width
=DisplayWidth(display
,x_screen
);
115 x_font
=XLoadQueryFont(display
,fontname
);
116 if (!x_font
){fprintf(stderr
,"Error: Can't load font \"%s\".\n",fontname
);XCloseDisplay(display
);exit(1);}
117 FONT_X_SIZE
=x_font
->max_bounds
.width
;
118 FONT_Y_SIZE
=x_font
->max_bounds
.ascent
+x_font
->max_bounds
.descent
;
120 x_delete_window_atom
=XInternAtom(display
,"WM_DELETE_WINDOW", False
);
121 x_wm_protocols_atom
=XInternAtom(display
,"WM_PROTOCOLS", False
);
123 /* create window and set properties */
124 window
=XCreateSimpleWindow(display
,RootWindow(display
,x_screen
),0,0,x_width
*FONT_X_SIZE
,x_height
*FONT_Y_SIZE
,BORDER_WIDTH
,BlackPixel(display
,x_screen
),BlackPixel(display
,x_screen
));
125 if (!window
){fprintf(stderr
,"Error: Can't create window.\n");XCloseDisplay(display
);exit(1);}
126 XStoreName(display
,window
,"0verkill");
131 window2
=XCreateSimpleWindow(display
,RootWindow(display
,x_screen
),0,0,x_width
*FONT_X_SIZE
,x_height
*FONT_Y_SIZE
,BORDER_WIDTH
,BlackPixel(display
,x_screen
),BlackPixel(display
,x_screen
));
132 if (!window2
){fprintf(stderr
,"Error: Can't create window.\n");XCloseDisplay(display
);exit(1);}
133 XStoreName(display
,window2
,"0verkill 3D");
137 size_hints
.min_width
=X_MIN_WIDTH
*FONT_X_SIZE
;
138 size_hints
.min_height
=X_MIN_HEIGHT
*FONT_Y_SIZE
;
139 size_hints
.width_inc
=FONT_X_SIZE
;
140 size_hints
.height_inc
=FONT_Y_SIZE
;
141 size_hints
.flags
=PMinSize
|PResizeInc
;
143 XSetNormalHints(display
,window
,&size_hints
);
147 XSetNormalHints(display
,window2
,&size_hints
);
151 XCreatePixmapFromData(display
,window
,icon_xpm
,&icon_pixmap
,&icon_mask
,0);
154 XCreatePixmapFromData(display
,window2
,icon_xpm
,&icon_pixmap
,&icon_mask
,0);
156 wm_hints
.flags
=IconPixmapHint
|IconMaskHint
;
157 wm_hints
.icon_pixmap
=icon_pixmap
;
158 wm_hints
.icon_mask
=icon_mask
;
160 XSetWMHints(display
,window
,&wm_hints
);
163 XSetWMHints(display
,window2
,&wm_hints
);
167 XSetWMProtocols(display
,window
,&x_delete_window_atom
,1);
168 XSelectInput(display
,window
,ExposureMask
|KeyPressMask
|KeyReleaseMask
|StructureNotifyMask
);
173 XSetWMProtocols(display
,window2
,&x_delete_window_atom
,1);
174 XSelectInput(display
,window2
,ExposureMask
|KeyPressMask
|KeyReleaseMask
|StructureNotifyMask
);
178 /* create 16 graphic contexts - one for each color */
179 gcv
.font
=x_font
->fid
;
180 gcv
.foreground
=BlackPixel(display
,x_screen
);
181 gcv
.background
=BlackPixel(display
,x_screen
);
182 gc
=XCreateGC(display
,window
,GCFont
+GCBackground
,&gcv
);
184 XAllocNamedColor(display
,DefaultColormap(display
,x_screen
),x_color_name
[a
],x_color
+a
,&dummy
);
188 XMapWindow(display
,window
);
192 XMapWindow(display
,window2
);
197 /* initialize keyboard */
205 void c_shutdown(void)
208 XDestroyWindow(display
,window
);
211 XDestroyWindow(display
,window2
);
213 XFreeFont(display
,x_font
);
215 XFreePixmap(display
,icon_pixmap
);
216 XFreePixmap(display
,icon_mask
);
218 XCloseDisplay(display
);
223 /* move cursor to [x,y] */
224 void c_goto(int x
,int y
)
231 /* set foreground color */
232 void c_setcolor(unsigned char a
)
234 x_current_color
=(a
&15);
238 /* set foreground and background color */
239 void c_setcolor_bg(unsigned char a
,unsigned char b
)
241 x_current_color
=(a
&15);
242 x_current_bgcolor
=(b
&7);
246 /* set background color */
247 void c_setbgcolor(unsigned char a
)
249 x_current_bgcolor
=a
&7;
253 /* set highlight color and background */
254 void c_sethlt_bg(unsigned char a
,unsigned char b
)
256 x_current_color
=(x_current_color
&7)|(!!a
)<<3;
257 x_current_bgcolor
=b
&7;
261 /* set highlight color */
262 void c_sethlt(unsigned char a
)
264 x_current_color
=(x_current_color
&7)|(!!a
)<<3;
268 /* set 3 bit foreground color and background color */
269 void c_setcolor_3b_bg(unsigned char a
,unsigned char b
)
271 x_current_color
=(x_current_color
&8)|(a
&7);
272 x_current_bgcolor
=b
&7;
276 /* set 3 bit foreground color */
277 void c_setcolor_3b(unsigned char a
)
279 x_current_color
=(x_current_color
&8)|(a
&7);
282 /* print on the cursor position */
286 void c_print_l(char *text
, int l
)
288 XSetForeground(display
,gc
,(x_color
[x_current_color
]).pixel
);
289 XSetBackground(display
,gc
,(x_color
[x_current_bgcolor
]).pixel
);
293 (TRI_D_ON
&&tri_d
)?window2
:window
,
298 x_current_x
*FONT_X_SIZE
,
299 (x_current_y
+1)*FONT_Y_SIZE
,
309 void c_print(char *text
)
311 c_print_l(text
, strlen(text
));
314 /* print char on the cursor position */
322 /* clear the screen */
325 XClearWindow(display
,window
);
328 XClearWindow(display
,window2
);
333 /* clear rectangle on the screen */
334 /* presumtions: x2>=x1 && y2>=y1 */
335 void c_clear(int x1
,int y1
,int x2
,int y2
)
340 XClearArea(display
,window
,x1
*FONT_X_SIZE
,y1
*FONT_Y_SIZE
,w
*FONT_X_SIZE
,h
*FONT_Y_SIZE
,False
);
343 XClearArea(display
,window2
,x1
*FONT_X_SIZE
,y1
*FONT_Y_SIZE
,w
*FONT_X_SIZE
,h
*FONT_Y_SIZE
,False
);
348 void c_update_kbd(void)
356 return kbd_is_pressed(k
);
360 int c_was_pressed(int k
)
362 return kbd_was_pressed(k
);
366 void c_wait_for_key(void)
372 /* set cursor shape */
373 void c_cursor(int type
)
386 /* get screen dimensions */
387 void c_get_size(int *x
, int *y
)