Пример #1
0
// commandHandler responds to client message events that issue commands.
func commandHandler(X *xgbutil.XUtil, cm xevent.ClientMessageEvent) {
	typeName, err := xprop.AtomName(X, cm.Type)
	if err != nil {
		logger.Warning.Printf(
			"Could not get type of ClientMessage event: %s", cm)
		return
	}

	if typeName == "_WINGO_CMD" {
		cmd, err := cmdusage.CmdGet(X)
		if err != nil {
			logger.Warning.Printf("Could not get _WINGO_CMD value: %s", err)
			return
		}

		// Blank out the command
		cmdusage.CmdSet(X, "")

		// Parse the command
		cmdName, args := commandParse(cmd)
		cmdFun := commandFun("", cmdName, args...)
		if cmdFun != nil {
			cmdFun()
			cmdusage.StatusSet(X, true)
		} else {
			cmdusage.StatusSet(X, false)
		}
	}
}
Пример #2
0
func main() {
	X, err := xgbutil.Dial("")
	if err != nil {
		logger.Error.Println(err)
		logger.Error.Println("Error connecting to X, quitting...")
		return
	}
	defer X.Conn().Close()

	// Get command from arguments
	flag.Parse()
	if flag.NArg() < 1 {
		fmt.Fprintln(os.Stderr, "Usage: wingo-cmd CommandName [CommandArgs]")
		return
	}

	commandPieces := flag.Args()
	cmdName := commandPieces[0]
	cmdFull := strings.Join(commandPieces, " ")

	// make sure we start with failure
	cmdusage.StatusSet(X, false)
	success := false

	// Set the command before sending request to run command.
	err = cmdusage.CmdSet(X, cmdFull)
	if err != nil {
		logger.Error.Printf("Could not set command: %s", err)
		return
	}

	// Issue the command!
	ewmh.ClientEvent(X, X.RootWin(), "_WINGO_CMD")

	// Now let's set up a handler to detect when the status changes
	xevent.PropertyNotifyFun(
		func(X *xgbutil.XUtil, ev xevent.PropertyNotifyEvent) {
			name, err := xprop.AtomName(X, ev.Atom)
			if err != nil {
				logger.Warning.Println(
					"Could not get property atom name for", ev.Atom)
				return
			}

			if name == "_WINGO_CMD_STATUS" {
				success = cmdusage.StatusGet(X)
				if success {
					os.Exit(0)
				} else {
					logger.Warning.Printf("Error running '%s'", cmdFull)
					cmdusage.ShowUsage(cmdName)
					os.Exit(1)
				}
			}
		}).Connect(X, X.RootWin())

	// Listen to Root property change events
	xwindow.Listen(X, X.RootWin(), xproto.EventMaskPropertyChange)

	go xevent.Main(X)

	time.Sleep(time.Second * 5)

	logger.Error.Println(
		"Timed out while trying to issue command to Wingo. " +
			"Are you sure Wingo is running?")
	os.Exit(1)
}