bump libtool version
[swfdec.git] / libswfdec / swfdec_event.c
blob06ce46e78b1e344df9a6997b8865c07345a20568
1 /* Swfdec
2 * Copyright (C) 2006 Benjamin Otte <otte@gnome.org>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301 USA
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
23 #include "swfdec_event.h"
24 #include "swfdec_as_internal.h"
25 #include "swfdec_as_strings.h"
26 #include "swfdec_debug.h"
27 #include "swfdec_player_internal.h"
28 #include "swfdec_script_internal.h"
30 typedef struct _SwfdecEvent SwfdecEvent;
32 struct _SwfdecEvent {
33 guint conditions;
34 guint8 key;
35 SwfdecScript *script;
38 struct _SwfdecEventList {
39 SwfdecPlayer * player;
40 guint refcount;
41 GArray * events;
44 /**
45 * swfdec_event_type_get_name:
46 * @type: a #SwfdecEventType
48 * Gets the name for the event as a refcounted string or %NULL if the
49 * given clip event has no associated event.
51 * Returns: The name of the event or %NULL if none.
52 **/
53 const char *
54 swfdec_event_type_get_name (SwfdecEventType type)
56 switch (type) {
57 case SWFDEC_EVENT_LOAD:
58 return SWFDEC_AS_STR_onLoad;
59 case SWFDEC_EVENT_ENTER:
60 return SWFDEC_AS_STR_onEnterFrame;
61 case SWFDEC_EVENT_UNLOAD:
62 return SWFDEC_AS_STR_onUnload;
63 case SWFDEC_EVENT_MOUSE_MOVE:
64 return SWFDEC_AS_STR_onMouseMove;
65 case SWFDEC_EVENT_MOUSE_DOWN:
66 return SWFDEC_AS_STR_onMouseDown;
67 case SWFDEC_EVENT_MOUSE_UP:
68 return SWFDEC_AS_STR_onMouseUp;
69 case SWFDEC_EVENT_KEY_UP:
70 return SWFDEC_AS_STR_onKeyUp;
71 case SWFDEC_EVENT_KEY_DOWN:
72 return SWFDEC_AS_STR_onKeyDown;
73 case SWFDEC_EVENT_DATA:
74 return SWFDEC_AS_STR_onData;
75 case SWFDEC_EVENT_INITIALIZE:
76 return NULL;
77 case SWFDEC_EVENT_PRESS:
78 return SWFDEC_AS_STR_onPress;
79 case SWFDEC_EVENT_RELEASE:
80 return SWFDEC_AS_STR_onRelease;
81 case SWFDEC_EVENT_RELEASE_OUTSIDE:
82 return SWFDEC_AS_STR_onReleaseOutside;
83 case SWFDEC_EVENT_ROLL_OVER:
84 return SWFDEC_AS_STR_onRollOver;
85 case SWFDEC_EVENT_ROLL_OUT:
86 return SWFDEC_AS_STR_onRollOut;
87 case SWFDEC_EVENT_DRAG_OVER:
88 return SWFDEC_AS_STR_onDragOver;
89 case SWFDEC_EVENT_DRAG_OUT:
90 return SWFDEC_AS_STR_onDragOut;
91 case SWFDEC_EVENT_KEY_PRESS:
92 return NULL;
93 case SWFDEC_EVENT_CONSTRUCT:
94 return SWFDEC_AS_STR_onConstruct;
95 default:
96 g_assert_not_reached ();
97 return NULL;
101 SwfdecEventList *
102 swfdec_event_list_new (SwfdecPlayer *player)
104 SwfdecEventList *list;
106 g_return_val_if_fail (SWFDEC_IS_PLAYER (player), NULL);
108 list = g_new0 (SwfdecEventList, 1);
109 list->player = player;
110 list->refcount = 1;
111 list->events = g_array_new (FALSE, FALSE, sizeof (SwfdecEvent));
113 return list;
116 /* FIXME: this is a bit nasty because of modifying */
117 SwfdecEventList *
118 swfdec_event_list_copy (SwfdecEventList *list)
120 g_return_val_if_fail (list != NULL, NULL);
121 g_return_val_if_fail (list->refcount > 0, NULL);
123 list->refcount++;
125 return list;
128 void
129 swfdec_event_list_free (SwfdecEventList *list)
131 guint i;
133 g_return_if_fail (list != NULL);
134 g_return_if_fail (list->refcount > 0);
136 list->refcount--;
137 if (list->refcount > 0)
138 return;
140 for (i = 0; i < list->events->len; i++) {
141 SwfdecEvent *event = &g_array_index (list->events, SwfdecEvent, i);
142 swfdec_script_unref (event->script);
144 g_array_free (list->events, TRUE);
145 g_free (list);
148 static const char *
149 swfdec_event_list_condition_name (guint conditions)
151 if (conditions & SWFDEC_EVENT_LOAD)
152 return "Load";
153 if (conditions & SWFDEC_EVENT_ENTER)
154 return "Enter";
155 if (conditions & SWFDEC_EVENT_UNLOAD)
156 return "Unload";
157 if (conditions & SWFDEC_EVENT_MOUSE_MOVE)
158 return "MouseMove";
159 if (conditions & SWFDEC_EVENT_MOUSE_DOWN)
160 return "MouseDown";
161 if (conditions & SWFDEC_EVENT_MOUSE_UP)
162 return "MouseUp";
163 if (conditions & SWFDEC_EVENT_KEY_UP)
164 return "KeyUp";
165 if (conditions & SWFDEC_EVENT_KEY_DOWN)
166 return "KeyDown";
167 if (conditions & SWFDEC_EVENT_DATA)
168 return "Data";
169 if (conditions & SWFDEC_EVENT_INITIALIZE)
170 return "Initialize";
171 if (conditions & SWFDEC_EVENT_PRESS)
172 return "Press";
173 if (conditions & SWFDEC_EVENT_RELEASE)
174 return "Release";
175 if (conditions & SWFDEC_EVENT_RELEASE_OUTSIDE)
176 return "ReleaseOutside";
177 if (conditions & SWFDEC_EVENT_ROLL_OVER)
178 return "RollOver";
179 if (conditions & SWFDEC_EVENT_ROLL_OUT)
180 return "RollOut";
181 if (conditions & SWFDEC_EVENT_DRAG_OVER)
182 return "DragOver";
183 if (conditions & SWFDEC_EVENT_DRAG_OUT)
184 return "DragOut";
185 if (conditions & SWFDEC_EVENT_KEY_PRESS)
186 return "KeyPress";
187 if (conditions & SWFDEC_EVENT_CONSTRUCT)
188 return "Construct";
189 return "No Event";
192 void
193 swfdec_event_list_parse (SwfdecEventList *list, SwfdecBits *bits, int version,
194 guint conditions, guint8 key, const char *description)
196 SwfdecEvent event;
197 char *name;
199 g_return_if_fail (list != NULL);
200 g_return_if_fail (list->refcount == 1);
201 g_return_if_fail (description != NULL);
203 event.conditions = conditions;
204 event.key = key;
205 name = g_strconcat (description, ".",
206 swfdec_event_list_condition_name (conditions), NULL);
207 event.script = swfdec_script_new_from_bits (bits, name, version);
208 g_free (name);
209 if (event.script)
210 g_array_append_val (list->events, event);
213 void
214 swfdec_event_list_execute (SwfdecEventList *list, SwfdecAsObject *object,
215 guint condition, guint8 key)
217 guint i;
219 g_return_if_fail (list != NULL);
220 g_return_if_fail (SWFDEC_IS_AS_OBJECT (object));
222 /* FIXME: Do we execute all events if the event list is gone already? */
223 /* need to ref here because followup code could free all references to the list */
224 list = swfdec_event_list_copy (list);
225 for (i = 0; i < list->events->len; i++) {
226 SwfdecEvent *event = &g_array_index (list->events, SwfdecEvent, i);
227 if ((event->conditions & condition) &&
228 event->key == key) {
229 SWFDEC_LOG ("executing script for event %u on scriptable %p", condition, object);
230 swfdec_as_object_run (object, event->script);
233 swfdec_event_list_free (list);
236 gboolean
237 swfdec_event_list_has_conditions (SwfdecEventList *list, SwfdecAsObject *object,
238 guint condition, guint8 key)
240 guint i;
241 const char *name;
243 g_return_val_if_fail (list != NULL, FALSE);
244 g_return_val_if_fail (SWFDEC_IS_AS_OBJECT (object), FALSE);
246 for (i = 0; i < list->events->len; i++) {
247 SwfdecEvent *event = &g_array_index (list->events, SwfdecEvent, i);
248 if ((event->conditions & condition) &&
249 event->key == key)
250 return TRUE;
252 name = swfdec_event_type_get_name (condition);
253 if (name)
254 return swfdec_as_object_has_function (object, name);
255 return FALSE;