comparison plugins/perl/perl-handlers.c @ 6567:6e25e1e08ffb

[gaim-migrate @ 7089] Perl signal callbacks just got more spiffy. You can now embed a sub inside of a callback instead of linking to the function. committer: Tailor Script <tailor@pidgin.im>
author Christian Hammond <chipx86@chipx86.com>
date Fri, 22 Aug 2003 04:09:06 +0000
parents f6c2a7b5afa7
children 33486b749aa9
comparison
equal deleted inserted replaced
6566:f6c2a7b5afa7 6567:6e25e1e08ffb
21 static void 21 static void
22 destroy_signal_handler(GaimPerlSignalHandler *handler) 22 destroy_signal_handler(GaimPerlSignalHandler *handler)
23 { 23 {
24 signal_handlers = g_list_remove(signal_handlers, handler); 24 signal_handlers = g_list_remove(signal_handlers, handler);
25 25
26 if (handler->instance != NULL)
27 SvREFCNT_dec(handler->instance);
28
29 if (handler->callback != NULL)
30 SvREFCNT_dec(handler->callback);
31
32 if (handler->data != NULL)
33 SvREFCNT_dec(handler->data);
34
26 g_free(handler->signal); 35 g_free(handler->signal);
27 g_free(handler->func);
28 g_free(handler); 36 g_free(handler);
29 } 37 }
30 38
31 static int 39 static int
32 perl_timeout_cb(gpointer data) 40 perl_timeout_cb(gpointer data)
80 88
81 gaim_debug(GAIM_DEBUG_INFO, "perl", "Pushing data %p\n", handler->data); 89 gaim_debug(GAIM_DEBUG_INFO, "perl", "Pushing data %p\n", handler->data);
82 XPUSHs((SV *)handler->data); 90 XPUSHs((SV *)handler->data);
83 91
84 PUTBACK; 92 PUTBACK;
85 gaim_debug(GAIM_DEBUG_INFO, "perl", "Calling handler %s\n", 93
86 handler->func); 94 if (ret_value != NULL)
87 count = call_pv(handler->func, G_EVAL | G_SCALAR); 95 {
88 SPAGAIN; 96 count = call_sv(handler->callback, G_SCALAR);
89 97
90 if (count > 1) 98 SPAGAIN;
91 ret_val = POPp; 99
92 100 if (count != 1)
93 PUTBACK; 101 croak("Uh oh! call_sv returned %i != 1", i);
102 else
103 ret_val = POPs;
104 }
105 else
106 call_sv(handler->callback, G_SCALAR);
107
94 FREETMPS; 108 FREETMPS;
95 LEAVE; 109 LEAVE;
96 110
97 return ret_val; 111 return ret_val;
98 } 112 }
99 113
100 static GaimPerlSignalHandler * 114 static GaimPerlSignalHandler *
101 find_signal_handler(GaimPlugin *plugin, void *instance, 115 find_signal_handler(GaimPlugin *plugin, void *instance, const char *signal)
102 const char *signal, const char *func)
103 { 116 {
104 GaimPerlSignalHandler *handler; 117 GaimPerlSignalHandler *handler;
105 GList *l; 118 GList *l;
106 119
107 for (l = signal_handlers; l != NULL; l = l->next) 120 for (l = signal_handlers; l != NULL; l = l->next)
108 { 121 {
109 handler = (GaimPerlSignalHandler *)l->data; 122 handler = (GaimPerlSignalHandler *)l->data;
110 123
111 if (handler->plugin == plugin && 124 if (handler->plugin == plugin &&
112 handler->instance == instance && 125 handler->instance == instance &&
113 !strcmp(handler->signal, signal) && 126 !strcmp(handler->signal, signal))
114 !strcmp(handler->func, func))
115 { 127 {
116 return handler; 128 return handler;
117 } 129 }
118 } 130 }
119 131
166 destroy_timeout_handler(timeout_handlers->data); 178 destroy_timeout_handler(timeout_handlers->data);
167 } 179 }
168 180
169 void 181 void
170 gaim_perl_signal_connect(GaimPlugin *plugin, void *instance, 182 gaim_perl_signal_connect(GaimPlugin *plugin, void *instance,
171 const char *signal, const char *func, void *data) 183 const char *signal, SV *callback, SV *data)
172 { 184 {
173 GaimPerlSignalHandler *handler; 185 GaimPerlSignalHandler *handler;
174 186
175 handler = g_new0(GaimPerlSignalHandler, 1); 187 handler = g_new0(GaimPerlSignalHandler, 1);
176 handler->plugin = plugin; 188 handler->plugin = plugin;
177 handler->instance = instance; 189 handler->instance = instance;
178 handler->signal = g_strdup(signal); 190 handler->signal = g_strdup(signal);
179 handler->func = g_strdup(func); 191 handler->callback = (callback != NULL && callback != &PL_sv_undef
180 handler->data = data; 192 ? newSVsv(callback) : NULL);
193 handler->data = (data != NULL && data != &PL_sv_undef
194 ? newSVsv(data) : NULL);
181 195
182 signal_handlers = g_list_append(signal_handlers, handler); 196 signal_handlers = g_list_append(signal_handlers, handler);
183 197
184 gaim_debug(GAIM_DEBUG_MISC, "perl", "plugin = %p\n", plugin);
185 gaim_signal_connect_vargs(instance, signal, 198 gaim_signal_connect_vargs(instance, signal,
186 plugin, GAIM_CALLBACK(perl_signal_cb), handler); 199 plugin, GAIM_CALLBACK(perl_signal_cb), handler);
187 } 200 }
188 201
189 void 202 void
190 gaim_perl_signal_disconnect(GaimPlugin *plugin, void *instance, 203 gaim_perl_signal_disconnect(GaimPlugin *plugin, void *instance,
191 const char *signal, const char *func) 204 const char *signal)
192 { 205 {
193 GaimPerlSignalHandler *handler; 206 GaimPerlSignalHandler *handler;
194 207
195 handler = find_signal_handler(plugin, instance, signal, func); 208 handler = find_signal_handler(plugin, instance, signal);
196 209
197 if (handler == NULL) 210 if (handler == NULL)
198 { 211 {
199 croak("Invalid signal handler information in " 212 croak("Invalid signal handler information in "
200 "disconnecting a perl signal handler.\n"); 213 "disconnecting a perl signal handler.\n");