// A convenience function to grab the KeyboardMapping and ModifierMapping // from X. We need to do this on startup (see Initialize) and whenever we // get a MappingNotify event. func MapsGet(xu *xgbutil.XUtil) (*xproto.GetKeyboardMappingReply, *xproto.GetModifierMappingReply) { min, max := minMaxKeycodeGet(xu) queryKeymap, _ := xproto.GetKeyboardMapping(xu.Conn(), nil, min, byte(max-min+1)) newKeymap, keyErr := queryKeymap.Reply() queryModmap, _ := xproto.GetModifierMapping(xu.Conn(), nil) newModmap, modErr := queryModmap.Reply() // If there are errors, we really need to panic. We just can't do // any key binding without a mapping from the server. if keyErr != nil { panic(fmt.Sprintf("COULD NOT GET KEYBOARD MAPPING: %v\n"+ "THIS IS AN UNRECOVERABLE ERROR.\n", keyErr)) } if modErr != nil { panic(fmt.Sprintf("COULD NOT GET MODIFIER MAPPING: %v\n"+ "THIS IS AN UNRECOVERABLE ERROR.\n", keyErr)) } return newKeymap, newModmap }
// PhyiscalHeads returns the list of heads in a physical ordering. // Namely, left to right then top to bottom. (Defined by (X, Y).) // Xinerama must have been initialized, otherwise the xinerama.QueryScreens // request will panic. // PhysicalHeads also checks to make sure each rectangle has a unique (x, y) // tuple, so as not to return the geometry of cloned displays. // (At present moment, xgbutil initializes Xinerama automatically during // initial connection.) func PhysicalHeads(xu *xgbutil.XUtil) (Heads, error) { query, _ := xinerama.QueryScreens(xu.Conn(), nil) xinfo, err := query.Reply() if err != nil { return nil, err } hds := make(Heads, 0) for _, info := range xinfo.ScreenInfo { head := xrect.New(int(info.XOrg), int(info.YOrg), int(info.Width), int(info.Height)) // Maybe Xinerama is enabled, but we have cloned displays... unique := true for _, h := range hds { if h.X() == head.X() && h.Y() == head.Y() { unique = false break } } if unique { hds = append(hds, head) } } sort.Sort(hds) return hds, nil }
// _NET_DESKTOP_LAYOUT set func DesktopLayoutSet(xu *xgbutil.XUtil, orientation, columns, rows, startingCorner uint) error { return xprop.ChangeProp32(xu, xu.RootWin(), "_NET_DESKTOP_LAYOUT", "CARDINAL", orientation, columns, rows, startingCorner) }
// GrabKeyboard grabs the entire keyboard. // Returns whether GrabStatus is successful and an error if one is reported by // XGB. It is possible to not get an error and the grab to be unsuccessful. // The purpose of 'win' is that after a grab is successful, ALL Key*Events will // be sent to that window. Make sure you have a callback attached :-) func GrabKeyboard(xu *xgbutil.XUtil, win xproto.Window) error { query, _ := xproto.GrabKeyboard(xu.Conn(), nil, false, win, 0, xproto.GrabModeAsync, xproto.GrabModeAsync) reply, err := query.Reply() if err != nil { return fmt.Errorf("GrabKeyboard: Error grabbing keyboard on "+ "window '%x': %s", win, err) } switch reply.Status { case xproto.GrabStatusSuccess: // all is well case xproto.GrabStatusAlreadyGrabbed: return fmt.Errorf("GrabKeyboard: Could not grab keyboard. " + "Status: AlreadyGrabbed.") case xproto.GrabStatusInvalidTime: return fmt.Errorf("GrabKeyboard: Could not grab keyboard. " + "Status: InvalidTime.") case xproto.GrabStatusNotViewable: return fmt.Errorf("GrabKeyboard: Could not grab keyboard. " + "Status: NotViewable.") case xproto.GrabStatusFrozen: return fmt.Errorf("GrabKeyboard: Could not grab keyboard. " + "Status: Frozen.") } return nil }
// Ungrab undoes Grab. It will handle all combinations od modifiers found // in xevent.IgnoreMods. func Ungrab(xu *xgbutil.XUtil, win xproto.Window, mods uint16, key xproto.Keycode) { for _, m := range xevent.IgnoreMods { query, _ := xproto.UngrabKeyChecked(xu.Conn(), nil, key, win, mods|m) query.Check() } }
// sendClientMessages is a goroutine that sends client messages to the root // window. We then listen to them later as a demonstration of responding to // X events. (They are sent with SubstructureNotify and SubstructureRedirect // masks set. So in order to receive them, we'll have to explicitly listen // to events of that type on the root window.) func xSource(X *xgbutil.XUtil) { i := 1 for { ewmh.ClientEvent(X, X.RootWin(), "NOOP", i) i++ time.Sleep(200 * time.Millisecond) } }
// Grab grabs a key with mods on a particular window. // This will also grab all combinations of modifiers found in xevent.IgnoreMods. func Grab(xu *xgbutil.XUtil, win xproto.Window, mods uint16, key xproto.Keycode) { for _, m := range xevent.IgnoreMods { xproto.GrabKey(xu.Conn(), nil, true, win, mods|m, key, xproto.GrabModeAsync, xproto.GrabModeAsync) } }
// Ungrab undoes Grab. It will handle all combinations of modifiers found // in xevent.IgnoreMods. func Ungrab(xu *xgbutil.XUtil, win xproto.Window, mods uint16, button xproto.Button) { for _, m := range xevent.IgnoreMods { query, _ := xproto.UngrabButtonChecked(xu.Conn(), nil, byte(button), win, mods|m) query.Check() } }
// _NET_SUPPORTED set // This will create any atoms in the argument if they don't already exist. func SupportedSet(xu *xgbutil.XUtil, atomNames []string) error { atoms, err := xprop.StrToAtoms(xu, atomNames) if err != nil { return err } return xprop.ChangeProp32(xu, xu.RootWin(), "_NET_SUPPORTED", "ATOM", atoms...) }
// _NET_SHOWING_DESKTOP req func ShowingDesktopReq(xu *xgbutil.XUtil, show bool) error { var showInt uint if show { showInt = 1 } else { showInt = 0 } return ClientEvent(xu, xu.RootWin(), "_NET_SHOWING_DESKTOP", showInt) }
// _NET_DESKTOP_NAMES set func DesktopNamesSet(xu *xgbutil.XUtil, names []string) error { nullterm := make([]byte, 0) for _, name := range names { nullterm = append(nullterm, name...) nullterm = append(nullterm, 0) } return xprop.ChangeProp(xu, xu.RootWin(), 8, "_NET_DESKTOP_NAMES", "UTF8_STRING", nullterm) }
// _NET_DESKTOP_GEOMETRY get func DesktopGeometryGet(xu *xgbutil.XUtil) (*DesktopGeometry, error) { geom, err := xprop.PropValNums(xprop.GetProperty(xu, xu.RootWin(), "_NET_DESKTOP_GEOMETRY")) if err != nil { return nil, err } return &DesktopGeometry{Width: int(geom[0]), Height: int(geom[1])}, nil }
// _NET_WM_HANDLED_ICONS set func WmHandledIconsSet(xu *xgbutil.XUtil, handle bool) error { var handled uint if handle { handled = 1 } else { handled = 0 } return xprop.ChangeProp32(xu, xu.RootWin(), "_NET_WM_HANDLED_ICONS", "CARDINAL", handled) }
// _NET_SHOWING_DESKTOP set func ShowingDesktopSet(xu *xgbutil.XUtil, show bool) error { var showInt uint if show { showInt = 1 } else { showInt = 0 } return xprop.ChangeProp32(xu, xu.RootWin(), "_NET_SHOWING_DESKTOP", "CARDINAL", showInt) }
// _NET_DESKTOP_VIEWPORT set func DesktopViewportSet(xu *xgbutil.XUtil, viewports []DesktopViewport) error { coords := make([]uint, len(viewports)*2) for i, viewport := range viewports { coords[i*2] = uint(viewport.X) coords[i*2+1] = uint(viewport.Y) } return xprop.ChangeProp32(xu, xu.RootWin(), "_NET_DESKTOP_VIEWPORT", "CARDINAL", coords...) }
// compressMotionNotify takes a MotionNotify event, and inspects the event // queue for any future MotionNotify events that can be received without // blocking. The most recent MotionNotify event is then returned. // Note that we need to make sure that the Event, Child, Detail, State, Root // and SameScreen fields are the same to ensure the same window/action is // generating events. That is, we are only compressing the RootX, RootY, // EventX and EventY fields. // This function is not thread safe, since Peek returns a *copy* of the // event queue---which could be out of date by the time we dequeue events. func compressMotionNotify(X *xgbutil.XUtil, ev xevent.MotionNotifyEvent) xevent.MotionNotifyEvent { // We force a round trip request so that we make sure to read all // available events. X.Sync() xevent.Read(X, false) // The most recent MotionNotify event that we'll end up returning. laste := ev // Look through each event in the queue. If it's an event and it matches // all the fields in 'ev' that are detailed above, then set it to 'laste'. // In which case, we'll also dequeue the event, otherwise it will be // processed twice! // N.B. If our only goal was to find the most recent relevant MotionNotify // event, we could traverse the event queue backwards and simply use // the first MotionNotify we see. However, this could potentially leave // other MotionNotify events in the queue, which we *don't* want to be // processed. So we stride along and just pick off MotionNotify events // until we don't see any more. for i, ee := range xevent.Peek(X) { if ee.Err != nil { // This is an error, skip it. continue } // Use type assertion to make sure this is a MotionNotify event. if mn, ok := ee.Event.(xproto.MotionNotifyEvent); ok { // Now make sure all appropriate fields are equivalent. if ev.Event == mn.Event && ev.Child == mn.Child && ev.Detail == mn.Detail && ev.State == mn.State && ev.Root == mn.Root && ev.SameScreen == mn.SameScreen { // Set the most recent/valid motion notify event. laste = xevent.MotionNotifyEvent{&mn} // We cheat and use the stack semantics of defer to dequeue // most recent motion notify events first, so that the indices // don't become invalid. (If we dequeued oldest first, we'd // have to account for all future events shifting to the left // by one.) defer func(i int) { xevent.DequeueAt(X, i) }(i) } } } // This isn't strictly necessary, but is correct. We should update // xgbutil's sense of time with the most recent event processed. // This is typically done in the main event loop, but since we are // subverting the main event loop, we should take care of it. X.TimeSet(laste.Time) return laste }
// _NET_WORKAREA set func WorkareaSet(xu *xgbutil.XUtil, workareas []Workarea) error { rects := make([]uint, len(workareas)*4) for i, workarea := range workareas { rects[i*4+0] = uint(workarea.X) rects[i*4+1] = uint(workarea.Y) rects[i*4+2] = workarea.Width rects[i*4+3] = workarea.Height } return xprop.ChangeProp32(xu, xu.RootWin(), "_NET_WORKAREA", "CARDINAL", rects...) }
// newWindow creates a new window with a random background color. It sets the // WM_PROTOCOLS property to contain the WM_DELETE_WINDOW atom. It also sets // up a ClientMessage event handler so that we know when to destroy the window. // We also set up a mouse binding so that clicking inside a window will // create another one. func newWindow(X *xgbutil.XUtil) { counter++ win, err := xwindow.Generate(X) if err != nil { log.Fatal(err) } // Get a random background color, create the window (ask to receive button // release events while we're at it) and map the window. bgColor := rand.Intn(0xffffff + 1) win.Create(X.RootWin(), 0, 0, 200, 200, xproto.CwBackPixel|xproto.CwEventMask, uint32(bgColor), xproto.EventMaskButtonRelease) // WMGracefulClose does all of the work for us. It sets the appropriate // values for WM_PROTOCOLS, and listens for ClientMessages that implement // the WM_DELETE_WINDOW protocol. When one is found, the provided callback // is executed. win.WMGracefulClose( func(w *xwindow.Window) { // Detach all event handlers. // This should always be done when a window can no longer // receive events. xevent.Detach(w.X, w.Id) mousebind.Detach(w.X, w.Id) w.Destroy() // Exit if there are no more windows left. counter-- if counter == 0 { os.Exit(0) } }) // It's important that the map comes after setting WMGracefulClose, since // the WM isn't obliged to watch updates to the WM_PROTOCOLS property. win.Map() // A mouse binding so that a left click will spawn a new window. // Note that we don't issue a grab here. Typically, window managers will // grab a button press on the client window (which usually activates the // window), so that we'd end up competing with the window manager if we // tried to grab it. // Instead, we set a ButtonRelease mask when creating the window and attach // a mouse binding *without* a grab. err = mousebind.ButtonReleaseFun( func(X *xgbutil.XUtil, ev xevent.ButtonReleaseEvent) { newWindow(X) }).Connect(X, win.Id, "1", false, false) if err != nil { log.Fatal(err) } }
// _NET_SHOWING_DESKTOP get func ShowingDesktopGet(xu *xgbutil.XUtil) (bool, error) { reply, err := xprop.GetProperty(xu, xu.RootWin(), "_NET_SHOWING_DESKTOP") if err != nil { return false, err } val, err := xprop.PropValNum(reply, nil) if err != nil { return false, err } return val == 1, nil }
// Grab grabs a button with mods on a particular window. // Will also grab all combinations of modifiers found in xevent.IgnoreMods // If 'sync' is True, then no further events can be processed until the // grabbing client allows them to be. (Which is done via AllowEvents. Thus, // if sync is True, you *must* make some call to AllowEvents at some // point, or else your client will lock.) func Grab(xu *xgbutil.XUtil, win xproto.Window, mods uint16, button xproto.Button, sync bool) { var pSync byte = xproto.GrabModeAsync if sync { pSync = xproto.GrabModeSync } for _, m := range xevent.IgnoreMods { xproto.GrabButton(xu.Conn(), nil, true, win, pointerMasks, pSync, xproto.GrabModeAsync, 0, 0, byte(button), mods|m) } }
// GrabChecked Grabs a key with mods on a particular window. // This is the same as Grab, except that it issue a checked request. // Which means that an error could be returned and handled on the spot. // (Checked requests are slower than unchecked requests.) // This will also grab all combinations of modifiers found in xevent.IgnoreMods. func GrabChecked(xu *xgbutil.XUtil, win xproto.Window, mods uint16, key xproto.Keycode) error { var err error for _, m := range xevent.IgnoreMods { query, _ := xproto.GrabKeyChecked(xu.Conn(), nil, true, win, mods|m, key, xproto.GrabModeAsync, xproto.GrabModeAsync) err = query.Check() if err != nil { return err } } return nil }
// GrabPointer grabs the entire pointer. // Returns whether GrabStatus is successful and an error if one is reported by // XGB. It is possible to not get an error and the grab to be unsuccessful. // The purpose of 'win' is that after a grab is successful, ALL Button*Events // will be sent to that window. Make sure you have a callback attached :-) func GrabPointer(xu *xgbutil.XUtil, win xproto.Window, confine xproto.Window, cursor xproto.Cursor) (bool, error) { query, _ := xproto.GrabPointer(xu.Conn(), nil, false, win, pointerMasks, xproto.GrabModeAsync, xproto.GrabModeAsync, confine, cursor, 0) reply, err := query.Reply() if err != nil { return false, fmt.Errorf("GrabPointer: Error grabbing pointer on "+ "window '%x': %s", win, err) } return reply.Status == xproto.GrabStatusSuccess, nil }
// updateMaps runs in response to MappingNotify events. // It is responsible for making sure our view of the world's keyboard // and modifier maps is correct. (Pointer mappings should be handled in // a similar callback in the mousebind package.) func updateMaps(xu *xgbutil.XUtil, e xevent.MappingNotifyEvent) { keyMap, modMap := MapsGet(xu) // So we used to go through the old mapping and the new mapping and pick // out precisely where there are changes. But after allowing for a // one-to-many mapping from keysym to keycodes, this process became too // complex. So we're going to bust out our hammer and rebind everything // based on the initial key strings. if e.Request == xproto.MappingKeyboard { // We must ungrab everything first, in case two keys are being swapped. keys := keyKeys(xu) for _, key := range keys { Ungrab(xu, key.Win, key.Mod, key.Code) detach(xu, key.Evtype, key.Win) } // Wipe the slate clean. xu.KeybindsLck.Lock() xu.Keybinds = make(map[xgbutil.KeyKey][]xgbutil.CallbackKey, len(keys)) xu.Keygrabs = make(map[xgbutil.KeyKey]int, len(keys)) keyStrs := xu.Keystrings xu.KeybindsLck.Unlock() // Update our mappings before rebinding. KeyMapSet(xu, keyMap) ModMapSet(xu, modMap) // Now rebind everything in Keystrings for _, ks := range keyStrs { err := connect(xu, ks.Callback, ks.Evtype, ks.Win, ks.Str, ks.Grab, true) if err != nil { xgbutil.Logger.Println(err) } } } else { // We don't have to do something with MappingModifier like we do with // MappingKeyboard. This is due to us requiring that key strings use // modifier names built into X. (i.e., the names seen in the output of // `xmodmap`.) This means that the modifier mappings happen on the X // server side, so we don't *typically* have to care what key is // actually being pressed to trigger a modifier. (There are some // exceptional cases, and when that happens, we simply query on-demand // which keys are modifiers. See the RunKey{Press,Release}Callbacks // functions in keybind/callback.go for the deets.) KeyMapSet(xu, keyMap) ModMapSet(xu, modMap) } }
// dragStep executes the "step" function registered for the current drag. // It also compresses the MotionNotify events. func dragStep(xu *xgbutil.XUtil, ev xevent.MotionNotifyEvent) { // If for whatever reason we don't have any *piece* of a grab, // we've gotta back out. if !mouseDrag(xu) || mouseDragStep(xu) == nil || mouseDragEnd(xu) == nil { dragUngrab(xu) mouseDragStepSet(xu, nil) mouseDragEndSet(xu, nil) return } // The most recent MotionNotify event that we'll end up returning. laste := ev // We force a round trip request so that we make sure to read all // available events. xu.Sync() xevent.Read(xu, false) // Compress MotionNotify events. for i, ee := range xevent.Peek(xu) { if ee.Err != nil { // This is an error, skip it. continue } // Use type assertion to make sure this is a MotionNotify event. if mn, ok := ee.Event.(xproto.MotionNotifyEvent); ok { // Now make sure all appropriate fields are equivalent. if ev.Event == mn.Event && ev.Child == mn.Child && ev.Detail == mn.Detail && ev.State == mn.State && ev.Root == mn.Root && ev.SameScreen == mn.SameScreen { // Set the most recent/valid motion notify event. laste = xevent.MotionNotifyEvent{&mn} // We cheat and use the stack semantics of defer to dequeue // most recent motion notify events first, so that the indices // don't become invalid. (If we dequeued oldest first, we'd // have to account for all future events shifting to the left // by one.) defer func(i int) { xevent.DequeueAt(xu, i) }(i) } } } xu.TimeSet(laste.Time) // now actually run the step mouseDragStep(xu)(xu, int(laste.RootX), int(laste.RootY), int(laste.EventX), int(laste.EventY)) }
// _NET_DESKTOP_VIEWPORT get func DesktopViewportGet(xu *xgbutil.XUtil) ([]DesktopViewport, error) { coords, err := xprop.PropValNums(xprop.GetProperty(xu, xu.RootWin(), "_NET_DESKTOP_VIEWPORT")) if err != nil { return nil, err } viewports := make([]DesktopViewport, len(coords)/2) for i, _ := range viewports { viewports[i] = DesktopViewport{ X: int(coords[i*2]), Y: int(coords[i*2+1]), } } return viewports, nil }
// newWindow creates a new window that listens to MotionNotify events with // the given backgroundcolor. func newWindow(X *xgbutil.XUtil, color uint32) *xwindow.Window { win, err := xwindow.Generate(X) if err != nil { log.Fatal(err) } err = win.CreateChecked(X.RootWin(), 0, 0, 400, 400, xproto.CwBackPixel|xproto.CwEventMask, color, xproto.EventMaskPointerMotion) if err != nil { log.Fatal(err) } win.Map() return win }
// Dequeue pops an event/error from the queue and returns it. // The queue item is unwrapped and returned as multiple return values. // Only one of the return values can be nil. func Dequeue(xu *xgbutil.XUtil) (xgb.Event, xgb.Error) { xu.EvqueueLck.Lock() defer xu.EvqueueLck.Unlock() everr := xu.Evqueue[0] xu.Evqueue = xu.Evqueue[1:] return everr.Event, everr.Err }
// Drag is the public interface that will make the appropriate connections // to register a drag event for three functions: the begin function, the // step function and the end function. // The 'grabwin' is the window that the grab is placed on (and therefore the // window where all button events are redirected to after the drag has started), // and the 'win' is the window that the initial 'begin' callback is set on. // In typical use cases, these windows should be the same. // If 'grab' is false, then no pointer grab is issued. func Drag(xu *xgbutil.XUtil, grabwin xproto.Window, win xproto.Window, buttonStr string, grab bool, begin xgbutil.MouseDragBeginFun, step xgbutil.MouseDragFun, end xgbutil.MouseDragFun) { ButtonPressFun( func(xu *xgbutil.XUtil, ev xevent.ButtonPressEvent) { dragBegin(xu, ev, grabwin, win, begin, step, end) }).Connect(xu, win, buttonStr, false, grab) // If the grab win isn't the dummy, then setup event handlers for the // grab window. if grabwin != xu.Dummy() { xevent.MotionNotifyFun(dragStep).Connect(xu, grabwin) xevent.ButtonReleaseFun(dragEnd).Connect(xu, grabwin) } }
// Enqueue queues up an event read from X. // Note that an event read may return an error, in which case, this queue // entry will be an error and not an event. // // ev, err := XUtilValue.Conn().WaitForEvent() // xevent.Enqueue(XUtilValue, ev, err) // // You probably shouldn't have to enqueue events yourself. This is done // automatically if you're using xevent.Main{Ping} and/or xevent.Read. func Enqueue(xu *xgbutil.XUtil, ev xgb.Event, err xgb.Error) { xu.EvqueueLck.Lock() defer xu.EvqueueLck.Unlock() xu.Evqueue = append(xu.Evqueue, xgbutil.EventOrError{ Event: ev, Err: err, }) }
// dragGrab is a shortcut for grabbing the pointer for a drag. func dragGrab(xu *xgbutil.XUtil, grabwin xproto.Window, win xproto.Window, cursor xproto.Cursor) bool { status, err := GrabPointer(xu, grabwin, xu.RootWin(), cursor) if err != nil { xgbutil.Logger.Printf("Mouse dragging was unsuccessful because: %v", err) return false } if !status { xgbutil.Logger.Println("Mouse dragging was unsuccessful because " + "we could not establish a pointer grab.") return false } mouseDragSet(xu, true) return true }