2top

Mirrored from GitHub

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


2top.go

package main

import (
	"fmt"
	"os"
	"strconv"

	"github.com/BurntSushi/xgb"
	"github.com/BurntSushi/xgb/xproto"
)

func main() {
	if len(os.Args) != 2 {
		fmt.Println("Usage:\n\ttotop <window id>")
		os.Exit(1)
	}

	display, err := xgb.NewConn()
	if err != nil {
		fmt.Fprintf(os.Stderr, "Failed to open display: %v\n", err)
		os.Exit(1)
	}
	defer display.Close()

	id, err := strconv.ParseUint(os.Args[1], 0, 32) // Use 0 for automatic base detection
	if err != nil {
	    fmt.Fprintf(os.Stderr, "Invalid window ID: %v\n", err)
	    os.Exit(1)
	}

	window := xproto.Window(id)

	xproto.ConfigureWindow(display, window, xproto.ConfigWindowStackMode, []uint32{xproto.StackModeAbove})
	xproto.SetInputFocus(display, xproto.InputFocusNone, window, xproto.TimeCurrentTime)
}

README.md

# 2top

Provided is my Go rewrite, stup and brup are example scripts to raise 'st' and 'chromium' make these bindable

To compile run c++ totop.cpp -L/usr/X11R6/lib -lX11 -o totop

The shell script, id=$(xwininfo -name "st" | grep id: | awk "{ print \$4 }") && totop $id

Alternatively you can ommit the second line, and only use the third with the id if you know it

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

#!/bin/sh

window_id=$(xwininfo -root -tree | grep -i 'chromium' | awk '{print $1}')

# Check if we found any window
if [ -n "$window_id" ]; then
  # Raise the window using totop
  totop "$window_id"
else
  echo "No window with name 'st' found."
fi

go.mod

module 2top

go 1.23.4

require github.com/BurntSushi/xgb v0.0.0-20210121224620-deaf085860bc

go.sum

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

stup

#!/bin/sh

window_id=$(xwininfo -root -tree | grep -Ei '(^|[^a-zA-Z])st([^a-zA-Z]|$)' | awk '{print $1}')

# Check if we found any window
if [ -n "$window_id" ]; then
  # Raise the window using totop
  totop "$window_id"
else
  echo "No window with name 'st' found."
fi

totop.cpp

#include <stdio.h>
#include <stdlib.h>
#include <X11/Xlib.h>

int main(int argc, char **argv)
{
if ( argc != 2 ) {
printf("Usage:\n\ttotop <window id>\n");
return 1;
}

Display *dsp = XOpenDisplay(NULL);

long id = strtol(argv[1], NULL, 16);
XRaiseWindow ( dsp, id );
XSetInputFocus ( dsp, id, RevertToNone, CurrentTime );

XCloseDisplay ( dsp );

return 0;
}