2015-02-12 16:16:32 -05:00
|
|
|
/**
|
|
|
|
* rofi
|
|
|
|
*
|
|
|
|
* MIT/X11 License
|
2015-12-31 18:27:00 -05:00
|
|
|
* Copyright 2013-2016 Qball Cow <qball@gmpclient.org>
|
2015-02-12 16:16:32 -05:00
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining
|
|
|
|
* a copy of this software and associated documentation files (the
|
|
|
|
* "Software"), to deal in the Software without restriction, including
|
|
|
|
* without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
* distribute, sublicense, and/or sell copies of the Software, and to
|
|
|
|
* permit persons to whom the Software is furnished to do so, subject to
|
|
|
|
* the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be
|
|
|
|
* included in all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
|
|
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
|
|
|
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
|
|
|
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
|
|
|
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
|
|
|
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
#include <config.h>
|
2015-09-14 02:57:10 -04:00
|
|
|
|
|
|
|
#ifdef WINDOW_MODE
|
|
|
|
|
2015-02-12 16:16:32 -05:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <strings.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
2016-02-27 14:45:47 -05:00
|
|
|
#include <xcb/xcb.h>
|
2016-02-27 15:55:43 -05:00
|
|
|
#include <xcb/xcb_ewmh.h>
|
2016-02-27 18:09:05 -05:00
|
|
|
#include <xcb/xcb_icccm.h>
|
2015-02-12 16:16:32 -05:00
|
|
|
|
2016-03-04 11:03:24 -05:00
|
|
|
#include <glib.h>
|
|
|
|
|
2016-03-01 12:11:55 -05:00
|
|
|
#include "xcb-internal.h"
|
|
|
|
#include "xcb.h"
|
|
|
|
|
2015-02-12 16:16:32 -05:00
|
|
|
#include "rofi.h"
|
2016-01-07 10:01:56 -05:00
|
|
|
#include "settings.h"
|
2015-02-12 16:16:32 -05:00
|
|
|
#include "helper.h"
|
2016-01-09 10:22:09 -05:00
|
|
|
#include "textbox.h"
|
2015-02-12 16:16:32 -05:00
|
|
|
#include "x11-helper.h"
|
2015-02-12 16:26:28 -05:00
|
|
|
#include "i3-support.h"
|
2015-03-25 03:36:19 -04:00
|
|
|
#include "dialogs/window.h"
|
2015-02-12 16:16:32 -05:00
|
|
|
|
2015-08-27 15:09:12 -04:00
|
|
|
#define WINLIST 32
|
2015-02-12 16:16:32 -05:00
|
|
|
|
2015-08-27 15:09:12 -04:00
|
|
|
#define CLIENTTITLE 100
|
|
|
|
#define CLIENTCLASS 50
|
|
|
|
#define CLIENTSTATE 10
|
|
|
|
#define CLIENTWINDOWTYPE 10
|
|
|
|
#define CLIENTROLE 50
|
2015-02-12 16:16:32 -05:00
|
|
|
|
2015-03-04 16:47:52 -05:00
|
|
|
// a manageable window
|
2015-02-12 16:16:32 -05:00
|
|
|
typedef struct
|
|
|
|
{
|
2016-02-28 09:32:53 -05:00
|
|
|
xcb_window_t window;
|
2016-02-27 16:55:47 -05:00
|
|
|
xcb_get_window_attributes_reply_t xattr;
|
2016-02-28 09:32:53 -05:00
|
|
|
char *title;
|
|
|
|
char *class;
|
|
|
|
char *name;
|
|
|
|
char *role;
|
|
|
|
int states;
|
|
|
|
xcb_atom_t state[CLIENTSTATE];
|
|
|
|
int window_types;
|
|
|
|
xcb_atom_t window_type[CLIENTWINDOWTYPE];
|
|
|
|
int active;
|
|
|
|
int demands;
|
|
|
|
long hint_flags;
|
2015-02-12 16:16:32 -05:00
|
|
|
} client;
|
2016-02-28 05:04:09 -05:00
|
|
|
|
2015-02-12 16:16:32 -05:00
|
|
|
// window lists
|
|
|
|
typedef struct
|
|
|
|
{
|
2016-02-28 06:18:15 -05:00
|
|
|
xcb_window_t *array;
|
2016-02-28 09:32:53 -05:00
|
|
|
client **data;
|
|
|
|
int len;
|
2015-02-12 16:16:32 -05:00
|
|
|
} winlist;
|
|
|
|
|
|
|
|
winlist *cache_client = NULL;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a window list, pre-seeded with WINLIST entries.
|
|
|
|
*
|
|
|
|
* @returns A new window list.
|
|
|
|
*/
|
|
|
|
static winlist* winlist_new ()
|
|
|
|
{
|
|
|
|
winlist *l = g_malloc ( sizeof ( winlist ) );
|
|
|
|
l->len = 0;
|
2016-02-28 06:18:15 -05:00
|
|
|
l->array = g_malloc_n ( WINLIST + 1, sizeof ( xcb_window_t ) );
|
2016-02-23 13:34:48 -05:00
|
|
|
l->data = g_malloc_n ( WINLIST + 1, sizeof ( client* ) );
|
2015-02-12 16:16:32 -05:00
|
|
|
return l;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param l The winlist.
|
|
|
|
* @param w The window to add.
|
|
|
|
* @param d Data pointer.
|
|
|
|
*
|
|
|
|
* Add one entry. If Full, extend with WINLIST entries.
|
|
|
|
*
|
|
|
|
* @returns 0 if failed, 1 is successful.
|
|
|
|
*/
|
2016-02-28 06:18:15 -05:00
|
|
|
static int winlist_append ( winlist *l, xcb_window_t w, client *d )
|
2015-02-12 16:16:32 -05:00
|
|
|
{
|
|
|
|
if ( l->len > 0 && !( l->len % WINLIST ) ) {
|
2016-02-28 06:18:15 -05:00
|
|
|
l->array = g_realloc ( l->array, sizeof ( xcb_window_t ) * ( l->len + WINLIST + 1 ) );
|
2016-02-23 13:34:48 -05:00
|
|
|
l->data = g_realloc ( l->data, sizeof ( client* ) * ( l->len + WINLIST + 1 ) );
|
2015-02-12 16:16:32 -05:00
|
|
|
}
|
|
|
|
// Make clang-check happy.
|
|
|
|
// TODO: make clang-check clear this should never be 0.
|
|
|
|
if ( l->data == NULL || l->array == NULL ) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
l->data[l->len] = d;
|
|
|
|
l->array[l->len++] = w;
|
|
|
|
return l->len - 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void winlist_empty ( winlist *l )
|
|
|
|
{
|
|
|
|
while ( l->len > 0 ) {
|
2016-02-23 17:54:35 -05:00
|
|
|
client *c = l->data[--l->len];
|
2016-02-23 13:34:48 -05:00
|
|
|
if ( c != NULL ) {
|
|
|
|
g_free ( c->title );
|
|
|
|
g_free ( c->class );
|
|
|
|
g_free ( c->name );
|
|
|
|
g_free ( c->role );
|
|
|
|
g_free ( c );
|
|
|
|
}
|
2015-02-12 16:16:32 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param l The winlist entry
|
|
|
|
*
|
|
|
|
* Free the winlist.
|
|
|
|
*/
|
|
|
|
static void winlist_free ( winlist *l )
|
|
|
|
{
|
|
|
|
if ( l != NULL ) {
|
|
|
|
winlist_empty ( l );
|
|
|
|
g_free ( l->array );
|
|
|
|
g_free ( l->data );
|
|
|
|
g_free ( l );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param l The winlist.
|
|
|
|
* @param w The window to find.
|
|
|
|
*
|
|
|
|
* Find the window in the list, and return the array entry.
|
|
|
|
*
|
|
|
|
* @returns -1 if failed, index is successful.
|
|
|
|
*/
|
2016-02-28 06:18:15 -05:00
|
|
|
static int winlist_find ( winlist *l, xcb_window_t w )
|
2015-02-12 16:16:32 -05:00
|
|
|
{
|
2015-03-05 14:26:52 -05:00
|
|
|
// iterate backwards. Theory is: windows most often accessed will be
|
|
|
|
// nearer the end. Testing with kcachegrind seems to support this...
|
2015-02-12 16:16:32 -05:00
|
|
|
int i;
|
|
|
|
|
|
|
|
for ( i = ( l->len - 1 ); i >= 0; i-- ) {
|
|
|
|
if ( l->array[i] == w ) {
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Create empty X11 cache for windows and windows attributes.
|
|
|
|
*/
|
|
|
|
static void x11_cache_create ( void )
|
|
|
|
{
|
2015-09-07 11:41:48 -04:00
|
|
|
if ( cache_client == NULL ) {
|
|
|
|
cache_client = winlist_new ();
|
|
|
|
}
|
2015-02-12 16:16:32 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Free the cache.
|
|
|
|
*/
|
|
|
|
static void x11_cache_free ( void )
|
|
|
|
{
|
|
|
|
winlist_free ( cache_client );
|
2015-07-30 12:18:37 -04:00
|
|
|
cache_client = NULL;
|
2015-02-12 16:16:32 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param d Display connection to X server
|
|
|
|
* @param w window
|
|
|
|
*
|
|
|
|
* Get window attributes.
|
|
|
|
* This functions uses caching.
|
|
|
|
*
|
|
|
|
* @returns a XWindowAttributes
|
|
|
|
*/
|
2016-03-01 12:11:55 -05:00
|
|
|
static xcb_get_window_attributes_reply_t * window_get_attributes ( xcb_window_t w )
|
2015-02-12 16:16:32 -05:00
|
|
|
{
|
2016-03-01 12:11:55 -05:00
|
|
|
xcb_get_window_attributes_cookie_t c = xcb_get_window_attributes ( xcb->connection, w );
|
|
|
|
xcb_get_window_attributes_reply_t *r = xcb_get_window_attributes_reply ( xcb->connection, c, NULL );
|
2016-02-28 05:04:09 -05:00
|
|
|
if ( r ) {
|
|
|
|
return r;
|
2015-02-12 16:16:32 -05:00
|
|
|
}
|
2016-02-23 13:34:48 -05:00
|
|
|
return NULL;
|
2015-02-12 16:16:32 -05:00
|
|
|
}
|
|
|
|
// _NET_WM_STATE_*
|
2016-02-28 06:19:56 -05:00
|
|
|
static int client_has_state ( client *c, xcb_atom_t state )
|
2015-02-12 16:16:32 -05:00
|
|
|
{
|
|
|
|
for ( int i = 0; i < c->states; i++ ) {
|
|
|
|
if ( c->state[i] == state ) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2016-02-28 06:19:56 -05:00
|
|
|
static int client_has_window_type ( client *c, xcb_atom_t type )
|
2015-08-27 15:09:12 -04:00
|
|
|
{
|
|
|
|
for ( int i = 0; i < c->window_types; i++ ) {
|
|
|
|
if ( c->window_type[i] == type ) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2015-02-12 16:16:32 -05:00
|
|
|
|
2016-03-01 12:11:55 -05:00
|
|
|
static client* window_client ( xcb_window_t win )
|
2015-02-12 16:16:32 -05:00
|
|
|
{
|
2016-02-28 06:18:15 -05:00
|
|
|
if ( win == XCB_WINDOW_NONE ) {
|
2015-02-12 16:16:32 -05:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
int idx = winlist_find ( cache_client, win );
|
|
|
|
|
|
|
|
if ( idx >= 0 ) {
|
|
|
|
return cache_client->data[idx];
|
|
|
|
}
|
|
|
|
|
|
|
|
// if this fails, we're up that creek
|
2016-03-01 12:11:55 -05:00
|
|
|
xcb_get_window_attributes_reply_t *attr = window_get_attributes ( win );
|
2015-02-12 16:16:32 -05:00
|
|
|
|
|
|
|
if ( !attr ) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
client *c = g_malloc0 ( sizeof ( client ) );
|
|
|
|
c->window = win;
|
|
|
|
|
|
|
|
// copy xattr so we don't have to care when stuff is freed
|
2016-02-27 16:55:47 -05:00
|
|
|
memmove ( &c->xattr, attr, sizeof ( xcb_get_window_attributes_reply_t ) );
|
|
|
|
|
2016-03-01 12:11:55 -05:00
|
|
|
xcb_get_property_cookie_t cky = xcb_ewmh_get_wm_state ( &xcb->ewmh, win );
|
2016-02-27 15:55:43 -05:00
|
|
|
xcb_ewmh_get_atoms_reply_t states;
|
2016-03-01 12:11:55 -05:00
|
|
|
if ( xcb_ewmh_get_wm_state_reply ( &xcb->ewmh, cky, &states, NULL ) ) {
|
2016-02-28 09:32:53 -05:00
|
|
|
c->states = MIN ( CLIENTSTATE, states.atoms_len );
|
2016-03-17 17:03:19 -04:00
|
|
|
memcpy ( c->state, states.atoms, MIN ( CLIENTSTATE, states.atoms_len )*sizeof(xcb_atom_t) );
|
2016-02-28 09:32:53 -05:00
|
|
|
xcb_ewmh_get_atoms_reply_wipe ( &states );
|
2016-02-27 15:55:43 -05:00
|
|
|
}
|
2016-03-01 12:11:55 -05:00
|
|
|
cky = xcb_ewmh_get_wm_window_type ( &xcb->ewmh, win );
|
|
|
|
if ( xcb_ewmh_get_wm_window_type_reply ( &xcb->ewmh, cky, &states, NULL ) ) {
|
2016-02-28 09:32:53 -05:00
|
|
|
c->window_types = MIN ( CLIENTWINDOWTYPE, states.atoms_len );
|
2016-03-17 17:03:19 -04:00
|
|
|
memcpy ( c->window_type, states.atoms, MIN ( CLIENTWINDOWTYPE, states.atoms_len )*sizeof(xcb_atom_t) );
|
2016-02-28 09:32:53 -05:00
|
|
|
xcb_ewmh_get_atoms_reply_wipe ( &states );
|
2016-02-27 15:55:43 -05:00
|
|
|
}
|
2015-02-12 16:16:32 -05:00
|
|
|
|
2016-03-01 12:11:55 -05:00
|
|
|
c->title = window_get_text_prop ( c->window, xcb->ewmh._NET_WM_NAME );
|
2016-02-28 05:16:54 -05:00
|
|
|
if ( c->title == NULL ) {
|
2016-03-01 12:11:55 -05:00
|
|
|
c->title = window_get_text_prop ( c->window, XCB_ATOM_WM_NAME );
|
2015-02-12 16:16:32 -05:00
|
|
|
}
|
|
|
|
|
2016-03-01 12:11:55 -05:00
|
|
|
c->role = window_get_text_prop ( c->window, netatoms[WM_WINDOW_ROLE] );
|
2015-02-12 16:16:32 -05:00
|
|
|
|
2016-03-01 12:11:55 -05:00
|
|
|
cky = xcb_icccm_get_wm_class ( xcb->connection, c->window );
|
2016-02-28 05:16:54 -05:00
|
|
|
xcb_icccm_get_wm_class_reply_t wcr;
|
2016-03-01 12:11:55 -05:00
|
|
|
if ( xcb_icccm_get_wm_class_reply ( xcb->connection, cky, &wcr, NULL ) ) {
|
2016-02-28 09:32:53 -05:00
|
|
|
c->class = g_strdup ( wcr.class_name );
|
|
|
|
xcb_icccm_get_wm_class_reply_wipe ( &wcr );
|
2015-02-12 16:16:32 -05:00
|
|
|
}
|
|
|
|
|
2016-03-01 12:11:55 -05:00
|
|
|
xcb_get_property_cookie_t cc = xcb_icccm_get_wm_hints ( xcb->connection, c->window );
|
2016-02-28 09:32:53 -05:00
|
|
|
xcb_icccm_wm_hints_t r;
|
2016-03-01 12:11:55 -05:00
|
|
|
if ( xcb_icccm_get_wm_hints_reply ( xcb->connection, cc, &r, NULL ) ) {
|
2016-02-27 18:09:05 -05:00
|
|
|
c->hint_flags = r.flags;
|
2015-04-03 12:01:03 -04:00
|
|
|
}
|
|
|
|
|
2015-02-12 16:16:32 -05:00
|
|
|
winlist_append ( cache_client, c->window, c );
|
2016-02-23 13:34:48 -05:00
|
|
|
g_free ( attr );
|
2015-02-12 16:16:32 -05:00
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
2015-11-25 03:26:38 -05:00
|
|
|
typedef struct _ModeModePrivateData
|
2015-03-27 15:28:53 -04:00
|
|
|
{
|
|
|
|
unsigned int id;
|
|
|
|
char **cmd_list;
|
|
|
|
unsigned int cmd_list_length;
|
|
|
|
winlist *ids;
|
|
|
|
int config_i3_mode;
|
2015-03-31 16:45:02 -04:00
|
|
|
// Current window.
|
|
|
|
unsigned int index;
|
|
|
|
char *cache;
|
2015-11-25 03:26:38 -05:00
|
|
|
} ModeModePrivateData;
|
2015-02-12 16:16:32 -05:00
|
|
|
|
2015-11-25 03:26:38 -05:00
|
|
|
static int window_match ( const Mode *sw, char **tokens,
|
2015-10-12 02:12:25 -04:00
|
|
|
__attribute__( ( unused ) ) int not_ascii,
|
2015-11-23 16:14:26 -05:00
|
|
|
int case_sensitive, unsigned int index )
|
2015-02-12 16:16:32 -05:00
|
|
|
{
|
2016-01-07 15:27:20 -05:00
|
|
|
ModeModePrivateData *rmpd = (ModeModePrivateData *) mode_get_private_data ( sw );
|
2015-11-25 03:26:38 -05:00
|
|
|
int match = 1;
|
|
|
|
const winlist *ids = ( winlist * ) rmpd->ids;
|
2015-11-21 18:33:26 -05:00
|
|
|
// Want to pull directly out of cache, X calls are not thread safe.
|
2015-11-25 03:26:38 -05:00
|
|
|
int idx = winlist_find ( cache_client, ids->array[index] );
|
2015-11-22 14:41:45 -05:00
|
|
|
g_assert ( idx >= 0 );
|
2015-11-25 03:26:38 -05:00
|
|
|
client *c = cache_client->data[idx];
|
2015-02-12 16:16:32 -05:00
|
|
|
|
|
|
|
if ( tokens ) {
|
2015-07-06 06:30:15 -04:00
|
|
|
for ( int j = 0; match && tokens != NULL && tokens[j] != NULL; j++ ) {
|
|
|
|
int test = 0;
|
|
|
|
// Dirty hack. Normally token_match does _all_ the matching,
|
|
|
|
// Now we want it to match only one item at the time.
|
|
|
|
// If hack not in place it would not match queries spanning multiple fields.
|
|
|
|
// e.g. when searching 'title element' and 'class element'
|
|
|
|
char *ftokens[2] = { tokens[j], NULL };
|
2016-02-23 13:14:21 -05:00
|
|
|
if ( !test && c->title != NULL && c->title[0] != '\0' ) {
|
2015-11-21 17:59:59 -05:00
|
|
|
test = token_match ( ftokens, c->title, not_ascii, case_sensitive );
|
2015-07-06 06:30:15 -04:00
|
|
|
}
|
|
|
|
|
2016-02-23 13:14:21 -05:00
|
|
|
if ( !test && c->class != NULL && c->class[0] != '\0' ) {
|
2015-11-21 17:59:59 -05:00
|
|
|
test = token_match ( ftokens, c->class, not_ascii, case_sensitive );
|
2015-07-06 06:30:15 -04:00
|
|
|
}
|
|
|
|
|
2016-02-23 13:14:21 -05:00
|
|
|
if ( !test && c->role != NULL && c->role[0] != '\0' ) {
|
2015-11-21 17:59:59 -05:00
|
|
|
test = token_match ( ftokens, c->role, not_ascii, case_sensitive );
|
2015-07-06 06:30:15 -04:00
|
|
|
}
|
|
|
|
|
2016-02-23 13:14:21 -05:00
|
|
|
if ( !test && c->name != NULL && c->name[0] != '\0' ) {
|
2015-11-21 17:59:59 -05:00
|
|
|
test = token_match ( ftokens, c->name, not_ascii, case_sensitive );
|
2015-07-06 06:30:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( test == 0 ) {
|
|
|
|
match = 0;
|
|
|
|
}
|
2015-02-12 16:16:32 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return match;
|
|
|
|
}
|
2015-03-27 15:28:53 -04:00
|
|
|
|
2015-11-25 03:26:38 -05:00
|
|
|
static unsigned int window_mode_get_num_entries ( const Mode *sw )
|
2015-03-27 15:28:53 -04:00
|
|
|
{
|
2016-01-07 15:27:20 -05:00
|
|
|
const ModeModePrivateData *pd = (const ModeModePrivateData *) mode_get_private_data ( sw );
|
2015-11-21 17:59:59 -05:00
|
|
|
return pd->cmd_list_length;
|
|
|
|
}
|
2015-11-25 03:26:38 -05:00
|
|
|
static void _window_mode_load_data ( Mode *sw, unsigned int cd )
|
2015-11-21 17:59:59 -05:00
|
|
|
{
|
2016-02-28 09:32:53 -05:00
|
|
|
ModeModePrivateData *pd = (ModeModePrivateData *) mode_get_private_data ( sw );
|
2015-11-21 17:59:59 -05:00
|
|
|
// find window list
|
2015-11-25 03:26:38 -05:00
|
|
|
int nwins = 0;
|
2016-02-28 09:32:53 -05:00
|
|
|
xcb_window_t wins[100];
|
|
|
|
xcb_window_t curr_win_id;
|
2015-11-21 17:59:59 -05:00
|
|
|
// Create cache
|
|
|
|
|
|
|
|
x11_cache_create ();
|
|
|
|
// Check for i3
|
2016-03-01 12:11:55 -05:00
|
|
|
pd->config_i3_mode = i3_support_initialize ( xcb );
|
2016-03-01 13:48:18 -05:00
|
|
|
xcb_get_property_cookie_t c = xcb_ewmh_get_active_window ( &( xcb->ewmh ), xcb->screen_nbr );
|
2016-03-01 12:11:55 -05:00
|
|
|
if ( !xcb_ewmh_get_active_window_reply ( &xcb->ewmh, c, &curr_win_id, NULL ) ) {
|
2015-11-21 17:59:59 -05:00
|
|
|
curr_win_id = 0;
|
|
|
|
}
|
2015-02-12 16:16:32 -05:00
|
|
|
|
2015-11-21 17:59:59 -05:00
|
|
|
// Get the current desktop.
|
2016-02-27 15:55:43 -05:00
|
|
|
unsigned int current_desktop = 0;
|
2016-03-01 12:11:55 -05:00
|
|
|
c = xcb_ewmh_get_current_desktop ( &xcb->ewmh, xcb->screen_nbr );
|
|
|
|
if ( !xcb_ewmh_get_current_desktop_reply ( &xcb->ewmh, c, ¤t_desktop, NULL ) ) {
|
2015-11-21 17:59:59 -05:00
|
|
|
current_desktop = 0;
|
|
|
|
}
|
2015-09-07 11:41:48 -04:00
|
|
|
|
2016-03-01 12:11:55 -05:00
|
|
|
c = xcb_ewmh_get_client_list_stacking ( &xcb->ewmh, 0 );
|
2016-02-27 16:55:47 -05:00
|
|
|
xcb_ewmh_get_windows_reply_t clients;
|
2016-03-01 12:11:55 -05:00
|
|
|
if ( xcb_ewmh_get_client_list_stacking_reply ( &xcb->ewmh, c, &clients, NULL ) ) {
|
2016-02-28 09:32:53 -05:00
|
|
|
nwins = MIN ( 100, clients.windows_len );
|
|
|
|
memcpy ( wins, clients.windows, nwins * sizeof ( xcb_window_t ) );
|
|
|
|
xcb_ewmh_get_windows_reply_wipe ( &clients );
|
2016-02-27 16:55:47 -05:00
|
|
|
}
|
|
|
|
else {
|
2016-03-01 12:11:55 -05:00
|
|
|
c = xcb_ewmh_get_client_list ( &xcb->ewmh, xcb->screen_nbr );
|
|
|
|
if ( xcb_ewmh_get_client_list_reply ( &xcb->ewmh, c, &clients, NULL ) ) {
|
2016-02-28 09:32:53 -05:00
|
|
|
nwins = MIN ( 100, clients.windows_len );
|
|
|
|
memcpy ( wins, clients.windows, nwins * sizeof ( xcb_window_t ) );
|
|
|
|
xcb_ewmh_get_windows_reply_wipe ( &clients );
|
2015-11-10 17:52:52 -05:00
|
|
|
}
|
2015-11-21 17:59:59 -05:00
|
|
|
}
|
|
|
|
if ( nwins > 0 ) {
|
2016-02-28 09:32:53 -05:00
|
|
|
char pattern[50];
|
|
|
|
int i;
|
|
|
|
unsigned int classfield = 0;
|
|
|
|
unsigned int desktops = 0;
|
2015-11-21 17:59:59 -05:00
|
|
|
// windows we actually display. May be slightly different to _NET_CLIENT_LIST_STACKING
|
|
|
|
// if we happen to have a window destroyed while we're working...
|
|
|
|
pd->ids = winlist_new ();
|
|
|
|
|
|
|
|
// calc widths of fields
|
|
|
|
for ( i = nwins - 1; i > -1; i-- ) {
|
2016-03-01 12:11:55 -05:00
|
|
|
client *c = window_client ( wins[i] );
|
2016-02-27 16:55:47 -05:00
|
|
|
if ( ( c != NULL )
|
2015-11-21 17:59:59 -05:00
|
|
|
&& !c->xattr.override_redirect
|
2016-03-01 12:11:55 -05:00
|
|
|
&& !client_has_window_type ( c, xcb->ewmh._NET_WM_WINDOW_TYPE_DOCK )
|
|
|
|
&& !client_has_window_type ( c, xcb->ewmh._NET_WM_WINDOW_TYPE_DESKTOP )
|
|
|
|
&& !client_has_state ( c, xcb->ewmh._NET_WM_STATE_SKIP_PAGER )
|
|
|
|
&& !client_has_state ( c, xcb->ewmh._NET_WM_STATE_SKIP_TASKBAR ) ) {
|
2016-03-12 08:00:19 -05:00
|
|
|
classfield = MAX ( classfield, (c->class != NULL)?(strlen ( c->class )):0 );
|
2015-11-21 17:59:59 -05:00
|
|
|
|
2016-03-01 12:11:55 -05:00
|
|
|
if ( client_has_state ( c, xcb->ewmh._NET_WM_STATE_DEMANDS_ATTENTION ) ) {
|
2015-11-21 17:59:59 -05:00
|
|
|
c->demands = TRUE;
|
|
|
|
}
|
2016-02-28 09:32:53 -05:00
|
|
|
if ( ( c->hint_flags & XCB_ICCCM_WM_HINT_X_URGENCY ) != 0 ) {
|
2015-11-21 17:59:59 -05:00
|
|
|
c->demands = TRUE;
|
|
|
|
}
|
2015-02-12 16:16:32 -05:00
|
|
|
|
2015-11-21 17:59:59 -05:00
|
|
|
if ( c->window == curr_win_id ) {
|
|
|
|
c->active = TRUE;
|
2015-02-12 16:16:32 -05:00
|
|
|
}
|
2015-11-21 17:59:59 -05:00
|
|
|
winlist_append ( pd->ids, c->window, NULL );
|
2015-02-12 16:16:32 -05:00
|
|
|
}
|
2015-11-21 17:59:59 -05:00
|
|
|
}
|
2015-02-12 16:16:32 -05:00
|
|
|
|
2015-11-21 17:59:59 -05:00
|
|
|
// Create pattern for printing the line.
|
2016-03-01 12:11:55 -05:00
|
|
|
xcb_get_property_cookie_t c = xcb_ewmh_get_number_of_desktops ( &xcb->ewmh, xcb->screen_nbr );
|
|
|
|
if ( !xcb_ewmh_get_number_of_desktops_reply ( &xcb->ewmh, c, &desktops, NULL ) ) {
|
2016-02-28 09:32:53 -05:00
|
|
|
desktops = 1;
|
2015-11-21 17:59:59 -05:00
|
|
|
}
|
2016-02-27 15:55:43 -05:00
|
|
|
|
2015-11-21 17:59:59 -05:00
|
|
|
if ( pd->config_i3_mode ) {
|
2016-01-09 13:25:03 -05:00
|
|
|
snprintf ( pattern, 50, "%%-%ds %%s", MAX ( 5, classfield ) );
|
2015-11-21 17:59:59 -05:00
|
|
|
}
|
|
|
|
else{
|
2016-01-09 13:25:03 -05:00
|
|
|
snprintf ( pattern, 50, "%%-%ds %%-%ds %%s", desktops < 10 ? 1 : 2,
|
|
|
|
MAX ( 5, classfield ) );
|
2015-11-21 17:59:59 -05:00
|
|
|
}
|
|
|
|
pd->cmd_list = g_malloc0_n ( ( pd->ids->len + 1 ), sizeof ( char* ) );
|
|
|
|
|
|
|
|
// build the actual list
|
|
|
|
for ( i = 0; i < ( pd->ids->len ); i++ ) {
|
2016-02-28 06:18:15 -05:00
|
|
|
xcb_window_t w = pd->ids->array[i];
|
2016-02-28 09:32:53 -05:00
|
|
|
client *c;
|
2015-11-21 17:59:59 -05:00
|
|
|
|
2016-03-01 12:11:55 -05:00
|
|
|
if ( ( c = window_client ( w ) ) ) {
|
2015-11-21 17:59:59 -05:00
|
|
|
// final line format
|
2016-03-02 12:07:59 -05:00
|
|
|
char desktop[5];
|
2015-11-21 17:59:59 -05:00
|
|
|
desktop[0] = 0;
|
2016-03-02 12:07:59 -05:00
|
|
|
size_t len =
|
2016-02-23 13:34:48 -05:00
|
|
|
( ( c->title != NULL ) ? strlen ( c->title ) : 0 ) + ( c->class ? strlen ( c->class ) : 0 ) + classfield + 50;
|
2016-03-02 12:07:59 -05:00
|
|
|
char *line = g_malloc ( len );
|
2015-11-21 17:59:59 -05:00
|
|
|
if ( !pd->config_i3_mode ) {
|
2016-03-02 12:07:59 -05:00
|
|
|
unsigned int wmdesktop = 0;
|
2015-11-21 17:59:59 -05:00
|
|
|
// find client's desktop.
|
2016-02-27 15:55:43 -05:00
|
|
|
xcb_get_property_cookie_t cookie;
|
2016-02-28 09:32:53 -05:00
|
|
|
xcb_get_property_reply_t *r;
|
|
|
|
|
|
|
|
cookie =
|
2016-03-01 12:11:55 -05:00
|
|
|
xcb_get_property ( xcb->connection, 0, c->window, xcb->ewmh._NET_WM_DESKTOP, XCB_GET_PROPERTY, 0,
|
2016-02-28 09:32:53 -05:00
|
|
|
sizeof ( unsigned int ) );
|
2016-03-01 12:11:55 -05:00
|
|
|
r = xcb_get_property_reply ( xcb->connection, cookie, NULL );
|
2016-02-28 09:32:53 -05:00
|
|
|
if ( r && r->type == XCB_ATOM_INTEGER ) {
|
|
|
|
wmdesktop = *( (int *) xcb_get_property_value ( r ) );
|
2016-02-27 15:55:43 -05:00
|
|
|
}
|
2016-02-28 09:32:53 -05:00
|
|
|
if ( r && r->type != XCB_ATOM_INTEGER ) {
|
2015-11-21 17:59:59 -05:00
|
|
|
// Assume the client is on all desktops.
|
|
|
|
wmdesktop = 0xFFFFFFFF;
|
|
|
|
}
|
|
|
|
else if ( cd && wmdesktop != current_desktop ) {
|
|
|
|
g_free ( line );
|
2016-02-28 09:32:53 -05:00
|
|
|
free ( r );
|
2015-11-21 17:59:59 -05:00
|
|
|
continue;
|
2015-02-12 16:16:32 -05:00
|
|
|
}
|
2016-02-28 09:32:53 -05:00
|
|
|
free ( r );
|
2015-11-21 17:59:59 -05:00
|
|
|
|
|
|
|
if ( wmdesktop < 0xFFFFFFFF ) {
|
2016-01-09 13:25:03 -05:00
|
|
|
snprintf ( desktop, 5, "%d", (int) wmdesktop );
|
2015-02-12 16:16:32 -05:00
|
|
|
}
|
|
|
|
|
2016-02-23 13:34:48 -05:00
|
|
|
snprintf ( line, len, pattern, desktop, c->class ? c->class : "", c->title ? c->title : "" );
|
2015-11-21 17:59:59 -05:00
|
|
|
}
|
|
|
|
else{
|
2016-02-23 13:34:48 -05:00
|
|
|
snprintf ( line, len, pattern, c->class ? c->class : "", c->title ? c->title : "" );
|
2015-02-12 16:16:32 -05:00
|
|
|
}
|
2015-11-21 17:59:59 -05:00
|
|
|
|
|
|
|
pd->cmd_list[pd->cmd_list_length++] = line;
|
2015-02-12 16:16:32 -05:00
|
|
|
}
|
|
|
|
}
|
2015-03-27 15:28:53 -04:00
|
|
|
}
|
|
|
|
}
|
2016-01-08 03:16:59 -05:00
|
|
|
static int window_mode_init ( Mode *sw )
|
2015-09-07 11:41:48 -04:00
|
|
|
{
|
2016-01-07 15:27:20 -05:00
|
|
|
if ( mode_get_private_data ( sw ) == NULL ) {
|
2015-11-25 03:26:38 -05:00
|
|
|
ModeModePrivateData *pd = g_malloc0 ( sizeof ( *pd ) );
|
2016-01-07 15:27:20 -05:00
|
|
|
mode_set_private_data ( sw, (void *) pd );
|
2015-11-21 17:59:59 -05:00
|
|
|
_window_mode_load_data ( sw, FALSE );
|
|
|
|
}
|
2016-01-08 03:16:59 -05:00
|
|
|
return TRUE;
|
2015-09-07 11:41:48 -04:00
|
|
|
}
|
2016-01-08 03:16:59 -05:00
|
|
|
static int window_mode_init_cd ( Mode *sw )
|
2015-09-07 11:41:48 -04:00
|
|
|
{
|
2016-01-07 15:27:20 -05:00
|
|
|
if ( mode_get_private_data ( sw ) == NULL ) {
|
2015-11-25 03:26:38 -05:00
|
|
|
ModeModePrivateData *pd = g_malloc0 ( sizeof ( *pd ) );
|
2016-01-07 15:27:20 -05:00
|
|
|
mode_set_private_data ( sw, (void *) pd );
|
2015-11-21 17:59:59 -05:00
|
|
|
_window_mode_load_data ( sw, TRUE );
|
|
|
|
}
|
2016-01-08 03:16:59 -05:00
|
|
|
return TRUE;
|
2015-09-07 11:41:48 -04:00
|
|
|
}
|
2015-11-25 03:26:38 -05:00
|
|
|
static ModeMode window_mode_result ( Mode *sw, int mretv, G_GNUC_UNUSED char **input,
|
|
|
|
unsigned int selected_line )
|
2015-03-27 15:28:53 -04:00
|
|
|
{
|
2016-01-07 15:27:20 -05:00
|
|
|
ModeModePrivateData *rmpd = (ModeModePrivateData *) mode_get_private_data ( sw );
|
2015-11-25 03:26:38 -05:00
|
|
|
ModeMode retv = MODE_EXIT;
|
2015-03-27 15:28:53 -04:00
|
|
|
if ( mretv & MENU_NEXT ) {
|
|
|
|
retv = NEXT_DIALOG;
|
|
|
|
}
|
|
|
|
else if ( mretv & MENU_PREVIOUS ) {
|
|
|
|
retv = PREVIOUS_DIALOG;
|
|
|
|
}
|
2015-05-03 07:04:03 -04:00
|
|
|
else if ( ( mretv & MENU_QUICK_SWITCH ) == MENU_QUICK_SWITCH ) {
|
|
|
|
retv = ( mretv & MENU_LOWER_MASK );
|
2015-03-27 15:28:53 -04:00
|
|
|
}
|
2015-10-31 13:04:55 -04:00
|
|
|
else if ( ( mretv & ( MENU_OK ) ) && rmpd->cmd_list[selected_line] ) {
|
2015-03-27 15:28:53 -04:00
|
|
|
if ( rmpd->config_i3_mode ) {
|
|
|
|
// Hack for i3.
|
|
|
|
i3_support_focus_window ( rmpd->ids->array[selected_line] );
|
2015-02-12 16:16:32 -05:00
|
|
|
}
|
2015-03-27 15:28:53 -04:00
|
|
|
else{
|
2016-03-01 12:11:55 -05:00
|
|
|
xcb_ewmh_request_change_active_window ( &xcb->ewmh, xcb->screen_nbr, rmpd->ids->array[selected_line],
|
2016-02-28 09:32:53 -05:00
|
|
|
XCB_EWMH_CLIENT_SOURCE_TYPE_OTHER,
|
|
|
|
XCB_CURRENT_TIME, XCB_WINDOW_NONE );
|
2016-03-01 12:11:55 -05:00
|
|
|
xcb_flush ( xcb->connection );
|
2015-02-12 16:16:32 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return retv;
|
|
|
|
}
|
2015-03-27 15:28:53 -04:00
|
|
|
|
2015-11-25 03:26:38 -05:00
|
|
|
static void window_mode_destroy ( Mode *sw )
|
2015-03-27 15:28:53 -04:00
|
|
|
{
|
2016-01-07 15:27:20 -05:00
|
|
|
ModeModePrivateData *rmpd = (ModeModePrivateData *) mode_get_private_data ( sw );
|
2015-03-27 15:28:53 -04:00
|
|
|
if ( rmpd != NULL ) {
|
|
|
|
g_strfreev ( rmpd->cmd_list );
|
|
|
|
winlist_free ( rmpd->ids );
|
|
|
|
i3_support_free_internals ();
|
|
|
|
x11_cache_free ();
|
2015-03-31 16:45:02 -04:00
|
|
|
g_free ( rmpd->cache );
|
2015-03-27 15:28:53 -04:00
|
|
|
g_free ( rmpd );
|
2016-01-07 15:27:20 -05:00
|
|
|
mode_set_private_data ( sw, NULL );
|
2015-03-27 15:28:53 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-07 15:27:20 -05:00
|
|
|
static char *_get_display_value ( const Mode *sw, unsigned int selected_line, int *state, int get_entry )
|
2015-03-31 16:45:02 -04:00
|
|
|
{
|
2016-01-07 15:27:20 -05:00
|
|
|
ModeModePrivateData *rmpd = mode_get_private_data ( sw );
|
2016-03-01 12:11:55 -05:00
|
|
|
if ( window_client ( rmpd->ids->array[selected_line] )->demands ) {
|
2015-04-03 12:40:07 -04:00
|
|
|
*state |= URGENT;
|
2015-04-02 16:23:17 -04:00
|
|
|
}
|
2016-03-01 12:11:55 -05:00
|
|
|
if ( window_client ( rmpd->ids->array[selected_line] )->active ) {
|
2015-04-03 12:40:07 -04:00
|
|
|
*state |= ACTIVE;
|
2015-03-31 16:45:02 -04:00
|
|
|
}
|
2015-11-21 17:59:59 -05:00
|
|
|
return get_entry ? g_strdup ( rmpd->cmd_list[selected_line] ) : NULL;
|
|
|
|
}
|
|
|
|
|
2015-11-25 03:26:38 -05:00
|
|
|
static int window_is_not_ascii ( const Mode *sw, unsigned int index )
|
2015-11-21 17:59:59 -05:00
|
|
|
{
|
2016-01-07 15:27:20 -05:00
|
|
|
const ModeModePrivateData *rmpd = mode_get_private_data ( sw );
|
2015-11-25 03:26:38 -05:00
|
|
|
const winlist *ids = ( winlist * ) rmpd->ids;
|
2015-11-21 18:33:26 -05:00
|
|
|
// Want to pull directly out of cache, X calls are not thread safe.
|
2015-11-25 03:26:38 -05:00
|
|
|
int idx = winlist_find ( cache_client, ids->array[index] );
|
2015-11-22 14:41:45 -05:00
|
|
|
g_assert ( idx >= 0 );
|
2015-11-25 03:26:38 -05:00
|
|
|
client *c = cache_client->data[idx];
|
2016-02-23 13:34:48 -05:00
|
|
|
if ( c->role && !g_str_is_ascii ( c->role ) ) {
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
if ( c->class && !g_str_is_ascii ( c->class ) ) {
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
if ( c->title && !g_str_is_ascii ( c->title ) ) {
|
|
|
|
return TRUE;
|
|
|
|
}
|
2016-02-23 13:14:21 -05:00
|
|
|
return FALSE;
|
2015-03-31 16:45:02 -04:00
|
|
|
}
|
|
|
|
|
2016-01-07 15:27:20 -05:00
|
|
|
#include "mode-private.h"
|
2015-11-25 03:26:38 -05:00
|
|
|
Mode window_mode =
|
2015-03-27 15:28:53 -04:00
|
|
|
{
|
2016-01-07 15:27:20 -05:00
|
|
|
.name = "window",
|
2016-01-12 16:17:53 -05:00
|
|
|
.cfg_name_key = "display-window",
|
2016-01-07 15:27:20 -05:00
|
|
|
._init = window_mode_init,
|
|
|
|
._get_num_entries = window_mode_get_num_entries,
|
|
|
|
._result = window_mode_result,
|
|
|
|
._destroy = window_mode_destroy,
|
|
|
|
._token_match = window_match,
|
|
|
|
._get_display_value = _get_display_value,
|
|
|
|
._get_completion = NULL,
|
|
|
|
._is_not_ascii = window_is_not_ascii,
|
|
|
|
.private_data = NULL,
|
|
|
|
.free = NULL
|
2015-03-27 15:28:53 -04:00
|
|
|
};
|
2015-11-25 03:26:38 -05:00
|
|
|
Mode window_mode_cd =
|
2015-09-07 11:41:48 -04:00
|
|
|
{
|
2016-01-07 15:27:20 -05:00
|
|
|
.name = "windowcd",
|
2016-01-12 16:17:53 -05:00
|
|
|
.cfg_name_key = "display-windowcd",
|
2016-01-07 15:27:20 -05:00
|
|
|
._init = window_mode_init_cd,
|
|
|
|
._get_num_entries = window_mode_get_num_entries,
|
|
|
|
._result = window_mode_result,
|
|
|
|
._destroy = window_mode_destroy,
|
|
|
|
._token_match = window_match,
|
|
|
|
._get_display_value = _get_display_value,
|
|
|
|
._get_completion = NULL,
|
|
|
|
._is_not_ascii = window_is_not_ascii,
|
|
|
|
.private_data = NULL,
|
|
|
|
.free = NULL
|
2015-09-07 11:41:48 -04:00
|
|
|
};
|
2015-09-14 02:57:10 -04:00
|
|
|
|
|
|
|
#endif // WINDOW_MODE
|
|
|
|
|