+/* warp pointer to center of selected item */
+static void
+warppointer(struct Menu *menu, struct Item *item)
+{
+ if (menu == NULL || item == NULL)
+ return;
+ if (menu->selected) {
+ XWarpPointer(dpy, None, menu->win, 0, 0, 0, 0, menu->w / 2, item->y + item->h / 2);
+ }
+}
+
+/* append buf into text */
+static int
+append(char *text, char *buf, size_t textsize, size_t buflen)
+{
+ size_t textlen;
+
+ textlen = strlen(text);
+ if (iscntrl(*buf))
+ return 0;
+ if (textlen + buflen > textsize - 1)
+ return 0;
+ if (buflen < 1)
+ return 0;
+ memcpy(text + textlen, buf, buflen);
+ text[textlen + buflen] = '\0';
+ return 1;
+}
+
+/* get item in menu matching text from given direction (or from beginning, if dir = 0) */
+static struct Item *
+matchitem(struct Menu *menu, char *text, int dir)
+{
+ struct Item *item, *lastitem;
+ char *s;
+ size_t textlen;
+
+ for (lastitem = menu->list; lastitem && lastitem->next; lastitem = lastitem->next)
+ ;
+ textlen = strlen(text);
+ if (dir < 0) {
+ if (menu->selected && menu->selected->prev)
+ item = menu->selected->prev;
+ else
+ item = lastitem;
+ } else if (dir > 0) {
+ if (menu->selected && menu->selected->next)
+ item = menu->selected->next;
+ else
+ item = menu->list;
+ } else {
+ item = menu->list;
+ }
+ /* find next item from selected item */
+ for ( ; item; item = (dir < 0) ? item->prev : item->next)
+ for (s = item->label; s && *s; s++)
+ if (strncasecmp(s, text, textlen) == 0)
+ return item;
+ /* if not found, try to find from the beginning/end of list */
+ if (dir > 0) {
+ for (item = menu->list ; item; item = item->next) {
+ for (s = item->label; s && *s; s++) {
+ if (strncasecmp(s, text, textlen) == 0) {
+ return item;
+ }
+ }
+ }
+ } else {
+ for (item = lastitem ; item; item = item->prev) {
+ for (s = item->label; s && *s; s++) {
+ if (strncasecmp(s, text, textlen) == 0) {
+ return item;
+ }
+ }
+ }
+ }
+ return NULL;
+}
+
+/* check keysyms defined on config.h */
+static KeySym
+normalizeksym(KeySym ksym)
+{
+ if (ksym == KSYMFIRST)
+ return XK_Home;
+ if (ksym == KSYMLAST)
+ return XK_End;
+ if (ksym == KSYMUP)
+ return XK_Up;
+ if (ksym == KSYMDOWN)
+ return XK_Down;
+ if (ksym == KSYMLEFT)
+ return XK_Left;
+ if (ksym == KSYMRIGHT)
+ return XK_Right;
+ return ksym;
+}
+