Example #1
0
void
HttpRequest::Write (gint64 offset, void *buffer, gint32 length)
{
    gint64 reported_offset = offset;

    VERIFY_MAIN_THREAD;
    LOG_DOWNLOADER ("HttpRequest::Write (%" G_GINT64_FORMAT ", %p, %i) HasHandlers: %i\n", offset, buffer, length, HasHandlers (WriteEvent));

    written_size += length;

    /* write to tmp file */
    if (tmpfile_fd != -1) {
        if (offset != -1 && lseek (tmpfile_fd, offset, SEEK_SET) == -1) {
            printf ("Moonlight: error while seeking to %" G_GINT64_FORMAT " in temporary file '%s': %s\n", offset, tmpfile, strerror (errno));
        } else if (write (tmpfile_fd, buffer, length) != length) {
            printf ("Moonlight: error while writing to temporary file '%s': %s\n", tmpfile, strerror (errno));
        }
    }

    if (HasHandlers (WriteEvent)) {
        if (reported_offset == -1 && ((options & CustomHeaders) == 0)) {
            /* We check if custom headers is required, if so, our creator might have issued a byte range request,
             * in which case written_size isn't related to offset */
            reported_offset = written_size - length;
        }
        Emit (WriteEvent, new HttpRequestWriteEventArgs (buffer, reported_offset, length));
    }

    if (notified_size > 0 && HasHandlers (ProgressChangedEvent))
        Emit (ProgressChangedEvent, new HttpRequestProgressChangedEventArgs (((double) offset + (double) length) / (double) notified_size));
}
Example #2
0
void
ResourceDictionary::EmitChanged (CollectionChangedAction action, Value *new_value, Value *old_value, const char *key)
{
#if EVENT_ARG_REUSE
	if (!HasHandlers (ResourceDictionary::ChangedEvent))
		return;

	if (!changedEventArgs)
		changedEventArgs = MoonUnmanagedFactory::CreateResourceDictionaryChangedEventArgs ();

	changedEventArgs->SetChangedAction (action);
	changedEventArgs->SetNewItem (new_value);
	changedEventArgs->SetOldItem (old_value);
	changedEventArgs->SetKey (key);

	changedEventArgs->ref ();

	Emit (ResourceDictionary::ChangedEvent, changedEventArgs);

	changedEventArgs->SetNewItem (NULL);
	changedEventArgs->SetOldItem (NULL);
	changedEventArgs->SetKey (NULL);
#else
	if (HasHandlers (ResourceDictionary::ChangedEvent))
		Emit (ResourceDictionary::ChangedEvent, new ResourceDictionaryChangedEventArgs (action, new_value, old_value, key));
#endif
}
Example #3
0
void
BitmapImage::PixmapComplete ()
{
	MoonPixbuf *pixbuf;
	
	SetProgress (1.0);

	if (!loader) {
		if (!moon_error)
			moon_error = new MoonError (MoonError::EXCEPTION, 4001, "no loader");
		goto failed;
	}

	loader->Close (moon_error == NULL ? &moon_error : NULL);

	if (moon_error)
		goto failed;

	if (!(pixbuf = loader->GetPixbuf ())) {
		moon_error = new MoonError (MoonError::EXCEPTION, 4001, "failed to create image data");
		goto failed;
	}
	
	SetPixelWidth (pixbuf->GetWidth ());
	SetPixelHeight (pixbuf->GetHeight ());
	
	// PixelFormat has been dropped and only Pbgra32 is supported
	// http://blogs.msdn.com/silverlight_sdk/archive/2009/07/01/breaking-changes-document-errata-silverlight-3.aspx
	// not clear if '3' channel is still supported (converted to 4) in SL3
	if (pixbuf->GetNumChannels () == 4) {
		SetBitmapData (premultiply_rgba (pixbuf), true);
	} else {
		SetBitmapData (expand_rgb_to_argb (pixbuf), true);
	}
	
	Invalidate ();
	
	delete loader;
	loader = NULL;
	
	if (HasHandlers (ImageOpenedEvent))
		Emit (ImageOpenedEvent, new RoutedEventArgs ());
	
	return;
	
failed:
	ImageErrorEventArgs *args = NULL;

	if (HasHandlers (ImageFailedEvent))
		args = new ImageErrorEventArgs (*moon_error);
	CleanupLoader ();
	if (args)
		Emit (ImageFailedEvent, args);
}
Example #4
0
void
HttpRequest::Succeeded ()
{
    VERIFY_MAIN_THREAD;
    LOG_DOWNLOADER ("HttpRequest::Succeeded (%s) HasHandlers: %i\n", uri, HasHandlers (StoppedEvent));

    is_completed = true;

    NotifySize (written_size);

    if (HasHandlers (StoppedEvent))
        Emit (StoppedEvent, new HttpRequestStoppedEventArgs (NULL));
}
Example #5
0
void
HttpRequest::Failed (const char *msg)
{
    VERIFY_MAIN_THREAD;
    LOG_DOWNLOADER ("HttpRequest::Failed (%s) HasHandlers: %i\n", msg, HasHandlers (StoppedEvent));

    if (is_completed)
        return;

    is_completed = true;

    if (HasHandlers (StoppedEvent))
        Emit (StoppedEvent, new HttpRequestStoppedEventArgs (msg));
}
Example #6
0
void
CaptureSource::Start ()
{
	if (current_state == CaptureSource::Started)
		return;

	d(printf ("CaptureSource::Start ()\n"));

	AudioCaptureDevice *audio_device = GetAudioCaptureDevice ();
	VideoCaptureDevice *video_device = GetVideoCaptureDevice ();

	if (audio_device)
		audio_device->Start ();
	if (video_device) {
		video_device->SetCallbacks (CaptureSource::ReportSampleCallback,
					    CaptureSource::VideoFormatChangedCallback,
					    this);
		if (!need_image_capture) {
			// if need_image_capture is true the image
			// capture stuff has already started the
			// device
			video_device->Start ();
		}
	}

	current_state = CaptureSource::Started;
	if (HasHandlers (CaptureSource::CaptureStartedEvent))
		Emit (CaptureSource::CaptureStartedEvent);
}
Example #7
0
File: media.cpp Project: snorp/moon
void
Image::ImageFailed (ImageErrorEventArgs *args)
{
	BitmapSource *source = (BitmapSource*) GetSource ();

	if (source->Is (Type::BITMAPIMAGE)) {
		source->RemoveHandler (BitmapImage::DownloadProgressEvent, download_progress, this);
		source->RemoveHandler (BitmapImage::ImageOpenedEvent, image_opened, this);
		source->RemoveHandler (BitmapImage::ImageFailedEvent, image_failed, this);
	}
	source->RemoveHandler (BitmapSource::PixelDataChangedEvent, source_pixel_data_changed, this);


	InvalidateArrange ();
	InvalidateMeasure ();
	UpdateBounds ();
	Invalidate ();

	args = new ImageErrorEventArgs (this, *(MoonError*)args->GetMoonError ());
	if (HasHandlers (ImageFailedEvent)) {
		Emit (ImageFailedEvent, args);
	} else {
		GetDeployment ()->GetSurface ()->EmitError (args);
	}
}
Example #8
0
void
Downloader::SendInternal ()
{
	LOG_DOWNLOADER ("Downloader::SendInternal ()\n");
	
	g_return_if_fail (request != NULL);

	if (!send_queued)
		return;
	
	send_queued = false;
	
	if (completed) {
		// Consumer is re-sending a request which finished successfully.
		NotifyFinished ();
		return;
	}
	
	if (failed_msg != NULL) {
		if (HasHandlers (DownloadFailedEvent)) {
			// Consumer is re-sending a request which failed.
			Emit (DownloadFailedEvent,
			      new ErrorEventArgs (this, DownloadError,
						  MoonError (MoonError::EXCEPTION, 4001, failed_msg)));
		}
		return;
	}
	
	started = true;
	aborted = false;
	
	g_return_if_fail (request != NULL);

	request->Send ();
}
Example #9
0
void
CaptureSource::Stop ()
{
	AudioCaptureDevice *audio_device = GetAudioCaptureDevice ();
	VideoCaptureDevice *video_device = GetVideoCaptureDevice ();

	if (current_state != CaptureSource::Started && !need_image_capture)
		return;

	if (audio_device)
		audio_device->Stop ();
	if (video_device) {
		if (need_image_capture) {
			// if we're waiting for an image capture,
			// replace the callbacks instead of stopping
			// the device outright.
			video_device->SetCallbacks (CaptureSource::CaptureImageReportSampleCallback,
						    CaptureSource::CaptureImageVideoFormatChangedCallback,
						    this);
		}
		else {
			video_device->Stop ();
		}
	}

	current_state = CaptureSource::Stopped;
	if (HasHandlers (CaptureSource::CaptureStoppedEvent))
		Emit (CaptureSource::CaptureStoppedEvent);
}
Example #10
0
void
CaptureSource::ReportSample (gint64 sampleTime, gint64 frameDuration, guint8 *sampleData, int sampleDataLength)
{
	SetCurrentDeployment ();

	if (!cached_sampleData || sampleDataLength != cached_sampleDataLength) {
		g_free (cached_sampleData);
		cached_sampleData = (guint8*)g_malloc (sampleDataLength);
	}

	memmove (cached_sampleData, sampleData, sampleDataLength);
	cached_sampleDataLength = sampleDataLength;
	cached_sampleTime = sampleTime;
	cached_frameDuration = frameDuration;

	if (need_image_capture) {
		CaptureImageReportSample (sampleTime, frameDuration, cached_sampleData, sampleDataLength);
		need_image_capture = false;
	}

	// printf ("CaptureSource::ReportSample (%llu, %llu, %d\n", sampleTime, frameDuration, sampleDataLength);
	if (HasHandlers (CaptureSource::SampleReadyEvent)) {
		Emit (CaptureSource::SampleReadyEvent,
		      new SampleReadyEventArgs (cached_sampleTime, cached_frameDuration, cached_sampleData, cached_sampleDataLength));
	}
}
Example #11
0
void
BitmapImage::OnPropertyChanged (PropertyChangedEventArgs *args, MoonError *error)
{
	if (args->GetProperty ()->GetOwnerType () != Type::BITMAPIMAGE) {
		BitmapSource::OnPropertyChanged (args, error);
		return;
	}

	if (args->GetId () == BitmapImage::UriSourceProperty) {
		Uri *uri = args->GetNewValue () ? args->GetNewValue ()->AsUri () : NULL;

		Abort ();

		if (Uri::IsNullOrEmpty (uri)) {
			SetBitmapData (NULL, false);
		} else if (uri->IsInvalidPath ()) {
			if (IsBeingParsed ())
				MoonError::FillIn (error, MoonError::ARGUMENT_OUT_OF_RANGE, 0, "invalid path found in uri");
			SetBitmapData (NULL, false);
		} else {
			AddTickCall (uri_source_changed_callback);
		}
	} else if (args->GetId () == BitmapImage::ProgressProperty) {
		if (HasHandlers (DownloadProgressEvent))
			Emit (DownloadProgressEvent, new DownloadProgressEventArgs (GetProgress ()));
	}

	NotifyListenersOfPropertyChange (args, error);
}
Example #12
0
void
ContentControl::OnPropertyChanged (PropertyChangedEventArgs *args, MoonError *error)
{
	if (args->GetProperty ()->GetOwnerType () != Type::CONTENTCONTROL) {
		Control::OnPropertyChanged (args, error);
		return;
	}
	
	if (args->GetId () == ContentControl::ContentProperty) {
		if (args->GetOldValue() && args->GetOldValue()->Is(GetDeployment (), Type::FRAMEWORKELEMENT)) {
			if (GetContentSetsParent ()) {
				args->GetOldValue()->AsFrameworkElement()->SetLogicalParent (NULL, error);
				if (error->number)
					return;
			}
		}
		if (args->GetNewValue() && args->GetNewValue()->Is(GetDeployment (), Type::FRAMEWORKELEMENT)) {
			if (GetContentSetsParent ()) {
				args->GetNewValue()->AsFrameworkElement()->SetLogicalParent (this, error);
				if (error->number)
					return;
			}
		}

		if (HasHandlers (ContentControl::ContentControlChangedEvent))
			Emit (ContentControl::ContentControlChangedEvent, new ContentControlChangedEventArgs (args->GetOldValue(), args->GetNewValue()));
		InvalidateMeasure ();
	}
	
	NotifyListenersOfPropertyChange (args, error);
}
Example #13
0
void
CaptureSource::CaptureImageReportSample (gint64 sampleTime, gint64 frameDuration, guint8 *sampleData, int sampleDataLength)
{
	SetCurrentDeployment ();

	d(printf ("CaptureSource::CaptureImageReportSample (%lld, %lld, %d\n", (long long) sampleTime, (long long) frameDuration, sampleDataLength));

	if (HasHandlers (CaptureSource::CaptureImageCompletedEvent)) {
		BitmapImage *source = MoonUnmanagedFactory::CreateBitmapImage ();
		source->SetPixelWidth (capture_format->width);
		source->SetPixelHeight (capture_format->height);

		source->SetBitmapData (sampleData, false);

		source->Invalidate (); // causes the BitmapSource to create its image_surface

		CaptureImageCompletedEventArgs *args = new CaptureImageCompletedEventArgs (NULL);
		args->EnsureManagedPeer ();

		args->SetSource (source);

		source->unref ();

		Emit (CaptureSource::CaptureImageCompletedEvent, args);
	}

	// if we started the pal device strictly for an image capture
	// (or kept it running after the user had called
	// CaptureSource::Stop), stop it now.
	if (current_state != CaptureSource::Started) {
		VideoCaptureDevice *video_device = GetVideoCaptureDevice ();
		video_device->Stop();
	}
}
Example #14
0
void
BitmapImage::DownloaderFailed ()
{
	//Uri *uri = GetUriSource ();
	//printf ("\tBitmapImage::DownloaderFailed() for %s\n", uri ? uri->ToString () : "null?");
	
	Abort ();
	if (HasHandlers (ImageFailedEvent))
		Emit (ImageFailedEvent, new ImageErrorEventArgs (MoonError (MoonError::EXCEPTION, 4001, "downloader failed")));
}
Example #15
0
void
LocalMessageSender::MessageSent (MoonError *error, const char *message, const char *response, GCHandle managedUserState)
{
	if (HasHandlers (SendCompletedEvent)) {
		Emit (SendCompletedEvent, new SendCompletedEventArgs (error,
								      message,
								      receiverName,
								      receiverDomain,
								      response,
								      managedUserState));
	}
}
Example #16
0
void
CaptureSource::VideoFormatChanged (MoonVideoFormat *format)
{
	SetCurrentDeployment ();

	d(printf ("CaptureSource::VideoFormatChanged\n"));

	delete capture_format;
	capture_format = new VideoFormat (format);

	if (HasHandlers (CaptureSource::FormatChangedEvent)) {
		VideoFormat vformat (format);
		Emit (CaptureSource::FormatChangedEvent,
		      new VideoFormatChangedEventArgs (&vformat));
	}
}
Example #17
0
void
HttpRequest::Started (HttpResponse *response)
{
    VERIFY_MAIN_THREAD;
    LOG_DOWNLOADER ("HttpRequest::Started ()\n");

    g_warn_if_fail (response != NULL);
    g_warn_if_fail (this->response == NULL);

    if (this->response != NULL)
        this->response->unref ();
    this->response = response;
    if (this->response != NULL)
        this->response->ref ();

    if (HasHandlers (StartedEvent))
        Emit (StartedEvent);
}
Example #18
0
void
Downloader::NotifyFailed (const char *msg)
{
	LOG_DOWNLOADER ("Downloader::NotifyFailed (%s)\n", msg);
	
	/* if we've already been notified of failure, no-op */
	if (failed_msg)
		return;
	
	// SetStatus (400);
	// For some reason the status is 0, not updated on errors?
	
	if (HasHandlers (DownloadFailedEvent))
		Emit (DownloadFailedEvent,
		      new ErrorEventArgs (this, DownloadError,
					  MoonError (MoonError::EXCEPTION, 4001, msg)));
	
	failed_msg = g_strdup (msg);
}
Example #19
0
File: media.cpp Project: snorp/moon
void
Image::ImageOpened (RoutedEventArgs *args)
{
	BitmapSource *source = (BitmapSource*)GetSource ();

	if (source->Is (Type::BITMAPIMAGE)) {
		source->RemoveHandler (BitmapImage::DownloadProgressEvent, download_progress, this);
		source->RemoveHandler (BitmapImage::ImageOpenedEvent, image_opened, this);
		source->RemoveHandler (BitmapImage::ImageFailedEvent, image_failed, this);
	}

	InvalidateArrange ();
	InvalidateMeasure ();
	UpdateBounds ();
	Invalidate ();

	if (HasHandlers (ImageOpenedEvent)) {
		args->ref (); // to counter the unref in Emit
		Emit (ImageOpenedEvent, args);
	}
}
Example #20
0
void
BitmapImage::DownloaderComplete ()
{
	if (downloader)
		CleanupDownloader ();

	SetProgress (1.0);

	if (downloader && loader == NULL) {
		char *filename = downloader->GetDownloadedFilename (part_name);
		guchar b[4096];
		int offset = 0;
		ssize_t n;
		int fd;

		if ((fd = g_open (filename, O_RDONLY)) == -1) {
			moon_error = new MoonError (MoonError::EXCEPTION, 4001, "failed to open file");
			goto failed;
		}

		do {
			do {
				n = read (fd, b, sizeof (b));
			} while (n == -1 && errno == EINTR);

			if (n == -1) break;

			PixbufWrite (b, offset, n);

			offset += n;
		} while (n > 0 && !moon_error);

		close (fd);

		if (moon_error)
			goto failed;
	}

	if (downloader) {
		downloader->unref ();
		downloader = NULL;
	}

	if (get_res_aborter) {
		delete get_res_aborter;
		get_res_aborter = NULL;
	}

	PixmapComplete ();

	return;
failed:
	if (downloader) {
		downloader->unref ();
		downloader = NULL;
	}

	if (get_res_aborter) {
		delete get_res_aborter;
		get_res_aborter = NULL;
	}

	if (loader)
		loader->Close ();

	ImageErrorEventArgs *args = NULL;

	if (HasHandlers(ImageFailedEvent))
		args = new ImageErrorEventArgs (*moon_error);
	CleanupLoader ();
	if (args)
		Emit (ImageFailedEvent, args);
}