summaryrefslogtreecommitdiff
path: root/src/vtegl.c
blob: 48614915b06c15e217833d6e0b69793e7f40eb11 (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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
/*
 * Copyright (C) 2003 Red Hat, Inc.
 *
 * This is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Library General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program 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
 * General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include "../config.h"

#ifdef HAVE_GL

#include <sys/param.h>
#include <stdio.h>
#include <string.h>
#include <gtk/gtk.h>
#include <gdk/gdkx.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glx.h>
#include <X11/Xutil.h>
#include "debug.h"
#include "vtedraw.h"
#include "vtegl.h"

struct _vte_gl_data
{
	XVisualInfo *visual_info;
	gboolean double_buffered;
	GLXContext context;
	GLXPixmap pixmap;
	GdkColor color;
	GdkPixbuf *pixbuf;
	gint scrollx, scrolly;
};

#define _vte_gl_pixmap_attributes \
	GLX_RGBA, \
	GLX_RED_SIZE, 8, \
	GLX_BLUE_SIZE, 8, \
	GLX_GREEN_SIZE, 8, \
	GLX_ALPHA_SIZE, 8, \
	None,
#define _vte_gl_window_attributes \
	GLX_RGBA, \
	GLX_RED_SIZE, 8, \
	GLX_BLUE_SIZE, 8, \
	GLX_GREEN_SIZE, 8, \
	GLX_ALPHA_SIZE, 8, \
	GLX_DOUBLEBUFFER, \
	None,

static gboolean
_vte_gl_check(struct _vte_draw *draw, GtkWidget *widget)
{
	int window_attributes[] = {_vte_gl_window_attributes};
	int pixmap_attributes[] = {_vte_gl_pixmap_attributes};
	XVisualInfo *visual_info;
	GLXContext context = NULL;
	GdkDisplay *gdisplay;
	Display *display;
	GdkScreen *gscreen;
	int screen;
	int error, event;

	gdisplay = gdk_display_get_default();
	display = gdk_x11_display_get_xdisplay(gdisplay);
	gscreen = gdk_screen_get_default();
	screen = gdk_x11_screen_get_screen_number(gscreen);

	/* Check for GLX. */
	if (!glXQueryExtension(display, &error, &event)) {
		g_warning("Unable to use GLX.\n");
		return FALSE;
	}

	/* See if a suitable visual exists. */
	visual_info = glXChooseVisual(display, screen,
				      pixmap_attributes);
	if (visual_info == NULL) {
		visual_info = glXChooseVisual(display, screen,
					      window_attributes);
	}
	if (visual_info == NULL) {
		return FALSE;
	}

	/* Create a GLX context. */
	context = glXCreateContext(display, visual_info, NULL, False);
	if (context == NULL) {
		return FALSE;
	}

	return TRUE;
}

static void
_vte_gl_create(struct _vte_draw *draw, GtkWidget *widget)
{
	struct _vte_gl_data *data;
	int window_attributes[] = {_vte_gl_window_attributes};
	int pixmap_attributes[] = {_vte_gl_pixmap_attributes};
	GdkDisplay *gdisplay;
	Display *display;
	GdkScreen *gscreen;
	int screen;

	draw->impl_data = g_malloc(sizeof(struct _vte_gl_data));
	data = (struct _vte_gl_data*) draw->impl_data;

	gdisplay = gdk_display_get_default();
	display = gdk_x11_display_get_xdisplay(gdisplay);
	gscreen = gdk_screen_get_default();
	screen = gdk_x11_screen_get_screen_number(gscreen);

	data->visual_info = glXChooseVisual(display, screen,
					    pixmap_attributes);
	if (data->visual_info != NULL) {
		data->double_buffered = FALSE;
	} else {
		data->visual_info = glXChooseVisual(display, screen,
						    window_attributes);
		data->double_buffered = TRUE;
	}
	gtk_widget_set_double_buffered(widget, !data->double_buffered);

	data->context = glXCreateContext(display, data->visual_info,
					 NULL, False);
	if (data->context == NULL) {
		g_error("Unable to create GLX context.\n");
	}

	data->color.red = 0;
	data->color.green = 0;
	data->color.blue = 0;
	data->pixbuf = NULL;
	data->pixmap = -1;
	data->scrollx = data->scrolly = 0;
}

static void
_vte_gl_destroy(struct _vte_draw *draw)
{
	struct _vte_gl_data *data;
	GdkDisplay *gdisplay;
	Display *display;
	GdkScreen *gscreen;
	int screen;

	data = (struct _vte_gl_data*) draw->impl_data;
	gdisplay = gdk_display_get_default();
	display = gdk_x11_display_get_xdisplay(gdisplay);
	gscreen = gdk_screen_get_default();
	screen = gdk_x11_screen_get_screen_number(gscreen);

	if (data->pixmap != -1) {
		glXDestroyGLXPixmap(display, data->pixmap);
	}
	glXDestroyContext(display, data->context);

	data->scrollx = data->scrolly = 0;

	if (GDK_IS_PIXBUF(data->pixbuf)) {
		g_object_unref(G_OBJECT(data->pixbuf));
	}
	data->pixbuf = NULL;
	memset(&data->color, 0, sizeof(data->color));

	glXDestroyContext(display, data->context);

	g_free(draw->impl_data);
}

static GdkVisual *
_vte_gl_get_visual(struct _vte_draw *draw)
{
	GdkScreen *gscreen;
	struct _vte_gl_data *data;

	data = (struct _vte_gl_data*) draw->impl_data;
	gscreen = gdk_screen_get_default();
	return gdk_x11_screen_lookup_visual(gscreen,
					    data->visual_info->visualid);
}

static GdkColormap *
_vte_gl_get_colormap(struct _vte_draw *draw)
{
	return NULL;
}

static void
_vte_gl_start(struct _vte_draw *draw)
{
	struct _vte_gl_data *data;
	GdkDisplay *gdisplay;
	Display *display;
	GdkScreen *gscreen;
	int screen;
	guint width, height;
	GdkDrawable *drawable;
	gint x_offset, y_offset;
	GLXDrawable glx_drawable = -1;

	data = (struct _vte_gl_data*) draw->impl_data;
	gdisplay = gdk_display_get_default();
	display = gdk_x11_display_get_xdisplay(gdisplay);
	gscreen = gdk_screen_get_default();
	screen = gdk_x11_screen_get_screen_number(gscreen);

	gdk_window_get_internal_paint_info(draw->widget->window,
					   &drawable,
					   &x_offset,
					   &y_offset);
	if (GDK_IS_PIXMAP(drawable)) {
		gdk_drawable_get_size(drawable, &width, &height);
		data->pixmap = glXCreateGLXPixmap(display, data->visual_info,
						  GDK_WINDOW_XID(drawable));
		glx_drawable = data->pixmap;
	} else
	if (GDK_IS_WINDOW(draw->widget->window)) {
		gdk_drawable_get_size(draw->widget->window, &width, &height);
		x_offset = y_offset = 0;
		glx_drawable = GDK_WINDOW_XID(drawable);
	} else {
		g_assert_not_reached();
	}

	glXMakeCurrent(display, glx_drawable, data->context);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
        gluOrtho2D(x_offset, width, y_offset, height);
        glViewport(x_offset, y_offset, width - x_offset, height - y_offset);

	glClearColor(data->color.red / 65535.0,
		     data->color.green / 65535.0,
		     data->color.blue / 65535.0,
		     1.0);
	glClear(GL_COLOR_BUFFER_BIT);
}

static void
_vte_gl_end(struct _vte_draw *draw)
{
	struct _vte_gl_data *data;
	GdkDisplay *gdisplay;
	Display *display;
	GdkScreen *gscreen;
	int screen;

	data = (struct _vte_gl_data*) draw->impl_data;
	gdisplay = gdk_display_get_default();
	display = gdk_x11_display_get_xdisplay(gdisplay);
	gscreen = gdk_screen_get_default();
	screen = gdk_x11_screen_get_screen_number(gscreen);

	glFlush();
	if (data->double_buffered) {
		glXSwapBuffers(display, GDK_WINDOW_XID(draw->widget->window));
	} else {
		glXDestroyGLXPixmap(display, data->pixmap);
		data->pixmap = -1;
	}
}

static void
_vte_gl_set_background_color(struct _vte_draw *draw, GdkColor *color)
{
	struct _vte_gl_data *data;
	data = (struct _vte_gl_data*) draw->impl_data;
	data->color = *color;
}

static void
_vte_gl_set_background_pixbuf(struct _vte_draw *draw, GdkPixbuf *pixbuf)
{
	struct _vte_gl_data *data;

	data = (struct _vte_gl_data*) draw->impl_data;
	if (GDK_IS_PIXBUF(pixbuf)) {
		g_object_ref(G_OBJECT(pixbuf));
	}
	if (GDK_IS_PIXBUF(data->pixbuf)) {
		g_object_unref(G_OBJECT(data->pixbuf));
	}
	data->pixbuf = pixbuf;
}

static void
_vte_gl_clear(struct _vte_draw *draw,
		 gint x, gint y, gint width, gint height)
{
	struct _vte_gl_data *data;
	data = (struct _vte_gl_data*) draw->impl_data;

	glBegin(GL_POLYGON);
	glColor4us(data->color.red, data->color.green, data->color.blue,
		   0xffff);
	glVertex2d(x, y);
	glVertex2d(x + width, y);
	glVertex2d(x + width, y + height);
	glVertex2d(x, y + height);
	glEnd();
}

static void
_vte_gl_set_text_font(struct _vte_draw *draw,
			 const PangoFontDescription *fontdesc)
{
}

static int
_vte_gl_get_text_width(struct _vte_draw *draw)
{
	return 5;
}

static int
_vte_gl_get_text_height(struct _vte_draw *draw)
{
	return 10;
}

static int
_vte_gl_get_text_ascent(struct _vte_draw *draw)
{
	return 8;
}

static void
_vte_gl_draw_text(struct _vte_draw *draw,
		  struct _vte_draw_text_request *requests, gsize n_requests,
		  GdkColor *color, guchar alpha)
{
	struct _vte_gl_data *data;
	data = (struct _vte_gl_data*) draw->impl_data;
}

static void
_vte_gl_draw_rectangle(struct _vte_draw *draw,
		       gint x, gint y, gint width, gint height,
		       GdkColor *color, guchar alpha)
{
	struct _vte_gl_data *data;
	data = (struct _vte_gl_data*) draw->impl_data;
	glBegin(GL_LINE_LOOP);
	glColor4us(color->red, color->green, color->blue,
		   (alpha == VTE_DRAW_OPAQUE) ? 0xffff : (alpha << 8));
	glVertex2d(x, y);
	glVertex2d(x + width, y);
	glVertex2d(x + width, y + height);
	glVertex2d(x, y + height);
	glEnd();
}

static void
_vte_gl_fill_rectangle(struct _vte_draw *draw,
		       gint x, gint y, gint width, gint height,
		       GdkColor *color, guchar alpha)
{
	struct _vte_gl_data *data;
	data = (struct _vte_gl_data*) draw->impl_data;
	glBegin(GL_POLYGON);
	glColor4us(color->red, color->green, color->blue,
		   (alpha == VTE_DRAW_OPAQUE) ? 0xffff : (alpha << 8));
	glVertex2d(x, y);
	glVertex2d(x + width, y);
	glVertex2d(x + width, y + height);
	glVertex2d(x, y + height);
	glEnd();
}

static void
_vte_gl_set_scroll(struct _vte_draw *draw, gint x, gint y)
{
	struct _vte_gl_data *data;
	data = (struct _vte_gl_data*) draw->impl_data;
	data->scrollx = x;
	data->scrolly = y;
}

struct _vte_draw_impl _vte_draw_gl = {
	"VteGL", "VTE_USE_GL",
	_vte_gl_check,
	_vte_gl_create,
	_vte_gl_destroy,
	_vte_gl_get_visual,
	_vte_gl_get_colormap,
	_vte_gl_start,
	_vte_gl_end,
	_vte_gl_set_background_color,
	_vte_gl_set_background_pixbuf,
	_vte_gl_clear,
	_vte_gl_set_text_font,
	_vte_gl_get_text_width,
	_vte_gl_get_text_height,
	_vte_gl_get_text_ascent,
	_vte_gl_draw_text,
	_vte_gl_draw_rectangle,
	_vte_gl_fill_rectangle,
	_vte_gl_set_scroll,
};

#endif