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 | /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
*
* Copyright (C) 2004-2009 William Jon McCann <mccann@jhu.edu>
* Copyright (C) 2009 Red Hat, Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
*
* Authors: William Jon McCann <mccann@jhu.edu>
*
*/
#include "config.h"
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <errno.h>
#include <string.h>
#include <sys/time.h>
#include <sys/types.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif /* HAVE_UNISTD_H */
#include <cdk/cdkx.h>
#include <ctk/ctk.h>
#include "gs-fade.h"
#include "gs-debug.h"
#define CAFE_DESKTOP_USE_UNSTABLE_API
#include "libcafe-desktop/cafe-rr.h"
/* XFree86 4.x+ Gamma fading */
#ifdef HAVE_XF86VMODE_GAMMA
#include <X11/extensions/xf86vmode.h>
#define XF86_MIN_GAMMA 0.1
#endif /* HAVE_XF86VMODE_GAMMA */
static void gs_fade_finalize (GObject *object);
struct GSGammaInfo
{
int size;
unsigned short *r;
unsigned short *g;
unsigned short *b;
};
struct GSFadeScreenPrivate
{
int fade_type;
int num_ramps;
/* one per crtc in randr mode */
struct GSGammaInfo *info;
/* one per screen in theory */
CafeRRScreen *rrscreen;
#ifdef HAVE_XF86VMODE_GAMMA
/* one per screen also */
XF86VidModeGamma vmg;
#endif /* HAVE_XF86VMODE_GAMMA */
gboolean (*fade_setup) (GSFade *fade);
gboolean (*fade_set_alpha_gamma) (GSFade *fade,
gdouble alpha);
void (*fade_finish) (GSFade *fade);
};
struct GSFadePrivate
{
guint enabled : 1;
guint active : 1;
guint timeout;
guint step;
guint num_steps;
guint timer_id;
gdouble alpha_per_iter;
gdouble current_alpha;
struct GSFadeScreenPrivate screen_priv;
};
enum
{
FADED,
LAST_SIGNAL
};
enum
{
FADE_TYPE_NONE,
FADE_TYPE_GAMMA_NUMBER,
FADE_TYPE_GAMMA_RAMP,
FADE_TYPE_XRANDR,
};
static guint signals [LAST_SIGNAL] = { 0, };
G_DEFINE_TYPE_WITH_PRIVATE (GSFade, gs_fade, G_TYPE_OBJECT)<--- There is an unknown macro here somewhere. Configuration is required. If G_DEFINE_TYPE_WITH_PRIVATE is a macro then please configure it.
static gpointer fade_object = NULL;
#ifdef HAVE_XF86VMODE_GAMMA
/* This is needed because the VidMode extension doesn't work
on remote displays -- but if the remote display has the extension
at all, XF86VidModeQueryExtension returns true, and then
XF86VidModeQueryVersion dies with an X error.
*/
static gboolean error_handler_hit = FALSE;
static int
ignore_all_errors_ehandler (Display *dpy,
XErrorEvent *error)
{
error_handler_hit = TRUE;
return 0;
}
static Bool
safe_XF86VidModeQueryVersion (Display *dpy,
int *majP,
int *minP)
{
Bool result;
XErrorHandler old_handler;
XSync (dpy, False);
error_handler_hit = FALSE;
old_handler = XSetErrorHandler (ignore_all_errors_ehandler);
result = XF86VidModeQueryVersion (dpy, majP, minP);
XSync (dpy, False);
XSetErrorHandler (old_handler);
XSync (dpy, False);
return (error_handler_hit
? False
: result);
}
static gboolean
xf86_whack_gamma (int screen,
struct GSFadeScreenPrivate *screen_priv,
float ratio)
{
Bool status;
struct GSGammaInfo *gamma_info;
CdkDisplay *display;
gamma_info = screen_priv->info;
if (!gamma_info)
return FALSE;
if (ratio < 0)
{
ratio = 0;
}
if (ratio > 1)
{
ratio = 1;
}
if (gamma_info->size == 0)
{
/* we only have a gamma number, not a ramp. */
XF86VidModeGamma g2;
g2.red = screen_priv->vmg.red * ratio;
g2.green = screen_priv->vmg.green * ratio;
g2.blue = screen_priv->vmg.blue * ratio;
if (g2.red < XF86_MIN_GAMMA)
{
g2.red = XF86_MIN_GAMMA;
}
if (g2.green < XF86_MIN_GAMMA)
{
g2.green = XF86_MIN_GAMMA;
}
if (g2.blue < XF86_MIN_GAMMA)
{
g2.blue = XF86_MIN_GAMMA;
}
status = XF86VidModeSetGamma (CDK_DISPLAY_XDISPLAY (cdk_display_get_default ()), screen, &g2);
}
else
{
# ifdef HAVE_XF86VMODE_GAMMA_RAMP
unsigned short *r, *g, *b;
int i;
r = g_new0 (unsigned short, gamma_info->size);
g = g_new0 (unsigned short, gamma_info->size);
b = g_new0 (unsigned short, gamma_info->size);
for (i = 0; i < gamma_info->size; i++)
{
r[i] = gamma_info->r[i] * ratio;
g[i] = gamma_info->g[i] * ratio;
b[i] = gamma_info->b[i] * ratio;
}
status = XF86VidModeSetGammaRamp (CDK_DISPLAY_XDISPLAY (cdk_display_get_default ()), screen, gamma_info->size, r, g, b);
g_free (r);
g_free (g);
g_free (b);
# else /* !HAVE_XF86VMODE_GAMMA_RAMP */
abort ();
# endif /* !HAVE_XF86VMODE_GAMMA_RAMP */
}
display = cdk_display_get_default ();
cdk_display_flush (display);
return status;
}
#endif /* HAVE_XF86VMODE_GAMMA */
/* VidModeExtension version 2.0 or better is needed to do gamma.
2.0 added gamma values; 2.1 added gamma ramps.
*/
# define XF86_VIDMODE_GAMMA_MIN_MAJOR 2
# define XF86_VIDMODE_GAMMA_MIN_MINOR 0
# define XF86_VIDMODE_GAMMA_RAMP_MIN_MAJOR 2
# define XF86_VIDMODE_GAMMA_RAMP_MIN_MINOR 1
gboolean
gs_fade_get_enabled (GSFade *fade)
{
g_return_val_if_fail (GS_IS_FADE (fade), FALSE);
return fade->priv->enabled;
}
void
gs_fade_set_enabled (GSFade *fade,
gboolean enabled)
{
g_return_if_fail (GS_IS_FADE (fade));
if (fade->priv->enabled != enabled)
{
fade->priv->enabled = enabled;
}
}
#ifdef HAVE_XF86VMODE_GAMMA
static gboolean
gamma_fade_setup (GSFade *fade)
{
gboolean res;
struct GSFadeScreenPrivate *screen_priv;
screen_priv = &fade->priv->screen_priv;
if (screen_priv->info)
return TRUE;
# ifndef HAVE_XF86VMODE_GAMMA_RAMP
if (FADE_TYPE_GAMMA_RAMP == screen_priv->fade_type)
{
/* server is newer than client! */
screen_priv->fade_type = FADE_TYPE_GAMMA_NUMBER;
}
# endif
# ifdef HAVE_XF86VMODE_GAMMA_RAMP
screen_priv->info = g_new0(struct GSGammaInfo, 1);
screen_priv->num_ramps = 1;
if (FADE_TYPE_GAMMA_RAMP == screen_priv->fade_type)
{
/* have ramps */
res = XF86VidModeGetGammaRampSize (CDK_DISPLAY_XDISPLAY (cdk_display_get_default ()),
CDK_SCREEN_XNUMBER (cdk_screen_get_default ()),
&screen_priv->info->size);
if (!res || screen_priv->info->size <= 0)
{
screen_priv->fade_type = FADE_TYPE_GAMMA_NUMBER;
goto test_number;
}
screen_priv->info->r = g_new0 (unsigned short, screen_priv->info->size);
screen_priv->info->g = g_new0 (unsigned short, screen_priv->info->size);
screen_priv->info->b = g_new0 (unsigned short, screen_priv->info->size);
if (! (screen_priv->info->r && screen_priv->info->g && screen_priv->info->b))
{
screen_priv->fade_type = FADE_TYPE_GAMMA_NUMBER;
goto test_number;
}
res = XF86VidModeGetGammaRamp (CDK_DISPLAY_XDISPLAY (cdk_display_get_default ()),
CDK_SCREEN_XNUMBER (cdk_screen_get_default ()),
screen_priv->info->size,
screen_priv->info->r,
screen_priv->info->g,
screen_priv->info->b);
if (! res)
{
screen_priv->fade_type = FADE_TYPE_GAMMA_NUMBER;
goto test_number;
}
gs_debug ("Initialized gamma ramp fade");
}
# endif /* HAVE_XF86VMODE_GAMMA_RAMP */
test_number:
if (FADE_TYPE_GAMMA_NUMBER == screen_priv->fade_type)
{
/* only have gamma parameter, not ramps. */
res = XF86VidModeGetGamma (CDK_DISPLAY_XDISPLAY (cdk_display_get_default ()),
CDK_SCREEN_XNUMBER (cdk_screen_get_default ()),
&screen_priv->vmg);
if (! res)
{
screen_priv->fade_type = FADE_TYPE_NONE;
goto test_none;
}
gs_debug ("Initialized gamma fade: %f %f %f",
screen_priv->vmg.red,
screen_priv->vmg.green,
screen_priv->vmg.blue);
}
test_none:
if (FADE_TYPE_NONE == screen_priv->fade_type)
{
goto FAIL;
}
return TRUE;
FAIL:
return FALSE;
}
#endif /* HAVE_XF86VMODE_GAMMA */
static void
screen_fade_finish (GSFade *fade)
{
struct GSFadeScreenPrivate *screen_priv;
int i;
screen_priv = &fade->priv->screen_priv;
if (!screen_priv->info)
return;
for (i = 0; i < screen_priv->num_ramps; i++)
{
if (screen_priv->info[i].r)
g_free (screen_priv->info[i].r);
if (screen_priv->info[i].g)
g_free (screen_priv->info[i].g);
if (screen_priv->info[i].b)
g_free (screen_priv->info[i].b);
}
g_free (screen_priv->info);
screen_priv->info = NULL;
screen_priv->num_ramps = 0;
}
#ifdef HAVE_XF86VMODE_GAMMA
static gboolean
gamma_fade_set_alpha_gamma (GSFade *fade,
gdouble alpha)
{
struct GSFadeScreenPrivate *screen_priv;
int screen_idx = CDK_SCREEN_XNUMBER (cdk_screen_get_default ());
screen_priv = &fade->priv->screen_priv;
xf86_whack_gamma (screen_idx, screen_priv, alpha);
return TRUE;
}
#endif /* HAVE_XF86VMODE_GAMMA */
static void
check_gamma_extension (GSFade *fade)
{
struct GSFadeScreenPrivate *screen_priv;
#ifdef HAVE_XF86VMODE_GAMMA
int event;
int error;
int major;
int minor;
gboolean res;
#endif /* HAVE_XF86VMODE_GAMMA */
screen_priv = &fade->priv->screen_priv;
#ifdef HAVE_XF86VMODE_GAMMA
res = XF86VidModeQueryExtension (CDK_DISPLAY_XDISPLAY (cdk_display_get_default ()), &event, &error);
if (! res)
goto fade_none;
res = safe_XF86VidModeQueryVersion (CDK_DISPLAY_XDISPLAY (cdk_display_get_default ()), &major, &minor);
if (! res)
goto fade_none;
if (major < XF86_VIDMODE_GAMMA_MIN_MAJOR ||
(major == XF86_VIDMODE_GAMMA_MIN_MAJOR &&
minor < XF86_VIDMODE_GAMMA_MIN_MINOR))
goto fade_none;
screen_priv->fade_setup = gamma_fade_setup;
screen_priv->fade_finish = screen_fade_finish;
screen_priv->fade_set_alpha_gamma = gamma_fade_set_alpha_gamma;
if (major < XF86_VIDMODE_GAMMA_RAMP_MIN_MAJOR ||
(major == XF86_VIDMODE_GAMMA_RAMP_MIN_MAJOR &&
minor < XF86_VIDMODE_GAMMA_RAMP_MIN_MINOR))
{
screen_priv->fade_type = FADE_TYPE_GAMMA_NUMBER;
return;
}
/* Copacetic */
screen_priv->fade_type = FADE_TYPE_GAMMA_RAMP;
return;
fade_none:
#endif
screen_priv->fade_type = FADE_TYPE_NONE;
}
/* Xrandr support */
static gboolean xrandr_fade_setup (GSFade *fade)
{
struct GSFadeScreenPrivate *screen_priv;
CafeRRCrtc *crtc;
CafeRRCrtc **crtcs;
int crtc_count = 0;
struct GSGammaInfo *info;
gboolean res;
screen_priv = &fade->priv->screen_priv;
if (screen_priv->info)
return TRUE;
/* refresh the screen info */
cafe_rr_screen_refresh (screen_priv->rrscreen, NULL);
crtcs = cafe_rr_screen_list_crtcs (screen_priv->rrscreen);
while (*crtcs)
{
crtc_count++;
crtcs++;
};
screen_priv->info = g_new0 (struct GSGammaInfo, crtc_count);
screen_priv->num_ramps = crtc_count;
crtc_count = 0;
crtcs = cafe_rr_screen_list_crtcs (screen_priv->rrscreen);
while (*crtcs)
{
crtc = *crtcs;
info = &screen_priv->info[crtc_count];
/* if no mode ignore crtc */
if (!cafe_rr_crtc_get_current_mode (crtc))
{
info->size = 0;
info->r = NULL;
info->g = NULL;
info->b = NULL;
}
else
{
res = cafe_rr_crtc_get_gamma (crtc, &info->size,
&info->r, &info->g,
&info->b);
if (res == FALSE)
goto fail;
}
crtcs++;
crtc_count++;
}
return TRUE;
fail:
return FALSE;
}
static void xrandr_crtc_whack_gamma (CafeRRCrtc *crtc,
struct GSGammaInfo *gamma_info,
float ratio)
{
unsigned short *r, *g, *b;
int i;
if (gamma_info->size == 0)
return;
if (ratio < 0)
{
ratio = 0;
}
if (ratio > 1)
{
ratio = 1;
}
r = g_new0 (unsigned short, gamma_info->size);
g = g_new0 (unsigned short, gamma_info->size);
b = g_new0 (unsigned short, gamma_info->size);
for (i = 0; i < gamma_info->size; i++)
{
r[i] = gamma_info->r[i] * ratio;
g[i] = gamma_info->g[i] * ratio;
b[i] = gamma_info->b[i] * ratio;
}
cafe_rr_crtc_set_gamma (crtc, gamma_info->size,
r, g, b);
g_free (r);
g_free (g);
g_free (b);
}
static gboolean xrandr_fade_set_alpha_gamma (GSFade *fade,
gdouble alpha)
{
struct GSFadeScreenPrivate *screen_priv;
struct GSGammaInfo *info;
CafeRRCrtc **crtcs;
int i;
screen_priv = &fade->priv->screen_priv;
if (!screen_priv->info)
return FALSE;
crtcs = cafe_rr_screen_list_crtcs (screen_priv->rrscreen);
i = 0;
while (*crtcs)
{
info = &screen_priv->info[i];
xrandr_crtc_whack_gamma (*crtcs, info, alpha);
i++;
crtcs++;
}
return TRUE;
}
static void
check_randr_extension (GSFade *fade)
{
CdkDisplay *display = cdk_display_get_default ();
CdkScreen *screen = cdk_display_get_default_screen (display);
struct GSFadeScreenPrivate *screen_priv;
screen_priv = &fade->priv->screen_priv;
screen_priv->rrscreen = cafe_rr_screen_new (screen,
NULL);
if (!screen_priv->rrscreen)
{
screen_priv->fade_type = FADE_TYPE_NONE;
return;
}
screen_priv->fade_type = FADE_TYPE_XRANDR;
screen_priv->fade_setup = xrandr_fade_setup;
screen_priv->fade_finish = screen_fade_finish;
screen_priv->fade_set_alpha_gamma = xrandr_fade_set_alpha_gamma;
}
static gboolean
gs_fade_set_alpha (GSFade *fade,
gdouble alpha)
{
gboolean ret = FALSE;
switch (fade->priv->screen_priv.fade_type)
{
case FADE_TYPE_GAMMA_RAMP:
case FADE_TYPE_GAMMA_NUMBER:
case FADE_TYPE_XRANDR:
ret = fade->priv->screen_priv.fade_set_alpha_gamma (fade, alpha);
break;
case FADE_TYPE_NONE:
ret = FALSE;
break;
default:
g_warning ("Unknown fade type");
ret = FALSE;
break;
}
return ret;
}
static gboolean
gs_fade_out_iter (GSFade *fade)
{
gboolean ret;
if (fade->priv->current_alpha < 0.01)
{
return FALSE;
}
fade->priv->current_alpha -= fade->priv->alpha_per_iter;
ret = gs_fade_set_alpha (fade, fade->priv->current_alpha);
return ret;
}
static gboolean
gs_fade_stop (GSFade *fade)
{
if (fade->priv->timer_id > 0)
{
g_source_remove (fade->priv->timer_id);
fade->priv->timer_id = 0;
}
fade->priv->step = 0;
fade->priv->active = FALSE;
return TRUE;
}
void
gs_fade_finish (GSFade *fade)
{
g_return_if_fail (GS_IS_FADE (fade));
if (! fade->priv->active)
{
return;
}
gs_fade_stop (fade);
g_signal_emit (fade, signals [FADED], 0);
fade->priv->active = FALSE;
}
static gboolean
fade_out_timer (GSFade *fade)
{
gboolean res;
res = gs_fade_out_iter (fade);
/* if failed then fade is complete */
if (! res)
{
gs_fade_finish (fade);
return FALSE;
}
return TRUE;
}
gboolean
gs_fade_get_active (GSFade *fade)
{
g_return_val_if_fail (GS_IS_FADE (fade), FALSE);
return fade->priv->active;
}
static void
gs_fade_set_timeout (GSFade *fade,
guint timeout)
{
g_return_if_fail (GS_IS_FADE (fade));
fade->priv->timeout = timeout;
}
static void
gs_fade_start (GSFade *fade,
guint timeout)
{
guint steps_per_sec = 60;
guint msecs_per_step;
gboolean active_fade, res;
g_return_if_fail (GS_IS_FADE (fade));
if (fade->priv->screen_priv.fade_type != FADE_TYPE_NONE)
{
res = fade->priv->screen_priv.fade_setup (fade);
if (res == FALSE)
return;
}
if (fade->priv->timer_id > 0)
{
gs_fade_stop (fade);
}
fade->priv->active = TRUE;
gs_fade_set_timeout (fade, timeout);
active_fade = FALSE;
if (fade->priv->screen_priv.fade_type != FADE_TYPE_NONE)
active_fade = TRUE;
if (active_fade)
{
guint num_steps;
num_steps = (fade->priv->timeout / 1000.0) * steps_per_sec;
msecs_per_step = 1000 / steps_per_sec;
fade->priv->alpha_per_iter = 1.0 / (gdouble)num_steps;
fade->priv->timer_id = g_timeout_add (msecs_per_step, (GSourceFunc)fade_out_timer, fade);
}
else
{
gs_fade_finish (fade);
}
}
typedef struct
{
GSFadeDoneFunc done_cb;
gpointer data;
} FadedCallbackData;
static void
gs_fade_async_callback (GSFade *fade,
FadedCallbackData *cdata)
{
g_signal_handlers_disconnect_by_func (fade,
gs_fade_async_callback,
cdata);
if (cdata->done_cb)
{
cdata->done_cb (fade, cdata->data);
}
g_free (cdata);
}
void
gs_fade_async (GSFade *fade,
guint timeout,
GSFadeDoneFunc func,
gpointer data)
{
g_return_if_fail (GS_IS_FADE (fade));
/* if fade is active then pause it */
if (fade->priv->active)
{
gs_fade_stop (fade);
}
if (func)
{
FadedCallbackData *cb_data;
cb_data = g_new0 (FadedCallbackData, 1);
cb_data->done_cb = func;
cb_data->data = data;
g_signal_connect (fade, "faded",
G_CALLBACK (gs_fade_async_callback),
cb_data);
}
gs_fade_start (fade, timeout);
}
static void
gs_fade_sync_callback (GSFade *fade,
int *flag)
{
*flag = TRUE;
g_signal_handlers_disconnect_by_func (fade,
gs_fade_sync_callback,
flag);
}
void
gs_fade_sync (GSFade *fade,
guint timeout)
{
int flag = FALSE;
g_return_if_fail (GS_IS_FADE (fade));
/* if fade is active then pause it */
if (fade->priv->active)
{
gs_fade_stop (fade);
}
g_signal_connect (fade, "faded",
G_CALLBACK (gs_fade_sync_callback),
&flag);
gs_fade_start (fade, timeout);
while (! flag)
{
ctk_main_iteration ();
}
}
void
gs_fade_reset (GSFade *fade)
{
g_return_if_fail (GS_IS_FADE (fade));
gs_debug ("Resetting fade");
if (fade->priv->active)
{
gs_fade_stop (fade);
}
fade->priv->current_alpha = 1.0;
gs_fade_set_alpha (fade, fade->priv->current_alpha);
if (fade->priv->screen_priv.fade_type != FADE_TYPE_NONE)
fade->priv->screen_priv.fade_finish (fade);
}
static void
gs_fade_class_init (GSFadeClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
object_class->finalize = gs_fade_finalize;
signals [FADED] =
g_signal_new ("faded",
G_TYPE_FROM_CLASS (object_class),
G_SIGNAL_RUN_LAST,
G_STRUCT_OFFSET (GSFadeClass, faded),
NULL,
NULL,
g_cclosure_marshal_VOID__VOID,
G_TYPE_NONE,
0, G_TYPE_NONE);
}
static void
gs_fade_init (GSFade *fade)
{
fade->priv = gs_fade_get_instance_private (fade);
fade->priv->timeout = 1000;
fade->priv->current_alpha = 1.0;
check_randr_extension (fade);
if (!fade->priv->screen_priv.fade_type)
check_gamma_extension (fade);
gs_debug ("Fade type: %d", fade->priv->screen_priv.fade_type);
}
static void
gs_fade_finalize (GObject *object)
{
GSFade *fade;
g_return_if_fail (object != NULL);
g_return_if_fail (GS_IS_FADE (object));
fade = GS_FADE (object);
g_return_if_fail (fade->priv != NULL);
fade->priv->screen_priv.fade_finish(fade);
if (fade->priv->screen_priv.rrscreen)
g_object_unref (fade->priv->screen_priv.rrscreen);
fade->priv->screen_priv.rrscreen = NULL;
G_OBJECT_CLASS (gs_fade_parent_class)->finalize (object);
}
GSFade *
gs_fade_new (void)
{
if (fade_object)
{
g_object_ref (fade_object);
}
else
{
fade_object = g_object_new (GS_TYPE_FADE, NULL);
g_object_add_weak_pointer (fade_object,
(gpointer *) &fade_object);
}
return GS_FADE (fade_object);
}
|