29 #define G_LOG_DOMAIN "Dialogs.Window"
43 #include <xcb/xcb_atom.h>
44 #include <xcb/xcb_ewmh.h>
45 #include <xcb/xcb_icccm.h>
65 #define CLIENTSTATE 10
66 #define CLIENTWINDOWTYPE 10
75 WIN_MATCH_FIELD_TITLE,
76 WIN_MATCH_FIELD_CLASS,
79 WIN_MATCH_FIELD_DESKTOP,
81 } WinModeMatchingFields;
83 static WinModeField matching_window_fields[WIN_MATCH_NUM_FIELDS] = {
85 .field_name =
"title",
89 .field_name =
"class",
101 .field_name =
"desktop",
105 static gboolean window_matching_fields_parsed = FALSE;
110 xcb_get_window_attributes_reply_t xattr;
116 xcb_atom_t state[CLIENTSTATE];
118 xcb_atom_t window_type[CLIENTWINDOWTYPE];
124 unsigned int wmdesktopstr_len;
125 cairo_surface_t *
icon;
126 gboolean icon_checked;
127 uint32_t icon_fetch_uid;
128 gboolean thumbnail_checked;
144 unsigned int wmdn_len;
145 unsigned int clf_len;
146 unsigned int name_len;
147 unsigned int title_len;
148 unsigned int role_len;
149 GRegex *window_regex;
150 } ModeModePrivateData;
152 winlist *cache_client = NULL;
159 static winlist *winlist_new() {
160 winlist *l = g_malloc(
sizeof(winlist));
162 l->array = g_malloc_n(WINLIST + 1,
sizeof(xcb_window_t));
163 l->data = g_malloc_n(WINLIST + 1,
sizeof(client *));
176 static int winlist_append(winlist *l, xcb_window_t w, client *d) {
177 if (l->len > 0 && !(l->len % WINLIST)) {
179 g_realloc(l->array,
sizeof(xcb_window_t) * (l->len + WINLIST + 1));
180 l->data = g_realloc(l->data,
sizeof(client *) * (l->len + WINLIST + 1));
184 if (l->data == NULL || l->array == NULL) {
189 l->array[l->len++] = w;
193 static void client_free(client *c) {
198 cairo_surface_destroy(c->icon);
204 g_free(c->wmdesktopstr);
206 static void winlist_empty(winlist *l) {
208 client *c = l->data[--l->len];
221 static void winlist_free(winlist *l) {
238 static int winlist_find(winlist *l, xcb_window_t w) {
243 for (i = (l->len - 1); i >= 0; i--) {
244 if (l->array[i] == w) {
254 static void x11_cache_create(
void) {
255 if (cache_client == NULL) {
256 cache_client = winlist_new();
263 static void x11_cache_free(
void) {
264 winlist_free(cache_client);
277 static xcb_get_window_attributes_reply_t *
278 window_get_attributes(xcb_window_t w) {
279 xcb_get_window_attributes_cookie_t c =
281 xcb_get_window_attributes_reply_t *r =
289 static int client_has_state(client *c, xcb_atom_t state) {
290 for (
int i = 0; i < c->states; i++) {
291 if (c->state[i] == state) {
298 static int client_has_window_type(client *c, xcb_atom_t type) {
299 for (
int i = 0; i < c->window_types; i++) {
300 if (c->window_type[i] == type) {
308 static client *window_client(ModeModePrivateData *pd, xcb_window_t win) {
309 if (win == XCB_WINDOW_NONE) {
313 int idx = winlist_find(cache_client, win);
316 return cache_client->data[idx];
320 xcb_get_window_attributes_reply_t *attr = window_get_attributes(win);
325 client *c = g_malloc0(
sizeof(client));
329 memmove(&c->xattr, attr,
sizeof(xcb_get_window_attributes_reply_t));
331 xcb_get_property_cookie_t cky = xcb_ewmh_get_wm_state(&
xcb->
ewmh, win);
332 xcb_ewmh_get_atoms_reply_t states;
333 if (xcb_ewmh_get_wm_state_reply(&
xcb->
ewmh, cky, &states, NULL)) {
334 c->states = MIN(CLIENTSTATE, states.atoms_len);
335 memcpy(c->state, states.atoms,
336 MIN(CLIENTSTATE, states.atoms_len) *
sizeof(xcb_atom_t));
337 xcb_ewmh_get_atoms_reply_wipe(&states);
339 cky = xcb_ewmh_get_wm_window_type(&
xcb->
ewmh, win);
340 if (xcb_ewmh_get_wm_window_type_reply(&
xcb->
ewmh, cky, &states, NULL)) {
341 c->window_types = MIN(CLIENTWINDOWTYPE, states.atoms_len);
342 memcpy(c->window_type, states.atoms,
343 MIN(CLIENTWINDOWTYPE, states.atoms_len) *
sizeof(xcb_atom_t));
344 xcb_ewmh_get_atoms_reply_wipe(&states);
348 if (tmp_title == NULL) {
351 c->title = g_markup_escape_text(tmp_title, -1);
353 MAX(c->title ? g_utf8_strlen(c->title, -1) : 0, pd->title_len);
357 c->role = g_markup_escape_text(tmp_role ? tmp_role :
"", -1);
358 pd->role_len = MAX(c->role ? g_utf8_strlen(c->role, -1) : 0, pd->role_len);
362 xcb_icccm_get_wm_class_reply_t wcr;
363 if (xcb_icccm_get_wm_class_reply(
xcb->
connection, cky, &wcr, NULL)) {
364 c->class = g_markup_escape_text(wcr.class_name, -1);
365 c->name = g_markup_escape_text(wcr.instance_name, -1);
366 pd->name_len = MAX(c->name ? g_utf8_strlen(c->name, -1) : 0, pd->name_len);
367 xcb_icccm_get_wm_class_reply_wipe(&wcr);
370 xcb_get_property_cookie_t cc =
372 xcb_icccm_wm_hints_t r;
373 if (xcb_icccm_get_wm_hints_reply(
xcb->
connection, cc, &r, NULL)) {
374 c->hint_flags = r.flags;
377 winlist_append(cache_client, c->window, c);
382 unsigned int index) {
385 const winlist *ids = (winlist *)rmpd->ids;
387 int idx = winlist_find(cache_client, ids->array[index]);
389 client *c = cache_client->data[idx];
392 for (
int j = 0; match && tokens != NULL && tokens[j] != NULL; j++) {
399 if (c->title != NULL && c->title[0] !=
'\0' &&
400 matching_window_fields[WIN_MATCH_FIELD_TITLE].enabled) {
404 if (test == tokens[j]->invert && c->class != NULL &&
405 c->class[0] !=
'\0' &&
406 matching_window_fields[WIN_MATCH_FIELD_CLASS].enabled) {
410 if (test == tokens[j]->invert && c->role != NULL && c->role[0] !=
'\0' &&
411 matching_window_fields[WIN_MATCH_FIELD_ROLE].enabled) {
415 if (test == tokens[j]->invert && c->name != NULL && c->name[0] !=
'\0' &&
416 matching_window_fields[WIN_MATCH_FIELD_NAME].enabled) {
419 if (test == tokens[j]->invert && c->wmdesktopstr != NULL &&
420 c->wmdesktopstr[0] !=
'\0' &&
421 matching_window_fields[WIN_MATCH_FIELD_DESKTOP].enabled) {
434 static void window_mode_parse_fields() {
435 window_matching_fields_parsed = TRUE;
439 const char *
const sep =
",#";
441 for (
unsigned int i = 0; i < WIN_MATCH_NUM_FIELDS; i++) {
442 matching_window_fields[i].enabled = FALSE;
444 for (
char *token = strtok_r(switcher_str, sep, &savept); token != NULL;
445 token = strtok_r(NULL, sep, &savept)) {
446 if (strcmp(token,
"all") == 0) {
447 for (
unsigned int i = 0; i < WIN_MATCH_NUM_FIELDS; i++) {
448 matching_window_fields[i].enabled = TRUE;
452 gboolean matched = FALSE;
453 for (
unsigned int i = 0; i < WIN_MATCH_NUM_FIELDS; i++) {
454 const char *field_name = matching_window_fields[i].field_name;
455 if (strcmp(token, field_name) == 0) {
456 matching_window_fields[i].enabled = TRUE;
461 g_warning(
"Invalid window field name :%s", token);
465 g_free(switcher_str);
468 static unsigned int window_mode_get_num_entries(
const Mode *sw) {
469 const ModeModePrivateData *pd =
472 return pd->ids ? pd->ids->len : 0;
478 const char *invalid_desktop_name =
"n/a";
479 static const char *_window_name_list_entry(
const char *str, uint32_t length,
483 while (index < entry && offset < length) {
484 if (str[offset] == 0) {
489 if (offset >= length) {
490 return invalid_desktop_name;
494 static void _window_mode_load_data(
Mode *sw,
unsigned int cd) {
497 xcb_window_t curr_win_id;
503 xcb_get_property_cookie_t c =
505 if (!xcb_ewmh_get_active_window_reply(&
xcb->
ewmh, c, &curr_win_id, NULL)) {
510 unsigned int current_desktop = 0;
512 if (!xcb_ewmh_get_current_desktop_reply(&
xcb->
ewmh, c, ¤t_desktop,
519 xcb_ewmh_get_windows_reply_t clients = {
522 if (xcb_ewmh_get_client_list_stacking_reply(&
xcb->
ewmh, c, &clients, NULL)) {
526 if (xcb_ewmh_get_client_list_reply(&
xcb->
ewmh, c, &clients, NULL)) {
534 if (clients.windows_len > 0) {
539 pd->ids = winlist_new();
541 xcb_get_property_cookie_t prop_cookie =
543 xcb_ewmh_get_utf8_strings_reply_t names;
544 int has_names = FALSE;
545 if (xcb_ewmh_get_desktop_names_reply(&
xcb->
ewmh, prop_cookie, &names,
550 for (i = clients.windows_len - 1; i > -1; i--) {
551 client *winclient = window_client(pd, clients.windows[i]);
552 if ((winclient != NULL) && !winclient->xattr.override_redirect &&
553 !client_has_window_type(winclient,
554 xcb->
ewmh._NET_WM_WINDOW_TYPE_DOCK) &&
555 !client_has_window_type(winclient,
556 xcb->
ewmh._NET_WM_WINDOW_TYPE_DESKTOP) &&
557 !client_has_state(winclient,
xcb->
ewmh._NET_WM_STATE_SKIP_PAGER) &&
558 !client_has_state(winclient,
xcb->
ewmh._NET_WM_STATE_SKIP_TASKBAR)) {
560 MAX(pd->clf_len, (winclient->class != NULL)
561 ? (g_utf8_strlen(winclient->class, -1))
564 if (client_has_state(winclient,
565 xcb->
ewmh._NET_WM_STATE_DEMANDS_ATTENTION)) {
566 winclient->demands = TRUE;
568 if ((winclient->hint_flags & XCB_ICCCM_WM_HINT_X_URGENCY) != 0) {
569 winclient->demands = TRUE;
572 if (winclient->window == curr_win_id) {
573 winclient->active = TRUE;
576 xcb_get_property_cookie_t cookie;
577 xcb_get_property_reply_t *r;
579 winclient->wmdesktop = 0xFFFFFFFF;
580 cookie = xcb_get_property(
xcb->
connection, 0, winclient->window,
581 xcb->
ewmh._NET_WM_DESKTOP, XCB_ATOM_CARDINAL,
585 if (r->type == XCB_ATOM_CARDINAL) {
586 winclient->wmdesktop = *((uint32_t *)xcb_get_property_value(r));
590 if (winclient->wmdesktop != 0xFFFFFFFF) {
595 if (pango_parse_markup(
596 _window_name_list_entry(names.strings, names.strings_len,
597 winclient->wmdesktop),
598 -1, 0, NULL, &output, NULL, NULL)) {
599 winclient->wmdesktopstr = g_strdup(_window_name_list_entry(
600 names.strings, names.strings_len, winclient->wmdesktop));
601 winclient->wmdesktopstr_len = g_utf8_strlen(output, -1);
602 pd->wmdn_len = MAX(pd->wmdn_len, winclient->wmdesktopstr_len);
605 winclient->wmdesktopstr = g_strdup(
"Invalid name");
606 pd->wmdn_len = MAX(pd->wmdn_len,
607 g_utf8_strlen(winclient->wmdesktopstr, -1));
610 winclient->wmdesktopstr = g_markup_escape_text(
611 _window_name_list_entry(names.strings, names.strings_len,
612 winclient->wmdesktop),
615 MAX(pd->wmdn_len, g_utf8_strlen(winclient->wmdesktopstr, -1));
618 winclient->wmdesktopstr =
619 g_strdup_printf(
"%u", (uint32_t)winclient->wmdesktop);
621 MAX(pd->wmdn_len, g_utf8_strlen(winclient->wmdesktopstr, -1));
624 winclient->wmdesktopstr = g_strdup(
"");
626 MAX(pd->wmdn_len, g_utf8_strlen(winclient->wmdesktopstr, -1));
628 if (cd && winclient->wmdesktop != current_desktop) {
631 winlist_append(pd->ids, winclient->window, NULL);
636 xcb_ewmh_get_utf8_strings_reply_wipe(&names);
639 xcb_ewmh_get_windows_reply_wipe(&clients);
641 static int window_mode_init(
Mode *sw) {
643 ModeModePrivateData *pd = g_malloc0(
sizeof(*pd));
644 pd->window_regex = g_regex_new(
"{[-\\w]+(:-?[0-9]+)?}", 0, 0, NULL);
646 _window_mode_load_data(sw, FALSE);
647 if (!window_matching_fields_parsed) {
648 window_mode_parse_fields();
653 static int window_mode_init_cd(
Mode *sw) {
655 ModeModePrivateData *pd = g_malloc0(
sizeof(*pd));
656 pd->window_regex = g_regex_new(
"{[-\\w]+(:-?[0-9]+)?}", 0, 0, NULL);
658 _window_mode_load_data(sw, TRUE);
659 if (!window_matching_fields_parsed) {
660 window_mode_parse_fields();
666 static inline int act_on_window(xcb_window_t window) {
670 char window_regex[100];
672 g_snprintf(window_regex,
sizeof window_regex,
"%d", window);
675 window_regex, (
char *)0);
677 GError *error = NULL;
678 g_spawn_async(NULL, args, NULL, G_SPAWN_SEARCH_PATH, NULL, NULL, NULL,
681 char *msg = g_strdup_printf(
682 "Failed to execute action for window: '%s'\nError: '%s'", window_regex,
697 G_GNUC_UNUSED
char **input,
698 unsigned int selected_line) {
703 act_on_window(rmpd->ids->array[selected_line]);
710 uint32_t wmdesktop = 0;
711 xcb_get_property_cookie_t cookie;
712 xcb_get_property_reply_t *r;
714 unsigned int current_desktop = 0;
715 xcb_get_property_cookie_t c =
717 if (!xcb_ewmh_get_current_desktop_reply(&
xcb->
ewmh, c, ¤t_desktop,
722 cookie = xcb_get_property(
724 xcb->
ewmh._NET_WM_DESKTOP, XCB_ATOM_CARDINAL, 0, 1);
726 if (r && r->type == XCB_ATOM_CARDINAL) {
727 wmdesktop = *((uint32_t *)xcb_get_property_value(r));
729 if (r && r->type != XCB_ATOM_CARDINAL) {
731 wmdesktop = current_desktop;
736 if (wmdesktop != current_desktop) {
738 wmdesktop, XCB_CURRENT_TIME);
742 xcb_ewmh_request_change_active_window(
744 XCB_EWMH_CLIENT_SOURCE_TYPE_OTHER, XCB_CURRENT_TIME,
749 xcb_ewmh_request_close_window(
751 XCB_CURRENT_TIME, XCB_EWMH_CLIENT_SOURCE_TYPE_OTHER);
758 client_free(rmpd->ids->data[selected_line]);
759 g_free(rmpd->ids->data[selected_line]);
760 memmove(&(rmpd->ids->array[selected_line]),
761 &(rmpd->ids->array[selected_line + 1]),
762 rmpd->ids->len - selected_line);
763 memmove(&(rmpd->ids->data[selected_line]),
764 &(rmpd->ids->data[selected_line + 1]),
765 rmpd->ids->len - selected_line);
772 GError *error = NULL;
774 gsize lf_cmd_size = 0;
775 gchar *lf_cmd = g_locale_from_utf8(*input, -1, NULL, &lf_cmd_size, &error);
777 g_warning(
"Failed to convert command to locale encoding: %s",
785 run_in_term ? &context : NULL)) {
795 static void window_mode_destroy(
Mode *sw) {
798 winlist_free(rmpd->ids);
801 g_regex_unref(rmpd->window_regex);
807 const ModeModePrivateData *pd;
811 static void helper_eval_add_str(GString *str,
const char *input,
int l,
812 int max_len,
int nc) {
814 const char *input_nn = input ? input :
"";
818 spaces = MAX(0, max_len - nc);
819 g_string_append(str, input_nn);
822 int bl = g_utf8_offset_to_pointer(input_nn, l) - input_nn;
823 char *tmp = g_markup_escape_text(input_nn, bl);
824 g_string_append(str, tmp);
828 char *tmp = g_markup_escape_text(input_nn, -1);
829 g_string_append(str, tmp);
834 g_string_append_c(str,
' ');
837 static gboolean helper_eval_cb(
const GMatchInfo *info, GString *str,
839 struct arg *d = (
struct arg *)data;
842 match = g_match_info_fetch(info, 0);
845 if (match[2] ==
':') {
846 l = (int)g_ascii_strtoll(&match[3], NULL, 10);
851 if (match[1] ==
'w') {
852 helper_eval_add_str(str, d->c->wmdesktopstr, l, d->pd->wmdn_len,
853 d->c->wmdesktopstr_len);
854 }
else if (match[1] ==
'c') {
855 helper_eval_add_str(str, d->c->class, l, d->pd->clf_len,
856 g_utf8_strlen(d->c->class, -1));
857 }
else if (match[1] ==
't') {
858 helper_eval_add_str(str, d->c->title, l, d->pd->title_len,
859 g_utf8_strlen(d->c->title, -1));
860 }
else if (match[1] ==
'n') {
861 helper_eval_add_str(str, d->c->name, l, d->pd->name_len,
862 g_utf8_strlen(d->c->name, -1));
863 }
else if (match[1] ==
'r') {
864 helper_eval_add_str(str, d->c->role, l, d->pd->role_len,
865 g_utf8_strlen(d->c->role, -1));
872 static char *_generate_display_string(
const ModeModePrivateData *pd,
874 struct arg d = {pd, c};
876 0, 0, helper_eval_cb, &d, NULL);
877 return g_strchomp(res);
881 int *state, G_GNUC_UNUSED GList **list,
884 client *c = window_client(rmpd, rmpd->ids->array[selected_line]);
886 return get_entry ? g_strdup(
"Window has fanished") : NULL;
895 return get_entry ? _generate_display_string(rmpd, c) : NULL;
901 static cairo_user_data_key_t data_key;
908 static cairo_surface_t *draw_surface_from_data(
int width,
int height,
910 unsigned long int len = width * height;
912 uint32_t *buffer = g_new0(uint32_t, len);
913 cairo_surface_t *surface;
916 for (i = 0; i < len; i++) {
917 uint8_t a = (data[i] >> 24) & 0xff;
918 double alpha = a / 255.0;
919 uint8_t r = ((data[i] >> 16) & 0xff) * alpha;
920 uint8_t g = ((data[i] >> 8) & 0xff) * alpha;
921 uint8_t b = ((data[i] >> 0) & 0xff) * alpha;
922 buffer[i] = (a << 24) | (r << 16) | (g << 8) | b;
925 surface = cairo_image_surface_create_for_data(
926 (
unsigned char *)buffer, CAIRO_FORMAT_ARGB32, width, height, width * 4);
928 cairo_surface_set_user_data(surface, &data_key, buffer, g_free);
932 static cairo_surface_t *ewmh_window_icon_from_reply(xcb_get_property_reply_t *r,
933 uint32_t preferred_size) {
934 uint32_t *data, *end, *found_data = 0;
935 uint32_t found_size = 0;
937 if (!r || r->type != XCB_ATOM_CARDINAL || r->format != 32 || r->length < 2) {
941 data = (uint32_t *)xcb_get_property_value(r);
946 end = data + r->length;
952 while (data + 1 < end) {
955 uint64_t data_size = (uint64_t)data[0] * data[1];
956 if (data_size > (uint64_t)(end - data - 2)) {
962 uint32_t size = MAX(data[0], data[1]);
965 gboolean found_icon_too_small = found_size < preferred_size;
966 gboolean found_icon_too_large = found_size > preferred_size;
967 gboolean icon_empty = data[0] == 0 || data[1] == 0;
968 gboolean better_because_bigger = found_icon_too_small && size > found_size;
969 gboolean better_because_smaller =
970 found_icon_too_large && size >= preferred_size && size < found_size;
972 (better_because_bigger || better_because_smaller || found_size == 0)) {
977 data += data_size + 2;
984 return draw_surface_from_data(found_data[0], found_data[1], found_data + 2);
987 static cairo_surface_t *get_net_wm_icon(xcb_window_t xid,
988 uint32_t preferred_size) {
989 xcb_get_property_cookie_t cookie = xcb_get_property_unchecked(
992 xcb_get_property_reply_t *r =
994 cairo_surface_t *surface = ewmh_window_icon_from_reply(r, preferred_size);
998 static cairo_surface_t *
_get_icon(
const Mode *sw,
unsigned int selected_line,
1001 client *c = window_client(rmpd, rmpd->ids->array[selected_line]);
1004 c->thumbnail_checked = TRUE;
1006 if (c->icon == NULL && c->icon_checked == FALSE) {
1007 c->icon = get_net_wm_icon(rmpd->ids->array[selected_line], size);
1008 c->icon_checked = TRUE;
1010 if (c->icon == NULL && c->class) {
1011 if (c->icon_fetch_uid > 0) {
1021 Mode window_mode = {.
name =
"window",
1022 .cfg_name_key =
"display-window",
1023 ._init = window_mode_init,
1024 ._get_num_entries = window_mode_get_num_entries,
1025 ._result = window_mode_result,
1026 ._destroy = window_mode_destroy,
1027 ._token_match = window_match,
1030 ._get_completion = NULL,
1031 ._preprocess_input = NULL,
1032 .private_data = NULL,
1034 Mode window_mode_cd = {.
name =
"windowcd",
1035 .cfg_name_key =
"display-windowcd",
1036 ._init = window_mode_init_cd,
1037 ._get_num_entries = window_mode_get_num_entries,
1038 ._result = window_mode_result,
1039 ._destroy = window_mode_destroy,
1040 ._token_match = window_match,
1043 ._get_completion = NULL,
1044 ._preprocess_input = NULL,
1045 .private_data = NULL,
static char * _get_display_value(const Mode *sw, unsigned int selected_line, int *state, G_GNUC_UNUSED GList **list, int get_entry)
static cairo_surface_t * _get_icon(const Mode *sw, unsigned int selected_line, int height)
gboolean helper_execute_command(const char *wd, const char *cmd, gboolean run_in_term, RofiHelperExecuteContext *context)
int helper_parse_setup(char *string, char ***output, int *length,...)
int helper_token_match(rofi_int_matcher *const *tokens, const char *input)
cairo_surface_t * rofi_icon_fetcher_get(const uint32_t uid)
uint32_t rofi_icon_fetcher_query(const char *name, const int size)
void mode_set_private_data(Mode *mode, void *pd)
void * mode_get_private_data(const Mode *mode)
void rofi_view_hide(void)
xcb_window_t rofi_view_get_window(void)
int rofi_view_error_dialog(const char *msg, int markup)
char * window_match_fields
gboolean window_thumbnail
xcb_connection_t * connection
xcb_ewmh_connection_t ewmh
xcb_window_t focus_revert
ThemeWidget * rofi_config_find_widget(const char *name, const char *state, gboolean exact)
Property * rofi_theme_find_property(ThemeWidget *widget, PropertyType type, const char *property, gboolean exact)
cairo_surface_t * x11_helper_get_screenshot_surface_window(xcb_window_t window, int size)
WindowManagerQuirk current_window_manager
char * window_get_text_prop(xcb_window_t w, xcb_atom_t atom)
xcb_atom_t netatoms[NUM_NETATOMS]
@ WM_PANGO_WORKSPACE_NAMES
@ WM_DO_NOT_CHANGE_CURRENT_DESKTOP