forked from linuxmint/cinnamon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cinnamon-recorder.c
1820 lines (1530 loc) · 55.3 KB
/
cinnamon-recorder.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
#include "config.h"
#include <fcntl.h>
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#define GST_USE_UNSTABLE_API
#include <gst/gst.h>
#include "cinnamon-recorder-src.h"
#include "cinnamon-recorder.h"
#include <clutter/x11/clutter-x11.h>
#include <X11/extensions/Xfixes.h>
#include "st.h"
typedef enum {
RECORDER_STATE_CLOSED,
RECORDER_STATE_PAUSED,
RECORDER_STATE_RECORDING
} RecorderState;
typedef struct _RecorderPipeline RecorderPipeline;
struct _CinnamonRecorderClass
{
GObjectClass parent_class;
};
struct _CinnamonRecorder {
GObject parent;
/* A "maximum" amount of memory to use for buffering. This is used
* to alert the user that they are filling up memory rather than
* any that actually affects recording. (In kB)
*/
guint memory_target;
guint memory_used; /* Current memory used. (In kB) */
RecorderState state;
char *unique; /* The unique string we are using for this recording */
int count; /* How many times the recording has been started */
ClutterStage *stage;
int stage_width;
int stage_height;
gboolean have_pointer;
int pointer_x;
int pointer_y;
guint vertical_adjust; // Y adjustment from bottom panel, if any
guint horizontal_adjust; // X adjustment to position on right edge of primary monitor
gboolean have_xfixes;
int xfixes_event_base;
CoglHandle recording_icon; /* icon shown while playing */
cairo_surface_t *cursor_image;
int cursor_hot_x;
int cursor_hot_y;
gboolean only_paint; /* Used to temporarily suppress recording */
int framerate;
char *pipeline_description;
char *filename;
gboolean filename_has_count; /* %c used: handle pausing differently */
/* We might have multiple pipelines that are finishing encoding
* to go along with the current pipeline where we are recording.
*/
RecorderPipeline *current_pipeline; /* current pipeline */
GSList *pipelines; /* all pipelines */
GstClockTime start_time; /* When we started recording (adjusted for pauses) */
GstClockTime pause_time; /* When the pipeline was paused */
/* GSource IDs for different timeouts and idles */
guint redraw_timeout;
guint redraw_idle;
guint update_memory_used_timeout;
guint update_pointer_timeout;
guint repaint_hook_id;
};
struct _RecorderPipeline
{
CinnamonRecorder *recorder;
GstElement *pipeline;
GstElement *src;
int outfile;
};
static void recorder_set_stage (CinnamonRecorder *recorder,
ClutterStage *stage);
static void recorder_set_framerate (CinnamonRecorder *recorder,
int framerate);
static void recorder_set_pipeline (CinnamonRecorder *recorder,
const char *pipeline);
static void recorder_set_filename (CinnamonRecorder *recorder,
const char *filename);
static void recorder_pipeline_set_caps (RecorderPipeline *pipeline);
static void recorder_pipeline_closed (RecorderPipeline *pipeline);
enum {
PROP_0,
PROP_STAGE,
PROP_FRAMERATE,
PROP_PIPELINE,
PROP_FILENAME
};
G_DEFINE_TYPE(CinnamonRecorder, cinnamon_recorder, G_TYPE_OBJECT);
/* The number of frames per second we configure for the GStreamer pipeline.
* (the number of frames we actually write into the GStreamer pipeline is
* based entirely on how fast clutter is drawing.) Using 60fps seems high
* but the observed smoothness is a lot better than for 30fps when encoding
* as theora for a minimal size increase. This may be an artifact of the
* encoding process.
*/
#define DEFAULT_FRAMES_PER_SECOND 15
/* The time (in milliseconds) between querying the server for the cursor
* position.
*/
#define UPDATE_POINTER_TIME 100
/* The time we wait (in milliseconds) before redrawing when the memory used
* changes.
*/
#define UPDATE_MEMORY_USED_DELAY 500
/* Maximum time between frames, in milliseconds. If we don't send data
* for a long period of time, then when we send the next frame, a lot
* of work can be created for the encoder to do, so we want to force a
* periodic redraw when nothing happen.
*/
#define MAXIMUM_PAUSE_TIME 1000
/* The default pipeline. videorate is used to give a constant stream of
* frames to theora even if there is a pause because nothing is moving.
* (Theora does have some support for frames at non-uniform times, but
* things seem to break down if there are large gaps.)
*/
#define DEFAULT_PIPELINE "vp8enc min_quantizer=13 max_quantizer=13 cpu-used=5 deadline=1000000 threads=%T ! queue ! webmmux"
/* The default filename pattern. Example cinnamon-20090311b-2.webm
*/
#define DEFAULT_FILENAME "cinnamon-%d%u-%c.webm"
/* If we can find the amount of memory on the machine, we use half
* of that for memory_target, otherwise, we use this value, in kB.
*/
#define DEFAULT_MEMORY_TARGET (512*1024)
#define PANEL_HEIGHT_KEY "panel-bottom-height"
#define SCHEMA_CINNAMON "org.cinnamon"
/* Create an emblem to show at the lower-left corner of the stage while
* recording. The emblem is drawn *after* we record the frame so doesn't
* show up in the frame.
*/
static CoglHandle
create_recording_icon (void)
{
cairo_surface_t *surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 32, 32);
cairo_t *cr;
cairo_pattern_t *pat;
CoglHandle texture;
cr = cairo_create (surface);
/* clear to transparent */
cairo_save (cr);
cairo_set_operator (cr, CAIRO_OPERATOR_CLEAR);
cairo_paint (cr);
cairo_restore (cr);
/* radial "glow" */
pat = cairo_pattern_create_radial (16, 16, 6,
16, 16, 14);
cairo_pattern_add_color_stop_rgba (pat, 0.0,
1, 0, 0, 1); /* opaque red */
cairo_pattern_add_color_stop_rgba (pat, 1.0,
1, 0, 0, 0); /* transparent red */
cairo_set_source (cr, pat);
cairo_paint (cr);
cairo_pattern_destroy (pat);
/* red circle */
cairo_arc (cr, 16, 16, 8,
0, 2 * M_PI);
cairo_set_source_rgb (cr, 1, 0, 0);
cairo_fill (cr);
cairo_destroy (cr);
texture = st_cogl_texture_new_from_data_wrapper (32, 32,
COGL_TEXTURE_NONE,
CLUTTER_CAIRO_FORMAT_ARGB32,
COGL_PIXEL_FORMAT_ANY,
cairo_image_surface_get_stride (surface),
cairo_image_surface_get_data (surface));
cairo_surface_destroy (surface);
return texture;
}
static guint
get_memory_target (void)
{
FILE *f;
/* Really simple "get amount of memory on the machine" if it
* doesn't work, you just get the default memory target.
*/
f = fopen("/proc/meminfo", "r");
if (!f)
return DEFAULT_MEMORY_TARGET;
while (!feof(f))
{
gchar line_buffer[1024];
guint mem_total;
if (fscanf(f, "MemTotal: %u", &mem_total) == 1)
{
fclose(f);
return mem_total / 2;
}
/* Skip to the next line and discard what we read */
if (fgets(line_buffer, sizeof(line_buffer), f) == NULL)
break;
}
fclose(f);
return DEFAULT_MEMORY_TARGET;
}
/*
* Used to force full stage redraws during recording to avoid artifacts
*
* Note: That this will cause the stage to be repainted on
* every animation frame even if the frame wouldn't normally cause any new
* drawing
*/
static gboolean
recorder_repaint_hook (gpointer data)
{
ClutterActor *stage = data;
clutter_actor_queue_redraw (stage);
return TRUE;
}
static void
cinnamon_recorder_init (CinnamonRecorder *recorder)
{
GdkRectangle work_rect, geo_rect;
GdkScreen *screen;
gint primary;
/* Calling gst_init() is a no-op if GStreamer was previously initialized */
gst_init (NULL, NULL);
cinnamon_recorder_src_register ();
screen = gdk_screen_get_default ();
primary = gdk_screen_get_primary_monitor (screen);
gdk_screen_get_monitor_workarea (screen, primary, &work_rect);
gdk_screen_get_monitor_geometry (screen, primary, &geo_rect);
recorder->vertical_adjust = (geo_rect.y + geo_rect.height) - (work_rect.y + work_rect.height);
recorder->horizontal_adjust = work_rect.x + work_rect.width;
recorder->recording_icon = create_recording_icon ();
recorder->memory_target = get_memory_target();
recorder->state = RECORDER_STATE_CLOSED;
recorder->framerate = DEFAULT_FRAMES_PER_SECOND;
}
static void
cinnamon_recorder_finalize (GObject *object)
{
CinnamonRecorder *recorder = CINNAMON_RECORDER (object);
if (recorder->update_memory_used_timeout) {
g_source_remove (recorder->update_memory_used_timeout);
recorder->update_memory_used_timeout = 0;
}
if (recorder->cursor_image)
cairo_surface_destroy (recorder->cursor_image);
recorder_set_stage (recorder, NULL);
recorder_set_pipeline (recorder, NULL);
recorder_set_filename (recorder, NULL);
cogl_handle_unref (recorder->recording_icon);
G_OBJECT_CLASS (cinnamon_recorder_parent_class)->finalize (object);
}
static void
recorder_on_stage_destroy (ClutterActor *actor,
CinnamonRecorder *recorder)
{
recorder_set_stage (recorder, NULL);
}
/* Add together the memory used by all pipelines; both the
* currently recording pipeline and pipelines finishing
* recording asynchronously.
*/
static void
recorder_update_memory_used (CinnamonRecorder *recorder,
gboolean repaint)
{
guint memory_used = 0;
GSList *l;
for (l = recorder->pipelines; l; l = l->next)
{
RecorderPipeline *pipeline = l->data;
guint pipeline_memory_used;
g_object_get (pipeline->src,
"memory-used", &pipeline_memory_used,
NULL);
memory_used += pipeline_memory_used;
}
if (memory_used != recorder->memory_used)
{
recorder->memory_used = memory_used;
if (repaint)
{
/* In other cases we just queue a redraw even if we only need
* to repaint and not redraw a frame, but having changes in
* memory usage cause frames to be painted and memory used
* seems like a bad idea.
*/
recorder->only_paint = TRUE;
clutter_stage_ensure_redraw (recorder->stage);
recorder->only_paint = FALSE;
}
}
}
/* Timeout used to avoid not drawing for more than MAXIMUM_PAUSE_TIME
*/
static gboolean
recorder_redraw_timeout (gpointer data)
{
CinnamonRecorder *recorder = data;
recorder->redraw_timeout = 0;
clutter_actor_queue_redraw (CLUTTER_ACTOR (recorder->stage));
return FALSE;
}
static void
recorder_add_redraw_timeout (CinnamonRecorder *recorder)
{
if (recorder->redraw_timeout == 0)
{
recorder->redraw_timeout = g_timeout_add (MAXIMUM_PAUSE_TIME,
recorder_redraw_timeout,
recorder);
}
}
static void
recorder_remove_redraw_timeout (CinnamonRecorder *recorder)
{
if (recorder->redraw_timeout != 0)
{
g_source_remove (recorder->redraw_timeout);
recorder->redraw_timeout = 0;
}
}
static void
recorder_fetch_cursor_image (CinnamonRecorder *recorder)
{
XFixesCursorImage *cursor_image;
guchar *data;
int stride;
int i, j;
if (!recorder->have_xfixes)
return;
cursor_image = XFixesGetCursorImage (clutter_x11_get_default_display ());
if (!cursor_image)
return;
recorder->cursor_hot_x = cursor_image->xhot;
recorder->cursor_hot_y = cursor_image->yhot;
recorder->cursor_image = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
cursor_image->width,
cursor_image->height);
/* The pixel data (in typical Xlib breakage) is longs even on
* 64-bit platforms, so we have to data-convert there. For simplicity,
* just do it always
*/
data = cairo_image_surface_get_data (recorder->cursor_image);
stride = cairo_image_surface_get_stride (recorder->cursor_image);
for (i = 0; i < cursor_image->height; i++)
for (j = 0; j < cursor_image->width; j++)
*(guint32 *)(data + i * stride + 4 * j) = cursor_image->pixels[i * cursor_image->width + j];
cairo_surface_mark_dirty (recorder->cursor_image);
XFree (cursor_image);
}
/* Overlay the cursor image on the frame. We draw the cursor image
* into the host-memory buffer after we've captured the frame. An
* alternate approach would be to turn off the cursor while recording
* and draw the cursor ourselves with GL, but then we'd need to figure
* out what the cursor looks like, or hard-code a non-system cursor.
*/
static void
recorder_draw_cursor (CinnamonRecorder *recorder,
GstBuffer *buffer)
{
GstMapInfo info;
cairo_surface_t *surface;
cairo_t *cr;
/* We don't show a cursor unless the hot spot is in the frame; this
* means that sometimes we aren't going to draw a cursor even when
* there is a little bit overlapping within the stage */
if (recorder->pointer_x < 0 ||
recorder->pointer_y < 0 ||
recorder->pointer_x >= recorder->stage_width ||
recorder->pointer_y >= recorder->stage_height)
return;
if (!recorder->cursor_image)
recorder_fetch_cursor_image (recorder);
if (!recorder->cursor_image)
return;
gst_buffer_map (buffer, &info, GST_MAP_WRITE);
surface = cairo_image_surface_create_for_data (info.data,
CAIRO_FORMAT_ARGB32,
recorder->stage_width,
recorder->stage_height,
recorder->stage_width * 4);
cr = cairo_create (surface);
cairo_set_source_surface (cr,
recorder->cursor_image,
recorder->pointer_x - recorder->cursor_hot_x,
recorder->pointer_y - recorder->cursor_hot_y);
cairo_paint (cr);
cairo_destroy (cr);
cairo_surface_destroy (surface);
gst_buffer_unmap (buffer, &info);
}
/* Draw an overlay indicating how much of the target memory is used
* for buffering frames.
*/
static void
recorder_draw_buffer_meter (CinnamonRecorder *recorder)
{
int fill_level;
recorder_update_memory_used (recorder, FALSE);
/* As the buffer gets more full, we go from green, to yellow, to red */
if (recorder->memory_used > (recorder->memory_target * 3) / 4)
cogl_set_source_color4f (1, 0, 0, 1);
else if (recorder->memory_used > recorder->memory_target / 2)
cogl_set_source_color4f (1, 1, 0, 1);
else
cogl_set_source_color4f (0, 1, 0, 1);
fill_level = MIN (60, (recorder->memory_used * 60) / recorder->memory_target);
/* A hollow rectangle filled from the left to fill_level */
cogl_rectangle (recorder->horizontal_adjust - 64, recorder->stage_height - recorder->vertical_adjust - 10,
recorder->horizontal_adjust - 2, recorder->stage_height - recorder->vertical_adjust - 9);
cogl_rectangle (recorder->horizontal_adjust - 64, recorder->stage_height - recorder->vertical_adjust - 9,
recorder->horizontal_adjust - (63 - fill_level), recorder->stage_height - recorder->vertical_adjust - 3);
cogl_rectangle (recorder->horizontal_adjust - 3, recorder->stage_height - recorder->vertical_adjust - 9,
recorder->horizontal_adjust - 2, recorder->stage_height - recorder->vertical_adjust - 3);
cogl_rectangle (recorder->horizontal_adjust - 64, recorder->stage_height - recorder->vertical_adjust - 3,
recorder->horizontal_adjust - 2, recorder->stage_height - recorder->vertical_adjust - 2);
}
/* We want to time-stamp each frame based on the actual time it was
* recorded. We probably should use the pipeline clock rather than
* gettimeofday(): that would be needed to get sync'ed audio correct.
* I'm not immediately sure how to handle the adjustment we currently
* do when pausing recording - is pausing the pipeline enough?
*/
static GstClockTime
get_wall_time (void)
{
GTimeVal tv;
g_get_current_time (&tv);
return tv.tv_sec * 1000000000LL + tv.tv_usec * 1000LL;
}
/* Retrieve a frame and feed it into the pipeline
*/
static void
recorder_record_frame (CinnamonRecorder *recorder)
{
GstBuffer *buffer;
guint8 *data;
guint size;
size = recorder->stage_width * recorder->stage_height * 4;
data = g_malloc (size);
buffer = gst_buffer_new();
gst_buffer_insert_memory (buffer, -1,
gst_memory_new_wrapped (0, data, size, 0,
size, data, g_free));
GST_BUFFER_PTS(buffer) = get_wall_time() - recorder->start_time;
cogl_read_pixels (0, 0,
recorder->stage_width, recorder->stage_height,
COGL_READ_PIXELS_COLOR_BUFFER,
CLUTTER_CAIRO_FORMAT_ARGB32,
data);
recorder_draw_cursor (recorder, buffer);
cinnamon_recorder_src_add_buffer (CINNAMON_RECORDER_SRC (recorder->current_pipeline->src), buffer);
gst_buffer_unref (buffer);
/* Reset the timeout that we used to avoid an overlong pause in the stream */
recorder_remove_redraw_timeout (recorder);
recorder_add_redraw_timeout (recorder);
}
/* We hook in by recording each frame right after the stage is painted
* by clutter before glSwapBuffers() makes it visible to the user.
*/
static void
recorder_on_stage_paint (ClutterActor *actor,
CinnamonRecorder *recorder)
{
if (recorder->state == RECORDER_STATE_RECORDING)
{
if (!recorder->only_paint)
recorder_record_frame (recorder);
cogl_set_source_texture (recorder->recording_icon);
cogl_rectangle (recorder->horizontal_adjust - 32, recorder->stage_height - recorder->vertical_adjust - 42,
recorder->horizontal_adjust, recorder->stage_height - recorder->vertical_adjust - 10);
}
if (recorder->state == RECORDER_STATE_RECORDING || recorder->memory_used != 0)
recorder_draw_buffer_meter (recorder);
}
static void
recorder_update_size (CinnamonRecorder *recorder)
{
ClutterActorBox allocation;
clutter_actor_get_allocation_box (CLUTTER_ACTOR (recorder->stage), &allocation);
recorder->stage_width = (int)(0.5 + allocation.x2 - allocation.x1);
recorder->stage_height = (int)(0.5 + allocation.y2 - allocation.y1);
}
static void
recorder_on_stage_notify_size (GObject *object,
GParamSpec *pspec,
CinnamonRecorder *recorder)
{
recorder_update_size (recorder);
/* This breaks the recording but tweaking the GStreamer pipeline a bit
* might make it work, at least if the codec can handle a stream where
* the frame size changes in the middle.
*/
if (recorder->current_pipeline)
recorder_pipeline_set_caps (recorder->current_pipeline);
}
static gboolean
recorder_idle_redraw (gpointer data)
{
CinnamonRecorder *recorder = data;
recorder->redraw_idle = 0;
clutter_actor_queue_redraw (CLUTTER_ACTOR (recorder->stage));
return FALSE;
}
static void
recorder_queue_redraw (CinnamonRecorder *recorder)
{
/* If we just queue a redraw on every mouse motion (for example), we
* starve Clutter, which operates at a very low priority. So
* we need to queue a "low priority redraw" after timeline updates
*/
if (recorder->state == RECORDER_STATE_RECORDING && recorder->redraw_idle == 0)
recorder->redraw_idle = g_idle_add_full (CLUTTER_PRIORITY_REDRAW + 1,
recorder_idle_redraw, recorder, NULL);
}
/* We use an event filter on the stage to get the XFixesCursorNotifyEvent
* and also to track cursor position (when the cursor is over the stage's
* input area); tracking cursor position here rather than with ClutterEvent
* allows us to avoid worrying about event propagation and competing
* signal handlers.
*/
static ClutterX11FilterReturn
recorder_event_filter (XEvent *xev,
ClutterEvent *cev,
gpointer data)
{
CinnamonRecorder *recorder = data;
if (xev->xany.window != clutter_x11_get_stage_window (recorder->stage))
return CLUTTER_X11_FILTER_CONTINUE;
if (xev->xany.type == recorder->xfixes_event_base + XFixesCursorNotify)
{
XFixesCursorNotifyEvent *notify_event = (XFixesCursorNotifyEvent *)xev;
if (notify_event->subtype == XFixesDisplayCursorNotify)
{
if (recorder->cursor_image)
{
cairo_surface_destroy (recorder->cursor_image);
recorder->cursor_image = NULL;
}
recorder_queue_redraw (recorder);
}
}
else if (xev->xany.type == MotionNotify)
{
recorder->pointer_x = xev->xmotion.x;
recorder->pointer_y = xev->xmotion.y;
recorder_queue_redraw (recorder);
}
/* We want to track whether the pointer is over the stage
* window itself, and not in a child window. A "virtual"
* crossing is one that goes directly from ancestor to child.
*/
else if (xev->xany.type == EnterNotify &&
(xev->xcrossing.detail != NotifyVirtual &&
xev->xcrossing.detail != NotifyNonlinearVirtual))
{
recorder->have_pointer = TRUE;
recorder->pointer_x = xev->xcrossing.x;
recorder->pointer_y = xev->xcrossing.y;
recorder_queue_redraw (recorder);
}
else if (xev->xany.type == LeaveNotify &&
(xev->xcrossing.detail != NotifyVirtual &&
xev->xcrossing.detail != NotifyNonlinearVirtual))
{
recorder->have_pointer = FALSE;
recorder->pointer_x = xev->xcrossing.x;
recorder->pointer_y = xev->xcrossing.y;
recorder_queue_redraw (recorder);
}
return CLUTTER_X11_FILTER_CONTINUE;
}
/* We optimize out querying the server for the pointer position if the
* pointer is in the input area of the ClutterStage. We track changes to
* that with Enter/Leave events, but we need to 100% accurate about the
* initial condition, which is a little involved.
*/
static void
recorder_get_initial_cursor_position (CinnamonRecorder *recorder)
{
Display *xdisplay = clutter_x11_get_default_display ();
Window xwindow = clutter_x11_get_stage_window (recorder->stage);
XWindowAttributes xwa;
Window root, child, parent;
Window *children;
guint n_children;
int root_x,root_y;
int window_x, window_y;
guint mask;
XGrabServer(xdisplay);
XGetWindowAttributes (xdisplay, xwindow, &xwa);
XQueryTree (xdisplay, xwindow, &root, &parent, &children, &n_children);
XFree (children);
if (xwa.map_state == IsViewable &&
XQueryPointer (xdisplay, parent,
&root, &child, &root_x, &root_y, &window_x, &window_y, &mask) &&
child == xwindow)
{
/* The point of this call is not actually to translate the coordinates -
* we could do that ourselves using xwa.{x,y} - but rather to see if
* the pointer is in a child of the window, which we count as "not in
* window", because we aren't guaranteed to get pointer events.
*/
XTranslateCoordinates(xdisplay, parent, xwindow,
window_x, window_y,
&window_x, &window_y, &child);
if (child == None)
{
recorder->have_pointer = TRUE;
recorder->pointer_x = window_x;
recorder->pointer_y = window_y;
}
}
else
recorder->have_pointer = FALSE;
XUngrabServer(xdisplay);
XFlush(xdisplay);
/* While we are at it, add mouse events to the event mask; they will
* be there for the stage windows that Clutter creates by default, but
* maybe this stage was created differently. Since we've already
* retrieved the event mask, it's almost free.
*/
XSelectInput(xdisplay, xwindow,
xwa.your_event_mask | EnterWindowMask | LeaveWindowMask | PointerMotionMask);
}
/* When the cursor is not over the stage's input area, we query for the
* pointer position in a timeout.
*/
static void
recorder_update_pointer (CinnamonRecorder *recorder)
{
Display *xdisplay = clutter_x11_get_default_display ();
Window xwindow = clutter_x11_get_stage_window (recorder->stage);
Window root, child;
int root_x,root_y;
int window_x, window_y;
guint mask;
if (recorder->have_pointer)
return;
if (XQueryPointer (xdisplay, xwindow,
&root, &child, &root_x, &root_y, &window_x, &window_y, &mask))
{
if (window_x != recorder->pointer_x || window_y != recorder->pointer_y)
{
recorder->pointer_x = window_x;
recorder->pointer_y = window_y;
recorder_queue_redraw (recorder);
}
}
}
static gboolean
recorder_update_pointer_timeout (gpointer data)
{
recorder_update_pointer (data);
return TRUE;
}
static void
recorder_add_update_pointer_timeout (CinnamonRecorder *recorder)
{
if (!recorder->update_pointer_timeout)
recorder->update_pointer_timeout = g_timeout_add (UPDATE_POINTER_TIME,
recorder_update_pointer_timeout,
recorder);
}
static void
recorder_remove_update_pointer_timeout (CinnamonRecorder *recorder)
{
if (recorder->update_pointer_timeout)
{
g_source_remove (recorder->update_pointer_timeout);
recorder->update_pointer_timeout = 0;
}
}
static void
recorder_set_stage (CinnamonRecorder *recorder,
ClutterStage *stage)
{
if (recorder->stage == stage)
return;
if (recorder->current_pipeline)
cinnamon_recorder_close (recorder);
if (recorder->stage)
{
g_signal_handlers_disconnect_by_func (recorder->stage,
(void *)recorder_on_stage_destroy,
recorder);
g_signal_handlers_disconnect_by_func (recorder->stage,
(void *)recorder_on_stage_paint,
recorder);
g_signal_handlers_disconnect_by_func (recorder->stage,
(void *)recorder_on_stage_notify_size,
recorder);
clutter_x11_remove_filter (recorder_event_filter, recorder);
/* We don't don't deselect for cursor changes in case someone else just
* happened to be selecting for cursor events on the same window; sending
* us the events is close to free in any case.
*/
if (recorder->redraw_idle)
{
g_source_remove (recorder->redraw_idle);
recorder->redraw_idle = 0;
}
}
recorder->stage = stage;
if (recorder->stage)
{
int error_base;
recorder->stage = stage;
g_signal_connect (recorder->stage, "destroy",
G_CALLBACK (recorder_on_stage_destroy), recorder);
g_signal_connect_after (recorder->stage, "paint",
G_CALLBACK (recorder_on_stage_paint), recorder);
g_signal_connect (recorder->stage, "notify::width",
G_CALLBACK (recorder_on_stage_notify_size), recorder);
g_signal_connect (recorder->stage, "notify::width",
G_CALLBACK (recorder_on_stage_notify_size), recorder);
clutter_x11_add_filter (recorder_event_filter, recorder);
recorder_update_size (recorder);
recorder->have_xfixes = XFixesQueryExtension (clutter_x11_get_default_display (),
&recorder->xfixes_event_base,
&error_base);
if (recorder->have_xfixes)
XFixesSelectCursorInput (clutter_x11_get_default_display (),
clutter_x11_get_stage_window (stage),
XFixesDisplayCursorNotifyMask);
clutter_stage_ensure_current (stage);
recorder_get_initial_cursor_position (recorder);
}
}
static void
recorder_set_framerate (CinnamonRecorder *recorder,
int framerate)
{
if (framerate == recorder->framerate)
return;
if (recorder->current_pipeline)
cinnamon_recorder_close (recorder);
recorder->framerate = framerate;
g_object_notify (G_OBJECT (recorder), "framerate");
}
static void
recorder_set_pipeline (CinnamonRecorder *recorder,
const char *pipeline)
{
if (pipeline == recorder->pipeline_description ||
(pipeline && recorder->pipeline_description && strcmp (recorder->pipeline_description, pipeline) == 0))
return;
if (recorder->current_pipeline)
cinnamon_recorder_close (recorder);
if (recorder->pipeline_description)
g_free (recorder->pipeline_description);
recorder->pipeline_description = g_strdup (pipeline);
g_object_notify (G_OBJECT (recorder), "pipeline");
}
static void
recorder_set_filename (CinnamonRecorder *recorder,
const char *filename)
{
if (filename == recorder->filename ||
(filename && recorder->filename && strcmp (recorder->filename, filename) == 0))
return;
if (recorder->current_pipeline)
cinnamon_recorder_close (recorder);
if (recorder->filename)
g_free (recorder->filename);
recorder->filename = g_strdup (filename);
g_object_notify (G_OBJECT (recorder), "filename");
}
static void
cinnamon_recorder_set_property (GObject *object,
guint prop_id,
const GValue *value,
GParamSpec *pspec)
{
CinnamonRecorder *recorder = CINNAMON_RECORDER (object);
switch (prop_id)
{
case PROP_STAGE:
recorder_set_stage (recorder, g_value_get_object (value));
break;
case PROP_FRAMERATE:
recorder_set_framerate (recorder, g_value_get_int (value));
break;
case PROP_PIPELINE:
recorder_set_pipeline (recorder, g_value_get_string (value));
break;
case PROP_FILENAME:
recorder_set_filename (recorder, g_value_get_string (value));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static void
cinnamon_recorder_get_property (GObject *object,
guint prop_id,
GValue *value,
GParamSpec *pspec)
{
CinnamonRecorder *recorder = CINNAMON_RECORDER (object);
switch (prop_id)
{
case PROP_STAGE:
g_value_set_object (value, G_OBJECT (recorder->stage));
break;
case PROP_FRAMERATE:
g_value_set_int (value, recorder->framerate);
break;
case PROP_PIPELINE:
g_value_set_string (value, recorder->pipeline_description);
break;
case PROP_FILENAME:
g_value_set_string (value, recorder->filename);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static void
cinnamon_recorder_class_init (CinnamonRecorderClass *klass)
{
GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
gobject_class->finalize = cinnamon_recorder_finalize;
gobject_class->get_property = cinnamon_recorder_get_property;
gobject_class->set_property = cinnamon_recorder_set_property;
g_object_class_install_property (gobject_class,
PROP_STAGE,