forked from SawfishWM/rep-gtk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rep-gtk.c
2630 lines (2243 loc) · 63 KB
/
rep-gtk.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
/* Copyright (C) 1997, 1998, 1999 Marius Vollmer
* Copyright (C) 1999 John Harper <[email protected]>
*
* $Id$
*
* 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, 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 software; see the file COPYING. If not, write to
* the Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*/
#include <config.h>
#include <assert.h>
#include <gtk/gtk.h>
#include <gdk/gdkprivate.h>
#include <gdk/gdkx.h>
#include "rep-gtk.h"
#include <string.h>
#include <limits.h>
#ifdef HAVE_LOCALE_H
# include <locale.h>
#endif
/* Define this to enable some output during GC and other interesting
actions. */
#undef DEBUG_PRINT
static int
list_length (repv list)
{
repv len = Flength (list);
return (len && rep_INTP (len)) ? rep_INT (len) : 0;
}
/* Associating SCM values with Gtk pointers.
We keep a hash table that can store a SCM value for an arbitray
gpointer. This is used for the proxies of GObjects and the boxed
types. */
static GHashTable *proxy_tab;
static void
enter_proxy (gpointer obj, repv proxy)
{
if (proxy_tab == NULL)
proxy_tab = g_hash_table_new (NULL, NULL);
g_hash_table_insert (proxy_tab, obj, (gpointer)proxy);
}
static repv
get_proxy (gpointer obj)
{
if (proxy_tab)
{
gpointer val = g_hash_table_lookup (proxy_tab, obj);
return val? (repv) val : Qnil;
}
return Qnil;
}
static void
forget_proxy (gpointer obj)
{
g_hash_table_remove (proxy_tab, obj);
}
/* Storing additional info about a GType.
We used to use the type's SEQNO, but these aren't globally
contiguous anymore, so we use type g_type_set_qdata() instead. */
static GQuark type_info_quark = 0;
static void
enter_type_info (sgtk_type_info *info)
{
if (!type_info_quark)
{
type_info_quark = g_quark_from_static_string ("rep-gtk-type-info");
}
g_type_set_qdata (info->type, type_info_quark, info);
}
sgtk_type_info*
sgtk_get_type_info (GType type)
{
return (type_info_quark
? g_type_get_qdata (type, type_info_quark)
: 0);
}
static sgtk_type_info*
must_get_type_info (GType type)
{
sgtk_type_info *info = sgtk_get_type_info (type);
if (info == NULL)
abort ();
return info;
}
typedef struct _type_infos {
struct _type_infos *next;
sgtk_type_info **infos;
} type_infos;
static type_infos *all_type_infos;
/* Find types that are mentioned in our *.defs files but are not
provided by the GLib run-time system. This is only used
occasionally to update the table in sgtk_try_missing_type. */
#ifdef NEED_UNUSED_CODE
static void
sgtk_find_missing_types (type_infos *infos)
{
sgtk_type_info **ip;
for (ip = infos->infos; *ip; ip++)
{
if (g_type_from_name ((*ip)->name) == G_TYPE_INVALID
&& (*ip)->type != G_TYPE_OBJECT)
printf ("missing: %s, %s\n",
(*ip)->name, g_type_name ((*ip)->type));
}
}
#endif
void
sgtk_register_type_infos (sgtk_type_info **infos)
{
type_infos *t;
sgtk_init ();
t = (type_infos *) rep_alloc (sizeof(type_infos));
t->infos = infos;
t->next = all_type_infos;
all_type_infos = t;
#if 0
sgtk_find_missing_types (t);
#endif
}
/* When INFO refers to one of the known `missing' types, we initialize
that type ourselves. This is used to fix certain discrepancies
between old Gtk versions and our *.defs files. It is not OK to do
this in general because we should not assume that we can safely
initialize types from other modules.
XXX this doesn't work at ALL, almost all of these types _do_
have corresponding standard types now and we _certainly_
can't register them ourselves with an all 0s type info. --owt
*/
static GType
sgtk_try_missing_type (char *name)
{
static sgtk_type_info missing[] = {
{ "GdkGC", G_TYPE_BOXED },
{ "GdkSegment", G_TYPE_BOXED },
{ "GdkSpan", G_TYPE_BOXED },
{ "GdkPixbuf", G_TYPE_BOXED }, /* XXX okay? */
{ "GtkTextIter", G_TYPE_BOXED },
{ "GtkTreeIter", G_TYPE_BOXED },
{ "GtkTreeModelForeachFunc", G_TYPE_BOOLEAN },
{ "GtkToolbarStyle", G_TYPE_ENUM },
{ "GtkToolbarChildType", G_TYPE_ENUM },
{ "GtkTreeViewMode", G_TYPE_ENUM },
{ "GtkSpinButtonUpdatePolicy", G_TYPE_ENUM },
{ "GtkCellType", G_TYPE_ENUM },
{ "GdkOverlapType", G_TYPE_ENUM },
{ "GdkWMDecoration", G_TYPE_FLAGS },
{ "GdkWMFunction", G_TYPE_FLAGS },
{ "GdkVisibilityState", G_TYPE_ENUM },
{ "GdkInputSource", G_TYPE_ENUM },
{NULL, G_TYPE_NONE}
};
sgtk_type_info *m;
for (m = missing; m->name; m++)
if (!strcmp (m->name, name))
{
GTypeInfo info = { 0 };
return g_type_register_static (m->type, m->name, &info, 0);
}
return G_TYPE_INVALID;
}
static int
sgtk_fillin_type_info (sgtk_type_info *info)
{
if (info->type != G_TYPE_OBJECT
&& info->type == G_TYPE_FUNDAMENTAL (info->type)
&& info->type != G_TYPE_INVALID)
{
GType parent_type = info->type;
GType this_type = g_type_from_name (info->name);
if (this_type == G_TYPE_INVALID)
this_type = sgtk_try_missing_type (info->name);
if (this_type == G_TYPE_INVALID)
{
if (info->type == G_TYPE_BOXED)
fprintf (stderr, "unknown type `%s'.\n", info->name);
return 0;
}
info->type = this_type;
if (G_TYPE_FUNDAMENTAL (info->type) != parent_type)
{
fprintf (stderr, "mismatch for type `%s'.\n", info->name);
info->type = G_TYPE_INVALID;
return 0;
}
enter_type_info (info);
}
return 1;
}
sgtk_type_info*
sgtk_maybe_find_type_info (GType type)
{
sgtk_type_info *info;
type_infos *infos;
const char *name;
info = sgtk_get_type_info (type);
if (info)
return info;
/* XXX - merge this with the GObject code. I don't have the brain
right now to do it. */
name = g_type_name (type);
for (infos = all_type_infos; infos; infos = infos->next)
{
sgtk_type_info **ip;
for (ip = infos->infos; *ip; ip++)
if (!strcmp ((*ip)->name, name))
{
if (G_TYPE_FUNDAMENTAL (type) != (*ip)->type)
{
fprintf (stderr, "mismatch for type `%s'.\n", name);
info->type = G_TYPE_INVALID;
abort ();
}
(*ip)->type = type;
enter_type_info (*ip);
return *ip;
}
}
/* XXX - should use the GLib type introspection here instead of
giving up. */
return NULL;
}
sgtk_type_info *
sgtk_find_type_info (GType type)
{
sgtk_type_info *info = sgtk_maybe_find_type_info (type);
if (info)
return info;
fprintf (stderr, "unknown type `%s'.\n", g_type_name (type));
abort ();
}
/* G[tk]Objects.
GtkObjects are wrapped with a smob. The smob of a GtkObject is
called its proxy. The proxy and its GtkObject are strongly
connected; that is, the GtkObject will stay around as long as the
proxy is referenced from Scheme, and the proxy will not be
collected as long as the GtkObject is used from outside of Scheme.
The lifetime of GtkObjects is controlled by a reference count,
while Scheme objects are managed by a tracing garbage collector
(mark/sweep). These two techniques are made to cooperate like
this: the pointer from the proxy to the GtkObject is reflected in
the reference count of the GtkObject. All proxies are kept in a
list and those that point to GtkObjects with a reference count
greater than the number of `internal' references are marked during
the marking phase of the tracing collector. An internal reference
is one that goes from a GtkObject with a proxy to another GtkObject
with a proxy. We can only find a subset of the true internal
references (because Gtk does not yet cooperate), but this should be
good enough.
By using this combination of tracing and reference counting it is
possible to break the cycle that is formed by the proxy pointing to
the GtkObject and the GtkObject pointing back. It is
straightforward to extend this to other kind of cycles that might
occur. For example, when connecting a Scheme procedure as a signal
handler, the procedure is very likely to have the GtkObject that it
is connected to in its environment. This cycle can be broken by
including the procedure in the set of Scheme objects that get
marked when we are tracing GtkObjects with a reference count
greater than 1.
Therefore, each proxy contains a list of `protects' that are marked
when the proxy itself is marked. In addition to this, there is
also a global list of `protects' that is used for Scheme objects
that are somewhere in Gtk land but not clearly associated with a
particular GtkObject (like timeout callbacks).
*/
struct sgtk_protshell {
repv object;
struct sgtk_protshell *next;
struct sgtk_protshell **prevp;
};
static GMemChunk *sgtk_protshell_chunk;
/* Analogous to the PROTECTS list of a proxy but for SCM values that
are not associated with a particular GObject. */
static struct sgtk_protshell *global_protects;
void
sgtk_unprotect (sgtk_protshell *prot)
{
if ((*prot->prevp = prot->next) != 0)
prot->next->prevp = prot->prevp;
g_chunk_free (prot, sgtk_protshell_chunk);
}
static void
sgtk_mark_protects (sgtk_protshell *prots)
{
while (prots)
{
rep_MARKVAL (prots->object);
prots = prots->next;
}
}
/* The CDR of a GObject smob points to one of these. PROTECTS is a
Scheme list of all SCM values that need to be protected from the GC
because they are in use by OBJ. PROTECTS includes the smob cell
itself. NEXT and PREVP are used to chain all proxies together for
the marking mentioned above. NEXT simply points to the next proxy
struct and PREVP points to the pointer that points to us. */
typedef struct _sgtk_object_proxy {
repv car;
GObject *obj;
struct sgtk_protshell *protects;
int traced_refs;
struct _sgtk_object_proxy *next;
} sgtk_object_proxy;
/* The list of all existing proxies. */
static sgtk_object_proxy *all_proxies = NULL;
/* Insert the list of protshells starting at PROTS into the global
protects list. This is used when a proxy is freed so that we don't
forget about its protects. */
static void
sgtk_move_prots_to_global (sgtk_protshell *prots)
{
if (prots)
{
sgtk_protshell *g = global_protects;
global_protects = prots;
global_protects->prevp = &global_protects;
if (g)
{
sgtk_protshell *p;
for (p = prots; p->next; p = p->next)
;
p->next = g;
g->prevp = &p->next;
}
}
}
/* The smob for GObjects. */
static long tc16_gobj;
#define GOBJP(x) (rep_CELL16_TYPEP(x, tc16_gobj))
#define GOBJ_PROXY(x) ((sgtk_object_proxy *)rep_PTR(x))
void
sgtk_set_protect (repv protector, sgtk_protshell *prot)
{
sgtk_protshell **prevp;
if (GOBJP (protector))
prevp = &(GOBJ_PROXY(protector)->protects);
else
prevp = &global_protects;
if ((prot->next = *prevp) != 0)
prot->next->prevp = &prot->next;
*prevp = prot;
prot->prevp = prevp;
}
repv
sgtk_get_protect (sgtk_protshell *prot)
{
return prot->object;
}
sgtk_protshell *
sgtk_new_protect (repv obj)
{
sgtk_protshell *prot = g_chunk_new (sgtk_protshell, sgtk_protshell_chunk);
prot->object = obj;
return prot;
}
sgtk_protshell *
sgtk_protect (repv protector, repv obj)
{
sgtk_protshell *prot = sgtk_new_protect (obj);
sgtk_set_protect (protector, prot);
return prot;
}
void
sgtk_set_gclosure (repv protector, GClosure *closure)
{
sgtk_protshell *prot = closure->data;
g_assert (prot != NULL);
sgtk_set_protect (protector, prot);
}
repv
sgtk_get_gclosure (GClosure *closure)
{
sgtk_protshell *prot = closure->data;
g_assert (prot != NULL);
return sgtk_get_protect (prot);
}
GClosure *
sgtk_new_gclosure (repv obj)
{
sgtk_protshell *prot = sgtk_new_protect (obj);
GClosure *closure = g_closure_new_simple (sizeof (GClosure), prot);
g_closure_add_finalize_notifier (closure, prot,
sgtk_gclosure_callback_destroy);
g_closure_set_marshal (closure, sgtk_gclosure_callback_marshal);
return closure;
}
GClosure *
sgtk_gclosure (repv protector, repv obj)
{
GClosure *prot = sgtk_new_gclosure (obj);
sgtk_set_gclosure (protector, prot);
return prot;
}
static void
mark_traced_ref (GObject *obj, void *data)
{
repv p = (repv)get_proxy (obj);
if (p != Qnil)
{
sgtk_object_proxy *proxy = GOBJ_PROXY (p);
#ifdef DEBUG_PRINT
fprintf (stderr, "marking trace %p %s\n",
proxy->obj, g_type_name (G_OBJECT_TYPE (proxy->obj)));
#endif
sgtk_mark_protects (proxy->protects);
}
}
static void
gobj_mark (repv obj)
{
sgtk_object_proxy *proxy = GOBJ_PROXY(obj);
#ifdef DEBUG_PRINT
fprintf (stderr, "marking %p %s\n",
proxy->obj, g_type_name (G_OBJECT_TYPE (proxy->obj)));
#endif
if (GTK_IS_CONTAINER (proxy->obj))
gtk_container_foreach (GTK_CONTAINER(proxy->obj),
(GtkCallback) mark_traced_ref, NULL);
sgtk_mark_protects (proxy->protects);
}
static void
gobj_print (repv stream, repv obj)
{
char buf[32];
sgtk_object_proxy *proxy = GOBJ_PROXY (obj);
GType tid = G_OBJECT_TYPE (proxy->obj);
const char *type = g_type_name (tid);
rep_stream_puts (stream, "#<", -1, rep_FALSE);
rep_stream_puts (stream, type ? (char *) type : "<unknown GObject>", -1, rep_FALSE);
rep_stream_puts (stream, " ", -1, rep_FALSE);
sprintf (buf, "%lx", (long)proxy->obj);
rep_stream_puts (stream, buf, -1, rep_FALSE);
rep_stream_putc (stream, '>');
}
static void
gobj_free (repv obj)
{
sgtk_object_proxy *proxy = GOBJ_PROXY (obj);
#ifdef DEBUG_PRINT
fprintf (stderr, "freeing %p %s\n",
proxy->obj, g_type_name (G_OBJECT_TYPE (proxy->obj)));
#endif
forget_proxy (proxy->obj);
g_object_unref (proxy->obj);
sgtk_move_prots_to_global (proxy->protects);
rep_FREE_CELL ((char *)proxy);
}
static void
gobj_sweep (void)
{
sgtk_object_proxy *proxy = all_proxies;
all_proxies = 0;
while (proxy != 0)
{
sgtk_object_proxy *next = proxy->next;
if (! rep_GC_CELL_MARKEDP(rep_VAL(proxy)))
gobj_free (rep_VAL(proxy));
else
{
rep_GC_CLR_CELL (rep_VAL(proxy));
proxy->next = all_proxies;
all_proxies = proxy;
}
proxy = next;
}
}
/* Treating GObject proxies right during GC. We need to run custom
code during the mark phase of the Scheme GC. We do this by
creating a new smob type and allocating one actual smob of it.
This smob is made permanent and thus its marking function is
invoked for every GC. We hijack this function to do the tracing of
all existing proxies as well. */
static void
count_traced_ref (GObject *obj, void *data)
{
repv p = (repv)get_proxy (obj);
if (p != Qnil)
{
sgtk_object_proxy *proxy = GOBJ_PROXY (p);
#ifdef DEBUG_PRINT
fprintf (stderr, "counting %p %s\n",
proxy->obj, g_type_name (G_OBJECT_TYPE (proxy->obj)));
#endif
proxy->traced_refs++;
}
}
static void
gobj_marker_hook (void)
{
sgtk_object_proxy *proxy;
/* We do two passes here. The first pass counts how many references
an object has from other objects that have a proxy. The second
pass marks all objects that have more than this number of
references. For the first pass to work, we need to enumerate all
references that an object has to other objects. We can't do that
precisely without help from Gtk+ itself. But luckily, *not*
knowing about an `internal' reference is the conservative thing.
Missing a reference will make it appear to us that an object has
more `external' references to it than it really has, thus making
us keep the proxy alive. Only when these `external' references
form a cycle over some Scheme values, we loose. As a first
approximation to the true set of references of a GtkObject, we
just traverse its children with gtk_container_foreach. */
/* First pass. */
for (proxy = all_proxies; proxy; proxy = proxy->next)
{
GObject *obj = proxy->obj;
#ifdef DEBUG_PRINT
fprintf (stderr, "on %p %p\n", proxy, obj);
#endif
if (GTK_IS_CONTAINER (obj))
gtk_container_foreach (GTK_CONTAINER(obj),
(GtkCallback) count_traced_ref, NULL);
}
#ifdef DEBUG_PRINT
fprintf (stderr, "done with pass 1.\n");
#endif
/* Second pass. */
for (proxy = all_proxies; proxy; proxy = proxy->next)
{
if (proxy->obj->ref_count > proxy->traced_refs + 1)
{
#ifdef DEBUG_PRINT
fprintf (stderr, "hooking %p %s\n",
proxy->obj, g_type_name (G_OBJECT_TYPE (proxy->obj)));
#endif
/* mark the proxy itself */
rep_MARKVAL (rep_VAL (proxy));
}
/* always mark the protected objects, since they're moved to
the global_protects list if the object is freed */
sgtk_mark_protects (proxy->protects);
proxy->traced_refs = 0;
}
sgtk_mark_protects (global_protects);
}
/* Create a proxy for OBJ. */
static repv
make_gobj (GObject *obj)
{
sgtk_object_proxy *proxy;
g_assert (obj->ref_count > 0);
proxy = (sgtk_object_proxy *)rep_ALLOC_CELL (sizeof(sgtk_object_proxy));
if (GTK_IS_OBJECT (obj))
{
gtk_object_ref (GTK_OBJECT (obj));
gtk_object_sink (GTK_OBJECT (obj));
}
else
g_object_ref (obj); /* XXX ref may leak? */
#ifdef DEBUG_PRINT
fprintf (stderr, "New proxy %p for %p %s\n", proxy, obj,
g_type_name (G_OBJECT_TYPE (obj)));
#endif
proxy->obj = obj;
proxy->protects = NULL;
proxy->traced_refs = 0;
proxy->next = all_proxies;
all_proxies = proxy;
proxy->car = tc16_gobj;
enter_proxy (obj, rep_VAL(proxy));
return rep_VAL(proxy);
}
/* Return the proxy for OBJ if it already has one, else create a new
one. When OBJ is NULL, return `#f'. */
repv
sgtk_wrap_gobj (GObject *obj)
{
repv handle;
if (obj == NULL)
return Qnil;
handle = get_proxy (obj);
if (handle == Qnil)
handle = make_gobj (obj);
return handle;
}
int
sgtk_is_a_gobj (GType type, repv obj)
{
if (!GOBJP (obj) || !G_IS_OBJECT (GOBJ_PROXY(obj)->obj))
{
return 0;
}
return g_type_is_a (G_OBJECT_TYPE(GOBJ_PROXY(obj)->obj), type);
}
GObject*
sgtk_get_gobj (repv obj)
{
if (obj == Qnil)
return NULL;
else
return GOBJ_PROXY(obj)->obj;
}
/* compat */
repv sgtk_wrap_gtkobj (GtkObject *obj)
{
return sgtk_wrap_gobj (G_OBJECT (obj));
}
int sgtk_is_a_gtkobj (GType type, repv obj)
{
return sgtk_is_a_gobj (type, obj) && GTK_IS_OBJECT (GOBJ_PROXY (obj)->obj);
}
GtkObject * sgtk_get_gtkobj (repv obj)
{
return GTK_OBJECT (sgtk_get_gobj (obj));
}
/* Enums.
Enumerations are described by a `sgtk_enum_info' structure. That
structure contains a list of all literals and their respective
values. In Scheme, an enum element is represented by a symbol
whose name is the literal. */
int
sgtk_valid_enum (repv obj, sgtk_enum_info *info)
{
int i;
char *obj_name;
if (!rep_SYMBOLP (obj))
return 0;
obj_name = rep_STR(rep_SYM(obj)->name);
for (i = 0; i < info->n_literals; i++)
if (!strcmp (info->literals[i].name, obj_name))
return 1;
return 0;
}
repv
sgtk_enum_to_rep (gint val, sgtk_enum_info *info)
{
int i;
for (i = 0; i < info->n_literals; i++)
if (info->literals[i].value == val)
return Fintern (rep_string_dup(info->literals[i].name), Qnil);
#if 0
/* XXX */
SCM_ASSERT (0, SCM_MAKINUM (val), SCM_ARG1, "enum->symbol");
#endif
return Qnil;
}
gint
sgtk_rep_to_enum (repv obj, sgtk_enum_info *info)
{
int i;
char *obj_name = rep_STR(rep_SYM(obj)->name);
for (i = 0; i < info->n_literals; i++)
if (!strcmp (info->literals[i].name, obj_name))
return info->literals[i].value;
return -1;
}
/* Flags.
Like enums, flags are described by a `sgtk_enum_info' structure.
In Scheme, flags are represented by a list of symbols, one for each
bit that is set in the flags value. */
int
sgtk_valid_flags (repv obj, sgtk_enum_info *info)
{
while (obj != Qnil)
{
int i, valid;
repv sym;
char *sym_name;
if (!rep_CONSP (obj))
return 0;
sym = rep_CAR (obj);
if (!rep_SYMBOLP (sym))
return 0;
sym_name = rep_STR(rep_SYM(sym)->name);
for (i = 0, valid = 0; i < info->n_literals; i++)
if (!strcmp (info->literals[i].name, sym_name))
{
valid = 1;
break;
}
if (!valid)
return 0;
obj = rep_CDR (obj);
}
return 1;
}
repv
sgtk_flags_to_rep (gint val, sgtk_enum_info *info)
{
repv ans = Qnil;
int i;
for (i = 0; i < info->n_literals; i++)
if (val & info->literals[i].value)
{
ans = Fcons (Fintern (rep_string_dup(info->literals[i].name), Qnil),
ans);
val &= ~info->literals[i].value;
}
return ans;
}
gint
sgtk_rep_to_flags (repv obj, sgtk_enum_info *info)
{
int ans = 0;
while (rep_CONSP(obj) && !rep_INTERRUPTP)
{
int i;
repv sym = rep_CAR (obj);
char *sym_name = rep_STR(rep_SYM(sym)->name);
for (i = 0; i < info->n_literals; i++)
if (!strcmp (info->literals[i].name, sym_name))
{
ans |= info->literals[i].value;
break;
}
obj = rep_CDR (obj);
rep_TEST_INT;
}
return ans;
}
/* String enums.
A string enum is like an enum, but the values are strings. The
range of values can be extended, so anywhere a "string enum" value
is accepted, we also accept a string (but not a symbol). */
int
sgtk_valid_senum (repv obj, sgtk_senum_info *info)
{
int i;
char *obj_name;
if (rep_STRINGP (obj))
return 1;
if (! rep_SYMBOLP (obj))
return 0;
obj_name = rep_STR(rep_SYM(obj)->name);
for (i = 0; i < info->n_literals; i++)
if (! strcmp (info->literals[i].name, obj_name))
return 1;
return 0;
}
repv
sgtk_senum_to_rep (char *val, sgtk_senum_info *info)
{
int i;
for (i = 0; i < info->n_literals; i++)
if (! strcmp (info->literals[i].value, val))
return Fintern (rep_string_dup(info->literals[i].name), Qnil);
return rep_string_dup (val);
}
char *
sgtk_rep_to_senum (repv obj, sgtk_senum_info *info)
{
int i;
char *obj_name;
if (rep_STRINGP (obj))
return rep_STR (obj);
obj_name = rep_STR (rep_SYM (obj)->name);
for (i = 0; i < info->n_literals; i++)
if (! strcmp (info->literals[i].name, obj_name))
return info->literals[i].value;
return NULL;
}
/* Boxed Values.
I'm trying to use the same hash table approach as with the gobj's,
but without such complex gc tracing. I'm hoping that the `opaqueness'
of the boxed types preclude any internal pointers.. --jsh
*/
typedef struct _sgtk_boxed_proxy {
repv car;
struct _sgtk_boxed_proxy *next;
GType type;
gpointer ptr;
} sgtk_boxed_proxy;
static sgtk_boxed_proxy *all_boxed;
static long tc16_boxed;
#define BOXED_P(x) (rep_CELL16_TYPEP(x, tc16_boxed))
#define BOXED_PROXY(x) ((sgtk_boxed_proxy *)rep_PTR(x))
#define BOXED_TYPE(x) (BOXED_PROXY(x)->type)
#define BOXED_PTR(x) (BOXED_PROXY(x)->ptr)
#define BOXED_INFO(x) ((sgtk_boxed_info*)must_get_type_info(BOXED_TYPE(x)))
static void
boxed_free (repv obj)
{
sgtk_boxed_info *info = BOXED_INFO (obj);
info->destroy (BOXED_PTR (obj));
forget_proxy (BOXED_PTR (obj));
rep_FREE_CELL (rep_PTR(obj));
}
static void
boxed_print (repv stream, repv exp)
{
char buf[32];
sgtk_boxed_info *info = BOXED_INFO (exp);
rep_stream_puts (stream, "#<", -1, rep_FALSE);
rep_stream_puts (stream, info->header.name, -1, rep_FALSE);
rep_stream_putc (stream, ' ');
sprintf (buf, "%lx", (long)BOXED_PTR (exp));
rep_stream_puts (stream, buf, -1, rep_FALSE);
rep_stream_putc (stream, '>');
}
static void
boxed_sweep (void)
{
sgtk_boxed_proxy *proxy = all_boxed;
all_boxed = 0;
while (proxy != 0)
{
sgtk_boxed_proxy *next = proxy->next;
if (! rep_GC_CELL_MARKEDP(rep_VAL(proxy)))
boxed_free (rep_VAL(proxy));
else
{
rep_GC_CLR_CELL (rep_VAL(proxy));
proxy->next = all_boxed;
all_boxed = proxy;
}
proxy = next;
}
}
repv
sgtk_boxed_to_rep (gpointer ptr, sgtk_boxed_info *info, int copyp)
{
repv handle;
if (ptr == NULL)
return Qnil;
if (!sgtk_fillin_type_info (&info->header))
return Qnil;
handle = get_proxy (ptr);
if (handle == Qnil) {
/* Allocate a new proxy */
sgtk_boxed_proxy *p = rep_ALLOC_CELL (sizeof (sgtk_boxed_proxy));
if (copyp)
ptr = info->copy (ptr);
p->car = tc16_boxed;
p->next = all_boxed;
all_boxed = p;
p->type = info->header.type;
p->ptr = ptr;
handle = rep_VAL(p);
}
return handle;
}
void *
sgtk_rep_to_boxed (repv obj)
{
if (obj == Qnil)
return NULL;