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