Skip to content

Commit

Permalink
Log and ignore missing flag definitions, and add placeholders for int…
Browse files Browse the repository at this point in the history
…ernal GdkModifierType flags (fixes #155)
  • Loading branch information
jwharm committed Nov 25, 2024
1 parent b243f0c commit b23faa4
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public class Patches {
new GstAudioPatch(),
new GstVideoPatch(),
new GstBasePatch(),
new GdkPatch(),
new GtkPatch(),
new HarfBuzzPatch(),
new PangoPatch(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/* Java-GI - Java language bindings for GObject-Introspection-based libraries
* Copyright (C) 2022-2024 Jan-Willem Harmannij
*
* SPDX-License-Identifier: LGPL-2.1-or-later
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, see <http://www.gnu.org/licenses/>.
*/

package io.github.jwharm.javagi.patches;

import io.github.jwharm.javagi.gir.*;
import io.github.jwharm.javagi.util.Patch;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import static java.util.Collections.emptyMap;

public class GdkPatch implements Patch {
@Override
public GirElement patch(GirElement element, String namespace) {

if (!"Gdk".equals(namespace))
return element;

/*
* The GdkModifierType documentation states:
* Note that GDK may add internal values to events which include
* values outside of this enumeration. Your code should preserve and
* ignore them.
*
* We preserve and ignore the internal flags in our Java EnumSet by
* adding placeholder "INTERNAL_n" flags for the missing ones.
*/
if (element instanceof Bitfield bf
&& "ModifierType".equals(bf.name())) {
var children = new ArrayList<>(bf.children());
Doc doc = new Doc(emptyMap(),
"Internal flag. Your code should preserve and ignore this flag.");
for (int bit = 0; bit < 31; bit++) {
var value = Integer.toString(1 << bit);
if (bf.members().stream().noneMatch(m -> m.value().equals(value)))
children.add(bit + 2, new Member(
Map.of("name", "INTERNAL_" + bit, "value", value),
List.of(doc)));
}
return element.withChildren(children);
}

return element;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,12 @@
import java.util.Arrays;
import java.util.function.Function;

import io.github.jwharm.javagi.Constants;
import io.github.jwharm.javagi.base.Enumeration;
import org.gnome.glib.GLib;

import io.github.jwharm.javagi.base.*;
import org.gnome.glib.LogLevelFlags;
import org.gnome.glib.Type;

import static java.lang.Long.max;
Expand Down Expand Up @@ -1345,7 +1347,9 @@ public static MemorySegment allocateNativeArray(MemorySegment[] array,
}

/**
* Create an EnumSet of class `cls` from the provided bitfield
* Create an EnumSet of class `cls` from the provided bitfield.
* Undefined flags are logged (with level
* {@link LogLevelFlags#LEVEL_WARNING}) and ignored.
*
* @param <T> an enum implementing the Java-GI Enumeration interface
* @param cls the class of the enum
Expand All @@ -1361,8 +1365,19 @@ EnumSet<T> intToEnumSet(Class<T> cls,
EnumSet<T> enumSet = EnumSet.noneOf(cls);
int position = 0;
while (n != 0) {
if ((n & 1) == 1)
enumSet.add(make.apply(1 << position));
if ((n & 1) == 1) {
// Gracefully handle undefined flags
try {
T flag = make.apply(1 << position);
enumSet.add(flag);
} catch (IllegalStateException e) {
GLib.log(Constants.LOG_DOMAIN,
LogLevelFlags.LEVEL_WARNING,
"Unexpected flag %d in enum %s\n",
n,
cls == null ? "NULL" : cls.getName());
}
}
position++;
n >>= 1;
}
Expand Down

0 comments on commit b23faa4

Please sign in to comment.