/* * Copyright (c) 2005 Matthieu Herrb * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above * copyright notice and this permission notice appear in all copies. * * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ /* $Id$ */ #include #include #include #include #include int done = 0; unsigned long black_pixel; typedef struct { Display *display; XtAppContext app; Window win; XftFont *font; XftColor color, bg; XftDraw *draw; GC gc; } XDataStr; static void sigHandler(int sig) { done = 1; } int createWin(XDataStr *data) { u_long attributeMask; XSetWindowAttributes attribute; Window w; Display *display = data->display; XRectangle clip; int screen = DefaultScreen(display); XGCValues gc_val; Screen *s; attribute.background_pixel = WhitePixel(display, screen); attribute.border_pixel = WhitePixel(display, screen); attribute.bit_gravity = NorthWestGravity; attribute.event_mask = ButtonPressMask|ButtonReleaseMask|KeyPressMask| ExposureMask; attributeMask = CWBorderPixel | CWBackPixel | CWEventMask | CWBitGravity; s = ScreenOfDisplay(data->display, screen); w = XCreateWindow(display, RootWindow(display, screen), 0, 0, DisplayWidth(display, screen)/2, 150, 0, DefaultDepth(display, screen), InputOutput, DefaultVisual(display, screen), attributeMask, &attribute); data->font = XftFontOpen(display, screen, XFT_FAMILY, XftTypeString, "mono", XFT_SIZE, XftTypeInteger, 16, NULL); if (!XftColorAllocName(display, XDefaultVisual(display, screen), DefaultColormap(display, screen), "red4", &data->color)) { fprintf(stderr, "cannot get color"); return -1; } if (!XftColorAllocName(display, XDefaultVisual(display, screen), DefaultColormap(display, screen), "linen", &data->bg)) { fprintf(stderr, "cannot get bg color"); return -1; } data->draw = XftDrawCreate(display, w, DefaultVisual(display, screen), DefaultColormap(display, screen)); gc_val.foreground = BlackPixel(display, screen); gc_val.background = WhitePixel(display, screen); data->gc = XCreateGC (display, w, GCForeground|GCBackground, &gc_val); clip.x = 0; clip.y = 0; clip.width = 200; clip.height = 100; XftDrawSetClipRectangles(data->draw, 20, 10, &clip, 1); data->win = w; return 0; } void show(XDataStr *data) { Status s; XMapWindow(data->display, data->win); s = XGrabKeyboard(data->display, data->win, False, GrabModeAsync, GrabModeAsync, CurrentTime); if (s != GrabSuccess) { printf("Error grabing kbd %d\n", s); } } int main(int argc, char *argv[]) { Display *display; Widget toplevel; XtAppContext app_con; XEvent event; char c, *string; unsigned int i; XDataStr *data; XExposeEvent *expose = (XExposeEvent *)&event; toplevel = XtAppInitialize(&app_con, "XSafe", NULL, 0, &argc, argv, NULL, NULL, 0); display = XtDisplay(toplevel); string = argc > 1 ? argv[1] : "Test string"; data = (XDataStr *)malloc(sizeof(XDataStr)); if (data == NULL) { perror("malloc"); exit(EXIT_FAILURE); } data->display = display; data->app = app_con; if (createWin(data) < 0) { fprintf(stderr, "can't create Data Window"); exit(EXIT_FAILURE); } show(data); signal(SIGINT, sigHandler); signal(SIGHUP, sigHandler); signal(SIGQUIT, sigHandler); signal(SIGTERM, sigHandler); done = 0; printf("Enter event loop\n"); while (!done) { /* wait on mouse input event to terminate */ XNextEvent(display, &event); switch(event.type) { case KeyPress: i = XLookupString(&event.xkey, &c, 1, NULL, NULL); if ((i == 1) && ((c == 'q') || (c == 'Q'))) { printf("done\n"); done = 1; } if (c == '\033') { printf("ESC\n"); } break; case Expose: printf("expose %d %d %d %d\n", expose->x, expose->y, expose->width, expose->height); XftDrawRect(data->draw, &data->bg, expose->x, expose->y, expose->width, expose->height); XftDrawString8(data->draw, &data->color, data->font, 50, 50, string, strlen(string)); break; } } printf("\nBye.\n"); XFlush(display); XUnmapWindow(data->display, data->win); XUngrabKeyboard(data->display, CurrentTime); XCloseDisplay(display); exit(EXIT_SUCCESS); }