Advertisement
Guest User

Untitled

a guest
Dec 29th, 2012
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.67 KB | None | 0 0
  1. #include X11/Xlib.h
  2. #include X11/Xutil.h
  3. #include X11/Xos.h
  4. #include X11/Xatom.h
  5.  
  6. #include stdlib.h
  7. #include string.h
  8. #include stdio.h
  9.  
  10. // Global variables
  11.  
  12. Display * display;
  13. int screen_num;
  14. static char * appname;
  15.  
  16. // main() function
  17.  
  18. int main( int argc, char * argv[] )
  19. {
  20.  
  21.     // Window variables
  22.  
  23.     Window win;
  24.     int x, y;
  25.     unsigned int width, height;
  26.     unsigned int border_width;
  27.     char * window_name = "Hello, X Window System!";
  28.     char * icon_name = "HelloX";
  29.  
  30.     // Display variables
  31.  
  32.     char * display_name = NULL;
  33.     unsigned int display_width, display_height;
  34.  
  35.     // Miscellaneous X variables
  36.  
  37.     XSizeHints * size_hints;
  38.     XWMHints * wm_hints;
  39.     XClassHint * class_hints;
  40.     XTextProperty windowName, iconName;
  41.     XEvent report;
  42.     XFontStruct * font_info;
  43.     XGCValues values;
  44.     GC gc;
  45.  
  46.     // Variables for color
  47.  
  48.     long fgcolor, bgcolor;
  49.     int screen, pointx, pointy;
  50.     XGCValues gcval;
  51.     Colormap cmap;
  52.     XColor color, ignore;
  53.     char *colorname="grey";
  54.  
  55.     appname = argv[0];
  56.  
  57.     // Allocate memory for our structures
  58.  
  59.     if
  60.     (
  61.         !( size_hints = XAllocSizeHints() )
  62.         ||
  63.         !( wm_hints = XAllocWMHints() )
  64.         ||
  65.         !( class_hints = XAllocClassHint() )
  66.     )
  67.     {
  68.         fprintf(stderr, "%s: couldn't allocate memory.\n", appname);
  69.         exit(EXIT_FAILURE);
  70.     }
  71.  
  72.     // Connect to X server
  73.  
  74.     if ( (display = XOpenDisplay(display_name)) == NULL )
  75.     {
  76.         fprintf(stderr, "%s: couldn't connect to X server %s\n",
  77.         appname, display_name);
  78.         exit(EXIT_FAILURE);
  79.     }
  80.  
  81.     // Get screen size from display structure macro
  82.  
  83.     screen_num = DefaultScreen(display);
  84.     display_width = DisplayWidth(display, screen_num);
  85.     display_height = DisplayHeight(display, screen_num);
  86.  
  87.     // Set initial window size and position, and create it
  88.  
  89.     bgcolor = BlackPixel(display,screen_num);
  90.     fgcolor = WhitePixel(display,screen_num);
  91.     x = y = 0;
  92.     width = 640;
  93.     height = 480;
  94.  
  95.     win = XCreateSimpleWindow(display, RootWindow(display, screen_num),
  96.     x, y, width, height, border_width,
  97.     fgcolor,
  98.     bgcolor);
  99.  
  100.     cmap = DefaultColormap (display, screen_num);
  101.     // XAllocNamedColor(display, cmap, colorname, &color, &ignore);
  102.     // fgcolor = color.pixel;
  103.  
  104.     // XStandardColormap *XAllocStandardColormap();
  105.     gcval.foreground = fgcolor;
  106.     gcval.background = bgcolor;
  107.     gc = XCreateGC(display,win,GCForeground|GCBackground,&gcval);
  108.  
  109.     // Set hints for window manager before mapping window
  110.  
  111.     if ( XStringListToTextProperty(&window_name, 1, &windowName) == 0 )
  112.     {
  113.         fprintf(stderr, "%s: structure allocation for windowName failed.\n",
  114.         appname);
  115.         exit(EXIT_FAILURE);
  116.     }
  117.  
  118.     if ( XStringListToTextProperty(&icon_name, 1, &iconName) == 0 )
  119.     {
  120.         fprintf(stderr, "%s: structure allocation for iconName failed.\n",
  121.         appname);
  122.         exit(EXIT_FAILURE);
  123.     }
  124.  
  125.     size_hints->flags = PPosition | PSize | PMinSize | PMaxSize;
  126.     size_hints->min_width = 640;
  127.     size_hints->min_height = 480;
  128.     size_hints->max_width = 640;
  129.     size_hints->max_height = 480;
  130.  
  131.     wm_hints->flags = StateHint | InputHint;
  132.     wm_hints->initial_state = NormalState;
  133.     wm_hints->input = True;
  134.  
  135.     class_hints->res_name = appname;
  136.     class_hints->res_class = "hellox";
  137.  
  138.     XSetWMProperties(display, win, &windowName, &iconName, argv, argc,
  139.     size_hints, wm_hints, class_hints);
  140.  
  141.     // XSetRGBColormaps(display, win, std_colormap, 0, prop1);
  142.  
  143.     // Choose which events we want to handle
  144.  
  145.     XSelectInput(display, win, ExposureMask | KeyPressMask |
  146.     ButtonPressMask | StructureNotifyMask);
  147.  
  148.     // Load a font called "9x15"
  149.  
  150.     if ( (font_info = XLoadQueryFont(display, "9x15")) == NULL )
  151.     {
  152.         fprintf(stderr, "%s: cannot open 9x15 font.\n", appname);
  153.         exit(EXIT_FAILURE);
  154.     }
  155.  
  156.     // Create graphics context
  157.  
  158.     gc = XCreateGC(display, win, 0, &values);
  159.  
  160.     XSetFont(display, gc, font_info->fid);
  161.     XSetForeground(display, gc, fgcolor);
  162.  
  163.     // Display Window
  164.  
  165.     XMapWindow(display, win);
  166.  
  167.     // Enter event loop
  168.  
  169.     while ( 1 )
  170.     {
  171.         static char * message = "Hello, X Window System!";
  172.         static int length;
  173.         static int font_height;
  174.         static int msg_x, msg_y;
  175.  
  176.         XNextEvent(display, &report);
  177.  
  178.         switch ( report.type )
  179.         {
  180.  
  181.             case Expose:
  182.                 if ( report.xexpose.count != 0 ) break;
  183.                 /* Output message centrally in window */
  184.                 length = XTextWidth(font_info, message, strlen(message));
  185.                 msg_x = (width - length) / 2;
  186.                 font_height = font_info->ascent + font_info->descent;
  187.                 msg_y = (height + font_height) / 2;
  188.                 XDrawString(display, win, gc, msg_x, msg_y,
  189.                 message, strlen(message));
  190.                 break;
  191.  
  192.             case ConfigureNotify:
  193.  
  194.                 /* Store new window width & height */
  195.                 width = report.xconfigure.width;
  196.                 height = report.xconfigure.height;
  197.                 break;
  198.  
  199.             case ButtonPress: /* Fall through */
  200.                 XDrawPoint(display,win,gc, report.xbutton.x,report.xbutton.y);
  201.                 break;
  202.  
  203.             case KeyPress:
  204.                 /* Clean up and exit */
  205.                 XUnloadFont(display, font_info->fid);
  206.                 XFreeGC(display, gc);
  207.                 XCloseDisplay(display);
  208.                 exit(EXIT_SUCCESS);
  209.                 break;
  210.  
  211.             default:;
  212.  
  213.         }
  214.  
  215.     }
  216.  
  217.     return EXIT_SUCCESS; /* We shouldn't get here */
  218. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement