编译在Windows上调用x11的c ++程序

时间:2010-12-02 15:07:16

标签: c++ windows x11

我认为我有一个c ++程序(我认为它是c#),它是在基于unix的系统上运行的。它包括X11调用。 有没有办法让我编译它在windows xp下的PC上运行? 我安装了mingw。

这是一个'补丁'程序,用于获取一块硬件(一个DreamCheeky usb棋盘)以允许输入到SCID国际象棋数据库包(我已经安装在Windows版本中,但它的开发非常很多unix / tcl / tk等)

该计划如下。 写它的人可能有时间创建一个Windows版本(或者我担心可能不会) 但我迫不及待地想让它在我的Windows笔记本电脑上运行。

//compile with g++ -o monitorcheeky monitorcheeky.c -L/usr/X11R6/lib -lX11

#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <X11/Xlib.h>
#include <X11/keysym.h>
#include <string.h>

#define KEY_DOWN True
#define KEY_UP False
#define KEYCODE_a 38
#define KEYCODE_b 56
#define KEYCODE_c 54
#define KEYCODE_d 40
#define KEYCODE_e 26
#define KEYCODE_f 41
#define KEYCODE_g 42
#define KEYCODE_h 43
#define KEYCODE_1 10
#define KEYCODE_2 11
#define KEYCODE_3 12
#define KEYCODE_4 13
#define KEYCODE_5 14
#define KEYCODE_6 15
#define KEYCODE_7 16
#define KEYCODE_8 17
#define KEYCODE_EQUALS 21 
#define KEYCODE_Q 24
#define KEYCODE_R 27
#define KEYCODE_B 56
#define KEYCODE_N 57
#define KEYCODE_RTN 36

Display *dpy;


// Function to create a keyboard event
XKeyEvent createKeyEvent(Display *display, Window &win,
                           Window &winRoot, bool press,
                           int keycode, int modifiers)
{
   XKeyEvent event;

   event.display     = display;
   event.window      = win;
   event.root        = winRoot;
   event.subwindow   = None;
   event.time        = CurrentTime;
   event.x           = 1;
   event.y           = 1;
   event.x_root      = 1;
   event.y_root      = 1;
   event.same_screen = True;
   event.keycode     = keycode;
   event.state       = modifiers;

   if(press)
      event.type = KeyPress;
   else
      event.type = KeyRelease;

return event;
}

int sendKeyPress(Window winRoot, char letter, int revert) {
    Window winFocus;
    int keycode;

    switch( letter ) 
    {
      case 'a':
        keycode = KEYCODE_a;
    break;
      case 'b':
        keycode = KEYCODE_b;
    break;
      case 'c':
        keycode = KEYCODE_c;
    break;
      case 'd':
        keycode = KEYCODE_d;
    break;
      case 'e':
        keycode = KEYCODE_e;
    break;
      case 'f':
        keycode = KEYCODE_f;
    break;
      case 'g':
        keycode = KEYCODE_g;
    break;
      case 'h':
        keycode = KEYCODE_h;
    break;
      case '1':
        keycode = KEYCODE_1;
    break;
      case '2':
        keycode = KEYCODE_2;
    break;
      case '3':
        keycode = KEYCODE_3;
    break;
      case '4':
        keycode = KEYCODE_4;
    break;
      case '5':
        keycode = KEYCODE_5;
    break;
      case '6':
        keycode = KEYCODE_6;
    break;
      case '7':
        keycode = KEYCODE_7;
    break;
      case '8':
        keycode = KEYCODE_8;
    break;
      default :
    keycode = KEYCODE_RTN;
     }

    XGetInputFocus(dpy, &winFocus, &revert);
    // Send a fake key press event to the window.
    XKeyEvent event = createKeyEvent(dpy, winFocus, winRoot, KEY_DOWN, keycode, 0);
    XSendEvent(event.display, event.window, True, KeyPressMask, (XEvent *)&event);
    // Send a fake key release event to the window.
    event = createKeyEvent(dpy, winFocus, winRoot, KEY_UP, keycode, 0);
    XSendEvent(event.display, event.window, True, KeyPressMask, (XEvent *)&event);
    XFlush(dpy);
    return 0;
}

int main() {
  int sockfd;
  int len;
  struct sockaddr_in address;
  int result;
  char input[5];  
  int revert;

  /* init */
  dpy = XOpenDisplay(NULL);
  if (!dpy) return 1;
  // Get the root window for the current display.
  Window winRoot = XDefaultRootWindow(dpy);

  sockfd = socket(AF_INET, SOCK_STREAM, 0);

  address.sin_family = AF_INET;
  address.sin_addr.s_addr = inet_addr("127.0.0.1");
  address.sin_port = htons(8796);
  len = sizeof(address);

  result = connect(sockfd, (struct sockaddr *)&address, len);

  if (result == -1) {
    perror("oops: client1");
    return 1;
  }
  while (1) {
    read(sockfd, input ,5);
  //  printf("%s",input);
  //  fflush(stdin);

    sendKeyPress(winRoot, input[0], revert);
    sendKeyPress(winRoot, input[1], revert);
    sendKeyPress(winRoot, input[2], revert);
    sendKeyPress(winRoot, input[3], revert);
  }

  /* cleanup */
  XCloseDisplay(dpy);
  return 0;
}

thnx all

杰里 杰里

2 个答案:

答案 0 :(得分:1)

即使它编译,mingw也无法运行它。

您需要运行X服务器的cygwin。

答案 1 :(得分:1)

也许,你应该尝试“Xming”或“Cygwin / X”?我没有经验,但它可以帮助。