compiling c++ program calling x11 on windows

I think I have a c++ program (I thought it was c#) that was written to run on a unix based system. It includes X11 calls. Is there a way for me to compile it to run on a pc under windows xp? I have installed mingw.

It is a 'patch' program written to get a piece of hardware (A DreamCheeky usb chessboard) to allow input to the SCID chess database package (I've got that installed in the windows version but the development of it is very much unix /tcl/tk etc)

The program is as follows. The person who wrote it may have time to create a windows version (Or I fear may not) But I am desperate to try to get it to work on my windows laptop.

//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

Jerry Jerry

Answers


mingw will not be able to run it even if it compiles.

You will need cygwin with the X server to run.


Maybe, you should try "Xming" or "Cygwin/X"? I have no experience with it, but it could help.


Need Your Help

PrimeFaces RowEditor - No Icon displayed

css jsf primefaces icons

I'm having trouble using the p:rowEditor from PrimeFaces.

How to build listbox's items with different controls in one listbox

c# xml silverlight windows-phone-7 listbox

In my application i show newsfeed from social network vkontakte (russian facebook:) ). I send request to server and receive an xml file, whitch inludes about 50 newsfeed items. Example of xml (2 it...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.