2015-02-12 21:16:32 +00:00
|
|
|
/**
|
|
|
|
* rofi
|
|
|
|
*
|
|
|
|
* MIT/X11 License
|
2015-04-14 20:12:21 +00:00
|
|
|
* Copyright 2013-2015 Qball Cow <qball@gmpclient.org>
|
2015-02-12 21:16:32 +00: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 06:57:10 +00:00
|
|
|
|
|
|
|
#ifdef WINDOW_MODE
|
|
|
|
|
2015-02-12 21:16:32 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <strings.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <X11/X.h>
|
|
|
|
#include <X11/Xatom.h>
|
|
|
|
#include <X11/Xutil.h>
|
|
|
|
|
|
|
|
#include "rofi.h"
|
|
|
|
#include "helper.h"
|
|
|
|
#include "x11-helper.h"
|
2015-02-12 21:26:28 +00:00
|
|
|
#include "i3-support.h"
|
2015-03-25 07:36:19 +00:00
|
|
|
#include "dialogs/window.h"
|
2015-02-12 21:16:32 +00:00
|
|
|
|
2015-08-27 19:09:12 +00:00
|
|
|
#define WINLIST 32
|
2015-02-12 21:16:32 +00:00
|
|
|
|
2015-08-27 19:09:12 +00:00
|
|
|
#define CLIENTTITLE 100
|
|
|
|
#define CLIENTCLASS 50
|
|
|
|
#define CLIENTNAME 50
|
|
|
|
#define CLIENTSTATE 10
|
|
|
|
#define CLIENTWINDOWTYPE 10
|
|
|
|
#define CLIENTROLE 50
|
2015-02-12 21:16:32 +00:00
|
|
|
|
2015-03-04 21:47:52 +00:00
|
|
|
// a manageable window
|
2015-02-12 21:16:32 +00:00
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
Window window, trans;
|
|
|
|
XWindowAttributes xattr;
|
|
|
|
char title[CLIENTTITLE];
|
|
|
|
char class[CLIENTCLASS];
|
|
|
|
char name[CLIENTNAME];
|
|
|
|
char role[CLIENTROLE];
|
|
|
|
int states;
|
|
|
|
Atom state[CLIENTSTATE];
|
2015-08-27 19:09:12 +00:00
|
|
|
int window_types;
|
|
|
|
Atom window_type[CLIENTWINDOWTYPE];
|
2015-02-12 21:16:32 +00:00
|
|
|
workarea monitor;
|
|
|
|
int active;
|
2015-04-02 20:23:17 +00:00
|
|
|
int demands;
|
2015-04-03 16:01:03 +00:00
|
|
|
long hint_flags;
|
2015-02-12 21:16:32 +00:00
|
|
|
} client;
|
|
|
|
// TODO
|
|
|
|
extern Display *display;
|
|
|
|
// window lists
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
Window *array;
|
|
|
|
void **data;
|
|
|
|
int len;
|
|
|
|
} winlist;
|
|
|
|
|
|
|
|
winlist *cache_client = NULL;
|
|
|
|
winlist *cache_xattr = 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;
|
|
|
|
l->array = g_malloc_n ( WINLIST + 1, sizeof ( Window ) );
|
|
|
|
l->data = g_malloc_n ( WINLIST + 1, sizeof ( void* ) );
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
static int winlist_append ( winlist *l, Window w, void *d )
|
|
|
|
{
|
|
|
|
if ( l->len > 0 && !( l->len % WINLIST ) ) {
|
|
|
|
l->array = g_realloc ( l->array, sizeof ( Window ) * ( l->len + WINLIST + 1 ) );
|
|
|
|
l->data = g_realloc ( l->data, sizeof ( void* ) * ( l->len + WINLIST + 1 ) );
|
|
|
|
}
|
|
|
|
// 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 ) {
|
|
|
|
g_free ( l->data[--( l->len )] );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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.
|
|
|
|
*/
|
|
|
|
static int winlist_find ( winlist *l, Window w )
|
|
|
|
{
|
2015-03-05 19:26:52 +00:00
|
|
|
// iterate backwards. Theory is: windows most often accessed will be
|
|
|
|
// nearer the end. Testing with kcachegrind seems to support this...
|
2015-02-12 21:16:32 +00: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 15:41:48 +00:00
|
|
|
if ( cache_client == NULL ) {
|
|
|
|
cache_client = winlist_new ();
|
|
|
|
}
|
|
|
|
if ( cache_xattr == NULL ) {
|
|
|
|
cache_xattr = winlist_new ();
|
|
|
|
}
|
2015-02-12 21:16:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Free the cache.
|
|
|
|
*/
|
|
|
|
static void x11_cache_free ( void )
|
|
|
|
{
|
|
|
|
winlist_free ( cache_xattr );
|
2015-07-30 16:18:37 +00:00
|
|
|
cache_xattr = NULL;
|
2015-02-12 21:16:32 +00:00
|
|
|
winlist_free ( cache_client );
|
2015-07-30 16:18:37 +00:00
|
|
|
cache_client = NULL;
|
2015-02-12 21:16:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param d Display connection to X server
|
|
|
|
* @param w window
|
|
|
|
*
|
|
|
|
* Get window attributes.
|
|
|
|
* This functions uses caching.
|
|
|
|
*
|
|
|
|
* @returns a XWindowAttributes
|
|
|
|
*/
|
|
|
|
static XWindowAttributes* window_get_attributes ( Display *display, Window w )
|
|
|
|
{
|
|
|
|
int idx = winlist_find ( cache_xattr, w );
|
|
|
|
|
|
|
|
if ( idx < 0 ) {
|
|
|
|
XWindowAttributes *cattr = g_malloc ( sizeof ( XWindowAttributes ) );
|
|
|
|
|
|
|
|
if ( XGetWindowAttributes ( display, w, cattr ) ) {
|
|
|
|
winlist_append ( cache_xattr, w, cattr );
|
|
|
|
return cattr;
|
|
|
|
}
|
|
|
|
|
|
|
|
g_free ( cattr );
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return cache_xattr->data[idx];
|
|
|
|
}
|
|
|
|
// _NET_WM_STATE_*
|
|
|
|
static int client_has_state ( client *c, Atom state )
|
|
|
|
{
|
|
|
|
for ( int i = 0; i < c->states; i++ ) {
|
|
|
|
if ( c->state[i] == state ) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2015-08-27 19:09:12 +00:00
|
|
|
static int client_has_window_type ( client *c, Atom type )
|
|
|
|
{
|
|
|
|
for ( int i = 0; i < c->window_types; i++ ) {
|
|
|
|
if ( c->window_type[i] == type ) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2015-02-12 21:16:32 +00:00
|
|
|
|
|
|
|
static client* window_client ( Display *display, Window win )
|
|
|
|
{
|
|
|
|
if ( win == None ) {
|
|
|
|
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
|
|
|
|
XWindowAttributes *attr = window_get_attributes ( display, win );
|
|
|
|
|
|
|
|
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
|
|
|
|
memmove ( &c->xattr, attr, sizeof ( XWindowAttributes ) );
|
|
|
|
XGetTransientForHint ( display, win, &c->trans );
|
|
|
|
|
|
|
|
c->states = window_get_atom_prop ( display, win, netatoms[_NET_WM_STATE], c->state, CLIENTSTATE );
|
|
|
|
|
2015-09-19 18:59:50 +00:00
|
|
|
c->window_types = window_get_atom_prop ( display, win, netatoms[_NET_WM_WINDOW_TYPE], c->window_type, CLIENTWINDOWTYPE );
|
2015-02-12 21:16:32 +00:00
|
|
|
char *name;
|
|
|
|
|
|
|
|
if ( ( name = window_get_text_prop ( display, c->window, netatoms[_NET_WM_NAME] ) ) && name ) {
|
|
|
|
snprintf ( c->title, CLIENTTITLE, "%s", name );
|
|
|
|
g_free ( name );
|
|
|
|
}
|
|
|
|
else if ( XFetchName ( display, c->window, &name ) ) {
|
|
|
|
snprintf ( c->title, CLIENTTITLE, "%s", name );
|
|
|
|
XFree ( name );
|
|
|
|
}
|
|
|
|
|
2015-09-19 18:59:50 +00:00
|
|
|
name = window_get_text_prop ( display, c->window, XInternAtom ( display, "WM_WINDOW_ROLE", False ) );
|
2015-02-12 21:16:32 +00:00
|
|
|
|
|
|
|
if ( name != NULL ) {
|
|
|
|
snprintf ( c->role, CLIENTROLE, "%s", name );
|
|
|
|
XFree ( name );
|
|
|
|
}
|
|
|
|
|
|
|
|
XClassHint chint;
|
|
|
|
|
|
|
|
if ( XGetClassHint ( display, c->window, &chint ) ) {
|
|
|
|
snprintf ( c->class, CLIENTCLASS, "%s", chint.res_class );
|
|
|
|
snprintf ( c->name, CLIENTNAME, "%s", chint.res_name );
|
|
|
|
XFree ( chint.res_class );
|
|
|
|
XFree ( chint.res_name );
|
|
|
|
}
|
|
|
|
|
2015-04-03 16:01:03 +00:00
|
|
|
XWMHints *wh;
|
|
|
|
if ( ( wh = XGetWMHints ( display, c->window ) ) != NULL ) {
|
|
|
|
c->hint_flags = wh->flags;
|
|
|
|
XFree ( wh );
|
|
|
|
}
|
|
|
|
|
2015-02-12 21:16:32 +00:00
|
|
|
monitor_dimensions ( display, c->xattr.screen, c->xattr.x, c->xattr.y, &c->monitor );
|
|
|
|
winlist_append ( cache_client, c->window, c );
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
2015-03-27 19:28:53 +00:00
|
|
|
typedef struct _SwitcherModePrivateData
|
|
|
|
{
|
|
|
|
unsigned int id;
|
|
|
|
char **cmd_list;
|
|
|
|
unsigned int cmd_list_length;
|
|
|
|
winlist *ids;
|
|
|
|
int config_i3_mode;
|
|
|
|
int init;
|
2015-03-31 20:45:02 +00:00
|
|
|
// Current window.
|
|
|
|
unsigned int index;
|
|
|
|
char *cache;
|
2015-03-27 19:28:53 +00:00
|
|
|
} SwitcherModePrivateData;
|
2015-02-12 21:16:32 +00:00
|
|
|
|
|
|
|
static int window_match ( char **tokens, __attribute__( ( unused ) ) const char *input,
|
2015-10-12 06:12:25 +00:00
|
|
|
__attribute__( ( unused ) ) int not_ascii,
|
2015-03-30 18:12:22 +00:00
|
|
|
int case_sensitive, unsigned int index, Switcher *sw )
|
2015-02-12 21:16:32 +00:00
|
|
|
{
|
2015-03-27 19:28:53 +00:00
|
|
|
SwitcherModePrivateData *rmpd = (SwitcherModePrivateData *) sw->private_data;
|
|
|
|
int match = 1;
|
|
|
|
winlist *ids = ( winlist * ) rmpd->ids;
|
|
|
|
client *c = window_client ( display, ids->array[index] );
|
2015-02-12 21:16:32 +00:00
|
|
|
|
|
|
|
if ( tokens ) {
|
2015-07-06 10:30:15 +00: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 };
|
|
|
|
if ( !test && c->title[0] != '\0' ) {
|
2015-10-12 06:12:25 +00:00
|
|
|
test = token_match ( ftokens, c->title, is_not_ascii ( c->title ), case_sensitive, 0, NULL );
|
2015-07-06 10:30:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( !test && c->class[0] != '\0' ) {
|
2015-10-12 06:12:25 +00:00
|
|
|
test = token_match ( ftokens, c->class, is_not_ascii ( c->class ), case_sensitive, 0, NULL );
|
2015-07-06 10:30:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( !test && c->role[0] != '\0' ) {
|
2015-10-12 06:12:25 +00:00
|
|
|
test = token_match ( ftokens, c->role, is_not_ascii ( c->role ), case_sensitive, 0, NULL );
|
2015-07-06 10:30:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( !test && c->name[0] != '\0' ) {
|
2015-10-12 06:12:25 +00:00
|
|
|
test = token_match ( ftokens, c->name, is_not_ascii ( c->name ), case_sensitive, 0, NULL );
|
2015-07-06 10:30:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( test == 0 ) {
|
|
|
|
match = 0;
|
|
|
|
}
|
2015-02-12 21:16:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return match;
|
|
|
|
}
|
2015-03-27 19:28:53 +00:00
|
|
|
static void window_mode_init ( Switcher *sw )
|
2015-02-12 21:16:32 +00:00
|
|
|
{
|
2015-03-27 19:28:53 +00:00
|
|
|
if ( sw->private_data == NULL ) {
|
|
|
|
SwitcherModePrivateData *pd = g_malloc0 ( sizeof ( *pd ) );
|
|
|
|
sw->private_data = (void *) pd;
|
|
|
|
pd->init = FALSE;
|
2015-02-12 21:16:32 +00:00
|
|
|
}
|
2015-03-27 19:28:53 +00:00
|
|
|
}
|
|
|
|
|
2015-09-07 15:41:48 +00:00
|
|
|
static char ** _window_mode_get_data ( unsigned int *length, Switcher *sw, unsigned int cd )
|
2015-03-27 19:28:53 +00:00
|
|
|
{
|
|
|
|
SwitcherModePrivateData *pd = (SwitcherModePrivateData *) sw->private_data;
|
|
|
|
if ( !pd->init ) {
|
|
|
|
Screen *screen = DefaultScreenOfDisplay ( display );
|
|
|
|
Window root = RootWindow ( display, XScreenNumberOfScreen ( screen ) );
|
|
|
|
// find window list
|
|
|
|
Atom type;
|
2015-11-10 22:52:52 +00:00
|
|
|
int nwins = 0;
|
2015-03-27 19:28:53 +00:00
|
|
|
Window wins[100];
|
|
|
|
int count = 0;
|
|
|
|
Window curr_win_id = 0;
|
|
|
|
// Create cache
|
|
|
|
|
|
|
|
x11_cache_create ();
|
|
|
|
// Check for i3
|
|
|
|
pd->config_i3_mode = i3_support_initialize ( display );
|
|
|
|
|
|
|
|
// Get the active window so we can highlight this.
|
2015-09-19 18:59:50 +00:00
|
|
|
if ( !( window_get_prop ( display, root, netatoms[_NET_ACTIVE_WINDOW], &type, &count, &curr_win_id, sizeof ( Window ) )
|
2015-03-27 19:28:53 +00:00
|
|
|
&& type == XA_WINDOW && count > 0 ) ) {
|
|
|
|
curr_win_id = 0;
|
|
|
|
}
|
2015-02-12 21:16:32 +00:00
|
|
|
|
2015-09-07 15:41:48 +00:00
|
|
|
// Get the current desktop.
|
|
|
|
unsigned long current_desktop = 0;
|
2015-09-19 18:59:50 +00:00
|
|
|
if ( !window_get_cardinal_prop ( display, root, netatoms[_NET_CURRENT_DESKTOP], ¤t_desktop, 1 ) ) {
|
2015-09-07 15:41:48 +00:00
|
|
|
current_desktop = 0;
|
|
|
|
}
|
|
|
|
|
2015-11-10 22:52:52 +00:00
|
|
|
unsigned int nw = 100 * sizeof ( Window );
|
|
|
|
// First try Stacking order.. If this fails.
|
|
|
|
if ( !( window_get_prop ( display, root, netatoms[_NET_CLIENT_LIST_STACKING], &type, &nwins, wins, nw )
|
|
|
|
&& type == XA_WINDOW ) ) {
|
|
|
|
// Try to get order by age.
|
|
|
|
if ( !( window_get_prop ( display, root, netatoms[_NET_CLIENT_LIST], &type, &nwins, wins, nw )
|
|
|
|
&& type == XA_WINDOW ) ) {
|
|
|
|
nwins = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ( nwins > 0 ) {
|
2015-03-27 19:28:53 +00:00
|
|
|
char pattern[50];
|
|
|
|
int i;
|
|
|
|
unsigned int classfield = 0;
|
|
|
|
unsigned long desktops = 0;
|
|
|
|
// 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 ();
|
2015-02-12 21:16:32 +00:00
|
|
|
|
2015-03-27 19:28:53 +00:00
|
|
|
// calc widths of fields
|
|
|
|
for ( i = nwins - 1; i > -1; i-- ) {
|
|
|
|
client *c;
|
2015-02-12 21:16:32 +00:00
|
|
|
|
2015-03-27 19:28:53 +00:00
|
|
|
if ( ( c = window_client ( display, wins[i] ) )
|
|
|
|
&& !c->xattr.override_redirect
|
2015-08-27 19:09:12 +00:00
|
|
|
&& !client_has_window_type ( c, netatoms[_NET_WM_WINDOW_TYPE_DOCK] )
|
2015-08-31 06:00:23 +00:00
|
|
|
&& !client_has_window_type ( c, netatoms[_NET_WM_WINDOW_TYPE_DESKTOP] )
|
2015-03-27 19:28:53 +00:00
|
|
|
&& !client_has_state ( c, netatoms[_NET_WM_STATE_SKIP_PAGER] )
|
|
|
|
&& !client_has_state ( c, netatoms[_NET_WM_STATE_SKIP_TASKBAR] ) ) {
|
|
|
|
classfield = MAX ( classfield, strlen ( c->class ) );
|
2015-02-12 21:16:32 +00:00
|
|
|
|
2015-04-02 20:23:17 +00:00
|
|
|
if ( client_has_state ( c, netatoms[_NET_WM_STATE_DEMANDS_ATTENTION] ) ) {
|
|
|
|
c->demands = TRUE;
|
|
|
|
}
|
2015-04-03 16:01:03 +00:00
|
|
|
if ( ( c->hint_flags & XUrgencyHint ) == XUrgencyHint ) {
|
|
|
|
c->demands = TRUE;
|
|
|
|
}
|
2015-02-12 21:16:32 +00:00
|
|
|
|
2015-03-27 19:28:53 +00:00
|
|
|
if ( c->window == curr_win_id ) {
|
|
|
|
c->active = TRUE;
|
|
|
|
}
|
|
|
|
winlist_append ( pd->ids, c->window, NULL );
|
2015-02-12 21:16:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-27 19:28:53 +00:00
|
|
|
// Create pattern for printing the line.
|
2015-09-19 18:59:50 +00:00
|
|
|
if ( !window_get_cardinal_prop ( display, root, netatoms[_NET_NUMBER_OF_DESKTOPS], &desktops, 1 ) ) {
|
2015-03-27 19:28:53 +00:00
|
|
|
desktops = 1;
|
|
|
|
}
|
|
|
|
if ( pd->config_i3_mode ) {
|
|
|
|
sprintf ( pattern, "%%-%ds %%s", MAX ( 5, classfield ) );
|
|
|
|
}
|
|
|
|
else{
|
2015-09-19 10:21:30 +00:00
|
|
|
sprintf ( pattern, "%%-%ds %%-%ds %%s", desktops < 10 ? 1 : 2,
|
|
|
|
MAX ( 5, classfield ) );
|
2015-03-27 19:28:53 +00: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++ ) {
|
|
|
|
Window w = pd->ids->array[i];
|
|
|
|
client *c;
|
|
|
|
|
|
|
|
if ( ( c = window_client ( display, w ) ) ) {
|
|
|
|
// final line format
|
|
|
|
unsigned long wmdesktop;
|
|
|
|
char desktop[5];
|
|
|
|
desktop[0] = 0;
|
2015-09-19 18:59:50 +00:00
|
|
|
char *line = g_malloc ( strlen ( c->title ) + strlen ( c->class ) + classfield + 50 );
|
2015-03-27 19:28:53 +00:00
|
|
|
if ( !pd->config_i3_mode ) {
|
2015-08-23 22:28:07 +00:00
|
|
|
// find client's desktop.
|
2015-09-19 18:59:50 +00:00
|
|
|
if ( !window_get_cardinal_prop ( display, c->window, netatoms[_NET_WM_DESKTOP], &wmdesktop, 1 ) ) {
|
2015-08-26 16:11:53 +00:00
|
|
|
// Assume the client is on all desktops.
|
2015-03-27 19:28:53 +00:00
|
|
|
wmdesktop = 0xFFFFFFFF;
|
|
|
|
}
|
2015-09-07 15:41:48 +00:00
|
|
|
else if ( cd && wmdesktop != current_desktop ) {
|
|
|
|
g_free ( line );
|
|
|
|
continue;
|
|
|
|
}
|
2015-03-27 19:28:53 +00:00
|
|
|
|
|
|
|
if ( wmdesktop < 0xFFFFFFFF ) {
|
2015-08-23 22:28:07 +00:00
|
|
|
sprintf ( desktop, "%d", (int) wmdesktop );
|
2015-03-27 19:28:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sprintf ( line, pattern, desktop, c->class, c->title );
|
2015-02-12 21:16:32 +00:00
|
|
|
}
|
2015-03-27 19:28:53 +00:00
|
|
|
else{
|
|
|
|
sprintf ( line, pattern, c->class, c->title );
|
2015-02-12 21:16:32 +00:00
|
|
|
}
|
|
|
|
|
2015-03-27 19:28:53 +00:00
|
|
|
pd->cmd_list[pd->cmd_list_length++] = line;
|
2015-02-12 21:16:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-03-27 19:28:53 +00:00
|
|
|
pd->init = TRUE;
|
|
|
|
}
|
|
|
|
*length = pd->cmd_list_length;
|
|
|
|
return pd->cmd_list;
|
|
|
|
}
|
2015-09-07 15:41:48 +00:00
|
|
|
static char ** window_mode_get_data_cd ( unsigned int *length, Switcher *sw )
|
|
|
|
{
|
|
|
|
return _window_mode_get_data ( length, sw, TRUE );
|
|
|
|
}
|
|
|
|
static char ** window_mode_get_data ( unsigned int *length, Switcher *sw )
|
|
|
|
{
|
|
|
|
return _window_mode_get_data ( length, sw, FALSE );
|
|
|
|
}
|
2015-09-19 10:21:30 +00:00
|
|
|
static SwitcherMode window_mode_result ( int mretv, G_GNUC_UNUSED char **input,
|
|
|
|
unsigned int selected_line,
|
|
|
|
Switcher *sw )
|
2015-03-27 19:28:53 +00:00
|
|
|
{
|
|
|
|
SwitcherModePrivateData *rmpd = (SwitcherModePrivateData *) sw->private_data;
|
|
|
|
SwitcherMode retv = MODE_EXIT;
|
|
|
|
if ( mretv & MENU_NEXT ) {
|
|
|
|
retv = NEXT_DIALOG;
|
|
|
|
}
|
|
|
|
else if ( mretv & MENU_PREVIOUS ) {
|
|
|
|
retv = PREVIOUS_DIALOG;
|
|
|
|
}
|
2015-05-03 11:04:03 +00:00
|
|
|
else if ( ( mretv & MENU_QUICK_SWITCH ) == MENU_QUICK_SWITCH ) {
|
|
|
|
retv = ( mretv & MENU_LOWER_MASK );
|
2015-03-27 19:28:53 +00:00
|
|
|
}
|
2015-10-31 17:04:55 +00:00
|
|
|
else if ( ( mretv & ( MENU_OK ) ) && rmpd->cmd_list[selected_line] ) {
|
2015-03-27 19:28:53 +00:00
|
|
|
if ( rmpd->config_i3_mode ) {
|
|
|
|
// Hack for i3.
|
|
|
|
i3_support_focus_window ( rmpd->ids->array[selected_line] );
|
2015-02-12 21:16:32 +00:00
|
|
|
}
|
2015-03-27 19:28:53 +00:00
|
|
|
else{
|
|
|
|
Screen *screen = DefaultScreenOfDisplay ( display );
|
|
|
|
Window root = RootWindow ( display, XScreenNumberOfScreen ( screen ) );
|
|
|
|
// Change to the desktop of the selected window/client.
|
|
|
|
// TODO: get rid of strtol
|
2015-09-19 18:59:50 +00:00
|
|
|
window_send_message ( display, root, root, netatoms[_NET_CURRENT_DESKTOP], strtol ( rmpd->cmd_list[selected_line], NULL, 10 ),
|
2015-03-27 19:28:53 +00:00
|
|
|
SubstructureNotifyMask | SubstructureRedirectMask, 0 );
|
|
|
|
XSync ( display, False );
|
|
|
|
|
2015-09-19 18:59:50 +00:00
|
|
|
window_send_message ( display, root, rmpd->ids->array[selected_line], netatoms[_NET_ACTIVE_WINDOW], 2, // 2 = pager
|
2015-03-27 19:28:53 +00:00
|
|
|
SubstructureNotifyMask | SubstructureRedirectMask, 0 );
|
2015-02-12 21:16:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return retv;
|
|
|
|
}
|
2015-03-27 19:28:53 +00:00
|
|
|
|
|
|
|
static void window_mode_destroy ( Switcher *sw )
|
|
|
|
{
|
|
|
|
SwitcherModePrivateData *rmpd = (SwitcherModePrivateData *) sw->private_data;
|
|
|
|
if ( rmpd != NULL ) {
|
|
|
|
g_strfreev ( rmpd->cmd_list );
|
|
|
|
winlist_free ( rmpd->ids );
|
|
|
|
i3_support_free_internals ();
|
|
|
|
x11_cache_free ();
|
2015-03-31 20:45:02 +00:00
|
|
|
g_free ( rmpd->cache );
|
2015-03-27 19:28:53 +00:00
|
|
|
g_free ( rmpd );
|
|
|
|
sw->private_data = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-03 16:34:02 +00:00
|
|
|
static const char *mgrv ( unsigned int selected_line, void *sw, int *state )
|
2015-03-31 20:45:02 +00:00
|
|
|
{
|
|
|
|
SwitcherModePrivateData *rmpd = ( (Switcher *) sw )->private_data;
|
2015-04-02 20:23:17 +00:00
|
|
|
if ( window_client ( display, rmpd->ids->array[selected_line] )->demands ) {
|
2015-04-03 16:40:07 +00:00
|
|
|
*state |= URGENT;
|
2015-04-02 20:23:17 +00:00
|
|
|
}
|
2015-03-31 20:45:02 +00:00
|
|
|
if ( window_client ( display, rmpd->ids->array[selected_line] )->active ) {
|
2015-04-03 16:40:07 +00:00
|
|
|
*state |= ACTIVE;
|
2015-03-31 20:45:02 +00:00
|
|
|
}
|
|
|
|
return rmpd->cmd_list[selected_line];
|
|
|
|
}
|
|
|
|
|
2015-03-27 19:28:53 +00:00
|
|
|
Switcher window_mode =
|
|
|
|
{
|
|
|
|
.name = "window",
|
|
|
|
.keycfg = NULL,
|
|
|
|
.keystr = NULL,
|
|
|
|
.modmask = AnyModifier,
|
|
|
|
.init = window_mode_init,
|
|
|
|
.get_data = window_mode_get_data,
|
|
|
|
.result = window_mode_result,
|
|
|
|
.destroy = window_mode_destroy,
|
|
|
|
.token_match = window_match,
|
2015-03-31 20:45:02 +00:00
|
|
|
.mgrv = mgrv,
|
2015-03-27 19:28:53 +00:00
|
|
|
.private_data = NULL,
|
|
|
|
.free = NULL
|
|
|
|
};
|
2015-09-07 15:41:48 +00:00
|
|
|
Switcher window_mode_cd =
|
|
|
|
{
|
|
|
|
.name = "windowcd",
|
|
|
|
.keycfg = NULL,
|
|
|
|
.keystr = NULL,
|
|
|
|
.modmask = AnyModifier,
|
|
|
|
.init = window_mode_init,
|
|
|
|
.get_data = window_mode_get_data_cd,
|
|
|
|
.result = window_mode_result,
|
|
|
|
.destroy = window_mode_destroy,
|
|
|
|
.token_match = window_match,
|
|
|
|
.mgrv = mgrv,
|
|
|
|
.private_data = NULL,
|
|
|
|
.free = NULL
|
|
|
|
};
|
2015-09-14 06:57:10 +00:00
|
|
|
|
|
|
|
#endif // WINDOW_MODE
|
|
|
|
|