Mirrored from GitHub

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


2top.go

1	package main
2	
3	import (
4		"fmt"
5		"os"
6		"strconv"
7	
8		"github.com/BurntSushi/xgb"
9		"github.com/BurntSushi/xgb/xproto"
10	)
11	
12	func 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	
3	Provided is my Go rewrite, stup and brup are example scripts to raise 'st' and 'chromium' make these bindable
4	
5	To compile run c++ totop.cpp -L/usr/X11R6/lib -lX11 -o totop
6	
7	The shell script, id=$(xwininfo -name "st" | grep id: | awk "{ print \$4 }") && totop $id
8	
9	Alternatively you can ommit the second line, and only use the third with the id if you know it
10	
11	Credit 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	
3	window_id=$(xwininfo -root -tree | grep -i 'chromium' | awk '{print $1}')
4	
5	# Check if we found any window
6	if [ -n "$window_id" ]; then
7	  # Raise the window using totop
8	  totop "$window_id"
9	else
10	  echo "No window with name 'st' found."
11	fi

go.mod

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

go.sum

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

stup

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

totop.cpp

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