2009-12-07 Rolf Bjarne Kvinge <RKvinge@novell.com>
[moon.git] / src / runtime.h
blob192fe7631024564c7c2687b867a2a3e3b16a44d3
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3 * runtime.h: Core surface.
5 * Contact:
6 * Moonlight List (moonlight-list@lists.ximian.com)
8 * Copyright 2007 Novell, Inc. (http://www.novell.com)
10 * See the LICENSE file included with the distribution for details.
14 #ifndef __RUNTIME_H__
15 #define __RUNTIME_H__
17 #include <gtk/gtkwidget.h>
18 #include <cairo.h>
20 #include "point.h"
21 #include "uielement.h"
22 #include "dependencyobject.h"
23 #include "dirty.h"
24 #include "value.h"
25 #include "type.h"
26 #include "list.h"
27 #include "error.h"
28 #include "window.h"
29 #include "mutex.h"
31 #define MAXIMUM_CACHE_SIZE 6000000
33 #define FRONT_TO_BACK_STATS 0
35 #define TIMERS 0
36 #define DEBUG_MARKER_KEY 0
37 #if TIMERS
38 #define STARTTIMER(id,str) TimeSpan id##_t_start = get_now(); printf ("timing of '%s' started at %" G_GINT64_FORMAT "\n", str, id##_t_start)
39 #define ENDTIMER(id,str) TimeSpan id##_t_end = get_now(); printf ("timing of '%s' ended at %" G_GINT64_FORMAT " (%f seconds)\n", str, id##_t_end, (double)(id##_t_end - id##_t_start) / 10000000)
40 #else
41 #define STARTTIMER(id,str)
42 #define ENDTIMER(id,str)
43 #endif
45 #if SANITY
46 #define VERIFY_MAIN_THREAD \
47 if (!Surface::InMainThread ()) { \
48 printf ("Moonlight: This method should only be called from the main thread (%s)\n", __PRETTY_FUNCTION__); \
49 print_stack_trace (); \
51 #else
52 #define VERIFY_MAIN_THREAD
53 #endif
55 enum RuntimeInitFlags {
56 RUNTIME_INIT_PANGO_TEXT_LAYOUT = 1 << 0,
57 // (not used) = 1 << 1,
58 RUNTIME_INIT_MANUAL_TIMESOURCE = 1 << 2,
59 RUNTIME_INIT_DISABLE_AUDIO = 1 << 3,
60 RUNTIME_INIT_SHOW_EXPOSE = 1 << 4,
61 RUNTIME_INIT_SHOW_CLIPPING = 1 << 5,
62 RUNTIME_INIT_SHOW_BOUNDING_BOXES = 1 << 6,
63 RUNTIME_INIT_SHOW_TEXTBOXES = 1 << 7,
64 RUNTIME_INIT_SHOW_FPS = 1 << 8,
65 RUNTIME_INIT_RENDER_FRONT_TO_BACK = 1 << 9,
66 RUNTIME_INIT_SHOW_CACHE_SIZE = 1 << 10,
67 RUNTIME_INIT_FFMPEG_YUV_CONVERTER = 1 << 11,
68 RUNTIME_INIT_USE_SHAPE_CACHE = 1 << 12,
69 RUNTIME_INIT_USE_UPDATE_POSITION = 1 << 13,
70 RUNTIME_INIT_ALLOW_WINDOWLESS = 1 << 14,
71 RUNTIME_INIT_AUDIO_ALSA_MMAP = 1 << 15,
72 RUNTIME_INIT_AUDIO_ALSA_RW = 1 << 16,
73 RUNTIME_INIT_AUDIO_ALSA = 1 << 17,
74 RUNTIME_INIT_AUDIO_PULSE = 1 << 18,
75 RUNTIME_INIT_USE_IDLE_HINT = 1 << 19,
76 RUNTIME_INIT_USE_BACKEND_XLIB = 1 << 20,
77 RUNTIME_INIT_KEEP_MEDIA = 1 << 21,
78 RUNTIME_INIT_ENABLE_MS_CODECS = 1 << 22,
79 RUNTIME_INIT_DISABLE_FFMPEG_CODECS = 1 << 23,
80 RUNTIME_INIT_ALL_IMAGE_FORMATS = 1 << 24,
81 RUNTIME_INIT_CREATE_ROOT_DOMAIN = 1 << 25,
82 RUNTIME_INIT_DESKTOP_EXTENSIONS = 1 << 26,
83 RUNTIME_INIT_OUT_OF_BROWSER = 1 << 27,
86 extern guint32 moonlight_flags;
89 #if LOGGING || DEBUG
90 enum RuntimeDebugFlags {
91 RUNTIME_DEBUG_ALSA = 1 << 0,
92 RUNTIME_DEBUG_AUDIO = 1 << 1,
93 RUNTIME_DEBUG_PULSE = 1 << 2,
94 RUNTIME_DEBUG_HTTPSTREAMING = 1 << 3,
95 RUNTIME_DEBUG_MARKERS = 1 << 4,
96 RUNTIME_DEBUG_MMS = 1 << 5,
97 RUNTIME_DEBUG_MEDIAPLAYER = 1 << 6,
98 RUNTIME_DEBUG_PIPELINE = 1 << 7,
99 RUNTIME_DEBUG_PIPELINE_ERROR = 1 << 8,
100 RUNTIME_DEBUG_FRAMEREADERLOOP = 1 << 9,
101 RUNTIME_DEBUG_FFMPEG = 1 << 10,
102 RUNTIME_DEBUG_UI = 1 << 11,
103 RUNTIME_DEBUG_CODECS = 1 << 12,
104 RUNTIME_DEBUG_DP = 1 << 13,
105 RUNTIME_DEBUG_DOWNLOADER = 1 << 14,
106 RUNTIME_DEBUG_FONT = 1 << 15,
107 RUNTIME_DEBUG_LAYOUT = 1 << 16,
108 RUNTIME_DEBUG_MEDIA = 1 << 17,
109 RUNTIME_DEBUG_MEDIAELEMENT = 1 << 18,
110 RUNTIME_DEBUG_BUFFERING = 1 << 19,
111 RUNTIME_DEBUG_ASF = 1 << 20,
112 RUNTIME_DEBUG_PLAYLIST = 1 << 21,
113 RUNTIME_DEBUG_TEXT = 1 << 22,
114 RUNTIME_DEBUG_XAML = 1 << 23,
115 RUNTIME_DEBUG_DEPLOYMENT = 1 << 24,
116 RUNTIME_DEBUG_MSI = 1 << 25,
117 RUNTIME_DEBUG_MP3 = 1 << 26,
118 RUNTIME_DEBUG_VALUE = 1 << 27,
121 enum RuntimeDebugFlagsExtra {
122 RUNTIME_DEBUG_ALSA_EX = 1 << 0,
123 RUNTIME_DEBUG_AUDIO_EX = 1 << 1,
124 RUNTIME_DEBUG_PULSE_EX = 1 << 2,
125 RUNTIME_DEBUG_MARKERS_EX = 1 << 3,
126 RUNTIME_DEBUG_MEDIAPLAYER_EX = 1 << 4,
127 RUNTIME_DEBUG_MEDIAELEMENT_EX = 1 << 5,
128 RUNTIME_DEBUG_PLAYLIST_EX = 1 << 6,
129 RUNTIME_DEBUG_PIPELINE_EX = 1 << 7,
132 extern guint32 debug_flags_ex;
133 extern guint32 debug_flags;
134 #endif
137 class TimeManager;
138 class Surface;
139 class Downloader;
141 typedef void (* MoonlightFPSReportFunc) (Surface *surface, int nframes, float nsecs, void *user_data);
142 typedef void (* MoonlightCacheReportFunc) (Surface *surface, long size, void *user_data);
143 typedef bool (* MoonlightEventEmitFunc) (UIElement *element, GdkEvent *event);
144 typedef void (* MoonlightExposeHandoffFunc) (Surface *surface, TimeSpan time, void *user_data);
146 /* @Namespace=None,ManagedEvents=Manual */
147 class Surface : public EventObject {
148 public:
149 /* @GenerateCBinding,GeneratePInvoke */
150 Surface (MoonWindow *window);
151 virtual void Dispose ();
153 /* @GenerateCBinding */
154 MoonWindow *GetWindow () { return active_window; }
155 MoonWindow *DetachWindow ();
157 // allows you to redirect painting of the surface to an
158 // arbitrary cairo context.
159 void Paint (cairo_t *ctx, Region *region);
160 /* @GenerateCBinding,GeneratePInvoke */
161 void Paint (cairo_t *ctx, int x, int y, int width, int height);
163 /* @GenerateCBinding,GeneratePInvoke */
164 void Attach (UIElement *toplevel);
166 void AttachLayer (UIElement *layer);
168 void DetachLayer (UIElement *layer);
170 void SetCursor (MouseCursor cursor);
172 void ReleaseMouseCapture (UIElement *capture);
173 bool SetMouseCapture (UIElement *capture);
175 /* @GenerateCBinding,GeneratePInvoke */
176 void Resize (int width, int height);
178 void EmitSourceDownloadComplete ();
179 void EmitSourceDownloadProgressChanged (DownloadProgressEventArgs *args);
180 void EmitError (ErrorEventArgs *args);
181 /* @GenerateCBinding,GeneratePInvoke */
182 void EmitError (int number, int code, const char *message);
184 void EmitLoad ();
186 void SetBackgroundColor (Color *color);
187 /* @GenerateCBinding,GeneratePInvoke */
188 Color *GetBackgroundColor ();
190 int GetFrameCount () { return frames; }
191 void ResetFrameCount () { frames = 0; }
193 virtual void Invalidate (Rect r);
194 virtual void ProcessUpdates ();
196 /* @GenerateCBinding,GeneratePInvoke */
197 UIElement *GetToplevel() { return toplevel; }
198 bool IsTopLevel (UIElement *top);
200 /* @GenerateCBinding,GeneratePInvoke,Version=2.0 */
201 UIElement *GetFocusedElement () { return focused_element; }
203 bool FocusElement (UIElement *element);
205 /* @GenerateCBinding,GeneratePInvoke */
206 bool IsLoaded () { return toplevel != NULL && ticked_after_attach; }
208 /* @GenerateCBinding,GeneratePInvoke */
209 static bool IsVersionSupported (const char *version);
211 const static int ResizeEvent;
212 const static int FullScreenChangeEvent;
213 const static int ErrorEvent;
214 const static int LoadEvent;
215 const static int SourceDownloadProgressChangedEvent;
216 const static int SourceDownloadCompleteEvent;
217 const static int ZoomedEvent;
219 /* @GenerateCBinding,GeneratePInvoke,Version=2.0 */
220 bool GetFullScreen () { return full_screen; }
221 /* @GenerateCBinding,GeneratePInvoke,Version=2.0 */
222 void SetFullScreen (bool value);
224 /* @GenerateCBinding,GeneratePInvoke,Version=3.0 */
225 double GetZoomFactor () { return zoom_factor; }
226 void SetZoomFactor (double value);
228 void SetUserInitiatedEvent (bool value);
230 bool FirstUserInitiatedEvent () { return first_user_initiated_event; }
231 /* @GenerateCBinding,GeneratePInvoke,Version=2.0 */
232 bool IsUserInitiatedEvent () { return user_initiated_event; }
234 const char* GetSourceLocation ();
235 void SetSourceLocation (const char *location);
236 bool FullScreenKeyHandled (GdkEventKey *key);
238 /* @GenerateCBinding,GeneratePInvoke */
239 TimeManager *GetTimeManager () { return time_manager; }
240 TimeManager *GetTimeManagerReffed ();
242 void SetDownloaderContext (gpointer context) { downloader_context = context; }
243 gpointer GetDownloaderContext () { return downloader_context; }
245 /* @GenerateCBinding,GeneratePInvoke */
246 Downloader *CreateDownloader ();
247 static Downloader *CreateDownloader (EventObject *obj);
249 void SetFPSReportFunc (MoonlightFPSReportFunc report, void *user_data);
250 void SetCacheReportFunc (MoonlightCacheReportFunc report, void *user_data);
251 void SetExposeHandoffFunc (MoonlightExposeHandoffFunc func, void *user_data);
253 bool VerifyWithCacheSizeCounter (int w, int h);
254 gint64 AddToCacheSizeCounter (int w, int h);
255 void RemoveFromCacheSizeCounter (gint64 size);
257 // called from the plugin if the surface is headed for death.
258 // stops event emission (since the plugin counterparts to xaml
259 // objects will be destroyed)
260 void Zombify ();
262 bool IsZombie () { return zombie; }
264 void DetachDownloaders ();
266 #if FRONT_TO_BACK_STATS
267 int uielements_rendered_front_to_back;
268 int uielements_rendered_back_to_front;
269 #endif
271 #ifdef DEBUG
272 UIElement *debug_selected_element;
273 #endif
275 void PaintToDrawable (GdkDrawable *drawable, GdkVisual *visual, GdkEventExpose *event, int off_x, int off_y, bool transparent, bool clear_transparent);
278 gboolean HandleUIMotion (GdkEventMotion *event);
279 gboolean HandleUICrossing (GdkEventCrossing *event);
280 gboolean HandleUIKeyPress (GdkEventKey *event);
281 gboolean HandleUIKeyRelease (GdkEventKey *event);
282 gboolean HandleUIButtonRelease (GdkEventButton *event);
283 gboolean HandleUIButtonPress (GdkEventButton *event);
284 gboolean HandleUIScroll (GdkEventScroll *event);
285 gboolean HandleUIFocusIn (GdkEventFocus *event);
286 gboolean HandleUIFocusOut (GdkEventFocus *event);
287 void HandleUIWindowAllocation (bool emit_resize);
288 void HandleUIWindowAvailable ();
289 void HandleUIWindowUnavailable ();
290 void HandleUIWindowDestroyed (MoonWindow *window);
292 // bad, but these live in dirty.cpp, not runtime.cpp
293 void AddDirtyElement (UIElement *element, DirtyType dirt);
294 void UpdateLayout ();
295 void RemoveDirtyElement (UIElement *element);
296 bool ProcessDirtyElements ();
297 void PropagateDirtyFlagToChildren (UIElement *element, DirtyType dirt);
299 static bool main_thread_inited;
300 static pthread_t main_thread;
301 /* @GenerateCBinding,GeneratePInvoke */
302 static bool InMainThread () { return (!main_thread_inited || pthread_equal (main_thread, pthread_self ())); }
304 protected:
305 // The current window we are drawing to
306 MoonWindow *active_window;
308 virtual ~Surface();
310 private:
311 // are we headed for death?
312 bool zombie;
314 // bad, but these two live in dirty.cpp, not runtime.cpp
315 void ProcessDownDirtyElements ();
316 void ProcessUpDirtyElements ();
318 DirtyLists *down_dirty;
319 DirtyLists *up_dirty;
321 gpointer downloader_context;
322 List *downloaders;
323 static void OnDownloaderDestroyed (EventObject *sender, EventArgs *args, gpointer closure);
325 Color *background_color;
327 // This is the normal-sized window
328 MoonWindow *normal_window;
330 // We set active_window to this whenever we are in fullscreen mode.
331 MoonWindow *fullscreen_window;
333 // We can have multiple top level elements, these are stored as layers
334 HitTestCollection *layers;
336 UIElement *toplevel;
338 // The element holding the keyboard focus, and the one that
339 // held it previously (so we can emit lostfocus events async)
340 UIElement *focused_element;
341 List *focus_changed_events;
343 // the list of elements (from most deeply nested to the
344 // toplevel) we've most recently sent a mouse event to.
345 List *input_list;
347 // is the mouse captured? if it is, it'll be by the first element in input_list.
348 UIElement *captured;
349 UIElement *pendingCapture;
350 bool pendingReleaseCapture;
352 // are we currently emitting a mouse event?
353 bool emittingMouseEvent;
355 // the currently shown cursor
356 MouseCursor cursor;
358 // Fullscreen support
359 bool full_screen;
360 Panel *full_screen_message;
361 char *source_location;
363 // Zoom support
364 double zoom_factor;
366 Panel *incomplete_support_message;
368 // True once we have received at least one user initiated event
369 bool first_user_initiated_event;
370 // Should be set to true only while executing MouseLeftButtonDown,
371 // MouseLeftButtonUp, KeyDown, and KeyUp event handlers
372 bool user_initiated_event;
374 void UpdateFullScreen (bool value);
376 TimeManager *time_manager;
377 Mutex time_manager_mutex;
378 bool ticked_after_attach;
379 static void tick_after_attach_reached (EventObject *data);
381 int frames;
383 GdkEvent *mouse_event;
385 // Variables for reporting FPS
386 MoonlightFPSReportFunc fps_report;
387 gint64 fps_start;
388 int fps_nframes;
389 void *fps_data;
391 // Variables for reporting cache size
392 MoonlightCacheReportFunc cache_report;
393 gint64 cache_size_in_bytes;
394 int cache_size_ticker;
395 void *cache_data;
396 int cache_size_multiplier;
398 // Expose handoff
399 TimeSpan expose_handoff_last_timespan;
400 MoonlightExposeHandoffFunc expose_handoff;
401 void *expose_handoff_data;
403 void Realloc ();
405 void ShowFullScreenMessage ();
406 void HideFullScreenMessage ();
407 static void HideFullScreenMessageCallback (EventObject *sender, EventArgs *args, gpointer closure);
409 void ShowIncompleteSilverlightSupportMessage ();
410 void HideIncompleteSilverlightSupportMessage ();
411 static void HideIncompleteSilverlightSupportMessageCallback (EventObject *sender, EventArgs *args, gpointer closure);
413 void CreateSimilarSurface ();
415 static void render_cb (EventObject *sender, EventArgs *calldata, gpointer closure);
416 static void update_input_cb (EventObject *sender, EventArgs *calldata, gpointer closure);
417 static void widget_destroyed (GtkWidget *w, gpointer data);
419 EventArgs* CreateArgsForEvent (int event_id, GdkEvent *event);
421 List* ElementPathToRoot (UIElement *source);
422 void EmitFocusChangeEvents();
423 static void EmitFocusChangeEventsAsync (EventObject *sender);
425 void FindFirstCommonElement (List *l1, int *index1, List *l2, int *index2);
426 bool EmitEventOnList (int event_id, List *element_list, GdkEvent *event, int end_idx);
427 void UpdateCursorFromInputList ();
428 bool HandleMouseEvent (int event_id, bool emit_leave, bool emit_enter, bool force_emit, GdkEvent *event);
429 void PerformCapture (UIElement *capture);
430 void PerformReleaseCapture ();
432 static void toplevel_loaded (EventObject *sender, EventArgs *args, gpointer closure);
433 void ToplevelLoaded (UIElement *element);
436 /* for hit testing */
437 class UIElementNode : public List::Node {
438 public:
439 UIElement *uielement;
441 UIElementNode (UIElement *el);
442 virtual ~UIElementNode ();
445 /* for rendering */
446 typedef void (*RenderFunc) (cairo_t *ctx, UIElement *uielement, Region *region, bool front_to_back);
448 class RenderNode : public List::Node {
449 public:
450 RenderNode (UIElement *el, Region *region, bool render_element, RenderFunc pre, RenderFunc post);
452 void Render (cairo_t *cr);
454 virtual ~RenderNode ();
456 private:
457 UIElement *uielement;
458 Region *region;
459 bool render_element;
460 RenderFunc pre_render;
461 RenderFunc post_render;
465 G_BEGIN_DECLS
467 void runtime_init (const char *platform_dir, guint32 flags);
469 void runtime_init_browser (const char *plugin_dir);
470 /* @GeneratePInvoke */
471 void runtime_init_desktop ();
472 /* @GeneratePInvoke */
473 bool runtime_is_running_out_of_browser ();
475 GList *runtime_get_surface_list (void);
477 void runtime_flags_set_manual_timesource (gboolean flag);
478 void runtime_flags_set_show_fps (gboolean flag);
479 void runtime_flags_set_use_shapecache (gboolean flag);
481 void runtime_shutdown (void);
483 G_END_DECLS
485 #endif