Commit 4daa2ca34be0be2f6ded81f00080a623c280c8d3

Authored by Jean Wasilewski
1 parent 96d6826d

Displaying a pixel, one

Showing 1 changed file with 115 additions and 0 deletions   Show diff stats
main.c 0 → 100644
... ... @@ -0,0 +1,115 @@
  1 +/*
  2 + Remember to compile try:
  3 + 1) gcc hi.c -o hi -lX11
  4 + 2) gcc hi.c -I /usr/include/X11 -L /usr/X11/lib -lX11
  5 + 3) gcc hi.c -I /where/ever -L /who/knows/where -l X11
  6 +
  7 + Brian Hammond 2/9/96. Feel free to do with this as you will!
  8 +*/
  9 +
  10 +
  11 +/* include the X library headers */
  12 +#include <X11/Xlib.h>
  13 +#include <X11/Xutil.h>
  14 +#include <X11/Xos.h>
  15 +
  16 +/* include some silly stuff */
  17 +#include <stdio.h>
  18 +#include <stdlib.h>
  19 +
  20 +/* here are our X variables */
  21 +Display *dis;
  22 +int screen;
  23 +Window win;
  24 +GC gc;
  25 +int width=320;
  26 +int height=240;
  27 +
  28 +/* here are our X routines declared! */
  29 +void init_x();
  30 +void close_x();
  31 +void redraw();
  32 +
  33 +main () {
  34 + XEvent event; /* the XEvent declaration !!! */
  35 + KeySym key; /* a dealie-bob to handle KeyPress Events */
  36 + char text[255]; /* a char buffer for KeyPress Events */
  37 + Pixmap pmap;
  38 + XColor col;
  39 +
  40 + init_x();
  41 +
  42 + printf("Patte de canard\n");
  43 + Colormap cmap = DefaultColormap(dis, DefaultScreen(dis));
  44 + printf("?\n");
  45 + pmap = XCreatePixmap(dis, win, width, height, DefaultDepth(dis, DefaultScreen(dis)));
  46 +
  47 + /* look for events forever... */
  48 + while(1) {
  49 + /* get the next event and stuff it into our event variable.
  50 + Note: only events we set the mask for are detected!
  51 + */
  52 + XNextEvent(dis, &event);
  53 +
  54 + if (event.type==Expose && event.xexpose.count==0) {
  55 + /* the window was exposed redraw it! */
  56 + redraw();
  57 + }
  58 + if (event.type==KeyPress&&
  59 + XLookupString(&event.xkey,text,255,&key,0)==1) {
  60 + /* use the XLookupString routine to convert the invent
  61 + KeyPress data into regular text. Weird but necessary...
  62 + */
  63 + if (text[0]=='q') {
  64 + close_x();
  65 + }
  66 + printf("You pressed the %c key!\n",text[0]);
  67 + }
  68 + if (event.type==ButtonPress) {
  69 + /* tell where the mouse Button was Pressed */
  70 + int x=event.xbutton.x,
  71 + y=event.xbutton.y;
  72 +
  73 + //strcpy(text,"X is FUN!");
  74 + //XSetForeground(dis,gc,rand()%event.xbutton.x%255);
  75 + //XDrawString(dis,win,gc,x,y, text, strlen(text));
  76 +
  77 +
  78 + col.red = 128;
  79 + col.green = 128;
  80 + col.blue = 0;
  81 + XAllocColor(dis, cmap, &col);
  82 + XSetForeground(dis, gc, col.pixel);
  83 + XDrawPoint(dis,win,gc,x,y);
  84 + }
  85 + }
  86 +}
  87 +
  88 +void init_x() {
  89 +/* get the colors black and white (see section for details) */
  90 + unsigned long black,white;
  91 +
  92 + dis=XOpenDisplay((char *)0);
  93 + screen=DefaultScreen(dis);
  94 + black=BlackPixel(dis,screen),
  95 + white=WhitePixel(dis, screen);
  96 + win=XCreateSimpleWindow(dis,DefaultRootWindow(dis),0,0,height,width, 0,black, white);
  97 + XSetStandardProperties(dis,win,"Howdy","Hi",None,NULL,0,NULL);
  98 + XSelectInput(dis, win, ExposureMask|ButtonPressMask|KeyPressMask);
  99 + gc=XCreateGC(dis, win, 0,0);
  100 + XSetBackground(dis,gc,white);
  101 + XSetForeground(dis,gc,black);
  102 + XClearWindow(dis, win);
  103 + XMapRaised(dis, win);
  104 +};
  105 +
  106 +void close_x() {
  107 + XFreeGC(dis, gc);
  108 + XDestroyWindow(dis,win);
  109 + XCloseDisplay(dis);
  110 + exit(1);
  111 +};
  112 +
  113 +void redraw() {
  114 + XClearWindow(dis, win);
  115 +};
... ...