Skip to content

Commit

Permalink
testing contextmonitor
Browse files Browse the repository at this point in the history
  • Loading branch information
Steven Houben committed Aug 27, 2012
1 parent de8f72a commit feaa02f
Show file tree
Hide file tree
Showing 10 changed files with 700 additions and 501 deletions.
483 changes: 242 additions & 241 deletions NooSphere/ActivityBar/ActivityUI.csproj

Large diffs are not rendered by default.

180 changes: 180 additions & 0 deletions NooSphere/ActivityBar/Context/InputRedirect.cs
@@ -0,0 +1,180 @@

/****************************************************************************
(c) 2012 Steven Houben(shou@itu.dk) and Søren Nielsen(snielsen@itu.dk)
Pervasive Interaction Technology Laboratory (pIT lab)
IT University of Copenhagen
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU GENERAL PUBLIC LICENSE V3 or later,
as published by the Free Software Foundation. Check
http://www.gnu.org/licenses/gpl.html for details.
****************************************************************************/

using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using NooSphere.ActivitySystem.Context;
using NooSphere.ActivitySystem.Context.Multicast;
using NooSphere.Platform.Windows.Hooks;

namespace ActivityUI.Context
{
public class InputRedirect : IContextService
{
private MulticastSocket _mSocket;
private Point _previousPoint;
private bool _unInitializedMouse = true;
public PointerRole PointerRole { get; private set; }

public InputRedirect(PointerRole role)
{
PointerRole = role;

}
public void Start()
{
_mSocket = new MulticastSocket("225.5.6.7", 5000, 10);
_mSocket.OnNotifyMulticastSocketListener += _mSocket_OnNotifyMulticastSocketListener;
Initialize(PointerRole);
}
public void Stop()
{
MouseHook.UnRegister();
}
private void Initialize(PointerRole role)
{
switch (role)
{
case PointerRole.Controller:
MouseHook.Register();
MouseHook.MouseDown += MouseHookMouseDown;
MouseHook.MouseMove += MouseHookMouseMove;
MouseHook.MouseUp += MouseHookMouseUp;
break;
case PointerRole.Slave:
MouseHook.MouseDown += MouseHookMouseDown;
MouseHook.MouseMove += MouseHookMouseMove;
MouseHook.MouseUp += MouseHookMouseUp;
_mSocket.StartReceiving();
break;
}
}


private void MouseHookMouseUp(object sender, MouseEventArgs e)
{
if (PointerRole == PointerRole.Controller)
Send(new PointerMessage(e.X, e.Y, PointerEvent.MouseUp).ToString());
}
private void MouseHookMouseMove(object sender, MouseEventArgs e)
{
if (_unInitializedMouse)
{
_previousPoint = e.Location;
_unInitializedMouse = false;
}
var xDif = _previousPoint.X - e.Location.X;
var yDif = _previousPoint.Y - e.Location.Y;
Console.WriteLine(xDif + "---" + yDif);

if (PointerRole == PointerRole.Controller)
Send(new PointerMessage(e.Location.X, e.Location.Y+ yDif, PointerEvent.MouseMove).ToString());
_previousPoint = e.Location;
}
private void MouseHookMouseDown(object sender, MouseEventArgs e)
{
if (PointerRole == PointerRole.Controller)
Send(new PointerMessage(e.X, e.Y, PointerEvent.MouseDown).ToString());
}

[DllImport("User32.dll")]
private static extern bool SetCursorPos(int x, int y);
[DllImport("user32.dll")]
public static extern void mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);

public const int MOUSEEVENTF_LEFTDOWN = 0x02;
public const int MOUSEEVENTF_LEFTUP = 0x04;
public const int MOUSEEVENTF_RIGHTDOWN = 0x08;
public const int MOUSEEVENTF_RIGHTUP = 0x10;
private void _mSocket_OnNotifyMulticastSocketListener(object sender, NotifyMulticastSocketListenerEventArgs e)
{
if (e.Type == MulticastSocketMessageType.MessageReceived)
{
var msg = System.Text.Encoding.ASCII.GetString((byte[])e.NewObject);
var res = new PointerMessage(Convert.ToInt32(msg.Split('@')[0]), Convert.ToInt32(msg.Split('@')[1]), (PointerEvent)Enum.Parse(typeof(PointerEvent), msg.Split('@')[2]));
HandleMessage(res);
if (DataReceived != null)
{
DataReceived(this, new DataEventArgs(msg));
}
}
}
private void HandleMessage(PointerMessage res)
{
switch (res.Message)
{
case PointerEvent.MouseMove:
SetCursorPos(res.X, res.Y);
break;
case PointerEvent.MouseDown:
mouse_event(MOUSEEVENTF_LEFTDOWN, res.X, res.Y, 0, 0);
break;
case PointerEvent.MouseUp:
mouse_event(MOUSEEVENTF_LEFTUP, res.X, res.Y, 0, 0);
break;
}
}


public string Name { get; set; }
public Guid Id { get; set; }

public void Send(string message)
{
_mSocket.Send(message);
}

public event DataReceivedHandler DataReceived;
public event System.EventHandler Started;
public event System.EventHandler Stopped;

}

public class PointerMessage
{
public int X { get; set; }
public int Y { get; set; }
public PointerEvent Message { get; set; }

public PointerMessage(int x, int y, PointerEvent msg)
{
X = x;
Y = y;
Message = msg;
}
public override string ToString()
{
return X + "@" + Y + "@" + Message;
}
}

public enum PointerRole
{
Controller,
Slave,
Both
}

public enum PointerEvent
{
MouseMove,
MouseUp,
MouseDown,
TouchUp,
TouchDown
}


}
7 changes: 6 additions & 1 deletion NooSphere/ActivityBar/Xaml/ActivityBar.xaml.cs
Expand Up @@ -23,6 +23,7 @@
using System.Windows.Threading; using System.Windows.Threading;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using System.Windows.Interop; using System.Windows.Interop;
using ActivityUI.Context;
using NooSphere.ActivitySystem.Base; using NooSphere.ActivitySystem.Base;
using NooSphere.ActivitySystem.Base.Client; using NooSphere.ActivitySystem.Base.Client;
using NooSphere.ActivitySystem.Base.Service; using NooSphere.ActivitySystem.Base.Service;
Expand Down Expand Up @@ -244,7 +245,10 @@ private void StartClient(string activityManagerHttpAddress)
_client.ContextMessageReceived += _client_ContextMessageReceived; _client.ContextMessageReceived += _client_ContextMessageReceived;


_client.ConnectionEstablished += ClientConnectionEstablished; _client.ConnectionEstablished += ClientConnectionEstablished;
_client.ServiceIsDown += new ServiceDownHandler(_client_ServiceIsDown); _client.ServiceIsDown += _client_ServiceIsDown;

_client.ContextMonitor.AddContextService(new InputRedirect(PointerRole.Slave));

_client.Open(activityManagerHttpAddress); _client.Open(activityManagerHttpAddress);
} }


Expand All @@ -268,6 +272,7 @@ void _client_ContextMessageReceived(object sender, ContextEventArgs e)


void ClientConnectionEstablished(object sender, EventArgs e) void ClientConnectionEstablished(object sender, EventArgs e)
{ {
_client.ContextMonitor.Start();
BuildUi(); BuildUi();
_startingUp = false; _startingUp = false;
} }
Expand Down
5 changes: 5 additions & 0 deletions NooSphere/ActivityTablet/ActivityTablet.csproj
Expand Up @@ -94,6 +94,7 @@
<Generator>MSBuild:Compile</Generator> <Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType> <SubType>Designer</SubType>
</Page> </Page>
<Compile Include="Context\InputRedirect.cs" />
<Compile Include="DisplayMode.cs" /> <Compile Include="DisplayMode.cs" />
<Compile Include="Xaml\ActivityButton.cs" /> <Compile Include="Xaml\ActivityButton.cs" />
<Compile Include="App.xaml.cs"> <Compile Include="App.xaml.cs">
Expand Down Expand Up @@ -181,6 +182,10 @@
<Project>{03C072A4-5960-4A5D-B0CC-98EBD854E970}</Project> <Project>{03C072A4-5960-4A5D-B0CC-98EBD854E970}</Project>
<Name>NooSphere.Core</Name> <Name>NooSphere.Core</Name>
</ProjectReference> </ProjectReference>
<ProjectReference Include="..\NooSphere.Platform.Windows\NooSphere.Platform.Windows.csproj">
<Project>{B3333919-EE66-441A-8AB3-6E6601A8A0B1}</Project>
<Name>NooSphere.Platform.Windows</Name>
</ProjectReference>
</ItemGroup> </ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Import Project="$(SolutionDir)\.nuget\nuget.targets" /> <Import Project="$(SolutionDir)\.nuget\nuget.targets" />
Expand Down

0 comments on commit feaa02f

Please sign in to comment.