File: callbacks.c

package info (click to toggle)
screentest 2.0-2.2
  • links: PTS
  • area: main
  • in suites: buster, jessie, jessie-kfreebsd, sid, stretch
  • size: 1,652 kB
  • ctags: 149
  • sloc: sh: 8,940; ansic: 802; makefile: 65
file content (247 lines) | stat: -rw-r--r-- 6,759 bytes parent folder | download | duplicates (4)
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
/*
 *  Screentest - CRT/LCD monitor testing utility.
 *  http://screentest.sourceforge.net/
 *  Copyright (C) 2001 Jan "Yenya" Kasprzak <kas@fi.muni.cz>
 *  Copyright (C) 2006-2007 Tobias Gruetzmacher <tobias@portfolio16.de>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License version 2 as
 *  published by the Free Software Foundation.
 *
 *  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 General Public License
 *  along with this program; 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

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <gtk/gtk.h>
#include <glade/glade.h>

#include "callbacks.h"
#include "main.h"
#include "gettext.h"
#define _(String) gettext(String)

GdkColor fgcolors[COLOR_MAX];
GdkColor *fg_color, *bg_color;
GdkColor grays[GRAYS_MAX];
GdkGC *gc, *backgc;
int fg_count = COLOR_WHITE;

static GtkWidget *mainwin = NULL;
static struct test_ops *current_test = &basic_ops;

void on_mainwin_realize(GtkWidget * widget, G_GNUC_UNUSED gpointer user_data)
{
	gint i;

#ifdef DEBUG
	gdk_window_resize(widget->window, 800, 600);
#else
	gtk_window_fullscreen(GTK_WINDOW(widget));
#endif

	memset(fgcolors, 0, COLOR_MAX * sizeof(GdkColor));

	fgcolors[COLOR_WHITE].red = fgcolors[COLOR_WHITE].green =
		fgcolors[COLOR_WHITE].blue = 65535;

	fgcolors[COLOR_RED].red = 65535;

	fgcolors[COLOR_GREEN].green = 65535;

	fgcolors[COLOR_BLUE].blue = 65535;

	fgcolors[COLOR_CYAN].green = 65535;
	fgcolors[COLOR_CYAN].blue = 65535;

	fgcolors[COLOR_MAGENTA].red = 65535;
	fgcolors[COLOR_MAGENTA].blue = 65535;

	fgcolors[COLOR_YELLOW].red = 65535;
	fgcolors[COLOR_YELLOW].green = 65535;

	/* COLOR_BLACK is 0 already */

	fg_color = gdk_color_copy(&fgcolors[COLOR_WHITE]);
	bg_color = gdk_color_copy(&fgcolors[COLOR_BLACK]);

	for (i = 0; i < GRAYS_MAX; i++)
		grays[i].red = grays[i].green = grays[i].blue = i *
			((1 << 16) - 1) / (GRAYS_MAX - 1);

	mainwin = widget;

	gc = gdk_gc_new(mainwin->window);
	backgc = gdk_gc_new(mainwin->window);

	if (current_test->init != NULL)
		current_test->init(widget);
}

static void update_fg_color(void)
{
	gdk_gc_set_rgb_fg_color(gc, fg_color);
	gdk_gc_set_rgb_bg_color(backgc, fg_color);

	gdk_window_invalidate_rect(mainwin->window, NULL, FALSE);
}

static void update_bg_color(void)
{
	gdk_rgb_find_color(gtk_widget_get_colormap(GTK_WIDGET(mainwin)), bg_color);
	gdk_gc_set_rgb_bg_color(gc, bg_color);
	gdk_gc_set_rgb_fg_color(backgc, bg_color);

	gdk_window_invalidate_rect(mainwin->window, NULL, FALSE);
}

gboolean
on_mainwin_button_press_event(GtkWidget *widget, GdkEventButton *event,
		G_GNUC_UNUSED gpointer user_data)
{
	GtkWidget *popup;

	switch (event->button) {
	case 1:
		if (current_test->cycle != NULL) {
			current_test->cycle(widget);
			gdk_window_invalidate_rect(mainwin->window, NULL, FALSE);
		}
		break;
	case 2:
		if (++fg_count >= COLOR_MAX)
			fg_count = COLOR_WHITE;
		gdk_color_free(fg_color);
		fg_color = gdk_color_copy(&fgcolors[fg_count]);
		update_fg_color();
		break;
	case 3:
		popup = glade_xml_get_widget(glade, "popup");
		gtk_menu_popup(GTK_MENU(popup), NULL, NULL, NULL, NULL,
			       event->button, event->time);
		break;
	}
	return TRUE;
}

/*
 * FIXME: GTK_WINDOW_POPUP does not seem to be able to receive
 * KeyPress events. How to enable this?
 */
gboolean
on_mainwin_key_press_event(G_GNUC_UNUSED GtkWidget *widget,
		G_GNUC_UNUSED GdkEventKey *event,
		G_GNUC_UNUSED gpointer user_data)
{
	gtk_main_quit();
	return FALSE;
}

gboolean
on_mainwin_expose_event(GtkWidget *widget, G_GNUC_UNUSED GdkEventExpose *event,
		G_GNUC_UNUSED gpointer user_data)
{
	gdk_window_set_background(widget->window, bg_color);
	gdk_window_clear(widget->window);

	if (current_test && current_test->draw)
		current_test->draw(widget);
	return TRUE;
}

void on_mode_change(GtkMenuItem *menuitem, G_GNUC_UNUSED gpointer user_data)
{
	GtkCheckMenuItem *checkmenuitem = GTK_CHECK_MENU_ITEM(menuitem);

	if (!checkmenuitem->active) {
		if (current_test->close != NULL) {
			current_test->close(mainwin);
			current_test = NULL;
		}
	} else {
		const char *test_name = glade_get_widget_name(GTK_WIDGET(menuitem));
		g_assert(test_name != NULL);
		GModule *exe = g_module_open(NULL, 0);
		g_assert(exe != NULL);
		gchar *test_struct = g_strconcat(test_name, "_ops", NULL);
		if (g_module_symbol(exe, test_struct, (gpointer) &current_test) != TRUE) {
			GtkWidget *dialog = gtk_message_dialog_new(GTK_WINDOW(mainwin),
				GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
				GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE,
				_("No implementation for the test \"%s\" found."),
				test_name);
			gtk_window_set_title(GTK_WINDOW(dialog), PACKAGE_NAME);
			gtk_dialog_run(GTK_DIALOG(dialog));
			gtk_main_quit();
		}
		free(test_struct);
		g_module_close(exe);
		if (current_test->init != NULL) {
			current_test->init(mainwin);
		}
		on_mainwin_expose_event(mainwin, NULL, NULL);
	}
}

void on_fg_color_activate(G_GNUC_UNUSED GtkMenuItem *menuitem,
		G_GNUC_UNUSED gpointer user_data)
{
	GtkColorSelection *colorsel;
	GtkWidget *fg_color_selector;

	fg_color_selector = glade_xml_get_widget(glade, "fg_color_selector");

	colorsel = GTK_COLOR_SELECTION(GTK_COLOR_SELECTION_DIALOG(fg_color_selector)->colorsel);
	gtk_color_selection_set_current_color(colorsel, fg_color);
	switch (gtk_dialog_run(GTK_DIALOG(fg_color_selector))) {
		case GTK_RESPONSE_OK:
			gtk_color_selection_get_current_color(colorsel,
					fg_color);
			update_fg_color();
			break;
		case GTK_RESPONSE_CANCEL:
		case GTK_RESPONSE_DELETE_EVENT:
			break;
		default:
			g_assert_not_reached();
	}
	gtk_widget_hide(fg_color_selector);
}

void on_bg_color_activate(G_GNUC_UNUSED GtkMenuItem *menuitem,
		G_GNUC_UNUSED gpointer user_data)
{
	GtkColorSelection *colorsel;
	GtkWidget *bg_color_selector;

	bg_color_selector = glade_xml_get_widget(glade, "bg_color_selector");

	colorsel = GTK_COLOR_SELECTION(GTK_COLOR_SELECTION_DIALOG(bg_color_selector)->colorsel);
	gtk_color_selection_set_current_color(colorsel, bg_color);
	switch (gtk_dialog_run(GTK_DIALOG(bg_color_selector))) {
		case GTK_RESPONSE_OK:
			gtk_color_selection_get_current_color(colorsel,
					bg_color);
			update_bg_color();
			break;
		case GTK_RESPONSE_CANCEL:
		case GTK_RESPONSE_DELETE_EVENT:
			break;
		default:
			g_assert_not_reached();
	}
	gtk_widget_hide(bg_color_selector);
}