Clone of mesa.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

manywin.c 8.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. /* $Id: manywin.c,v 1.3 2001/01/23 23:45:05 brianp Exp $ */
  2. /*
  3. * Create N GLX windows/contexts and render to them in round-robin
  4. * order.
  5. *
  6. * Copyright (C) 2000 Brian Paul All Rights Reserved.
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a
  9. * copy of this software and associated documentation files (the "Software"),
  10. * to deal in the Software without restriction, including without limitation
  11. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  12. * and/or sell copies of the Software, and to permit persons to whom the
  13. * Software is furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included
  16. * in all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  19. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  21. * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
  22. * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  23. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  24. */
  25. #include <GL/gl.h>
  26. #include <GL/glx.h>
  27. #include <stdio.h>
  28. #include <stdlib.h>
  29. #include <unistd.h>
  30. /*
  31. * Each display/window/context:
  32. */
  33. struct head {
  34. char DisplayName[1000];
  35. Display *Dpy;
  36. Window Win;
  37. GLXContext Context;
  38. float Angle;
  39. char Renderer[1000];
  40. char Vendor[1000];
  41. char Version[1000];
  42. };
  43. #define MAX_HEADS 200
  44. static struct head Heads[MAX_HEADS];
  45. static int NumHeads = 0;
  46. static GLboolean SwapSeparate = GL_TRUE;
  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, const char *name)
  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 = 90, height = 90;
  71. int xpos = 0, ypos = 0;
  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. xpos = (NumHeads % 10) * 100;
  88. ypos = (NumHeads / 10) * 100;
  89. printf("%d, %d\n", xpos, ypos);
  90. attr.background_pixel = 0;
  91. attr.border_pixel = 0;
  92. attr.colormap = XCreateColormap(dpy, root, visinfo->visual, AllocNone);
  93. attr.event_mask = StructureNotifyMask | ExposureMask | KeyPressMask;
  94. mask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask;
  95. win = XCreateWindow(dpy, root, xpos, ypos, width, height,
  96. 0, visinfo->depth, InputOutput,
  97. visinfo->visual, mask, &attr);
  98. if (!win) {
  99. Error(displayName, "Couldn't create window");
  100. return NULL;
  101. }
  102. {
  103. XSizeHints sizehints;
  104. sizehints.x = xpos;
  105. sizehints.y = ypos;
  106. sizehints.width = width;
  107. sizehints.height = height;
  108. sizehints.flags = USSize | USPosition;
  109. XSetNormalHints(dpy, win, &sizehints);
  110. XSetStandardProperties(dpy, win, name, name,
  111. None, (char **)NULL, 0, &sizehints);
  112. }
  113. ctx = glXCreateContext(dpy, visinfo, NULL, True);
  114. if (!ctx) {
  115. Error(displayName, "Couldn't create GLX context");
  116. return NULL;
  117. }
  118. XMapWindow(dpy, win);
  119. if (!glXMakeCurrent(dpy, win, ctx)) {
  120. Error(displayName, "glXMakeCurrent failed");
  121. printf("glXMakeCurrent failed in Redraw()\n");
  122. return;
  123. }
  124. /* save the info for this head */
  125. {
  126. struct head *h = &Heads[NumHeads];
  127. strcpy(h->DisplayName, name);
  128. h->Dpy = dpy;
  129. h->Win = win;
  130. h->Context = ctx;
  131. h->Angle = 0.0;
  132. strcpy(h->Version, (char *) glGetString(GL_VERSION));
  133. strcpy(h->Vendor, (char *) glGetString(GL_VENDOR));
  134. strcpy(h->Renderer, (char *) glGetString(GL_RENDERER));
  135. NumHeads++;
  136. return &Heads[NumHeads-1];
  137. }
  138. }
  139. static void
  140. DestroyHeads(void)
  141. {
  142. int i;
  143. for (i = 0; i < NumHeads; i++) {
  144. XDestroyWindow(Heads[i].Dpy, Heads[i].Win);
  145. glXDestroyContext(Heads[i].Dpy, Heads[i].Context);
  146. XCloseDisplay(Heads[i].Dpy);
  147. }
  148. }
  149. static void
  150. Redraw(struct head *h)
  151. {
  152. if (!glXMakeCurrent(h->Dpy, h->Win, h->Context)) {
  153. Error(h->DisplayName, "glXMakeCurrent failed");
  154. printf("glXMakeCurrent failed in Redraw()\n");
  155. return;
  156. }
  157. h->Angle += 1.0;
  158. glShadeModel(GL_FLAT);
  159. glClearColor(0.5, 0.5, 0.5, 1.0);
  160. glClear(GL_COLOR_BUFFER_BIT);
  161. /* draw green triangle */
  162. glColor3f(0.0, 1.0, 0.0);
  163. glPushMatrix();
  164. glRotatef(h->Angle, 0, 0, 1);
  165. glBegin(GL_TRIANGLES);
  166. glVertex2f(0, 0.8);
  167. glVertex2f(-0.8, -0.7);
  168. glVertex2f(0.8, -0.7);
  169. glEnd();
  170. glPopMatrix();
  171. if (!SwapSeparate)
  172. glXSwapBuffers(h->Dpy, h->Win);
  173. }
  174. static void
  175. Swap(struct head *h)
  176. {
  177. glXSwapBuffers(h->Dpy, h->Win);
  178. }
  179. static void
  180. Resize(const struct head *h, unsigned int width, unsigned int height)
  181. {
  182. if (!glXMakeCurrent(h->Dpy, h->Win, h->Context)) {
  183. Error(h->DisplayName, "glXMakeCurrent failed in Resize()");
  184. return;
  185. }
  186. glFlush();
  187. glViewport(0, 0, width, height);
  188. glMatrixMode(GL_PROJECTION);
  189. glLoadIdentity();
  190. glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);
  191. }
  192. static void
  193. EventLoop(void)
  194. {
  195. while (1) {
  196. int i;
  197. for (i = 0; i < NumHeads; i++) {
  198. struct head *h = &Heads[i];
  199. while (XPending(h->Dpy) > 0) {
  200. XEvent event;
  201. XNextEvent(h->Dpy, &event);
  202. if (event.xany.window == h->Win) {
  203. switch (event.type) {
  204. case Expose:
  205. Redraw(h);
  206. if (SwapSeparate)
  207. Swap(h);
  208. break;
  209. case ConfigureNotify:
  210. Resize(h, event.xconfigure.width, event.xconfigure.height);
  211. break;
  212. case KeyPress:
  213. return;
  214. default:
  215. /*no-op*/ ;
  216. }
  217. }
  218. else {
  219. printf("window mismatch\n");
  220. }
  221. }
  222. }
  223. /* redraw all windows */
  224. for (i = 0; i < NumHeads; i++) {
  225. Redraw(&Heads[i]);
  226. }
  227. /* swapbuffers on all windows, if not already done */
  228. if (SwapSeparate) {
  229. for (i = 0; i < NumHeads; i++) {
  230. Swap(&Heads[i]);
  231. }
  232. }
  233. usleep(1);
  234. }
  235. }
  236. static void
  237. PrintInfo(const struct head *h)
  238. {
  239. printf("Name: %s\n", h->DisplayName);
  240. printf(" Display: 0x%x\n", h->Dpy);
  241. printf(" Window: 0x%x\n", h->Win);
  242. printf(" Context: 0x%x\n", h->Context);
  243. printf(" GL_VERSION: %s\n", h->Version);
  244. printf(" GL_VENDOR: %s\n", h->Vendor);
  245. printf(" GL_RENDERER: %s\n", h->Renderer);
  246. }
  247. int
  248. main(int argc, char *argv[])
  249. {
  250. int i;
  251. if (argc == 1) {
  252. struct head *h;
  253. printf("manywin: open N simultaneous glx windows\n");
  254. printf("Usage:\n");
  255. printf(" manywin [-s] numWindows\n");
  256. printf("Options:\n");
  257. printf(" -s = swap immediately after drawing (see src code)\n");
  258. printf("Example:\n");
  259. printf(" manywin 10\n");
  260. return 0;
  261. }
  262. else {
  263. int n = 3;
  264. for (i = 1; i < argc; i++) {
  265. if (strcmp(argv[i], "-s") == 0) {
  266. SwapSeparate = GL_FALSE;
  267. }
  268. else {
  269. n = atoi(argv[i]);
  270. }
  271. }
  272. if (n < 1)
  273. n = 1;
  274. printf("%d windows\n", n);
  275. for (i = 0; i < n; i++) {
  276. char name[100];
  277. struct head *h;
  278. sprintf(name, "%d", i);
  279. h = AddHead(NULL, name);
  280. if (h) {
  281. PrintInfo(h);
  282. }
  283. }
  284. }
  285. EventLoop();
  286. DestroyHeads();
  287. return 0;
  288. }