~ubuntu-branches/ubuntu/saucy/gnash/saucy-proposed

« back to all changes in this revision

Viewing changes to libcore/asobj/Key.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Alexander Sack
  • Date: 2008-10-13 14:29:49 UTC
  • mfrom: (1.1.9 upstream)
  • Revision ID: james.westby@ubuntu.com-20081013142949-f6qdvnu4mn05ltdc
Tags: 0.8.4~~bzr9980-0ubuntu1
* new upstream release 0.8.4 (LP: #240325)
* ship new lib usr/lib/gnash/libmozsdk.so.* in mozilla-plugin-gnash
  - update debian/mozilla-plugin-gnash.install
* ship new lib usr/lib/gnash/libgnashnet.so.* in gnash-common
  - update debian/gnash-common.install
* add basic debian/build_head script to build latest CVS head packages.
  - add debian/build_head
* new sound architecture requires build depend on libsdl1.2-dev
  - update debian/control
* head build script now has been completely migrated to bzr (upstream +
  ubuntu)
  - update debian/build_head
* disable kde gui until klash/qt4 has been fixed; keep kde packages as empty
  packages for now.
  - update debian/rules
  - debian/klash.install
  - debian/klash.links
  - debian/klash.manpages
  - debian/konqueror-plugin-gnash.install
* drop libkonq5-dev build dependency accordingly
  - update debian/control
* don't install headers manually anymore. gnash doesnt provide a -dev
  package after all
  - update debian/rules
* update libs installed in gnash-common; libgnashserver-*.so is not available
  anymore (removed); in turn we add the new libgnashcore-*.so
  - update debian/gnash-common.install
* use -Os for optimization and properly pass CXXFLAGS=$(CFLAGS) to configure
  - update debian/rules
* touch firefox .autoreg in postinst of mozilla plugin
  - update debian/mozilla-plugin-gnash.postinst
* link gnash in ubufox plugins directory for the plugin alternative switcher
  - add debian/mozilla-plugin-gnash.links
* suggest ubufox accordingly
  - update debian/control
* add new required build-depends on libgif-dev
  - update debian/control
* add Xb-Npp-Description and Xb-Npp-File as new plugin database meta data
  - update debian/control

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Key.cpp:  ActionScript "Key" class (keyboards), for Gnash.
 
2
// 
 
3
//   Copyright (C) 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
 
4
//
 
5
// This program is free software; you can redistribute it and/or modify
 
6
// it under the terms of the GNU General Public License as published by
 
7
// the Free Software Foundation; either version 3 of the License, or
 
8
// (at your option) any later version.
 
9
 
 
10
// This program is distributed in the hope that it will be useful,
 
11
// but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
// GNU General Public License for more details.
 
14
// 
 
15
// You should have received a copy of the GNU General Public License
 
16
// along with this program; if not, write to the Free Software
 
17
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 
18
//
 
19
 
 
20
#include "smart_ptr.h" // GNASH_USE_GC
 
21
#include "log.h"
 
22
#include "Key.h"
 
23
#include "fn_call.h"
 
24
#include "movie_root.h"
 
25
#include "action.h" // for call_method
 
26
#include "VM.h" // for registerNative
 
27
#include "builtin_function.h"
 
28
#include "Object.h" // for getObjectInterface()
 
29
#include "AsBroadcaster.h" // for initializing self as a broadcaster
 
30
#include "namedStrings.h"
 
31
#include "GnashKey.h" // key::code
 
32
 
 
33
namespace gnash {
 
34
 
 
35
/************************************************************************
 
36
*
 
37
* This has been moved from action.cpp, when things are clean
 
38
* everything should have been moved up
 
39
*
 
40
************************************************************************/
 
41
 
 
42
key_as_object::key_as_object()
 
43
    :
 
44
    as_object(getObjectInterface()),
 
45
    _unreleasedKeys(0),
 
46
    _lastKeyEvent(0)
 
47
{
 
48
    // Key is a broadcaster only in SWF6 and up (correct?)
 
49
    int swfversion = _vm.getSWFVersion();
 
50
    if ( swfversion > 5 )
 
51
    {
 
52
        AsBroadcaster::initialize(*this);
 
53
    }
 
54
}
 
55
 
 
56
bool
 
57
key_as_object::is_key_down(int keycode)
 
58
{
 
59
    if (keycode < 0 || keycode >= key::KEYCOUNT) return false;
 
60
    if (_unreleasedKeys.test(keycode)) return true;
 
61
    return false;
 
62
}
 
63
 
 
64
void
 
65
key_as_object::set_key_down(key::code code)
 
66
{
 
67
    if (code >= key::KEYCOUNT) return;
 
68
 
 
69
    // This is used for getAscii() of the last key event, so we store
 
70
    // the unique gnash::key::code.
 
71
    _lastKeyEvent = code;
 
72
 
 
73
    // Key.isDown() only cares about flash keycode, not character, so
 
74
    // we lookup keycode to add to _unreleasedKeys.   
 
75
    size_t keycode = key::codeMap[code][key::KEY];
 
76
 
 
77
    _unreleasedKeys.set(keycode, 1);
 
78
}
 
79
 
 
80
void
 
81
key_as_object::set_key_up(key::code code)
 
82
{
 
83
    if (code >= key::KEYCOUNT) return;
 
84
 
 
85
    // This is used for getAscii() of the last key event, so we store
 
86
    // the unique gnash::key::code.    
 
87
    _lastKeyEvent = code;
 
88
 
 
89
    // Key.isDown() only cares about flash keycode, not character, so
 
90
    // we lookup keycode to add to _unreleasedKeys.
 
91
    size_t keycode = key::codeMap[code][key::KEY];
 
92
 
 
93
    _unreleasedKeys.set(keycode, 0);
 
94
}
 
95
 
 
96
 
 
97
void 
 
98
key_as_object::notify_listeners(const event_id& key_event)
 
99
{  
 
100
    // There is no user defined "onKeyPress" event handler
 
101
    if( (key_event.m_id != event_id::KEY_DOWN) && (key_event.m_id != event_id::KEY_UP) ) return;
 
102
 
 
103
    as_value ev(key_event.get_function_name());
 
104
 
 
105
    //log_debug("notify_listeners calling broadcastMessage with arg %s", ev);
 
106
    callMethod(NSV::PROP_BROADCAST_MESSAGE, ev);
 
107
}
 
108
 
 
109
int
 
110
key_as_object::get_last_key() const
 
111
{
 
112
    return _lastKeyEvent;
 
113
}
 
114
 
 
115
static as_value
 
116
key_is_accessible(const fn_call& fn)
 
117
{
 
118
 
 
119
    boost::intrusive_ptr<key_as_object> ko = 
 
120
        ensureType<key_as_object>(fn.this_ptr);
 
121
 
 
122
    log_unimpl("Key.isAccessible");
 
123
    return as_value();
 
124
}
 
125
 
 
126
 
 
127
/// Return the ascii number of the last key pressed.
 
128
static as_value   
 
129
key_get_ascii(const fn_call& fn)
 
130
{
 
131
    boost::intrusive_ptr<key_as_object> ko = 
 
132
        ensureType<key_as_object>(fn.this_ptr);
 
133
 
 
134
    int code = ko->get_last_key();
 
135
 
 
136
    return as_value(gnash::key::codeMap[code][key::ASCII]);
 
137
}
 
138
 
 
139
/// Returns the keycode of the last key pressed.
 
140
static as_value   
 
141
key_get_code(const fn_call& fn)
 
142
{
 
143
    boost::intrusive_ptr<key_as_object> ko = 
 
144
        ensureType<key_as_object>(fn.this_ptr);
 
145
 
 
146
    int code = ko->get_last_key();
 
147
 
 
148
    return as_value(key::codeMap[code][key::KEY]);
 
149
}
 
150
 
 
151
/// Return true if the specified (first arg keycode) key is pressed.
 
152
static as_value   
 
153
key_is_down(const fn_call& fn)
 
154
{
 
155
    boost::intrusive_ptr<key_as_object> ko = 
 
156
        ensureType<key_as_object>(fn.this_ptr);
 
157
 
 
158
    if (fn.nargs < 1)
 
159
    {
 
160
        IF_VERBOSE_ASCODING_ERRORS(
 
161
            log_aserror(_("Key.isDown needs one argument (the key code)"));
 
162
        );
 
163
        return as_value();
 
164
    }
 
165
 
 
166
    int keycode = fn.arg(0).to_int();
 
167
 
 
168
    return as_value(ko->is_key_down(keycode));
 
169
}
 
170
 
 
171
/// \brief
 
172
/// Given the keycode of NUM_LOCK or CAPSLOCK, returns true if
 
173
/// the associated state is on.
 
174
///
 
175
static as_value   
 
176
key_is_toggled(const fn_call& /* fn */)
 
177
{
 
178
    log_unimpl("Key.isToggled");
 
179
    // @@ TODO
 
180
    return as_value(false);
 
181
}
 
182
 
 
183
void key_class_init(as_object& global)
 
184
{
 
185
 
 
186
    //  GNASH_REPORT_FUNCTION;
 
187
    //
 
188
 
 
189
    // Create built-in key object.
 
190
    // NOTE: _global.Key *is* an object, not a constructor
 
191
    as_object*  key_obj = new key_as_object;
 
192
 
 
193
    const int flags = as_prop_flags::readOnly |
 
194
                      as_prop_flags::dontDelete |
 
195
                      as_prop_flags::dontEnum;
 
196
 
 
197
    // constants
 
198
#define KEY_CONST(k) key_obj->init_member(#k, key::codeMap[key::k][key::KEY], flags)
 
199
    KEY_CONST(BACKSPACE);
 
200
    KEY_CONST(CAPSLOCK);
 
201
    KEY_CONST(CONTROL);
 
202
    KEY_CONST(DELETEKEY);
 
203
    KEY_CONST(DOWN);
 
204
    KEY_CONST(END);
 
205
    KEY_CONST(ENTER);
 
206
    KEY_CONST(ESCAPE);
 
207
    KEY_CONST(HOME);
 
208
    KEY_CONST(INSERT);
 
209
    KEY_CONST(LEFT);
 
210
    KEY_CONST(PGDN);
 
211
    KEY_CONST(PGUP);
 
212
    KEY_CONST(RIGHT);
 
213
    KEY_CONST(SHIFT);
 
214
    KEY_CONST(SPACE);
 
215
    KEY_CONST(TAB);
 
216
    KEY_CONST(UP);
 
217
    KEY_CONST(ALT);
 
218
 
 
219
    // methods
 
220
 
 
221
    VM& vm = global.getVM();
 
222
 
 
223
    vm.registerNative(key_get_ascii, 800, 0);
 
224
    key_obj->init_member("getAscii", vm.getNative(800, 0), flags);
 
225
 
 
226
    vm.registerNative(key_get_code, 800, 1);
 
227
    key_obj->init_member("getCode", vm.getNative(800, 1), flags);
 
228
 
 
229
    vm.registerNative(key_is_down, 800, 2);
 
230
    key_obj->init_member("isDown", vm.getNative(800, 2), flags);
 
231
 
 
232
    vm.registerNative(key_is_toggled, 800, 3);
 
233
    key_obj->init_member("isToggled", vm.getNative(800, 3), flags);
 
234
 
 
235
    key_obj->init_member("isAccessible", 
 
236
            new builtin_function(key_is_accessible), flags);
 
237
 
 
238
    global.init_member("Key", key_obj);
 
239
}
 
240
 
 
241
#ifdef GNASH_USE_GC
 
242
void
 
243
key_as_object::markReachableResources() const
 
244
{
 
245
    markAsObjectReachable();
 
246
    for (Listeners::const_iterator i=_listeners.begin(), e=_listeners.end();
 
247
                i != e; ++i)
 
248
    {
 
249
        (*i)->setReachable();
 
250
    }
 
251
}
 
252
#endif // def GNASH_USE_GC
 
253
 
 
254
} // end of gnash namespace
 
255