Merge branch '7.8'
[mesa.git] / src / gallium / targets / graw-xlib / graw_xlib.c
1 #include "pipe/p_compiler.h"
2 #include "util/u_debug.h"
3 #include "util/u_memory.h"
4 #include "target-helpers/wrap_screen.h"
5 #include "state_tracker/xlib_sw_winsys.h"
6
7 #ifdef GALLIUM_SOFTPIPE
8 #include "softpipe/sp_public.h"
9 #endif
10
11 #ifdef GALLIUM_LLVMPIPE
12 #include "llvmpipe/lp_public.h"
13 #endif
14
15 /* Haven't figured out a decent way to build the helper code yet -
16 * #include it here temporarily.
17 */
18 #include "sw/sw_public.h"
19 #include "sw/sw.c"
20
21 #include "graw.h"
22
23 #include <X11/Xlib.h>
24 #include <X11/Xlibint.h>
25 #include <X11/Xutil.h>
26 #include <stdio.h>
27
28 static struct {
29 Display *display;
30 } graw;
31
32
33 struct pipe_screen *
34 graw_init( void )
35 {
36 const char *default_driver;
37 const char *driver;
38 struct pipe_screen *screen = NULL;
39 struct sw_winsys *winsys = NULL;
40
41 graw.display = XOpenDisplay(NULL);
42 if (graw.display == NULL)
43 return NULL;
44
45 /* Create the underlying winsys, which performs presents to Xlib
46 * drawables:
47 */
48 winsys = xlib_create_sw_winsys( graw.display );
49 if (winsys == NULL)
50 return NULL;
51
52 #if defined(GALLIUM_LLVMPIPE)
53 default_driver = "llvmpipe";
54 #elif defined(GALLIUM_SOFTPIPE)
55 default_driver = "softpipe";
56 #else
57 default_driver = "";
58 #endif
59
60 driver = debug_get_option("GALLIUM_DRIVER", default_driver);
61
62 #if defined(GALLIUM_LLVMPIPE)
63 if (screen == NULL && strcmp(driver, "llvmpipe") == 0)
64 screen = llvmpipe_create_screen( winsys );
65 #endif
66
67 #if defined(GALLIUM_SOFTPIPE)
68 if (screen == NULL)
69 screen = softpipe_create_screen( winsys );
70 #endif
71
72 /* Inject any wrapping layers we want to here:
73 */
74 return gallium_wrap_screen( screen );
75 }
76
77
78
79
80
81 void *
82 graw_create_window( int x,
83 int y,
84 unsigned width,
85 unsigned height,
86 enum pipe_format format )
87 {
88 struct xlib_drawable *handle = NULL;
89 XSetWindowAttributes attr;
90 Window root;
91 Window win = 0;
92 XVisualInfo templat, *visinfo = NULL;
93 unsigned mask;
94 int n;
95 int scrnum;
96
97
98 scrnum = DefaultScreen( graw.display );
99 root = RootWindow( graw.display, scrnum );
100
101
102 if (format != PIPE_FORMAT_R8G8B8A8_UNORM)
103 goto fail;
104
105 if (graw.display == NULL)
106 goto fail;
107
108 handle = CALLOC_STRUCT(xlib_drawable);
109 if (handle == NULL)
110 goto fail;
111
112
113 mask = VisualScreenMask | VisualDepthMask | VisualClassMask;
114 templat.screen = DefaultScreen(graw.display);
115 templat.depth = 32;
116 templat.class = TrueColor;
117
118 visinfo = XGetVisualInfo(graw.display, mask, &templat, &n);
119 if (!visinfo) {
120 printf("Error: couldn't get an RGB, Double-buffered visual\n");
121 exit(1);
122 }
123
124 /* window attributes */
125 attr.background_pixel = 0;
126 attr.border_pixel = 0;
127 attr.colormap = XCreateColormap( graw.display, root, visinfo->visual, AllocNone);
128 attr.event_mask = StructureNotifyMask | ExposureMask | KeyPressMask;
129 /* XXX this is a bad way to get a borderless window! */
130 mask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask;
131
132 win = XCreateWindow( graw.display, root, x, y, width, height,
133 0, visinfo->depth, InputOutput,
134 visinfo->visual, mask, &attr );
135
136
137 /* set hints and properties */
138 {
139 char *name = NULL;
140 XSizeHints sizehints;
141 sizehints.x = x;
142 sizehints.y = y;
143 sizehints.width = width;
144 sizehints.height = height;
145 sizehints.flags = USSize | USPosition;
146 XSetNormalHints(graw.display, win, &sizehints);
147 XSetStandardProperties(graw.display, win, name, name,
148 None, (char **)NULL, 0, &sizehints);
149 }
150
151 XFree(visinfo);
152 XMapWindow(graw.display, win);
153 while (1) {
154 XEvent e;
155 XNextEvent( graw.display, &e );
156 if (e.type == MapNotify && e.xmap.window == win) {
157 break;
158 }
159 }
160
161 handle->visual = visinfo->visual;
162 handle->drawable = (Drawable)win;
163 handle->depth = visinfo->depth;
164 return (void *)handle;
165
166 fail:
167 FREE(handle);
168 XFree(visinfo);
169
170 if (win)
171 XDestroyWindow(graw.display, win);
172
173 return NULL;
174 }
175
176
177 void
178 graw_destroy_window( void *xlib_drawable )
179 {
180 }
181