示例#1
0
func grabKeyboardAndMouse(m *Manager) {
	if m == nil {
		return
	}

	//go func() {
	X, err := xgbutil.NewConn()
	if err != nil {
		logger.Info("Get New Connection Failed:", err)
		return
	}
	keybind.Initialize(X)
	mousebind.Initialize(X)

	err = keybind.GrabKeyboard(X, X.RootWin())
	if err != nil {
		logger.Info("Grab Keyboard Failed:", err)
		return
	}

	grabAllMouseButton(X)

	xevent.ButtonPressFun(
		func(X *xgbutil.XUtil, e xevent.ButtonPressEvent) {
			dbus.Emit(m, "KeyReleaseEvent", "")
			ungrabAllMouseButton(X)
			keybind.UngrabKeyboard(X)
			logger.Info("Button Press Event")
			xevent.Quit(X)
		}).Connect(X, X.RootWin())

	xevent.KeyPressFun(
		func(X *xgbutil.XUtil, e xevent.KeyPressEvent) {
			value := parseKeyEnvent(X, e.State, e.Detail)
			pressKeyStr = value
			dbus.Emit(m, "KeyPressEvent", value)
		}).Connect(X, X.RootWin())

	xevent.KeyReleaseFun(
		func(X *xgbutil.XUtil, e xevent.KeyReleaseEvent) {
			if strings.ToLower(pressKeyStr) == "super_l" ||
				strings.ToLower(pressKeyStr) == "super_r" {
				pressKeyStr = "Super"
			}

			dbus.Emit(m, "KeyReleaseEvent", pressKeyStr)
			pressKeyStr = ""
			ungrabAllMouseButton(X)
			keybind.UngrabKeyboard(X)
			logger.Infof("Key: %s\n", pressKeyStr)
			xevent.Quit(X)
		}).Connect(X, X.RootWin())

	xevent.Main(X)
	//}()
}
示例#2
0
// connect is essentially 'Connect' for either ButtonPress or
// ButtonRelease events.
func connect(xu *xgbutil.XUtil, callback xgbutil.CallbackMouse, evtype int,
	win xproto.Window, buttonStr string, sync bool, grab bool) error {

	// Get the mods/button first
	mods, button, err := ParseString(xu, buttonStr)
	if err != nil {
		return err
	}

	// Only do the grab if we haven't yet on this window.
	// And if we WANT a grab...
	if grab && mouseBindGrabs(xu, evtype, win, mods, button) == 0 {
		err := GrabChecked(xu, win, mods, button, sync)
		if err != nil {
			// If a bad access, let's be nice and give a good error message.
			switch err.(type) {
			case xproto.AccessError:
				return fmt.Errorf("Got a bad access error when trying to bind "+
					"'%s'. This usually means another client has already "+
					"grabbed this mouse binding.", buttonStr)
			default:
				return fmt.Errorf("Could not bind '%s' because: %s",
					buttonStr, err)
			}
		}
	}

	// If we've never grabbed anything on this window before, we need to
	// make sure we can respond to it in the main event loop.
	var allCb xgbutil.Callback
	if evtype == xevent.ButtonPress {
		allCb = xevent.ButtonPressFun(runButtonPressCallbacks)
	} else {
		allCb = xevent.ButtonReleaseFun(runButtonReleaseCallbacks)
	}

	// If this is the first Button{Press|Release}Event on this window,
	// then we need to listen to Button{Press|Release} events in the main loop.
	if !connectedMouseBind(xu, evtype, win) {
		allCb.Connect(xu, win)
	}

	// Finally, attach the callback.
	attachMouseBindCallback(xu, evtype, win, mods, button, callback)

	return nil
}