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