Esempio n. 1
0
func main() {
	display := new(Display)
	display.display = gowl.NewDisplay()
	display.registry = gowl.NewRegistry()

	err := display.display.Connect()
	if err != nil {
		fmt.Println("Couldn't connect:", err)
		return
	}

	display.compositor = gowl.NewCompositor()
	display.shm = gowl.NewShm()
	display.shell = gowl.NewShell()
	display.pool = gowl.NewShmPool()
	display.buffer = gowl.NewBuffer()
	display.surface = gowl.NewSurface()
	display.shell_surface = gowl.NewShellSurface()

	display.display.GetRegistry(display.registry)
	globchan := make(chan gowl.RegistryGlobal)
	go display.globalListener(globchan)
	display.registry.AddGlobalListener(globchan)
	waitForSync(display.display)

	// create pool
	fd := gowl.CreateTmp(250 * 250 * 4)
	mmap, err := syscall.Mmap(int(fd), 0, 250000, syscall.PROT_READ|syscall.PROT_WRITE, syscall.MAP_SHARED)
	if err != nil {
		fmt.Println(err)
	}
	display.data = mmap
	col = 0
	add = 1
	display.shm.CreatePool(display.pool, fd, 2500000)
	display.pool.CreateBuffer(display.buffer, 0, 250, 250, 1000, 1)
	display.pool.Destroy()

	// Create surfaces
	display.compositor.CreateSurface(display.surface)
	display.shell.GetShellSurface(display.shell_surface, display.surface)
	go Pong(display.shell_surface)
	display.shell_surface.SetToplevel()
	display.shell_surface.SetTitle("Gowl test window")

	display.surface.Attach(display.buffer, 0, 0)
	redraw(display)

	display.buffer.Destroy()
	display.surface.Destroy()
}
Esempio n. 2
0
func NewWindow(width, height int) (w *Window, err error) {
	w = new(Window)
	w.eventchan = make(chan interface{})

	// Create display and connect to wayland server
	w.display = gowl.NewDisplay()
	err = w.display.Connect()
	if err != nil {
		w = nil
		return
	}

	// Allocate other components
	w.compositor = gowl.NewCompositor()
	w.surface = gowl.NewSurface()
	w.shell = gowl.NewShell()
	w.shellsurface = gowl.NewShellSurface()

	w.shm = gowl.NewShm()
	w.pool = gowl.NewShmPool()
	w.buffer = gowl.NewBuffer()

	w.seat = gowl.NewSeat()
	w.pointer = gowl.NewPointer()
	w.keyboard = gowl.NewKeyboard()
	w.ddm = gowl.NewDataDeviceManager()
	w.dd = gowl.NewDataDevice()

	// Listen for global events from display
	globals := make(chan interface{})
	go func() {
		for event := range globals {
			global := event.(gowl.DisplayGlobal)
			switch strings.TrimSpace(global.Iface) {
			case "wl_compositor":
				w.display.Bind(global.Name, global.Iface, global.Version, w.compositor)
			case "wl_shm":
				w.display.Bind(global.Name, global.Iface, global.Version, w.shm)
			case "wl_shell":
				w.display.Bind(global.Name, global.Iface, global.Version, w.shell)
			case "wl_seat":
				w.display.Bind(global.Name, global.Iface, global.Version, w.seat)
				w.ddm.GetDataDevice(w.dd, w.seat)
				w.seat.GetPointer(w.pointer)
			case "wl_data_device_manager":
				w.display.Bind(global.Name, global.Iface, global.Version, w.ddm)
			}
		}
	}()
	w.display.AddGlobalListener(globals)

	// Iterate until we are sync'ed
	err = w.display.Iterate()
	if err != nil {
		w = nil
		return
	}
	waitForSync(w.display)

	// Create memory map
	w.screen = image.NewRGBA(image.Rect(0, 0, width, height))
	size := w.screen.Stride * w.screen.Rect.Dy()
	fd := gowl.CreateTmp(int64(size))
	mmap, err := syscall.Mmap(int(fd), 0, size, syscall.PROT_READ|syscall.PROT_WRITE, syscall.MAP_SHARED)
	if err != nil {
		w = nil
		return
	}
	w.screen.Pix = mmap

	// Create pool and buffer
	w.shm.CreatePool(w.pool, fd, int32(size))
	w.pool.CreateBuffer(w.buffer, 0, int32(width), int32(height), int32(w.screen.Stride), 1) // 1 = RGBA format
	w.pool.Destroy()

	// Ask compositor to create surface
	w.compositor.CreateSurface(w.surface)
	w.shell.GetShellSurface(w.shellsurface, w.surface)
	w.shellsurface.SetToplevel()

	// Make shell surface respond to pings
	pings := make(chan interface{})
	w.shellsurface.AddPingListener(pings)
	go func() {
		for p := range pings {
			ping := p.(gowl.ShellSurfacePing)
			w.shellsurface.Pong(ping.Serial)
		}
	}()

	go handleEvents(w)

	// Iterate
	go func() {
		for {
			w.display.Iterate()
		}
	}()

	return
}