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.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. /* $Id: manywin.c,v 1.5 2001/11/26 17:21:46 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 <string.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 200
  45. static struct head Heads[MAX_HEADS];
  46. static int NumHeads = 0;
  47. static GLboolean SwapSeparate = GL_TRUE;
  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, const char *name)
  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 = 90, height = 90;
  72. int xpos = 0, ypos = 0;
  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. xpos = (NumHeads % 10) * 100;
  89. ypos = (NumHeads / 10) * 100;
  90. printf("%d, %d\n", xpos, ypos);
  91. attr.background_pixel = 0;
  92. attr.border_pixel = 0;
  93. attr.colormap = XCreateColormap(dpy, root, visinfo->visual, AllocNone);
  94. attr.event_mask = StructureNotifyMask | ExposureMask | KeyPressMask;
  95. mask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask;
  96. win = XCreateWindow(dpy, root, xpos, ypos, width, height,
  97. 0, visinfo->depth, InputOutput,
  98. visinfo->visual, mask, &attr);
  99. if (!win) {
  100. Error(displayName, "Couldn't create window");
  101. return NULL;
  102. }
  103. {
  104. XSizeHints sizehints;
  105. sizehints.x = xpos;
  106. sizehints.y = ypos;
  107. sizehints.width = width;
  108. sizehints.height = height;
  109. sizehints.flags = USSize | USPosition;
  110. XSetNormalHints(dpy, win, &sizehints);
  111. XSetStandardProperties(dpy, win, name, name,
  112. None, (char **)NULL, 0, &sizehints);
  113. }
  114. ctx = glXCreateContext(dpy, visinfo, NULL, True);
  115. if (!ctx) {
  116. Error(displayName, "Couldn't create GLX context");
  117. return NULL;
  118. }
  119. XMapWindow(dpy, win);
  120. if (!glXMakeCurrent(dpy, win, ctx)) {
  121. Error(displayName, "glXMakeCurrent failed");
  122. printf("glXMakeCurrent failed in Redraw()\n");
  123. return NULL;
  124. }
  125. /* save the info for this head */
  126. {
  127. struct head *h = &Heads[NumHeads];
  128. strcpy(h->DisplayName, name);
  129. h->Dpy = dpy;
  130. h->Win = win;
  131. h->Context = ctx;
  132. h->Angle = 0.0;
  133. strcpy(h->Version, (char *) glGetString(GL_VERSION));
  134. strcpy(h->Vendor, (char *) glGetString(GL_VENDOR));
  135. strcpy(h->Renderer, (char *) glGetString(GL_RENDERER));
  136. NumHeads++;
  137. return &Heads[NumHeads-1];
  138. }
  139. }
  140. static void
  141. DestroyHeads(void)
  142. {
  143. int i;
  144. for (i = 0; i < NumHeads; i++) {
  145. XDestroyWindow(Heads[i].Dpy, Heads[i].Win);
  146. glXDestroyContext(Heads[i].Dpy, Heads[i].Context);
  147. XCloseDisplay(Heads[i].Dpy);
  148. }
  149. }
  150. static void
  151. Redraw(struct head *h)
  152. {
  153. if (!glXMakeCurrent(h->Dpy, h->Win, h->Context)) {
  154. Error(h->DisplayName, "glXMakeCurrent failed");
  155. printf("glXMakeCurrent failed in Redraw()\n");
  156. return;
  157. }
  158. h->Angle += 1.0;
  159. glShadeModel(GL_FLAT);
  160. glClearColor(0.5, 0.5, 0.5, 1.0);
  161. glClear(GL_COLOR_BUFFER_BIT);
  162. /* draw green triangle */
  163. glColor3f(0.0, 1.0, 0.0);
  164. glPushMatrix();
  165. glRotatef(h->Angle, 0, 0, 1);
  166. glBegin(GL_TRIANGLES);
  167. glVertex2f(0, 0.8);
  168. glVertex2f(-0.8, -0.7);
  169. glVertex2f(0.8, -0.7);
  170. glEnd();
  171. glPopMatrix();
  172. if (!SwapSeparate)
  173. glXSwapBuffers(h->Dpy, h->Win);
  174. }
  175. static void
  176. Swap(struct head *h)
  177. {
  178. glXSwapBuffers(h->Dpy, h->Win);
  179. }
  180. static void
  181. Resize(const struct head *h, unsigned int width, unsigned int height)
  182. {
  183. if (!glXMakeCurrent(h->Dpy, h->Win, h->Context)) {
  184. Error(h->DisplayName, "glXMakeCurrent failed in Resize()");
  185. return;
  186. }
  187. glFlush();
  188. glViewport(0, 0, width, height);
  189. glMatrixMode(GL_PROJECTION);
  190. glLoadIdentity();
  191. glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);
  192. }
  193. static void
  194. EventLoop(void)
  195. {
  196. while (1) {
  197. int i;
  198. for (i = 0; i < NumHeads; i++) {
  199. struct head *h = &Heads[i];
  200. while (XPending(h->Dpy) > 0) {
  201. XEvent event;
  202. XNextEvent(h->Dpy, &event);
  203. if (event.xany.window == h->Win) {
  204. switch (event.type) {
  205. case Expose:
  206. Redraw(h);
  207. if (SwapSeparate)
  208. Swap(h);
  209. break;
  210. case ConfigureNotify:
  211. Resize(h, event.xconfigure.width, event.xconfigure.height);
  212. break;
  213. case KeyPress:
  214. return;
  215. default:
  216. /*no-op*/ ;
  217. }
  218. }
  219. else {
  220. printf("window mismatch\n");
  221. }
  222. }
  223. }
  224. /* redraw all windows */
  225. for (i = 0; i < NumHeads; i++) {
  226. Redraw(&Heads[i]);
  227. }
  228. /* swapbuffers on all windows, if not already done */
  229. if (SwapSeparate) {
  230. for (i = 0; i < NumHeads; i++) {
  231. Swap(&Heads[i]);
  232. }
  233. }
  234. usleep(1);
  235. }
  236. }
  237. static void
  238. PrintInfo(const struct head *h)
  239. {
  240. printf("Name: %s\n", h->DisplayName);
  241. printf(" Display: %p\n", (void *) h->Dpy);
  242. printf(" Window: 0x%x\n", (int) h->Win);
  243. printf(" Context: 0x%x\n", (int) h->Context);
  244. printf(" GL_VERSION: %s\n", h->Version);
  245. printf(" GL_VENDOR: %s\n", h->Vendor);
  246. printf(" GL_RENDERER: %s\n", h->Renderer);
  247. }
  248. int
  249. main(int argc, char *argv[])
  250. {
  251. char *dpyName = NULL;
  252. int i;
  253. if (argc == 1) {
  254. printf("manywin: open N simultaneous glx windows\n");
  255. printf("Usage:\n");
  256. printf(" manywin [-s] numWindows\n");
  257. printf("Options:\n");
  258. printf(" -s = swap immediately after drawing (see src code)\n");
  259. printf("Example:\n");
  260. printf(" manywin 10\n");
  261. return 0;
  262. }
  263. else {
  264. int n = 3;
  265. for (i = 1; i < argc; i++) {
  266. if (strcmp(argv[i], "-s") == 0) {
  267. SwapSeparate = GL_FALSE;
  268. }
  269. else if (strcmp(argv[i], "-display") == 0 && i < argc) {
  270. dpyName = argv[i+1];
  271. i++;
  272. }
  273. else {
  274. n = atoi(argv[i]);
  275. }
  276. }
  277. if (n < 1)
  278. n = 1;
  279. printf("%d windows\n", n);
  280. for (i = 0; i < n; i++) {
  281. char name[100];
  282. struct head *h;
  283. sprintf(name, "%d", i);
  284. h = AddHead(dpyName, name);
  285. if (h) {
  286. PrintInfo(h);
  287. }
  288. }
  289. }
  290. EventLoop();
  291. DestroyHeads();
  292. return 0;
  293. }