1848b8605Smrg/*
2848b8605Smrg * Copyright © 2011 Intel Corporation
3848b8605Smrg *
4848b8605Smrg * Permission is hereby granted, free of charge, to any person obtaining a
5848b8605Smrg * copy of this software and associated documentation files (the "Software"),
6848b8605Smrg * to deal in the Software without restriction, including without limitation
7848b8605Smrg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8848b8605Smrg * and/or sell copies of the Software, and to permit persons to whom the
9848b8605Smrg * Software is furnished to do so, subject to the following conditions:
10848b8605Smrg *
11848b8605Smrg * The above copyright notice and this permission notice (including the next
12848b8605Smrg * paragraph) shall be included in all copies or substantial portions of the
13848b8605Smrg * Software.
14848b8605Smrg *
15848b8605Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16848b8605Smrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17848b8605Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18848b8605Smrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19848b8605Smrg * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20848b8605Smrg * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21848b8605Smrg * DEALINGS IN THE SOFTWARE.
22848b8605Smrg */
23848b8605Smrg
24848b8605Smrg#include <string.h>
25848b8605Smrg#include <ctype.h>
26848b8605Smrg
27848b8605Smrg#include "glxclient.h"
28848b8605Smrg#include <xcb/glx.h>
29848b8605Smrg#include <X11/Xlib-xcb.h>
30848b8605Smrg
31848b8605Smrg_X_HIDDEN void
32848b8605Smrg__glX_send_client_info(struct glx_display *glx_dpy)
33848b8605Smrg{
34848b8605Smrg   const unsigned ext_length = strlen("GLX_ARB_create_context");
35848b8605Smrg   const unsigned prof_length = strlen("_profile");
36848b8605Smrg   char *gl_extension_string;
37848b8605Smrg   int gl_extension_length;
38848b8605Smrg   xcb_connection_t *c;
39848b8605Smrg   Bool any_screen_has_ARB_create_context = False;
40848b8605Smrg   Bool any_screen_has_ARB_create_context_profile = False;
41848b8605Smrg   unsigned i;
42848b8605Smrg   static const uint32_t gl_versions[] = {
43848b8605Smrg      1, 4,
44848b8605Smrg   };
45848b8605Smrg   static const uint32_t gl_versions_profiles[] = {
46848b8605Smrg      1, 4, 0x00000000,
47848b8605Smrg   };
48848b8605Smrg   static const char glx_extensions[] =
49848b8605Smrg      "GLX_ARB_create_context GLX_ARB_create_context_profile";
50848b8605Smrg
51848b8605Smrg   /* There are three possible flavors of the client info structure that the
52848b8605Smrg    * client could send to the server.  The version sent depends on the
53848b8605Smrg    * combination of GLX versions and extensions supported by the client and
54848b8605Smrg    * the server.
55848b8605Smrg    *
56848b8605Smrg    * Server supports                  Client sends
57848b8605Smrg    * ----------------------------------------------------------------------
58848b8605Smrg    * GLX version = 1.0                Nothing.
59848b8605Smrg    *
60848b8605Smrg    * GLX version >= 1.1               struct GLXClientInfo
61848b8605Smrg    *
62848b8605Smrg    * GLX version >= 1.4 and
63848b8605Smrg    * GLX_ARB_create_context           struct glXSetClientInfoARB
64848b8605Smrg    *
65848b8605Smrg    * GLX version >= 1.4 and
66848b8605Smrg    * GLX_ARB_create_context_profile   struct glXSetClientInfo2ARB
67848b8605Smrg    *
68848b8605Smrg    * GLX_ARB_create_context and GLX_ARB_create_context_profile use FBConfigs,
69848b8605Smrg    * and these only exist in GLX 1.4 or with GLX_SGIX_fbconfig.  I can't
70848b8605Smrg    * imagine an implementation that supports GLX_SGIX_fbconfig and
71848b8605Smrg    * GLX_ARB_create_context but not GLX 1.4.  Making GLX 1.4 a hard
72848b8605Smrg    * requirement in this case does not seem like a limitation.
73848b8605Smrg    *
74848b8605Smrg    * This library currently only supports struct GLXClientInfo.
75848b8605Smrg    */
76848b8605Smrg
77848b8605Smrg   if (glx_dpy->majorVersion == 1 && glx_dpy->minorVersion == 0)
78848b8605Smrg      return;
79848b8605Smrg
80848b8605Smrg   /* Determine whether any screen on the server supports either of the
81848b8605Smrg    * create-context extensions.
82848b8605Smrg    */
83848b8605Smrg   for (i = 0; i < ScreenCount(glx_dpy->dpy); i++) {
84848b8605Smrg      struct glx_screen *src = glx_dpy->screens[i];
85848b8605Smrg
86848b8605Smrg      const char *haystack = src->serverGLXexts;
87848b8605Smrg      while (haystack != NULL) {
88848b8605Smrg	 char *match = strstr(haystack, "GLX_ARB_create_context");
89848b8605Smrg
90848b8605Smrg	 if (match == NULL)
91848b8605Smrg	    break;
92848b8605Smrg
93848b8605Smrg	 match += ext_length;
94848b8605Smrg
95848b8605Smrg	 switch (match[0]) {
96848b8605Smrg	 case '\0':
97848b8605Smrg	 case ' ':
98848b8605Smrg	    any_screen_has_ARB_create_context = True;
99848b8605Smrg	    break;
100848b8605Smrg
101848b8605Smrg	 case '_':
102848b8605Smrg	    if (strncmp(match, "_profile", prof_length) == 0
103848b8605Smrg		    && (match[prof_length] == '\0'
104848b8605Smrg			|| match[prof_length] == ' ')) {
105848b8605Smrg	       any_screen_has_ARB_create_context_profile = True;
106848b8605Smrg	       match += prof_length;
107848b8605Smrg	    }
108848b8605Smrg	    break;
109848b8605Smrg	 }
110848b8605Smrg
111848b8605Smrg	 haystack = match;
112848b8605Smrg      }
113848b8605Smrg   }
114848b8605Smrg
115848b8605Smrg   gl_extension_string = __glXGetClientGLExtensionString();
116848b8605Smrg   if (gl_extension_string == NULL) {
117848b8605Smrg      return;
118848b8605Smrg   }
119848b8605Smrg
120848b8605Smrg   gl_extension_length = strlen(gl_extension_string) + 1;
121848b8605Smrg
122848b8605Smrg   c = XGetXCBConnection(glx_dpy->dpy);
123848b8605Smrg
124848b8605Smrg   /* Depending on the GLX verion and the available extensions on the server,
125848b8605Smrg    * send the correct "flavor" of protocol to the server.
126848b8605Smrg    *
127848b8605Smrg    * THE ORDER IS IMPORTANT.  We want to send the most recent version of the
128848b8605Smrg    * protocol that the server can support.
129848b8605Smrg    */
130848b8605Smrg   if (glx_dpy->majorVersion == 1 && glx_dpy->minorVersion == 4
131848b8605Smrg       && any_screen_has_ARB_create_context_profile) {
132848b8605Smrg      xcb_glx_set_client_info_2arb(c,
133848b8605Smrg				  GLX_MAJOR_VERSION, GLX_MINOR_VERSION,
134848b8605Smrg				   sizeof(gl_versions_profiles)
135848b8605Smrg				   / (3 * sizeof(gl_versions_profiles[0])),
136848b8605Smrg				  gl_extension_length,
137848b8605Smrg				  strlen(glx_extensions) + 1,
138848b8605Smrg				  gl_versions_profiles,
139848b8605Smrg				  gl_extension_string,
140848b8605Smrg				  glx_extensions);
141848b8605Smrg   } else if (glx_dpy->majorVersion == 1 && glx_dpy->minorVersion == 4
142848b8605Smrg	      && any_screen_has_ARB_create_context) {
143848b8605Smrg      xcb_glx_set_client_info_arb(c,
144848b8605Smrg				  GLX_MAJOR_VERSION, GLX_MINOR_VERSION,
145848b8605Smrg				  sizeof(gl_versions)
146848b8605Smrg				  / (2 * sizeof(gl_versions[0])),
147848b8605Smrg				  gl_extension_length,
148848b8605Smrg				  strlen(glx_extensions) + 1,
149848b8605Smrg				  gl_versions,
150848b8605Smrg				  gl_extension_string,
151848b8605Smrg				  glx_extensions);
152848b8605Smrg   } else {
153848b8605Smrg      xcb_glx_client_info(c,
154848b8605Smrg			  GLX_MAJOR_VERSION, GLX_MINOR_VERSION,
155848b8605Smrg			  gl_extension_length,
156848b8605Smrg			  gl_extension_string);
157848b8605Smrg   }
158848b8605Smrg
159848b8605Smrg   free(gl_extension_string);
160848b8605Smrg}
161