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