Skip to content

Instantly share code, notes, and snippets.

@whosaysni
Created June 8, 2013 02:17
Show Gist options
  • Save whosaysni/5733660 to your computer and use it in GitHub Desktop.
Save whosaysni/5733660 to your computer and use it in GitHub Desktop.
Hello world with Xlib.
/*
hellox -- Hello world with Xlib.
$(CC) -o hellox hellox.c -lX11 -L/usr/X11/lib
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
int main(argc,argv)
int argc;
char **argv;
{
char hello[] = "Hello World!";
char hi[] = "hi!";
Display *mydisplay;
Window mywindow;
GC mygc;
XEvent myevent;
KeySym mykey;
XSizeHints myhint;
int myscreen;
unsigned long myforeground, mybackground;
int i;
char text[10];
int done;
/* setup display/screen */
mydisplay = XOpenDisplay("");
myscreen = DefaultScreen(mydisplay);
/* drawing contexts for an window */
myforeground = BlackPixel(mydisplay, myscreen);
mybackground = WhitePixel(mydisplay, myscreen);
myhint.x = 200;
myhint.y = 300;
myhint.width = 350;
myhint.height = 250;
myhint.flags = PPosition|PSize;
/* create window */
mywindow = XCreateSimpleWindow(mydisplay, DefaultRootWindow(mydisplay),
myhint.x, myhint.y,
myhint.width, myhint.height,
5, myforeground, mybackground);
/* window manager properties (yes, use of StdProp is obsolete) */
XSetStandardProperties(mydisplay, mywindow, hello, hello,
None, argv, argc, &myhint);
/* graphics context */
mygc = XCreateGC(mydisplay, mywindow, 0, 0);
XSetBackground(mydisplay, mygc, mybackground);
XSetForeground(mydisplay, mygc, myforeground);
/* allow receiving mouse events */
XSelectInput(mydisplay,mywindow,
ButtonPressMask|KeyPressMask|ExposureMask);
/* show up window */
XMapRaised(mydisplay, mywindow);
/* event loop */
done = 0;
while(done==0){
/* fetch event */
XNextEvent(mydisplay, &myevent);
switch(myevent.type){
case Expose:
/* Window was showed. */
if(myevent.xexpose.count==0)
XDrawImageString(myevent.xexpose.display,
myevent.xexpose.window,
mygc,
50, 50,
hello, strlen(hello));
break;
case MappingNotify:
/* Modifier key was up/down. */
XRefreshKeyboardMapping(&myevent);
break;
case ButtonPress:
/* Mouse button was pressed. */
XDrawImageString(myevent.xbutton.display,
myevent.xbutton.window,
mygc,
myevent.xbutton.x, myevent.xbutton.y,
hi, strlen(hi));
break;
case KeyPress:
/* Key input. */
i = XLookupString(&myevent, text, 10, &mykey, 0);
if(i==1 && text[0]=='q') done = 1;
break;
}
}
/* finalization */
XFreeGC(mydisplay,mygc);
XDestroyWindow(mydisplay, mywindow);
XCloseDisplay(mydisplay);
exit(0);
}
@vituchon
Copy link

vituchon commented Sep 9, 2022

Thanks for sharing it!

I stored the code under a file called demo.c and then ran the following command

gcc -o demo demo.c -lX11 -lGL -lGLU

Output

demo.c: In function ‘main’:
demo.c:94:31: warning: passing argument 1 of ‘XRefreshKeyboardMapping’ from incompatible pointer type [-Wincompatible-pointer-types]
   94 |       XRefreshKeyboardMapping(&myevent);
      |                               ^~~~~~~~
      |                               |
      |                               XEvent * {aka union _XEvent *}
In file included from demo.c:11:
/usr/include/X11/Xlib.h:3068:5: note: expected ‘XMappingEvent *’ {aka ‘struct <anonymous> *’} but argument is of type ‘XEvent *’ {aka ‘union _XEvent *’}
 3068 |     XMappingEvent* /* event_map */
      |     ^~~~~~~~~~~~~~
demo.c:106:25: warning: passing argument 1 of ‘XLookupString’ from incompatible pointer type [-Wincompatible-pointer-types]
  106 |       i = XLookupString(&myevent, text, 10, &mykey, 0);
      |                         ^~~~~~~~
      |                         |
      |                         XEvent * {aka union _XEvent *}
In file included from demo.c:12:
/usr/include/X11/Xutil.h:535:5: note: expected ‘XKeyEvent *’ {aka ‘struct <anonymous> *’} but argument is of type ‘XEvent *’ {aka ‘union _XEvent *’}
  535 |     XKeyEvent*  /* event_struct */,
      |     ^~~~~~~~~~

My enviorment is:

image

Greeting
Víctor From StackOverflow

@olafdietsche
Copy link

XEvent is a union, and you must select the appropriate member, e.g. line 94:

XRefreshKeyboardMapping(&myevent.xmapping);

and at line 106:

i = XLookupString(&myevent.xkey, text, 10, &mykey, 0);

@frankitox
Copy link

For anyone looking to just copy paste this, here's the code with olafdietsche modifications

/* 
 
  hellox -- Hello world with Xlib.
 
  $(CC) -o hellox hellox.c -lX11 -L/usr/X11/lib
 
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
 
 
int main(argc,argv)
     int argc;
     char **argv;
{
  char hello[] = "Hello World!";
  char hi[] = "hi!";
 
  Display *mydisplay;
  Window  mywindow;
 
  GC      mygc;
  
  XEvent myevent;
  KeySym mykey;
  
  XSizeHints myhint;
  
  int myscreen;
  unsigned long myforeground, mybackground;
  int i;
  char text[10];
  int done;
 
  /* setup display/screen */
  mydisplay = XOpenDisplay("");
  
  myscreen = DefaultScreen(mydisplay);
 
  /* drawing contexts for an window */
  myforeground = BlackPixel(mydisplay, myscreen);
  mybackground = WhitePixel(mydisplay, myscreen);
  myhint.x = 200;
  myhint.y = 300;
  myhint.width = 350;
  myhint.height = 250;
  myhint.flags = PPosition|PSize;
 
  /* create window */
  mywindow = XCreateSimpleWindow(mydisplay, DefaultRootWindow(mydisplay),
                                 myhint.x, myhint.y,
                                 myhint.width, myhint.height,
                                 5, myforeground, mybackground);
 
  /* window manager properties (yes, use of StdProp is obsolete) */
  XSetStandardProperties(mydisplay, mywindow, hello, hello,
                         None, argv, argc, &myhint);
 
  /* graphics context */
  mygc = XCreateGC(mydisplay, mywindow, 0, 0);
  XSetBackground(mydisplay, mygc, mybackground);
  XSetForeground(mydisplay, mygc, myforeground);
 
  /* allow receiving mouse events */
  XSelectInput(mydisplay,mywindow,
               ButtonPressMask|KeyPressMask|ExposureMask);
 
  /* show up window */
  XMapRaised(mydisplay, mywindow);
 
  /* event loop */
  done = 0;
  while(done==0){
 
    /* fetch event */
    XNextEvent(mydisplay, &myevent);
 
    switch(myevent.type){
      
    case Expose:
      /* Window was showed. */
      if(myevent.xexpose.count==0)
        XDrawImageString(myevent.xexpose.display,
                         myevent.xexpose.window,
                         mygc, 
                         50, 50, 
                         hello, strlen(hello));
      break;
    case MappingNotify:
      /* Modifier key was up/down. */
      XRefreshKeyboardMapping(&myevent.xmapping);
      break;
    case ButtonPress:
      /* Mouse button was pressed. */
      XDrawImageString(myevent.xbutton.display,
                       myevent.xbutton.window,
                       mygc, 
                       myevent.xbutton.x, myevent.xbutton.y,
                       hi, strlen(hi));
      break;
    case KeyPress:
      /* Key input. */
      i = XLookupString(&myevent.xkey, text, 10, &mykey, 0);
      if(i==1 && text[0]=='q') done = 1;
      break;
    }
  }
  
  /* finalization */
  XFreeGC(mydisplay,mygc);
  XDestroyWindow(mydisplay, mywindow);
  XCloseDisplay(mydisplay);
 
  exit(0);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment