3 /* http://www.geocities.com/jeff_louie/x11/helloworld.htm* */
8 * Created by Jeff Louie on Tue Feb 03 2004.
9 * Copyright (c) 2004 __MyCompanyName__. All rights reserved.
19 /* include the X library headers */
21 #include <X11/Xutil.h>
28 Main(int argc
, char * const argv
[]);
35 /* here are our X variables */
41 /* here are our X routines declared! */
53 /*** START MAIN.CPP ***/
55 // modified from Brian Hammond's Howdy program at
56 // http://www.insanityengine.com/doc/x11/xintro.html
57 // jeff louie 02.05.2004
61 int main (int argc
, char * const argv
[]) {
67 Main::Main (int argc
, char * const argv
[]) {
68 XEvent event
; // XEvent declaration
69 KeySym key
; // KeyPress Events
70 char text
[255]; // char buffer for KeyPress Events
76 // get the next event and stuff it into our event variable.
77 // Note: only events we set the mask for are detected!
78 XNextEvent(dis
, &event
);
85 if (event
.xexpose
.count
==0) {
90 if (XLookupString(&event
.xkey
,text
,255,&key
,0)==1) {
91 // use the XLookupString routine to convert the invent
92 // KeyPress data into regular text. Weird but necessary...
93 if ((text
[0]=='q') || (text
[0]=='Q')) {
98 printf("You pressed the %c key!\n",text
[0]);
103 // get cursor position
106 strcpy(text
,"X is FUN!");
107 XSetForeground(dis
,gc
,rand()%event
.xbutton
.x
%255);
108 // draw text at cursor
109 XDrawString(dis
,win
,gc
,x
,y
, text
, strlen(text
));
112 printf("Unhandled event.\n");
117 void Main::init_x() {
118 unsigned long black
,white
;
120 dis
=XOpenDisplay(NULL
);
121 screen
=DefaultScreen(dis
);
122 black
=BlackPixel(dis
,screen
),
123 white
=WhitePixel(dis
, screen
);
124 win
=XCreateSimpleWindow(dis
,DefaultRootWindow(dis
),0,0,
125 300, 300, 5,black
, white
);
126 XSetStandardProperties(dis
,win
,"Hello World","Hi",None
,NULL
,0,NULL
);
127 XSelectInput(dis
, win
, ExposureMask
|ButtonPressMask
|KeyPressMask
);
128 // get Graphics Context
129 gc
=XCreateGC(dis
, win
, 0,0);
130 XSetBackground(dis
,gc
,white
);
131 XSetForeground(dis
,gc
,black
);
132 XClearWindow(dis
, win
);
133 XMapRaised(dis
, win
);
136 void Main::close_x() {
138 XDestroyWindow(dis
,win
);
143 void Main::redraw() {
144 XClearWindow(dis
, win
);