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