Mirrored from GitHub

Jump to: 2top.go README.md brup go.mod go.sum stup totop.cpp


2top.go

1package main
2
3import (
4	"fmt"
5	"os"
6	"strconv"
7
8	"github.com/BurntSushi/xgb"
9	"github.com/BurntSushi/xgb/xproto"
10)
11
12func main() {
13	if len(os.Args) != 2 {
14		fmt.Println("Usage:\n\ttotop <window id>")
15		os.Exit(1)
16	}
17
18	display, err := xgb.NewConn()
19	if err != nil {
20		fmt.Fprintf(os.Stderr, "Failed to open display: %v\n", err)
21		os.Exit(1)
22	}
23	defer display.Close()
24
25	id, err := strconv.ParseUint(os.Args[1], 0, 32) // Use 0 for automatic base detection
26	if err != nil {
27	    fmt.Fprintf(os.Stderr, "Invalid window ID: %v\n", err)
28	    os.Exit(1)
29	}
30
31	window := xproto.Window(id)
32
33	xproto.ConfigureWindow(display, window, xproto.ConfigWindowStackMode, []uint32{xproto.StackModeAbove})
34	xproto.SetInputFocus(display, xproto.InputFocusNone, window, xproto.TimeCurrentTime)
35}

README.md

1# 2top
2
3Provided is my Go rewrite, stup and brup are example scripts to raise 'st' and 'chromium' make these bindable
4
5To compile run c++ totop.cpp -L/usr/X11R6/lib -lX11 -o totop
6
7The shell script, id=$(xwininfo -name "st" | grep id: | awk "{ print \$4 }") && totop $id
8
9Alternatively you can ommit the second line, and only use the third with the id if you know it
10
11Credit to original cpp [author](https://www.linuxquestions.org/questions/linux-general-1/how-to-bring-up-application-window-to-front-from-shell-script-83545/)

brup

1#!/bin/sh
2
3window_id=$(xwininfo -root -tree | grep -i 'chromium' | awk '{print $1}')
4
5# Check if we found any window
6if [ -n "$window_id" ]; then
7  # Raise the window using totop
8  totop "$window_id"
9else
10  echo "No window with name 'st' found."
11fi

go.mod

1module 2top
2
3go 1.23.4
4
5require github.com/BurntSushi/xgb v0.0.0-20210121224620-deaf085860bc

go.sum

1github.com/BurntSushi/xgb v0.0.0-20210121224620-deaf085860bc h1:7D+Bh06CRPCJO3gr2F7h1sriovOZ8BMhca2Rg85c2nk=
2github.com/BurntSushi/xgb v0.0.0-20210121224620-deaf085860bc/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=

stup

1#!/bin/sh
2
3window_id=$(xwininfo -root -tree | grep -Ei '(^|[^a-zA-Z])st([^a-zA-Z]|$)' | awk '{print $1}')
4
5# Check if we found any window
6if [ -n "$window_id" ]; then
7  # Raise the window using totop
8  totop "$window_id"
9else
10  echo "No window with name 'st' found."
11fi

totop.cpp

1#include <stdio.h>
2#include <stdlib.h>
3#include <X11/Xlib.h>
4
5int main(int argc, char **argv)
6{
7if ( argc != 2 ) {
8printf("Usage:\n\ttotop <window id>\n");
9return 1;
10}
11
12Display *dsp = XOpenDisplay(NULL);
13
14long id = strtol(argv[1], NULL, 16);
15XRaiseWindow ( dsp, id );
16XSetInputFocus ( dsp, id, RevertToNone, CurrentTime );
17
18XCloseDisplay ( dsp );
19
20return 0;
21}