+
+/*
+ * Gets the first matching client for the given window class/window title.
+ * If the paramater specific is set to a specific client, only this one
+ * will be checked.
+ *
+ */
+Client *get_matching_client(xcb_connection_t *conn, const char *window_classtitle,
+ Client *specific) {
+ char *to_class, *to_title, *to_title_ucs = NULL;
+ int to_title_ucs_len = 0;
+ Client *matching = NULL;
+
+ to_class = sstrdup(window_classtitle);
+
+ /* If a title was specified, split both strings at the slash */
+ if ((to_title = strstr(to_class, "/")) != NULL) {
+ *(to_title++) = '\0';
+ /* Convert to UCS-2 */
+ to_title_ucs = convert_utf8_to_ucs2(to_title, &to_title_ucs_len);
+ }
+
+ /* If we were given a specific client we only check if that one matches */
+ if (specific != NULL) {
+ if (client_matches_class_name(specific, to_class, to_title, to_title_ucs, to_title_ucs_len))
+ matching = specific;
+ goto done;
+ }
+
+ LOG("Getting clients for class \"%s\" / title \"%s\"\n", to_class, to_title);
+ Workspace *ws;
+ TAILQ_FOREACH(ws, workspaces, workspaces) {
+ if (ws->screen == NULL)
+ continue;
+
+ Client *client;
+ SLIST_FOREACH(client, &(ws->focus_stack), focus_clients) {
+ LOG("Checking client with class=%s, name=%s\n", client->window_class, client->name);
+ if (!client_matches_class_name(client, to_class, to_title, to_title_ucs, to_title_ucs_len))
+ continue;
+
+ matching = client;
+ goto done;
+ }
+ }
+
+done:
+ free(to_class);
+ FREE(to_title_ucs);
+ return matching;
+}
+
+#if defined(__OpenBSD__)
+
+/*
+ * Taken from FreeBSD
+ * Find the first occurrence of the byte string s in byte string l.
+ *
+ */
+void *memmem(const void *l, size_t l_len, const void *s, size_t s_len) {
+ register char *cur, *last;
+ const char *cl = (const char *)l;
+ const char *cs = (const char *)s;
+
+ /* we need something to compare */
+ if (l_len == 0 || s_len == 0)
+ return NULL;
+
+ /* "s" must be smaller or equal to "l" */
+ if (l_len < s_len)
+ return NULL;
+
+ /* special case where s_len == 1 */
+ if (s_len == 1)
+ return memchr(l, (int)*cs, l_len);
+
+ /* the last position where its possible to find "s" in "l" */
+ last = (char *)cl + l_len - s_len;
+
+ for (cur = (char *)cl; cur <= last; cur++)
+ if (cur[0] == cs[0] && memcmp(cur, cs, s_len) == 0)
+ return cur;
+
+ return NULL;
+}
+
+#endif
+