Clone of mesa.
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. /* $Id: glxheads.c,v 1.3 2002/03/08 19:44:28 brianp Exp $ */
  2. /*
  3. * Exercise multiple GLX connections on multiple X displays.
  4. * Direct GLX contexts are attempted first, then indirect.
  5. * Each window will display a spinning green triangle.
  6. *
  7. * Copyright (C) 2000 Brian Paul All Rights Reserved.
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a
  10. * copy of this software and associated documentation files (the "Software"),
  11. * to deal in the Software without restriction, including without limitation
  12. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  13. * and/or sell copies of the Software, and to permit persons to whom the
  14. * Software is furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included
  17. * in all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  20. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  22. * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
  23. * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  24. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  25. */
  26. #include <GL/gl.h>
  27. #include <GL/glx.h>
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include <string.h>
  31. #include <unistd.h>
  32. /*
  33. * Each display/window/context:
  34. */
  35. struct head {
  36. char DisplayName[1000];
  37. Display *Dpy;
  38. Window Win;
  39. GLXContext Context;
  40. float Angle;
  41. char Renderer[1000];
  42. char Vendor[1000];
  43. char Version[1000];
  44. };
  45. #define MAX_HEADS 20
  46. static struct head Heads[MAX_HEADS];
  47. static int NumHeads = 0;
  48. static void
  49. Error(const char *display, const char *msg)
  50. {
  51. fprintf(stderr, "Error on display %s - %s\n", display, msg);
  52. exit(1);
  53. }
  54. static struct head *
  55. AddHead(const char *displayName)
  56. {
  57. Display *dpy;
  58. Window win;
  59. GLXContext ctx;
  60. int attrib[] = { GLX_RGBA,
  61. GLX_RED_SIZE, 1,
  62. GLX_GREEN_SIZE, 1,
  63. GLX_BLUE_SIZE, 1,
  64. GLX_DOUBLEBUFFER,
  65. None };
  66. int scrnum;
  67. XSetWindowAttributes attr;
  68. unsigned long mask;
  69. Window root;
  70. XVisualInfo *visinfo;
  71. int width = 300, height = 300;
  72. int xpos = 10, ypos = 10;
  73. if (NumHeads >= MAX_HEADS)
  74. return NULL;
  75. dpy = XOpenDisplay(displayName);
  76. if (!dpy) {
  77. Error(displayName, "Unable to open display");
  78. return NULL;
  79. }
  80. scrnum = DefaultScreen(dpy);
  81. root = RootWindow(dpy, scrnum);
  82. visinfo = glXChooseVisual(dpy, scrnum, attrib);
  83. if (!visinfo) {
  84. Error(displayName, "Unable to find RGB, double-buffered visual");
  85. return NULL;
  86. }
  87. /* window attributes */
  88. attr.background_pixel = 0;
  89. attr.border_pixel = 0;
  90. attr.colormap = XCreateColormap(dpy, root, visinfo->visual, AllocNone);
  91. attr.event_mask = StructureNotifyMask | ExposureMask | KeyPressMask;
  92. mask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask;
  93. win = XCreateWindow(dpy, root, 0, 0, width, height,
  94. 0, visinfo->depth, InputOutput,
  95. visinfo->visual, mask, &attr);
  96. if (!win) {
  97. Error(displayName, "Couldn't create window");
  98. return NULL;
  99. }
  100. {
  101. XSizeHints sizehints;
  102. sizehints.x = xpos;
  103. sizehints.y = ypos;
  104. sizehints.width = width;
  105. sizehints.height = height;
  106. sizehints.flags = USSize | USPosition;
  107. XSetNormalHints(dpy, win, &sizehints);
  108. XSetStandardProperties(dpy, win, displayName, displayName,
  109. None, (char **)NULL, 0, &sizehints);
  110. }
  111. ctx = glXCreateContext(dpy, visinfo, NULL, True);
  112. if (!ctx) {
  113. Error(displayName, "Couldn't create GLX context");
  114. return NULL;
  115. }
  116. XMapWindow(dpy, win);
  117. if (!glXMakeCurrent(dpy, win, ctx)) {
  118. Error(displayName, "glXMakeCurrent failed");
  119. printf("glXMakeCurrent failed in Redraw()\n");
  120. return NULL;
  121. }
  122. /* save the info for this head */
  123. {
  124. struct head *h = &Heads[NumHeads];
  125. strcpy(h->DisplayName, displayName);
  126. h->Dpy = dpy;
  127. h->Win = win;
  128. h->Context = ctx;
  129. h->Angle = 0.0;
  130. strcpy(h->Version, (char *) glGetString(GL_VERSION));
  131. strcpy(h->Vendor, (char *) glGetString(GL_VENDOR));
  132. strcpy(h->Renderer, (char *) glGetString(GL_RENDERER));
  133. NumHeads++;
  134. return &Heads[NumHeads-1];
  135. }
  136. }
  137. static void
  138. Redraw(struct head *h)
  139. {
  140. if (!glXMakeCurrent(h->Dpy, h->Win, h->Context)) {
  141. Error(h->DisplayName, "glXMakeCurrent failed");
  142. printf("glXMakeCurrent failed in Redraw()\n");
  143. return;
  144. }
  145. h->Angle += 1.0;
  146. glShadeModel(GL_FLAT);
  147. glClearColor(0.5, 0.5, 0.5, 1.0);
  148. glClear(GL_COLOR_BUFFER_BIT);
  149. /* draw green triangle */
  150. glColor3f(0.0, 1.0, 0.0);
  151. glPushMatrix();
  152. glRotatef(h->Angle, 0, 0, 1);
  153. glBegin(GL_TRIANGLES);
  154. glVertex2f(0, 0.8);
  155. glVertex2f(-0.8, -0.7);
  156. glVertex2f(0.8, -0.7);
  157. glEnd();
  158. glPopMatrix();
  159. glXSwapBuffers(h->Dpy, h->Win);
  160. }
  161. static void
  162. Resize(const struct head *h, unsigned int width, unsigned int height)
  163. {
  164. if (!glXMakeCurrent(h->Dpy, h->Win, h->Context)) {
  165. Error(h->DisplayName, "glXMakeCurrent failed in Resize()");
  166. return;
  167. }
  168. glFlush();
  169. glViewport(0, 0, width, height);
  170. glMatrixMode(GL_PROJECTION);
  171. glLoadIdentity();
  172. glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);
  173. }
  174. static void
  175. EventLoop(void)
  176. {
  177. while (1) {
  178. int i;
  179. for (i = 0; i < NumHeads; i++) {
  180. struct head *h = &Heads[i];
  181. while (XPending(h->Dpy) > 0) {
  182. XEvent event;
  183. XNextEvent(h->Dpy, &event);
  184. if (event.xany.window == h->Win) {
  185. switch (event.type) {
  186. case Expose:
  187. Redraw(h);
  188. break;
  189. case ConfigureNotify:
  190. Resize(h, event.xconfigure.width, event.xconfigure.height);
  191. break;
  192. case KeyPress:
  193. return;
  194. default:
  195. /*no-op*/ ;
  196. }
  197. }
  198. else {
  199. printf("window mismatch\n");
  200. }
  201. }
  202. Redraw(h);
  203. }
  204. usleep(1);
  205. }
  206. }
  207. static void
  208. PrintInfo(const struct head *h)
  209. {
  210. printf("Name: %s\n", h->DisplayName);
  211. printf(" Display: 0x%x\n", (int) h->Dpy);
  212. printf(" Window: 0x%x\n", (int) h->Win);
  213. printf(" Context: 0x%x\n", (int) h->Context);
  214. printf(" GL_VERSION: %s\n", h->Version);
  215. printf(" GL_VENDOR: %s\n", h->Vendor);
  216. printf(" GL_RENDERER: %s\n", h->Renderer);
  217. }
  218. int
  219. main(int argc, char *argv[])
  220. {
  221. int i;
  222. if (argc == 1) {
  223. struct head *h;
  224. printf("glxheads: exercise multiple GLX connections (any key = exit)\n");
  225. printf("Usage:\n");
  226. printf(" glxheads xdisplayname ...\n");
  227. printf("Example:\n");
  228. printf(" glxheads :0 mars:0 venus:1\n");
  229. h = AddHead(":0");
  230. if (h)
  231. PrintInfo(h);
  232. }
  233. else {
  234. for (i = 1; i < argc; i++) {
  235. const char *name = argv[i];
  236. struct head *h = AddHead(name);
  237. if (h) {
  238. PrintInfo(h);
  239. }
  240. }
  241. }
  242. EventLoop();
  243. return 0;
  244. }