root/sweep/trunk/src/timeouts.c

Revision 701, 3.6 kB (checked in by erikd, 2 years ago)

Replace deprecated gtk_timeout_add/remove functions with their replacements.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1 /*
2  * Sweep, a sound wave editor.
3  *
4  * Copyright (C) 2000 Conrad Parker
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19  */
20
21 #ifdef HAVE_CONFIG_H
22 #  include <config.h>
23 #endif
24
25 #include <stdio.h>
26 #include <string.h>
27 #include <errno.h>
28 #include <glib.h>
29 #include <gdk/gdkkeysyms.h>
30 #include <gtk/gtk.h>
31
32 #include <sweep/sweep_i18n.h>
33 #include <sweep/sweep_types.h>
34 #include <sweep/sweep_sample.h>
35
36 static GMutex * timeouts_mutex = NULL;
37
38 static GList * timeouts = NULL;
39
40 static guint sweep_tag = 0;
41
42 typedef struct {
43   guint sweep_tag;
44   guint gtk_tag;
45   guint32 interval;
46   GtkFunction function;
47   gpointer data;
48 } sweep_timeout_data;
49
50 static gint
51 sweep_timeout_wrapper (gpointer data)
52 {
53   sweep_timeout_data * td = (sweep_timeout_data *)data;
54
55   if (td->function (td->data)) {
56     return TRUE;
57   } else {
58     g_mutex_lock (timeouts_mutex);
59     timeouts = g_list_remove (timeouts, td);
60     g_mutex_unlock (timeouts_mutex);
61    
62     g_free (td);
63
64     return FALSE;
65   }
66 }
67
68 static gint
69 sweep_timeouts_handle_pending (gpointer data)
70 {
71   GList * gl, * gl_next;
72   sweep_timeout_data * td;
73
74   g_mutex_lock (timeouts_mutex);
75
76   for (gl = timeouts; gl; gl = gl_next) {
77     td = (sweep_timeout_data *)gl->data;
78
79     gl_next = gl->next;
80
81     if (td->sweep_tag == -1) {
82       g_assert (td->gtk_tag != -1);
83
84       g_source_remove (td->gtk_tag);
85       timeouts = g_list_remove_link (timeouts, gl);
86       g_free (td);
87     } else if (td->gtk_tag == -1) {
88       td->gtk_tag = g_timeout_add (td->interval, sweep_timeout_wrapper, td);
89     }
90   }
91
92   g_mutex_unlock (timeouts_mutex);
93
94   return TRUE;
95 }
96
97 void
98 sweep_timeouts_init (void)
99 {
100   if (timeouts_mutex == NULL)
101     timeouts_mutex = g_mutex_new ();
102
103   g_mutex_lock (timeouts_mutex); /* get some practice in ;) */
104   if (timeouts != NULL) g_list_free (timeouts);
105   timeouts = NULL;
106   g_mutex_unlock (timeouts_mutex);
107
108   g_timeout_add ((guint32)500, sweep_timeouts_handle_pending, NULL);
109 }
110
111 guint
112 sweep_timeout_add (guint32 interval, GtkFunction function, gpointer data)
113 {
114   sweep_timeout_data * td;
115
116   td = g_malloc (sizeof (sweep_timeout_data));
117
118   td->sweep_tag = ++sweep_tag;
119   td->gtk_tag = -1;
120   td->interval = interval;
121   td->function = function;
122   td->data = data;
123
124   g_mutex_lock (timeouts_mutex);
125   timeouts = g_list_append (timeouts, td);
126   g_mutex_unlock (timeouts_mutex);
127
128   return td->sweep_tag;
129 }
130
131 void
132 sweep_timeout_remove (guint sweep_timeout_handler_id)
133 {
134   GList * gl, * gl_next;
135   sweep_timeout_data * td;
136
137   if (sweep_timeout_handler_id == -1)
138     return;
139
140   g_mutex_lock (timeouts_mutex);
141
142   for (gl = timeouts; gl; gl = gl_next) {
143     td = (sweep_timeout_data *)gl->data;
144
145     gl_next = gl->next;
146
147     if (td->sweep_tag == sweep_timeout_handler_id) {
148       if (td->gtk_tag == -1) {
149         /* gtk_timeout not yet started */
150         timeouts = g_list_remove_link (timeouts, gl);
151         g_free (td);
152       } else {
153         /* need to remove gtk_timeout -- mark this for pending thread */
154         td->sweep_tag = -1;
155       }
156       break;
157     }
158   }
159
160   g_mutex_unlock (timeouts_mutex);
161 }
Note: See TracBrowser for help on using the browser.