~emersion/drmdb

5dee14f1b32f360eae8a8f76d45db39f9b53099b — Simon Ser 2 years ago ad07722
cmd/drmdump: use drmtree's FormatTree functions
1 files changed, 3 insertions(+), 191 deletions(-)

M cmd/drmdump/main.go
M cmd/drmdump/main.go => cmd/drmdump/main.go +3 -191
@@ 81,34 81,6 @@ func driver(n *drm.Node) (*drmtree.Driver, error) {
	}, nil
}

func printDriver(tf treefmt.Formatter, drv *drmtree.Driver) {
	tf.Printf("Driver: %v (%v) version %v", drv.Name, drv.Desc, &drv.Version)
	tfc := tf.NewChild()

	for c, v := range drv.Caps {
		if v != nil {
			if c == "PRIME" {
				tfc.Printf("DRM_CAP_PRIME supported")
				tfcc := tfc.NewChild()
				tfcc.Printf("DRM_CAP_PRIME_IMPORT = %v", *v&drm.CapPrimeImport != 0)
				tfcc.Printf("DRM_CAP_PRIME_EXPORT = %v", *v&drm.CapPrimeExport != 0)
			} else {
				tfc.Printf("DRM_CAP_%v = %v", c, *v)
			}
		} else {
			tfc.Printf("DRM_CAP_%v unsupported", c)
		}
	}

	for c, ok := range drv.ClientCaps {
		if ok {
			tfc.Printf("DRM_CLIENT_CAP_%v supported", c)
		} else {
			tfc.Printf("DRM_CLIENT_CAP_%v unsupported", c)
		}
	}
}

func device(n *drm.Node) (*drmtree.Device, error) {
	dev, err := n.GetDevice()
	if err != nil {


@@ 131,32 103,6 @@ func device(n *drm.Node) (*drmtree.Device, error) {
	}
}

func printDevice(tf treefmt.Formatter, dev *drmtree.Device) {
	switch dev.BusType {
	case drm.BusPCI:
		pci := dev.DeviceData.(*drmtree.DevicePCI)
		tf.Printf("Device: %v %04X:%04X", dev.BusType, pci.Vendor, pci.Device)
	default:
		tf.Printf("Device: %v", dev.BusType)
	}
}

func bitfieldString(v uint32) string {
	s := "{"
	first := true
	for i := 0; i < 32; i++ {
		if v&(1<<uint(i)) != 0 {
			if !first {
				s += ", "
			}
			s += fmt.Sprintf("%v", i)
			first = false
		}
	}
	s += "}"
	return s
}

func modeList(modes []drm.ModeModeInfo) []drmtree.Mode {
	l := make([]drmtree.Mode, len(modes))
	for i, m := range modes {


@@ 307,18 253,6 @@ func properties(n *drm.Node, id drm.AnyID) (map[string]drmtree.Property, error) 
	return m, nil
}

func printProperties(tf treefmt.Formatter, props map[string]drmtree.Property) {
	for name, prop := range props {
		// TODO: immutable, atomic
		// TODO: type-specific property data
		val := prop.Data
		if val == nil {
			val = prop.Value
		}
		tf.Printf("%q: %v = %v", name, prop.Type, val)
	}
}

func connectors(n *drm.Node, card *drm.ModeCard) ([]drmtree.Connector, error) {
	l := make([]drmtree.Connector, len(card.Connectors))
	for i, id := range card.Connectors {


@@ 347,48 281,6 @@ func connectors(n *drm.Node, card *drm.ModeCard) ([]drmtree.Connector, error) {
	return l, nil
}

func encoderIDsString(encs []drm.EncoderID) string {
	s := "{"
	for i, id := range encs {
		if i != 0 {
			s += ", "
		}
		s += fmt.Sprintf("%v", id)
	}
	s += "}"
	return s
}

func printModes(tf treefmt.Formatter, modes []drmtree.Mode) {
	for _, mode := range modes {
		tf.Printf("%v", &mode)
	}
}

func printConnectors(tf treefmt.Formatter, conns []drmtree.Connector) {
	for i, conn := range conns {
		tf.Printf("Connector %v", i)
		tfc := tf.NewChild()

		tfc.Printf("Object ID: %v", conn.ID)
		tfc.Printf("Type: %v", conn.Type)
		tfc.Printf("Status: %v", conn.Status)
		if conn.Status == drm.ConnectorStatusConnected {
			tfc.Printf("Physical size: %vx%v mm", conn.PhyWidth, conn.PhyHeight)
			tfc.Printf("Subpixel: %v", conn.Subpixel)
		}
		tfc.Printf("Encoders: %v", encoderIDsString(conn.Encoders))
		if len(conn.Modes) > 0 {
			tfc.Printf("Modes")
			printModes(tfc.NewChild(), conn.Modes)
		}
		if len(conn.Properties) > 0 {
			tfc.Printf("Properties")
			printProperties(tfc.NewChild(), conn.Properties)
		}
	}
}

func encoders(n *drm.Node, card *drm.ModeCard) ([]drmtree.Encoder, error) {
	l := make([]drmtree.Encoder, len(card.Encoders))
	for i, id := range card.Encoders {


@@ 402,18 294,6 @@ func encoders(n *drm.Node, card *drm.ModeCard) ([]drmtree.Encoder, error) {
	return l, nil
}

func printEncoders(tf treefmt.Formatter, encs []drmtree.Encoder) {
	for i, enc := range encs {
		tf.Printf("Encoder %v", i)
		tfc := tf.NewChild()

		tfc.Printf("Object ID: %v", enc.ID)
		tfc.Printf("Type: %v", enc.Type)
		tfc.Printf("CRTCs: %v", bitfieldString(enc.PossibleCRTCs))
		tfc.Printf("Clones: %v", bitfieldString(enc.PossibleClones))
	}
}

func crtcs(n *drm.Node, card *drm.ModeCard) ([]drmtree.CRTC, error) {
	l := make([]drmtree.CRTC, len(card.CRTCs))
	for i, id := range card.CRTCs {


@@ 440,25 320,6 @@ func crtcs(n *drm.Node, card *drm.ModeCard) ([]drmtree.CRTC, error) {
	return l, nil
}

func printCRTCs(tf treefmt.Formatter, crtcs []drmtree.CRTC) {
	for i, crtc := range crtcs {
		tf.Printf("CRTC %v", i)
		tfc := tf.NewChild()

		tfc.Printf("Object ID: %v", crtc.ID)
		tfc.Printf("FB: %v", crtc.FB)
		tfc.Printf("Position: %v, %v", crtc.X, crtc.Y)
		tfc.Printf("Gamma size: %v", crtc.GammaSize)
		if crtc.Mode != nil {
			tfc.Printf("Mode: %v", crtc.Mode)
		}
		if len(crtc.Properties) > 0 {
			tfc.Printf("Properties")
			printProperties(tfc.NewChild(), crtc.Properties)
		}
	}
}

func planes(n *drm.Node) ([]drmtree.Plane, error) {
	planes, err := n.ModeGetPlaneResources()
	if err != nil {


@@ 491,33 352,6 @@ func planes(n *drm.Node) ([]drmtree.Plane, error) {
	return l, nil
}

func printFormats(tf treefmt.Formatter, formats []drm.Format) {
	for _, fmt := range formats {
		tf.Printf("%v", fmt)
	}
}

func printPlanes(tf treefmt.Formatter, planes []drmtree.Plane) {
	for i, plane := range planes {
		tf.Printf("Plane %v", i)
		tfc := tf.NewChild()

		tfc.Printf("Object ID: %v", plane.ID)
		tfc.Printf("CRTC: %v", plane.CRTC)
		tfc.Printf("FB: %v", plane.FB)
		tfc.Printf("CRTCs: %v", bitfieldString(plane.PossibleCRTCs))
		tfc.Printf("Gamma size: %v", plane.GammaSize)
		if len(plane.Formats) > 0 {
			tfc.Printf("Formats")
			printFormats(tfc.NewChild(), plane.Formats)
		}
		if len(plane.Properties) > 0 {
			tfc.Printf("Properties")
			printProperties(tfc.NewChild(), plane.Properties)
		}
	}
}

func node(nodePath string) (*drmtree.Node, error) {
	f, err := os.Open(nodePath)
	if err != nil {


@@ 572,26 406,6 @@ func node(nodePath string) (*drmtree.Node, error) {
	}, nil
}

func printNode(tf treefmt.Formatter, path string, n *drmtree.Node) {
	tf.Printf("Node: %s", path)
	tfc := tf.NewChild()

	printDriver(tfc, n.Driver)
	printDevice(tfc, n.Device)

	tfc.Printf("Connectors")
	printConnectors(tfc.NewChild(), n.Connectors)

	tfc.Printf("Encoders")
	printEncoders(tfc.NewChild(), n.Encoders)

	tfc.Printf("CRTCs")
	printCRTCs(tfc.NewChild(), n.CRTCs)

	tfc.Printf("Planes")
	printPlanes(tfc.NewChild(), n.Planes)
}

func main() {
	var (
		outputJSON bool


@@ 601,7 415,7 @@ func main() {
	flag.BoolVar(&input, "i", false, "Read JSON data from stdin")
	flag.Parse()

	var nodes map[string]*drmtree.Node
	var nodes drmtree.NodeMap
	if input {
		if err := json.NewDecoder(os.Stdin).Decode(&nodes); err != nil {
			log.Fatalf("Failed to read JSON: %v", err)


@@ 612,7 426,7 @@ func main() {
			log.Fatalf("Failed to list DRM nodes: %v", err)
		}

		nodes = make(map[string]*drmtree.Node)
		nodes = make(drmtree.NodeMap)
		for _, p := range paths {
			n, err := node(p)
			if err != nil {


@@ 629,8 443,6 @@ func main() {
		}
	} else {
		tf := treefmt.NewTextFormatter(os.Stdout)
		for path, n := range nodes {
			printNode(tf, path, n)
		}
		nodes.FormatTree(tf)
	}
}