rnd-20001204-1-src
[rocksndiamonds.git] / src / libgame / x11.c
1 /***********************************************************
2 * Artsoft Retro-Game Library                               *
3 *----------------------------------------------------------*
4 * (c) 1994-2000 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 "misc.h"
16
17
18 #if defined(TARGET_X11)
19
20 static void X11InitDisplay();
21 static DrawWindow X11InitWindow();
22
23 inline void X11InitVideoDisplay(void)
24 {
25   /* initialize X11 video */
26   X11InitDisplay();
27
28   /* set default X11 depth */
29   video.default_depth = XDefaultDepth(display, screen);
30 }
31
32 inline void X11InitVideoBuffer(DrawBuffer *backbuffer, DrawWindow *window)
33 {
34   *window = X11InitWindow();
35
36   XMapWindow(display, (*window)->drawable);
37   FlushDisplay();
38
39   /* create additional (off-screen) buffer for double-buffering */
40   *backbuffer = CreateBitmap(video.width, video.height, video.depth);
41 }
42
43 static void X11InitDisplay()
44 {
45 #if !defined(PLATFORM_MSDOS)
46   XVisualInfo vinfo_template, *vinfo;
47   int num_visuals;
48 #endif
49   unsigned int depth;
50
51   /* connect to X server */
52   if (!(display = XOpenDisplay(options.display_name)))
53     Error(ERR_EXIT, "cannot connect to X server %s",
54           XDisplayName(options.display_name));
55
56   screen = DefaultScreen(display);
57   visual = DefaultVisual(display, screen);
58   depth  = DefaultDepth(display, screen);
59   cmap   = DefaultColormap(display, screen);
60
61 #if !defined(PLATFORM_MSDOS)
62   /* look for good enough visual */
63   vinfo_template.screen = screen;
64   vinfo_template.class = (depth == 8 ? PseudoColor : TrueColor);
65   vinfo_template.depth = depth;
66   if ((vinfo = XGetVisualInfo(display, VisualScreenMask | VisualClassMask |
67                               VisualDepthMask, &vinfo_template, &num_visuals)))
68   {
69     visual = vinfo->visual;
70     XFree((void *)vinfo);
71   }
72
73   /* got appropriate visual? */
74   if (depth < 8)
75   {
76     printf("Sorry, displays with less than 8 bits per pixel not supported.\n");
77     exit(-1);
78   }
79   else if ((depth ==8 && visual->class != PseudoColor) ||
80            (depth > 8 && visual->class != TrueColor &&
81             visual->class != DirectColor))
82   {
83     printf("Sorry, cannot get appropriate visual.\n");
84     exit(-1);
85   }
86 #endif /* !PLATFORM_MSDOS */
87 }
88
89 static DrawWindow X11InitWindow()
90 {
91   DrawWindow new_window = CreateBitmapStruct();
92   unsigned int border_width = 4;
93   XGCValues gc_values;
94   unsigned long gc_valuemask;
95 #if !defined(PLATFORM_MSDOS)
96   XTextProperty windowName, iconName;
97   Pixmap icon_pixmap, iconmask_pixmap;
98   unsigned int icon_width, icon_height;
99   int icon_hot_x, icon_hot_y;
100   char icon_filename[256];
101   XSizeHints size_hints;
102   XWMHints wm_hints;
103   XClassHint class_hints;
104   char *window_name = program.window_title;
105   char *icon_name = program.window_title;
106   long window_event_mask;
107   Atom proto_atom = None, delete_atom = None;
108 #endif
109   int screen_width, screen_height;
110   int win_xpos, win_ypos;
111   unsigned long pen_fg = WhitePixel(display,screen);
112   unsigned long pen_bg = BlackPixel(display,screen);
113   const int width = video.width, height = video.height;
114
115 #if 0
116 #if !defined(PLATFORM_MSDOS)
117   static struct IconFileInfo icon_pic =
118   {
119     "rocks_icon.xbm",
120     "rocks_iconmask.xbm"
121   };
122 #endif
123 #endif
124
125   screen_width = XDisplayWidth(display, screen);
126   screen_height = XDisplayHeight(display, screen);
127
128   win_xpos = (screen_width - width) / 2;
129   win_ypos = (screen_height - height) / 2;
130
131   new_window->drawable = XCreateSimpleWindow(display,
132                                              RootWindow(display, screen),
133                                              win_xpos, win_ypos,
134                                              width, height, border_width,
135                                              pen_fg, pen_bg);
136
137 #if !defined(PLATFORM_MSDOS)
138   proto_atom = XInternAtom(display, "WM_PROTOCOLS", FALSE);
139   delete_atom = XInternAtom(display, "WM_DELETE_WINDOW", FALSE);
140   if ((proto_atom != None) && (delete_atom != None))
141     XChangeProperty(display, new_window->drawable, proto_atom, XA_ATOM, 32,
142                     PropModePrepend, (unsigned char *) &delete_atom, 1);
143
144 #if 0
145   sprintf(icon_filename, "%s/%s/%s",
146           options.ro_base_directory, GRAPHICS_DIRECTORY,
147           icon_pic.picture_filename);
148 #endif
149   XReadBitmapFile(display, new_window->drawable, program.x11_icon_filename,
150                   &icon_width, &icon_height,
151                   &icon_pixmap, &icon_hot_x, &icon_hot_y);
152   if (!icon_pixmap)
153     Error(ERR_EXIT, "cannot read icon bitmap file '%s'", icon_filename);
154
155 #if 0
156   sprintf(icon_filename, "%s/%s/%s",
157           options.ro_base_directory, GRAPHICS_DIRECTORY,
158           icon_pic.picturemask_filename);
159 #endif
160   XReadBitmapFile(display, new_window->drawable, program.x11_iconmask_filename,
161                   &icon_width, &icon_height,
162                   &iconmask_pixmap, &icon_hot_x, &icon_hot_y);
163   if (!iconmask_pixmap)
164     Error(ERR_EXIT, "cannot read icon bitmap file '%s'", icon_filename);
165
166   size_hints.width  = size_hints.min_width  = size_hints.max_width  = width;
167   size_hints.height = size_hints.min_height = size_hints.max_height = height;
168   size_hints.flags = PSize | PMinSize | PMaxSize;
169
170   if (win_xpos || win_ypos)
171   {
172     size_hints.x = win_xpos;
173     size_hints.y = win_ypos;
174     size_hints.flags |= PPosition;
175   }
176
177   if (!XStringListToTextProperty(&window_name, 1, &windowName))
178     Error(ERR_EXIT, "structure allocation for windowName failed");
179
180   if (!XStringListToTextProperty(&icon_name, 1, &iconName))
181     Error(ERR_EXIT, "structure allocation for iconName failed");
182
183   wm_hints.initial_state = NormalState;
184   wm_hints.input = True;
185   wm_hints.icon_pixmap = icon_pixmap;
186   wm_hints.icon_mask = iconmask_pixmap;
187   wm_hints.flags = StateHint | IconPixmapHint | IconMaskHint | InputHint;
188
189   class_hints.res_name = program.command_basename;
190   class_hints.res_class = program.program_title;
191
192   XSetWMProperties(display, new_window->drawable, &windowName, &iconName, 
193                    NULL, 0, &size_hints, &wm_hints, 
194                    &class_hints);
195
196   XFree(windowName.value);
197   XFree(iconName.value);
198
199   /* Select event types wanted */
200   window_event_mask =
201     ExposureMask | StructureNotifyMask | FocusChangeMask |
202     ButtonPressMask | ButtonReleaseMask | PointerMotionMask |
203     PointerMotionHintMask | KeyPressMask | KeyReleaseMask;
204
205   XSelectInput(display, new_window->drawable, window_event_mask);
206 #endif
207
208   /* create GC for drawing with window depth and background color (black) */
209   gc_values.graphics_exposures = False;
210   gc_values.foreground = pen_bg;
211   gc_values.background = pen_bg;
212   gc_valuemask = GCGraphicsExposures | GCForeground | GCBackground;
213   new_window->gc =
214     XCreateGC(display, new_window->drawable, gc_valuemask, &gc_values);
215
216   return new_window;
217 }
218
219 #endif /* TARGET_X11 */