1f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org/*
2f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * Copyright © 2011 Intel Corporation
3f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org *
4f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * Permission is hereby granted, free of charge, to any person obtaining a
5f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * copy of this software and associated documentation files (the "Software"),
6f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * to deal in the Software without restriction, including without limitation
7f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * and/or sell copies of the Software, and to permit persons to whom the
9f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * Software is furnished to do so, subject to the following conditions:
10f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org *
11f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * The above copyright notice and this permission notice (including the next
12f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * paragraph) shall be included in all copies or substantial portions of the
13f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * Software.
14f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org *
15f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * DEALINGS IN THE SOFTWARE.
22f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org */
23f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
24f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include <string.h>
25f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include <ctype.h>
26f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
27f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include "glxclient.h"
28f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include <xcb/glx.h>
29f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include <X11/Xlib-xcb.h>
30f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
31f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org_X_HIDDEN void
32f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org__glX_send_client_info(struct glx_display *glx_dpy)
33f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org{
34f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   const unsigned ext_length = strlen("GLX_ARB_create_context");
35f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   const unsigned prof_length = strlen("_profile");
36f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   char *gl_extension_string;
37f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   int gl_extension_length;
38f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   xcb_connection_t *c;
39f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   Bool any_screen_has_ARB_create_context = False;
40f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   Bool any_screen_has_ARB_create_context_profile = False;
41f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   unsigned i;
42f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   static const uint32_t gl_versions[] = {
43f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      1, 4,
44f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   };
45f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   static const uint32_t gl_versions_profiles[] = {
46f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      1, 4, 0x00000000,
47f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   };
48f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   static const char glx_extensions[] =
49f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      "GLX_ARB_create_context GLX_ARB_create_context_profile";
50f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
51f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   /* There are three possible flavors of the client info structure that the
52f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org    * client could send to the server.  The version sent depends on the
53f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org    * combination of GLX versions and extensions supported by the client and
54f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org    * the server.
55f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org    *
56f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org    * Server supports                  Client sends
57f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org    * ----------------------------------------------------------------------
58f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org    * GLX version = 1.0                Nothing.
59f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org    *
60f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org    * GLX version >= 1.1               struct GLXClientInfo
61f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org    *
62f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org    * GLX version >= 1.4 and
63f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org    * GLX_ARB_create_context           struct glXSetClientInfoARB
64f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org    *
65f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org    * GLX version >= 1.4 and
66f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org    * GLX_ARB_create_context_profile   struct glXSetClientInfo2ARB
67f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org    *
68f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org    * GLX_ARB_create_context and GLX_ARB_create_context_profile use FBConfigs,
69f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org    * and these only exist in GLX 1.4 or with GLX_SGIX_fbconfig.  I can't
70f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org    * imagine an implementation that supports GLX_SGIX_fbconfig and
71f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org    * GLX_ARB_create_context but not GLX 1.4.  Making GLX 1.4 a hard
72f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org    * requirement in this case does not seem like a limitation.
73f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org    *
74f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org    * This library currently only supports struct GLXClientInfo.
75f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org    */
76f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
77f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   if (glx_dpy->majorVersion == 1 && glx_dpy->minorVersion == 0)
78f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      return;
79f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
80f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   /* Determine whether any screen on the server supports either of the
81f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org    * create-context extensions.
82f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org    */
83f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   for (i = 0; i < ScreenCount(glx_dpy->dpy); i++) {
84f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      struct glx_screen *src = glx_dpy->screens[i];
85f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
86f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      const char *haystack = src->serverGLXexts;
87f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      while (haystack != NULL) {
88f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	 char *match = strstr(haystack, "GLX_ARB_create_context");
89f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
90f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	 if (match == NULL)
91f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	    break;
92f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
93f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	 match += ext_length;
94f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
95f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	 switch (match[0]) {
96f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	 case '\0':
97f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	 case ' ':
98f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	    any_screen_has_ARB_create_context = True;
99f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	    break;
100f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
101f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	 case '_':
102f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	    if (strncmp(match, "_profile", prof_length) == 0
103f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org		    && (match[prof_length] == '\0'
104f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org			|| match[prof_length] == ' ')) {
105f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	       any_screen_has_ARB_create_context_profile = True;
106f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	       match += prof_length;
107f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	    }
108f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	    break;
109f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	 }
110f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
111f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	 haystack = match;
112f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      }
113f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   }
114f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
115f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   gl_extension_string = __glXGetClientGLExtensionString();
116f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   gl_extension_length = strlen(gl_extension_string) + 1;
117f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
118f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   c = XGetXCBConnection(glx_dpy->dpy);
119f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
120f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   /* Depending on the GLX verion and the available extensions on the server,
121f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org    * send the correct "flavor" of protocol to the server.
122f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org    *
123f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org    * THE ORDER IS IMPORTANT.  We want to send the most recent version of the
124f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org    * protocol that the server can support.
125f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org    */
126f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   if (glx_dpy->majorVersion == 1 && glx_dpy->minorVersion == 4
127f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org       && any_screen_has_ARB_create_context_profile) {
128f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      xcb_glx_set_client_info_2arb(c,
129f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org				  GLX_MAJOR_VERSION, GLX_MINOR_VERSION,
130f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org				   sizeof(gl_versions_profiles)
131f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org				   / (3 * sizeof(gl_versions_profiles[0])),
132f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org				  gl_extension_length,
133f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org				  strlen(glx_extensions) + 1,
134f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org				  gl_versions_profiles,
135f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org				  gl_extension_string,
136f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org				  glx_extensions);
137f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   } else if (glx_dpy->majorVersion == 1 && glx_dpy->minorVersion == 4
138f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	      && any_screen_has_ARB_create_context) {
139f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      xcb_glx_set_client_info_arb(c,
140f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org				  GLX_MAJOR_VERSION, GLX_MINOR_VERSION,
141f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org				  sizeof(gl_versions)
142f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org				  / (2 * sizeof(gl_versions[0])),
143f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org				  gl_extension_length,
144f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org				  strlen(glx_extensions) + 1,
145f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org				  gl_versions,
146f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org				  gl_extension_string,
147f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org				  glx_extensions);
148f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   } else {
149f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      xcb_glx_client_info(c,
150f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org			  GLX_MAJOR_VERSION, GLX_MINOR_VERSION,
151f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org			  gl_extension_length,
152f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org			  gl_extension_string);
153f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   }
154f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
155f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   Xfree(gl_extension_string);
156f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org}
157