INTERACT FORUM

Windows => Plug-in Development => Topic started by: glynor on July 23, 2013, 03:09:54 pm

Title: Help Me Do Zone-Math Because Math Is Hard And I'm Lazy
Post by: glynor on July 23, 2013, 03:09:54 pm
So, I'm working on a little utility that we need for work here, and I have it working fine, but I hit an issue that I'm hoping someone can help me to solve more elegantly.  In my MC COM Wrapper, I have a set of methods that can issue MCCs out via Windows Messages (essentially a WM_MC_COMMAND -based message to the MC frame).  They look like this:

Code: [Select]
public static int SendMCCore(int command, int parameter)
        {

            glynor.Utilities.MessageHelper msg = new glynor.Utilities.MessageHelper();
            return msg.sendWindowsMessage(hWnd, WM_MC_COMMAND, command, parameter);
        }
(I know, I know, use var or else it is redundant, but I don't like to so na-na-na-na-na-na.)

The issue is that I wanted to add an overload that would take a zone index, like this:

public static int SendMCCore(int command, int parameter, int zoneIndex)

Which means, I need to convert the zone index to a "zone specifier" and then OR it with the parameter (http://wiki.jriver.com/index.php/Media_Center_Core_Commands#Specifying_Zones) to get the proper int32 value to send out via Windows Message.  I stared at it for a while, understanding how the Hex values for the zone specifiers increment, but not really knowing bitwise math well enough to know what to do intuitively... I decided I'm lazy and I implemented it like this:

Code: [Select]
public static int SendMCCore(int command, int parameter, int zoneIndex)
        {
            return RunMCCommandLine(CLI_Options.MCC, command.ToString(CultureInfo.InvariantCulture) + "," + parameter.ToString(CultureInfo.InvariantCulture) + ":" + zoneIndex.ToString(CultureInfo.InvariantCulture));
        }

Which, of course, works perfectly with MC 16+ because they added the handy <Param>:<Zone Index> syntax to the mc##.exe /MCC command line function.  But it is ugly and if I ever want to use the MCWS MCC command to send these over the network it won't work.

So...  I'd love a C# Method that does Zone Math for me, like this:

public static int ZonifyParameter(int parameter, int zoneIndex)

And, looking at the code for this would almost certainly teach me something useful.  Any love for the weary?
Title: Re: Help Me Do Zone-Math Because Math Is Hard And I'm Lazy
Post by: Matt on July 23, 2013, 03:27:35 pm
Do the macros near the bottom here help:
jriver.com/DevZone/MCCommands.h

MAKE_MCC_PLAYBACK_PARAM(...) in particular.
Title: Re: Help Me Do Zone-Math Because Math Is Hard And I'm Lazy
Post by: glynor on July 23, 2013, 03:39:21 pm
I'd seen that in the header, but unfortunately:

1. I can't use your DEFINES directly via COM in C# (or, at least, I haven't figured out a way to do so).
2. I don't know C++ that well (meaning, really much at all), and so converting it manually to a method involved the math is hard and therefore triggered my lazy.

That is, essentially, exactly what I want, though.  Convert this:

MAKE_MCC_PLAYBACK_PARAM(PARAM, ZONE_INDEX) (((ZONE_INDEX) == -1) ? ((PARAM) & 0x00FFFFFF) : ((((ZONE_INDEX) + 1) << 24) & 0xFF000000) | ((PARAM) & 0x00FFFFFF))

To my C# method.

I'll probably be less lazy later if no one nice comes along.
Title: Re: Help Me Do Zone-Math Because Math Is Hard And I'm Lazy
Post by: glynor on July 23, 2013, 05:07:50 pm
I think I see how to do it, now that I've stared at it for a little while.

We'll give it a shot later.
Title: Re: Help Me Do Zone-Math Because Math Is Hard And I'm Lazy
Post by: glynor on July 23, 2013, 09:04:34 pm
Thanks, Matt.  Better to lead a horse and all that.

I should have looked at it more closely earlier.  The ternary operator, of course, is the same in C++, and the rest was just parenthesis noise that was scaring me off.  It threw me that it was testing against just == -1, which seemed odd instead of < 0.

So, I assumed there was some big piece of syntax that I didn't get relating to bitwise operations.

Anyhow, thanks.

Here's the code if anyone is interested.  I stripped out the ternary operator because it is simpler for me to read (and I don't care and I have to maintain it later).

Code: [Select]
static int MakeMCCPlaybackParam(int param, int zoneIndex)
{
if (zoneIndex == -1)
return (int)(param) & 0x00FFFFFF;
else
return Convert.ToInt32(((zoneIndex + 1) << 24) & 0xFF000000 | (param & 0x00FFFFFF));
}
Title: Re: Help Me Do Zone-Math Because Math Is Hard And I'm Lazy
Post by: Matt on July 23, 2013, 09:26:58 pm
Internally, -1 always means 'the current zone'.  That's why it gets special handling.

-2 means the programmer (often me) is an idiot.  There's only so much you can do to handle that, so the macro doesn't try :P
Title: Re: Help Me Do Zone-Math Because Math Is Hard And I'm Lazy
Post by: glynor on July 23, 2013, 10:45:44 pm
Internally, -1 always means 'the current zone'.  That's why it gets special handling.

-2 means the programmer (often me) is an idiot.  There's only so much you can do to handle that, so the macro doesn't try :P

Hah.

I figured that part out, when I really sat there and looked at it.  Of course, the amusing thing is that I'd already decided to do the same thing.  The actual function I was writing operates on the currently active zone by default, but you can also specify a zone manually.

So, internally, I decided to encode that as...

Code: [Select]
private MediaCenter.IMJZoneAutomation GetSelectedZone(int zoneIndex)
{
if (zoneIndex < -1)
throw new ArgumentOutOfRangeException("zoneIndex", "Invalid Zone Index provided (less than -1): " + zoneIndex);
else if (zoneIndex > (MCAutomation.Instance.GetZones().GetNumberZones() - 1))
throw new ArgumentOutOfRangeException("zoneIndex", "Zone Index provided exceeds available zones: " + zoneIndex);
else if (zoneIndex == -1)
return MCAutomation.Instance.GetZones().GetZone(MCAutomation.Instance.GetZones().GetActiveZone());
else
return MCAutomation.Instance.GetZones().GetZone(zoneIndex);
}

EDIT:  Suppose I can take out that second else if...  ;)
Title: Re: Help Me Do Zone-Math Because Math Is Hard And I'm Lazy
Post by: glynor on July 23, 2013, 11:32:21 pm
Ahhh... Much better.

Code: [Select]
public static int SendMCCore(int command, int parameter, int zoneIndex)
{
return SendMCCore(command, MCnet.Utilities.MakeMCCPlaybackParam(parameter, zoneIndex));
}