rnd-20001203-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 static void X11InitDisplay();
20 static DrawWindow X11InitWindow();
21
22 inline void X11InitVideoDisplay(void)
23 {
24   /* initialize X11 video */
25   X11InitDisplay();
26
27   /* set default X11 depth */
28   video.default_depth = XDefaultDepth(display, screen);
29 }
30
31 inline void X11InitVideoBuffer(DrawBuffer *backbuffer, DrawWindow *window)
32 {
33   *window = X11InitWindow();
34
35   XMapWindow(display, (*window)->drawable);
36   FlushDisplay();
37
38   /* create additional (off-screen) buffer for double-buffering */
39   *backbuffer = CreateBitmap(video.width, video.height, video.depth);
40 }
41
42 static void X11InitDisplay()
43 {
44 #if !defined(PLATFORM_MSDOS)
45   XVisualInfo vinfo_template, *vinfo;
46   int num_visuals;
47 #endif
48   unsigned int depth;
49
50   /* connect to X server */
51   if (!(display = XOpenDisplay(options.display_name)))
52     Error(ERR_EXIT, "cannot connect to X server %s",
53           XDisplayName(options.display_name));
54
55   screen = DefaultScreen(display);
56   visual = DefaultVisual(display, screen);
57   depth  = DefaultDepth(display, screen);
58   cmap   = DefaultColormap(display, screen);
59
60 #if !defined(PLATFORM_MSDOS)
61   /* look for good enough visual */
62   vinfo_template.screen = screen;
63   vinfo_template.class = (depth == 8 ? PseudoColor : TrueColor);
64   vinfo_template.depth = depth;
65   if ((vinfo = XGetVisualInfo(display, VisualScreenMask | VisualClassMask |
66                               VisualDepthMask, &vinfo_template, &num_visuals)))
67   {
68     visual = vinfo->visual;
69     XFree((void *)vinfo);
70   }
71
72   /* got appropriate visual? */
73   if (depth < 8)
74   {
75     printf("Sorry, displays with less than 8 bits per pixel not supported.\n");
76     exit(-1);
77   }
78   else if ((depth ==8 && visual->class != PseudoColor) ||
79            (depth > 8 && visual->class != TrueColor &&
80             visual->class != DirectColor))
81   {
82     printf("Sorry, cannot get appropriate visual.\n");
83     exit(-1);
84   }
85 #endif /* !PLATFORM_MSDOS */
86 }
87
88 static DrawWindow X11InitWindow()
89 {
90   DrawWindow new_window = CreateBitmapStruct();
91   unsigned int border_width = 4;
92   XGCValues gc_values;
93   unsigned long gc_valuemask;
94 #if !defined(PLATFORM_MSDOS)
95   XTextProperty windowName, iconName;
96   Pixmap icon_pixmap, iconmask_pixmap;
97   unsigned int icon_width, icon_height;
98   int icon_hot_x, icon_hot_y;
99   char icon_filename[256];
100   XSizeHints size_hints;
101   XWMHints wm_hints;
102   XClassHint class_hints;
103   char *window_name = program.window_title;
104   char *icon_name = program.window_title;
105   long window_event_mask;
106   Atom proto_atom = None, delete_atom = None;
107 #endif
108   int screen_width, screen_height;
109   int win_xpos, win_ypos;
110   unsigned long pen_fg = WhitePixel(display,screen);
111   unsigned long pen_bg = BlackPixel(display,screen);
112   const int width = video.width, height = video.height;
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/%s",
145           options.ro_base_directory, GRAPHICS_DIRECTORY,
146           icon_pic.picture_filename);
147 #endif
148   XReadBitmapFile(display, new_window->drawable, program.x11_icon_filename,
149                   &icon_width, &icon_height,
150                   &icon_pixmap, &icon_hot_x, &icon_hot_y);
151   if (!icon_pixmap)
152     Error(ERR_EXIT, "cannot read icon bitmap file '%s'", icon_filename);
153
154 #if 0
155   sprintf(icon_filename, "%s/%s/%s",
156           options.ro_base_directory, GRAPHICS_DIRECTORY,
157           icon_pic.picturemask_filename);
158 #endif
159   XReadBitmapFile(display, new_window->drawable, program.x11_iconmask_filename,
160                   &icon_width, &icon_height,
161                   &iconmask_pixmap, &icon_hot_x, &icon_hot_y);
162   if (!iconmask_pixmap)
163     Error(ERR_EXIT, "cannot read icon bitmap file '%s'", icon_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_name;
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   return new_window;
216 }
217
218 #endif /* TARGET_X11 */