Eventhandler
Closed this issue · 17 comments
Hi me again sorry to bug you:P
Trying to hook in to the eventhandler to handle media events (keypresses)
on the main screen instead of a separate screen. Here is what I have done:
struct MyEventHandler extends ObjectList {
function MyEventHandler() {
%ObjectList();
}
function HandleEvent($event, $param, $buttons) {
WriteLogLn("MYEVENT");
$i = size(%objs);
while ($i--) {
if (%objs[$i].call($event, $) | %objs[$i].HandleEvent(@$)) break;
}
}
function StopEvent() {
SetEventHandler("KeyDown");
}
function StartEvent() {
SetEventHandler("KeyDown", "HandleEvent", $this);
}
}
struct MyMedia extends MediaView {
var %MyHandler;
function MyMedia() {
%MediaView(MPCController(), VLCController(), WinampController(),
ItunesController());
%MyHandler = MyEventHandler();
}
function MyMediaUpdate() {
for ($i=0; $i < size(%players); $i++) {
%players[$i].Update();
if (%players[$i].state > %players[%player].state)
%player = $i;
}
}
function MyMediaStartEvent() {
%MyHandler.StartEvent();
%MyHandler.Insert(0, MyMedia());
}
function MyMediaEndEvent() {
%MyHandler.StopEvent();
%MyHandler.Remove(MyMedia());
}
function VolumeWheel($change) {
$p = %players[%player];
if ($p.state < 0) %selected = -1;
if (%selected == 0) {
if ($change < 0) {
$p.Prev();
}
else {
$p.Next();
}
}
else if (%selected == 1) {
$p.ChangeMode($change);
}
else if (%selected == 2) {
$p.ChangeBalance($change);
}
else {
$p.ChangeVolume($change);
}
}
function KeyDown($event, $param, $modifiers, $vk) {
WriteLogLn("KEYPRESS");
WriteLogLn($vk);
if (!$modifiers) {
if ($vk == 0xAE) {
%VolumeWheel(-1);
}
else if ($vk == 0xAF) {
%VolumeWheel(1);
}
else if ($vk == 0xB2) {
%players[%player].Stop();
WriteLogLn("STOP");
}
else if ($vk == 0xB3) {
%players[%player].PlayPause();
}
else if ($vk == 0xB0) {
%players[%player].Next();
}
else if ($vk == 0xB1) {
%players[%player].Prev();
}
else if ($vk == 0xAD) {
%players[%player].ToggleMute();
}
return 1;
}
}
}
function GetMyMedia() {
if (IsNull(_myMediaManager)) {
_myMediaManager = MyMedia();
}
return _myMediaManager;
}
struct G19Status {
var %miniFont, %font, %counters, %mycounters, %mymedia;
function G19Status() {
%miniFont = Font("6px2bus", 6);
%mycounters = GetMyCounters();
%mymedia = GetMyMedia();
%font = Font("Arial", 18,0,1,0,CLEARTYPE_QUALITY);
}
function Draw($event, $param, $name, $res) {
ClearScreen();
.........bunch of other code.......
//display winamp/fraps/network usage depends what is running
%mymedia.MyMediaUpdate();
$currmedia = %mymedia.players[%mymedia.player];
if ($currmedia.state != -1) {
if (MediaEventStarted == 0) {
%mymedia.MyMediaStartEvent();
MediaEventStarted = 1;
}
%mymedia.Show();
%mymedia.Draw(,,,,1);
} else {
/*
DisplayText("Dn:", 162, 95);
DisplayTextRight(FormatSize(%mycounters.down, 1,0,1), 232, 95);
DisplayText("Up:", 248, 95);
DisplayTextRight(FormatSize(%mycounters.up, 1,0,1), 318, 95);
*/
MediaEventStarted = 0;
%mymedia.MyMediaEndEvent();
DrawMultiGraph(list(%mycounters.downGraph, %mycounters.upGraph), 2, 238,
318, 180, colorBg, list(0,2));
}
}
Now this all works with a couple problems. When I originally load the lcdm
program my mute volume keys etc work. When I load winamp everything shows
on the lcd properly but the keys no longer (SEEM) to work. I have added
debug code that shows my eventhandler is calling the correct function
(MyMedia.KeyDown()) with the correct keypress data but it is not then being
passed to the media player by ie %players[%player].Stop(); %player is 2
for winamp so not sure why it isn't calling the function?
Also when I then turn winamp off none of my media keys work anymore (ie
volume/mute) and they did work before loading winamp. Is the way I am
removing watching that event incorrect?
Thanks for any help sorry for the wall of text:)
Edit: attached the file if that is easier for you.
Original issue reported on code.google.com by drwag...@gmail.com
on 12 Nov 2009 at 9:49
Attachments:
Happy to help. Really like to see people playing with the scripting language.
StopEvent() should also set the KeyDown event handler to be the eventHandler
object
(Believe it's a global, though can't check now). I only keep track of one
active
handler per event, rather than keeping a list of multiple handlers.
Having trouble figuring out the other problem. I'll look into it when I have
time.
Original comment by mattme...@gmail.com
on 12 Nov 2009 at 10:16
Like this?
SetEventHandler("KeyDown", "HandleEvent", eventHandler);
I tried that and it still does not restore the key functions.
Original comment by drwag...@gmail.com
on 13 Nov 2009 at 12:40
Actually it doesn't call my event anymore but it also doesn't work. This is in
LCDmiscScript.c so I assume it is defined right:
eventHandler = EventHandler();
Original comment by drwag...@gmail.com
on 13 Nov 2009 at 12:47
Hmm... Looks right. I must be missing something. Should have more time to
look at
it tomorrow.
Original comment by mattme...@gmail.com
on 13 Nov 2009 at 12:49
Ok thanks:)
Original comment by drwag...@gmail.com
on 13 Nov 2009 at 1:15
Playing with it a bit more and this is the debug I am getting:
MYEVENT
KEYPRESS
178
STOP
MYEVENT
KEYPRESS
178
STOP
The blank line is where I added:
WriteLogLn(%player);
in the keydown event. For some reason %player seems to be undefined (and
%players as
well as %players[2] doesn't call the event)
Seems weird since the MyMediaUpdate() function works just fine and shows
%player = 2.
Original comment by drwag...@gmail.com
on 16 Nov 2009 at 1:32
%player is an object. WriteLogLn is just a standard function that takes a
string
argument. Object to string conversions currently yields a zero-length string.
WriteLogLn(type(%player)) should give you something.
I'm having some computer issues (Raid drive messing up), which is why I haven't
looked into it myself yet.
Original comment by mattme...@gmail.com
on 16 Nov 2009 at 1:37
That gives me null. Hope you get your raid fixed:)
Original comment by drwag...@gmail.com
on 16 Nov 2009 at 3:15
Oh...%player is an index, was thinking it was the actual current player. As I
don't
initialize it, null just corresponds to 0.
Original comment by mattme...@gmail.com
on 16 Nov 2009 at 3:19
Ok got it mostly figured out I think. Attached is the file that is working for
me.
The control keys now work when the media player is running BUT they still don't
restore the old function when I turn off the media player. But if I switch to a
different screen on the LCD and switch back everything is restored again. I
just
can't seem to figure out how to properly remove the eventhandler. If I
uncomment
line 85:
%eventHandler.Remove($this);
it seems to remove the event as the screen no longer updates with media
progress but
the keypresses still don't work and I can't get it back working with line 75:
%eventHandler.Insert(0, $this);
I have also tried MyMedia() instead of this with no difference. Color me
stumped anyway.
Original comment by drwag...@gmail.com
on 17 Nov 2009 at 1:02
Attachments:
MyMedia extends view. The currently active view must always be in the event
handler
list. Removing it is bad. Very bad. So...ummm...what are you trying to do,
exactly?
Original comment by mattme...@gmail.com
on 17 Nov 2009 at 1:11
Well there are technically 2 currently active views. As I display media info
on the
main g19status screen but only when media player is on. But I made MyMedia not
extend View and it still works fine but still don't know how to restore the
original
event:)
Original comment by drwag...@gmail.com
on 17 Nov 2009 at 2:41
[deleted comment]
Here is all of the override to see for yourself:)
Original comment by drwag...@gmail.com
on 17 Nov 2009 at 2:45
Attachments:
That's because you never check if the media player just stopped and then call
%mymedia.Hide() to stop listening to it.
You have:
if ($currmedia.state != -1) {
//display media
if (MediaStarted == 0) {
%mymedia.Show();
MediaStarted = 1;
}
} else {
to check for when a media player starts, but not corresponding check for when
it is
stopped. %mymedia.Show() is, of course, the function that starts listening to
the
media keys.
Original comment by mattme...@gmail.com
on 17 Nov 2009 at 3:00
Woohoo! That did it thanks. Funny thing is I had it that way before and it
didn't
work but that must have been cause it was extending View or something. Thanks
again:)
Original comment by drwag...@gmail.com
on 17 Nov 2009 at 3:09
Original comment by mattme...@gmail.com
on 17 Nov 2009 at 3:22
- Changed state: Verified
- Added labels: Type-Other
- Removed labels: Type-Defect