1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * runtime.h: Core surface and canvas definitions.
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.
17 #include <gtk/gtkwidget.h>
21 #include "uielement.h"
22 #include "dependencyobject.h"
30 #define MAXIMUM_CACHE_SIZE 6000000
32 #define FRONT_TO_BACK_STATS 0
35 #define DEBUG_MARKER_KEY 0
37 #define STARTTIMER(id,str) TimeSpan id##_t_start = get_now(); printf ("timing of '%s' started at %lld\n", str, id##_t_start)
38 #define ENDTIMER(id,str) TimeSpan id##_t_end = get_now(); printf ("timing of '%s' ended at %lld (%f seconds)\n", str, id##_t_end, (double)(id##_t_end - id##_t_start) / 10000000)
40 #define STARTTIMER(id,str)
41 #define ENDTIMER(id,str)
45 #define VERIFY_MAIN_THREAD \
46 if (!Surface::InMainThread ()) { \
47 printf ("Moonlight: This method should only be called from the main thread (%s)\n", __PRETTY_FUNCTION__); \
48 print_stack_trace (); \
51 #define VERIFY_MAIN_THREAD
54 enum RuntimeInitFlags
{
55 RUNTIME_INIT_PANGO_TEXT_LAYOUT
= 1 << 0,
56 // (not used) = 1 << 1,
57 RUNTIME_INIT_MANUAL_TIMESOURCE
= 1 << 2,
58 RUNTIME_INIT_DISABLE_AUDIO
= 1 << 3,
59 RUNTIME_INIT_SHOW_EXPOSE
= 1 << 4,
60 RUNTIME_INIT_SHOW_CLIPPING
= 1 << 5,
61 RUNTIME_INIT_SHOW_BOUNDING_BOXES
= 1 << 6,
62 RUNTIME_INIT_SHOW_TEXTBOXES
= 1 << 7,
63 RUNTIME_INIT_SHOW_FPS
= 1 << 8,
64 RUNTIME_INIT_RENDER_FRONT_TO_BACK
= 1 << 9,
65 RUNTIME_INIT_SHOW_CACHE_SIZE
= 1 << 10,
66 RUNTIME_INIT_FFMPEG_YUV_CONVERTER
= 1 << 11,
67 RUNTIME_INIT_USE_SHAPE_CACHE
= 1 << 12,
68 RUNTIME_INIT_USE_UPDATE_POSITION
= 1 << 13,
69 RUNTIME_INIT_ALLOW_WINDOWLESS
= 1 << 14,
70 RUNTIME_INIT_AUDIO_ALSA_MMAP
= 1 << 15,
71 RUNTIME_INIT_AUDIO_ALSA_RW
= 1 << 16,
72 RUNTIME_INIT_AUDIO_ALSA
= 1 << 17,
73 RUNTIME_INIT_AUDIO_PULSE
= 1 << 18,
74 RUNTIME_INIT_USE_IDLE_HINT
= 1 << 19,
75 RUNTIME_INIT_USE_BACKEND_XLIB
= 1 << 20,
76 RUNTIME_INIT_KEEP_MEDIA
= 1 << 21,
77 RUNTIME_INIT_ENABLE_MS_CODECS
= 1 << 22,
78 RUNTIME_INIT_DISABLE_FFMPEG_CODECS
= 1 << 23,
79 RUNTIME_INIT_ALL_IMAGE_FORMATS
= 1 << 24,
80 RUNTIME_INIT_CREATE_ROOT_DOMAIN
= 1 << 25,
81 RUNTIME_INIT_DESKTOP_EXTENSIONS
= 1 << 26,
84 extern guint32 moonlight_flags
;
88 enum RuntimeDebugFlags
{
89 RUNTIME_DEBUG_ALSA
= 1 << 0,
90 RUNTIME_DEBUG_AUDIO
= 1 << 1,
91 RUNTIME_DEBUG_PULSE
= 1 << 2,
92 RUNTIME_DEBUG_HTTPSTREAMING
= 1 << 3,
93 RUNTIME_DEBUG_MARKERS
= 1 << 4,
94 RUNTIME_DEBUG_MMS
= 1 << 5,
95 RUNTIME_DEBUG_MEDIAPLAYER
= 1 << 6,
96 RUNTIME_DEBUG_PIPELINE
= 1 << 7,
97 RUNTIME_DEBUG_PIPELINE_ERROR
= 1 << 8,
98 RUNTIME_DEBUG_FRAMEREADERLOOP
= 1 << 9,
99 RUNTIME_DEBUG_FFMPEG
= 1 << 10,
100 RUNTIME_DEBUG_UI
= 1 << 11,
101 RUNTIME_DEBUG_CODECS
= 1 << 12,
102 RUNTIME_DEBUG_DP
= 1 << 13,
103 RUNTIME_DEBUG_DOWNLOADER
= 1 << 14,
104 RUNTIME_DEBUG_FONT
= 1 << 15,
105 RUNTIME_DEBUG_LAYOUT
= 1 << 16,
106 RUNTIME_DEBUG_MEDIA
= 1 << 17,
107 RUNTIME_DEBUG_MEDIAELEMENT
= 1 << 18,
108 RUNTIME_DEBUG_BUFFERING
= 1 << 19,
109 RUNTIME_DEBUG_ASF
= 1 << 20,
110 RUNTIME_DEBUG_PLAYLIST
= 1 << 21,
111 RUNTIME_DEBUG_TEXT
= 1 << 22,
112 RUNTIME_DEBUG_XAML
= 1 << 23,
113 RUNTIME_DEBUG_DEPLOYMENT
= 1 << 24,
114 RUNTIME_DEBUG_MSI
= 1 << 25,
115 RUNTIME_DEBUG_MP3
= 1 << 26,
116 RUNTIME_DEBUG_VALUE
= 1 << 27,
119 enum RuntimeDebugFlagsExtra
{
120 RUNTIME_DEBUG_ALSA_EX
= 1 << 0,
121 RUNTIME_DEBUG_AUDIO_EX
= 1 << 1,
122 RUNTIME_DEBUG_PULSE_EX
= 1 << 2,
123 RUNTIME_DEBUG_MARKERS_EX
= 1 << 3,
124 RUNTIME_DEBUG_MEDIAPLAYER_EX
= 1 << 4,
125 RUNTIME_DEBUG_MEDIAELEMENT_EX
= 1 << 5,
126 RUNTIME_DEBUG_PLAYLIST_EX
= 1 << 6,
127 RUNTIME_DEBUG_PIPELINE_EX
= 1 << 7,
130 extern guint32 debug_flags_ex
;
131 extern guint32 debug_flags
;
139 typedef void (* MoonlightFPSReportFunc
) (Surface
*surface
, int nframes
, float nsecs
, void *user_data
);
140 typedef void (* MoonlightCacheReportFunc
) (Surface
*surface
, long size
, void *user_data
);
141 typedef bool (* MoonlightEventEmitFunc
) (UIElement
*element
, GdkEvent
*event
);
142 typedef void (* MoonlightExposeHandoffFunc
) (Surface
*surface
, TimeSpan time
, void *user_data
);
144 class Surface
: public EventObject
{
146 /* @GenerateCBinding,GeneratePInvoke */
147 Surface (MoonWindow
*window
);
148 virtual void Dispose ();
150 /* @GenerateCBinding */
151 MoonWindow
*GetWindow () { return active_window
; }
153 // allows you to redirect painting of the surface to an
154 // arbitrary cairo context.
155 void Paint (cairo_t
*ctx
, Region
*region
);
156 /* @GenerateCBinding,GeneratePInvoke */
157 void Paint (cairo_t
*ctx
, int x
, int y
, int width
, int height
);
159 /* @GenerateCBinding,GeneratePInvoke */
160 void Attach (UIElement
*toplevel
);
162 void AttachLayer (UIElement
*layer
);
164 void DetachLayer (UIElement
*layer
);
166 void SetCursor (MouseCursor cursor
);
168 void ReleaseMouseCapture (UIElement
*capture
);
169 bool SetMouseCapture (UIElement
*capture
);
171 /* @GenerateCBinding,GeneratePInvoke */
172 void Resize (int width
, int height
);
174 void EmitSourceDownloadComplete ();
175 void EmitSourceDownloadProgressChanged (DownloadProgressEventArgs
*args
);
176 void EmitError (ErrorEventArgs
*args
);
177 /* @GenerateCBinding,GeneratePInvoke */
178 void EmitError (int number
, int code
, const char *message
);
182 void SetBackgroundColor (Color
*color
);
183 /* @GenerateCBinding,GeneratePInvoke */
184 Color
*GetBackgroundColor ();
186 int GetFrameCount () { return frames
; }
187 void ResetFrameCount () { frames
= 0; }
189 virtual void Invalidate (Rect r
);
190 virtual void ProcessUpdates ();
192 /* @GenerateCBinding,GeneratePInvoke */
193 UIElement
*GetToplevel() { return toplevel
; }
194 bool IsTopLevel (UIElement
*top
);
196 /* @GenerateCBinding,GeneratePInvoke,Version=2.0 */
197 UIElement
*GetFocusedElement () { return focused_element
; }
199 bool FocusElement (UIElement
*element
);
201 /* @GenerateCBinding,GeneratePInvoke */
202 bool IsLoaded () { return toplevel
!= NULL
&& ticked_after_attach
; }
204 /* @GenerateCBinding,GeneratePInvoke */
205 static bool IsVersionSupported (const char *version
);
207 const static int ResizeEvent
;
208 const static int FullScreenChangeEvent
;
209 const static int ErrorEvent
;
210 const static int LoadEvent
;
211 const static int SourceDownloadProgressChangedEvent
;
212 const static int SourceDownloadCompleteEvent
;
214 /* @GenerateCBinding,GeneratePInvoke,Version=2.0 */
215 bool GetFullScreen () { return full_screen
; }
216 /* @GenerateCBinding,GeneratePInvoke,Version=2.0 */
217 void SetFullScreen (bool value
);
219 void SetUserInitiatedEvent (bool value
);
221 bool FirstUserInitiatedEvent () { return first_user_initiated_event
; }
222 /* @GenerateCBinding,GeneratePInvoke,Version=2.0 */
223 bool IsUserInitiatedEvent () { return user_initiated_event
; }
225 const char* GetSourceLocation ();
226 void SetSourceLocation (const char *location
);
227 bool FullScreenKeyHandled (GdkEventKey
*key
);
229 /* @GenerateCBinding,GeneratePInvoke */
230 TimeManager
*GetTimeManager () { return time_manager
; }
232 void SetDownloaderContext (gpointer context
) { downloader_context
= context
; }
233 gpointer
GetDownloaderContext () { return downloader_context
; }
235 /* @GenerateCBinding,GeneratePInvoke */
236 Downloader
*CreateDownloader ();
237 static Downloader
*CreateDownloader (EventObject
*obj
);
239 void SetFPSReportFunc (MoonlightFPSReportFunc report
, void *user_data
);
240 void SetCacheReportFunc (MoonlightCacheReportFunc report
, void *user_data
);
241 void SetExposeHandoffFunc (MoonlightExposeHandoffFunc func
, void *user_data
);
243 bool VerifyWithCacheSizeCounter (int w
, int h
);
244 gint64
AddToCacheSizeCounter (int w
, int h
);
245 void RemoveFromCacheSizeCounter (gint64 size
);
247 // called from the plugin if the surface is headed for death.
248 // stops event emission (since the plugin counterparts to xaml
249 // objects will be destroyed)
252 bool IsZombie () { return zombie
; }
254 void DetachDownloaders ();
256 #if FRONT_TO_BACK_STATS
257 int uielements_rendered_front_to_back
;
258 int uielements_rendered_back_to_front
;
262 UIElement
*debug_selected_element
;
265 void PaintToDrawable (GdkDrawable
*drawable
, GdkVisual
*visual
, GdkEventExpose
*event
, int off_x
, int off_y
, bool transparent
, bool clear_transparent
);
268 gboolean
HandleUIMotion (GdkEventMotion
*event
);
269 gboolean
HandleUICrossing (GdkEventCrossing
*event
);
270 gboolean
HandleUIKeyPress (GdkEventKey
*event
);
271 gboolean
HandleUIKeyRelease (GdkEventKey
*event
);
272 gboolean
HandleUIButtonRelease (GdkEventButton
*event
);
273 gboolean
HandleUIButtonPress (GdkEventButton
*event
);
274 gboolean
HandleUIScroll (GdkEventScroll
*event
);
275 gboolean
HandleUIFocusIn (GdkEventFocus
*event
);
276 gboolean
HandleUIFocusOut (GdkEventFocus
*event
);
277 void HandleUIWindowAllocation (bool emit_resize
);
278 void HandleUIWindowAvailable ();
279 void HandleUIWindowUnavailable ();
280 void HandleUIWindowDestroyed (MoonWindow
*window
);
282 // bad, but these live in dirty.cpp, not runtime.cpp
283 void AddDirtyElement (UIElement
*element
, DirtyType dirt
);
284 void UpdateLayout ();
285 void RemoveDirtyElement (UIElement
*element
);
286 bool ProcessDirtyElements ();
287 void PropagateDirtyFlagToChildren (UIElement
*element
, DirtyType dirt
);
289 static bool main_thread_inited
;
290 static pthread_t main_thread
;
291 /* @GenerateCBinding,GeneratePInvoke */
292 static bool InMainThread () { return (!main_thread_inited
|| pthread_equal (main_thread
, pthread_self ())); }
297 // The current window we are drawing to
298 MoonWindow
*active_window
;
303 // are we headed for death?
306 // bad, but these two live in dirty.cpp, not runtime.cpp
307 void ProcessDownDirtyElements ();
308 void ProcessUpDirtyElements ();
310 DirtyLists
*down_dirty
;
311 DirtyLists
*up_dirty
;
313 gpointer downloader_context
;
315 static void OnDownloaderDestroyed (EventObject
*sender
, EventArgs
*args
, gpointer closure
);
317 Color
*background_color
;
319 // This is the normal-sized window
320 MoonWindow
*normal_window
;
322 // We set active_window to this whenever we are in fullscreen mode.
323 MoonWindow
*fullscreen_window
;
325 // We can have multiple top level elements, these are stored as layers
326 HitTestCollection
*layers
;
328 // This currently can only be a canvas.
331 // The element holding the keyboard focus, and the one that
332 // held it previously (so we can emit lostfocus events async)
333 UIElement
*focused_element
;
334 Queue
*focus_changed_events
;
336 // the list of elements (from most deeply nested to the
337 // toplevel) we've most recently sent a mouse event to.
340 // is the mouse captured? if it is, it'll be by the first element in input_list.
342 UIElement
*pendingCapture
;
343 bool pendingReleaseCapture
;
345 // are we currently emitting a mouse event?
346 bool emittingMouseEvent
;
348 // the currently shown cursor
351 // Fullscreen support
353 Canvas
*full_screen_message
;
354 char *source_location
;
356 // True once we have received at least one user initiated event
357 bool first_user_initiated_event
;
358 // Should be set to true only while executing MouseLeftButtonDown,
359 // MouseLeftButtonUp, KeyDown, and KeyUp event handlers
360 bool user_initiated_event
;
362 void UpdateFullScreen (bool value
);
364 TimeManager
*time_manager
;
365 bool ticked_after_attach
;
366 static void tick_after_attach_reached (EventObject
*data
);
370 GdkEvent
*mouse_event
;
372 // Variables for reporting FPS
373 MoonlightFPSReportFunc fps_report
;
378 // Variables for reporting cache size
379 MoonlightCacheReportFunc cache_report
;
380 gint64 cache_size_in_bytes
;
381 int cache_size_ticker
;
383 int cache_size_multiplier
;
386 TimeSpan expose_handoff_last_timespan
;
387 MoonlightExposeHandoffFunc expose_handoff
;
388 void *expose_handoff_data
;
391 static void AutoFocusAsync (EventObject
*sender
);
394 void ShowFullScreenMessage ();
395 void HideFullScreenMessage ();
397 void CreateSimilarSurface ();
399 static void render_cb (EventObject
*sender
, EventArgs
*calldata
, gpointer closure
);
400 static void update_input_cb (EventObject
*sender
, EventArgs
*calldata
, gpointer closure
);
401 static void widget_destroyed (GtkWidget
*w
, gpointer data
);
403 EventArgs
* CreateArgsForEvent (int event_id
, GdkEvent
*event
);
405 List
* ElementPathToRoot (UIElement
*source
);
406 void GenerateFocusChangeEvents();
408 void FindFirstCommonElement (List
*l1
, int *index1
, List
*l2
, int *index2
);
409 bool EmitEventOnList (int event_id
, List
*element_list
, GdkEvent
*event
, int end_idx
);
410 void UpdateCursorFromInputList ();
411 bool HandleMouseEvent (int event_id
, bool emit_leave
, bool emit_enter
, bool force_emit
, GdkEvent
*event
);
412 void PerformCapture (UIElement
*capture
);
413 void PerformReleaseCapture ();
415 static void toplevel_loaded (EventObject
*sender
, EventArgs
*args
, gpointer closure
);
416 void ToplevelLoaded (UIElement
*element
);
419 /* for hit testing */
420 class UIElementNode
: public List::Node
{
422 UIElement
*uielement
;
424 UIElementNode (UIElement
*el
);
425 virtual ~UIElementNode ();
429 typedef void (*RenderFunc
) (cairo_t
*ctx
, UIElement
*uielement
, Region
*region
, bool front_to_back
);
431 class RenderNode
: public List::Node
{
433 RenderNode (UIElement
*el
, Region
*region
, bool render_element
, RenderFunc pre
, RenderFunc post
);
435 void Render (cairo_t
*cr
);
437 virtual ~RenderNode ();
440 UIElement
*uielement
;
443 RenderFunc pre_render
;
444 RenderFunc post_render
;
450 void runtime_init (const char *platform_dir
, guint32 flags
);
452 void runtime_init_browser (const char *plugin_dir
);
453 /* @GeneratePInvoke */
454 void runtime_init_desktop ();
456 GList
*runtime_get_surface_list (void);
458 void runtime_flags_set_manual_timesource (gboolean flag
);
459 void runtime_flags_set_show_fps (gboolean flag
);
460 void runtime_flags_set_use_shapecache (gboolean flag
);
462 void runtime_shutdown (void);