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 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. /*
  2. * Create N GLX windows/contexts and render to them in round-robin order.
  3. * Also, have the contexts share all texture objects.
  4. * Press 'd' to delete a texture, 'u' to unbind it.
  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 <assert.h>
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include <string.h>
  31. #include <unistd.h>
  32. #include <X11/keysym.h>
  33. /*
  34. * Each display/window/context:
  35. */
  36. struct head {
  37. char DisplayName[1000];
  38. Display *Dpy;
  39. Window Win;
  40. GLXContext Context;
  41. float Angle;
  42. char Renderer[1000];
  43. char Vendor[1000];
  44. char Version[1000];
  45. };
  46. #define MAX_HEADS 200
  47. static struct head Heads[MAX_HEADS];
  48. static int NumHeads = 0;
  49. static GLboolean SwapSeparate = GL_TRUE;
  50. static GLuint TexObj = 0;
  51. static void
  52. Error(const char *display, const char *msg)
  53. {
  54. fprintf(stderr, "Error on display %s - %s\n", XDisplayName(display), msg);
  55. exit(1);
  56. }
  57. static struct head *
  58. AddHead(const char *displayName, const char *name)
  59. {
  60. Display *dpy;
  61. Window win;
  62. GLXContext ctx;
  63. int attrib[] = { GLX_RGBA,
  64. GLX_RED_SIZE, 1,
  65. GLX_GREEN_SIZE, 1,
  66. GLX_BLUE_SIZE, 1,
  67. GLX_DOUBLEBUFFER,
  68. None };
  69. int scrnum;
  70. XSetWindowAttributes attr;
  71. unsigned long mask;
  72. Window root;
  73. XVisualInfo *visinfo;
  74. int width = 90, height = 90;
  75. int xpos = 0, ypos = 0;
  76. if (NumHeads >= MAX_HEADS)
  77. return NULL;
  78. dpy = XOpenDisplay(displayName);
  79. if (!dpy) {
  80. Error(displayName, "Unable to open display");
  81. return NULL;
  82. }
  83. scrnum = DefaultScreen(dpy);
  84. root = RootWindow(dpy, scrnum);
  85. visinfo = glXChooseVisual(dpy, scrnum, attrib);
  86. if (!visinfo) {
  87. Error(displayName, "Unable to find RGB, double-buffered visual");
  88. return NULL;
  89. }
  90. /* window attributes */
  91. xpos = (NumHeads % 10) * 100;
  92. ypos = (NumHeads / 10) * 100;
  93. printf("%d, %d\n", xpos, ypos);
  94. attr.background_pixel = 0;
  95. attr.border_pixel = 0;
  96. attr.colormap = XCreateColormap(dpy, root, visinfo->visual, AllocNone);
  97. attr.event_mask = StructureNotifyMask | ExposureMask | KeyPressMask;
  98. mask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask;
  99. win = XCreateWindow(dpy, root, xpos, ypos, width, height,
  100. 0, visinfo->depth, InputOutput,
  101. visinfo->visual, mask, &attr);
  102. if (!win) {
  103. Error(displayName, "Couldn't create window");
  104. return NULL;
  105. }
  106. {
  107. XSizeHints sizehints;
  108. sizehints.x = xpos;
  109. sizehints.y = ypos;
  110. sizehints.width = width;
  111. sizehints.height = height;
  112. sizehints.flags = USSize | USPosition;
  113. XSetNormalHints(dpy, win, &sizehints);
  114. XSetStandardProperties(dpy, win, name, name,
  115. None, (char **)NULL, 0, &sizehints);
  116. }
  117. if (NumHeads == 0) {
  118. ctx = glXCreateContext(dpy, visinfo, NULL, True);
  119. }
  120. else {
  121. /* share textures & dlists with 0th context */
  122. printf("sharing\n");
  123. ctx = glXCreateContext(dpy, visinfo, Heads[0].Context, True);
  124. }
  125. if (!ctx) {
  126. Error(displayName, "Couldn't create GLX context");
  127. return NULL;
  128. }
  129. XMapWindow(dpy, win);
  130. if (!glXMakeCurrent(dpy, win, ctx)) {
  131. Error(displayName, "glXMakeCurrent failed");
  132. printf("glXMakeCurrent failed in Redraw()\n");
  133. return NULL;
  134. }
  135. if (NumHeads == 0) {
  136. /* create texture object now */
  137. static const GLubyte checker[2][2][4] = {
  138. { {255, 255, 255, 255}, { 0, 0, 0, 255} },
  139. { { 0, 0, 0, 0}, {255, 255, 255, 255} }
  140. };
  141. glGenTextures(1, &TexObj);
  142. assert(TexObj);
  143. glBindTexture(GL_TEXTURE_2D, TexObj);
  144. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 2, 2, 0, GL_RGB,
  145. GL_UNSIGNED_BYTE, checker);
  146. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  147. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  148. }
  149. else {
  150. /* bind 0th context's texture in this context too */
  151. assert(TexObj);
  152. glBindTexture(GL_TEXTURE_2D, TexObj);
  153. }
  154. glEnable(GL_TEXTURE_2D);
  155. /* save the info for this head */
  156. {
  157. struct head *h = &Heads[NumHeads];
  158. strcpy(h->DisplayName, name);
  159. h->Dpy = dpy;
  160. h->Win = win;
  161. h->Context = ctx;
  162. h->Angle = 0.0;
  163. strcpy(h->Version, (char *) glGetString(GL_VERSION));
  164. strcpy(h->Vendor, (char *) glGetString(GL_VENDOR));
  165. strcpy(h->Renderer, (char *) glGetString(GL_RENDERER));
  166. NumHeads++;
  167. return &Heads[NumHeads-1];
  168. }
  169. }
  170. static void
  171. DestroyHeads(void)
  172. {
  173. int i;
  174. for (i = 0; i < NumHeads; i++) {
  175. XDestroyWindow(Heads[i].Dpy, Heads[i].Win);
  176. glXDestroyContext(Heads[i].Dpy, Heads[i].Context);
  177. XCloseDisplay(Heads[i].Dpy);
  178. }
  179. }
  180. static void
  181. Redraw(struct head *h)
  182. {
  183. if (!glXMakeCurrent(h->Dpy, h->Win, h->Context)) {
  184. Error(h->DisplayName, "glXMakeCurrent failed");
  185. printf("glXMakeCurrent failed in Redraw()\n");
  186. return;
  187. }
  188. h->Angle += 1.0;
  189. glShadeModel(GL_FLAT);
  190. glClearColor(0.5, 0.5, 0.5, 1.0);
  191. glClear(GL_COLOR_BUFFER_BIT);
  192. /* draw green triangle */
  193. glColor3f(0.0, 1.0, 0.0);
  194. glPushMatrix();
  195. glRotatef(h->Angle, 0, 0, 1);
  196. glBegin(GL_TRIANGLES);
  197. glTexCoord2f(0.5, 1.0); glVertex2f(0, 0.8);
  198. glTexCoord2f(0.0, 0.0); glVertex2f(-0.8, -0.7);
  199. glTexCoord2f(1.0, 0.0); glVertex2f(0.8, -0.7);
  200. glEnd();
  201. glPopMatrix();
  202. if (!SwapSeparate)
  203. glXSwapBuffers(h->Dpy, h->Win);
  204. }
  205. static void
  206. Swap(struct head *h)
  207. {
  208. glXSwapBuffers(h->Dpy, h->Win);
  209. }
  210. static void
  211. Resize(const struct head *h, unsigned int width, unsigned int height)
  212. {
  213. if (!glXMakeCurrent(h->Dpy, h->Win, h->Context)) {
  214. Error(h->DisplayName, "glXMakeCurrent failed in Resize()");
  215. return;
  216. }
  217. glFlush();
  218. glViewport(0, 0, width, height);
  219. glMatrixMode(GL_PROJECTION);
  220. glLoadIdentity();
  221. glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);
  222. }
  223. static void
  224. EventLoop(void)
  225. {
  226. while (1) {
  227. int i;
  228. for (i = 0; i < NumHeads; i++) {
  229. struct head *h = &Heads[i];
  230. while (XPending(h->Dpy) > 0) {
  231. XEvent event;
  232. XNextEvent(h->Dpy, &event);
  233. if (event.xany.window == h->Win) {
  234. switch (event.type) {
  235. case Expose:
  236. Redraw(h);
  237. if (SwapSeparate)
  238. Swap(h);
  239. break;
  240. case ConfigureNotify:
  241. Resize(h, event.xconfigure.width, event.xconfigure.height);
  242. break;
  243. case KeyPress:
  244. {
  245. char buf[100];
  246. KeySym keySym;
  247. XComposeStatus stat;
  248. XLookupString(&event.xkey, buf, sizeof(buf), &keySym, &stat);
  249. switch (keySym) {
  250. case XK_Escape:
  251. exit(0);
  252. break;
  253. case XK_d:
  254. case XK_D:
  255. printf("Delete Texture in window %d\n", i);
  256. glXMakeCurrent(h->Dpy, h->Win, h->Context);
  257. glDeleteTextures(1, &TexObj);
  258. break;
  259. case XK_u:
  260. case XK_U:
  261. printf("Unbind Texture in window %d\n", i);
  262. glXMakeCurrent(h->Dpy, h->Win, h->Context);
  263. glBindTexture(GL_TEXTURE_2D, 0);
  264. break;
  265. }
  266. }
  267. break;
  268. default:
  269. /*no-op*/ ;
  270. }
  271. }
  272. else {
  273. printf("window mismatch\n");
  274. }
  275. }
  276. }
  277. /* redraw all windows */
  278. for (i = 0; i < NumHeads; i++) {
  279. Redraw(&Heads[i]);
  280. }
  281. /* swapbuffers on all windows, if not already done */
  282. if (SwapSeparate) {
  283. for (i = 0; i < NumHeads; i++) {
  284. Swap(&Heads[i]);
  285. }
  286. }
  287. usleep(1);
  288. }
  289. }
  290. static void
  291. PrintInfo(const struct head *h)
  292. {
  293. printf("Name: %s\n", h->DisplayName);
  294. printf(" Display: %p\n", (void *) h->Dpy);
  295. printf(" Window: 0x%x\n", (int) h->Win);
  296. printf(" Context: 0x%lx\n", (long) h->Context);
  297. printf(" GL_VERSION: %s\n", h->Version);
  298. printf(" GL_VENDOR: %s\n", h->Vendor);
  299. printf(" GL_RENDERER: %s\n", h->Renderer);
  300. }
  301. int
  302. main(int argc, char *argv[])
  303. {
  304. char *dpyName = NULL;
  305. int i;
  306. if (argc == 1) {
  307. printf("manywin: open N simultaneous glx windows\n");
  308. printf("Usage:\n");
  309. printf(" manywin [-s] numWindows\n");
  310. printf("Options:\n");
  311. printf(" -s = swap immediately after drawing (see src code)\n");
  312. printf("Example:\n");
  313. printf(" manywin 10\n");
  314. return 0;
  315. }
  316. else {
  317. int n = 3;
  318. for (i = 1; i < argc; i++) {
  319. if (strcmp(argv[i], "-s") == 0) {
  320. SwapSeparate = GL_FALSE;
  321. }
  322. else if (strcmp(argv[i], "-display") == 0 && i < argc) {
  323. dpyName = argv[i+1];
  324. i++;
  325. }
  326. else {
  327. n = atoi(argv[i]);
  328. }
  329. }
  330. if (n < 1)
  331. n = 1;
  332. printf("%d windows\n", n);
  333. for (i = 0; i < n; i++) {
  334. char name[100];
  335. struct head *h;
  336. sprintf(name, "%d", i);
  337. h = AddHead(dpyName, name);
  338. if (h) {
  339. PrintInfo(h);
  340. }
  341. }
  342. }
  343. EventLoop();
  344. DestroyHeads();
  345. return 0;
  346. }