Clone of mesa.
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

glxheads.c 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. /* $Id: glxheads.c,v 1.2 2000/11/10 17:23:07 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 <unistd.h>
  31. /*
  32. * Each display/window/context:
  33. */
  34. struct head {
  35. char DisplayName[1000];
  36. Display *Dpy;
  37. Window Win;
  38. GLXContext Context;
  39. float Angle;
  40. char Renderer[1000];
  41. char Vendor[1000];
  42. char Version[1000];
  43. };
  44. #define MAX_HEADS 20
  45. static struct head Heads[MAX_HEADS];
  46. static int NumHeads = 0;
  47. static void
  48. Error(const char *display, const char *msg)
  49. {
  50. fprintf(stderr, "Error on display %s - %s\n", display, msg);
  51. exit(1);
  52. }
  53. static struct head *
  54. AddHead(const char *displayName)
  55. {
  56. Display *dpy;
  57. Window win;
  58. GLXContext ctx;
  59. int attrib[] = { GLX_RGBA,
  60. GLX_RED_SIZE, 1,
  61. GLX_GREEN_SIZE, 1,
  62. GLX_BLUE_SIZE, 1,
  63. GLX_DOUBLEBUFFER,
  64. None };
  65. int scrnum;
  66. XSetWindowAttributes attr;
  67. unsigned long mask;
  68. Window root;
  69. XVisualInfo *visinfo;
  70. int width = 300, height = 300;
  71. int xpos = 10, ypos = 10;
  72. if (NumHeads >= MAX_HEADS)
  73. return NULL;
  74. dpy = XOpenDisplay(displayName);
  75. if (!dpy) {
  76. Error(displayName, "Unable to open display");
  77. return NULL;
  78. }
  79. scrnum = DefaultScreen(dpy);
  80. root = RootWindow(dpy, scrnum);
  81. visinfo = glXChooseVisual(dpy, scrnum, attrib);
  82. if (!visinfo) {
  83. Error(displayName, "Unable to find RGB, double-buffered visual");
  84. return NULL;
  85. }
  86. /* window attributes */
  87. attr.background_pixel = 0;
  88. attr.border_pixel = 0;
  89. attr.colormap = XCreateColormap(dpy, root, visinfo->visual, AllocNone);
  90. attr.event_mask = StructureNotifyMask | ExposureMask | KeyPressMask;
  91. mask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask;
  92. win = XCreateWindow(dpy, root, 0, 0, width, height,
  93. 0, visinfo->depth, InputOutput,
  94. visinfo->visual, mask, &attr);
  95. if (!win) {
  96. Error(displayName, "Couldn't create window");
  97. return NULL;
  98. }
  99. {
  100. XSizeHints sizehints;
  101. sizehints.x = xpos;
  102. sizehints.y = ypos;
  103. sizehints.width = width;
  104. sizehints.height = height;
  105. sizehints.flags = USSize | USPosition;
  106. XSetNormalHints(dpy, win, &sizehints);
  107. XSetStandardProperties(dpy, win, displayName, displayName,
  108. None, (char **)NULL, 0, &sizehints);
  109. }
  110. ctx = glXCreateContext(dpy, visinfo, NULL, True);
  111. if (!ctx) {
  112. Error(displayName, "Couldn't create GLX context");
  113. return NULL;
  114. }
  115. XMapWindow(dpy, win);
  116. if (!glXMakeCurrent(dpy, win, ctx)) {
  117. Error(displayName, "glXMakeCurrent failed");
  118. printf("glXMakeCurrent failed in Redraw()\n");
  119. return NULL;
  120. }
  121. /* save the info for this head */
  122. {
  123. struct head *h = &Heads[NumHeads];
  124. strcpy(h->DisplayName, displayName);
  125. h->Dpy = dpy;
  126. h->Win = win;
  127. h->Context = ctx;
  128. h->Angle = 0.0;
  129. strcpy(h->Version, (char *) glGetString(GL_VERSION));
  130. strcpy(h->Vendor, (char *) glGetString(GL_VENDOR));
  131. strcpy(h->Renderer, (char *) glGetString(GL_RENDERER));
  132. NumHeads++;
  133. return &Heads[NumHeads-1];
  134. }
  135. }
  136. static void
  137. Redraw(struct head *h)
  138. {
  139. if (!glXMakeCurrent(h->Dpy, h->Win, h->Context)) {
  140. Error(h->DisplayName, "glXMakeCurrent failed");
  141. printf("glXMakeCurrent failed in Redraw()\n");
  142. return;
  143. }
  144. h->Angle += 1.0;
  145. glShadeModel(GL_FLAT);
  146. glClearColor(0.5, 0.5, 0.5, 1.0);
  147. glClear(GL_COLOR_BUFFER_BIT);
  148. /* draw green triangle */
  149. glColor3f(0.0, 1.0, 0.0);
  150. glPushMatrix();
  151. glRotatef(h->Angle, 0, 0, 1);
  152. glBegin(GL_TRIANGLES);
  153. glVertex2f(0, 0.8);
  154. glVertex2f(-0.8, -0.7);
  155. glVertex2f(0.8, -0.7);
  156. glEnd();
  157. glPopMatrix();
  158. glXSwapBuffers(h->Dpy, h->Win);
  159. }
  160. static void
  161. Resize(const struct head *h, unsigned int width, unsigned int height)
  162. {
  163. if (!glXMakeCurrent(h->Dpy, h->Win, h->Context)) {
  164. Error(h->DisplayName, "glXMakeCurrent failed in Resize()");
  165. return;
  166. }
  167. glFlush();
  168. glViewport(0, 0, width, height);
  169. glMatrixMode(GL_PROJECTION);
  170. glLoadIdentity();
  171. glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);
  172. }
  173. static void
  174. EventLoop(void)
  175. {
  176. while (1) {
  177. int i;
  178. for (i = 0; i < NumHeads; i++) {
  179. struct head *h = &Heads[i];
  180. while (XPending(h->Dpy) > 0) {
  181. XEvent event;
  182. XNextEvent(h->Dpy, &event);
  183. if (event.xany.window == h->Win) {
  184. switch (event.type) {
  185. case Expose:
  186. Redraw(h);
  187. break;
  188. case ConfigureNotify:
  189. Resize(h, event.xconfigure.width, event.xconfigure.height);
  190. break;
  191. case KeyPress:
  192. return;
  193. default:
  194. /*no-op*/ ;
  195. }
  196. }
  197. else {
  198. printf("window mismatch\n");
  199. }
  200. }
  201. Redraw(h);
  202. }
  203. usleep(1);
  204. }
  205. }
  206. static void
  207. PrintInfo(const struct head *h)
  208. {
  209. printf("Name: %s\n", h->DisplayName);
  210. printf(" Display: 0x%x\n", h->Dpy);
  211. printf(" Window: 0x%x\n", h->Win);
  212. printf(" Context: 0x%x\n", h->Context);
  213. printf(" GL_VERSION: %s\n", h->Version);
  214. printf(" GL_VENDOR: %s\n", h->Vendor);
  215. printf(" GL_RENDERER: %s\n", h->Renderer);
  216. }
  217. int
  218. main(int argc, char *argv[])
  219. {
  220. int i;
  221. if (argc == 1) {
  222. struct head *h;
  223. printf("glxheads: exercise multiple GLX connections (any key = exit)\n");
  224. printf("Usage:\n");
  225. printf(" glxheads xdisplayname ...\n");
  226. printf("Example:\n");
  227. printf(" glxheads :0 mars:0 venus:1\n");
  228. h = AddHead(":0");
  229. if (h)
  230. PrintInfo(h);
  231. }
  232. else {
  233. for (i = 1; i < argc; i++) {
  234. const char *name = argv[i];
  235. struct head *h = AddHead(name);
  236. if (h) {
  237. PrintInfo(h);
  238. }
  239. }
  240. }
  241. EventLoop();
  242. return 0;
  243. }