示例#1
0
文件: util.go 项目: sbinet/xgbutil
// findIconEwmh helps FindIcon by trying to return an ewmh-style icon that is
// closest to the preferred size specified.
func findIconEwmh(X *xgbutil.XUtil, wid xproto.Window,
	width, height int) (*Image, error) {

	icons, err := ewmh.WmIconGet(X, wid)
	if err != nil {
		return nil, err
	}

	icon := FindBestEwmhIcon(width, height, icons)
	if icon == nil {
		return nil, fmt.Errorf("Could not find any _NET_WM_ICON icon.")
	}

	return NewEwmhIcon(X, icon), nil
}
示例#2
0
func main() {
	defer Recovery()

	X, Xerr = xgbutil.Dial("")
	if Xerr != nil {
		panic(Xerr)
	}

	active, _ := ewmh.ActiveWindowGet(X)
	icons, _ := ewmh.WmIconGet(X, active)

	var width, height int = 300, 300

	work := xgraphics.FindBestIcon(width, height, icons)
	if work != nil {
		fmt.Printf("Working with icon (%d, %d)\n", work.Width, work.Height)
	} else {
		fmt.Println("No good icon... :-(")
		return
	}

	eimg, emask := xgraphics.EwmhIconToImage(work)

	img := xgraphics.Scale(eimg, int(width), int(height))
	mask := xgraphics.Scale(emask, int(width), int(height))

	dest := xgraphics.BlendBg(img, mask, 100, color.RGBA{0, 0, 255, 255})

	// Let's try to write some text...
	// xgraphics.DrawText(dest, 50, 50, color.RGBA{255, 255, 255, 255}, 20,
	// fontFile, "Hello, world!")

	// tw, th, err := xgraphics.TextExtents(fontFile, 11, "Hiya")
	// fmt.Println(tw, th, err)

	win := xgraphics.CreateImageWindow(X, dest, 3940, 0)
	X.Conn().MapWindow(win)

	// time.Sleep(20 * time.Second)
	select {}
}
示例#3
0
func main() {
	X, Xerr = xgbutil.NewConn()
	if Xerr != nil {
		panic(Xerr)
	}

	active, _ := ewmh.ActiveWindowGet(X)

	parent, _ := xwindow.ParentWindow(X, active)
	actOpacity, _ := ewmh.WmWindowOpacityGet(X, parent)
	fmt.Printf("Opacity for active window: %f\n", actOpacity)

	showDesk, _ := ewmh.ShowingDesktopGet(X)
	fmt.Printf("Showing desktop? %v\n", showDesk)

	wmName, err := ewmh.GetEwmhWM(X)
	if err != nil {
		fmt.Printf("No conforming window manager found... :-(\n")
		fmt.Println(err)
	} else {
		fmt.Printf("Window manager: %s\n", wmName)
	}

	pager := xproto.Window(0x160001e)
	middle := xproto.Window(0x3200016)
	geom, _ := ewmh.DesktopGeometryGet(X)
	desktops, _ := ewmh.DesktopNamesGet(X)
	curdesk, _ := ewmh.CurrentDesktopGet(X)
	clients, _ := ewmh.ClientListGet(X)
	activeName, _ := ewmh.WmNameGet(X, active)

	fmt.Printf("Active window: %x\n", active)
	fmt.Printf("Current desktop: %d\n", curdesk)
	fmt.Printf("Client list: %v\n", clients)
	fmt.Printf("Desktop geometry: (width: %d, height: %d)\n",
		geom.Width, geom.Height)
	fmt.Printf("Active window name: %s\n", activeName)
	fmt.Printf("Desktop names: %s\n", desktops)

	var desk string
	if curdesk < len(desktops) {
		desk = desktops[curdesk]
	} else {
		desk = string(curdesk)
	}
	fmt.Printf("Current desktop: %s\n", desk)

	// fmt.Printf("\nChanging current desktop to 25 from %d\n", curdesk)
	ewmh.CurrentDesktopSet(X, curdesk)
	// fmt.Printf("Current desktop is now: %d\n", ewmh.CurrentDesktop(X))

	fmt.Printf("Setting active win to %x\n", middle)
	// ewmh.ActiveWindowReq(X, middle)

	rand.Seed(int64(time.Now().Nanosecond()))
	randStr := make([]byte, 20)
	for i, _ := range randStr {
		if rf := rand.Float32(); rf < 0.40 {
			randStr[i] = byte('a' + rand.Intn('z'-'a'))
		} else if rf < 0.80 {
			randStr[i] = byte('A' + rand.Intn('Z'-'A'))
		} else {
			randStr[i] = ' '
		}
	}

	ewmh.WmNameSet(X, active, string(randStr))
	newName, _ := ewmh.WmNameGet(X, active)
	fmt.Printf("New name: %s\n", newName)

	// deskNames := ewmh.DesktopNamesGet(X)
	// fmt.Printf("Desktop names: %s\n", deskNames)
	// deskNames[len(deskNames) - 1] = "xgbutil"
	// ewmh.DesktopNamesSet(X, deskNames)
	// fmt.Printf("Desktop names: %s\n", ewmh.DesktopNamesGet(X))

	supported, _ := ewmh.SupportedGet(X)
	fmt.Printf("Supported hints: %v\n", supported)
	fmt.Printf("Setting supported hints...\n")
	ewmh.SupportedSet(X, []string{"_NET_CLIENT_LIST", "_NET_WM_NAME",
		"_NET_WM_DESKTOP"})

	numDesks, _ := ewmh.NumberOfDesktopsGet(X)
	fmt.Printf("Number of desktops: %d\n", numDesks)
	// ewmh.NumberOfDesktopsReq(X.EwmhNumberOfDesktops(X) + 1)
	// time.Sleep(time.Second)
	// fmt.Printf("Number of desktops: %d\n", ewmh.NumberOfDesktops(X))

	viewports, _ := ewmh.DesktopViewportGet(X)
	fmt.Printf("Viewports (%d): %v\n", len(viewports), viewports)

	// viewports[2].X = 50
	// viewports[2].Y = 293
	// ewmh.DesktopViewportSet(X, viewports)
	// time.Sleep(time.Second)
	//
	// viewports = ewmh.DesktopViewport(X)
	// fmt.Printf("Viewports (%d): %v\n", len(viewports), viewports)

	// ewmh.CurrentDesktopReq(X, 3)

	visDesks, _ := ewmh.VisibleDesktopsGet(X)
	workarea, _ := ewmh.WorkareaGet(X)
	fmt.Printf("Visible desktops: %v\n", visDesks)
	fmt.Printf("Workareas: %v\n", workarea)
	// fmt.Printf("Virtual roots: %v\n", ewmh.VirtualRoots(X))
	// fmt.Printf("Desktop layout: %v\n", ewmh.DesktopLayout(X))
	fmt.Printf("Closing window %x\n", 0x2e004c5)
	ewmh.CloseWindow(X, 0x1e00cdf)

	fmt.Printf("Moving/resizing window: %x\n", 0x2e004d0)
	ewmh.MoveresizeWindow(X, 0x2e004d0, 1920, 30, 500, 500)

	// fmt.Printf("Trying to initiate a moveresize...\n")
	// ewmh.WmMoveresize(X, 0x2e004db, xgbutil.EwmhMove)
	// time.Sleep(5 * time.Second)
	// ewmh.WmMoveresize(X, 0x2e004db, xgbutil.EwmhCancel)

	// fmt.Printf("Stacking window %x...\n", 0x2e00509)
	// ewmh.RestackWindow(X, 0x2e00509)

	fmt.Printf("Requesting frame extents for active window...\n")
	ewmh.RequestFrameExtents(X, active)

	activeDesk, _ := ewmh.WmDesktopGet(X, active)
	activeType, _ := ewmh.WmWindowTypeGet(X, active)
	fmt.Printf("Active window's desktop: %d\n", activeDesk)
	fmt.Printf("Active's types: %v\n", activeType)
	// fmt.Printf("Pager's types: %v\n", ewmh.WmWindowType(X, 0x180001e))

	// fmt.Printf("Pager's state: %v\n", ewmh.WmState(X, 0x180001e))

	// ewmh.WmStateReq(X, active, xgbutil.EwmhStateToggle,
	// "_NET_WM_STATE_HIDDEN")
	// ewmh.WmStateReqExtra(X, active, xgbutil.EwmhStateToggle,
	// "_NET_WM_STATE_MAXIMIZED_VERT",
	// "_NET_WM_STATE_MAXIMIZED_HORZ", 2)

	activeAllowed, _ := ewmh.WmAllowedActionsGet(X, active)
	fmt.Printf("Allowed actions on active: %v\n", activeAllowed)

	struts, err := ewmh.WmStrutGet(X, pager)
	if err != nil {
		fmt.Printf("Pager struts: %v\n", err)
	} else {
		fmt.Printf("Pager struts: %v\n", struts)
	}

	pstruts, err := ewmh.WmStrutPartialGet(X, pager)
	if err != nil {
		fmt.Printf("Pager struts partial: %v - %v\n", pstruts, err)
	} else {
		fmt.Printf("Pager struts partial: %v\n", pstruts.BottomStartX)
	}

	// fmt.Printf("Icon geometry for active: %v\n",
	// ewmh.WmIconGeometry(X, active))

	icons, _ := ewmh.WmIconGet(X, active)
	fmt.Printf("Active window's (%x) icon data: (length: %v)\n",
		active, len(icons))
	for _, icon := range icons {
		fmt.Printf("\t(%d, %d)", icon.Width, icon.Height)
		fmt.Printf(" :: %d == %d\n", icon.Width*icon.Height, len(icon.Data))
	}
	// fmt.Printf("Now set them again...\n")
	// ewmh.WmIconSet(X, active, icons[:len(icons) - 1])
}
示例#4
0
func main() {
	defer Recovery()

	X, Xerr = xgbutil.Dial("")
	if Xerr != nil {
		panic(Xerr)
	}

	active, _ := ewmh.ActiveWindowGet(X)
	icons, _ := ewmh.WmIconGet(X, active)
	fmt.Printf("Active window's (%x) icon data: (length: %v)\n",
		active, len(icons))
	for _, icon := range icons {
		fmt.Printf("\t(%d, %d)", icon.Width, icon.Height)
		fmt.Printf(" :: %d == %d\n", icon.Width*icon.Height, len(icon.Data))
	}

	work := icons[2]
	fmt.Printf("Working with (%d, %d)\n", work.Width, work.Height)

	width, height := int(work.Width), int(work.Height)
	mask := image.NewRGBA(image.Rect(0, 0, width, height))
	img := image.NewRGBA(image.Rect(0, 0, width, height))

	for x := 0; x < width; x++ {
		for y := 0; y < height; y++ {
			argb := work.Data[x+(y*height)]
			alpha := argb >> 24
			red := ((alpha << 24) ^ argb) >> 16
			green := (((alpha << 24) + (red << 16)) ^ argb) >> 8
			blue := (((alpha << 24) + (red << 16) + (green << 8)) ^ argb) >> 0

			c := color.RGBA{
				R: uint8(red),
				G: uint8(green),
				B: uint8(blue),
				A: uint8(alpha),
			}
			img.SetRGBA(x, y, c)

			mask.Set(x, y, color.Alpha{uint8(alpha)})
		}
	}

	// blendMask := image.NewUniform(color.Alpha{127})
	// draw.DrawMask(mask, mask.Bounds(), mask, image.ZP, blendMask,
	// image.ZP, draw.Src)

	dest := image.NewRGBA(image.Rect(0, 0, width, height))
	allBlue := image.NewUniform(color.RGBA{127, 127, 127, 255})
	draw.Draw(dest, dest.Bounds(), allBlue, image.ZP, draw.Src)
	draw.DrawMask(dest, dest.Bounds(), img, image.ZP, mask, image.ZP, draw.Over)

	// Let's try to write some text...
	WriteText(dest)

	destWriter, err := os.Create("someicon.png")
	if err != nil {
		fmt.Print("could not create someicon.png")
		os.Exit(1)
	}

	png.Encode(destWriter, dest)

	// Let's see if we can paint the image we generated above to a window.

	win := X.Conn().NewId()
	gc := X.Conn().NewId()
	scrn := X.Conn().DefaultScreen()

	cursor := xcursor.CreateCursor(X, xcursor.Fleur)

	winMask := uint32(xgb.CWBackPixmap | xgb.CWOverrideRedirect |
		xgb.CWBackPixel | xgb.CWCursor)
	winVals := []uint32{xgb.BackPixmapParentRelative, scrn.BlackPixel,
		1, uint32(cursor)}
	X.Conn().CreateWindow(scrn.RootDepth, win, X.RootWin(), 100, 400,
		uint16(width), uint16(height),
		0, xgb.WindowClassInputOutput, scrn.RootVisual,
		winMask, winVals)
	X.Conn().CreateGC(gc, X.RootWin(), xgb.GCForeground,
		[]uint32{scrn.WhitePixel})
	X.Conn().MapWindow(win)

	// try paitning the image we created above...
	// First we have to transform the image into X format. (BGRA)
	// Then we have to allocate resources for the pixmap.
	// Then we can paint the pixmap.
	// Finally, we attach that pixmap as the "BackPixmap" of our window above.
	// (And free pixmap right thereafter, of course.)
	imgData := make([]byte, width*height*4)
	for x := 0; x < width; x++ {
		for y := 0; y < height; y++ {
			r, g, b, a := dest.At(x, y).RGBA()
			i := 4 * (x + (y * height))
			imgData[i+0] = byte(b)
			imgData[i+1] = byte(g)
			imgData[i+2] = byte(r)
			imgData[i+3] = byte(a)
		}
	}
	pix := X.Conn().NewId()
	X.Conn().CreatePixmap(scrn.RootDepth, pix, X.RootWin(),
		uint16(width), uint16(height))
	X.Conn().PutImage(xgb.ImageFormatZPixmap, pix, gc,
		uint16(width), uint16(height), 0, 0, 0, 24, imgData)
	X.Conn().ChangeWindowAttributes(win, uint32(xgb.CWBackPixmap),
		[]uint32{uint32(pix)})
	X.Conn().ClearArea(false, win, 0, 0, 0, 0)
	X.Conn().FreePixmap(pix)

	time.Sleep(20 * time.Second)
}