summaryrefslogtreecommitdiff
path: root/xlsclients.go
blob: b13dd4813d9d20e0c758484d8700528bc6855de4 (plain)
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
package main

import (
	"bytes"
	"flag"
	"fmt"
	"strings"
	"unicode"
	"xgob"
	"xgob/util/atom"
	"xgob/xproto"
	"xmu"
)

var display = flag.String("display", "", "Display to connect to")
var maxcmdlen = flag.Uint("max", 10000, "Maximum command length to show")
var all = flag.Bool("all", false, "Show all screens")
var verbose = flag.Bool("long", false, "Verbose output")

func initAtoms(c *xgob.Connection) {
	atom.PreloadAtom(c, "WM_STATE")

	/* The rest of these are pre-defined atoms.
	   They can be removed when we have a real xproto generator. */
	atom.PreloadAtom(c, "WM_CLIENT_MACHINE")
	atom.PreloadAtom(c, "WM_COMMAND")
	atom.PreloadAtom(c, "WM_NAME")
	atom.PreloadAtom(c, "WM_ICON_NAME")
	atom.PreloadAtom(c, "WM_CLASS")
	atom.PreloadAtom(c, "STRING")
}

func formatTextField(c *xgob.Connection, s string, t *xproto.GetPropertyReply) string {
	if t.Error != nil || t.Type == xproto.Atom(0) || t.Format == 0 {
		return "''"
	}

	rv := s

	if t.Type == atom.Atom(c, "STRING") && t.Format == 8 {
		rv += string(t.Value)
	} else {
		rv += "<unknown type "
		if t.Type == xproto.Atom(0) {
			rv += "None"
		} else {
			/* This should happen so rarely as to make no odds. Eat a round-trip: */
			rv += atom.AtomName(c, t.Type)
		}
		rv += fmt.Sprintf(" (%d) or format %d>", t.Type, t.Format)
	}

	if s != "" {
		rv += "\n"
	}
	return rv
}

func quotedWord(buf *bytes.Buffer, word []byte, maxlen uint) uint {
	quote := '\''
	otherQuote := '"'
	var printed uint

	/*
	 * walk down seeing whether or not we need to quote
	 */
	needQuote := false
	for _, c := range word {
		if !(c < 0x80 && (unicode.IsLetter(int(c)) || unicode.IsDigit(int(c))) ||
			(c == '-' || c == '_' || c == '.' || c == '+' ||
				c == '/' || c == '=' || c == ':' || c == ',')) {
			needQuote = true
			break
		}
	}

	/*
	 * write out the string: if we hit a quote, then close any previous quote,
	 * emit the other quote, swap quotes and continue on.
	 */
	inQuote := needQuote
	if needQuote {
		buf.WriteRune(quote)
		printed++
	}
	for _, c := range word {
		if int(c) == quote {
			if inQuote {
				buf.WriteRune(quote)
				printed++
			}
			buf.WriteRune(otherQuote)
			printed++
			tmp := otherQuote
			otherQuote = quote
			quote = tmp
			inQuote = true
		}
		buf.WriteRune(int(c))
		printed++
		if printed >= maxlen {
			break
		}
	}

	/* close the quote if we opened one and if we printed the whole string */
	if inQuote && printed < maxlen {
		buf.WriteRune(quote)
		printed++
	}

	return printed
}

func quotedWords(prop []byte) string {
	buf := bytes.NewBufferString("")

	charsleft := *maxcmdlen
	words := bytes.Split(prop, []byte{0}, -1)
	for i, word := range words {
		charsleft -= quotedWord(buf, word, charsleft)
		if i < len(words)-1 {
			if charsleft > 0 {
				buf.WriteRune(' ')
			} else {
				break
			}
		}
	}

	return buf.String()
}

func clientProperties(c *xgob.Connection, win xproto.Window, result chan string) {
	machineCookie := xproto.GetProperty(c, false, win, atom.Atom(c, "WM_CLIENT_MACHINE"), xproto.Atom(0), 0, 1000000)
	commandCookie := xproto.GetProperty(c, false, win, atom.Atom(c, "WM_COMMAND"), xproto.Atom(0), 0, 1000000)
	var nameCookie chan xproto.GetPropertyReply
	var iconNameCookie chan xproto.GetPropertyReply
	var classCookie chan xproto.GetPropertyReply
	if *verbose {
		nameCookie = xproto.GetProperty(c, false, win, atom.Atom(c, "WM_NAME"), xproto.Atom(0), 0, 1000000)
		iconNameCookie = xproto.GetProperty(c, false, win, atom.Atom(c, "WM_ICON_NAME"), xproto.Atom(0), 0, 1000000)
		classCookie = xproto.GetProperty(c, false, win, atom.Atom(c, "WM_CLASS"), xproto.Atom(0), 0, 1000000)
	}
	c.Flush()

	machine := <-machineCookie
	command := <-commandCookie

	if command.Error != nil || command.Format != 8 {
		result <- ""
		return
	}

	var name, iconName, class xproto.GetPropertyReply
	if *verbose {
		name = <-nameCookie
		iconName = <-iconNameCookie
		class = <-classCookie
	}

	list := make([]string, 0)

	// Header
	if *verbose {
		list = append(list, fmt.Sprintf("Window 0x%X:\n", win))
		list = append(list, formatTextField(c, "  Machine:  ", &machine))
		if name.Error == nil && name.Type != 0 {
			list = append(list, formatTextField(c, "  Name:  ", &name))
		}
		if iconName.Error == nil && iconName.Type != 0 {
			list = append(list, formatTextField(c, "  Icon Name:  ", &iconName))
		}
	} else {
		list = append(list, formatTextField(c, "", &machine))
	}

	// Command
	commandHeader := "  "
	if *verbose {
		commandHeader = "  Command:  "
	}
	list = append(list, commandHeader)
	list = append(list, quotedWords(command.Value))
	list = append(list, "\n")

	// Trailer
	if *verbose {
		if class.Error == nil && class.Type != xproto.Atom(0) {
			name := ""
			cls := "(nil)"
			index := bytes.IndexByte(class.Value, 0)
			if index >= 0 {
				name = string(class.Value[:index])
				cls = string(class.Value[index+1:])
			} else {
				name = string(class.Value)
			}
			list = append(list, fmt.Sprintf("  Instance/Class: %s/%s\n", name, cls))
		}
	}
	result <- strings.Join(list, "")
}

func findClientProperties(c *xgob.Connection, win xproto.Window, result chan string) {
	client := xmu.ClientWindow(c, win)
	if client == xproto.Window(0) {
		result <- ""
		return
	}
	clientProperties(c, client, result)
}

func lookat(c *xgob.Connection, root xproto.Window, result chan string) {
	var count uint
	prop := make(chan string)

	/*
	 * clients are not allowed to stomp on the root and ICCCM doesn't yet
	 * say anything about window managers putting stuff there; but, try
	 * anyway.
	 */
	go clientProperties(c, root, prop)
	count++

	treeCookie := xproto.QueryTree(c, root)
	c.Flush()
	tree := <-treeCookie
	for _, child := range tree.Children {
		go findClientProperties(c, child, prop)
		count++
	}

	list := make([]string, 0, count)
	for i := uint(0); i < count; i++ {
		list = append(list, <-prop)
	}
	result <- strings.Join(list, "")
}

func main() {
	flag.Parse()
	c, screen := xgob.Connect(*display)

	if c.HasError() {
		println("Cannot open display", *display)
		return
	}

	if screen >= len(c.Setup.Roots) && !*all {
		println("Invalid Screen", screen)
		return
	}

	initAtoms(c)

	results := make([]chan string, 0)

	if *all {
		for _, r := range c.Setup.Roots {
			out := make(chan string)
			go lookat(c, xproto.Window(r.Root), out)
			results = append(results, out)
		}
	} else {
		out := make(chan string)
		go lookat(c, xproto.Window(c.Setup.Roots[screen].Root), out)
		results = append(results, out)
	}

	for _, result := range results {
		fmt.Print(<-result)
	}
}