rnd-20020803-1-src
[rocksndiamonds.git] / src / libgame / x11.c
1 /***********************************************************
2 * Artsoft Retro-Game Library                               *
3 *----------------------------------------------------------*
4 * (c) 1994-2002 Artsoft Entertainment                      *
5 *               Holger Schemel                             *
6 *               Detmolder Strasse 189                      *
7 *               33604 Bielefeld                            *
8 *               Germany                                    *
9 *               e-mail: info@artsoft.org                   *
10 *----------------------------------------------------------*
11 * x11.c                                                    *
12 ***********************************************************/
13
14 #include "system.h"
15 #include "pcx.h"
16 #include "misc.h"
17 #include "setup.h"
18
19
20 #if defined(TARGET_X11)
21
22 static void X11InitDisplay();
23 static DrawWindow *X11InitWindow();
24
25 inline void X11InitVideoDisplay(void)
26 {
27   /* initialize X11 video */
28   X11InitDisplay();
29
30   /* set default X11 depth */
31   video.default_depth = XDefaultDepth(display, screen);
32 }
33
34 inline void X11InitVideoBuffer(DrawBuffer **backbuffer, DrawWindow **window)
35 {
36   *window = X11InitWindow();
37
38   XMapWindow(display, (*window)->drawable);
39   FlushDisplay();
40
41   /* create additional (off-screen) buffer for double-buffering */
42   *backbuffer = CreateBitmap(video.width, video.height, video.depth);
43 }
44
45 static void X11InitDisplay()
46 {
47 #if !defined(PLATFORM_MSDOS)
48   XVisualInfo vinfo_template, *vinfo;
49   int num_visuals;
50 #endif
51   unsigned int depth;
52
53   /* connect to X server */
54   if (!(display = XOpenDisplay(options.display_name)))
55     Error(ERR_EXIT, "cannot connect to X server %s",
56           XDisplayName(options.display_name));
57
58   screen = DefaultScreen(display);
59   visual = DefaultVisual(display, screen);
60   depth  = DefaultDepth(display, screen);
61   cmap   = DefaultColormap(display, screen);
62
63 #if !defined(PLATFORM_MSDOS)
64   /* look for good enough visual */
65   vinfo_template.screen = screen;
66   vinfo_template.class = (depth == 8 ? PseudoColor : TrueColor);
67   vinfo_template.depth = depth;
68   if ((vinfo = XGetVisualInfo(display, VisualScreenMask | VisualClassMask |
69                               VisualDepthMask, &vinfo_template, &num_visuals)))
70   {
71     visual = vinfo->visual;
72     XFree((void *)vinfo);
73   }
74
75   /* got appropriate visual? */
76   if (depth < 8)
77     Error(ERR_EXIT, "X11 display not supported (less than 8 bits per pixel)");
78   else if ((depth ==8 && visual->class != PseudoColor) ||
79            (depth > 8 && visual->class != TrueColor &&
80             visual->class != DirectColor))
81     Error(ERR_EXIT, "X11 display not supported (inappropriate visual)");
82 #endif /* !PLATFORM_MSDOS */
83 }
84
85 static DrawWindow *X11InitWindow()
86 {
87   DrawWindow *new_window = CreateBitmapStruct();
88   unsigned int border_width = 4;
89   XGCValues gc_values;
90   unsigned long gc_valuemask;
91 #if !defined(PLATFORM_MSDOS)
92   XTextProperty windowName, iconName;
93   Pixmap icon_pixmap, iconmask_pixmap;
94   unsigned int icon_width, icon_height;
95   int icon_hot_x, icon_hot_y;
96 #if 0
97   char icon_filename[256];
98 #endif
99   XSizeHints size_hints;
100   XWMHints wm_hints;
101   XClassHint class_hints;
102   char *window_name = program.window_title;
103   char *icon_name = program.window_title;
104   long window_event_mask;
105   Atom proto_atom = None, delete_atom = None;
106 #endif
107   int screen_width, screen_height;
108   int win_xpos, win_ypos;
109   unsigned long pen_fg = WhitePixel(display,screen);
110   unsigned long pen_bg = BlackPixel(display,screen);
111   const int width = video.width, height = video.height;
112   int i;
113
114 #if 0
115 #if !defined(PLATFORM_MSDOS)
116   static struct IconFileInfo icon_pic =
117   {
118     "rocks_icon.xbm",
119     "rocks_iconmask.xbm"
120   };
121 #endif
122 #endif
123
124   screen_width = XDisplayWidth(display, screen);
125   screen_height = XDisplayHeight(display, screen);
126
127   win_xpos = (screen_width - width) / 2;
128   win_ypos = (screen_height - height) / 2;
129
130   new_window->drawable = XCreateSimpleWindow(display,
131                                              RootWindow(display, screen),
132                                              win_xpos, win_ypos,
133                                              width, height, border_width,
134                                              pen_fg, pen_bg);
135
136 #if !defined(PLATFORM_MSDOS)
137   proto_atom = XInternAtom(display, "WM_PROTOCOLS", FALSE);
138   delete_atom = XInternAtom(display, "WM_DELETE_WINDOW", FALSE);
139   if ((proto_atom != None) && (delete_atom != None))
140     XChangeProperty(display, new_window->drawable, proto_atom, XA_ATOM, 32,
141                     PropModePrepend, (unsigned char *) &delete_atom, 1);
142
143 #if 0
144   sprintf(icon_filename, "%s/%s", options.graphics_directory,
145           icon_pic.picture_filename);
146 #endif
147   if (XReadBitmapFile(display, new_window->drawable,
148                       getCustomImageFilename(program.x11_icon_filename),
149                       &icon_width, &icon_height, &icon_pixmap,
150                       &icon_hot_x, &icon_hot_y) != BitmapSuccess)
151     Error(ERR_EXIT, "cannot read icon bitmap file '%s'",
152           program.x11_icon_filename);
153
154 #if 0
155   sprintf(icon_filename, "%s/%s", options.graphics_directory,
156           icon_pic.picturemask_filename);
157 #endif
158   if (XReadBitmapFile(display, new_window->drawable,
159                       getCustomImageFilename(program.x11_iconmask_filename),
160                       &icon_width, &icon_height, &iconmask_pixmap,
161                       &icon_hot_x, &icon_hot_y) != BitmapSuccess)
162     Error(ERR_EXIT, "cannot read icon bitmap file '%s'",
163           program.x11_iconmask_filename);
164
165   size_hints.width  = size_hints.min_width  = size_hints.max_width  = width;
166   size_hints.height = size_hints.min_height = size_hints.max_height = height;
167   size_hints.flags = PSize | PMinSize | PMaxSize;
168
169   if (win_xpos || win_ypos)
170   {
171     size_hints.x = win_xpos;
172     size_hints.y = win_ypos;
173     size_hints.flags |= PPosition;
174   }
175
176   if (!XStringListToTextProperty(&window_name, 1, &windowName))
177     Error(ERR_EXIT, "structure allocation for windowName failed");
178
179   if (!XStringListToTextProperty(&icon_name, 1, &iconName))
180     Error(ERR_EXIT, "structure allocation for iconName failed");
181
182   wm_hints.initial_state = NormalState;
183   wm_hints.input = True;
184   wm_hints.icon_pixmap = icon_pixmap;
185   wm_hints.icon_mask = iconmask_pixmap;
186   wm_hints.flags = StateHint | IconPixmapHint | IconMaskHint | InputHint;
187
188   class_hints.res_name = program.command_basename;
189   class_hints.res_class = program.program_title;
190
191   XSetWMProperties(display, new_window->drawable, &windowName, &iconName, 
192                    NULL, 0, &size_hints, &wm_hints, 
193                    &class_hints);
194
195   XFree(windowName.value);
196   XFree(iconName.value);
197
198   /* Select event types wanted */
199   window_event_mask =
200     ExposureMask | StructureNotifyMask | FocusChangeMask |
201     ButtonPressMask | ButtonReleaseMask | PointerMotionMask |
202     PointerMotionHintMask | KeyPressMask | KeyReleaseMask;
203
204   XSelectInput(display, new_window->drawable, window_event_mask);
205 #endif
206
207   /* create GC for drawing with window depth and background color (black) */
208   gc_values.graphics_exposures = False;
209   gc_values.foreground = pen_bg;
210   gc_values.background = pen_bg;
211   gc_valuemask = GCGraphicsExposures | GCForeground | GCBackground;
212   new_window->gc =
213     XCreateGC(display, new_window->drawable, gc_valuemask, &gc_values);
214
215   /* create GCs for line drawing (black and white) */
216   for(i=0; i<2; i++)
217   {
218     gc_values.graphics_exposures = False;
219     gc_values.foreground = (i ? pen_fg : pen_bg);
220     gc_values.background = pen_bg;
221     gc_values.line_width = 4;
222     gc_values.line_style = LineSolid;
223     gc_values.cap_style = CapRound;
224     gc_values.join_style = JoinRound;
225
226     gc_valuemask = GCGraphicsExposures | GCForeground | GCBackground |
227                    GCLineWidth | GCLineStyle | GCCapStyle | GCJoinStyle;
228     new_window->line_gc[i] =
229       XCreateGC(display, new_window->drawable, gc_valuemask, &gc_values);
230   }
231
232   return new_window;
233 }
234
235 static void SetImageDimensions(Bitmap *bitmap)
236 {
237 #if defined(TARGET_ALLEGRO)
238   BITMAP *allegro_bitmap = (BITMAP *)(bitmap->drawable);
239
240   bitmap->width  = allegro_bitmap->w;
241   bitmap->height = allegro_bitmap->h;
242 #else
243   Window root;
244   int x, y;
245   unsigned int border_width, depth;
246
247   XGetGeometry(display, bitmap->drawable, &root, &x, &y,
248                &bitmap->width, &bitmap->height, &border_width, &depth);
249 #endif
250 }
251
252 Bitmap *X11LoadImage(char *filename)
253 {
254   Bitmap *new_bitmap = CreateBitmapStruct();
255   char *error = "Read_PCX_to_Pixmap(): %s '%s'";
256   int pcx_err;
257
258   pcx_err = Read_PCX_to_Pixmap(display, window->drawable, window->gc, filename,
259                                &new_bitmap->drawable, &new_bitmap->clip_mask);
260   switch(pcx_err)
261   {
262     case PCX_Success:
263       break;
264     case PCX_OpenFailed:
265       SetError(error, "cannot open PCX file", filename);
266       return NULL;
267     case PCX_ReadFailed:
268       SetError(error, "cannot read PCX file", filename);
269       return NULL;
270     case PCX_FileInvalid:
271       SetError(error, "invalid PCX file", filename);
272       return NULL;
273     case PCX_NoMemory:
274       SetError(error, "not enough memory for PCX file", filename);
275       return NULL;
276     case PCX_ColorFailed:
277       SetError(error, "cannot get colors for PCX file", filename);
278       return NULL;
279     case PCX_OtherError:
280       /* this should already have called SetError() */
281       return NULL;
282     default:
283       SetError(error, "unknown error reading PCX file", filename);
284       return NULL;
285   }
286
287   if (!new_bitmap->drawable)
288   {
289     SetError("X11LoadImage(): cannot get graphics for '%s'", filename);
290     return NULL;
291   }
292
293   if (!new_bitmap->clip_mask)
294   {
295     SetError("X11LoadImage(): cannot get clipmask for '%s'", filename);
296     return NULL;
297   }
298
299   /* set GraphicContext inheritated from Window */
300   new_bitmap->gc = window->gc;
301
302   /* set image width and height */
303   SetImageDimensions(new_bitmap);
304
305   return new_bitmap;
306 }
307
308 #endif /* TARGET_X11 */