HelloWorldX11.cxx 2.91 KB
Newer Older
Bill Hoffman's avatar
Bill Hoffman committed
1 2 3 4 5 6 7 8 9 10 11

/*** START MAIN.H ***/
/* http://www.geocities.com/jeff_louie/x11/helloworld.htm* */
/*
 *  main.h
 *  TestX
 *
 *  Created by Jeff Louie on Tue Feb 03 2004.
 *  Copyright (c) 2004 __MyCompanyName__. All rights reserved.
 *
 */
12

Bill Hoffman's avatar
Bill Hoffman committed
13 14 15 16 17

#ifndef MAIN_H
#define MAIN_H 1

#include <iostream>
18
#include <stdlib.h>
Bill Hoffman's avatar
Bill Hoffman committed
19 20 21 22 23 24 25 26 27 28 29 30

/* include the X library headers */
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xos.h>

class Main {

public:
  // constructor
  Main(int argc, char * const argv[]);
  //virtual ~Main();
31

Bill Hoffman's avatar
Bill Hoffman committed
32 33

private:
34

Bill Hoffman's avatar
Bill Hoffman committed
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55

  /* here are our X variables */
  Display *dis;
  int screen;
  Window win;
  GC gc;

  /* here are our X routines declared! */
  void init_x();
  void close_x();
  void redraw();
  int delay(int i);

};

#endif

/*** END MAIN.H ***/

/*** START MAIN.CPP ***/

56
// modified from Brian Hammond's Howdy program at
Bill Hoffman's avatar
Bill Hoffman committed
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
// http://www.insanityengine.com/doc/x11/xintro.html
// jeff louie 02.05.2004



int main (int argc, char * const argv[]) {
  Main m(argc, argv);
  return 0;
}

//Main::~Main() {;};
Main::Main (int argc, char * const argv[]) {
  XEvent event;           // XEvent declaration
  KeySym key;             // KeyPress Events
  char text[255];         // char buffer for KeyPress Events

  init_x();

  // event loop
76
  while(1) {
Bill Hoffman's avatar
Bill Hoffman committed
77 78 79
  // get the next event and stuff it into our event variable.
  // Note:  only events we set the mask for are detected!
  XNextEvent(dis, &event);
80

Bill Hoffman's avatar
Bill Hoffman committed
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117

  switch (event.type) {
  int x;
  int y;
  case Expose:
    if (event.xexpose.count==0) {
    redraw();
    }
    break;
  case KeyPress:
    if (XLookupString(&event.xkey,text,255,&key,0)==1) {
    // use the XLookupString routine to convert the invent
    // KeyPress data into regular text.  Weird but necessary...
    if ((text[0]=='q') || (text[0]=='Q')) {
    close_x();
    }
    else {
    // echo key press
    printf("You pressed the %c key!\n",text[0]);
    }
    }
    break;
  case ButtonPress:
    // get cursor position
    x= event.xbutton.x;
    y= event.xbutton.y;
    strcpy(text,"X is FUN!");
    XSetForeground(dis,gc,rand()%event.xbutton.x%255);
    // draw text at cursor
    XDrawString(dis,win,gc,x,y, text, strlen(text));
    break;
  default:
    printf("Unhandled event.\n");
  }
  }
}

118
void Main::init_x() {
Bill Hoffman's avatar
Bill Hoffman committed
119 120 121 122 123 124
  unsigned long black,white;

  dis=XOpenDisplay(NULL);
  screen=DefaultScreen(dis);
  black=BlackPixel(dis,screen),
    white=WhitePixel(dis, screen);
125
  win=XCreateSimpleWindow(dis,DefaultRootWindow(dis),0,0,
Bill Hoffman's avatar
Bill Hoffman committed
126 127 128 129
                          300, 300, 5,black, white);
  XSetStandardProperties(dis,win,"Hello World","Hi",None,NULL,0,NULL);
  XSelectInput(dis, win, ExposureMask|ButtonPressMask|KeyPressMask);
  // get Graphics Context
130
  gc=XCreateGC(dis, win, 0,0);
Bill Hoffman's avatar
Bill Hoffman committed
131 132 133 134 135 136 137 138 139
  XSetBackground(dis,gc,white);
  XSetForeground(dis,gc,black);
  XClearWindow(dis, win);
  XMapRaised(dis, win);
};

void Main::close_x() {
  XFreeGC(dis, gc);
  XDestroyWindow(dis,win);
140 141
  XCloseDisplay(dis);
  exit(1);
Bill Hoffman's avatar
Bill Hoffman committed
142 143 144 145 146
};

void Main::redraw() {
  XClearWindow(dis, win);
};