x.c 8.62 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
#include "x.h"
#include "x_defs.h"
#include "gui_defs.h"
#include <X11/Xlib.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int x_connection_fd(x_connection *_x)
{
  struct x_connection *x = _x;
  return ConnectionNumber(x->d);
}

static GC create_gc(Display *d, char *color)
{
  GC ret = XCreateGC(d, DefaultRootWindow(d), 0, NULL);
  XGCValues gcv;
  XColor rcol, scol;

  XCopyGC(d, DefaultGC(d, DefaultScreen(d)), -1L, ret);
  if (XAllocNamedColor(d, DefaultColormap(d, DefaultScreen(d)),
                      color, &scol, &rcol)) {
    gcv.foreground = scol.pixel;
    XChangeGC(d, ret, GCForeground, &gcv);
  } else ERR("X: could not allocate color '%s'\n", color);

  return ret;
}

int x_new_color(x_connection *_x, char *color)
{
  struct x_connection *x = _x;
  x->ncolors++;
  x->colors = realloc(x->colors, x->ncolors * sizeof(GC));
  if (x->colors == NULL) OOM;
  x->colors[x->ncolors-1] = create_gc(x->d, color);
  return x->ncolors - 1;
}

x_connection *x_open(void)
{
  struct x_connection *ret;

  ret = calloc(1, sizeof(struct x_connection));
  if (ret == NULL) OOM;

  ret->d = XOpenDisplay(0);
printf("XOpenDisplay display %p return x_connection %p\n", ret->d, ret);
  if (ret->d == NULL) ERR("error calling XOpenDisplay: no X? you root?\n");

  x_new_color(ret, "white");    /* background color */
  x_new_color(ret, "black");    /* foreground color */

  return ret;
}

x_window *x_create_window(x_connection *_x, int width, int height,
    char *title)
{
  struct x_connection *x = _x;
  struct x_window *ret;

  ret = calloc(1, sizeof(struct x_window));
  if (ret == NULL) OOM;

  ret->w = XCreateSimpleWindow(x->d, DefaultRootWindow(x->d), 0, 0,
      width, height, 0, WhitePixel(x->d, DefaultScreen(x->d)),
      WhitePixel(x->d, DefaultScreen(x->d)));
  ret->width = width;
  ret->height = height;

  XStoreName(x->d, ret->w, title);

  ret->p = XCreatePixmap(x->d, ret->w, width, height,
      DefaultDepth(x->d, DefaultScreen(x->d)));
77 78
  XFillRectangle(x->d, ret->p, x->colors[BACKGROUND_COLOR],
      0, 0, width, height);
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198

  /* enable backing store */
  {
    XSetWindowAttributes att;
    att.backing_store = Always;
    XChangeWindowAttributes(x->d, ret->w, CWBackingStore, &att);
  }

  XSelectInput(x->d, ret->w,
      KeyPressMask      |
      ButtonPressMask   |
      ButtonReleaseMask |
      PointerMotionMask |
      ExposureMask      |
      StructureNotifyMask);

  XMapWindow(x->d, ret->w);

#if 0
  /* wait for window to be mapped */
printf("wait for map\n");
  while (1) {
    XEvent ev;
    //XWindowEvent(x->d, ret->w, StructureNotifyMask, &ev);
    XWindowEvent(x->d, ret->w, ExposureMask, &ev);
printf("got ev %d\n", ev.type);
    //if (ev.type == MapNotify) break;
    if (ev.type == Expose) break;
  }
printf("XXX create connection %p window %p (win id %d pixmap %d) w h %d %d\n", x, ret, (int)ret->w, (int)ret->p, width, height);
#endif

  return ret;
}

static struct toplevel_window_widget *find_x_window(struct gui *g, Window id)
{
  struct widget_list *cur;
  struct toplevel_window_widget *w;
  struct x_window *xw;
  cur = g->toplevel;
  while (cur) {
    w = (struct toplevel_window_widget *)cur->item;
    xw = w->x;
    if (xw->w == id) return w;
    cur = cur->next;
  }
  return NULL;
}

void x_events(gui *_gui)
{
  struct gui *g = _gui;
  struct widget_list *cur;
  struct x_connection *x = g->x;
  struct toplevel_window_widget *w;

printf("x_events START\n");
  /* preprocessing (to "compress" events) */
  cur = g->toplevel;
  while (cur) {
    struct x_window *xw;
    w = (struct toplevel_window_widget *)cur->item;
    xw = w->x;
    xw->redraw = 0;
    xw->repaint = 0;
    xw->resize = 0;
    cur = cur->next;
  }

  while (XPending(x->d)) {
    XEvent ev;
    XNextEvent(x->d, &ev);
printf("XEV %d\n", ev.type);
    switch (ev.type) {
    case Expose:
      if ((w = find_x_window(g, ev.xexpose.window)) != NULL) {
        struct x_window *xw = w->x;
        xw->redraw = 1;
      }
      break;
    case ConfigureNotify:
      if ((w = find_x_window(g, ev.xexpose.window)) != NULL) {
        struct x_window *xw = w->x;
        xw->resize = 1;
        xw->new_width = ev.xconfigure.width;
        xw->new_height = ev.xconfigure.height;
        if (xw->new_width < 10) xw->new_width = 10;
        if (xw->new_height < 10) xw->new_height = 10;
printf("ConfigureNotify %d %d\n", ev.xconfigure.width, ev.xconfigure.height);
      }
      break;
#if 0
    case MapNotify:
      if ((w = find_x_window(g, ev.xexpose.window)) != NULL) {
        struct x_window *xw = w->x;
        xw->repaint = 1;
      }
      break;
#endif
    default: WARN("TODO: X event type %d\n", ev.type); break;
    }
  }

  /* postprocessing */
printf("post processing\n");
  cur = g->toplevel;
  while (cur) {
    struct toplevel_window_widget *w =
        (struct toplevel_window_widget *)cur->item;
    struct x_window *xw = w->x;
    if (xw->resize) {
printf("resize old %d %d new %d %d\n", xw->width, xw->height, xw->new_width, xw->new_height);
      if (xw->width != xw->new_width || xw->height != xw->new_height) {
        w->common.allocate(g, w, 0, 0, xw->new_width, xw->new_height);
        xw->width = xw->new_width;
        xw->height = xw->new_height;
        XFreePixmap(x->d, xw->p);
        xw->p = XCreatePixmap(x->d, xw->w, xw->width, xw->height,
            DefaultDepth(x->d, DefaultScreen(x->d)));
199 200
        XFillRectangle(x->d, xw->p, x->colors[BACKGROUND_COLOR],
            0, 0, xw->width, xw->height);
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
        //xw->repaint = 1;
      }
    }
    if (xw->repaint) {
      w->common.paint(g, w);
      xw->redraw = 1;
    }
    if (xw->redraw) {
      struct x_connection *x = g->x;
printf("XCopyArea w h %d %d\n", xw->width, xw->height);
      XCopyArea(x->d, xw->p, xw->w, x->colors[1],
          0, 0, xw->width, xw->height, 0, 0);
    }
    cur = cur->next;
  }
printf("x_events DONE\n");
}

void x_flush(x_connection *_x)
{
  struct x_connection *x = _x;
  XFlush(x->d);
}

void x_text_get_dimensions(x_connection *_c, const char *t,
    int *width, int *height, int *baseline)
{
  struct x_connection *c = _c;
  int dir;
  int ascent;
  int descent;
  XCharStruct overall;

  /* TODO: don't use XQueryTextExtents (X roundtrip) */
  XQueryTextExtents(c->d, XGContextFromGC(c->colors[1]), t, strlen(t),
      &dir, &ascent, &descent, &overall);

//printf("dir %d ascent %d descent %d lbearing %d rbearing %d width %d ascent %d descent %d\n", dir, ascent, descent, overall.lbearing, overall.rbearing, overall.width, overall.ascent, overall.descent);

  *width = overall.width;
  *height = ascent + descent;
  *baseline = ascent;
}

/***********************************************************************/
/*                    public drawing functions                         */
/***********************************************************************/

void x_draw_line(x_connection *_c, x_window *_w, int color,
    int x1, int y1, int x2, int y2)
{
  struct x_connection *c = _c;
  struct x_window *w = _w;
  XDrawLine(c->d, w->p, c->colors[color], x1, y1, x2, y2);
}

void x_draw_rectangle(x_connection *_c, x_window *_w, int color,
    int x, int y, int width, int height)
{
  struct x_connection *c = _c;
  struct x_window *w = _w;
  XDrawRectangle(c->d, w->p, c->colors[color], x, y, width, height);
}

void x_fill_rectangle(x_connection *_c, x_window *_w, int color,
    int x, int y, int width, int height)
{
  struct x_connection *c = _c;
  struct x_window *w = _w;
  XFillRectangle(c->d, w->p, c->colors[color], x, y, width, height);
}

void x_draw_string(x_connection *_c, x_window *_w, int color,
    int x, int y, const char *t)
{
  struct x_connection *c = _c;
  struct x_window *w = _w;
  int tlen = strlen(t);
  XDrawString(c->d, w->p, c->colors[color], x, y, t, tlen);
}

void x_draw(x_connection *_c, x_window *_w)
{
  struct x_connection *c = _c;
  struct x_window *w = _w;
printf("x_draw XCopyArea w h %d %d display %p window %d pixmap %d\n", w->width, w->height, c->d, (int)w->w, (int)w->p);
  XCopyArea(c->d, w->p, w->w, c->colors[1], 0, 0, w->width, w->height, 0, 0);
}
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316

/* those two special functions are to plot many points
 * first call x_add_point many times then x_plot_points once
 */
void x_add_point(x_connection *_c, int x, int y)
{
  struct x_connection *c = _c;

  if (c->pts_size == c->pts_maxsize) {
    c->pts_maxsize += 65536;
    c->pts = realloc(c->pts, c->pts_maxsize * sizeof(XPoint));
    if (c->pts == NULL) OOM;
  }

  c->pts[c->pts_size].x = x;
  c->pts[c->pts_size].y = y;
  c->pts_size++;
}

void x_plot_points(x_connection *_c, x_window *_w, int color)
{
  struct x_connection *c = _c;
fprintf(stderr, "x_plot_points %d points\n", c->pts_size);
  struct x_window *w = _w;
  XDrawPoints(c->d, w->p, c->colors[color], c->pts, c->pts_size,
      CoordModeOrigin);
  c->pts_size = 0;
}