comparison src/video/x11/SDL_x11window.c @ 1951:7177581dc9fa

Initial work on X11 window code in.
author Sam Lantinga <slouken@libsdl.org>
date Thu, 27 Jul 2006 06:53:23 +0000
parents
children 420716272158
comparison
equal deleted inserted replaced
1950:a344e42bce3b 1951:7177581dc9fa
1 /*
2 SDL - Simple DirectMedia Layer
3 Copyright (C) 1997-2006 Sam Lantinga
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18
19 Sam Lantinga
20 slouken@libsdl.org
21 */
22 #include "SDL_config.h"
23
24 #include "SDL_syswm.h"
25 #include "../SDL_sysvideo.h"
26 #include "../../events/SDL_keyboard_c.h"
27
28 #include "SDL_x11video.h"
29
30
31 static int
32 SetupWindowData(_THIS, SDL_Window * window, Window w, BOOL created)
33 {
34 SDL_VideoData *videodata = (SDL_VideoData *) _this->driverdata;
35 SDL_WindowData *data;
36 int numwindows = videodata->numwindows;
37 SDL_WindowData **windowlist = videodata->windowlist;
38
39 /* Allocate the window data */
40 data = (SDL_WindowData *) SDL_malloc(sizeof(*data));
41 if (!data) {
42 SDL_OutOfMemory();
43 return -1;
44 }
45 data->windowID = window->id;
46 data->window = w;
47 #ifdef X_HAVE_UTF8_STRING
48 if (SDL_X11_HAVE_UTF8) {
49 data->ic =
50 pXCreateIC(videodata->im, XNClientWindow, w, XNFocusWindow, w,
51 XNInputStyle, XIMPreeditNothing | XIMStatusNothing,
52 XNResourceName, videodata->classname, XNResourceClass,
53 videodata->classname, NULL);
54 }
55 #endif
56 data->created = created;
57 data->videodata = videodata;
58
59 /* Associate the data with the window */
60 windowlist =
61 (SDL_WindowData **) SDL_realloc(windowlist,
62 (numwindows +
63 1) * sizeof(*windowlist));
64 if (!windowlist) {
65 SDL_OutOfMemory();
66 SDL_free(data);
67 return -1;
68 }
69 windowlist[numwindows++] = data;
70 videodata->numwindows = numwindows;
71 videodata->windowlist = windowlist;
72
73 /* Fill in the SDL window with the window data */
74 {
75 XWindowAttributes attrib;
76
77 XGetWindowAttributes(data->videodata->display, w, &attrib);
78 window->x = attrib.x;
79 window->y = attrib.y;
80 window->w = attrib.width;
81 window->h = attrib.height;
82 if (attrib.map_state != IsUnmapped) {
83 window->flags |= SDL_WINDOW_SHOWN;
84 } else {
85 window->flags &= ~SDL_WINDOW_SHOWN;
86 }
87 }
88 /* FIXME: How can I tell?
89 {
90 DWORD style = GetWindowLong(hwnd, GWL_STYLE);
91 if (style & WS_VISIBLE) {
92 if (style & (WS_BORDER | WS_THICKFRAME)) {
93 window->flags &= ~SDL_WINDOW_BORDERLESS;
94 } else {
95 window->flags |= SDL_WINDOW_BORDERLESS;
96 }
97 if (style & WS_THICKFRAME) {
98 window->flags |= SDL_WINDOW_RESIZABLE;
99 } else {
100 window->flags &= ~SDL_WINDOW_RESIZABLE;
101 }
102 if (style & WS_MAXIMIZE) {
103 window->flags |= SDL_WINDOW_MAXIMIZED;
104 } else {
105 window->flags &= ~SDL_WINDOW_MAXIMIZED;
106 }
107 if (style & WS_MINIMIZE) {
108 window->flags |= SDL_WINDOW_MINIMIZED;
109 } else {
110 window->flags &= ~SDL_WINDOW_MINIMIZED;
111 }
112 }
113 if (GetFocus() == hwnd) {
114 int index = data->videodata->keyboard;
115 window->flags |= SDL_WINDOW_INPUT_FOCUS;
116 SDL_SetKeyboardFocus(index, data->windowID);
117
118 if (window->flags & SDL_WINDOW_INPUT_GRABBED) {
119 RECT rect;
120 GetClientRect(hwnd, &rect);
121 ClientToScreen(hwnd, (LPPOINT) & rect);
122 ClientToScreen(hwnd, (LPPOINT) & rect + 1);
123 ClipCursor(&rect);
124 }
125 }
126 */
127
128 /* All done! */
129 window->driverdata = data;
130 return 0;
131 }
132
133 int
134 X11_CreateWindow(_THIS, SDL_Window * window)
135 {
136 SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
137 SDL_DisplayData *displaydata =
138 (SDL_DisplayData *) SDL_CurrentDisplay.driverdata;
139 Visual *visual;
140 int depth;
141 XSetWindowAttributes xattr;
142 int x, y;
143 Window w;
144 XSizeHints *sizehints;
145 XWMHints *wmhints;
146 XClassHint *classhints;
147
148 #if SDL_VIDEO_DRIVER_X11_XINERAMA
149 /* FIXME
150 if ( use_xinerama ) {
151 x = xinerama_info.x_org;
152 y = xinerama_info.y_org;
153 }
154 */
155 #endif
156 if (window->flags & SDL_WINDOW_OPENGL) {
157 /* FIXME: get the glx visual */
158 } else {
159 visual = displaydata->visual;
160 depth = displaydata->depth;
161 }
162
163 if (window->flags & SDL_WINDOW_FULLSCREEN) {
164 xattr.override_redirect = True;
165 } else {
166 xattr.override_redirect = False;
167 }
168 xattr.background_pixel = 0;
169 xattr.border_pixel = 0;
170 if (visual->class == PseudoColor || visual->class == DirectColor) {
171 xattr.colormap =
172 XCreateColormap(data->display,
173 RootWindow(data->display, displaydata->screen),
174 visual, AllocAll);
175 } else {
176 xattr.colormap =
177 XCreateColormap(data->display,
178 RootWindow(data->display, displaydata->screen),
179 visual, AllocNone);
180 }
181
182 if ((window->flags & SDL_WINDOW_FULLSCREEN) ||
183 window->x == SDL_WINDOWPOS_CENTERED) {
184 x = (DisplayWidth(data->display, displaydata->screen) -
185 window->w) / 2;
186 } else if (window->x == SDL_WINDOWPOS_UNDEFINED) {
187 x = 0;
188 } else {
189 x = window->x;
190 }
191 if ((window->flags & SDL_WINDOW_FULLSCREEN) ||
192 window->y == SDL_WINDOWPOS_CENTERED) {
193 y = (DisplayHeight(data->display, displaydata->screen) -
194 window->h) / 2;
195 } else if (window->y == SDL_WINDOWPOS_UNDEFINED) {
196 y = 0;
197 } else {
198 y = window->y;
199 }
200
201 w = XCreateWindow(data->display,
202 RootWindow(data->display, displaydata->screen), x, y,
203 window->w, window->h, 0, depth, InputOutput, visual,
204 (CWOverrideRedirect | CWBackPixel | CWBorderPixel |
205 CWColormap), &xattr);
206
207 sizehints = XAllocSizeHints();
208 if (sizehints) {
209 if (window->flags & SDL_WINDOW_RESIZABLE) {
210 sizehints->min_width = 32;
211 sizehints->min_height = 32;
212 sizehints->max_height = 4096;
213 sizehints->max_width = 4096;
214 } else {
215 sizehints->min_width = sizehints->max_width = window->w;
216 sizehints->min_height = sizehints->max_height = window->h;
217 }
218 sizehints->flags = PMaxSize | PMinSize;
219 if (!(window->flags & SDL_WINDOW_FULLSCREEN)
220 && window->x != SDL_WINDOWPOS_UNDEFINED
221 && window->y != SDL_WINDOWPOS_UNDEFINED) {
222 sizehints->x = x;
223 sizehints->y = y;
224 sizehints->flags |= USPosition;
225 }
226 XSetWMNormalHints(data->display, w, sizehints);
227 XFree(sizehints);
228 }
229
230 if (window->flags & SDL_WINDOW_BORDERLESS) {
231 SDL_bool set;
232 Atom WM_HINTS;
233
234 /* We haven't modified the window manager hints yet */
235 set = SDL_FALSE;
236
237 /* First try to set MWM hints */
238 WM_HINTS = XInternAtom(data->display, "_MOTIF_WM_HINTS", True);
239 if (WM_HINTS != None) {
240 /* Hints used by Motif compliant window managers */
241 struct
242 {
243 unsigned long flags;
244 unsigned long functions;
245 unsigned long decorations;
246 long input_mode;
247 unsigned long status;
248 } MWMHints = {
249 (1L << 1), 0, 0, 0, 0};
250
251 XChangeProperty(data->display, w, WM_HINTS, WM_HINTS, 32,
252 PropModeReplace, (unsigned char *) &MWMHints,
253 sizeof(MWMHints) / sizeof(long));
254 set = SDL_TRUE;
255 }
256 /* Now try to set KWM hints */
257 WM_HINTS = XInternAtom(data->display, "KWM_WIN_DECORATION", True);
258 if (WM_HINTS != None) {
259 long KWMHints = 0;
260
261 XChangeProperty(data->display, w,
262 WM_HINTS, WM_HINTS, 32,
263 PropModeReplace,
264 (unsigned char *) &KWMHints,
265 sizeof(KWMHints) / sizeof(long));
266 set = SDL_TRUE;
267 }
268 /* Now try to set GNOME hints */
269 WM_HINTS = XInternAtom(data->display, "_WIN_HINTS", True);
270 if (WM_HINTS != None) {
271 long GNOMEHints = 0;
272
273 XChangeProperty(data->display, w,
274 WM_HINTS, WM_HINTS, 32,
275 PropModeReplace,
276 (unsigned char *) &GNOMEHints,
277 sizeof(GNOMEHints) / sizeof(long));
278 set = SDL_TRUE;
279 }
280 /* Finally set the transient hints if necessary */
281 if (!set) {
282 XSetTransientForHint(data->display, w,
283 RootWindow(data->display,
284 displaydata->screen));
285 }
286 } else {
287 SDL_bool set;
288 Atom WM_HINTS;
289
290 /* We haven't modified the window manager hints yet */
291 set = SDL_FALSE;
292
293 /* First try to unset MWM hints */
294 WM_HINTS = XInternAtom(data->display, "_MOTIF_WM_HINTS", True);
295 if (WM_HINTS != None) {
296 XDeleteProperty(data->display, w, WM_HINTS);
297 set = SDL_TRUE;
298 }
299 /* Now try to unset KWM hints */
300 WM_HINTS = XInternAtom(data->display, "KWM_WIN_DECORATION", True);
301 if (WM_HINTS != None) {
302 XDeleteProperty(data->display, w, WM_HINTS);
303 set = SDL_TRUE;
304 }
305 /* Now try to unset GNOME hints */
306 WM_HINTS = XInternAtom(data->display, "_WIN_HINTS", True);
307 if (WM_HINTS != None) {
308 XDeleteProperty(data->display, w, WM_HINTS);
309 set = SDL_TRUE;
310 }
311 /* Finally unset the transient hints if necessary */
312 if (!set) {
313 /* NOTE: Does this work? */
314 XSetTransientForHint(data->display, w, None);
315 }
316 }
317
318 /* Tell KDE to keep fullscreen windows on top */
319 if (window->flags & SDL_WINDOW_FULLSCREEN) {
320 XEvent ev;
321 long mask;
322
323 SDL_zero(ev);
324 ev.xclient.type = ClientMessage;
325 ev.xclient.window = RootWindow(data->display, displaydata->screen);
326 ev.xclient.message_type =
327 XInternAtom(data->display, "KWM_KEEP_ON_TOP", False);
328 ev.xclient.format = 32;
329 ev.xclient.data.l[0] = w;
330 ev.xclient.data.l[1] = CurrentTime;
331 XSendEvent(data->display,
332 RootWindow(data->display, displaydata->screen), False,
333 SubstructureRedirectMask, &ev);
334 }
335
336 /* Set the input hints so we get keyboard input */
337 wmhints = XAllocWMHints();
338 if (wmhints) {
339 wmhints->input = True;
340 if (window->flags & SDL_WINDOW_MINIMIZED) {
341 wmhints->initial_state = IconicState;
342 } else if (window->flags & SDL_WINDOW_SHOWN) {
343 wmhints->initial_state = NormalState;
344 } else {
345 wmhints->initial_state = WithdrawnState;
346 }
347 wmhints->flags = InputHint | StateHint;
348 XSetWMHints(data->display, w, wmhints);
349 XFree(wmhints);
350 }
351
352 XSelectInput(data->display, w,
353 (FocusChangeMask | EnterWindowMask | LeaveWindowMask |
354 ExposureMask | ButtonPressMask | ButtonReleaseMask |
355 PointerMotionMask | KeyPressMask | KeyReleaseMask |
356 PropertyChangeMask | StructureNotifyMask |
357 KeymapStateMask));
358
359 /* Set the class hints so we can get an icon (AfterStep) */
360 classhints = XAllocClassHint();
361 if (classhints != NULL) {
362 classhints->res_name = data->classname;
363 classhints->res_class = data->classname;
364 XSetClassHint(data->display, w, classhints);
365 XFree(classhints);
366 }
367
368 /* Allow the window to be deleted by the window manager */
369 XSetWMProtocols(data->display, w, &data->WM_DELETE_WINDOW, 1);
370
371 /* Finally, show the window */
372 if (window->flags & SDL_WINDOW_SHOWN) {
373 XMapRaised(data->display, w);
374 }
375 XSync(data->display, False);
376
377 if (SetupWindowData(_this, window, w, SDL_TRUE) < 0) {
378 XDestroyWindow(data->display, w);
379 return -1;
380 }
381
382 X11_SetWindowTitle(_this, window);
383
384 #ifdef SDL_VIDEO_OPENGL
385 /*
386 if (window->flags & SDL_WINDOW_OPENGL) {
387 if (X11_GL_SetupWindow(_this, window) < 0) {
388 X11_DestroyWindow(_this, window);
389 return -1;
390 }
391 }
392 */
393 #endif
394 return 0;
395 }
396
397 int
398 X11_CreateWindowFrom(_THIS, SDL_Window * window, const void *data)
399 {
400 Window w = (Window) data;
401
402 /* FIXME: Query the title from the existing window */
403
404 if (SetupWindowData(_this, window, w, SDL_FALSE) < 0) {
405 return -1;
406 }
407 return 0;
408 }
409
410 void
411 X11_SetWindowTitle(_THIS, SDL_Window * window)
412 {
413 SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
414 Display *display = data->videodata->display;
415 XTextProperty titleprop, iconprop;
416 Status status;
417 const char *title = window->title;
418 const char *icon = NULL;
419
420 #ifdef X_HAVE_UTF8_STRING
421 Atom _NET_WM_NAME = 0;
422 Atom _NET_WM_ICON_NAME = 0;
423
424 /* Look up some useful Atoms */
425 if (SDL_X11_HAVE_UTF8) {
426 _NET_WM_NAME = XInternAtom(display, "_NET_WM_NAME", False);
427 _NET_WM_ICON_NAME = XInternAtom(display, "_NET_WM_ICON_NAME", False);
428 }
429 #endif
430
431 if (title != NULL) {
432 char *title_latin1 = SDL_iconv_utf8_latin1((char *) title);
433 if (!title_latin1) {
434 SDL_OutOfMemory();
435 return;
436 }
437 status = XStringListToTextProperty(&title_latin1, 1, &titleprop);
438 SDL_free(title_latin1);
439 if (status) {
440 XSetTextProperty(display, data->window, &titleprop, XA_WM_NAME);
441 XFree(titleprop.value);
442 }
443 #ifdef X_HAVE_UTF8_STRING
444 if (SDL_X11_HAVE_UTF8) {
445 status =
446 Xutf8TextListToTextProperty(display, (char **) &title, 1,
447 XUTF8StringStyle, &titleprop);
448 if (status == Success) {
449 XSetTextProperty(display, data->window, &titleprop,
450 _NET_WM_NAME);
451 XFree(titleprop.value);
452 }
453 }
454 #endif
455 }
456 if (icon != NULL) {
457 char *icon_latin1 = SDL_iconv_utf8_latin1((char *) icon);
458 if (!icon_latin1) {
459 SDL_OutOfMemory();
460 return;
461 }
462 status = XStringListToTextProperty(&icon_latin1, 1, &iconprop);
463 SDL_free(icon_latin1);
464 if (status) {
465 XSetTextProperty(display, data->window, &iconprop,
466 XA_WM_ICON_NAME);
467 XFree(iconprop.value);
468 }
469 #ifdef X_HAVE_UTF8_STRING
470 if (SDL_X11_HAVE_UTF8) {
471 status =
472 Xutf8TextListToTextProperty(display, (char **) &icon, 1,
473 XUTF8StringStyle, &iconprop);
474 if (status == Success) {
475 XSetTextProperty(display, data->window, &iconprop,
476 _NET_WM_ICON_NAME);
477 XFree(iconprop.value);
478 }
479 }
480 #endif
481 }
482 }
483
484 void
485 X11_SetWindowPosition(_THIS, SDL_Window * window)
486 {
487 SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
488 SDL_DisplayData *displaydata =
489 (SDL_DisplayData *) SDL_CurrentDisplay.driverdata;
490 Display *display = data->videodata->display;
491 int x, y;
492
493 if ((window->flags & SDL_WINDOW_FULLSCREEN) ||
494 window->x == SDL_WINDOWPOS_CENTERED) {
495 x = (DisplayWidth(display, displaydata->screen) - window->w) / 2;
496 } else if (window->x == SDL_WINDOWPOS_UNDEFINED) {
497 x = 0;
498 } else {
499 x = window->x;
500 }
501 if ((window->flags & SDL_WINDOW_FULLSCREEN) ||
502 window->y == SDL_WINDOWPOS_CENTERED) {
503 y = (DisplayHeight(display, displaydata->screen) - window->h) / 2;
504 } else if (window->y == SDL_WINDOWPOS_UNDEFINED) {
505 y = 0;
506 } else {
507 y = window->y;
508 }
509 XMoveWindow(display, data->window, x, y);
510 }
511
512 void
513 X11_SetWindowSize(_THIS, SDL_Window * window)
514 {
515 SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
516 Display *display = data->videodata->display;
517
518 XMoveWindow(display, data->window, window->w, window->h);
519 }
520
521 void
522 X11_ShowWindow(_THIS, SDL_Window * window)
523 {
524 SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
525 Display *display = data->videodata->display;
526
527 XMapRaised(display, data->window);
528 }
529
530 void
531 X11_HideWindow(_THIS, SDL_Window * window)
532 {
533 SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
534 Display *display = data->videodata->display;
535
536 XUnmapWindow(display, data->window);
537 }
538
539 void
540 X11_RaiseWindow(_THIS, SDL_Window * window)
541 {
542 SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
543 Display *display = data->videodata->display;
544
545 XRaiseWindow(display, data->window);
546 }
547
548 void
549 X11_MaximizeWindow(_THIS, SDL_Window * window)
550 {
551 /* FIXME: is this even possible? */
552 }
553
554 void
555 X11_MinimizeWindow(_THIS, SDL_Window * window)
556 {
557 X11_HideWindow(_this, window);
558 }
559
560 void
561 X11_RestoreWindow(_THIS, SDL_Window * window)
562 {
563 X11_ShowWindow(_this, window);
564 }
565
566 void
567 X11_SetWindowGrab(_THIS, SDL_Window * window)
568 {
569 /* FIXME */
570 }
571
572 void
573 X11_DestroyWindow(_THIS, SDL_Window * window)
574 {
575 SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
576
577 if (data) {
578 Display *display = data->videodata->display;
579 #ifdef SDL_VIDEO_OPENGL
580 /*
581 if (window->flags & SDL_WINDOW_OPENGL) {
582 X11_GL_CleanupWindow(_this, window);
583 }
584 */
585 #endif
586 #ifdef X_HAVE_UTF8_STRING
587 if (data->ic) {
588 XDestroyIC(data->ic);
589 }
590 #endif
591 if (data->created) {
592 XDestroyWindow(display, data->window);
593 }
594 SDL_free(data);
595 }
596 }
597
598 SDL_bool
599 X11_GetWindowWMInfo(_THIS, SDL_Window * window, SDL_SysWMinfo * info)
600 {
601 if (info->version.major <= SDL_MAJOR_VERSION) {
602 /* FIXME! */
603 return SDL_TRUE;
604 } else {
605 SDL_SetError("Application not compiled with SDL %d.%d\n",
606 SDL_MAJOR_VERSION, SDL_MINOR_VERSION);
607 return SDL_FALSE;
608 }
609 }
610
611 /* vi: set ts=4 sw=4 expandtab: */