~emersion/drmdb

f3f1f9b59322bba800b3cb14b1588bb0be5a9f7e — Simon Ser 2 years ago 5ecafb4
Add links in device list
4 files changed, 34 insertions(+), 28 deletions(-)

M db.go
M public/device.html
M public/devices.html
M server.go
M db.go => db.go +11 -9
@@ 9,6 9,7 @@ import (
	"io/ioutil"
	"os"
	"path/filepath"
	"strings"

	"git.sr.ht/~emersion/drmdb/drmtree"
)


@@ 78,8 79,8 @@ func store(n *drmtree.Node) (string, error) {
	return k, f.Close()
}

func _load(filename string) (*drmtree.Node, error) {
	p := filepath.Join(dbDir, filename)
func load(k string) (*drmtree.Node, error) {
	p := filepath.Join(dbDir, k + ".json")
	f, err := os.Open(p)
	if err != nil {
		return nil, err


@@ 94,23 95,24 @@ func _load(filename string) (*drmtree.Node, error) {
	return &n, f.Close()
}

func load(k string) (*drmtree.Node, error) {
	return _load(k + ".json")
}

func walk(fn func(n *drmtree.Node) error) error {
func walk(fn func(k string, n *drmtree.Node) error) error {
	files, err := ioutil.ReadDir(dbDir)
	if err != nil {
		return err
	}

	for _, fi := range files {
		n, err := _load(fi.Name())
		if !strings.HasSuffix(fi.Name(), ".json") {
			continue
		}
		k := strings.TrimSuffix(fi.Name(), ".json")

		n, err := load(k)
		if err != nil {
			return err
		}

		if err := fn(n); err == errStop {
		if err := fn(k, n); err == errStop {
			return nil
		} else if err != nil {
			return err

M public/device.html => public/device.html +2 -0
@@ 9,6 9,8 @@
	<a href="/">Back to index</a>
</p>

<h2>Raw data</h2>

{{define "tree"}}
	<li>{{.Text}}
		{{if gt (len .Children) 0}}

M public/devices.html => public/devices.html +3 -1
@@ 17,7 17,9 @@
    <tbody>
        {{range .Devices}}
        <tr>
            <td class="pre">{{.ID}}</td>
            <td class="pre">
                <a href="/devices/{{.Key}}">{{.BusID}}</a>
            </td>
            <td>{{.BusType}}</td>
            {{if .Vendor}}
            <td>{{.Vendor}}</td>

M server.go => server.go +18 -18
@@ 43,7 43,8 @@ type propertyData struct {
}

type deviceData struct {
	ID      string
	Key     string
	BusID   string
	BusType drm.BusType
	Vendor  string
	Name    string


@@ 80,7 81,7 @@ func walkNodeProps(n *drmtree.Node, f func(drm.AnyID, string, *drmtree.Property)

func walkLatestDriver(fn func(n *drmtree.Node) error) error {
	latest := make(map[string]*drmtree.Node)
	err := walk(func(n *drmtree.Node) error {
	err := walk(func(k string, n *drmtree.Node) error {
		other, ok := latest[n.Driver.Name]
		if ok {
			if n.Driver.Version.Less(&other.Driver.Version) {


@@ 211,26 212,25 @@ func New() *echo.Echo {

	e.GET("/devices", func(c echo.Context) error {
		devices := make(map[string]deviceData)
		err := walk(func(n *drmtree.Node) error {
		err := walk(func(k string, n *drmtree.Node) error {
			data := deviceData{
				Key: k,
				BusID: n.Device.BusID(),
				BusType: n.Device.BusType,
				Driver:  n.Driver.Name,
			}

			switch dev := n.Device.DeviceData.(type) {
			case *drmtree.DevicePCI:
				id := fmt.Sprintf("%04X:%04X", dev.Vendor, dev.Device)
				devices[id] = deviceData{
					ID:      id,
					BusType: n.Device.BusType,
					Vendor:  pciVendors[uint16(dev.Vendor)],
					Name:    pciDevices[dev.Vendor<<16|dev.Device],
					Driver:  n.Driver.Name,
				}
				data.Vendor = pciVendors[uint16(dev.Vendor)]
				data.Name = pciDevices[dev.Vendor<<16|dev.Device]
			case *drmtree.DevicePlatform:
				id := strings.Join(dev.Compatible, " ")
				devices[id] = deviceData{
					ID:      id,
					BusType: n.Device.BusType,
					Driver:  n.Driver.Name,
				}
				// No-op
			default:
				return nil
			}

			devices[data.BusID] = data
			return nil
		})
		if err != nil {


@@ 310,7 310,7 @@ func New() *echo.Echo {
	e.GET("/properties", func(c echo.Context) error {
		drivers := make(map[string]struct{})
		props := make(map[string]propertyData)
		err := walk(func(n *drmtree.Node) error {
		err := walk(func(k string, n *drmtree.Node) error {
			drv := n.Driver.Name
			drivers[drv] = struct{}{}