glsl2: Add and use new variable mode ir_var_temporary
[mesa/nouveau-pmpeg.git] / src / gallium / targets / graw-xlib / graw_xlib.c
blob41120ba3c70f1b78ffba2eee73f9064fee811b69
1 #include "pipe/p_compiler.h"
2 #include "pipe/p_context.h"
3 #include "util/u_debug.h"
4 #include "util/u_memory.h"
5 #include "target-helpers/wrap_screen.h"
6 #include "state_tracker/xlib_sw_winsys.h"
8 #ifdef GALLIUM_SOFTPIPE
9 #include "softpipe/sp_public.h"
10 #endif
12 #ifdef GALLIUM_LLVMPIPE
13 #include "llvmpipe/lp_public.h"
14 #endif
16 /* Haven't figured out a decent way to build the helper code yet -
17 * #include it here temporarily.
19 #include "sw/sw_public.h"
20 #include "sw/sw.c"
22 #include "state_tracker/graw.h"
24 #include <X11/Xlib.h>
25 #include <X11/Xlibint.h>
26 #include <X11/Xutil.h>
27 #include <stdio.h>
29 static struct {
30 Display *display;
31 void (*draw)(void);
32 } graw;
35 static struct pipe_screen *
36 graw_create_screen( void )
38 const char *default_driver;
39 const char *driver;
40 struct pipe_screen *screen = NULL;
41 struct sw_winsys *winsys = NULL;
43 /* Create the underlying winsys, which performs presents to Xlib
44 * drawables:
46 winsys = xlib_create_sw_winsys( graw.display );
47 if (winsys == NULL)
48 return NULL;
50 #if defined(GALLIUM_LLVMPIPE)
51 default_driver = "llvmpipe";
52 #elif defined(GALLIUM_SOFTPIPE)
53 default_driver = "softpipe";
54 #else
55 default_driver = "";
56 #endif
58 driver = debug_get_option("GALLIUM_DRIVER", default_driver);
60 #if defined(GALLIUM_LLVMPIPE)
61 if (screen == NULL && strcmp(driver, "llvmpipe") == 0)
62 screen = llvmpipe_create_screen( winsys );
63 #endif
65 #if defined(GALLIUM_SOFTPIPE)
66 if (screen == NULL)
67 screen = softpipe_create_screen( winsys );
68 #endif
70 /* Inject any wrapping layers we want to here:
72 return gallium_wrap_screen( screen );
79 struct pipe_screen *
80 graw_create_window_and_screen( int x,
81 int y,
82 unsigned width,
83 unsigned height,
84 enum pipe_format format,
85 void **handle)
87 struct pipe_screen *screen = NULL;
88 struct xlib_drawable *xlib_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;
97 graw.display = XOpenDisplay(NULL);
98 if (graw.display == NULL)
99 return NULL;
101 scrnum = DefaultScreen( graw.display );
102 root = RootWindow( graw.display, scrnum );
105 if (format != PIPE_FORMAT_R8G8B8A8_UNORM)
106 goto fail;
108 if (graw.display == NULL)
109 goto fail;
111 xlib_handle = CALLOC_STRUCT(xlib_drawable);
112 if (xlib_handle == NULL)
113 goto fail;
116 mask = VisualScreenMask | VisualDepthMask | VisualClassMask;
117 templat.screen = DefaultScreen(graw.display);
118 templat.depth = 32;
119 templat.class = TrueColor;
121 visinfo = XGetVisualInfo(graw.display, mask, &templat, &n);
122 if (!visinfo) {
123 printf("Error: couldn't get an RGB, Double-buffered visual\n");
124 exit(1);
127 /* window attributes */
128 attr.background_pixel = 0;
129 attr.border_pixel = 0;
130 attr.colormap = XCreateColormap( graw.display, root, visinfo->visual, AllocNone);
131 attr.event_mask = StructureNotifyMask | ExposureMask | KeyPressMask;
132 /* XXX this is a bad way to get a borderless window! */
133 mask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask;
135 win = XCreateWindow( graw.display, root, x, y, width, height,
136 0, visinfo->depth, InputOutput,
137 visinfo->visual, mask, &attr );
140 /* set hints and properties */
142 char *name = NULL;
143 XSizeHints sizehints;
144 sizehints.x = x;
145 sizehints.y = y;
146 sizehints.width = width;
147 sizehints.height = height;
148 sizehints.flags = USSize | USPosition;
149 XSetNormalHints(graw.display, win, &sizehints);
150 XSetStandardProperties(graw.display, win, name, name,
151 None, (char **)NULL, 0, &sizehints);
154 XMapWindow(graw.display, win);
155 while (1) {
156 XEvent e;
157 XNextEvent( graw.display, &e );
158 if (e.type == MapNotify && e.xmap.window == win) {
159 break;
163 xlib_handle->visual = visinfo->visual;
164 xlib_handle->drawable = (Drawable)win;
165 xlib_handle->depth = visinfo->depth;
166 *handle = (void *)xlib_handle;
168 screen = graw_create_screen();
169 if (screen == NULL)
170 goto fail;
172 XFree(visinfo);
173 return screen;
175 fail:
176 if (screen)
177 screen->destroy(screen);
179 if (xlib_handle)
180 FREE(xlib_handle);
182 if (visinfo)
183 XFree(visinfo);
185 if (win)
186 XDestroyWindow(graw.display, win);
188 return NULL;
192 void
193 graw_set_display_func( void (*draw)( void ) )
195 graw.draw = draw;
198 void
199 graw_main_loop( void )
201 int i;
202 for (i = 0; i < 10; i++) {
203 graw.draw();
204 sleep(1);