public void LoadResource(Stream file, byte type, uint size, Resource resource) { switch (type) { case MoSync.Constants.RT_PLACEHOLDER: break; case MoSync.Constants.RT_UBIN: { resource.SetResourceType(MoSync.Constants.RT_BINARY); resource.SetInternalObject(new BoundedStream(file, file.Position, (long)size)); file.Seek(size, SeekOrigin.Current); } break; case MoSync.Constants.RT_BINARY: { byte[] bytes = new byte[size]; file.Read(bytes, 0, (int)size); MemoryStream memStream = new MemoryStream(bytes); resource.SetInternalObject(memStream); } break; case MoSync.Constants.RT_IMAGE: { byte[] bytes = new byte[size]; file.Read(bytes, 0, (int)size); using (MemoryStream ms = new MemoryStream(bytes, 0, bytes.Length)) { resource.SetInternalObject(MoSync.Util.CreateWriteableBitmapFromStream(ms)); ms.Close(); } } break; case MoSync.Constants.RT_LABEL: { byte[] bytes = new byte[size]; file.Read(bytes, 0, (int)size); if (bytes[size - 1] != 0) { throw new Exception("invalid label (no null terminator)"); } String s = System.Text.Encoding.UTF8.GetString(bytes, 0, (int)size - 1); if (mLabels.ContainsKey(s)) { throw new Exception("duplicate label"); } mLabels.Add(s, mCurrentResourceHandle); } break; default: Util.Log("Unknown resource type " + type + ", size " + size + "\n"); file.Seek(size, SeekOrigin.Current); break; } }
public void Init(Syscalls syscalls, Core core, Runtime runtime) { PhoneApplicationFrame frame = (PhoneApplicationFrame)Application.Current.RootVisual; double screenWidth = System.Windows.Application.Current.Host.Content.ActualWidth; double screenHeight = System.Windows.Application.Current.Host.Content.ActualHeight; if ((int)screenHeight == 0) { throw new Exception("screenHeight"); } PhoneApplicationPage mainPage = (PhoneApplicationPage)frame.Content; Image mainImage = new Image(); mainPage.Width = screenWidth; mainPage.Height = screenHeight; mainImage.Width = screenWidth; mainImage.Height = screenHeight; mainPage.Content = mainImage; mClipRect.X = 0.0; mClipRect.Y = 0.0; mClipRect.Width = screenWidth; mClipRect.Height = screenHeight; // no apparent effect on memory leaks. runtime.RegisterCleaner(delegate() { MoSync.Util.RunActionOnMainThreadSync(() => { mainPage.Content = null; }); }); mBackBuffer = new WriteableBitmap( (int)screenWidth, (int)screenHeight); mFrontBuffer = new WriteableBitmap( (int)screenWidth, (int)screenHeight); mainImage.Source = mFrontBuffer; mCurrentDrawTarget = mBackBuffer; mCurrentWindowsColor = System.Windows.Media.Color.FromArgb(0xff, (byte)(mCurrentColor >> 16), (byte)(mCurrentColor >> 8), (byte)(mCurrentColor)); syscalls.maSetColor = delegate(int rgb) { int oldColor = (int)mCurrentColor; mCurrentColor = 0xff000000 | (uint)(rgb & 0xffffff); mCurrentWindowsColor = System.Windows.Media.Color.FromArgb(0xff, (byte)(mCurrentColor >> 16), (byte)(mCurrentColor >> 8), (byte)(mCurrentColor)); return(oldColor & 0xffffff); }; syscalls.maSetClipRect = delegate(int x, int y, int w, int h) { mClipRect.X = x; mClipRect.Y = y; mClipRect.Width = w; mClipRect.Height = h; }; syscalls.maGetClipRect = delegate(int cliprect) { Memory mem = core.GetDataMemory(); mem.WriteInt32(cliprect + MoSync.Struct.MARect.left, (int)mClipRect.X); mem.WriteInt32(cliprect + MoSync.Struct.MARect.top, (int)mClipRect.Y); mem.WriteInt32(cliprect + MoSync.Struct.MARect.width, (int)mClipRect.Width); mem.WriteInt32(cliprect + MoSync.Struct.MARect.height, (int)mClipRect.Height); }; syscalls.maPlot = delegate(int x, int y) { mCurrentDrawTarget.SetPixel(x, y, (int)mCurrentColor); }; syscalls.maUpdateScreen = delegate() { System.Array.Copy(mBackBuffer.Pixels, mFrontBuffer.Pixels, mFrontBuffer.PixelWidth * mFrontBuffer.PixelHeight); InvalidateWriteableBitmapOnMainThread(mFrontBuffer); }; syscalls.maFillRect = delegate(int x, int y, int w, int h) { mCurrentDrawTarget.FillRectangle(x, y, x + w, y + h, (int)mCurrentColor); }; syscalls.maLine = delegate(int x1, int y1, int x2, int y2) { GraphicsUtil.Point p1 = new GraphicsUtil.Point(x1, y1); GraphicsUtil.Point p2 = new GraphicsUtil.Point(x2, y2); if (!GraphicsUtil.ClipLine(p1, p2, (int)mClipRect.X, (int)(mClipRect.X + mClipRect.Width), (int)mClipRect.Y, (int)(mClipRect.Y + mClipRect.Height))) { return; } mCurrentDrawTarget.DrawLine((int)p1.x, (int)p1.y, (int)p2.x, (int)p2.y, (int)mCurrentColor); }; TextBlock textBlock = new TextBlock(); textBlock.FontSize = mCurrentFontSize; syscalls.maDrawText = delegate(int left, int top, int str) { String text = core.GetDataMemory().ReadStringAtAddress(str); if (text.Length == 0) { return; } MoSync.Util.RunActionOnMainThreadSync(() => { textBlock.Text = text; textBlock.Foreground = new SolidColorBrush(mCurrentWindowsColor); WriteableBitmap b = new WriteableBitmap(textBlock, null); mCurrentDrawTarget.Blit(new Rect(left, top, b.PixelWidth, b.PixelHeight), b, new Rect(0, 0, b.PixelWidth, b.PixelHeight)); }); }; syscalls.maGetTextSize = delegate(int str) { String text = core.GetDataMemory().ReadStringAtAddress(str); int textWidth = 0; int textHeight = 0; MoSync.Util.RunActionOnMainThreadSync(() => { textBlock.Text = text; textWidth = (int)textBlock.ActualWidth; textHeight = (int)textBlock.ActualHeight; }); return(MoSync.Util.CreateExtent(textWidth, textHeight)); }; syscalls.maDrawTextW = delegate(int left, int top, int str) { String text = core.GetDataMemory().ReadWStringAtAddress(str); if (text.Length == 0) { return; } MoSync.Util.RunActionOnMainThreadSync(() => { textBlock.Text = text; textBlock.Foreground = new SolidColorBrush(mCurrentWindowsColor); WriteableBitmap b = new WriteableBitmap(textBlock, null); Rect dstRect = new Rect(left, top, b.PixelWidth, b.PixelHeight); Rect srcRect = new Rect(0, 0, b.PixelWidth, b.PixelHeight); // cliprect.. Rect clipRect = new Rect(0, 0, mBackBuffer.PixelWidth, mBackBuffer.PixelHeight); clipRect.Intersect(dstRect); if (clipRect.IsEmpty == true) { return; } mCurrentDrawTarget.Blit(dstRect, b, srcRect); }); }; syscalls.maGetTextSizeW = delegate(int str) { String text = core.GetDataMemory().ReadWStringAtAddress(str); int textWidth = 0; int textHeight = 0; MoSync.Util.RunActionOnMainThreadSync(() => { textBlock.Text = text; textWidth = (int)textBlock.ActualWidth; textHeight = (int)textBlock.ActualHeight; }); return(MoSync.Util.CreateExtent(textWidth, textHeight)); }; syscalls.maFillTriangleFan = delegate(int points, int count) { int[] newPoints = new int[count * 2 + 2]; for (int i = 0; i < count; i++) { newPoints[i * 2 + 0] = core.GetDataMemory().ReadInt32(points + i * 8 + MoSync.Struct.MAPoint2d.x); newPoints[i * 2 + 1] = core.GetDataMemory().ReadInt32(points + i * 8 + MoSync.Struct.MAPoint2d.y); } newPoints[count * 2 + 0] = core.GetDataMemory().ReadInt32(points + MoSync.Struct.MAPoint2d.x); newPoints[count * 2 + 1] = core.GetDataMemory().ReadInt32(points + MoSync.Struct.MAPoint2d.y); mCurrentDrawTarget.FillPolygon(newPoints, (int)mCurrentColor); }; syscalls.maFillTriangleStrip = delegate(int points, int count) { int[] xcoords = new int[count]; int[] ycoords = new int[count]; for (int i = 0; i < count; i++) { xcoords[i] = core.GetDataMemory().ReadInt32(points + i * 8 + MoSync.Struct.MAPoint2d.x); ycoords[i] = core.GetDataMemory().ReadInt32(points + i * 8 + MoSync.Struct.MAPoint2d.y); } for (int i = 2; i < count; i++) { mCurrentDrawTarget.FillTriangle( xcoords[i - 2], ycoords[i - 2], xcoords[i - 1], ycoords[i - 1], xcoords[i - 0], ycoords[i - 0], (int)mCurrentColor); } }; syscalls.maSetDrawTarget = delegate(int drawTarget) { int oldDrawTarget = mCurrentDrawTargetIndex; if (drawTarget == mCurrentDrawTargetIndex) { return(oldDrawTarget); } if (drawTarget == MoSync.Constants.HANDLE_SCREEN) { mCurrentDrawTarget = mBackBuffer; mCurrentDrawTargetIndex = drawTarget; return(oldDrawTarget); } Resource res = runtime.GetResource(MoSync.Constants.RT_IMAGE, drawTarget); mCurrentDrawTarget = (WriteableBitmap)res.GetInternalObject(); mCurrentDrawTargetIndex = drawTarget; return(oldDrawTarget); }; syscalls.maGetScrSize = delegate() { return(MoSync.Util.CreateExtent(mBackBuffer.PixelWidth, mBackBuffer.PixelHeight)); }; syscalls.maGetImageSize = delegate(int handle) { Resource res = runtime.GetResource(MoSync.Constants.RT_IMAGE, handle); BitmapSource src = (BitmapSource)res.GetInternalObject(); int w = 0, h = 0; MoSync.Util.RunActionOnMainThreadSync(() => { w = src.PixelWidth; h = src.PixelHeight; }); return(MoSync.Util.CreateExtent(w, h)); }; syscalls.maDrawImage = delegate(int image, int left, int top) { Resource res = runtime.GetResource(MoSync.Constants.RT_IMAGE, image); WriteableBitmap src = (WriteableBitmap)res.GetInternalObject(); Rect srcRect = new Rect(0, 0, src.PixelWidth, src.PixelHeight); Rect dstRect = new Rect(left, top, src.PixelWidth, src.PixelHeight); mCurrentDrawTarget.Blit(dstRect, src, srcRect, WriteableBitmapExtensions.BlendMode.Alpha); }; syscalls.maDrawImageRegion = delegate(int image, int srcRectPtr, int dstPointPtr, int transformMode) { Resource res = runtime.GetResource(MoSync.Constants.RT_IMAGE, image); WriteableBitmap src = (WriteableBitmap)res.GetInternalObject(); Memory dataMemory = core.GetDataMemory(); int srcRectX = dataMemory.ReadInt32(srcRectPtr + MoSync.Struct.MARect.left); int srcRectY = dataMemory.ReadInt32(srcRectPtr + MoSync.Struct.MARect.top); int srcRectW = dataMemory.ReadInt32(srcRectPtr + MoSync.Struct.MARect.width); int srcRectH = dataMemory.ReadInt32(srcRectPtr + MoSync.Struct.MARect.height); int dstPointX = dataMemory.ReadInt32(dstPointPtr + MoSync.Struct.MAPoint2d.x); int dstPointY = dataMemory.ReadInt32(dstPointPtr + MoSync.Struct.MAPoint2d.y); Rect srcRect = new Rect(srcRectX, srcRectY, srcRectW, srcRectH); Rect dstRect = new Rect(dstPointX, dstPointY, srcRectW, srcRectH); // mCurrentDrawTarget.Blit(dstRect, src, srcRect, WriteableBitmapExtensions.BlendMode.Alpha); GraphicsUtil.DrawImageRegion(mCurrentDrawTarget, dstPointX, dstPointY, srcRect, src, transformMode); }; syscalls.maCreateDrawableImage = delegate(int placeholder, int width, int height) { Resource res = runtime.GetResource(MoSync.Constants.RT_PLACEHOLDER, placeholder); res.SetResourceType(MoSync.Constants.RT_IMAGE); WriteableBitmap bitmap = null; MoSync.Util.RunActionOnMainThreadSync(() => { bitmap = new WriteableBitmap(width, height); }); if (bitmap == null) { return(MoSync.Constants.RES_OUT_OF_MEMORY); } res.SetInternalObject(bitmap); return(MoSync.Constants.RES_OK); }; syscalls.maCreateImageRaw = delegate(int _placeholder, int _src, int _size, int _alpha) { int width = MoSync.Util.ExtentX(_size); int height = MoSync.Util.ExtentY(_size); WriteableBitmap bitmap = null; MoSync.Util.RunActionOnMainThreadSync(() => { bitmap = new WriteableBitmap(width, height); }); //core.GetDataMemory().ReadIntegers(bitmap.Pixels, _src, width * height); bitmap.FromByteArray(core.GetDataMemory().GetData(), _src, width * height * 4); if (_alpha == 0) { int[] pixels = bitmap.Pixels; int numPixels = width * height; for (int i = 0; i < numPixels; i++) { pixels[i] = (int)((uint)pixels[i] | 0xff000000); } } runtime.SetResource(_placeholder, new Resource( bitmap, MoSync.Constants.RT_IMAGE ) ); return(MoSync.Constants.RES_OK); }; syscalls.maDrawRGB = delegate(int _dstPoint, int _src, int _srcRect, int _scanlength) { Memory dataMemory = core.GetDataMemory(); int dstX = dataMemory.ReadInt32(_dstPoint + MoSync.Struct.MAPoint2d.x); int dstY = dataMemory.ReadInt32(_dstPoint + MoSync.Struct.MAPoint2d.y); int srcRectX = dataMemory.ReadInt32(_srcRect + MoSync.Struct.MARect.left); int srcRectY = dataMemory.ReadInt32(_srcRect + MoSync.Struct.MARect.top); int srcRectW = dataMemory.ReadInt32(_srcRect + MoSync.Struct.MARect.width); int srcRectH = dataMemory.ReadInt32(_srcRect + MoSync.Struct.MARect.height); int[] pixels = mCurrentDrawTarget.Pixels; // todo: clipRect _scanlength *= 4; // sizeof(int) for (int h = 0; h < srcRectH; h++) { int pixelIndex = dstY * mCurrentDrawTarget.PixelWidth + dstX; int address = _src + (srcRectY + h) * _scanlength; for (int w = 0; w < srcRectW; w++) { uint srcPixel = dataMemory.ReadUInt32(address); uint dstPixel = (uint)pixels[pixelIndex]; uint srcPixelR = (srcPixel & 0x00ff0000) >> 16; uint srcPixelG = (srcPixel & 0x0000ff00) >> 8; uint srcPixelB = (srcPixel & 0x000000ff) >> 0; uint srcPixelA = (srcPixel & 0xff000000) >> 24; uint dstPixelR = (dstPixel & 0x00ff0000) >> 16; uint dstPixelG = (dstPixel & 0x0000ff00) >> 8; uint dstPixelB = (dstPixel & 0x000000ff) >> 0; uint dstPixelA = (dstPixel & 0xff000000) >> 24; dstPixelR += ((srcPixelR - dstPixelR) * srcPixelA) / 255; dstPixelG += ((srcPixelG - dstPixelG) * srcPixelA) / 255; dstPixelB += ((srcPixelB - dstPixelB) * srcPixelA) / 255; dstPixel = (dstPixelA << 24) | (dstPixelR << 16) | (dstPixelG << 8) | (dstPixelB); pixels[pixelIndex] = (int)dstPixel; address += 4; pixelIndex++; } dstY++; } }; syscalls.maGetImageData = delegate(int _image, int _dst, int _srcRect, int _scanlength) { Resource res = runtime.GetResource(MoSync.Constants.RT_IMAGE, _image); WriteableBitmap src = (WriteableBitmap)res.GetInternalObject(); Memory dataMemory = core.GetDataMemory(); int srcRectX = dataMemory.ReadInt32(_srcRect + MoSync.Struct.MARect.left); int srcRectY = dataMemory.ReadInt32(_srcRect + MoSync.Struct.MARect.top); int srcRectW = dataMemory.ReadInt32(_srcRect + MoSync.Struct.MARect.width); int srcRectH = dataMemory.ReadInt32(_srcRect + MoSync.Struct.MARect.height); int lineDst = _dst; byte[] data = src.ToByteArray(srcRectY * src.PixelWidth, srcRectH * src.PixelWidth); byte[] coreArray = dataMemory.GetData(); for (int y = 0; y < srcRectH; y++) { System.Array.Copy(data, y * src.PixelWidth * 4, coreArray, lineDst, src.PixelWidth * 4); lineDst += _scanlength * 4; } }; syscalls.maCreateImageFromData = delegate(int _placeholder, int _data, int _offset, int _size) { Resource res = runtime.GetResource(MoSync.Constants.RT_BINARY, _data); Memory mem = (Memory)res.GetInternalObject(); Stream s = mem.GetStream(_offset, _size); WriteableBitmap bitmap = MoSync.Util.CreateWriteableBitmapFromStream(s); s.Close(); runtime.SetResource( _placeholder, new Resource( bitmap, MoSync.Constants.RT_IMAGE ) ); return(MoSync.Constants.RES_OK); }; }
public void Init(Syscalls syscalls, Core core, Runtime runtime) { PhoneApplicationFrame frame = (PhoneApplicationFrame)Application.Current.RootVisual; double screenWidth = System.Windows.Application.Current.Host.Content.ActualWidth; double screenHeight = System.Windows.Application.Current.Host.Content.ActualHeight; if ((int)screenHeight == 0) { throw new Exception("screenHeight"); } PhoneApplicationPage mainPage = (PhoneApplicationPage)frame.Content; mMainImage = new Image(); mainPage.Width = screenWidth; mainPage.Height = screenHeight; mMainImage.Width = screenWidth; mMainImage.Height = screenHeight; mainPage.Content = mMainImage; mClipRect.X = 0.0; mClipRect.Y = 0.0; mClipRect.Width = screenWidth; mClipRect.Height = screenHeight; // no apparent effect on memory leaks. runtime.RegisterCleaner(delegate() { MoSync.Util.RunActionOnMainThreadSync(() => { mainPage.Content = null; }); }); mBackBuffer = new WriteableBitmap( (int)screenWidth, (int)screenHeight); mFrontBuffer = new WriteableBitmap( (int)screenWidth, (int)screenHeight); mMainImage.Source = mFrontBuffer; // clear front and backbuffer. for (int i = 0; i < mFrontBuffer.PixelWidth * mFrontBuffer.PixelHeight; i++) { mBackBuffer.Pixels[i] = mBackBuffer.Pixels[i] = (int)(0xff << 24); } mCurrentDrawTarget = mBackBuffer; mCurrentWindowsColor = System.Windows.Media.Color.FromArgb(0xff, (byte)(mCurrentColor >> 16), (byte)(mCurrentColor >> 8), (byte)(mCurrentColor)); syscalls.maSetColor = delegate(int rgb) { int oldColor = (int)mCurrentColor; mCurrentColor = 0xff000000 | (uint)(rgb & 0xffffff); mCurrentWindowsColor = System.Windows.Media.Color.FromArgb(0xff, (byte)(mCurrentColor >> 16), (byte)(mCurrentColor >> 8), (byte)(mCurrentColor)); return(oldColor & 0xffffff); }; syscalls.maSetClipRect = delegate(int x, int y, int w, int h) { MoSync.GraphicsUtil.ClipRectangle( x, y, w, h, 0, 0, mCurrentDrawTarget.PixelWidth, mCurrentDrawTarget.PixelHeight, out x, out y, out w, out h); mClipRect.X = x; mClipRect.Y = y; mClipRect.Width = w; mClipRect.Height = h; }; syscalls.maGetClipRect = delegate(int cliprect) { #if !LIB Memory mem = core.GetDataMemory(); #else SystemMemory mem = core.GetDataMemory(); #endif mem.WriteInt32(cliprect + MoSync.Struct.MARect.left, (int)mClipRect.X); mem.WriteInt32(cliprect + MoSync.Struct.MARect.top, (int)mClipRect.Y); mem.WriteInt32(cliprect + MoSync.Struct.MARect.width, (int)mClipRect.Width); mem.WriteInt32(cliprect + MoSync.Struct.MARect.height, (int)mClipRect.Height); }; syscalls.maPlot = delegate(int x, int y) { mCurrentDrawTarget.SetPixel(x, y, (int)mCurrentColor); }; syscalls.maUpdateScreen = delegate() { //System.Array.Copy(mBackBuffer.Pixels, mFrontBuffer.Pixels, mFrontBuffer.PixelWidth * mFrontBuffer.PixelHeight); System.Buffer.BlockCopy(mBackBuffer.Pixels, 0, mFrontBuffer.Pixels, 0, mFrontBuffer.PixelWidth * mFrontBuffer.PixelHeight * 4); InvalidateWriteableBitmapOnMainThread(mFrontBuffer); }; syscalls.maFillRect = delegate(int x, int y, int w, int h) { // this function has a bug (it only fills one pixel less than the image.) //mCurrentDrawTarget.FillRectangle(x, y, x + w, y + h, (int)mCurrentColor); MoSync.GraphicsUtil.ClipRectangle( x, y, w, h, 0, 0, mCurrentDrawTarget.PixelWidth, mCurrentDrawTarget.PixelHeight, out x, out y, out w, out h); MoSync.GraphicsUtil.ClipRectangle( x, y, w, h, (int)mClipRect.X, (int)mClipRect.Y, (int)mClipRect.Width, (int)mClipRect.Height, out x, out y, out w, out h); if (w <= 0 || h <= 0) { return; } int index = x + y * mCurrentDrawTarget.PixelWidth; while (h-- != 0) { int width = w; while (width-- != 0) { mCurrentDrawTarget.Pixels[index] = (int)mCurrentColor; index++; } index += -w + mCurrentDrawTarget.PixelWidth; } }; syscalls.maLine = delegate(int x1, int y1, int x2, int y2) { GraphicsUtil.Point p1 = new GraphicsUtil.Point(x1, y1); GraphicsUtil.Point p2 = new GraphicsUtil.Point(x2, y2); if (!GraphicsUtil.ClipLine(p1, p2, (int)mClipRect.X, (int)(mClipRect.X + mClipRect.Width), (int)mClipRect.Y, (int)(mClipRect.Y + mClipRect.Height))) { return; } mCurrentDrawTarget.DrawLine((int)p1.x, (int)p1.y, (int)p2.x, (int)p2.y, (int)mCurrentColor); }; textBlock.FontSize = mCurrentFontSize; syscalls.maDrawText = delegate(int left, int top, int str) { String text = core.GetDataMemory().ReadStringAtAddress(str); if (text.Length == 0) { return; } DrawText(text, left, top); }; syscalls.maGetTextSize = delegate(int str) { String text = core.GetDataMemory().ReadStringAtAddress(str); int textWidth = 0; int textHeight = 0; GetTextSize(text, out textWidth, out textHeight); return(MoSync.Util.CreateExtent(textWidth, textHeight)); }; syscalls.maDrawTextW = delegate(int left, int top, int str) { String text = core.GetDataMemory().ReadWStringAtAddress(str); if (text.Length == 0) { return; } DrawText(text, left, top); }; syscalls.maGetTextSizeW = delegate(int str) { String text = core.GetDataMemory().ReadWStringAtAddress(str); int textWidth = 0; int textHeight = 0; GetTextSize(text, out textWidth, out textHeight); return(MoSync.Util.CreateExtent(textWidth, textHeight)); }; syscalls.maFillTriangleFan = delegate(int points, int count) { int[] newPoints = new int[count * 2 + 2]; for (int i = 0; i < count; i++) { newPoints[i * 2 + 0] = core.GetDataMemory().ReadInt32(points + i * 8 + MoSync.Struct.MAPoint2d.x); newPoints[i * 2 + 1] = core.GetDataMemory().ReadInt32(points + i * 8 + MoSync.Struct.MAPoint2d.y); } newPoints[count * 2 + 0] = core.GetDataMemory().ReadInt32(points + MoSync.Struct.MAPoint2d.x); newPoints[count * 2 + 1] = core.GetDataMemory().ReadInt32(points + MoSync.Struct.MAPoint2d.y); mCurrentDrawTarget.FillPolygon(newPoints, (int)mCurrentColor); }; syscalls.maFillTriangleStrip = delegate(int points, int count) { int[] xcoords = new int[count]; int[] ycoords = new int[count]; for (int i = 0; i < count; i++) { xcoords[i] = core.GetDataMemory().ReadInt32(points + i * 8 + MoSync.Struct.MAPoint2d.x); ycoords[i] = core.GetDataMemory().ReadInt32(points + i * 8 + MoSync.Struct.MAPoint2d.y); } for (int i = 2; i < count; i++) { mCurrentDrawTarget.FillTriangle( xcoords[i - 2], ycoords[i - 2], xcoords[i - 1], ycoords[i - 1], xcoords[i - 0], ycoords[i - 0], (int)mCurrentColor); } }; syscalls.maSetDrawTarget = delegate(int drawTarget) { int oldDrawTarget = mCurrentDrawTargetIndex; if (drawTarget == mCurrentDrawTargetIndex) { return(oldDrawTarget); } if (drawTarget == MoSync.Constants.HANDLE_SCREEN) { mCurrentDrawTarget = mBackBuffer; mCurrentDrawTargetIndex = drawTarget; return(oldDrawTarget); } Resource res = runtime.GetResource(MoSync.Constants.RT_IMAGE, drawTarget); mCurrentDrawTarget = (WriteableBitmap)res.GetInternalObject(); mCurrentDrawTargetIndex = drawTarget; return(oldDrawTarget); }; syscalls.maGetScrSize = delegate() { int w = mBackBuffer.PixelWidth; int h = mBackBuffer.PixelHeight; MoSync.Util.RunActionOnMainThreadSync(() => { // if the orientation is landscape, the with and height should be swaped PhoneApplicationPage currentPage = (((PhoneApplicationFrame)Application.Current.RootVisual).Content as PhoneApplicationPage); if (currentPage.Orientation == PageOrientation.Landscape || currentPage.Orientation == PageOrientation.LandscapeLeft || currentPage.Orientation == PageOrientation.LandscapeRight) { int aux = w; w = h; h = aux; } }); return(MoSync.Util.CreateExtent(w, h)); }; syscalls.maGetImageSize = delegate(int handle) { Resource res = runtime.GetResource(MoSync.Constants.RT_IMAGE, handle); int w = 0, h = 0; #if !LIB BitmapSource src = (BitmapSource)res.GetInternalObject(); if (src == null) { MoSync.Util.CriticalError("maGetImageSize used with an invalid image resource."); } else { MoSync.Util.RunActionOnMainThreadSync(() => { w = src.PixelWidth; h = src.PixelHeight; }); } #else if (res.GetInternalObject() == null) { MoSync.Util.CriticalError("maGetImageSize used with an invalid image resource."); } else { MoSync.Util.RunActionOnMainThreadSync(() => { if (res.GetInternalObject() is Stream) { BitmapImage bi = new BitmapImage(); bi.SetSource((Stream)res.GetInternalObject()); w = bi.PixelWidth; h = bi.PixelHeight; } else if (res.GetInternalObject() is WriteableBitmap) { BitmapSource src = (BitmapSource)res.GetInternalObject(); w = src.PixelWidth; h = src.PixelHeight; } }); } #endif return(MoSync.Util.CreateExtent(w, h)); }; syscalls.maDrawImage = delegate(int image, int left, int top) { Resource res = runtime.GetResource(MoSync.Constants.RT_IMAGE, image); #if !LIB WriteableBitmap src = (WriteableBitmap)res.GetInternalObject(); #else Object src = null; MoSync.Util.RunActionOnMainThreadSync(() => { Object internalObj = res.GetInternalObject(); if (internalObj is Stream) { src = new WriteableBitmap(0, 0); (src as WriteableBitmap).SetSource((Stream)res.GetInternalObject()); } else if (internalObj is WriteableBitmap) { src = res.GetInternalObject(); } }); #endif Rect srcRect = new Rect(0, 0, (src as WriteableBitmap).PixelWidth, (src as WriteableBitmap).PixelHeight); Rect dstRect = new Rect(left, top, (src as WriteableBitmap).PixelWidth, (src as WriteableBitmap).PixelHeight); mCurrentDrawTarget.Blit(dstRect, (src as WriteableBitmap), srcRect, WriteableBitmapExtensions.BlendMode.Alpha); }; syscalls.maDrawImageRegion = delegate(int image, int srcRectPtr, int dstPointPtr, int transformMode) { Resource res = runtime.GetResource(MoSync.Constants.RT_IMAGE, image); #if !LIB WriteableBitmap src = (WriteableBitmap)res.GetInternalObject(); Memory dataMemory = core.GetDataMemory(); #else SystemMemory dataMemory = core.GetDataMemory(); Object src = null; MoSync.Util.RunActionOnMainThreadSync(() => { Object internalObj = res.GetInternalObject(); if (internalObj is Stream) { src = new WriteableBitmap(0, 0); (src as WriteableBitmap).SetSource((Stream)res.GetInternalObject()); } else if (internalObj is WriteableBitmap) { src = res.GetInternalObject(); } }); #endif int srcRectX = dataMemory.ReadInt32(srcRectPtr + MoSync.Struct.MARect.left); int srcRectY = dataMemory.ReadInt32(srcRectPtr + MoSync.Struct.MARect.top); int srcRectW = dataMemory.ReadInt32(srcRectPtr + MoSync.Struct.MARect.width); int srcRectH = dataMemory.ReadInt32(srcRectPtr + MoSync.Struct.MARect.height); int dstPointX = dataMemory.ReadInt32(dstPointPtr + MoSync.Struct.MAPoint2d.x); int dstPointY = dataMemory.ReadInt32(dstPointPtr + MoSync.Struct.MAPoint2d.y); Rect srcRect = new Rect(srcRectX, srcRectY, srcRectW, srcRectH); Rect dstRect = new Rect(dstPointX, dstPointY, srcRectW, srcRectH); GraphicsUtil.DrawImageRegion(mCurrentDrawTarget, dstPointX, dstPointY, srcRect, (src as WriteableBitmap), transformMode, mClipRect); }; syscalls.maCreateDrawableImage = delegate(int placeholder, int width, int height) { Resource res = runtime.GetResource(MoSync.Constants.RT_PLACEHOLDER, placeholder); res.SetResourceType(MoSync.Constants.RT_IMAGE); WriteableBitmap bitmap = null; MoSync.Util.RunActionOnMainThreadSync(() => { bitmap = new WriteableBitmap(width, height); for (int i = 0; i < bitmap.PixelWidth * bitmap.PixelHeight; i++) { bitmap.Pixels[i] = (int)(0xff << 24); } }); if (bitmap == null) { return(MoSync.Constants.RES_OUT_OF_MEMORY); } res.SetInternalObject(bitmap); return(MoSync.Constants.RES_OK); }; syscalls.maCreateImageRaw = delegate(int _placeholder, int _src, int _size, int _alpha) { int width = MoSync.Util.ExtentX(_size); int height = MoSync.Util.ExtentY(_size); WriteableBitmap bitmap = null; MoSync.Util.RunActionOnMainThreadSync(() => { bitmap = new WriteableBitmap(width, height); }); //core.GetDataMemory().ReadIntegers(bitmap.Pixels, _src, width * height); #if !LIB bitmap.FromByteArray(core.GetDataMemory().GetData(), _src, width * height * 4); #else byte[] bytes = new byte[width * height * 4]; core.GetDataMemory().ReadBytes(bytes, _src, width * height * 4); bitmap.FromByteArray(bytes, 0, width * height * 4); // TO BE TESTED #endif if (_alpha == 0) { int[] pixels = bitmap.Pixels; int numPixels = width * height; for (int i = 0; i < numPixels; i++) { pixels[i] = (int)((uint)pixels[i] | 0xff000000); } } Resource res = runtime.GetResource(MoSync.Constants.RT_PLACEHOLDER, _placeholder); res.SetInternalObject(bitmap); res.SetResourceType(MoSync.Constants.RT_IMAGE); return(MoSync.Constants.RES_OK); }; syscalls.maDrawRGB = delegate(int _dstPoint, int _src, int _srcRect, int _scanlength) { #if !LIB Memory dataMemory = core.GetDataMemory(); #else SystemMemory dataMemory = core.GetDataMemory(); #endif int dstX = dataMemory.ReadInt32(_dstPoint + MoSync.Struct.MAPoint2d.x); int dstY = dataMemory.ReadInt32(_dstPoint + MoSync.Struct.MAPoint2d.y); int srcRectX = dataMemory.ReadInt32(_srcRect + MoSync.Struct.MARect.left); int srcRectY = dataMemory.ReadInt32(_srcRect + MoSync.Struct.MARect.top); int srcRectW = dataMemory.ReadInt32(_srcRect + MoSync.Struct.MARect.width); int srcRectH = dataMemory.ReadInt32(_srcRect + MoSync.Struct.MARect.height); int[] pixels = mCurrentDrawTarget.Pixels; // todo: clipRect _scanlength *= 4; // sizeof(int) for (int h = 0; h < srcRectH; h++) { int pixelIndex = dstY * mCurrentDrawTarget.PixelWidth + dstX; int address = _src + (srcRectY + h) * _scanlength; for (int w = 0; w < srcRectW; w++) { uint srcPixel = dataMemory.ReadUInt32(address); uint dstPixel = (uint)pixels[pixelIndex]; uint srcPixelR = (srcPixel & 0x00ff0000) >> 16; uint srcPixelG = (srcPixel & 0x0000ff00) >> 8; uint srcPixelB = (srcPixel & 0x000000ff) >> 0; uint srcPixelA = (srcPixel & 0xff000000) >> 24; uint dstPixelR = (dstPixel & 0x00ff0000) >> 16; uint dstPixelG = (dstPixel & 0x0000ff00) >> 8; uint dstPixelB = (dstPixel & 0x000000ff) >> 0; uint dstPixelA = (dstPixel & 0xff000000) >> 24; dstPixelR += ((srcPixelR - dstPixelR) * srcPixelA) / 255; dstPixelG += ((srcPixelG - dstPixelG) * srcPixelA) / 255; dstPixelB += ((srcPixelB - dstPixelB) * srcPixelA) / 255; dstPixel = (dstPixelA << 24) | (dstPixelR << 16) | (dstPixelG << 8) | (dstPixelB); pixels[pixelIndex] = (int)dstPixel; address += 4; pixelIndex++; } dstY++; } }; syscalls.maGetImageData = delegate(int _image, int _dst, int _srcRect, int _scanlength) { Resource res = runtime.GetResource(MoSync.Constants.RT_IMAGE, _image); WriteableBitmap src = (WriteableBitmap)res.GetInternalObject(); #if !LIB Memory dataMemory = core.GetDataMemory(); #else SystemMemory dataMemory = core.GetDataMemory(); #endif int srcRectX = dataMemory.ReadInt32(_srcRect + MoSync.Struct.MARect.left); int srcRectY = dataMemory.ReadInt32(_srcRect + MoSync.Struct.MARect.top); int srcRectW = dataMemory.ReadInt32(_srcRect + MoSync.Struct.MARect.width); int srcRectH = dataMemory.ReadInt32(_srcRect + MoSync.Struct.MARect.height); int lineDst = _dst; byte[] data = src.ToByteArray(srcRectY * src.PixelWidth, srcRectH * src.PixelWidth); // BlockCopy? #if !LIB byte[] coreArray = dataMemory.GetData(); #else byte[] coreArray = new byte[_scanlength]; dataMemory.ReadBytes(coreArray, _srcRect, _scanlength); #endif for (int y = 0; y < srcRectH; y++) { System.Array.Copy(data, y * src.PixelWidth * 4, coreArray, lineDst, src.PixelWidth * 4); lineDst += _scanlength * 4; } }; syscalls.maCreateImageFromData = delegate(int _placeholder, int _data, int _offset, int _size) { Resource res = runtime.GetResource(MoSync.Constants.RT_BINARY, _data); if (res == null) { return(MoSync.Constants.RES_BAD_INPUT); } Stream bin = (Stream)res.GetInternalObject(); if (bin == null) { return(MoSync.Constants.RES_BAD_INPUT); } BoundedStream s = new BoundedStream(bin, _offset, _size); WriteableBitmap bitmap = MoSync.Util.CreateWriteableBitmapFromStream(s); s.Close(); if (bitmap == null) { return(MoSync.Constants.RES_BAD_INPUT); } Resource imageRes = runtime.GetResource(MoSync.Constants.RT_PLACEHOLDER, _placeholder); imageRes.SetInternalObject(bitmap); imageRes.SetResourceType(MoSync.Constants.RT_IMAGE); return(MoSync.Constants.RES_OK); }; }
public bool LoadResources(Stream file, bool initial = true) { if (MoSync.Util.StreamReadInt8(file) != 'M') { return(false); } if (MoSync.Util.StreamReadInt8(file) != 'A') { return(false); } if (MoSync.Util.StreamReadInt8(file) != 'R') { return(false); } if (MoSync.Util.StreamReadInt8(file) != 'S') { return(false); } uint numResources = ReadUnsignedVarInt(file); uint resSize = ReadUnsignedVarInt(file); mCurrentResourceHandle = 1; mStaticResourceCount = 0; while (true) { byte type = MoSync.Util.StreamReadUint8(file); if (type == 0) { break; } uint size = ReadUnsignedVarInt(file); Util.Log("Resource type " + type + ", size " + size + "\n"); Resource resource = new Resource(null, type); mResources.Add(mCurrentResourceHandle, resource); if (initial && ( type == MoSync.Constants.RT_BINARY || type == MoSync.Constants.RT_IMAGE || type == MoSync.Constants.RT_SPRITE )) { resource.SetFileStream(new BoundedStream(file, file.Position, (long)size)); #if LIB //This needs to be investigated. If I don't call this some syscalls will fail since mInternalObject is null. Used to //work on the previous system. resource.SetInternalObject(new BoundedStream(file, file.Position, (long)size)); #endif file.Seek(size, SeekOrigin.Current); } else { LoadResource(file, type, size, resource); } mCurrentResourceHandle++; mStaticResourceCount++; } return(true); }
public void Init(Syscalls syscalls, Core core, Runtime runtime) { syscalls.memset = delegate(int dst, int val, int num) { core.GetDataMemory().FillRange(dst, (byte)val, num); return(dst); }; syscalls.memcpy = delegate(int dst, int src, int num) { core.GetDataMemory().WriteMemoryAtAddress(dst, core.GetDataMemory(), src, num); return(dst); }; syscalls.strcpy = delegate(int dst, int src) { byte[] mem = core.GetDataMemory().GetData(); int origDst = dst; src--; do { src++; mem[dst] = mem[src]; dst++; } while (mem[src] != 0); return(origDst); }; syscalls.strcmp = delegate(int str1, int str2) { byte[] mem = core.GetDataMemory().GetData(); while (mem[str1] != 0 && mem[str1] == mem[str2]) { str1++; str2++; } return(mem[str1] - mem[str2]); }; syscalls.maCreateData = delegate(int placeholder, int size) { MemoryStream mem = null; try { mem = new MemoryStream(size); mem.SetLength(size); } catch (OutOfMemoryException e) { MoSync.Util.Log(e); return(MoSync.Constants.RES_OUT_OF_MEMORY); } Resource res = runtime.GetResource(MoSync.Constants.RT_PLACEHOLDER, placeholder); res.SetInternalObject(mem); res.SetResourceType(MoSync.Constants.RT_BINARY); return(MoSync.Constants.RES_OK); }; syscalls.maWriteData = delegate(int data, int src, int offset, int size) { Resource res = runtime.GetResource(MoSync.Constants.RT_BINARY, data); Stream mem = (Stream)res.GetInternalObject(); mem.Seek(offset, SeekOrigin.Begin); mem.Write(core.GetDataMemory().GetData(), src, size); }; syscalls.maReadData = delegate(int data, int dst, int offset, int size) { Resource res = runtime.GetResource(MoSync.Constants.RT_BINARY, data); Stream mem = (Stream)res.GetInternalObject(); mem.Seek(offset, SeekOrigin.Begin); mem.Read(core.GetDataMemory().GetData(), dst, size); }; syscalls.maGetDataSize = delegate(int data) { Resource res = runtime.GetResource(MoSync.Constants.RT_BINARY, data); Stream mem = (Stream)res.GetInternalObject(); return((int)mem.Length); }; syscalls.maCopyData = delegate(int _params) { throw new Exception("maCopyData not implemented"); }; }
public void Init(Syscalls syscalls, Core core, Runtime runtime) { syscalls.memset = delegate(int dst, int val, int num) { core.GetDataMemory().FillRange(dst, (byte)val, num); return(dst); }; syscalls.memcpy = delegate(int dst, int src, int num) { core.GetDataMemory().WriteMemoryAtAddress(dst, core.GetDataMemory(), src, num); return(dst); }; syscalls.strcpy = delegate(int dst, int src) { #if !LIB byte[] mem = core.GetDataMemory().GetData(); int origDst = dst; src--; do { src++; mem[dst] = mem[src]; dst++; } while (mem[src] != 0); return(origDst); #else string source = core.GetDataMemory().ReadStringAtAddress(src); Byte[] bytes = new Byte[source.Length * sizeof(char)]; System.Buffer.BlockCopy(source.ToCharArray(), 0, bytes, 0, bytes.Length); core.GetDataMemory().WriteBytes(dst, bytes, source.Length * sizeof(char)); return(dst); #endif }; syscalls.strcmp = delegate(int str1, int str2) { #if !LIB byte[] mem = core.GetDataMemory().GetData(); while (mem[str1] != 0 && mem[str1] == mem[str2]) { str1++; str2++; } return(mem[str1] - mem[str2]); #else string s1 = core.GetDataMemory().ReadStringAtAddress(str1); string s2 = core.GetDataMemory().ReadStringAtAddress(str2); return(s1.CompareTo(s2)); #endif }; syscalls.maCreateData = delegate(int placeholder, int size) { MemoryStream mem = null; try { mem = new MemoryStream(size); mem.SetLength(size); } catch (OutOfMemoryException e) { MoSync.Util.Log(e); return(MoSync.Constants.RES_OUT_OF_MEMORY); } Resource res = runtime.GetResource(MoSync.Constants.RT_PLACEHOLDER, placeholder); res.SetInternalObject(mem); res.SetResourceType(MoSync.Constants.RT_BINARY); return(MoSync.Constants.RES_OK); }; syscalls.maWriteData = delegate(int data, int src, int offset, int size) { Resource res = runtime.GetResource(MoSync.Constants.RT_BINARY, data); Stream mem = (Stream)res.GetInternalObject(); mem.Seek(offset, SeekOrigin.Begin); #if !LIB mem.Write(core.GetDataMemory().GetData(), src, size); #else byte[] bytes = new byte[size]; core.GetDataMemory().ReadBytes(bytes, src, size); mem.Write(bytes, 0, size); //TO BE TESTED #endif }; syscalls.maReadData = delegate(int data, int dst, int offset, int size) { Resource res = runtime.GetResource(MoSync.Constants.RT_BINARY, data); Stream mem = (Stream)res.GetInternalObject(); mem.Seek(offset, SeekOrigin.Begin); #if !LIB mem.Read(core.GetDataMemory().GetData(), dst, size); #else byte[] bytes = new byte[size]; mem.Read(bytes, 0, size); core.GetDataMemory().WriteBytes(dst, bytes, size); //TO BE TESTED #endif }; syscalls.maGetDataSize = delegate(int data) { Resource res = runtime.GetResource(MoSync.Constants.RT_BINARY, data); Stream mem = (Stream)res.GetInternalObject(); return((int)mem.Length); }; syscalls.maCopyData = delegate(int _params) { throw new Exception("maCopyData not implemented"); }; }
/** * Initializing the ioctls. */ public void Init(Ioctls ioctls, Core core, Runtime runtime) { mCamera = new PhotoCamera(mCameraType); mVideoBrush = new VideoBrush(); runtime.RegisterCleaner(delegate() { if (null != mCamera) { mCamera.Dispose(); mCamera = null; } }); mRuntime = runtime; PhoneApplicationPage currentPage = (((PhoneApplicationFrame)Application.Current.RootVisual).Content as PhoneApplicationPage); // set the initial camera orientation in respect to the current page orientation SetInitialCameraOrientation(currentPage); // handle current page orientation and adjust the camera orientation accordingly HandleDeviceOrientation(currentPage); /** * Stores an output format in fmm parameter. * @param _index int the index of the required format. * @param _fmt int the momory address at which to write the output format dimensions. * * Note: the _index should be greater than 0 and smaller than the number of camera formats. */ ioctls.maCameraFormat = delegate(int _index, int _fmt) { System.Windows.Size dim; if (GetCameraFormat(_index, out dim) == false) { return(MoSync.Constants.MA_CAMERA_RES_FAILED); } core.GetDataMemory().WriteInt32(_fmt + MoSync.Struct.MA_CAMERA_FORMAT.width, (int)dim.Width); core.GetDataMemory().WriteInt32(_fmt + MoSync.Struct.MA_CAMERA_FORMAT.height, (int)dim.Height); return(MoSync.Constants.MA_CAMERA_RES_OK); }; /** * Returns the number of different output formats supported by the current device's camera. * \< 0 if there is no camera support. * 0 if there is camera support, but the format is unknown. */ ioctls.maCameraFormatNumber = delegate() { // if the camera is not initialized, we cannot access any of its properties if (!isCameraInitialized) { // because the cammera is supported but not initialized, we return 0 return(0); } IEnumerable <System.Windows.Size> res = mCamera.AvailableResolutions; if (res == null) { return(0); } IEnumerator <System.Windows.Size> resolutions = res.GetEnumerator(); resolutions.MoveNext(); int number = 0; while (resolutions.Current != null) { number++; resolutions.MoveNext(); if (resolutions.Current == new System.Windows.Size(0, 0)) { break; } } return(number); }; /** * Starts the viewfinder and the camera */ ioctls.maCameraStart = delegate() { if (isCameraSnapshotInProgress) { return(MoSync.Constants.MA_CAMERA_RES_SNAPSHOT_IN_PROGRESS); } InitCamera(); MoSync.Util.RunActionOnMainThreadSync(() => { mCameraPrev.StartViewFinder(); }); return(0); }; /** * stops the view finder and the camera. */ ioctls.maCameraStop = delegate() { if (isCameraSnapshotInProgress) { // We need to post snapshot failed if the camera was stopped during snapshot operation postSnapshotEvent(snapshotPlaceHolder, mCamera.Resolution, MoSync.Constants.MA_IMAGE_REPRESENTATION_UNKNOWN, MoSync.Constants.MA_CAMERA_RES_FAILED); isCameraSnapshotInProgress = false; } MoSync.Util.RunActionOnMainThreadSync(() => { mCameraPrev.StopViewFinder(); }); return(0); }; /** * Adds a previewWidget to the camera controller in devices that support native UI. */ ioctls.maCameraSetPreview = delegate(int _widgetHandle) { // if the camera is not initialized, we need to initialize it before // setting the preview if (!isCameraInitialized) { InitCamera(); } IWidget w = runtime.GetModule <NativeUIModule>().GetWidget(_widgetHandle); if (w.GetType() != typeof(MoSync.NativeUI.CameraPreview)) { return(MoSync.Constants.MA_CAMERA_RES_FAILED); } mCameraPrev = (NativeUI.CameraPreview)w; mCameraPrev.SetViewFinderContent(mVideoBrush); return(MoSync.Constants.MA_CAMERA_RES_OK); }; /** * Returns the number of available Camera on the device. */ ioctls.maCameraNumber = delegate() { if (PhotoCamera.IsCameraTypeSupported(CameraType.FrontFacing) && PhotoCamera.IsCameraTypeSupported(CameraType.Primary)) { return(2); } else if (PhotoCamera.IsCameraTypeSupported(CameraType.FrontFacing) || PhotoCamera.IsCameraTypeSupported(CameraType.Primary)) { return(1); } return(0); }; /** * Captures an image and stores it as a new data object in the * supplied placeholder. * @param _formatIndex int the required format. * @param _placeHolder int the placeholder used for storing the image. */ ioctls.maCameraSnapshot = delegate(int _formatIndex, int _placeHolder) { if (isCameraSnapshotInProgress) { return(MoSync.Constants.MA_CAMERA_RES_SNAPSHOT_IN_PROGRESS); } // If MA_CAMERA_SNAPSHOT_MAX_SIZE is sent via _formatIndex then we // need to select the biggest available snapshot size/resolution. if (MoSync.Constants.MA_CAMERA_SNAPSHOT_MAX_SIZE == _formatIndex) { _formatIndex = (int)ioctls.maCameraFormatNumber() - 1; } AutoResetEvent are = new AutoResetEvent(false); System.Windows.Size dim; if (GetCameraFormat(_formatIndex, out dim) == false) { return(MoSync.Constants.MA_CAMERA_RES_FAILED); } mCamera.Resolution = dim; if (mCameraSnapshotDelegate != null) { mCamera.CaptureImageAvailable -= mCameraSnapshotDelegate; } mCameraSnapshotDelegate = delegate(object o, ContentReadyEventArgs args) { MoSync.Util.RunActionOnMainThreadSync(() => { Resource res = runtime.GetResource(MoSync.Constants.RT_PLACEHOLDER, _placeHolder); Stream data = null; try { // as the camera always takes a snapshot in landscape left orientation, // we need to rotate the resulting image 90 degrees for a current PortraitUp orientation // and 180 degrees for a current LandscapeRight orientation int rotateAngle = 0; if (currentPage.Orientation == PageOrientation.PortraitUp) { rotateAngle = 90; } else if (currentPage.Orientation == PageOrientation.LandscapeRight) { rotateAngle = 180; } // if the current page is in a LandscapeLeft orientation, the orientation angle will be 0 data = RotateImage(args.ImageStream, rotateAngle); } catch { // the orientation angle was not a multiple of 90 - we keep the original image data = args.ImageStream; } MemoryStream dataMem = new MemoryStream((int)data.Length); MoSync.Util.CopySeekableStreams(data, 0, dataMem, 0, (int)data.Length); res.SetInternalObject(dataMem); }); are.Set(); }; mCamera.CaptureImageAvailable += mCameraSnapshotDelegate; mCamera.CaptureImage(); are.WaitOne(); return(MoSync.Constants.MA_CAMERA_RES_OK); }; /** * Captures an image and stores it as a new data object in new * placeholder that is sent via #EVENT_TYPE_CAMERA_SNAPSHOT event. * @param _placeHolder int the placeholder used for storing the image. * @param _sizeIndex int the required size index. */ ioctls.maCameraSnapshotAsync = delegate(int _placeHolder, int _sizeIndex) { if (isCameraSnapshotInProgress) { return(MoSync.Constants.MA_CAMERA_RES_SNAPSHOT_IN_PROGRESS); } // If MA_CAMERA_SNAPSHOT_MAX_SIZE is sent via _sizeIndex then we // need to select the biggest available snapshot size/resolution. if (MoSync.Constants.MA_CAMERA_SNAPSHOT_MAX_SIZE == _sizeIndex) { _sizeIndex = (int)ioctls.maCameraFormatNumber() - 1; } System.Windows.Size dim; if (GetCameraFormat(_sizeIndex, out dim) == false) { return(MoSync.Constants.MA_CAMERA_RES_FAILED); } mCamera.Resolution = dim; if (mCameraSnapshotDelegate != null) { mCamera.CaptureImageAvailable -= mCameraSnapshotDelegate; } mCameraSnapshotDelegate = delegate(object o, ContentReadyEventArgs args) { MoSync.Util.RunActionOnMainThreadSync(() => { // If the camera was stopped and this delegate was still called then we do nothing here // because in maCameraStop we already send the snapshot failed event. if (!isCameraSnapshotInProgress) { return; } Stream data = null; try { // as the camera always takes a snapshot in landscape left orientation, // we need to rotate the resulting image 90 degrees for a current PortraitUp orientation // and 180 degrees for a current LandscapeRight orientation int rotateAngle = 0; if (currentPage.Orientation == PageOrientation.PortraitUp) { // This is for the front camera. if (mCamera.CameraType != CameraType.Primary) { rotateAngle = 270; } else { rotateAngle = 90; } } else if (currentPage.Orientation == PageOrientation.LandscapeRight) { rotateAngle = 180; } // if the current page is in a LandscapeLeft orientation, the orientation angle will be 0 data = RotateImage(args.ImageStream, rotateAngle); } catch { // the orientation angle was not a multiple of 90 - we keep the original image data = args.ImageStream; } Resource res = runtime.GetResource(MoSync.Constants.RT_PLACEHOLDER, _placeHolder); MemoryStream dataMem = new MemoryStream((int)data.Length); MoSync.Util.CopySeekableStreams(data, 0, dataMem, 0, (int)data.Length); res.SetInternalObject(dataMem); postSnapshotEvent(_placeHolder, mCamera.Resolution, MoSync.Constants.MA_IMAGE_REPRESENTATION_RAW, MoSync.Constants.MA_CAMERA_RES_OK); isCameraSnapshotInProgress = false; }); }; mCamera.CaptureImageAvailable += mCameraSnapshotDelegate; mCamera.CaptureImage(); snapshotPlaceHolder = _placeHolder; isCameraSnapshotInProgress = true; return(MoSync.Constants.MA_CAMERA_RES_OK); }; /** * Sets the property represented by the string situated at the * _property address with the value situated at the _value address. * @param _property int the property name address * @param _value int the value address * * Note: the fallowing properties are not available on windows phone * MA_CAMERA_FOCUS_MODE, MA_CAMERA_IMAGE_FORMAT, MA_CAMERA_ZOOM, * MA_CAMERA_MAX_ZOOM. */ ioctls.maCameraSetProperty = delegate(int _property, int _value) { // if the camera is not initialized, we cannot access any of its properties if (!isCameraInitialized) { return(MoSync.Constants.MA_CAMERA_RES_PROPERTY_NOTSUPPORTED); } String property = core.GetDataMemory().ReadStringAtAddress(_property); String value = core.GetDataMemory().ReadStringAtAddress(_value); if (property.Equals(MoSync.Constants.MA_CAMERA_FLASH_MODE)) { if (value.Equals(MoSync.Constants.MA_CAMERA_FLASH_ON) && mCamera.IsFlashModeSupported(FlashMode.On)) { mCamera.FlashMode = FlashMode.On; mFlashMode = FlashMode.On; } else if (value.Equals(MoSync.Constants.MA_CAMERA_FLASH_OFF) && mCamera.IsFlashModeSupported(FlashMode.Off)) { mCamera.FlashMode = FlashMode.Off; mFlashMode = FlashMode.Off; } else if (value.Equals(MoSync.Constants.MA_CAMERA_FLASH_AUTO) && mCamera.IsFlashModeSupported(FlashMode.Auto)) { mCamera.FlashMode = FlashMode.Auto; mFlashMode = FlashMode.Auto; } else { return(MoSync.Constants.MA_CAMERA_RES_INVALID_PROPERTY_VALUE); } return(MoSync.Constants.MA_CAMERA_RES_OK); } else if (property.Equals(MoSync.Constants.MA_CAMERA_FOCUS_MODE)) { return(MoSync.Constants.MA_CAMERA_RES_PROPERTY_NOTSUPPORTED); } else if (property.Equals(MoSync.Constants.MA_CAMERA_IMAGE_FORMAT)) { return(MoSync.Constants.MA_CAMERA_RES_PROPERTY_NOTSUPPORTED); } else if (property.Equals(MoSync.Constants.MA_CAMERA_ZOOM)) { return(MoSync.Constants.MA_CAMERA_RES_PROPERTY_NOTSUPPORTED); } else if (property.Equals(MoSync.Constants.MA_CAMERA_MAX_ZOOM)) { return(MoSync.Constants.MA_CAMERA_RES_PROPERTY_NOTSUPPORTED); } else { return(MoSync.Constants.MA_CAMERA_RES_PROPERTY_NOTSUPPORTED); } }; /** * Selects a camera from the avalable ones; * in this eigther the back or the front camera is * chosen */ ioctls.maCameraSelect = delegate(int _camera) { // if the camera is not initialized, we cannot access any of its properties if (!isCameraInitialized) { return(MoSync.Constants.MA_CAMERA_RES_FAILED); } if (MoSync.Constants.MA_CAMERA_CONST_BACK_CAMERA == _camera) { if (mCamera.CameraType != CameraType.Primary) { mCameraType = CameraType.Primary; InitCamera(); MoSync.Util.RunActionOnMainThreadSync(() => { SetInitialCameraOrientation(currentPage); } ); } } else if (MoSync.Constants.MA_CAMERA_CONST_FRONT_CAMERA == _camera) { if (mCamera.CameraType != CameraType.FrontFacing) { mCameraType = CameraType.FrontFacing; InitCamera(); MoSync.Util.RunActionOnMainThreadSync(() => { SetInitialCameraOrientation(currentPage); } ); } } else { return(MoSync.Constants.MA_CAMERA_RES_FAILED); } return(MoSync.Constants.MA_CAMERA_RES_OK); }; /** * Retrieves the specified property value in the given buffer. * @param _property int the address for the property string * @param _value int the address for the property value string (the buffer) * @param _bufSize int the buffer size */ ioctls.maCameraGetProperty = delegate(int _property, int _value, int _bufSize) { String property = core.GetDataMemory().ReadStringAtAddress(_property); if (property.Equals(MoSync.Constants.MA_CAMERA_MAX_ZOOM)) { core.GetDataMemory().WriteStringAtAddress( _value, "0", _bufSize); } else if (property.Equals(MoSync.Constants.MA_CAMERA_ZOOM_SUPPORTED)) { core.GetDataMemory().WriteStringAtAddress( _value, "false", _bufSize); } else if (property.Equals(MoSync.Constants.MA_CAMERA_FLASH_SUPPORTED)) { /* * Since we cannot see if flash is supported because the camera may be not * fully initialized when this is called, we assume that each windows phone * has flash support for primary camera but not for the from camera. */ String result = "true"; if (mCamera.CameraType != CameraType.Primary) { result = "false"; } core.GetDataMemory().WriteStringAtAddress( _value, result, _bufSize); } else { return(MoSync.Constants.MA_CAMERA_RES_PROPERTY_NOTSUPPORTED); } return(0); }; ioctls.maCameraRecord = delegate(int _stopStartFlag) { return(MoSync.Constants.MA_CAMERA_RES_FAILED); }; }
public bool LoadResources(Stream file) { if (MoSync.Util.StreamReadInt8(file) != 'M') { return(false); } if (MoSync.Util.StreamReadInt8(file) != 'A') { return(false); } if (MoSync.Util.StreamReadInt8(file) != 'R') { return(false); } if (MoSync.Util.StreamReadInt8(file) != 'S') { return(false); } uint numResources = ReadUnsignedVarInt(file); uint resSize = ReadUnsignedVarInt(file); mCurrentResourceHandle = 1; while (true) { byte type = MoSync.Util.StreamReadUint8(file); if (type == 0) { break; } uint size = ReadUnsignedVarInt(file); Util.Log("Resource type " + type + ", size " + size + "\n"); Resource resource = new Resource(null, type); mResources.Add(mCurrentResourceHandle, resource); switch (type) { case MoSync.Constants.RT_PLACEHOLDER: break; case MoSync.Constants.RT_UBIN: case MoSync.Constants.RT_BINARY: Memory memory = new Memory((int)size); memory.WriteFromStream(0, file, (int)size); resource.SetInternalObject(memory); break; case MoSync.Constants.RT_IMAGE: byte[] bytes = new byte[size]; file.Read(bytes, 0, (int)size); using (MemoryStream ms = new MemoryStream(bytes, 0, bytes.Length)) { /* * MoSync.Util.RunActionOnMainThreadSync(() => * { * BitmapImage im = new BitmapImage(); * im.CreateOptions = BitmapCreateOptions.None; * im.SetSource(ms); * WriteableBitmap wb = new WriteableBitmap(im); * resource.SetInternalObject(wb); * }); */ resource.SetInternalObject(MoSync.Util.CreateWriteableBitmapFromStream(ms)); ms.Close(); } break; case MoSync.Constants.RT_LABEL: bytes = new byte[size]; file.Read(bytes, 0, (int)size); if (bytes[size - 1] != 0) { throw new Exception("invalid label (no null terminator)"); } String s = System.Text.Encoding.UTF8.GetString(bytes, 0, (int)size - 1); if (mLabels.ContainsKey(s)) { throw new Exception("duplicate label"); } mLabels.Add(s, mCurrentResourceHandle); break; default: Util.Log("Unknown resource type " + type + ", size " + size + "\n"); file.Seek(size, SeekOrigin.Current); break; } mCurrentResourceHandle++; } return(true); }