Skip to content

Instantly share code, notes, and snippets.

@SeungheonOh
Created February 1, 2020 03:43
Show Gist options
  • Save SeungheonOh/6929185369e134a35fe97e39bcf82388 to your computer and use it in GitHub Desktop.
Save SeungheonOh/6929185369e134a35fe97e39bcf82388 to your computer and use it in GitHub Desktop.
package main
/*
#cgo LDFLAGS: -L/usr/x11/lib -lX11
#cgo CFLAGS: -w
#include <X11/Xlib.h>
#include <unistd.h>
#include <stdio.h>
Window GetRoot(Display *d) {
return DefaultScreen(d);
}
void coords (Display *display, int *x, int *y)
{
XEvent event;
XQueryPointer (display, DefaultRootWindow (display),
&event.xbutton.root, &event.xbutton.window,
&event.xbutton.x_root, &event.xbutton.y_root,
&event.xbutton.x, &event.xbutton.y,
&event.xbutton.state);
*x = event.xbutton.x;
*y = event.xbutton.y;
}
void MoveTo(Display *display, int x, int y) {
int cur_x, cur_y;
coords(display, &cur_x, &cur_y);
XWarpPointer(display, None, None, 0, 0, 0, 0, -cur_x, -cur_y);
XWarpPointer(display, None, None, 0, 0, 0, 0, x, y);
usleep(1);
}
void ClickOn(Display *display) {
XEvent event;
memset (&event, 0, sizeof (event));
event.xbutton.button = Button1;
event.xbutton.same_screen = True;
event.xbutton.subwindow = DefaultRootWindow (display);
while (event.xbutton.subwindow)
{
event.xbutton.window = event.xbutton.subwindow;
XQueryPointer (display, event.xbutton.window,
&event.xbutton.root, &event.xbutton.subwindow,
&event.xbutton.x_root, &event.xbutton.y_root,
&event.xbutton.x, &event.xbutton.y,
&event.xbutton.state);
}
// Press
event.type = ButtonPress;
if (XSendEvent (display, PointerWindow, True, ButtonPressMask, &event) == 0)
fprintf (stderr, "Error to send the event!\n");
XFlush (display);
usleep (1);
// Release
event.type = ButtonRelease;
if (XSendEvent (display, PointerWindow, True, ButtonReleaseMask, &event) == 0)
fprintf (stderr, "Error to send the event!\n");
XFlush (display);
usleep (1);
}
*/
import "C"
import (
"fmt"
"image"
"os"
"strconv"
cv "gocv.io/x/gocv"
)
func main() {
Display := C.XOpenDisplay(nil)
fmt.Println("starting")
x, _ := strconv.Atoi(os.Args[1])
y, _ := strconv.Atoi(os.Args[2])
w, _ := strconv.Atoi(os.Args[3])
h, _ := strconv.Atoi(os.Args[4])
image_file := os.Args[5]
img := cv.NewMat()
img = cv.IMRead(image_file, 0)
cv.Resize(img, &img, image.Point{w, h}, 1, 1, 2)
fmt.Println("resize done")
imgPtr := img.DataPtrUint8()
C.MoveTo(Display, C.int(x), C.int(y))
C.ClickOn(Display)
C.MoveTo(Display, C.int(x+w), C.int(y))
C.ClickOn(Display)
C.MoveTo(Display, C.int(x), C.int(y+h))
C.ClickOn(Display)
C.MoveTo(Display, C.int(x+w), C.int(y+h))
C.ClickOn(Display)
fmt.Println(w, y, w, h)
fmt.Println(img.Rows(), img.Cols())
for i := 0; i < img.Rows(); i += 3 {
for j := 0; j < img.Cols(); j += 3 {
if imgPtr[i*img.Cols()+j] < 10 {
C.MoveTo(Display, C.int(x+j), C.int(y+i))
C.ClickOn(Display)
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment