commit - 8937fc85b8a4228fa4c5fbffa1ebf6887f23c56b
commit + 3e175b5632bd93893813bd27777e9c34829474bf
blob - a47faee79b414f781cbe283235f36a14bfc9c7be
blob + 21f80e098464cacb33cc59f8b3655514e8ba7bc6
--- mymenu.c
+++ mymenu.c
struct completion {
char *completion;
char *rcompletion;
- int offset; /* the x (or y, depending on the layout) coordinate at
- which the item is rendered */
+
+ /*
+ * The X (or Y, depending on the layour) at which the item is
+ * rendered
+ */
+ int offset;
};
/* Wrap the linked list of completions */
size_t length;
};
-/* idea stolen from lemonbar. ty lemonboy */
+/* idea stolen from lemonbar; ty lemonboy */
typedef union {
struct {
uint8_t b;
/* Update the given completion */
void
-update_completions(
- struct completions *cs, char *text, char **lines, char **vlines, short first_selected)
+update_completions(struct completions *cs, char *text, char **lines,
+ char **vlines, short first_selected)
{
filter(cs, text, lines, vlines);
if (first_selected && cs->length > 0)
* set to `ERR'.
*/
void
-complete(struct completions *cs, short first_selected, short p, char **text, int *textlen,
- enum state *status)
+complete(struct completions *cs, short first_selected, short p,
+ char **text, int *textlen, enum state *status)
{
struct completion *n;
int index;
* If the first is always selected and the first entry is
* different from the text, expand the text and return
*/
- if (first_selected && cs->selected == 0 && strcmp(cs->completions->completion, *text) != 0
- && !p) {
+ if (first_selected &&
+ cs->selected == 0 &&
+ strcmp(cs->completions->completion, *text) != 0 &&
+ !p) {
free(*text);
*text = strdup(cs->completions->completion);
if (text == NULL) {
if (index == -1 && p)
index = 0;
- index = cs->selected = (cs->length + (p ? index - 1 : index + 1)) % cs->length;
+ index = cs->selected = (cs->length + (p ? index - 1 : index + 1))
+ % cs->length;
n = &cs->completions[cs->selected];
* It'll return the width and set ret_width and ret_height if not NULL
*/
int
-text_extents(char *str, int len, struct rendering *r, int *ret_width, int *ret_height)
+text_extents(char *str, int len, struct rendering *r, int *ret_width,
+ int *ret_height)
{
int height, width;
XGlyphInfo gi;
}
void
-draw_string(char *str, int len, int x, int y, struct rendering *r, enum obj_type tt)
+draw_string(char *str, int len, int x, int y, struct rendering *r,
+ enum obj_type tt)
{
XftColor xftcolor;
if (tt == PROMPT)
}
int
-draw_v_box(struct rendering *r, int y, char *prefix, int prefix_width, enum obj_type t, char *text)
+draw_v_box(struct rendering *r, int y, char *prefix, int prefix_width,
+ enum obj_type t, char *text)
{
GC *border_color, bg;
int *padding, *borders;
inner_height = padding[0] + r->text_height + padding[2];
/* Border top */
- XFillRectangle(r->d, r->w, border_color[0], r->x_zero, y, r->width, borders[0]);
+ XFillRectangle(r->d, r->w, border_color[0], r->x_zero, y, r->width,
+ borders[0]);
/* Border right */
- XFillRectangle(r->d, r->w, border_color[1], r->x_zero + INNER_WIDTH(r) - borders[1], y,
- borders[1], ret);
+ XFillRectangle(r->d, r->w, border_color[1],
+ r->x_zero + INNER_WIDTH(r) - borders[1], y, borders[1], ret);
/* Border bottom */
XFillRectangle(r->d, r->w, border_color[2], r->x_zero,
- y + borders[0] + padding[0] + r->text_height + padding[2], r->width, borders[2]);
+ y + borders[0] + padding[0] + r->text_height + padding[2],
+ r->width, borders[2]);
/* Border left */
- XFillRectangle(r->d, r->w, border_color[3], r->x_zero, y, borders[3], ret);
+ XFillRectangle(r->d, r->w, border_color[3], r->x_zero, y, borders[3],
+ ret);
/* bg */
x = r->x_zero + borders[3];
}
int
-draw_h_box(struct rendering *r, int x, char *prefix, int prefix_width, enum obj_type t, char *text)
+draw_h_box(struct rendering *r, int x, char *prefix, int prefix_width,
+ enum obj_type t, char *text)
{
GC *border_color, bg;
int *padding, *borders;
break;
}
- if (padding[0] < 0 || padding[2] < 0)
- padding[0] = padding[2]
- = (INNER_HEIGHT(r) - borders[0] - borders[2] - r->text_height) / 2;
+ if (padding[0] < 0 || padding[2] < 0) {
+ padding[0] = INNER_HEIGHT(r) - borders[0] - borders[2]
+ - r->text_height;
+ padding[0] /= 2;
+ padding[2] = padding[0];
+ }
+
/* If they are still lesser than 0, set 'em to 0 */
if (padding[0] < 0 || padding[2] < 0)
padding[0] = padding[2] = 0;
inner_height = INNER_HEIGHT(r) - borders[0] - borders[2];
/* Border top */
- XFillRectangle(r->d, r->w, border_color[0], x, r->y_zero, ret, borders[0]);
+ XFillRectangle(r->d, r->w, border_color[0], x, r->y_zero, ret,
+ borders[0]);
/* Border right */
- XFillRectangle(r->d, r->w, border_color[1], x + borders[3] + inner_width, r->y_zero,
- borders[1], INNER_HEIGHT(r));
+ XFillRectangle(r->d, r->w, border_color[1],
+ x + borders[3] + inner_width, r->y_zero, borders[1],
+ INNER_HEIGHT(r));
/* Border bottom */
- XFillRectangle(r->d, r->w, border_color[2], x, r->y_zero + INNER_HEIGHT(r) - borders[2],
- ret, borders[2]);
+ XFillRectangle(r->d, r->w, border_color[2], x,
+ r->y_zero + INNER_HEIGHT(r) - borders[2], ret,
+ borders[2]);
/* Border left */
- XFillRectangle(r->d, r->w, border_color[3], x, r->y_zero, borders[3], INNER_HEIGHT(r));
+ XFillRectangle(r->d, r->w, border_color[3], x, r->y_zero, borders[3],
+ INNER_HEIGHT(r));
/* bg */
x += borders[3];
return ret;
}
-/* ,-----------------------------------------------------------------, */
-/* | 20 char text | completion | completion | completion | compl | */
-/* `-----------------------------------------------------------------' */
+/*
+ * ,-----------------------------------------------------------------,
+ * | 20 char text | completion | completion | completion | compl |
+ * `-----------------------------------------------------------------'
+ */
void
draw_horizontally(struct rendering *r, char *text, struct completions *cs)
{
x += draw_h_box(r, x, r->ps1, r->ps1w, PROMPT, text);
for (i = r->offset; i < cs->length; ++i) {
- enum obj_type t = cs->selected == (ssize_t)i ? COMPL_HIGH : COMPL;
+ enum obj_type t;
+ if (cs->selected == (ssize_t)i)
+ t = COMPL_HIGH;
+ else
+ t = COMPL;
+
cs->completions[i].offset = x;
- x += draw_h_box(r, x, NULL, 0, t, cs->completions[i].completion);
+ x += draw_h_box(r, x, NULL, 0, t,
+ cs->completions[i].completion);
if (x > INNER_WIDTH(r))
break;
cs->completions[i].offset = -1;
}
-/* ,-----------------------------------------------------------------, */
-/* | prompt | */
-/* |-----------------------------------------------------------------| */
-/* | completion | */
-/* |-----------------------------------------------------------------| */
-/* | completion | */
-/* `-----------------------------------------------------------------' */
+/*
+ * ,-----------------------------------------------------------------,
+ * | prompt |
+ * |-----------------------------------------------------------------|
+ * | completion |
+ * |-----------------------------------------------------------------|
+ * | completion |
+ * `-----------------------------------------------------------------'
+ */
void
draw_vertically(struct rendering *r, char *text, struct completions *cs)
{
y += draw_v_box(r, y, r->ps1, r->ps1w, PROMPT, text);
for (i = r->offset; i < cs->length; ++i) {
- enum obj_type t = cs->selected == (ssize_t)i ? COMPL_HIGH : COMPL;
+ enum obj_type t;
+ if (cs->selected == (ssize_t)i)
+ t = COMPL_HIGH;
+ else
+ t = COMPL;
+
cs->completions[i].offset = y;
- y += draw_v_box(r, y, NULL, 0, t, cs->completions[i].completion);
+ y += draw_v_box(r, y, NULL, 0, t,
+ cs->completions[i].completion);
if (y > INNER_HEIGHT(r))
break;
draw(struct rendering *r, char *text, struct completions *cs)
{
/* Draw the background */
- XFillRectangle(
- r->d, r->w, r->bgs[1], r->x_zero, r->y_zero, INNER_WIDTH(r), INNER_HEIGHT(r));
+ XFillRectangle(r->d, r->w, r->bgs[1], r->x_zero, r->y_zero,
+ INNER_WIDTH(r), INNER_HEIGHT(r));
/* Draw the contents */
if (r->horizontal_layout)
/* Draw the borders */
if (r->borders[0] != 0)
- XFillRectangle(r->d, r->w, r->borders_bg[0], 0, 0, r->width, r->borders[0]);
+ XFillRectangle(r->d, r->w, r->borders_bg[0], 0, 0, r->width,
+ r->borders[0]);
if (r->borders[1] != 0)
- XFillRectangle(r->d, r->w, r->borders_bg[1], r->width - r->borders[1], 0,
- r->borders[1], r->height);
+ XFillRectangle(r->d, r->w, r->borders_bg[1],
+ r->width - r->borders[1], 0, r->borders[1],
+ r->height);
if (r->borders[2] != 0)
- XFillRectangle(r->d, r->w, r->borders_bg[2], 0, r->height - r->borders[2], r->width,
- r->borders[2]);
+ XFillRectangle(r->d, r->w, r->borders_bg[2], 0,
+ r->height - r->borders[2], r->width, r->borders[2]);
if (r->borders[3] != 0)
- XFillRectangle(r->d, r->w, r->borders_bg[3], 0, 0, r->borders[3], r->height);
+ XFillRectangle(r->d, r->w, r->borders_bg[3], 0, 0,
+ r->borders[3], r->height);
/* render! */
XFlush(r->d);
XSizeHints *size_hint;
type = XInternAtom(d, "_NET_WM_WINDOW_TYPE_DOCK", 0);
- XChangeProperty(d, w, XInternAtom(d, "_NET_WM_WINDOW_TYPE", 0), XInternAtom(d, "ATOM", 0),
- 32, PropModeReplace, (unsigned char *)&type, 1);
+ XChangeProperty(d, w, XInternAtom(d, "_NET_WM_WINDOW_TYPE", 0),
+ XInternAtom(d, "ATOM", 0), 32, PropModeReplace,
+ (unsigned char *)&type, 1);
/* some window managers honor this properties */
type = XInternAtom(d, "_NET_WM_STATE_ABOVE", 0);
- XChangeProperty(d, w, XInternAtom(d, "_NET_WM_STATE", 0), XInternAtom(d, "ATOM", 0), 32,
- PropModeReplace, (unsigned char *)&type, 1);
+ XChangeProperty(d, w, XInternAtom(d, "_NET_WM_STATE", 0),
+ XInternAtom(d, "ATOM", 0), 32, PropModeReplace,
+ (unsigned char *)&type, 1);
type = XInternAtom(d, "_NET_WM_STATE_FOCUSED", 0);
- XChangeProperty(d, w, XInternAtom(d, "_NET_WM_STATE", 0), XInternAtom(d, "ATOM", 0), 32,
- PropModeAppend, (unsigned char *)&type, 1);
+ XChangeProperty(d, w, XInternAtom(d, "_NET_WM_STATE", 0),
+ XInternAtom(d, "ATOM", 0), 32, PropModeAppend,
+ (unsigned char *)&type, 1);
/* Setting window hints */
class_hint = XAllocClassHint();
{
int i;
for (i = 0; i < 100; i++) {
- if (XGrabKeyboard(d, w, 1, GrabModeAsync, GrabModeAsync, CurrentTime)
- == GrabSuccess)
+ if (XGrabKeyboard(d, w, 1, GrabModeAsync, GrabModeAsync,
+ CurrentTime) == GrabSuccess)
return 1;
usleep(1000);
}
return i;
}
-/* Like parse_integer but recognize the percentages (i.e. strings ending with
- * `%') */
+/*
+ * Like parse_integer but recognize the percentages (i.e. strings
+ * ending with `%')
+ */
int
parse_int_with_percentage(const char *str, int default_value, int max)
{
* - my y coordinate of the mouse
*/
int
-parse_int_with_pos(Display *d, const char *str, int default_value, int max, int self)
+parse_int_with_pos(Display *d, const char *str, int default_value, int max,
+ int self)
{
if (!strcmp(str, "start"))
return 0;
if (i == 3)
ret[3] = strdup(ret[1]);
- /* before we didn't check for the return type of strdup, here we will
+ /*
+ * before we didn't check for the return type of strdup, here
+ * we will
*/
any_null = 0;
Xutf8LookupString(xic, ev, str, SYM_BUF_SIZE, 0, &s);
if (s == XBufferOverflow) {
fprintf(stderr,
- "Buffer overflow when trying to create keyboard "
- "symbol map.\n");
+ "Buffer overflow when trying to create keyboard "
+ "symbol map.\n");
return EXIT;
}
}
void
-confirm(enum state *status, struct rendering *r, struct completions *cs, char **text, int *textlen)
+confirm(enum state *status, struct rendering *r, struct completions *cs,
+ char **text, int *textlen)
{
if ((cs->selected != -1) || (cs->length > 0 && r->first_selected)) {
/* if there is something selected expand it and return */
*status = LOOPING;
}
-/* cs: completion list
+/*
+ * cs: completion list
* offset: the offset of the click
* first: the first (rendered) item
* def: the default action
*/
enum action
-select_clicked(struct completions *cs, size_t offset, size_t first, enum action def)
+select_clicked(struct completions *cs, size_t offset, size_t first,
+ enum action def)
{
ssize_t selected = first;
int set = 0;
}
enum action
-handle_mouse(struct rendering *r, struct completions *cs, XButtonPressedEvent *e)
+handle_mouse(struct rendering *r, struct completions *cs,
+ XButtonPressedEvent *e)
{
size_t off;
/* event loop */
enum state
-loop(struct rendering *r, char **text, int *textlen, struct completions *cs, char **lines,
- char **vlines)
+loop(struct rendering *r, char **text, int *textlen, struct completions *cs,
+ char **lines, char **vlines)
{
enum state status = LOOPING;
char *input = NULL;
if (e.type == KeyPress)
- a = parse_event(r->d, (XKeyPressedEvent *)&e, r->xic, &input);
+ a = parse_event(r->d, (XKeyPressedEvent *)&e,
+ r->xic, &input);
else
- a = handle_mouse(r, cs, (XButtonPressedEvent *)&e);
+ a = handle_mouse(r, cs,
+ (XButtonPressedEvent *)&e);
switch (a) {
case NO_OP:
}
case PREV_COMPL: {
- complete(cs, r->first_selected, 1, text, textlen, &status);
+ complete(cs, r->first_selected, 1, text,
+ textlen, &status);
r->offset = cs->selected;
break;
}
case NEXT_COMPL: {
- complete(cs, r->first_selected, 0, text, textlen, &status);
+ complete(cs, r->first_selected, 0, text,
+ textlen, &status);
r->offset = cs->selected;
break;
}
case DEL_CHAR:
popc(*text);
- update_completions(cs, *text, lines, vlines, r->first_selected);
+ update_completions(cs, *text, lines, vlines,
+ r->first_selected);
r->offset = 0;
break;
case DEL_WORD: {
popw(*text);
- update_completions(cs, *text, lines, vlines, r->first_selected);
+ update_completions(cs, *text, lines, vlines,
+ r->first_selected);
break;
}
int i;
for (i = 0; i < *textlen; ++i)
*(*text + i) = 0;
- update_completions(cs, *text, lines, vlines, r->first_selected);
+ update_completions(cs, *text, lines, vlines,
+ r->first_selected);
r->offset = 0;
break;
}
break;
for (i = 0; i < str_len; ++i) {
- *textlen = pushc(text, *textlen, input[i]);
+ *textlen = pushc(text, *textlen,
+ input[i]);
if (*textlen == -1) {
fprintf(stderr,
- "Memory allocation "
- "error\n");
+ "Memory allocation "
+ "error\n");
status = ERR;
break;
}
}
if (status != ERR) {
- update_completions(
- cs, *text, lines, vlines, r->first_selected);
+ update_completions(cs, *text, lines,
+ vlines, r->first_selected);
free(input);
}
XFree(xis);
if (!best_match_style)
- fprintf(stderr, "No matching input style could be determined\n");
+ fprintf(stderr,
+ "No matching input style could be determined\n");
- r->xic = XCreateIC(r->xim, XNInputStyle, best_match_style, XNClientWindow, r->w,
- XNFocusWindow, r->w, NULL);
+ r->xic = XCreateIC(r->xim, XNInputStyle, best_match_style,
+ XNClientWindow, r->w, XNFocusWindow, r->w, NULL);
if (r->xic == NULL)
err(1, "XCreateIC");
}
void
-create_window(struct rendering *r, Window parent_window, Colormap cmap, XVisualInfo vinfo, int x,
- int y, int ox, int oy, unsigned long background_pixel)
+create_window(struct rendering *r, Window parent_window, Colormap cmap,
+ XVisualInfo vinfo, int x, int y, int ox, int oy,
+ unsigned long background_pixel)
{
XSetWindowAttributes attr;
+ unsigned long vmask;
/* Create the window */
attr.colormap = cmap;
attr.override_redirect = 1;
attr.border_pixel = 0;
attr.background_pixel = background_pixel;
- attr.event_mask = StructureNotifyMask | ExposureMask | KeyPressMask | KeymapStateMask
- | ButtonPress | VisibilityChangeMask;
+ attr.event_mask = StructureNotifyMask | ExposureMask | KeyPressMask
+ | KeymapStateMask | ButtonPress | VisibilityChangeMask;
+ vmask = CWBorderPixel | CWBackPixel | CWColormap | CWEventMask |
+ CWOverrideRedirect;
+
r->w = XCreateWindow(r->d, parent_window, x + ox, y + oy, r->width, r->height, 0,
- vinfo.depth, InputOutput, vinfo.visual,
- CWBorderPixel | CWBackPixel | CWColormap | CWEventMask | CWOverrideRedirect, &attr);
+ vinfo.depth, InputOutput, vinfo.visual, vmask, &attr);
}
void
usage(char *prgname)
{
fprintf(stderr,
- "%s [-Aahmv] [-B colors] [-b size] [-C color] [-c color]\n"
- " [-d separator] [-e window] [-f font] [-G color] [-g "
- "size]\n"
- " [-H height] [-I color] [-i size] [-J color] [-j "
- "size] [-l layout]\n"
- " [-P padding] [-p prompt] [-S color] [-s color] [-T "
- "color]\n"
- " [-t color] [-W width] [-x coord] [-y coord]\n",
- prgname);
+ "%s [-Aahmv] [-B colors] [-b size] [-C color] [-c color]\n"
+ " [-d separator] [-e window] [-f font] [-G color] [-g "
+ "size]\n"
+ " [-H height] [-I color] [-i size] [-J color] [-j "
+ "size] [-l layout]\n"
+ " [-P padding] [-p prompt] [-S color] [-s color] [-T "
+ "color]\n"
+ " [-t color] [-W width] [-x coord] [-y coord]\n",
+ prgname);
}
int
r.ch_borders[i] = 0;
}
- /* the prompt. We duplicate the string so later is easy to
- * free (in the case it's been overwritten by the user) */
+ /*
+ * The prompt. We duplicate the string so later is easy to
+ * free (in the case it's been overwritten by the user)
+ */
if ((r.ps1 = strdup("$ ")) == NULL)
err(1, "strdup");
}
XMatchVisualInfo(r.d, DefaultScreen(r.d), 32, TrueColor, &vinfo);
- cmap = XCreateColormap(r.d, XDefaultRootWindow(r.d), vinfo.visual, AllocNone);
+ cmap = XCreateColormap(r.d, XDefaultRootWindow(r.d), vinfo.visual,
+ AllocNone);
fgs[0] = fgs[1] = parse_color("#fff", NULL);
fgs[2] = parse_color("#000", NULL);
bgs[0] = bgs[1] = parse_color("#000", NULL);
bgs[2] = parse_color("#fff", NULL);
- borders_bg[0] = borders_bg[1] = borders_bg[2] = borders_bg[3] = parse_color("#000", NULL);
+ borders_bg[0] = borders_bg[1] = borders_bg[2] = borders_bg[3] =
+ parse_color("#000", NULL);
p_borders_bg[0] = p_borders_bg[1] = p_borders_bg[2] = p_borders_bg[3]
= parse_color("#000", NULL);