File: ags_timestamp.c

package info (click to toggle)
gsequencer 8.2.9-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 74,272 kB
  • sloc: ansic: 1,195,333; xml: 31,048; cpp: 9,749; sh: 5,798; makefile: 4,024; perl: 536; sed: 16; python: 11
file content (373 lines) | stat: -rw-r--r-- 7,855 bytes parent folder | download | duplicates (3)
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
/* GSequencer - Advanced GTK Sequencer
 * Copyright (C) 2005-2023 Joël Krähemann
 *
 * This file is part of GSequencer.
 *
 * GSequencer 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 3 of the License, or
 * (at your option) any later version.
 *
 * GSequencer 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 GSequencer.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <ags/thread/ags_timestamp.h>

#include <stdlib.h>

void ags_timestamp_class_init(AgsTimestampClass *timestamp);
void ags_timestamp_init (AgsTimestamp *timestamp);
void ags_timestamp_finalize(GObject *gobject);

/**
 * SECTION:ags_timestamp
 * @short_description: timestamp unix and alike
 * @title: AgsTimestamp
 * @section_id:
 * @include: ags/thread/ags_timestamp.h
 *
 * #AgsTimestamp measure of time.
 */

static gpointer ags_timestamp_parent_class = NULL;

GType
ags_timestamp_get_type (void)
{
  static gsize g_define_type_id__static = 0;

  if(g_once_init_enter(&g_define_type_id__static)){
    GType ags_type_timestamp = 0;

    static const GTypeInfo ags_timestamp_info = {
      sizeof(AgsTimestampClass),
      NULL, /* base_init */
      NULL, /* base_finalize */
      (GClassInitFunc) ags_timestamp_class_init,
      NULL, /* class_finalize */
      NULL, /* class_data */
      sizeof(AgsTimestamp),
      0,    /* n_preallocs */
      (GInstanceInitFunc) ags_timestamp_init,
    };

    ags_type_timestamp = g_type_register_static(G_TYPE_OBJECT,
						"AgsTimestamp",
						&ags_timestamp_info,
						0);

    g_once_init_leave(&g_define_type_id__static, ags_type_timestamp);
  }

  return(g_define_type_id__static);
}

GType
ags_timestamp_flags_get_type()
{
  static gsize g_flags_type_id__static;

  if(g_once_init_enter(&g_flags_type_id__static)){
    static const GFlagsValue values[] = {
      { AGS_TIMESTAMP_UNIX, "AGS_TIMESTAMP_UNIX", "timestamp-unix" },
      { AGS_TIMESTAMP_OFFSET, "AGS_TIMESTAMP_OFFSET", "timestamp-offset" },
      { AGS_TIMESTAMP_OUTDATED, "AGS_TIMESTAMP_OUTDATED", "timestamp-outdated" },
      { 0, NULL, NULL }
    };

    GType g_flags_type_id = g_flags_register_static(g_intern_static_string("AgsTimestampFlags"), values);

    g_once_init_leave(&g_flags_type_id__static, g_flags_type_id);
  }
  
  return(g_flags_type_id__static);
}

void
ags_timestamp_class_init(AgsTimestampClass *timestamp)
{
  GObjectClass *gobject;

  ags_timestamp_parent_class = g_type_class_peek_parent(timestamp);

  /* GObjectClass */
  gobject = (GObjectClass *) timestamp;

  gobject->finalize = ags_timestamp_finalize;
}

void
ags_timestamp_init(AgsTimestamp *timestamp)
{
  timestamp->flags = AGS_TIMESTAMP_UNIX;

  /* add timestamp mutex */
  g_rec_mutex_init(&(timestamp->obj_mutex));

  /* common fields */
  time(&(timestamp->timer.unix_time.time_val));

  timestamp->delay = 0.0;
  timestamp->attack = 0;
}

void
ags_timestamp_finalize(GObject *gobject)
{
  AgsTimestamp *timestamp;

  timestamp = AGS_TIMESTAMP(gobject);

  /* call parent */
  G_OBJECT_CLASS(ags_timestamp_parent_class)->finalize(gobject);
}

/**
 * ags_timestamp_test_flags:
 * @timestamp: the #AgsTimestamp
 * @flags: the flags
 * 
 * Test @flags to be set.
 * 
 * Returns: if @flags set returning %TRUE otherwise %FALSE
 * 
 * Since: 3.0.0
 */
gboolean
ags_timestamp_test_flags(AgsTimestamp *timestamp,
			 AgsTimestampFlags flags)
{
  gboolean success;
  
  GRecMutex *timestamp_mutex;

  if(!AGS_IS_TIMESTAMP(timestamp)){
    return(FALSE);
  }
  
  /* get timestamp mutex */
  timestamp_mutex = AGS_TIMESTAMP_GET_OBJ_MUTEX(timestamp);

  /* test flags */
  g_rec_mutex_lock(timestamp_mutex);

  success = ((flags & (timestamp->flags)) != 0) ? TRUE: FALSE;

  g_rec_mutex_unlock(timestamp_mutex);

  return(success);
}

/**
 * ags_timestamp_set_flags:
 * @timestamp: the #AgsTimestamp
 * @flags: the flags
 * 
 * Set @flags of @timestamp.
 * 
 * Since: 3.0.0
 */
void
ags_timestamp_set_flags(AgsTimestamp *timestamp,
			AgsTimestampFlags flags)
{
  GRecMutex *timestamp_mutex;

  if(!AGS_IS_TIMESTAMP(timestamp)){
    return;
  }
  
  /* get timestamp mutex */
  timestamp_mutex = AGS_TIMESTAMP_GET_OBJ_MUTEX(timestamp);

  /* set flags */
  g_rec_mutex_lock(timestamp_mutex);

  timestamp->flags |= flags;

  g_rec_mutex_unlock(timestamp_mutex);
}

/**
 * ags_timestamp_unset_flags:
 * @timestamp: the #AgsTimestamp
 * @flags: the flags
 * 
 * Unset @flags of @timestamp.
 * 
 * Since: 3.0.0
 */
void
ags_timestamp_unset_flags(AgsTimestamp *timestamp,
			  AgsTimestampFlags flags)
{
  GRecMutex *timestamp_mutex;

  if(!AGS_IS_TIMESTAMP(timestamp)){
    return;
  }
  
  /* get timestamp mutex */
  timestamp_mutex = AGS_TIMESTAMP_GET_OBJ_MUTEX(timestamp);

  /* unset flags */
  g_rec_mutex_lock(timestamp_mutex);

  timestamp->flags &= (~flags);

  g_rec_mutex_unlock(timestamp_mutex);
}

/**
 * ags_timestamp_get_unix_time:
 * @timestamp: the #AgsTimestamp
 * 
 * Get unix time.
 * 
 * Returns: the unix time as time_t value
 * 
 * Since: 3.0.0
 */
time_t
ags_timestamp_get_unix_time(AgsTimestamp *timestamp)
{
  time_t unix_time;

  GRecMutex *timestamp_mutex;

  if(!AGS_IS_TIMESTAMP(timestamp)){
    return(0);
  }
  
  /* get timestamp mutex */
  timestamp_mutex = AGS_TIMESTAMP_GET_OBJ_MUTEX(timestamp);

  /* get ags offset */
  g_rec_mutex_lock(timestamp_mutex);
  
  unix_time = timestamp->timer.unix_time.time_val;

  g_rec_mutex_unlock(timestamp_mutex);
  
  return(unix_time);
}

/**
 * ags_timestamp_set_unix_time:
 * @timestamp: the #AgsTimestamp
 * @unix_time: the unix time value
 * 
 * Set unix time.
 * 
 * Since: 3.0.0
 */
void
ags_timestamp_set_unix_time(AgsTimestamp *timestamp,
			    time_t unix_time)
{
  GRecMutex *timestamp_mutex;

  if(!AGS_IS_TIMESTAMP(timestamp)){
    return;
  }
  
  /* get timestamp mutex */
  timestamp_mutex = AGS_TIMESTAMP_GET_OBJ_MUTEX(timestamp);

  /* get ags offset */
  g_rec_mutex_lock(timestamp_mutex);
  
  timestamp->timer.unix_time.time_val = unix_time;

  g_rec_mutex_unlock(timestamp_mutex);
}

/**
 * ags_timestamp_get_ags_offset:
 * @timestamp: the #AgsTimestamp
 * 
 * Get AGS offset.
 * 
 * Returns: the AGS offset as unsigned 64 bit integer
 * 
 * Since: 3.0.0
 */
guint64
ags_timestamp_get_ags_offset(AgsTimestamp *timestamp)
{
  guint64 ags_offset;

  GRecMutex *timestamp_mutex;

  if(!AGS_IS_TIMESTAMP(timestamp)){
    return(0);
  }
  
  /* get timestamp mutex */
  timestamp_mutex = AGS_TIMESTAMP_GET_OBJ_MUTEX(timestamp);

  /* get ags offset */
  g_rec_mutex_lock(timestamp_mutex);
  
  ags_offset = timestamp->timer.ags_offset.offset;

  g_rec_mutex_unlock(timestamp_mutex);
  
  return(ags_offset);
}

/**
 * ags_timestamp_set_ags_offset:
 * @timestamp: the #AgsTimestamp
 * @ags_offset: the AGS offset
 * 
 * Set AGS offset as unsigned 64 bit integer.
 * 
 * Since: 3.0.0
 */
void
ags_timestamp_set_ags_offset(AgsTimestamp *timestamp,
			     guint64 ags_offset)
{
  GRecMutex *timestamp_mutex;

  if(!AGS_IS_TIMESTAMP(timestamp)){
    return;
  }
  
  /* get timestamp mutex */
  timestamp_mutex = AGS_TIMESTAMP_GET_OBJ_MUTEX(timestamp);

  /* get ags offset */
  g_rec_mutex_lock(timestamp_mutex);
  
  timestamp->timer.ags_offset.offset = ags_offset;

  g_rec_mutex_unlock(timestamp_mutex);
}

/**
 * ags_timestamp_new:
 *
 * Creates an #AgsTimestamp
 *
 * Returns: a new #AgsTimestamp
 *
 * Since: 3.0.0
 */
AgsTimestamp*
ags_timestamp_new()
{
  AgsTimestamp *timestamp;

  timestamp = (AgsTimestamp *) g_object_new(AGS_TYPE_TIMESTAMP,
					    NULL);

  return(timestamp);
}