File: clock.c

package info (click to toggle)
nip2 8.9.1-2
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 23,404 kB
  • sloc: ansic: 64,076; sh: 4,681; yacc: 1,133; makefile: 927; lex: 386; xml: 40; perl: 17
file content (276 lines) | stat: -rw-r--r-- 7,129 bytes parent folder | download | duplicates (7)
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
/* a clock ... triggers a recomp every whenever
 */

/*

    Copyright (C) 1991-2003 The National Gallery

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU 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 General Public License along
    with this program; if not, write to the Free Software Foundation, Inc.,
    51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

 */

/*

    These files are distributed with VIPS - http://www.vips.ecs.soton.ac.uk

 */

/*
#define DEBUG
 */

#include "ip.h"

static ValueClass *parent_class = NULL;

static void
clock_dispose( GObject *gobject )
{
	Clock *clock = CLOCK( gobject );

#ifdef DEBUG
	printf( "clock_dispose\n" );
#endif /*DEBUG*/

	IM_FREEF( g_source_remove, clock->recalc_timeout );
	IM_FREEF( g_timer_destroy, clock->elapsed_timer );

	G_OBJECT_CLASS( parent_class )->dispose( gobject );
}

static void
clock_done_cb( iWindow *iwnd, void *client, iWindowNotifyFn nfn, void *sys )
{
	Clock *clock = CLOCK( client );
	Stringset *ss = STRINGSET( iwnd );

	StringsetChild *interval = stringset_child_get( ss, _( "Interval" ) );
	StringsetChild *value = stringset_child_get( ss, _( "Elapsed time" ) );

	if( !get_geditable_double( interval->entry, &clock->interval ) ||
		!get_geditable_double( value->entry, &clock->value ) ) {
		nfn( sys, IWINDOW_ERROR );
		return;
	}

	if( clock->interval < 0.1 )
		clock->interval = 0.1;
	if( clock->value < 0.0 )
		clock->value = 0.0;

	/* Magic: ask for the clock timer to be reset.
	 */
	clock->time_offset = -1;

	classmodel_update( CLASSMODEL( clock ) );
	symbol_recalculate_all();

	nfn( sys, IWINDOW_YES );
}

static void 
clock_edit( GtkWidget *parent, Model *model )
{
	Clock *clock = CLOCK( model );
	GtkWidget *ss = stringset_new();
	char txt[256];

	im_snprintf( txt, 256, "%g", clock->interval );
	stringset_child_new( STRINGSET( ss ), 
		_( "Interval" ), txt, _( "Interval between ticks (seconds)" ) );
	im_snprintf( txt, 256, "%g", clock->value );
	stringset_child_new( STRINGSET( ss ), 
		_( "Elapsed time" ), txt, _( "Elapsed time (seconds)" ) );

	/* Expands to eg. "Edit Toggle A1".
	 */
	iwindow_set_title( IWINDOW( ss ), _( "Edit %s %s" ),
		IOBJECT_GET_CLASS_NAME( model ),
		IOBJECT( HEAPMODEL( model )->row )->name );
	idialog_set_callbacks( IDIALOG( ss ), 
		iwindow_true_cb, NULL, NULL, clock );
	idialog_add_ok( IDIALOG( ss ), 
		clock_done_cb, _( "Set %s" ), IOBJECT_GET_CLASS_NAME( model ) );
	iwindow_set_parent( IWINDOW( ss ), GTK_WIDGET( parent ) );
	idialog_set_iobject( IDIALOG( ss ), IOBJECT( model ) );
	iwindow_build( IWINDOW( ss ) );

	gtk_widget_show( ss );
}

static gboolean
clock_timeout_cb( Clock *clock )
{
#ifdef DEBUG
	printf( "clock_timeout_cb: " );
	row_name_print( HEAPMODEL( clock )->row );
	printf( " interval=%g, value=%g\n", clock->interval, clock->value );
#endif /*DEBUG*/

	/* Test autocalc ... if it's off, make sure we don't update the
	 * interface.
	 */
	if( mainw_auto_recalc ) {
		clock->value = g_timer_elapsed( clock->elapsed_timer, NULL ) + 
			clock->time_offset;

		classmodel_update( CLASSMODEL( clock ) );

		symbol_recalculate_all();
	}

	return( TRUE );
}

static void *
clock_update_model( Heapmodel *heapmodel )
{
	Clock *clock = CLOCK( heapmodel );

#ifdef DEBUG
	printf( "clock_update_model: " );
	row_name_print( HEAPMODEL( clock )->row );
	printf( " interval=%g, value=%g\n", clock->interval, clock->value );
#endif /*DEBUG*/

	if( HEAPMODEL_CLASS( parent_class )->update_model( heapmodel ) )
		return( heapmodel );

	/* Milliseconds for the update timeout ... don't let it go under 100,
	 * there's a danger the interface will lock up.
	 */
	int ms = IM_MAX( 100, clock->interval * 1000 );
	IM_FREEF( g_source_remove, clock->recalc_timeout );
	clock->recalc_timeout = g_timeout_add( ms, 
		(GSourceFunc) clock_timeout_cb, clock );

	/* Should we reset the timer from the value?
	 */
	if( clock->time_offset == -1 ) {
		g_timer_start( clock->elapsed_timer );
		clock->time_offset = clock->value;
	}

	return( NULL );
}

/* Override value_generate_caption(): pick from the model.
 */
static const char *
clock_generate_caption( iObject *iobject )
{
	Value *value = VALUE( iobject );
	ValueClass *value_class = VALUE_GET_CLASS( value );
	Clock *clock = CLOCK( iobject );
	VipsBuf *buf = &value->caption_buffer;

	vips_buf_rewind( buf );
	if( !heapmodel_name( HEAPMODEL( value ), buf ) ) 
		vips_buf_appends( buf, G_OBJECT_CLASS_NAME( value_class ) );
	vips_buf_appendf( buf, " %g %g", clock->interval, clock->value );

	return( vips_buf_all( buf ) );
}

/* Members of clock we automate.
 */
static ClassmodelMember clock_members[] = {
	{ CLASSMODEL_MEMBER_DOUBLE, NULL, 0,
		MEMBER_INTERVAL, "interval", N_( "Interval" ),
		G_STRUCT_OFFSET( Clock, interval ) },
	{ CLASSMODEL_MEMBER_DOUBLE, NULL, 0,
		MEMBER_VALUE, "value", N_( "Value" ),
		G_STRUCT_OFFSET( Clock, value ) }
};

static void
clock_class_init( ClockClass *class )
{
	GObjectClass *gobject_class = (GObjectClass *) class;
	iObjectClass *iobject_class = (iObjectClass *) class;
	ModelClass *model_class = (ModelClass *) class;
	HeapmodelClass *heapmodel_class = (HeapmodelClass *) class;
	ClassmodelClass *classmodel_class = (ClassmodelClass *) class;

	parent_class = g_type_class_peek_parent( class );

	/* Create signals.
	 */

	/* Init methods.
	 */
	gobject_class->dispose = clock_dispose;

	iobject_class->user_name = _( "Clock" );
	iobject_class->generate_caption = clock_generate_caption;

	model_class->edit = clock_edit;

	heapmodel_class->update_model = clock_update_model;

	/* Static init.
	 */
	model_register_loadable( MODEL_CLASS( class ) );

	classmodel_class->members = clock_members;
	classmodel_class->n_members = IM_NUMBER( clock_members );
}

static void
clock_init( Clock *clock )
{
#ifdef DEBUG
	printf( "clock_init\n" );
#endif /*DEBUG*/

	/* Overridden later. Just something sensible.
	 */
        clock->interval = 1;
        clock->value = 0;

	/* time_offset: set to -1 means we should set the offset from value on
	 * the next rebuild.
	 */
	clock->elapsed_timer = g_timer_new();
	clock->time_offset = -1;	
        clock->recalc_timeout = 0;

	iobject_set( IOBJECT( clock ), CLASS_CLOCK, NULL );
}

GType
clock_get_type( void )
{
	static GType type = 0;

	if( !type ) {
		static const GTypeInfo info = {
			sizeof( ClockClass ),
			NULL,           /* base_init */
			NULL,           /* base_finalize */
			(GClassInitFunc) clock_class_init,
			NULL,           /* class_finalize */
			NULL,           /* class_data */
			sizeof( Clock ),
			32,             /* n_preallocs */
			(GInstanceInitFunc) clock_init,
		};

		type = g_type_register_static( TYPE_VALUE, 
			"Clock", &info, 0 );
	}

	return( type );
}