All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.moaxcp.x11client.protocol.xv.AdaptorInfo Maven / Gradle / Ivy

There is a newer version: 0.18.2
Show newest version
package com.github.moaxcp.x11client.protocol.xv;

import com.github.moaxcp.x11client.protocol.X11Input;
import com.github.moaxcp.x11client.protocol.X11Output;
import com.github.moaxcp.x11client.protocol.XObject;
import com.github.moaxcp.x11client.protocol.XStruct;
import java.io.IOException;
import java.lang.Byte;
import java.lang.Override;
import java.util.ArrayList;
import java.util.List;
import lombok.Builder;
import lombok.NonNull;
import lombok.Value;

@Value
@Builder
public class AdaptorInfo implements XStruct {
  private int baseId;

  private short numPorts;

  private short numFormats;

  private byte type;

  @NonNull
  private List name;

  @NonNull
  private List formats;

  public static AdaptorInfo readAdaptorInfo(X11Input in) throws IOException {
    int baseId = in.readCard32();
    short nameSize = in.readCard16();
    short numPorts = in.readCard16();
    short numFormats = in.readCard16();
    byte type = in.readCard8();
    in.readPad(1);
    List name = in.readChar(Short.toUnsignedInt(nameSize));
    in.readPadAlign(Short.toUnsignedInt(nameSize));
    List formats = new ArrayList<>(Short.toUnsignedInt(numFormats));
    for(int i = 0; i < Short.toUnsignedInt(numFormats); i++) {
      formats.add(Format.readFormat(in));
    }
    AdaptorInfo.AdaptorInfoBuilder javaBuilder = AdaptorInfo.builder();
    javaBuilder.baseId(baseId);
    javaBuilder.numPorts(numPorts);
    javaBuilder.numFormats(numFormats);
    javaBuilder.type(type);
    javaBuilder.name(name);
    javaBuilder.formats(formats);
    return javaBuilder.build();
  }

  @Override
  public void write(X11Output out) throws IOException {
    out.writeCard32(baseId);
    short nameSize = (short) name.size();
    out.writeCard16(nameSize);
    out.writeCard16(numPorts);
    out.writeCard16(numFormats);
    out.writeCard8(type);
    out.writePad(1);
    out.writeChar(name);
    out.writePadAlign(Short.toUnsignedInt(nameSize));
    for(Format t : formats) {
      t.write(out);
    }
  }

  public boolean isTypeEnabled(@NonNull Type... maskEnums) {
    for(Type m : maskEnums) {
      if(!m.isEnabled(type)) {
        return false;
      }
    }
    return true;
  }

  @Override
  public int getSize() {
    return 12 + 1 * name.size() + XObject.getSizeForPadAlign(4, 1 * name.size()) + XObject.sizeOf(formats);
  }

  public static class AdaptorInfoBuilder {
    public boolean isTypeEnabled(@NonNull Type... maskEnums) {
      for(Type m : maskEnums) {
        if(!m.isEnabled(type)) {
          return false;
        }
      }
      return true;
    }

    public AdaptorInfo.AdaptorInfoBuilder typeEnable(Type... maskEnums) {
      for(Type m : maskEnums) {
        type((byte) m.enableFor(type));
      }
      return this;
    }

    public AdaptorInfo.AdaptorInfoBuilder typeDisable(Type... maskEnums) {
      for(Type m : maskEnums) {
        type((byte) m.disableFor(type));
      }
      return this;
    }

    public int getSize() {
      return 12 + 1 * name.size() + XObject.getSizeForPadAlign(4, 1 * name.size()) + XObject.sizeOf(formats);
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy