#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
+#include <stdint.h>
#include <fcntl.h>
#include <string.h>
#include <xcb/xcb.h>
#include <stdarg.h>
+#include <sys/wait.h>
#ifndef __CHECKER__
#include <xcb/xkb.h>
#else
#undef True
#undef False
+
+#define PANE_DATA_TYPE struct xcb_data
#include "core.h"
enum my_atoms {
a_WM_ICON_NAME, a_NET_WM_ICON_NAME,
a_WM_PROTOCOLS, a_WM_DELETE_WINDOW,
a_NET_WM_PING,
+ a_NET_WM_ICON,
a_WM_CLIENT_MACHINE,
a_UTF8_STRING,
NR_ATOMS
};
-static char *atom_names[NR_ATOMS] = {
+static const char *atom_names[NR_ATOMS] = {
[a_NONE] = "NONE",
[a_WM_STATE] = "_NET_WM_STATE",
[a_STATE_FULLSCREEN] = "_NET_WM_STATE_FULLSCREEN",
[a_WM_PROTOCOLS] = "WM_PROTOCOLS",
[a_WM_DELETE_WINDOW] = "WM_DELETE_WINDOW",
[a_NET_WM_PING] = "_NET_WM_PING",
+ [a_NET_WM_ICON] = "_NET_WM_ICON",
[a_WM_CLIENT_MACHINE] = "WM_CLIENT_MACHINE",
[a_UTF8_STRING] = "UTF8_STRING",
};
cairo_t *cairo safe;
cairo_surface_t *surface safe;
PangoFontDescription *fd safe;
- char *noclose;
int charwidth, lineheight;
cairo_region_t *need_update;
struct xkb_compose_table *compose_table;
struct xkb_keymap *xkb_keymap;
+ struct pids {
+ pid_t pid;
+ struct pids *next;
+ } *pids;
+
/* FIXME use hash?? */
struct panes {
struct panes *next;
cairo_region_t *need_update;
} *panes;
};
+#include "core-pane.h"
/* panes->r.x is NEVER_DRAWN if the pane has not been drawn */
#define NEVER_DRAWN (-60000)
char *fg = NULL, *bg = NULL;
bool ul = False;
bool inv = False;
- int size = 10*1000;
+ int size = 12*1000;
PangoFontDescription *fd = NULL;
PangoStyle style = PANGO_STYLE_NORMAL;
PangoVariant variant = PANGO_VARIANT_NORMAL;
if (fdp) {
fd = pango_font_description_new();
*fdp = fd;
- pango_font_description_set_family_static(fd, "mono");
+ pango_font_description_set_family_static(fd, "monospace");
}
while ((word = strsep(&ap, ",")) != NULL) {
- if (fd && strncmp(word, "family:", 7) == 0)
+ if (fd && strstarts(word, "family:"))
pango_font_description_set_family(fd, word+7);
if (strcmp(word, "large") == 0)
size = 14 * 1000;
if (strcmp(word, "nobold") == 0)
weight = PANGO_WEIGHT_NORMAL;
- if (strncmp(word, "fg:", 3) == 0)
+ if (strstarts(word, "fg:"))
fg = word + 3;
- if (strncmp(word, "bg:", 3) == 0)
+ if (strstarts(word, "bg:"))
bg = word + 3;
if (strcmp(word, "inverse") == 0)
inv = True;
+ if (strcmp(word, "noinverse") == 0)
+ inv = False;
if (strcmp(word, "underline") == 0)
ul = True;
+ if (strcmp(word, "nounderline") == 0)
+ ul = False;
}
if (inv) {
} else if (bgp)
bgp->g = -1;
if (fd) {
- pango_font_description_set_size(fd, size * scale / PANGO_SCALE);
+ pango_font_description_set_size(fd, PANGO_SCALE * size /1000 * scale / 1000);
if (style != PANGO_STYLE_NORMAL)
pango_font_description_set_style(fd, style);
if (variant != PANGO_VARIANT_NORMAL)
return 1;
}
-DEF_CMD(xcb_close_display)
+DEF_CMD_CLOSED(xcb_close_display)
{
/* If this is only display, then refuse to close this one */
struct call_return cr;
- struct xcb_data *xd = ci->home->data;
- if (xd->noclose) {
- call("Message", ci->focus, 0, NULL, xd->noclose);
+ char *nc = pane_attr_get(ci->home, "no-close");
+
+ if (nc) {
+ call("Message", ci->focus, 0, NULL, nc);
return 1;
}
cr.c = cnt_disp;
cr.i = 0;
call_comm("editor:notify:all-displays", ci->focus, &cr.c);
if (cr.i > 1)
- pane_close(ci->home);
+ return Efallthrough;
else
call("Message", ci->focus, 0, NULL,
"Cannot close only window.");
return 1;
}
-DEF_CMD(xcb_set_noclose)
+static void wait_for(struct xcb_data *xd safe)
{
- struct xcb_data *xd = ci->home->data;
-
- free(xd->noclose);
- xd->noclose = NULL;
- if (ci->str)
- xd->noclose = strdup(ci->str);
- return 1;
+ struct pids **pp = &xd->pids;
+
+ while (*pp) {
+ struct pids *p = *pp;
+ if (waitpid(p->pid, NULL, WNOHANG) > 0) {
+ *pp = p->next;
+ free(p);
+ } else
+ pp = &p->next;
+ }
}
DEF_CMD(xcb_external_viewer)
{
struct xcb_data *xd = ci->home->data;
const char *path = ci->str;
+ struct pids *p;
int pid;
int fd;
execlp("xdg-open", "xdg-open", path, NULL);
exit(1);
default: /* parent */
+ p = malloc(sizeof(*p));
+ p->pid = pid;
+ p->next = xd->pids;
+ xd->pids = p;
break;
}
+ wait_for(xd);
return 1;
}
static void kbd_free(struct xcb_data *xd safe);
-DEF_CMD(xcb_close)
+DEF_CMD_CLOSED(xcb_close)
{
struct xcb_data *xd = ci->home->data;
xcb_destroy_window(xd->conn, xd->win);
kbd_free(xd);
panes_free(xd);
- return 1;
-}
-
-DEF_CMD(xcb_free)
-{
- struct xcb_data *xd = ci->home->data;
pango_font_description_free(xd->fd);
cairo_destroy(xd->cairo);
cairo_surface_destroy(xd->surface);
free(xd->display);
free(xd->disp_auth);
- free(xd->noclose);
xcb_disconnect(xd->conn);
if (xd->need_update)
cairo_region_destroy(xd->need_update);
- unalloc(xd, pane);
return 1;
}
const char *attr = ci->str;
struct panes *src = NULL, *dest;
struct rgb bg;
- int x, y;
+ int x=0, y=0;
cairo_rectangle_int_t r;
if (attr) {
cairo_paint(dest->ctx);
dest->bg.g = -1;
}
- } else
- LOG("ERROR neither src or bg");
+ }
pane_damaged(ci->home, DAMAGED_POSTORDER);
if (!dest->need_update)
PangoRectangle curs;
bool in_focus = xd->in_focus;
struct pane *f = ci->focus;
+ double cx, cy, cw, ch;
pango_layout_index_to_pos(layout, ci->num, &curs);
if (curs.width <= 0) {
pango_layout_get_extents(layout, NULL, &log);
curs.width = log.width;
}
- cairo_rectangle(ctx, x+curs.x/PANGO_SCALE, y-baseline+curs.y/PANGO_SCALE,
- (curs.width - PANGO_SCALE/2) / PANGO_SCALE,
- (curs.height - PANGO_SCALE/2) / PANGO_SCALE);
- cairo_set_line_width(ctx, 1.0);
- cairo_stroke(ctx);
while (in_focus && f->parent->parent != f &&
f->parent != ci->home) {
in_focus = False;
f = f->parent;
}
- if (in_focus) {
- if (fg.g >= 0)
- cairo_set_source_rgb(ctx, fg.r, fg.g, fg.b);
- cairo_rectangle(ctx, x+curs.x/PANGO_SCALE,
+ if (!in_focus) {
+ /* Just an fg:rectangle around the fg:text */
+ /* Add half to x,y as stroke is either side of the line */
+ cx = x * PANGO_SCALE + curs.x + PANGO_SCALE/2;
+ cy = (y - baseline) * PANGO_SCALE + curs.y + PANGO_SCALE/2;
+ ch = curs.height - PANGO_SCALE;
+ cw = curs.width - PANGO_SCALE;
+ cairo_rectangle(ctx, cx/PANGO_SCALE, cy/PANGO_SCALE,
+ cw/PANGO_SCALE, ch/PANGO_SCALE);
+ cairo_set_line_width(ctx, 1.0);
+ cairo_stroke(ctx);
+ } else {
+ /* solid fd:block with txt in bg color */
+ cairo_rectangle(ctx,
+ x+curs.x/PANGO_SCALE,
y-baseline+curs.y/PANGO_SCALE,
curs.width / PANGO_SCALE,
curs.height / PANGO_SCALE);
return 1;
}
+struct di_info {
+ struct command c;
+ MagickWand *wd safe;
+ int x,y,w,h;
+ int xo, yo;
+ struct panes *ps safe;
+};
+
+DEF_CB(xcb_draw_image_cb)
+{
+ struct di_info *dii = container_of(ci->comm, struct di_info, c);
+ int stride;
+ int fmt[2];
+ unsigned char *buf;
+ cairo_surface_t *surface;
+
+ switch (ci->key[0]) {
+ case 'w': /* width */
+ return MagickGetImageWidth(dii->wd);
+ case 'h': /* height */
+ return MagickGetImageHeight(dii->wd);
+ case 's': /* scale */
+ MagickResizeImage(dii->wd, ci->num, ci->num2, BoxFilter, 1.0);
+ return 1;
+ case 'c': /* crop or cursor */
+ if (ci->key[1] != 'u') {
+ /* crop */
+ dii->x = ci->x;
+ dii->y = ci->y;
+ dii->w = ci->num;
+ dii->h = ci->num2;
+ return 1;
+ } else {
+ /* cursor */
+ cairo_rectangle(dii->ps->ctx,
+ ci->x + dii->xo, ci->y + dii->yo,
+ ci->num, ci->num2);
+ cairo_set_line_width(dii->ps->ctx, 1.0);
+ cairo_set_source_rgb(dii->ps->ctx, 1.0, 0.0, 0.0);
+ cairo_stroke(dii->ps->ctx);
+ return 1;
+ }
+ case 'd': /* draw */
+ if (dii->w <= 0 || dii->h <= 0)
+ return Efail;
+ stride = cairo_format_stride_for_width(CAIRO_FORMAT_RGB24,
+ dii->w);
+ buf = malloc(dii->h * stride);
+ // Cairo expects 32bit values with A in the high byte, then RGB.
+ // Magick provides 8bit values in the order requests.
+ // So depending on byte order, a different string is needed
+
+ fmt[0] = ('A'<<24) | ('R' << 16) | ('G' << 8) | ('B' << 0);
+ fmt[1] = 0;
+ MagickExportImagePixels(dii->wd, dii->x, dii->y,
+ dii->w, dii->h,
+ (char*)fmt, CharPixel, buf);
+ surface = cairo_image_surface_create_for_data(
+ buf, CAIRO_FORMAT_ARGB32, dii->w, dii->h, stride);
+ cairo_set_source_surface(dii->ps->ctx, surface,
+ ci->num + dii->xo, ci->num2 + dii->yo);
+ cairo_paint(dii->ps->ctx);
+ cairo_surface_destroy(surface);
+ free(buf);
+ return 1;
+ default:
+ return Efail;
+ }
+}
+
DEF_CMD(xcb_draw_image)
{
/* 'str' identifies the image. Options are:
* file:filename - load file from fs
* comm:command - run command collecting bytes
- * 'num' is '1' if image should be stretched to fill pane
- * if 'num is '0', then 'num2' is 'or' of
- * 0,1,2 for left/middle/right in x direction
- * 0,4,8 for top/middle/bottom in y direction
- * only one of these can be used as image will fill pane in other direction.
+ * 'str2' and numbers are handled by Draw:scale-image.
*/
struct xcb_data *xd = ci->home->data;
- bool stretch = ci->num == 1;
- int pos = ci->num2;
- int w = ci->focus->w, h = ci->focus->h;
- int x = 0, y = 0;
- int xo, yo;
- int stride;
+ struct di_info dii;
+ MagickWand *wd = NULL;
struct panes *ps;
- MagickBooleanType status;
- MagickWand *wd;
- int fmt[2];
- unsigned char *buf;
- cairo_surface_t *surface;
if (!ci->str)
return Enoarg;
- ps = find_pixmap(xd, ci->focus, &xo, &yo);
+ ps = find_pixmap(xd, ci->focus, &dii.xo, &dii.yo);
if (!ps)
return Einval;
- if (!ps->ctx)
- instantiate_pixmap(xd, ps);
- ps->bg.g = -1;
- if (!ps->ctx)
+ dii.ps = ps;
+ if (!dii.ps->ctx)
+ instantiate_pixmap(xd, dii.ps);
+ dii.ps->bg.g = -1;
+ if (!dii.ps->ctx)
return Efail;
- if (strncmp(ci->str, "file:", 5) == 0) {
+ if (strstarts(ci->str, "file:")) {
+ MagickBooleanType status;
+
wd = NewMagickWand();
status = MagickReadImage(wd, ci->str + 5);
if (status == MagickFalse) {
DestroyMagickWand(wd);
return Efail;
}
- } else if (strncmp(ci->str, "comm:", 5) == 0) {
+ } else if (strstarts(ci->str, "comm:")) {
+ MagickBooleanType status;
struct call_return cr;
+
wd = NewMagickWand();
- cr = call_ret(bytes, ci->str+5, ci->focus, 0, NULL, ci->str2);
+ cr = call_ret(bytes, ci->str+5, ci->focus);
if (!cr.s) {
DestroyMagickWand(wd);
return Efail;
DestroyMagickWand(wd);
return Efail;
}
- } else
+ }
+
+ if (!wd)
return Einval;
MagickAutoOrientImage(wd);
- if (!stretch) {
- int ih = MagickGetImageHeight(wd);
- int iw = MagickGetImageWidth(wd);
+ dii.c = xcb_draw_image_cb;
+ dii.wd = wd;
+ dii.w = dii.h = dii.x = dii.y = dii.xo = dii.yo = 0;
+ call("Draw:scale-image", ci->focus,
+ ci->num, NULL, NULL, ci->num2, NULL, ci->str2,
+ ci->x, ci->y, &dii.c);
- if (iw <= 0 || iw <= 0) {
- DestroyMagickWand(wd);
- return Efail;
- }
- if (iw * h > ih * w) {
- /* Image is wider than space, use less height */
- ih = ih * w / iw;
- switch(pos & (8+4)) {
- case 4: /* center */
- y = (h - ih) / 2; break;
- case 8: /* bottom */
- y = h - ih; break;
- }
- h = ih;
- } else {
- /* image is too tall, use less width */
- iw = iw * h / ih;
- switch (pos & (1+2)) {
- case 1: /* center */
- x = (w - iw) / 2; break;
- case 2: /* right */
- x = w - iw ; break;
- }
- w = iw;
- }
- }
- MagickAdaptiveResizeImage(wd, w, h);
- stride = cairo_format_stride_for_width(CAIRO_FORMAT_RGB24, w);
- buf = malloc(h * stride);
- // Cairo expects 32bit values with A in the high byte, then RGB.
- // Magick provides 8bit values in the order requests.
- // So depending on byte order, a different string is needed
-
- fmt[0] = ('A'<<24) | ('R' << 16) | ('G' << 8) | ('B' << 0);
- fmt[1] = 0;
- MagickExportImagePixels(wd, 0, 0, w, h, (char*)fmt, CharPixel, buf);
- surface = cairo_image_surface_create_for_data(buf, CAIRO_FORMAT_ARGB32,
- w, h, stride);
- cairo_set_source_surface(ps->ctx, surface, x + xo, y + yo);
- cairo_paint(ps->ctx);
- cairo_surface_destroy(surface);
- free(buf);
DestroyMagickWand(wd);
pane_damaged(ci->home, DAMAGED_POSTORDER);
if (!ci->str)
return Enoarg;
- if (strncmp(ci->str, "file:", 5) == 0) {
+ if (strstarts(ci->str, "file:")) {
wd = NewMagickWand();
status = MagickReadImage(wd, ci->str + 5);
if (status == MagickFalse) {
DestroyMagickWand(wd);
return Efail;
}
- } else if (strncmp(ci->str, "comm:", 5) == 0) {
+ } else if (strstarts(ci->str, "comm:")) {
struct call_return cr;
wd = NewMagickWand();
- cr = call_ret(bytes, ci->str+5, ci->focus, 0, NULL, ci->str2);
+ cr = call_ret(bytes, ci->str+5, ci->focus);
if (!cr.s) {
DestroyMagickWand(wd);
return Efail;
struct mark *pt;
xd->in_focus = in;
- p = pane_leaf(home);
+ p = pane_focus(home);
pt = call_ret(mark, "doc:point", p);
if (pt)
call("view:changed", p, 0, pt);
cme->format == 32 &&
cme->window == xd->win &&
cme->data.data32[0] == xd->atoms[a_WM_DELETE_WINDOW]) {
- pane_call(home, "Display:close", pane_leaf(home));
+ call("Window:close", pane_focus(home));
return;
}
xcb_generic_event_t *ev;
int ret = 1;
+ wait_for(xd);
if (ci->num < 0)
/* This is a poll - only return 1 on something happening */
ret = Efalse;
}
xcb_flush(xd->conn);
}
- if (xcb_connection_has_error(xd->conn))
+ if (xcb_connection_has_error(xd->conn)) {
+ call("Window:close", ci->home->parent);
pane_close(ci->home);
+ }
return ret;
}
8, strlen(str), str);
}
+static void set_card32_property(struct xcb_data *xd safe,
+ enum my_atoms a,
+ const uint32_t *data, int cnt)
+{
+ xcb_change_property(xd->conn,
+ XCB_PROP_MODE_REPLACE,
+ xd->win, xd->atoms[a],
+ XCB_ATOM_CARDINAL, 32,
+ cnt, data);
+}
+
static void set_atom_prop(struct xcb_data *xd safe,
enum my_atoms prop, enum my_atoms alist, ...)
{
32, anum, atoms);
}
+static void xcb_load_icon(struct pane *p safe,
+ struct xcb_data *xd safe,
+ char *file safe)
+{
+ char *path;
+ int h, w, n;
+ unsigned int *data;
+ MagickBooleanType status;
+ MagickWand *wd;
+ uint32_t fmt[2];
+
+ path = call_ret(str, "xdg-find-edlib-file", p, 0, NULL,
+ file, 0, NULL, "data");
+ if (!path)
+ return;
+
+ wd = NewMagickWand();
+ status = MagickReadImage(wd, path);
+ free(path);
+ if (status == MagickFalse)
+ goto done;
+
+ h = MagickGetImageHeight(wd);
+ w = MagickGetImageWidth(wd);
+ n = 2 + w*h;
+ data = malloc(sizeof(data[0]) * n);
+ if (!data)
+ goto done;
+ data[0] = w;
+ data[1] = h;
+ /* Need host-endian ARGB data */
+ fmt[0] = ('A'<<24) | ('R' << 16) | ('G' << 8) | ('B' << 0);
+ fmt[1] = 0;
+ MagickExportImagePixels(wd, 0, 0, w, h, (char*)fmt,
+ CharPixel, data+2);
+ set_card32_property(xd, a_NET_WM_ICON, data, n);
+ free(data);
+done:
+ DestroyMagickWand(wd);
+ return;
+}
+
static struct pane *xcb_display_init(const char *d safe,
const char *disp_auth,
struct pane *focus safe)
if (xcb_connection_has_error(conn))
return NULL;
- alloc(xd, pane);
+ p = call_ret(pane, "attach-window-core", focus);
+ if (!p)
+ return NULL;
+ p = pane_register(p, 1, &xcb_handle.c);
+ if (!p)
+ return NULL;
+ xd = p->data;
xd->motion_blocked = True;
xd->in_focus = True;
xd->visual = xcb_depth_visuals(di.data);
for (i = 0; i < NR_ATOMS; i++) {
- char *n = atom_names[i];
+ const char *n = atom_names[i];
if (!n)
continue;
cookies[i] = xcb_intern_atom(conn, 0, strlen(n), n);
if (!fd)
goto abort;
xd->fd = fd;
- pango_font_description_set_family(xd->fd, "mono");
+ pango_font_description_set_family(xd->fd, "monospace");
pango_font_description_set_size(xd->fd, 12 * PANGO_SCALE);
layout = pango_cairo_create_layout(xd->cairo);
XCB_MOD_MASK_LOCK |
XCB_MOD_MASK_CONTROL);
+ xcb_load_icon(focus, xd, "{COMM}-icon.png");
xcb_map_window(conn, xd->win);
xcb_flush(conn);
- p = pane_register(pane_root(focus), 1, &xcb_handle.c, xd);
- if (!p)
- goto abort;
pane_resize(p, 0, 0, xd->charwidth*80, xd->lineheight*26);
call_comm("event:read", p, &xcb_input, xcb_get_file_descriptor(conn));
call_comm("event:poll", p, &xcb_input);
xcb_disconnect(conn);
free(xd->display);
free(xd->disp_auth);
- unalloc(xd, pane);
return NULL;
}
-DEF_CMD(display_xcb)
-{
- struct pane *p;
- const char *d = ci->str;
-
- if (!d)
- return Enoarg;
- p = xcb_display_init(d, ci->str2, ci->focus);
- if (p)
- return comm_call(ci->comm2, "cb", p);
- return Efail;
-}
-
DEF_CMD(xcb_new_display)
{
struct pane *p;
if (!d)
return Enoarg;
p = xcb_display_init(d, disp_auth, ci->focus);
- if (p)
- p = call_ret(pane, "editor:activate-display", p);
- if (p)
- home_call_ret(pane, ci->focus, "doc:attach-view", p, 1);
+ if (strcmp(ci->key, "interactive-cmd-x11window") == 0)
+ p = home_call_ret(pane, p,
+ "Window:activate-display", ci->focus);
if (p)
comm_call(ci->comm2, "cb", p);
return 1;
void edlib_init(struct pane *ed safe)
{
- call_comm("global-set-command", ed, &display_xcb, 0, NULL,
+ call_comm("global-set-command", ed, &xcb_new_display, 0, NULL,
"attach-display-x11");
call_comm("global-set-command", ed, &xcb_new_display, 0, NULL,
"interactive-cmd-x11window");
xcb_map = key_alloc();
- key_add(xcb_map, "Display:close", &xcb_close_display);
- key_add(xcb_map, "Display:set-noclose", &xcb_set_noclose);
- key_add(xcb_map, "Display:external-viewer", &xcb_external_viewer);
- key_add(xcb_map, "Display:fullscreen", &xcb_fullscreen);
- key_add(xcb_map, "Display:new", &xcb_new_display);
+ key_add(xcb_map, "Window:close", &xcb_close_display);
+ key_add(xcb_map, "Window:external-viewer", &xcb_external_viewer);
+ key_add(xcb_map, "Window:fullscreen", &xcb_fullscreen);
+ key_add(xcb_map, "Window:new", &xcb_new_display);
key_add(xcb_map, "Close", &xcb_close);
- key_add(xcb_map, "Free", &xcb_free);
key_add(xcb_map, "Draw:clear", &xcb_clear);
key_add(xcb_map, "Draw:text-size", &xcb_text_size);
key_add(xcb_map, "Draw:text", &xcb_draw_text);