doumi9618 2015-12-12 19:05
浏览 101

将Go与cgo和Xlib一起使用时出错

I trying to rewrite C-code to Go with help of cgo, but when trying to run the go-code it gives me some errors, which I don't know how to solve. The code allows to get coordinates of mouse clicks on a Linux-desktop.

C code:

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

int main () {
    int x=-1, y=-1;
    XEvent event;
    int button;
    Display *display = XOpenDisplay(NULL);
    if (display == NULL) {
    fprintf(stderr, "Cannot connect to X server!
");
    exit (EXIT_FAILURE);
    }
    Window root= XDefaultRootWindow(display);
    XGrabPointer(display, root, False, ButtonPressMask, GrabModeAsync,
         GrabModeAsync, None, None, CurrentTime);
    while(1){
    XSelectInput(display, root, ButtonReleaseMask);
    while(1) {
      XNextEvent(display,&event);
      switch(event.type) {
    case ButtonPress:
        switch(event.xbutton.button) {
          case Button1:
          x=event.xbutton.x;
          y=event.xbutton.y;
          button=Button1;
          break;

          case Button3:
          x=event.xbutton.x;
          y=event.xbutton.y;
          button=Button3;
          break;
          default:
          break;

        }
        break;
    default:
        break;
      }
      if(x>=0 && y>=0)break;
    }

    if(button==Button1)printf("leftclick at %d %d 
",x,y);
    else printf("rightclick at %d %d 
",x,y);}
    XCloseDisplay(display);
    return 0;
}

Go code:

package main

// #cgo LDFLAGS: -lX11
// #include <X11/Xlib.h>
// #include <X11/Xutil.h>
import "C"

import (
    "fmt"
)

func main() {
    var x = -1
    var y = -1
    var event C.XEvent
    var button int
    var display = C.XOpenDisplay(nil)
    if display == nil {
        panic("Cannot connect to X server!
")
    }

    var root = C.XDefaultRootWindow(display);
    C.XGrabPointer(display, root, C.False, C.ButtonPressMask, C.GrabModeAsync, 
        C.GrabModeAsync, C.None, C.None, C.CurrentTime)
    for {
        C.XSelectInput(display, root, C.ButtonReleaseMask)
        for {
            C.XNextEvent(display, &event)
            switch(C.event.type) {
                case C.ButtonPress:
                    switch C.event.xbutton.button  {
                        case C.Button1:
                            x=C.event.xbutton.x
                            y=C.event.xbutton.y
                            button=C.Button1
                            break

                        case Button3:
                            x=C.event.xbutton.x
                            y=C.event.xbutton.y
                            button=C.Button3
                            break
                            default:
                                break

                    }
                    break
                default:
                    break
            }
            if(x>=0 && y>=0) {
                break
            }
        }
        if(button==Button1) {
            fmt.Printf("leftclick at %d %d 
",x,y)
        }
        else  {
            fmt.Printf("rightclick at %d %d 
",x,y)
        }
    }
    C.XCloseDisplay(display)
}

Errors in Go code:

go run g.go
# command-line-arguments
./g.go:29:19: expected selector or type assertion, found 'type'
./g.go:58:3: expected statement, found 'else'
./g.go:63:3: expected '}', found 'EOF'
  • 写回答

1条回答 默认 最新

  • doupafu6980 2015-12-12 20:03
    关注

    For the error about "type", that is a reserved word in go so it is not directly accessible.

    According to this posting you should change your reference to:

    switch C.event._type {
    

    Also, else cannot be on a line on it's own. Change it to

    if button == Button1 {
        fmt.Printf("leftclick at %d %d 
    ", x, y)
    } else {
        fmt.Printf("rightclick at %d %d 
    ", x, y)
    }
    

    Switch statements don't fallthrough in go, so your break statements are unnecessary.

    Lastly, make sure to run go fmt on your code, you are using parens where they are not needed.

    Here's a formatted version with breaks removed that should work for you (I did not test this code):

    package main
    
    // #cgo LDFLAGS: -lX11
    // #include <X11/Xlib.h>
    // #include <X11/Xutil.h>
    import "C"
    
    import (
        "fmt"
    )
    
    func main() {
        var x = -1
        var y = -1
        var event C.XEvent
        var button int
        var display = C.XOpenDisplay(nil)
        if display == nil {
            panic("Cannot connect to X server!
    ")
        }
    
        var root = C.XDefaultRootWindow(display)
        C.XGrabPointer(display, root, C.False, C.ButtonPressMask, C.GrabModeAsync,
            C.GrabModeAsync, C.None, C.None, C.CurrentTime)
        for {
            C.XSelectInput(display, root, C.ButtonReleaseMask)
            for {
                C.XNextEvent(display, &event)
                switch C.event._type {
                case C.ButtonPress:
                    switch C.event.xbutton.button {
                    case C.Button1:
                        x = C.event.xbutton.x
                        y = C.event.xbutton.y
                        button = C.Button1
    
                    case Button3:
                        x = C.event.xbutton.x
                        y = C.event.xbutton.y
                        button = C.Button3
                    }
                }
                if x >= 0 && y >= 0 {
                    break
                }
            }
            if button == Button1 {
                fmt.Printf("leftclick at %d %d 
    ", x, y)
            } else {
                fmt.Printf("rightclick at %d %d 
    ", x, y)
            }
        }
        C.XCloseDisplay(display)
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)