File: controller.c

package info (click to toggle)
groach 0.4.0-5
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 2,564 kB
  • ctags: 660
  • sloc: sh: 8,754; ansic: 5,377; makefile: 351; sed: 93
file content (430 lines) | stat: -rw-r--r-- 11,501 bytes parent folder | download | duplicates (2)
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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
/*
 * Controller module.
 * Refer to controller.h about details.
 *
 * Copyright INOUE Seiichiro <inoue@ainet.or.jp>, licensed under the GPL.
 */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <signal.h>

#include <gnome.h>
#include "grotype.h"
#include "controller.h"
#include "gromove.h"
#include "inviswin.h"
#include "visregion.h"
#include "guimisc.h"


/* Interval time(milli-second) is defined as
   (controller->interval_base * interval_delays[controller->delay_i]).
   interval_base can be changed by external modules.
   interval_delays will become larger while nothing happens.*/
#define INTERVAL_BASE_DEFAULT	50
static gint interval_delays[] = {1, 2, 3, 0};


/* Data structure definitions */
/* Controller private data */
struct _GroControllerPrivate {
	guint interval_base;/* milli-second */
	gint timer_id;		/* timer id (signed for -1, which implies stopped) */
	guint delay_i;		/* index of interval_delays array */

	/* These two are handled like union, but it's not union.
	   Because when I use invis, actual_widget should be NULL. */
	InvisWin *invis;/* Invisible window, used for detect push on GroMove. */
	GtkWidget *actual_widget;
};


/* Private function declarations */
static void remove_all_gmoves(GroController *controller);
static void remove_gmove(GroController *controller, GroMove *gmove);
static gint timer_cb(gpointer data);
static int button_press_cb(GtkWidget *widget, GdkEventButton *event, gpointer data);


/**
 * gro_controller_new:
 * @window is a window to draw roach.
 * If @actual_widget is NULL, it implies the caller uses root window.
 * In that case, prepare an invisible window to get button press event.
 **/
GroController*
gro_controller_new(GdkWindow *window, GtkWidget *actual_widget)
{
	GroController *controller;
	GroControllerPrivate *privat;

	g_return_val_if_fail(window != NULL, NULL);
	
	controller = g_new(GroController, 1);
	controller->cur_theme = NULL;/* should be attached later */
	controller->gro_win = gro_window_new(window);
	controller->gmove_list = NULL;

	privat = g_new(GroControllerPrivate, 1);
	controller->privat = privat;
	privat->interval_base = INTERVAL_BASE_DEFAULT;
	privat->timer_id = -1;
	privat->delay_i = 0;

	if (actual_widget) {
		privat->invis = NULL;
		privat->actual_widget = actual_widget;
		gtk_widget_add_events(privat->actual_widget, GDK_BUTTON_PRESS_MASK);
		gtk_signal_connect(GTK_OBJECT(privat->actual_widget),
						   "button_press_event",
						   GTK_SIGNAL_FUNC(button_press_cb),
						   controller);
	} else {/* Default, typically root window */
		privat->actual_widget = NULL;
		privat->invis = invis_win_new(controller->gro_win);
		gtk_signal_connect(GTK_OBJECT(privat->invis->invis_w),
						   "button_press_event",
						   GTK_SIGNAL_FUNC(button_press_cb),
						   controller);
	}
	return controller;
}

/**
 * gro_controller_delete:
 * Delete the controller instance.
 **/
void
gro_controller_delete(GroController *controller)
{
	GroControllerPrivate *privat;

	g_return_if_fail(controller != NULL);

	remove_all_gmoves(controller);
	if (controller->cur_theme)
		gro_controller_detach_theme(controller);

	privat = controller->privat;

	if (privat->timer_id != -1) {
		g_warning("the caller should have stopped it.\n");
		gro_controller_stop(controller);
	}

	if (privat->actual_widget) {
		gtk_signal_disconnect_by_func(GTK_OBJECT(privat->actual_widget),
									  GTK_SIGNAL_FUNC(button_press_cb),
									  controller);
	} else {
		gtk_signal_disconnect_by_func(GTK_OBJECT(privat->invis->invis_w),
									  GTK_SIGNAL_FUNC(button_press_cb),
									  controller);
		invis_win_delete(privat->invis);
	}
	
	gro_window_delete(controller->gro_win);

	g_free(privat);
	g_free(controller);
}

/**
 * gro_controller_attach_theme:
 * Attach a theme to the controller. Controller owns the theme.
 **/
void
gro_controller_attach_theme(GroController *controller, GroTheme *theme)
{
	g_return_if_fail(controller != NULL);
	g_return_if_fail(theme != NULL);
	g_return_if_fail(controller->cur_theme == NULL);

	controller->cur_theme = theme;
	gro_theme_ref(controller->cur_theme);/* own it */
	if (theme->theme_init)
		theme->theme_init(controller);
}

/**
 * gro_controller_detach_theme:
 * Implicitly, this might destroy 'theme'.
 * Also remove all gmoves, because they might be dependent on this theme.
 **/
void
gro_controller_detach_theme(GroController *controller)
{
	GroTheme *theme;
	
	g_return_if_fail(controller != NULL);
	g_return_if_fail(controller->cur_theme != NULL);

	theme = controller->cur_theme;
	if (theme->theme_finalize)
		theme->theme_finalize(controller);

	gro_theme_unref(controller->cur_theme);/* leave it */
	controller->cur_theme = NULL;

	remove_all_gmoves(controller);
}

/**
 * gro_controller_run:
 * Timer should be enabled only via this function.
 **/
void
gro_controller_run(GroController *controller)
{
	GroControllerPrivate *privat;

	g_return_if_fail(controller != NULL);
	g_return_if_fail(controller->cur_theme != NULL);
	
	privat = controller->privat;
	g_return_if_fail(privat->timer_id == -1);/* the last timer remains */

	privat->timer_id = gtk_timeout_add(privat->interval_base * interval_delays[privat->delay_i], timer_cb, controller);
}

/**
 * gro_controller_stop:
 * Timer should be disabled only via this function.
 * Timer callback sometimes returns FALSE, and it disables the timer
 * implicitly.
 **/
void
gro_controller_stop(GroController *controller)
{
	GroControllerPrivate *privat;

	g_return_if_fail(controller != NULL);
	/* I accept this, because this is no harm.
	   g_return_if_fail(controller->cur_theme != NULL); */

	privat = controller->privat;
	if (privat->timer_id != -1) {
		gtk_timeout_remove(privat->timer_id);
		privat->timer_id = -1;
	} else {
		g_warning("No timer to stop\n");
	}
}

/**
 * gro_controller_is_running:
 **/
gboolean
gro_controller_is_running(const GroController *controller)
{
	return (controller->privat->timer_id != -1) ? TRUE : FALSE;
}

/**
 * gro_controller_change_interval:
 * It might be safe for the caller to stop timer before calling this.
 * In that case, the caller should call gro_controller_run() to rerun.
 **/
void
gro_controller_change_interval(GroController *controller, guint interval_base)
{
	GroControllerPrivate *privat;

	g_return_if_fail(controller != NULL);

	privat = controller->privat;
	privat->interval_base = interval_base;
}

/**
 * gro_controller_add_gmoves:
 * Before this calling, 'theme' should be attached to this controller.
 **/
void
gro_controller_add_gmoves(GroController *controller, guint num_gmoves)
{
	GroTheme *cur_theme;
	GroControllerPrivate *privat;
	int i, ns, nd;
	
	g_return_if_fail(controller != NULL);
	g_return_if_fail(controller->cur_theme != NULL);

	privat = controller->privat;
	cur_theme = controller->cur_theme;
	for (i = 0; i < num_gmoves; i++) {
		GtkObject *gmove;

		gmove = gro_move_new(cur_theme->num_gstat,
							 cur_theme->num_direct,
							 cur_theme->pix_width, cur_theme->pix_height);
		gtk_object_ref(gmove);
		for (ns = 0; ns < cur_theme->num_gstat; ns++) {
			for (nd = 0; nd < cur_theme->num_direct; nd++) {
				gro_move_set_frames(GRO_MOVE(gmove),
									cur_theme->frames[ns][nd], ns, nd, TRUE);
			}
		}
		if (cur_theme->move_init)
			cur_theme->move_init(controller, GRO_MOVE(gmove));
		controller->gmove_list = g_list_prepend(controller->gmove_list,
												GRO_MOVE(gmove));
	}
}


/* ---The followings are private functions--- */
/**
 * remove_all_gmoves:
 **/
static void
remove_all_gmoves(GroController *controller)
{
	GroTheme *cur_theme = controller->cur_theme;
	GList *node;
	
	for (node = controller->gmove_list; node; node = node->next) {
		GtkObject *gmove = node->data;
		/* This routine is called from gro_controller_delete.
		   Usually, theme has been removed. */
		if (cur_theme && cur_theme->move_finalize)
			cur_theme->move_finalize(controller, GRO_MOVE(gmove));
		gtk_object_unref(gmove);
	}
	g_list_free(controller->gmove_list);
	controller->gmove_list = NULL;
}

/**
 * remove_gmove:
 * Remove one GroMove from contoller.
 **/
static void
remove_gmove(GroController *controller, GroMove *gmove)
{
	GroTheme *cur_theme = controller->cur_theme;

	if (cur_theme && cur_theme->move_finalize)
		cur_theme->move_finalize(controller, gmove);
	gtk_object_unref(GTK_OBJECT(gmove));
	controller->gmove_list = g_list_remove(controller->gmove_list, gmove);
}

/**
 * timer_cb:
 * Do various tasks.
 * At first, remove dead GroMove instances.
 * Next move GroMove instances by calling gro_move_move(), internally emit "move" signal.
 * Finally, take care of invisible window map.
 * Timer interval can be changed, but the controller always has one timer,
 * this routine guarantees it.
 **/
static gint
timer_cb(gpointer data)
{
	GroController *controller = data;
	GroTheme *cur_theme = controller->cur_theme;
	GroControllerPrivate *privat = controller->privat;
	GList *node;
	GdkRegion *vis_region;
	gint num_show = 0;
	gboolean b_changed = FALSE;
	gboolean b_cont_timer = TRUE;/* Return value, TRUE implies not to stop timer */

	g_return_val_if_fail(cur_theme->move_compute, TRUE);

	if (controller->gmove_list == NULL) {
		if (privat->invis)
			invis_win_hide(privat->invis);
		b_cont_timer = FALSE;/* remove timer */
		/* There are no gmoves, send myself SIGHUP signal.
		   Especially for command-line interface,
		   see sighup_handler() in src/cmain.c */
		kill(getpid(), SIGHUP);
		goto DONE;
	}

	/* Remove dead gmove */
	node = controller->gmove_list;
	while (node) {
		GroMove *gmove = node->data;
		node = node->next;
		if (gmove->cur_gstat == GRO_STAT_DEAD) {
			remove_gmove(controller, gmove);
		}
	}

	/* Calculate a new position and move each GroMove */
	vis_region = visible_region_new(controller->gro_win->window);
	for (node = controller->gmove_list; node; node = node->next) {
		GroMove *gmove = node->data;
		GroMoveRet ret;
		GroVector ret_vec;

		num_show++;
		ret = (cur_theme->move_compute)(controller, gmove, vis_region, &ret_vec);
		if (ret == GRO_RET_MOVE) {
			b_changed = TRUE;
			gro_move_move(gmove, controller, &ret_vec);
		} else if (ret == GRO_RET_HIDDEN) {
			num_show--;
		}
	}
	visible_region_delete(vis_region);

	/* Map or unmap invisible window */
	if (privat->invis) {
		if (num_show == 0)
			invis_win_hide(privat->invis);
		else
			invis_win_show(privat->invis);
	}
	
	/* Calculate delay and rerun if necessary */
	if (b_changed == TRUE) {
		if (privat->delay_i != 0) {
			privat->delay_i = 0;
			privat->timer_id = -1;/* set it explicitly */
			b_cont_timer = FALSE;
			gro_controller_run(controller);
		}
	} else {
		if (interval_delays[privat->delay_i + 1] != 0) {
			privat->delay_i++;
			privat->timer_id = -1;/* set it explicitly */
			b_cont_timer = FALSE;
			gro_controller_run(controller);
		}
    }

 DONE:
	return b_cont_timer;
}

/**
 * button_press_cb:
 * If GroMove is pushed, call gro_move_push() for the instance.
 * It internally emits "push" signal, so usually calls "push" event handler,
 * such as default_move_push_cb().
 **/
static int
button_press_cb(GtkWidget *widget, GdkEventButton *event, gpointer data)
{
	GroController *controller = data;
	GList *node;

	/* ignore double click and triple click */
	if (event->type != GDK_BUTTON_PRESS)
		return TRUE;
	
	for (node = controller->gmove_list; node; node = node->next) {
		GroMove *gmove = node->data;
		if (gmove->cur_gstat != GRO_STAT_DEAD
			&& is_rect_point_in(&gmove->cur_rect, event->x, event->y) == TRUE) {
			gro_move_push(gmove, controller, event);
		}
	}

	return TRUE;
}