i3
|
00001 /* 00002 * vim:ts=4:sw=4:expandtab 00003 * 00004 * i3 - an improved dynamic tiling window manager 00005 * © 2009-2011 Michael Stapelberg and contributors (see also: LICENSE) 00006 * 00007 * A "match" is a data structure which acts like a mask or expression to match 00008 * certain windows or not. For example, when using commands, you can specify a 00009 * command like this: [title="*Firefox*"] kill. The title member of the match 00010 * data structure will then be filled and i3 will check each window using 00011 * match_matches_window() to find the windows affected by this command. 00012 * 00013 */ 00014 #include "all.h" 00015 00016 /* From sys/time.h, not sure if it’s available on all systems. */ 00017 # define _i3_timercmp(a, b, CMP) \ 00018 (((a).tv_sec == (b).tv_sec) ? \ 00019 ((a).tv_usec CMP (b).tv_usec) : \ 00020 ((a).tv_sec CMP (b).tv_sec)) 00021 00022 /* 00023 * Initializes the Match data structure. This function is necessary because the 00024 * members representing boolean values (like dock) need to be initialized with 00025 * -1 instead of 0. 00026 * 00027 */ 00028 void match_init(Match *match) { 00029 memset(match, 0, sizeof(Match)); 00030 match->dock = -1; 00031 match->urgent = U_DONTCHECK; 00032 } 00033 00034 /* 00035 * Check if a match is empty. This is necessary while parsing commands to see 00036 * whether the user specified a match at all. 00037 * 00038 */ 00039 bool match_is_empty(Match *match) { 00040 /* we cannot simply use memcmp() because the structure is part of a 00041 * TAILQ and I don’t want to start with things like assuming that the 00042 * last member of a struct really is at the end in memory… */ 00043 return (match->title == NULL && 00044 match->mark == NULL && 00045 match->application == NULL && 00046 match->class == NULL && 00047 match->instance == NULL && 00048 match->role == NULL && 00049 match->urgent == U_DONTCHECK && 00050 match->id == XCB_NONE && 00051 match->con_id == NULL && 00052 match->dock == -1 && 00053 match->floating == M_ANY); 00054 } 00055 00056 /* 00057 * Copies the data of a match from src to dest. 00058 * 00059 */ 00060 void match_copy(Match *dest, Match *src) { 00061 memcpy(dest, src, sizeof(Match)); 00062 00063 /* The DUPLICATE_REGEX macro creates a new regular expression from the 00064 * ->pattern of the old one. It therefore does use a little more memory then 00065 * with a refcounting system, but it’s easier this way. */ 00066 #define DUPLICATE_REGEX(field) do { \ 00067 if (src->field != NULL) \ 00068 dest->field = regex_new(src->field->pattern); \ 00069 } while (0) 00070 00071 DUPLICATE_REGEX(title); 00072 DUPLICATE_REGEX(mark); 00073 DUPLICATE_REGEX(application); 00074 DUPLICATE_REGEX(class); 00075 DUPLICATE_REGEX(instance); 00076 DUPLICATE_REGEX(role); 00077 } 00078 00079 /* 00080 * Check if a match data structure matches the given window. 00081 * 00082 */ 00083 bool match_matches_window(Match *match, i3Window *window) { 00084 LOG("Checking window 0x%08x (class %s)\n", window->id, window->class_class); 00085 00086 if (match->class != NULL) { 00087 if (window->class_class != NULL && 00088 regex_matches(match->class, window->class_class)) { 00089 LOG("window class matches (%s)\n", window->class_class); 00090 } else { 00091 return false; 00092 } 00093 } 00094 00095 if (match->instance != NULL) { 00096 if (window->class_instance != NULL && 00097 regex_matches(match->instance, window->class_instance)) { 00098 LOG("window instance matches (%s)\n", window->class_instance); 00099 } else { 00100 return false; 00101 } 00102 } 00103 00104 if (match->id != XCB_NONE) { 00105 if (window->id == match->id) { 00106 LOG("match made by window id (%d)\n", window->id); 00107 } else { 00108 LOG("window id does not match\n"); 00109 return false; 00110 } 00111 } 00112 00113 if (match->title != NULL) { 00114 if (window->name_json != NULL && 00115 regex_matches(match->title, window->name_json)) { 00116 LOG("title matches (%s)\n", window->name_json); 00117 } else { 00118 return false; 00119 } 00120 } 00121 00122 if (match->role != NULL) { 00123 if (window->role != NULL && 00124 regex_matches(match->role, window->role)) { 00125 LOG("window_role matches (%s)\n", window->role); 00126 } else { 00127 return false; 00128 } 00129 } 00130 00131 Con *con = NULL; 00132 if (match->urgent == U_LATEST) { 00133 /* if the window isn't urgent, no sense in searching */ 00134 if (window->urgent.tv_sec == 0) { 00135 return false; 00136 } 00137 /* if we find a window that is newer than this one, bail */ 00138 TAILQ_FOREACH(con, &all_cons, all_cons) { 00139 if ((con->window != NULL) && 00140 _i3_timercmp(con->window->urgent, window->urgent, >)) { 00141 return false; 00142 } 00143 } 00144 LOG("urgent matches latest\n"); 00145 } 00146 00147 if (match->urgent == U_OLDEST) { 00148 /* if the window isn't urgent, no sense in searching */ 00149 if (window->urgent.tv_sec == 0) { 00150 return false; 00151 } 00152 /* if we find a window that is older than this one (and not 0), bail */ 00153 TAILQ_FOREACH(con, &all_cons, all_cons) { 00154 if ((con->window != NULL) && 00155 (con->window->urgent.tv_sec != 0) && 00156 _i3_timercmp(con->window->urgent, window->urgent, <)) { 00157 return false; 00158 } 00159 } 00160 LOG("urgent matches oldest\n"); 00161 } 00162 00163 if (match->dock != -1) { 00164 if ((window->dock == W_DOCK_TOP && match->dock == M_DOCK_TOP) || 00165 (window->dock == W_DOCK_BOTTOM && match->dock == M_DOCK_BOTTOM) || 00166 ((window->dock == W_DOCK_TOP || window->dock == W_DOCK_BOTTOM) && 00167 match->dock == M_DOCK_ANY) || 00168 (window->dock == W_NODOCK && match->dock == M_NODOCK)) { 00169 LOG("dock status matches\n"); 00170 } else { 00171 LOG("dock status does not match\n"); 00172 return false; 00173 } 00174 } 00175 00176 /* We don’t check the mark because this function is not even called when 00177 * the mark would have matched - it is checked in cmdparse.y itself */ 00178 if (match->mark != NULL) { 00179 LOG("mark does not match\n"); 00180 return false; 00181 } 00182 00183 return true; 00184 } 00185 00186 /* 00187 * Frees the given match. It must not be used afterwards! 00188 * 00189 */ 00190 void match_free(Match *match) { 00191 /* First step: free the regex fields / patterns */ 00192 regex_free(match->title); 00193 regex_free(match->application); 00194 regex_free(match->class); 00195 regex_free(match->instance); 00196 regex_free(match->mark); 00197 regex_free(match->role); 00198 00199 /* Second step: free the regex helper struct itself */ 00200 FREE(match->title); 00201 FREE(match->application); 00202 FREE(match->class); 00203 FREE(match->instance); 00204 FREE(match->mark); 00205 FREE(match->role); 00206 }