summaryrefslogtreecommitdiff
path: root/plugins/gtkui/gtkglext-gtk3/gtk/gtkglinit.c
blob: ba658094eb470f20df26b76bc5ad5de07d803a51 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/* GtkGLExt - OpenGL Extension to GTK+
 * Copyright (C) 2002-2004  Naofumi Yasufuku
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA.
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif /* HAVE_CONFIG_H */

#include <string.h>

#include "gtkglprivate.h"
#include "gtkglinit.h"

static gboolean gtk_gl_initialized = FALSE;

guint gtk_gl_debug_flags = 0;   /* Global GtkGLExt debug flag */

#ifdef G_ENABLE_DEBUG

static const GDebugKey gtk_gl_debug_keys[] = {
  {"misc", GTK_GL_DEBUG_MISC},
  {"func", GTK_GL_DEBUG_FUNC}
};

static const guint gtk_gl_ndebug_keys = G_N_ELEMENTS (gtk_gl_debug_keys);

#endif /* G_ENABLE_DEBUG */

/**
 * gtk_gl_parse_args:
 * @argc: the number of command line arguments.
 * @argv: the array of command line arguments.
 *
 * Parses command line arguments, and initializes global
 * attributes of GtkGLExt.
 *
 * Any arguments used by GtkGLExt are removed from the array and
 * @argc and @argv are updated accordingly.
 *
 * You shouldn't call this function explicitely if you are using
 * gtk_gl_init(), or gtk_gl_init_check().
 **/
static void
gtk_gl_parse_args (int    *argc,
                   char ***argv)
{
  const gchar *env_string;

  if (gtk_gl_initialized)
    return;

  /*
   * If window manager doesn't watch the WM_COLORMAP_WINDOWS property on
   * the top-level window, we have to set OpenGL window's colormap to the
   * top-level window, especially in color index mode (color index mode
   * uses own private colormap). This is achieved by setting the window's
   * visual.
   */
  env_string = g_getenv ("GTK_GL_INSTALL_VISUAL");
  if (env_string != NULL)
    {
      _gtk_gl_widget_install_toplevel_visual = !!g_ascii_strtoll (env_string, NULL, 0);
      env_string = NULL;
    }

#ifdef G_ENABLE_DEBUG
  env_string = g_getenv ("GTK_GL_DEBUG");
  if (env_string != NULL)
    {
      gtk_gl_debug_flags = g_parse_debug_string (env_string,
                                                 gtk_gl_debug_keys,
                                                 gtk_gl_ndebug_keys);
      env_string = NULL;
    }
#endif	/* G_ENABLE_DEBUG */

  if (argc && argv)
    {
      gint i, j, k;

      for (i = 1; i < *argc;)
	{
          if (strcmp ("--gtk-gl-install-visual", (*argv)[i]) == 0)
            {
              _gtk_gl_widget_install_toplevel_visual = TRUE;
              (*argv)[i] = NULL;
            }
#ifdef G_ENABLE_DEBUG
          else if ((strcmp ("--gtk-gl-debug", (*argv)[i]) == 0) ||
                   (strncmp ("--gtk-gl-debug=", (*argv)[i], 15) == 0))
	    {
	      gchar *equal_pos = strchr ((*argv)[i], '=');

	      if (equal_pos != NULL)
		{
		  gtk_gl_debug_flags |= g_parse_debug_string (equal_pos+1,
                                                              gtk_gl_debug_keys,
                                                              gtk_gl_ndebug_keys);
		}
	      else if ((i + 1) < *argc && (*argv)[i + 1])
		{
		  gtk_gl_debug_flags |= g_parse_debug_string ((*argv)[i+1],
                                                              gtk_gl_debug_keys,
                                                              gtk_gl_ndebug_keys);
		  (*argv)[i] = NULL;
		  i += 1;
		}
	      (*argv)[i] = NULL;
	    }
	  else if ((strcmp ("--gtk-gl-no-debug", (*argv)[i]) == 0) ||
		   (strncmp ("--gtk-gl-no-debug=", (*argv)[i], 18) == 0))
	    {
	      gchar *equal_pos = strchr ((*argv)[i], '=');

	      if (equal_pos != NULL)
		{
		  gtk_gl_debug_flags &= ~g_parse_debug_string (equal_pos+1,
                                                               gtk_gl_debug_keys,
                                                               gtk_gl_ndebug_keys);
		}
	      else if ((i + 1) < *argc && (*argv)[i + 1])
		{
		  gtk_gl_debug_flags &= ~g_parse_debug_string ((*argv)[i+1],
                                                               gtk_gl_debug_keys,
                                                               gtk_gl_ndebug_keys);
		  (*argv)[i] = NULL;
		  i += 1;
		}
	      (*argv)[i] = NULL;
	    }
#endif /* G_ENABLE_DEBUG */
	  i += 1;
	}

      for (i = 1; i < *argc; i++)
	{
	  for (k = i; k < *argc; k++)
	    if ((*argv)[k] != NULL)
	      break;

	  if (k > i)
	    {
	      k -= i;
	      for (j = i + k; j < *argc; j++)
		(*argv)[j-k] = (*argv)[j];
	      *argc -= k;
	    }
	}

    }

  /* Set the 'initialized' flag. */
  gtk_gl_initialized = TRUE;
}

/**
 * gtk_gl_init_check:
 * @argc: Address of the <parameter>argc</parameter> parameter of your
 *        <function>main()</function> function. Changed if any arguments
 *        were handled.
 * @argv: Address of the <parameter>argv</parameter> parameter of
 *        <function>main()</function>. Any parameters understood by
 *        gtk_gl_init() are stripped before return.
 *
 * This function does the same work as gtk_gl_init() with only
 * a single change: It does not terminate the program if the library can't be
 * initialized. Instead it returns %FALSE on failure.
 *
 * This way the application can fall back to some other means of communication
 * with the user - for example a curses or command line interface.
 *
 * Return value: %TRUE if the GUI has been successfully initialized,
 *               %FALSE otherwise.
 **/
gboolean
gtk_gl_init_check (int    *argc,
                   char ***argv)
{
  /* Init GdkGLExt library. */
  if (!gdk_gl_init_check (argc, argv))
    return FALSE;

  /* Parse args and init GtkGLExt library. */
  gtk_gl_parse_args (argc, argv);

  return TRUE;
}

/**
 * gtk_gl_init:
 * @argc: Address of the <parameter>argc</parameter> parameter of your
 *        <function>main()</function> function. Changed if any arguments
 *        were handled.
 * @argv: Address of the <parameter>argv</parameter> parameter of
 *        <function>main()</function>. Any parameters understood by
 *        gtk_gl_init() are stripped before return.
 *
 * Call this function before using any other GtkGLExt functions in your
 * applications.  It will initialize everything needed to operate the library
 * and parses some standard command line options. @argc and
 * @argv are adjusted accordingly so your own code will
 * never see those standard arguments.
 *
 * <note><para>
 * This function will terminate your program if it was unable to initialize
 * the library for some reason. If you want your program to fall back to a
 * textual interface you want to call gtk_gl_init_check() instead.
 * </para></note>
 **/
void
gtk_gl_init (int    *argc,
             char ***argv)
{
  if (!gtk_gl_init_check (argc, argv))
    g_error ("GdkGLExt library initialization fails.");
}