-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathPerlXlib.c
2110 lines (1926 loc) · 116 KB
/
PerlXlib.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
#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"
#include "ppport.h"
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/extensions/XTest.h>
#ifdef HAVE_XRENDER
#include <X11/extensions/Xrender.h>
#endif
#include "PerlXlib.h"
#define OR_NULL PerlXlib_OR_NULL
#define OR_UNDEF PerlXlib_OR_UNDEF
#define OR_DIE PerlXlib_OR_DIE
#define AUTOCREATE PerlXlib_AUTOCREATE
static const char* T_DISPLAY= "Display";
static const char* OBJ_CACHE_NAME= "X11::Xlib::_obj_cache";
static struct PerlXlib_fields* PerlXlib_get_magic_fields(SV *sv, int create_flag);
/*-----------------------------------------------------------------------------------
* This struct is attached to each of the X11::Xlib objects that reference C structs.
*/
struct PerlXlib_fields {
SV *self; /* reference to the SV this struct is attached to */
SV *display_sv; /* optional reference to Display, no lifespan tracking */
void *ptr; /* struct/opaque pointer to something in xlib */
const char *ptr_type; /* static string identifying the type of the object */
int xfree_cleanup: 1; /* whether to call XFree(ptr) during destructor */
struct PerlXlib_fields *parent; /* Object whose ->ptr owns the lifespan of this ->ptr */
AV *dependents; /* weak references to X11::Xlib objects whose ptr depends on this object */
};
static void PerlXlib_fields_init(struct PerlXlib_fields *fields, SV *self) {
Zero(fields, 1, struct PerlXlib_fields);
fields->self= self;
}
/* Set the ->ptr field, but also update the cache of pointers-to-objects.
* type is optional, but useful for debugging.
* obj is the *inner* SV/HV/AV of the object not a RV pointing to it.
*/
static void PerlXlib_fields_set_ptr(struct PerlXlib_fields *fields, void *ptr, const char *type) {
HV *cache= NULL;
SV **ent, *sv;
if (fields->ptr == ptr)
return; /* nothing to do */
if (fields->ptr) { /* remove any previous object from cache */
cache= get_hv(OBJ_CACHE_NAME, GV_ADD);
hv_delete(cache, (void*) &fields->ptr, sizeof(void*), G_DISCARD);
}
fields->ptr= ptr;
fields->ptr_type= ptr? type : NULL;
fields->xfree_cleanup= 0;
if (fields->self && fields->ptr) {
if (!cache) cache= get_hv(OBJ_CACHE_NAME, GV_ADD);
sv= newRV_inc(fields->self); /* create new weak-ref to the object */
sv_rvweaken(sv);
ent= hv_store(cache, (void*) &fields->ptr, sizeof(void*), sv, 0);
if (!ent) {
sv_2mortal(sv);
croak("Can't cache X11 wrapper object into %s", OBJ_CACHE_NAME);
}
}
}
/* Set 'fields' as the parent of 'dep', adding a weak-ref to 'dep' in the dependents list.
* dep is the *inner* SV/AV/HV of the object, not a RV pointing to it.
*/
static void PerlXlib_fields_add_dependent(struct PerlXlib_fields *fields, SV *dep) {
int i;
SV **ent, *sv;
AV *deps= fields->dependents? fields->dependents : (fields->dependents= newAV());
struct PerlXlib_fields *dep_fields= PerlXlib_get_magic_fields(dep, AUTOCREATE);
if (dep_fields->parent)
croak("Dependent object already has a parent");
/* sanity check to avoid pointless refcnt management below */
if (SvMAGICAL(deps))
croak("bug");
/* The list contains weak references, and every now and then we should clean up any
* that got unset. Do this every time the list reaches a multiple of 8. */
if (!(av_len(deps) & 7)) {
for (i= av_len(deps); i >= 0; --i) {
ent= av_fetch(deps, i, 0);
if (ent && !SvROK(*ent)) {
sv= av_pop(deps);
if (i <= av_len(deps)) av_store(deps, i, sv);
else SvREFCNT_dec(sv);
}
}
}
av_push(deps, sv_rvweaken(newRV_inc(dep)));
dep_fields->parent= fields;
}
/* When the C-level object responsible for this object's C-level data gets freed,
* Set the pointers to NULL. This chains through all dependents.
* At the end, there are no more dependents.
*/
static void PerlXlib_fields_invalidate_dependents(struct PerlXlib_fields *fields) {
struct PerlXlib_fields *peer_fields;
int i;
SV **ent;
/* If other C-level objects depended on this one, their wrappers also need ->ptr set to NULL. */
if (fields->dependents) {
for (i= av_len(fields->dependents); i >= 0; --i) {
ent= av_fetch(fields->dependents, i, 0);
if (ent && SvROK(*ent)) {
if (peer_fields= PerlXlib_get_magic_fields(SvRV(*ent), 0)) {
if (peer_fields->xfree_cleanup)
warn("An object using XFree was incorrectly listed as a dependent on another object");
else {
peer_fields->parent= NULL;
if (peer_fields->ptr)
PerlXlib_fields_set_ptr(peer_fields, NULL, NULL);
if (peer_fields->dependents)
PerlXlib_fields_invalidate_dependents(peer_fields);
}
}
}
}
/* no more dependents */
av_clear(fields->dependents);
sv_2mortal((SV*)fields->dependents);
fields->dependents= NULL;
}
}
/* Called automatically when the magic-bearing object is freed */
static void PerlXlib_fields_free(struct PerlXlib_fields *fields) {
/* unset the ->ptr, which by extension removes the containing object from the object cache */
if (fields->ptr) {
if (fields->xfree_cleanup)
XFree(fields->ptr);
PerlXlib_fields_set_ptr(fields, NULL, NULL);
}
/* release the reference to the X11::Xlib instance if this object was holding one */
if (fields->display_sv) {
sv_2mortal(fields->display_sv);
fields->display_sv= NULL;
}
/* No need to tell the parent we are gone because the parent holds a weak-ref */
fields->parent= NULL;
/* tell dependent objects that they are no longer valid */
PerlXlib_fields_invalidate_dependents(fields);
fields->self= NULL;
Safefree(fields);
}
/*------------------------------------------------------------------------------------
* This defines the "Magic" that perl attaches to a scalar.
*/
static int PerlXlib_magic_free(pTHX_ SV* sv, MAGIC* mg) {
if (mg->mg_ptr)
PerlXlib_fields_free((struct PerlXlib_fields*) mg->mg_ptr);
return 0; // ignored anyway
}
#ifdef USE_ITHREADS
static int PerlXlib_magic_dup(pTHX_ MAGIC *mg, CLONE_PARAMS *param) {
croak("This object cannot be shared between threads");
return 0;
};
#else
#define PerlXlib_magic_dup 0
#endif
static MGVTBL PerlXlib_magic_vt= {
0, /* get */
0, /* write */
0, /* length */
0, /* clear */
PerlXlib_magic_free,
0, /* copy */
PerlXlib_magic_dup
#ifdef MGf_LOCAL
,0
#endif
};
/* Get existing magic fields or attach magic fields to the object.
* The sv should be the inner SV/HV/AV of the object, not an RV pointing to it.
* Use AUTOCREATE to attach magic if it was not present.
* Use NOTNULL for a built-in croak() if the return value would be NULL.
*/
static struct PerlXlib_fields* PerlXlib_get_magic_fields(SV *sv, int create_flag) {
MAGIC* magic;
struct PerlXlib_fields *fields;
if (SvMAGICAL(sv)) {
/* Iterate magic attached to this scalar, looking for one with our vtable */
for (magic= SvMAGIC(sv); magic; magic = magic->mg_moremagic)
if (magic->mg_type == PERL_MAGIC_ext && magic->mg_virtual == &PerlXlib_magic_vt)
/* If found, the mg_ptr points to the fields structure. */
return (struct PerlXlib_fields*) magic->mg_ptr;
}
if (create_flag == OR_DIE)
croak("Object lacks X11 magic");
if (create_flag == AUTOCREATE) {
Newx(fields, 1, struct PerlXlib_fields);
PerlXlib_fields_init(fields, sv);
magic= sv_magicext(sv, NULL, PERL_MAGIC_ext, &PerlXlib_magic_vt, (const char*) fields, 0);
#ifdef USE_ITHREADS
magic->mg_flags |= MGf_DUP;
#endif
return fields;
}
return NULL;
}
/*----------------------------------------------------------------------------------------
* Public API
*/
/* This gets a cached object known to wrap the C-level pointer 'thing'.
* If 'thing' is NULL, this always returns NULL regardless of create_flag.
* If one does not exist and 'create' is requested, this will create a new wrapper
* object of the given obj_svtype blessed as thing_class, and optionally listing
* it as a dependency of 'parent'.
* Returns a mortal reference to the object.
*/
extern SV * PerlXlib_get_objref(void *thing, int create_flag,
const char *thing_type, int obj_svtype, const char *thing_class, void *parent
) {
HV *cache, *pkg;
GV *build_method;
AV *isa;
SV **ent, *ret, *parent_objref;
struct PerlXlib_fields *f, *parent_fields;
if (thing) {
ent= hv_fetch(get_hv(OBJ_CACHE_NAME, GV_ADD), (void*) &thing, sizeof(thing), 0);
/* Return existing object? It's a weak-ref, so check that it still points to something. */
if (ent && SvROK(*ent))
/* create strong-ref from weakref */
return sv_mortalcopy(*ent);
}
if (create_flag == OR_NULL)
return NULL;
if (create_flag == OR_UNDEF || (create_flag == AUTOCREATE && !thing))
return &PL_sv_undef;
if (create_flag != AUTOCREATE)
croak("No such reference");
/* Doesn't exist. Create a new one. */
pkg= gv_stashpv(thing_class, GV_ADD);
if (obj_svtype == SVt_PVMG) {
/* return value is a new mortal RV pointing to a PV blessed as thing_class,
* and the PV points to thing.
*/
ret= sv_setref_pv(sv_newmortal(), thing_class, thing);
}
else if (obj_svtype == SVt_PVHV) {
/* return value is a new mortal RV pointing to a HV blessed as thing_class,
* and with "dpy_innerptr" magic attached holding the pointer to thing.
*/
ret= sv_2mortal(newRV_noinc((SV*) newHV()));
sv_bless(ret, pkg);
}
else if (obj_svtype == SVt_PVAV) {
/* return value is a new mortal RV pointing to a AV blessed as thing_class,
* and with "dpy_innerptr" magic attached holding the pointer to thing.
*/
ret= sv_2mortal(newRV_noinc((SV*) newAV()));
sv_bless(ret, pkg);
}
else
croak("Unsupported obj_svtype in PerlXlib_get_obj_for_ptr");
f= PerlXlib_get_magic_fields(SvRV(ret), AUTOCREATE);
PerlXlib_fields_set_ptr(f, thing, thing_type); /* adds weak-ref to cache */
/* If there is an owner, add this object to the owner's list */
if (parent) {
parent_objref= PerlXlib_get_objref(parent, OR_NULL, NULL, 0, NULL, NULL);
if (!parent_objref || !SvROK(parent_objref))
croak("No containing object for parent pointer %p", parent);
parent_fields= PerlXlib_get_magic_fields(SvRV(parent_objref), AUTOCREATE);
PerlXlib_fields_add_dependent(parent_fields, SvRV(ret));
/* If the parent is a Display, also reference it as the ->display attribute */
if (parent_fields->ptr_type == T_DISPLAY)
f->display_sv= newRV_inc(parent_fields->self);
}
/* Call the 'BUILD' method of the package, if any */
if ((build_method= gv_fetchmeth(pkg, "BUILD", 5, 0)) && GvCV(build_method)) {
dSP;
ENTER;
SAVETMPS;
PUSHMARK(SP);
EXTEND(SP, 1);
PUSHs(sv_mortalcopy(ret));
PUTBACK;
call_sv((SV*) GvCV(build_method), G_DISCARD);
FREETMPS;
LEAVE;
}
return ret;
}
/* If objref is an object with X11 magic attached, this retrieves the ->ptr from its fields.
*/
extern void* PerlXlib_objref_get_pointer(SV *objref, const char *ptr_type, int fail_flag) {
struct PerlXlib_fields *f= NULL;
if (sv_isobject(objref)) {
f= PerlXlib_get_magic_fields(SvRV(objref), OR_NULL);
if (f && f->ptr) {
if (ptr_type && !(f->ptr_type && 0 == strcmp(f->ptr_type, ptr_type)))
croak("Object pointer is %s (need %s)", f->ptr_type? f->ptr_type : "(unknown)", ptr_type);
return f->ptr;
}
if (fail_flag == OR_DIE)
croak("No Xlib pointer attached to this object");
}
/* OR_NULL permits things like an SV set to zero, or undef, but still rejects nonsense arguments */
else if ((fail_flag == OR_DIE) || !(!SvOK(objref) || looks_like_number(objref) && SvIV(objref) == 0))
croak("Not a reference to a %s", ptr_type);
return NULL;
}
extern void PerlXlib_objref_set_pointer(SV *objref, void *pointer, const char *ptr_type) {
struct PerlXlib_fields *f= NULL;
if (!sv_isobject(objref))
croak("Not an object");
f= PerlXlib_get_magic_fields(SvRV(objref), AUTOCREATE);
if (f->ptr_type && pointer) {
if (!ptr_type || 0 != strcmp(ptr_type, f->ptr_type))
croak("Cannot replace pointer with different type (%s != %s)", ptr_type? ptr_type : "NULL", f->ptr_type);
ptr_type= f->ptr_type; /* preserve T_DISPLAY special case */
} else if (ptr_type && 0 == strcmp(ptr_type, T_DISPLAY)) {
ptr_type= T_DISPLAY; /* T_DISPLAY special case, used as a flag */
}
PerlXlib_fields_set_ptr(f, pointer, ptr_type);
}
/* Same as PerlXlib_get_objref, but with a few special cases.
* When given a pointer and the create flag is false, this returns the pointer as an integer.
* This handles cases like returning the event->display field which might have been corrupted with
* any value, and prevents creating new X11::Xlib connections for those.
*/
extern SV * PerlXlib_get_display_objref(Display *dpy, int create_flag) {
SV *objref= PerlXlib_get_objref(dpy, create_flag == OR_UNDEF? OR_NULL : create_flag,
T_DISPLAY, SVt_PVHV, "X11::Xlib", NULL);
/* objref is either a strong&mortal reference to X11::Xlib, or NULL, or undef.
* For the NULL/undef case, just return the pointer as an integer. */
if (objref && SvOK(objref))
return objref;
if (create_flag == OR_DIE)
croak("No such display %p", dpy);
return dpy? sv_2mortal(newSVuv(PTR2UV(dpy)))
: create_flag == OR_NULL? NULL
: &PL_sv_undef;
}
/* Get the Display* pointer from an instance of X11::Xlib.
* This is the same as PerlXlib_objref_get_pointer but with improved diagnostics.
*/
extern Display* PerlXlib_display_objref_get_pointer(SV *displayref, int fail_flag) {
void *pointer= PerlXlib_objref_get_pointer(displayref, T_DISPLAY, 0);
if (!pointer && fail_flag == OR_DIE) {
if (SvTRUE(get_sv("X11::Xlib::_error_fatal_trapped", GV_ADD)))
croak("Cannot call further Xlib functions after fatal Xlib error");
if (!sv_derived_from(displayref, "X11::Xlib"))
croak("Invalid X11 connection parameter; must be instance of X11::Xlib");
/* has magic, but NULL pointer */
croak("X11 connection was closed");
}
return pointer;
}
/* Return the X11 Screen* pointer from a Perl X11::Xlib::Screen object.
* The ::Screen objects are a hashref with a normal reference to the ->{display},
* and a second field of ->{screen_number}. Use thes two values to call
* ScreenOfDisplay to get the Screen* pointer.
*/
extern Screen * PerlXlib_screen_objref_get_pointer(SV *sv, int fail_flag) {
HV *hv;
SV **elem;
Display *dpy;
int screennum;
if (!sv || !SvROK(sv) || !SvTYPE(SvRV(sv)) == SVt_PVHV) {
if (fail_flag == OR_DIE || (sv && SvOK(sv)))
croak("expected X11::Xlib::Screen object");
return NULL;
}
hv= (HV*) SvRV(sv);
elem= hv_fetch(hv, "display", 7, 0);
if (!elem || !(dpy= PerlXlib_display_objref_get_pointer(*elem, OR_NULL)))
croak("missing $screen->{display}");
elem= hv_fetch(hv, "screen_number", 13, 0);
if (!elem || !SvIOK(*elem))
croak("missing $screen->{screen_number}");
screennum= SvIV(*elem);
if (screennum >= ScreenCount(dpy) || screennum < 0)
croak("Screen number %d out of bounds for this display (0..%d)",
screennum, ScreenCount(dpy)-1);
return ScreenOfDisplay(dpy, screennum);
}
/* Given a Xlib Screen* pointer, find the X11::Xlib::Screen object for it.
* This is done by getting the X11::Xlib instance for the screen's Display pointer,
* and then getting the screen object from the X11::Xlib object.
*/
extern SV * PerlXlib_get_screen_objref(Screen *screen, int create_flag) {
Display *dpy= NULL;
SV *dpy_sv= NULL, *ret= NULL;
int i;
if (!screen) {
if (create_flag == OR_DIE) croak("NULL Screen pointer");
if (create_flag == OR_UNDEF || create_flag == AUTOCREATE) return &PL_sv_undef;
return NULL;
}
/* from here on, it's basically AUTOCREATE because screen objects aren't cached by pointer */
dpy= DisplayOfScreen(screen);
dpy_sv= PerlXlib_get_display_objref(dpy, OR_DIE);
/* There's actually no way to get the screen number from the pointer,
* other than subtraction on private pointers... so just iterate.
* There's probably only one anyway. */
for (i= ScreenCount(dpy) - 1; i >= 0; --i)
if (screen == ScreenOfDisplay(dpy, i))
break;
if (i < 0)
croak("Corrupt Xlib screen/display structures!");
/* Then call $display->screen(i) method */
dSP;
ENTER;
SAVETMPS;
PUSHMARK(SP);
EXTEND(SP, 2);
PUSHs(sv_mortalcopy(dpy_sv));
PUSHs(sv_2mortal(newSViv(i)));
PUTBACK;
if (call_method("screen", G_SCALAR) != 1)
croak("stack assertion failed");
SPAGAIN;
ret= POPs;
SvREFCNT_inc(ret); /* make sure it lives a little longer */
PUTBACK;
FREETMPS;
LEAVE;
sv_2mortal(ret);
return ret;
}
/* Read-accessor for the $obj->display attribute */
extern SV * PerlXlib_objref_get_display(SV *objref) {
struct PerlXlib_fields *f;
if (!sv_isobject(objref))
croak("Not an object - can't read attribute of %s", SvPV_nolen(objref));
f= PerlXlib_get_magic_fields(SvRV(objref), OR_NULL);
return (f && f->ptr_type == T_DISPLAY)? objref /* X11::Xlib is its own ->display attribute */
: (f && f->display_sv && sv_isobject(f->display_sv))? f->display_sv
: &PL_sv_undef;
}
/* Write-accessor for the $obj->display attribute */
extern void PerlXlib_objref_set_display(SV *objref, SV *dpy_sv) {
struct PerlXlib_fields *f;
if (!sv_isobject(objref))
croak("Not an object");
f= PerlXlib_get_magic_fields(SvRV(objref), AUTOCREATE);
if (dpy_sv && sv_isobject(dpy_sv)) { /* being assigned */
if (f->display_sv) sv_setsv(f->display_sv, dpy_sv);
else f->display_sv= newSVsv(dpy_sv);
}
else if (f->display_sv) { /* being unset */
sv_2mortal(f->display_sv);
f->display_sv= NULL;
}
}
/* Allow unsigned integer, or hashref with field ->{xid} */
XID PerlXlib_sv_to_xid(SV *sv) {
SV **xid_field;
if (SvUOK(sv) || SvIOK(sv))
return (XID) SvUV(sv);
if (!SvROK(sv) || !(SvTYPE(SvRV(sv)) == SVt_PVHV)
|| !(xid_field= hv_fetch((HV*)SvRV(sv), "xid", 3, 0))
|| !*xid_field || !(SvIOK(*xid_field) || SvUOK(*xid_field)))
croak("Invalid XID (Window, etc); must be an unsigned int, or an instance of X11::Xlib::XID");
return (XID) SvUV(*xid_field);
}
/* Inspect each of the perl data structures that are directly manipulated by XS.
* This is only for debugging.
*/
void PerlXlib_sanity_check_data_structures() {
HV *dpys, *obj_cache, *display_attr;
HE *dpy_he, *obj_he;
SV *dpy_sv, *obj_sv, **elem;
Display *dpy;
void *opaque;
dpys= get_hv("X11::Xlib::_connections", GV_ADD);
/*hv_assert(dpys);*/
display_attr= get_hv("X11::Xlib::_display_attr", GV_ADD);
/*hv_assert(display_attr);*/
/* for each display */
for (hv_iterinit(dpys); (dpy_he= hv_iternext(dpys)); ) {
dpy_sv= hv_iterval(dpys, dpy_he);
/* SV refcnt should be exactly 1, and should be weakref. Ref'd object should have >1 refcnt */
if (SvREFCNT(dpy_sv) != 1) croak("Refcnt of %%_connections member is %d", SvREFCNT(dpy_sv));
if (!SvROK(dpy_sv) || !SvWEAKREF(dpy_sv)) croak("%%_connections member is not a weakref");
if (!sv_derived_from(dpy_sv, "X11::Xlib")) croak("%%_connections contains non-X11::Xlib object");
dpy= PerlXlib_display_objref_get_pointer(dpy_sv, OR_DIE);
/* Check each of the objects in the $dpy->{_obj_cache} */
elem= hv_fetch((HV*)SvRV(dpy_sv), "_obj_cache", 10, 0);
if (elem) {
if (!*elem || !SvROK(*elem) || SvTYPE(SvRV(*elem)) != SVt_PVHV)
croak("Display contains invalid _obj_cache");
if (SvREFCNT(*elem) != 1 || SvREFCNT(SvRV(*elem)) != 1)
croak("_obj_cache has wrong refcnt");
obj_cache= (HV*) SvRV(*elem);
/*hv_assert(obj_cache);*/
for (hv_iterinit(obj_cache); (obj_he= hv_iternext(dpys)); ) {
obj_sv= hv_iterval(obj_cache, obj_he);
if (SvREFCNT(obj_sv) != 1) croak("Refcnt of _obj_cache member is %d", SvREFCNT(obj_sv));
if (!SvROK(obj_sv) || !SvWEAKREF(obj_sv)) croak("_obj_cache member is not a weakref");
if (!sv_derived_from(obj_sv, "X11::Xlib::Opaque")) croak("_obj_cache member is not a X11::Xlib::Opaque");
opaque= (SvTYPE(SvRV(obj_sv)) <= SVt_PVMG)? INT2PTR(void*, SvIV(SvRV(obj_sv)))
: PerlXlib_objref_get_pointer(obj_sv, NULL, OR_DIE);
/* the pointer should have a ->display attribute attached */
elem= hv_fetch(display_attr, (void*) &opaque, sizeof(void*), 0);
if (!elem || !*elem || !SvROK(*elem))
croak("Missing or invalid _display_attr{} reference");
if (SvREFCNT(*elem) != 1 || SvWEAKREF(*elem))
croak("_display_attr ref is not strongref with refcnt==1");
if (SvRV(dpy_sv) != SvRV(*elem))
croak("_display_attr points to wrong dpy_sv");
}
}
}
}
/* Xlib warns that some structs might change size, and provide "XAllocFoo"
* functions. However, this only solves the case of Xlib access violations
* from an old perl module on a new Xlib. New perl modules on old Xlib would
* still write beyond the buffer (on the perl side) and corrupt memory.
* Annoyingly, Xlib doesn't seem to have any way to query the size of the struct,
* only allocate it.
* Instead of using XAllocFoo sillyness (and the memory management hassle it
* would cause), just pad the struct with some extra bytes.
* Perl modules will probably always be compiled fresh anyway.
*/
#ifndef X11_Xlib_Struct_Padding
#define X11_Xlib_Struct_Padding 64
#endif
/* Coercions allowed for RValue:
* foo( "buffer_of_the_correct_length_or_more" );
* foo( \"ref_to_buffer_of_the_correct_length_or_more" );
* foo( \%hashref_of_fields );
* foo( bless(\"buffer_of_correct_length_or_more", "pkg_or_subclass") )
* Coercions allowed for LValue:
* foo( my $x= undef );
* foo( "buffer_of_correct_length_or_more" );
* foo( \(my $x= undef) );
* foo( \"buffer_of_correct_length_or_more" );
* foo( bless(\"buffer_of_correct_length_or_more", "any_struct_class") )
*/
void* PerlXlib_get_struct_ptr(SV *sv, int lvalue, const char* pkg, int struct_size, PerlXlib_struct_pack_fn *packer) {
SV *tmp, *refsv= NULL;
char* buf;
size_t n;
if (SvROK(sv)) {
refsv= sv;
sv= SvRV(sv);
/* Follow scalar refs, to get to the buffer of a blessed object */
if (SvTYPE(sv) == SVt_PVMG) {
/* If it is a blessed object, ensure the type matches */
if (sv_isobject(refsv) && !sv_isa(refsv, pkg)) {
if (!sv_derived_from(refsv, lvalue? "X11::Xlib::Struct" : pkg)) {
buf= SvPV(refsv, n);
croak("Can't coerce %.*s to %s %s", (int) n, buf, pkg, lvalue? "lvalue":"rvalue");
}
}
}
/* Also accept a hashref, which we pass to "pack" */
else if (SvTYPE(sv) == SVt_PVHV) {
if (lvalue) croak("Can't coerce hashref to %s lvalue", pkg);
/* Need a buffer that lasts for the rest of our XS call stack.
* Cheat by using a mortal SV :-)
*/
tmp= sv_2mortal(newSV(struct_size + X11_Xlib_Struct_Padding));
buf= SvPVX(tmp);
memset(buf, 0, struct_size);
packer(buf, (HV*) sv, 0);
return buf;
}
else if (SvTYPE(sv) >= SVt_PVAV) { /* not a scalar */
buf= SvPV(refsv, n);
croak("Can't coerce %.*s to %s %s", (int) n, buf, pkg, lvalue? "lvalue":"rvalue");
}
}
/* If uninitialized, initialize to a blessed struct object,
* unless we are looking at \undef in which case just initialize to a string
*/
if (!SvOK(sv)) {
if (!lvalue) croak("Can't coerce %sundef to %s rvalue", refsv? "\\" : "", pkg);
if (!refsv) {
refsv= sv, sv= newSVrv(sv, pkg);
/* sv is now the referenced scalar, which is undef, and gets inflated next */
}
sv_setpvn(sv, "", 0);
SvGROW(sv, struct_size+X11_Xlib_Struct_Padding);
SvCUR_set(sv, struct_size);
memset(SvPVX(sv), 0, struct_size+1);
}
else if (!SvPOK(sv))
croak("Paramters requiring %s can only be coerced from string, string ref, hashref, or undef", pkg);
else if (SvCUR(sv) < struct_size)
croak("Scalars used as %s must be at least length %d (got %d)", pkg, (int) struct_size, (int) SvCUR(sv));
/* Make sure we have the padding even if the user tinkered with the buffer */
SvPV_force(sv, n);
SvGROW(sv, struct_size+X11_Xlib_Struct_Padding);
return SvPVX(sv);
}
#include "keysym_to_codepoint.c"
KeySym PerlXlib_codepoint_to_keysym(int uc) {
/* Latin-1 is identical */
if ((uc >= 0x0020 && uc <= 0x007E) || (uc >= 0x00A0 && uc <= 0x00FF))
return uc;
/* Unicode in range 0..0xFFFFFF can be stored directly */
if ((uc & 0xFFFFFF) == uc)
return 0x1000000 | uc;
return NoSymbol;
}
SV * PerlXlib_keysym_to_sv(KeySym sym, int symbolic) {
int sym_codepoint;
const char *symname;
if (sym == NoSymbol)
return &PL_sv_undef;
/* Only convert to Unicode character if reverse mapping matches forward mapping */
if (symbolic >= 2
&& (sym_codepoint= PerlXlib_keysym_to_codepoint(sym)) >= 0
&& (PerlXlib_codepoint_to_keysym(sym_codepoint) == sym))
return newSVpvf("%c", sym_codepoint);
/* Fall back to symbol name, but ensure reverse mapping here, as well */
else if (symbolic >= 1
&& sym > 0
&& (symname= XKeysymToString(sym))
&& (XStringToKeysym(symname) == sym))
return newSVpv(symname, 0);
/* Else just use the symbol ID */
else if (!symbolic || sym > 9)
return newSViv(sym);
/* Else it's ambiguous! Can't be loaded symbolicly. */
else
return NULL;
}
KeySym PerlXlib_sv_to_keysym(SV *sv) {
size_t len;
long ival, codepoint;
KeySym sym;
char *name, *endp;
if (!sv || !SvOK(sv))
return NoSymbol;
/* First try to process it as an X11 symbol name */
name= SvPV(sv, len);
sym= XStringToKeysym(name);
/* Else, use multi-digit numbers directly, and single-digit as a char lookup */
if (sym == NoSymbol) {
if (SvIOK(sv) && SvIV(sv) > 9)
sym= SvIV(sv);
else if ((ival= strtol(name, &endp, 0)) && (endp - name > 1) && *endp == 0)
sym= ival;
/* If it is a single character, try looking up a keysym for it */
else if ((DO_UTF8(sv)? sv_len_utf8(sv) : len) == 1) {
codepoint= NATIVE_TO_UNI(DO_UTF8(sv)? utf8n_to_uvchr(name, len, &len, 0) : (name[0] & 0xFF));
sym= PerlXlib_codepoint_to_keysym(codepoint);
}
}
return sym;
}
int PerlXlib_X_error_handler(Display *d, XErrorEvent *e) {
dSP;
ENTER;
SAVETMPS;
PUSHMARK(SP);
EXTEND(SP, 1);
PUSHs(sv_2mortal(sv_setref_pvn(newSV(0), "X11::Xlib::XErrorEvent", (void*) e, sizeof(XEvent))));
PUTBACK;
call_pv("X11::Xlib::_error_nonfatal", G_VOID|G_DISCARD|G_EVAL|G_KEEPERR);
FREETMPS;
LEAVE;
return 0;
}
/*
What a mess. So Xlib has a stupid design where they forcibly abort the
program when an I/O error occurs and the X server is lost. Even if you
install the error handler, they expect you to abort the program and they
do it for you if you return. Furthermore, they tell you that you may not
call any more Xlib functions at all.
Luckily we can cheat with croak (longjmp) back out of the callback and
avoid the forced program exit. However now we can't officially use Xlib
again for the duration of the program, and there could be lost resources
from our longjmp. So, set a global flag to prevent any re-entry into XLib.
*/
int PerlXlib_X_IO_error_handler(Display *d) {
sv_setiv(get_sv("X11::Xlib::_error_fatal_trapped", GV_ADD), 1);
warn("Xlib fatal error. Further calls to Xlib are forbidden.");
dSP;
ENTER;
SAVETMPS;
PUSHMARK(SP);
EXTEND(SP, 1);
PUSHs(PerlXlib_get_display_objref(d, OR_UNDEF));
PUTBACK;
call_pv("X11::Xlib::_error_fatal", G_VOID|G_DISCARD|G_EVAL|G_KEEPERR);
FREETMPS;
LEAVE;
croak("Fatal X11 I/O Error"); /* longjmp past Xlib, which wants to kill us */
return 0; /* never reached. Make compiler happy. */
}
/* Install the Xlib error handlers, only if they have not already been installed.
* Use perl scalars to store this status, to avoid threading issues and to
* give users potential to inspect.
*/
void PerlXlib_install_error_handlers(Bool nonfatal, Bool fatal) {
SV *nonfatal_installed= get_sv("X11::Xlib::_error_nonfatal_installed", GV_ADD);
SV *fatal_installed= get_sv("X11::Xlib::_error_fatal_installed", GV_ADD);
if (nonfatal && !SvTRUE(nonfatal_installed)) {
XSetErrorHandler(&PerlXlib_X_error_handler);
sv_setiv(nonfatal_installed, 1);
}
if (fatal && !SvTRUE(fatal_installed)) {
XSetIOErrorHandler(&PerlXlib_X_IO_error_handler);
sv_setiv(fatal_installed, 1);
}
}
/*--------------------------------------------------------------------------*/
/* BEGIN GENERATED X11_Xlib_XEvent */
const char* PerlXlib_xevent_pkg_for_type(int type) {
switch (type) {
case 0: return "X11::Xlib::XErrorEvent";
case ButtonPress: return "X11::Xlib::XButtonEvent";
case ButtonRelease: return "X11::Xlib::XButtonEvent";
case CirculateNotify: return "X11::Xlib::XCirculateEvent";
case CirculateRequest: return "X11::Xlib::XCirculateRequestEvent";
case ClientMessage: return "X11::Xlib::XClientMessageEvent";
case ColormapNotify: return "X11::Xlib::XColormapEvent";
case ConfigureNotify: return "X11::Xlib::XConfigureEvent";
case ConfigureRequest: return "X11::Xlib::XConfigureRequestEvent";
case CreateNotify: return "X11::Xlib::XCreateWindowEvent";
case DestroyNotify: return "X11::Xlib::XDestroyWindowEvent";
case EnterNotify: return "X11::Xlib::XCrossingEvent";
case Expose: return "X11::Xlib::XExposeEvent";
case FocusIn: return "X11::Xlib::XFocusChangeEvent";
case FocusOut: return "X11::Xlib::XFocusChangeEvent";
case GenericEvent: return "X11::Xlib::XGenericEvent";
case GraphicsExpose: return "X11::Xlib::XGraphicsExposeEvent";
case GravityNotify: return "X11::Xlib::XGravityEvent";
case KeyPress: return "X11::Xlib::XKeyEvent";
case KeyRelease: return "X11::Xlib::XKeyEvent";
case KeymapNotify: return "X11::Xlib::XKeymapEvent";
case LeaveNotify: return "X11::Xlib::XCrossingEvent";
case MapNotify: return "X11::Xlib::XMapEvent";
case MapRequest: return "X11::Xlib::XMapRequestEvent";
case MappingNotify: return "X11::Xlib::XMappingEvent";
case MotionNotify: return "X11::Xlib::XMotionEvent";
case NoExpose: return "X11::Xlib::XNoExposeEvent";
case PropertyNotify: return "X11::Xlib::XPropertyEvent";
case ReparentNotify: return "X11::Xlib::XReparentEvent";
case ResizeRequest: return "X11::Xlib::XResizeRequestEvent";
case SelectionClear: return "X11::Xlib::XSelectionClearEvent";
case SelectionNotify: return "X11::Xlib::XSelectionEvent";
case SelectionRequest: return "X11::Xlib::XSelectionRequestEvent";
case UnmapNotify: return "X11::Xlib::XUnmapEvent";
case VisibilityNotify: return "X11::Xlib::XVisibilityEvent";
default: return "X11::Xlib::XEvent";
}
}
/* First, pack type, then pack fields for XAnyEvent, then any fields known for that type */
void PerlXlib_XEvent_pack(XEvent *s, HV *fields, Bool consume) {
SV **fp;
int newtype;
const char *oldpkg, *newpkg;
/* Type gets special handling */
fp= hv_fetch(fields, "type", 4, 0);
if (fp && *fp) {
newtype= SvIV(*fp);
if (s->type != newtype) {
oldpkg= PerlXlib_xevent_pkg_for_type(s->type);
newpkg= PerlXlib_xevent_pkg_for_type(newtype);
s->type= newtype;
if (oldpkg != newpkg) {
/* re-initialize all fields in the area that changed */
memset( ((char*)(void*)s) + sizeof(XAnyEvent), 0, sizeof(XEvent)-sizeof(XAnyEvent) );
}
}
if (consume) hv_delete(fields, "type", 4, G_DISCARD);
}
if (s->type) {
fp= hv_fetch(fields, "display", 7, 0);
if (fp && *fp) { s->xany.display= PerlXlib_display_objref_get_pointer(*fp, PerlXlib_OR_NULL);; if (consume) hv_delete(fields, "display", 7, G_DISCARD); }
fp= hv_fetch(fields, "send_event", 10, 0);
if (fp && *fp) { s->xany.send_event= SvIV(*fp);; if (consume) hv_delete(fields, "send_event", 10, G_DISCARD); }
fp= hv_fetch(fields, "serial", 6, 0);
if (fp && *fp) { s->xany.serial= SvUV(*fp);; if (consume) hv_delete(fields, "serial", 6, G_DISCARD); }
fp= hv_fetch(fields, "type", 4, 0);
if (fp && *fp) { s->xany.type= SvIV(*fp);; if (consume) hv_delete(fields, "type", 4, G_DISCARD); }
}
else {
fp= hv_fetch(fields, "serial", 6, 0);
if (fp && *fp) { s->xerror.serial= SvUV(*fp);; if (consume) hv_delete(fields, "serial", 6, G_DISCARD); }
fp= hv_fetch(fields, "display", 7, 0);
if (fp && *fp) { s->xerror.display= PerlXlib_display_objref_get_pointer(*fp, PerlXlib_OR_NULL);; if (consume) hv_delete(fields, "display", 7, G_DISCARD); }
}
switch( s->type ) {
case ButtonPress:
case ButtonRelease:
fp= hv_fetch(fields, "button", 6, 0);
if (fp && *fp) { s->xbutton.button= SvUV(*fp);; if (consume) hv_delete(fields, "button", 6, G_DISCARD); }
fp= hv_fetch(fields, "root", 4, 0);
if (fp && *fp) { s->xbutton.root= PerlXlib_sv_to_xid(*fp);; if (consume) hv_delete(fields, "root", 4, G_DISCARD); }
fp= hv_fetch(fields, "same_screen", 11, 0);
if (fp && *fp) { s->xbutton.same_screen= SvIV(*fp);; if (consume) hv_delete(fields, "same_screen", 11, G_DISCARD); }
fp= hv_fetch(fields, "state", 5, 0);
if (fp && *fp) { s->xbutton.state= SvUV(*fp);; if (consume) hv_delete(fields, "state", 5, G_DISCARD); }
fp= hv_fetch(fields, "subwindow", 9, 0);
if (fp && *fp) { s->xbutton.subwindow= PerlXlib_sv_to_xid(*fp);; if (consume) hv_delete(fields, "subwindow", 9, G_DISCARD); }
fp= hv_fetch(fields, "time", 4, 0);
if (fp && *fp) { s->xbutton.time= SvUV(*fp);; if (consume) hv_delete(fields, "time", 4, G_DISCARD); }
fp= hv_fetch(fields, "window", 6, 0);
if (fp && *fp) { s->xbutton.window= PerlXlib_sv_to_xid(*fp);; if (consume) hv_delete(fields, "window", 6, G_DISCARD); }
fp= hv_fetch(fields, "x", 1, 0);
if (fp && *fp) { s->xbutton.x= SvIV(*fp);; if (consume) hv_delete(fields, "x", 1, G_DISCARD); }
fp= hv_fetch(fields, "x_root", 6, 0);
if (fp && *fp) { s->xbutton.x_root= SvIV(*fp);; if (consume) hv_delete(fields, "x_root", 6, G_DISCARD); }
fp= hv_fetch(fields, "y", 1, 0);
if (fp && *fp) { s->xbutton.y= SvIV(*fp);; if (consume) hv_delete(fields, "y", 1, G_DISCARD); }
fp= hv_fetch(fields, "y_root", 6, 0);
if (fp && *fp) { s->xbutton.y_root= SvIV(*fp);; if (consume) hv_delete(fields, "y_root", 6, G_DISCARD); }
break;
case CirculateNotify:
fp= hv_fetch(fields, "event", 5, 0);
if (fp && *fp) { s->xcirculate.event= PerlXlib_sv_to_xid(*fp);; if (consume) hv_delete(fields, "event", 5, G_DISCARD); }
fp= hv_fetch(fields, "place", 5, 0);
if (fp && *fp) { s->xcirculate.place= SvIV(*fp);; if (consume) hv_delete(fields, "place", 5, G_DISCARD); }
fp= hv_fetch(fields, "window", 6, 0);
if (fp && *fp) { s->xcirculate.window= PerlXlib_sv_to_xid(*fp);; if (consume) hv_delete(fields, "window", 6, G_DISCARD); }
break;
case CirculateRequest:
fp= hv_fetch(fields, "parent", 6, 0);
if (fp && *fp) { s->xcirculaterequest.parent= PerlXlib_sv_to_xid(*fp);; if (consume) hv_delete(fields, "parent", 6, G_DISCARD); }
fp= hv_fetch(fields, "place", 5, 0);
if (fp && *fp) { s->xcirculaterequest.place= SvIV(*fp);; if (consume) hv_delete(fields, "place", 5, G_DISCARD); }
fp= hv_fetch(fields, "window", 6, 0);
if (fp && *fp) { s->xcirculaterequest.window= PerlXlib_sv_to_xid(*fp);; if (consume) hv_delete(fields, "window", 6, G_DISCARD); }
break;
case ClientMessage:
fp= hv_fetch(fields, "b", 1, 0);
if (fp && *fp) { { if (!SvPOK(*fp) || SvCUR(*fp) != sizeof(char)*20) croak("Expected scalar of length %ld but got %ld", (long)(sizeof(char)*20), (long) SvCUR(*fp)); memcpy(s->xclient.data.b, SvPVX(*fp), sizeof(char)*20);}; if (consume) hv_delete(fields, "b", 1, G_DISCARD); }
fp= hv_fetch(fields, "l", 1, 0);
if (fp && *fp) { { if (!SvPOK(*fp) || SvCUR(*fp) != sizeof(long)*5) croak("Expected scalar of length %ld but got %ld", (long)(sizeof(long)*5), (long) SvCUR(*fp)); memcpy(s->xclient.data.l, SvPVX(*fp), sizeof(long)*5);}; if (consume) hv_delete(fields, "l", 1, G_DISCARD); }
fp= hv_fetch(fields, "s", 1, 0);
if (fp && *fp) { { if (!SvPOK(*fp) || SvCUR(*fp) != sizeof(short)*10) croak("Expected scalar of length %ld but got %ld", (long)(sizeof(short)*10), (long) SvCUR(*fp)); memcpy(s->xclient.data.s, SvPVX(*fp), sizeof(short)*10);}; if (consume) hv_delete(fields, "s", 1, G_DISCARD); }
fp= hv_fetch(fields, "format", 6, 0);
if (fp && *fp) { s->xclient.format= SvIV(*fp);; if (consume) hv_delete(fields, "format", 6, G_DISCARD); }
fp= hv_fetch(fields, "message_type", 12, 0);
if (fp && *fp) { s->xclient.message_type= PerlXlib_sv_to_xid(*fp);; if (consume) hv_delete(fields, "message_type", 12, G_DISCARD); }
fp= hv_fetch(fields, "window", 6, 0);
if (fp && *fp) { s->xclient.window= PerlXlib_sv_to_xid(*fp);; if (consume) hv_delete(fields, "window", 6, G_DISCARD); }
break;
case ColormapNotify:
fp= hv_fetch(fields, "colormap", 8, 0);
if (fp && *fp) { s->xcolormap.colormap= PerlXlib_sv_to_xid(*fp);; if (consume) hv_delete(fields, "colormap", 8, G_DISCARD); }
fp= hv_fetch(fields, "new", 3, 0);
if (fp && *fp) { s->xcolormap.new= SvIV(*fp);; if (consume) hv_delete(fields, "new", 3, G_DISCARD); }
fp= hv_fetch(fields, "state", 5, 0);
if (fp && *fp) { s->xcolormap.state= SvIV(*fp);; if (consume) hv_delete(fields, "state", 5, G_DISCARD); }
fp= hv_fetch(fields, "window", 6, 0);
if (fp && *fp) { s->xcolormap.window= PerlXlib_sv_to_xid(*fp);; if (consume) hv_delete(fields, "window", 6, G_DISCARD); }
break;
case ConfigureNotify:
fp= hv_fetch(fields, "above", 5, 0);
if (fp && *fp) { s->xconfigure.above= PerlXlib_sv_to_xid(*fp);; if (consume) hv_delete(fields, "above", 5, G_DISCARD); }
fp= hv_fetch(fields, "border_width", 12, 0);
if (fp && *fp) { s->xconfigure.border_width= SvIV(*fp);; if (consume) hv_delete(fields, "border_width", 12, G_DISCARD); }
fp= hv_fetch(fields, "event", 5, 0);
if (fp && *fp) { s->xconfigure.event= PerlXlib_sv_to_xid(*fp);; if (consume) hv_delete(fields, "event", 5, G_DISCARD); }
fp= hv_fetch(fields, "height", 6, 0);
if (fp && *fp) { s->xconfigure.height= SvIV(*fp);; if (consume) hv_delete(fields, "height", 6, G_DISCARD); }
fp= hv_fetch(fields, "override_redirect", 17, 0);
if (fp && *fp) { s->xconfigure.override_redirect= SvIV(*fp);; if (consume) hv_delete(fields, "override_redirect", 17, G_DISCARD); }
fp= hv_fetch(fields, "width", 5, 0);
if (fp && *fp) { s->xconfigure.width= SvIV(*fp);; if (consume) hv_delete(fields, "width", 5, G_DISCARD); }
fp= hv_fetch(fields, "window", 6, 0);
if (fp && *fp) { s->xconfigure.window= PerlXlib_sv_to_xid(*fp);; if (consume) hv_delete(fields, "window", 6, G_DISCARD); }
fp= hv_fetch(fields, "x", 1, 0);
if (fp && *fp) { s->xconfigure.x= SvIV(*fp);; if (consume) hv_delete(fields, "x", 1, G_DISCARD); }
fp= hv_fetch(fields, "y", 1, 0);
if (fp && *fp) { s->xconfigure.y= SvIV(*fp);; if (consume) hv_delete(fields, "y", 1, G_DISCARD); }
break;
case ConfigureRequest:
fp= hv_fetch(fields, "above", 5, 0);
if (fp && *fp) { s->xconfigurerequest.above= PerlXlib_sv_to_xid(*fp);; if (consume) hv_delete(fields, "above", 5, G_DISCARD); }
fp= hv_fetch(fields, "border_width", 12, 0);
if (fp && *fp) { s->xconfigurerequest.border_width= SvIV(*fp);; if (consume) hv_delete(fields, "border_width", 12, G_DISCARD); }
fp= hv_fetch(fields, "detail", 6, 0);
if (fp && *fp) { s->xconfigurerequest.detail= SvIV(*fp);; if (consume) hv_delete(fields, "detail", 6, G_DISCARD); }
fp= hv_fetch(fields, "height", 6, 0);
if (fp && *fp) { s->xconfigurerequest.height= SvIV(*fp);; if (consume) hv_delete(fields, "height", 6, G_DISCARD); }
fp= hv_fetch(fields, "parent", 6, 0);
if (fp && *fp) { s->xconfigurerequest.parent= PerlXlib_sv_to_xid(*fp);; if (consume) hv_delete(fields, "parent", 6, G_DISCARD); }
fp= hv_fetch(fields, "value_mask", 10, 0);
if (fp && *fp) { s->xconfigurerequest.value_mask= SvUV(*fp);; if (consume) hv_delete(fields, "value_mask", 10, G_DISCARD); }
fp= hv_fetch(fields, "width", 5, 0);
if (fp && *fp) { s->xconfigurerequest.width= SvIV(*fp);; if (consume) hv_delete(fields, "width", 5, G_DISCARD); }
fp= hv_fetch(fields, "window", 6, 0);
if (fp && *fp) { s->xconfigurerequest.window= PerlXlib_sv_to_xid(*fp);; if (consume) hv_delete(fields, "window", 6, G_DISCARD); }
fp= hv_fetch(fields, "x", 1, 0);
if (fp && *fp) { s->xconfigurerequest.x= SvIV(*fp);; if (consume) hv_delete(fields, "x", 1, G_DISCARD); }
fp= hv_fetch(fields, "y", 1, 0);
if (fp && *fp) { s->xconfigurerequest.y= SvIV(*fp);; if (consume) hv_delete(fields, "y", 1, G_DISCARD); }
break;
case CreateNotify:
fp= hv_fetch(fields, "border_width", 12, 0);
if (fp && *fp) { s->xcreatewindow.border_width= SvIV(*fp);; if (consume) hv_delete(fields, "border_width", 12, G_DISCARD); }
fp= hv_fetch(fields, "height", 6, 0);
if (fp && *fp) { s->xcreatewindow.height= SvIV(*fp);; if (consume) hv_delete(fields, "height", 6, G_DISCARD); }
fp= hv_fetch(fields, "override_redirect", 17, 0);
if (fp && *fp) { s->xcreatewindow.override_redirect= SvIV(*fp);; if (consume) hv_delete(fields, "override_redirect", 17, G_DISCARD); }
fp= hv_fetch(fields, "parent", 6, 0);
if (fp && *fp) { s->xcreatewindow.parent= PerlXlib_sv_to_xid(*fp);; if (consume) hv_delete(fields, "parent", 6, G_DISCARD); }
fp= hv_fetch(fields, "width", 5, 0);
if (fp && *fp) { s->xcreatewindow.width= SvIV(*fp);; if (consume) hv_delete(fields, "width", 5, G_DISCARD); }
fp= hv_fetch(fields, "window", 6, 0);
if (fp && *fp) { s->xcreatewindow.window= PerlXlib_sv_to_xid(*fp);; if (consume) hv_delete(fields, "window", 6, G_DISCARD); }
fp= hv_fetch(fields, "x", 1, 0);
if (fp && *fp) { s->xcreatewindow.x= SvIV(*fp);; if (consume) hv_delete(fields, "x", 1, G_DISCARD); }
fp= hv_fetch(fields, "y", 1, 0);
if (fp && *fp) { s->xcreatewindow.y= SvIV(*fp);; if (consume) hv_delete(fields, "y", 1, G_DISCARD); }
break;
case EnterNotify:
case LeaveNotify:
fp= hv_fetch(fields, "detail", 6, 0);
if (fp && *fp) { s->xcrossing.detail= SvIV(*fp);; if (consume) hv_delete(fields, "detail", 6, G_DISCARD); }
fp= hv_fetch(fields, "focus", 5, 0);
if (fp && *fp) { s->xcrossing.focus= SvIV(*fp);; if (consume) hv_delete(fields, "focus", 5, G_DISCARD); }
fp= hv_fetch(fields, "mode", 4, 0);
if (fp && *fp) { s->xcrossing.mode= SvIV(*fp);; if (consume) hv_delete(fields, "mode", 4, G_DISCARD); }
fp= hv_fetch(fields, "root", 4, 0);
if (fp && *fp) { s->xcrossing.root= PerlXlib_sv_to_xid(*fp);; if (consume) hv_delete(fields, "root", 4, G_DISCARD); }
fp= hv_fetch(fields, "same_screen", 11, 0);
if (fp && *fp) { s->xcrossing.same_screen= SvIV(*fp);; if (consume) hv_delete(fields, "same_screen", 11, G_DISCARD); }
fp= hv_fetch(fields, "state", 5, 0);
if (fp && *fp) { s->xcrossing.state= SvUV(*fp);; if (consume) hv_delete(fields, "state", 5, G_DISCARD); }
fp= hv_fetch(fields, "subwindow", 9, 0);
if (fp && *fp) { s->xcrossing.subwindow= PerlXlib_sv_to_xid(*fp);; if (consume) hv_delete(fields, "subwindow", 9, G_DISCARD); }
fp= hv_fetch(fields, "time", 4, 0);
if (fp && *fp) { s->xcrossing.time= SvUV(*fp);; if (consume) hv_delete(fields, "time", 4, G_DISCARD); }
fp= hv_fetch(fields, "window", 6, 0);
if (fp && *fp) { s->xcrossing.window= PerlXlib_sv_to_xid(*fp);; if (consume) hv_delete(fields, "window", 6, G_DISCARD); }
fp= hv_fetch(fields, "x", 1, 0);
if (fp && *fp) { s->xcrossing.x= SvIV(*fp);; if (consume) hv_delete(fields, "x", 1, G_DISCARD); }
fp= hv_fetch(fields, "x_root", 6, 0);
if (fp && *fp) { s->xcrossing.x_root= SvIV(*fp);; if (consume) hv_delete(fields, "x_root", 6, G_DISCARD); }
fp= hv_fetch(fields, "y", 1, 0);
if (fp && *fp) { s->xcrossing.y= SvIV(*fp);; if (consume) hv_delete(fields, "y", 1, G_DISCARD); }
fp= hv_fetch(fields, "y_root", 6, 0);
if (fp && *fp) { s->xcrossing.y_root= SvIV(*fp);; if (consume) hv_delete(fields, "y_root", 6, G_DISCARD); }
break;
case DestroyNotify:
fp= hv_fetch(fields, "event", 5, 0);
if (fp && *fp) { s->xdestroywindow.event= PerlXlib_sv_to_xid(*fp);; if (consume) hv_delete(fields, "event", 5, G_DISCARD); }
fp= hv_fetch(fields, "window", 6, 0);
if (fp && *fp) { s->xdestroywindow.window= PerlXlib_sv_to_xid(*fp);; if (consume) hv_delete(fields, "window", 6, G_DISCARD); }
break;
case 0:
fp= hv_fetch(fields, "error_code", 10, 0);