2017-04-15 10:32:05 +00:00
|
|
|
|
/*
|
2015-02-12 21:34:06 +00:00
|
|
|
|
* rofi
|
|
|
|
|
*
|
|
|
|
|
* MIT/X11 License
|
2017-04-15 10:32:05 +00:00
|
|
|
|
* Copyright © 2012 Sean Pringle <sean.pringle@gmail.com>
|
2020-01-01 11:23:12 +00:00
|
|
|
|
* Copyright © 2013-2020 Qball Cow <qball@gmpclient.org>
|
2015-02-12 21:34:06 +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.
|
|
|
|
|
*
|
|
|
|
|
*/
|
2017-04-15 09:58:49 +00:00
|
|
|
|
|
|
|
|
|
/** Log domain for this module */
|
|
|
|
|
#define G_LOG_DOMAIN "X11Helper"
|
|
|
|
|
|
2015-02-09 18:35:51 +00:00
|
|
|
|
#include <config.h>
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
#include <stdint.h>
|
|
|
|
|
#include <glib.h>
|
2015-09-27 09:46:19 +00:00
|
|
|
|
#include <cairo.h>
|
2016-05-06 10:40:28 +00:00
|
|
|
|
#include <cairo-xcb.h>
|
2015-02-09 18:35:51 +00:00
|
|
|
|
|
2016-02-21 18:42:32 +00:00
|
|
|
|
#include <xcb/xcb.h>
|
2017-03-29 17:49:10 +00:00
|
|
|
|
#include <xcb/xcb_aux.h>
|
2016-08-19 08:46:38 +00:00
|
|
|
|
#include <xcb/randr.h>
|
2016-08-31 07:10:30 +00:00
|
|
|
|
#include <xcb/xinerama.h>
|
2016-08-22 06:39:22 +00:00
|
|
|
|
#include <xcb/xcb_ewmh.h>
|
2016-08-31 07:10:30 +00:00
|
|
|
|
#include <xcb/xproto.h>
|
2017-05-31 22:39:11 +00:00
|
|
|
|
#include <xcb/xkb.h>
|
|
|
|
|
#include <xkbcommon/xkbcommon.h>
|
|
|
|
|
#include <xkbcommon/xkbcommon-x11.h>
|
2018-06-12 12:11:08 +00:00
|
|
|
|
/** Indicate that we know the startup notification api is not yet stable. */
|
2017-06-02 12:57:51 +00:00
|
|
|
|
#define SN_API_NOT_YET_FROZEN
|
2018-06-12 12:11:08 +00:00
|
|
|
|
/** This function is declared as sn_launcher_context_set_application_id but implemented as sn_launcher_set_application_id. Quick Fix. */
|
2017-06-02 12:57:51 +00:00
|
|
|
|
#define sn_launcher_context_set_application_id sn_launcher_set_application_id
|
2017-08-18 09:38:54 +00:00
|
|
|
|
#include "rofi-types.h"
|
2017-06-02 12:57:51 +00:00
|
|
|
|
#include <libsn/sn.h>
|
2017-06-01 12:55:17 +00:00
|
|
|
|
#include "display.h"
|
2016-03-01 17:11:55 +00:00
|
|
|
|
#include "xcb-internal.h"
|
|
|
|
|
#include "xcb.h"
|
2016-01-07 15:01:56 +00:00
|
|
|
|
#include "settings.h"
|
2016-04-10 10:05:34 +00:00
|
|
|
|
#include "helper.h"
|
2017-05-31 22:39:11 +00:00
|
|
|
|
#include "timings.h"
|
2015-02-09 18:35:51 +00:00
|
|
|
|
|
2015-04-06 15:13:26 +00:00
|
|
|
|
#include <rofi.h>
|
2018-02-07 15:06:16 +00:00
|
|
|
|
|
2020-11-14 23:14:02 +00:00
|
|
|
|
/** Minimal randr preferred for running rofi (1.5) (Major version number) */
|
2018-08-08 13:55:13 +00:00
|
|
|
|
#define RANDR_PREF_MAJOR_VERSION 1
|
2020-11-14 23:14:02 +00:00
|
|
|
|
/** Minimal randr preferred for running rofi (1.5) (Minor version number) */
|
2018-08-08 13:55:13 +00:00
|
|
|
|
#define RANDR_PREF_MINOR_VERSION 5
|
2018-02-07 15:06:16 +00:00
|
|
|
|
|
2016-10-25 20:45:11 +00:00
|
|
|
|
/** Checks if the if x and y is inside rectangle. */
|
2016-10-26 06:24:34 +00:00
|
|
|
|
#define INTERSECT( x, y, x1, y1, w1, h1 ) ( ( ( ( x ) >= ( x1 ) ) && ( ( x ) < ( x1 + w1 ) ) ) && ( ( ( y ) >= ( y1 ) ) && ( ( y ) < ( y1 + h1 ) ) ) )
|
2017-09-17 19:40:36 +00:00
|
|
|
|
WindowManagerQuirk current_window_manager = WM_EWHM;
|
2017-02-11 15:53:37 +00:00
|
|
|
|
|
2016-11-15 07:24:06 +00:00
|
|
|
|
/**
|
|
|
|
|
* Structure holding xcb objects needed to function.
|
|
|
|
|
*/
|
2016-11-15 07:24:27 +00:00
|
|
|
|
struct _xcb_stuff xcb_int = {
|
2016-08-19 08:46:38 +00:00
|
|
|
|
.connection = NULL,
|
|
|
|
|
.screen = NULL,
|
2018-08-08 13:55:13 +00:00
|
|
|
|
.screen_nbr = -1,
|
2016-08-19 08:46:38 +00:00
|
|
|
|
.sndisplay = NULL,
|
|
|
|
|
.sncontext = NULL,
|
|
|
|
|
.monitors = NULL
|
2016-03-01 17:33:26 +00:00
|
|
|
|
};
|
2016-11-15 07:24:27 +00:00
|
|
|
|
xcb_stuff *xcb = &xcb_int;
|
2016-03-01 17:33:26 +00:00
|
|
|
|
|
2016-11-15 07:24:06 +00:00
|
|
|
|
/**
|
|
|
|
|
* Depth of root window.
|
|
|
|
|
*/
|
2016-11-15 07:24:27 +00:00
|
|
|
|
xcb_depth_t *depth = NULL;
|
|
|
|
|
xcb_visualtype_t *visual = NULL;
|
|
|
|
|
xcb_colormap_t map = XCB_COLORMAP_NONE;
|
2016-11-15 07:24:06 +00:00
|
|
|
|
/**
|
|
|
|
|
* Visual of the root window.
|
|
|
|
|
*/
|
2016-11-15 07:24:27 +00:00
|
|
|
|
static xcb_visualtype_t *root_visual = NULL;
|
|
|
|
|
xcb_atom_t netatoms[NUM_NETATOMS];
|
|
|
|
|
const char *netatom_names[] = { EWMH_ATOMS ( ATOM_CHAR ) };
|
2016-11-15 07:24:06 +00:00
|
|
|
|
|
2020-02-02 12:56:37 +00:00
|
|
|
|
static xcb_visualtype_t * lookup_visual ( xcb_screen_t *s, xcb_visualid_t visual )
|
2019-10-17 19:19:19 +00:00
|
|
|
|
{
|
|
|
|
|
xcb_depth_iterator_t d;
|
2020-02-02 12:56:37 +00:00
|
|
|
|
d = xcb_screen_allowed_depths_iterator ( s );
|
|
|
|
|
for (; d.rem; xcb_depth_next ( &d ) ) {
|
|
|
|
|
xcb_visualtype_iterator_t v = xcb_depth_visuals_iterator ( d.data );
|
|
|
|
|
for (; v.rem; xcb_visualtype_next ( &v ) ) {
|
|
|
|
|
if ( v.data->visual_id == visual ) {
|
2019-10-17 19:19:19 +00:00
|
|
|
|
return v.data;
|
2020-02-02 12:56:37 +00:00
|
|
|
|
}
|
2019-10-17 19:19:19 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cairo_surface_t *x11_helper_get_screenshot_surface_window ( xcb_window_t window, int size )
|
|
|
|
|
{
|
|
|
|
|
xcb_get_geometry_cookie_t cookie;
|
2020-02-02 12:56:37 +00:00
|
|
|
|
xcb_get_geometry_reply_t *reply;
|
2019-10-17 19:19:19 +00:00
|
|
|
|
|
2020-02-02 12:56:37 +00:00
|
|
|
|
cookie = xcb_get_geometry ( xcb->connection, window );
|
|
|
|
|
reply = xcb_get_geometry_reply ( xcb->connection, cookie, NULL );
|
2019-10-17 19:19:19 +00:00
|
|
|
|
if ( reply == NULL ) {
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-02 12:56:37 +00:00
|
|
|
|
xcb_get_window_attributes_cookie_t attributesCookie = xcb_get_window_attributes ( xcb->connection, window );
|
|
|
|
|
xcb_get_window_attributes_reply_t *attributes = xcb_get_window_attributes_reply ( xcb->connection,
|
|
|
|
|
attributesCookie,
|
|
|
|
|
NULL );
|
|
|
|
|
if ( attributes == NULL || ( attributes->map_state != XCB_MAP_STATE_VIEWABLE ) ) {
|
|
|
|
|
free ( reply );
|
|
|
|
|
if ( attributes ) {
|
|
|
|
|
free ( attributes );
|
|
|
|
|
}
|
2019-10-17 19:19:19 +00:00
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
// Create a cairo surface for the window.
|
2020-02-02 12:56:37 +00:00
|
|
|
|
xcb_visualtype_t * vt = lookup_visual ( xcb->screen, attributes->visual );
|
2019-10-17 19:19:19 +00:00
|
|
|
|
free ( attributes );
|
|
|
|
|
|
2020-02-02 12:56:37 +00:00
|
|
|
|
cairo_surface_t *t = cairo_xcb_surface_create ( xcb->connection, window, vt, reply->width, reply->height );
|
2019-10-17 19:19:19 +00:00
|
|
|
|
|
|
|
|
|
if ( cairo_surface_status ( t ) != CAIRO_STATUS_SUCCESS ) {
|
2020-02-02 12:56:37 +00:00
|
|
|
|
cairo_surface_destroy ( t );
|
|
|
|
|
free ( reply );
|
2019-10-17 19:19:19 +00:00
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Scale the image, as we don't want to keep large one around.
|
2020-02-02 12:56:37 +00:00
|
|
|
|
int max = MAX ( reply->width, reply->height );
|
|
|
|
|
double scale = (double) size / max;
|
2019-10-17 19:19:19 +00:00
|
|
|
|
|
2020-02-02 12:56:37 +00:00
|
|
|
|
cairo_surface_t *s2 = cairo_surface_create_similar_image ( t, CAIRO_FORMAT_ARGB32, reply->width * scale, reply->height * scale );
|
2019-10-17 19:19:19 +00:00
|
|
|
|
free ( reply );
|
|
|
|
|
|
|
|
|
|
if ( cairo_surface_status ( s2 ) != CAIRO_STATUS_SUCCESS ) {
|
2020-02-02 12:56:37 +00:00
|
|
|
|
cairo_surface_destroy ( t );
|
2019-10-17 19:19:19 +00:00
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
// Paint it in.
|
2020-02-02 12:56:37 +00:00
|
|
|
|
cairo_t *d = cairo_create ( s2 );
|
2019-10-17 19:19:19 +00:00
|
|
|
|
cairo_scale ( d, scale, scale );
|
|
|
|
|
cairo_set_source_surface ( d, t, 0, 0 );
|
2020-02-02 12:56:37 +00:00
|
|
|
|
cairo_paint ( d );
|
|
|
|
|
cairo_destroy ( d );
|
2019-10-17 19:19:19 +00:00
|
|
|
|
|
2020-02-02 12:56:37 +00:00
|
|
|
|
cairo_surface_destroy ( t );
|
2019-10-17 19:19:19 +00:00
|
|
|
|
return s2;
|
|
|
|
|
}
|
2016-11-15 07:24:06 +00:00
|
|
|
|
/**
|
|
|
|
|
* Holds for each supported modifier the possible modifier mask.
|
|
|
|
|
* Check x11_mod_masks[MODIFIER]&mask != 0 to see if MODIFIER is activated.
|
|
|
|
|
*/
|
|
|
|
|
cairo_surface_t *x11_helper_get_screenshot_surface ( void )
|
|
|
|
|
{
|
|
|
|
|
return cairo_xcb_surface_create ( xcb->connection,
|
2018-08-08 13:55:13 +00:00
|
|
|
|
xcb_stuff_get_root_window (), root_visual,
|
|
|
|
|
xcb->screen->width_in_pixels, xcb->screen->height_in_pixels );
|
2016-11-15 07:24:06 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-05-06 10:40:28 +00:00
|
|
|
|
static xcb_pixmap_t get_root_pixmap ( xcb_connection_t *c,
|
2018-08-08 13:55:13 +00:00
|
|
|
|
xcb_screen_t *screen,
|
|
|
|
|
xcb_atom_t atom )
|
2016-05-06 10:40:28 +00:00
|
|
|
|
{
|
|
|
|
|
xcb_get_property_cookie_t cookie;
|
|
|
|
|
xcb_get_property_reply_t *reply;
|
2016-06-26 15:10:29 +00:00
|
|
|
|
xcb_pixmap_t rootpixmap = XCB_NONE;
|
2016-05-06 10:40:28 +00:00
|
|
|
|
|
|
|
|
|
cookie = xcb_get_property ( c,
|
2018-08-08 13:55:13 +00:00
|
|
|
|
0,
|
|
|
|
|
screen->root,
|
|
|
|
|
atom,
|
|
|
|
|
XCB_ATOM_PIXMAP,
|
|
|
|
|
0,
|
|
|
|
|
1 );
|
2016-05-06 10:40:28 +00:00
|
|
|
|
|
|
|
|
|
reply = xcb_get_property_reply ( c, cookie, NULL );
|
|
|
|
|
|
2016-06-26 15:10:29 +00:00
|
|
|
|
if ( reply ) {
|
|
|
|
|
if ( xcb_get_property_value_length ( reply ) == sizeof ( xcb_pixmap_t ) ) {
|
|
|
|
|
memcpy ( &rootpixmap, xcb_get_property_value ( reply ), sizeof ( xcb_pixmap_t ) );
|
|
|
|
|
}
|
|
|
|
|
free ( reply );
|
2016-05-06 10:40:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-06-26 15:10:29 +00:00
|
|
|
|
return rootpixmap;
|
2016-05-06 10:40:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cairo_surface_t * x11_helper_get_bg_surface ( void )
|
|
|
|
|
{
|
|
|
|
|
xcb_pixmap_t pm = get_root_pixmap ( xcb->connection, xcb->screen, netatoms[ESETROOT_PMAP_ID] );
|
|
|
|
|
if ( pm == XCB_NONE ) {
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
return cairo_xcb_surface_create ( xcb->connection, pm, root_visual,
|
2018-08-08 13:55:13 +00:00
|
|
|
|
xcb->screen->width_in_pixels, xcb->screen->height_in_pixels );
|
2016-05-06 10:40:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-02-09 18:35:51 +00:00
|
|
|
|
// retrieve a text property from a window
|
|
|
|
|
// technically we could use window_get_prop(), but this is better for character set support
|
2016-03-01 17:11:55 +00:00
|
|
|
|
char* window_get_text_prop ( xcb_window_t w, xcb_atom_t atom )
|
2015-02-09 18:35:51 +00:00
|
|
|
|
{
|
2016-03-01 17:11:55 +00:00
|
|
|
|
xcb_get_property_cookie_t c = xcb_get_property ( xcb->connection, 0, w, atom, XCB_GET_PROPERTY_TYPE_ANY, 0, UINT_MAX );
|
|
|
|
|
xcb_get_property_reply_t *r = xcb_get_property_reply ( xcb->connection, c, NULL );
|
2016-02-28 14:32:53 +00:00
|
|
|
|
if ( r ) {
|
2016-03-04 18:41:32 +00:00
|
|
|
|
if ( xcb_get_property_value_length ( r ) > 0 ) {
|
2016-04-10 10:05:34 +00:00
|
|
|
|
char *str = NULL;
|
|
|
|
|
if ( r->type == netatoms[UTF8_STRING] ) {
|
|
|
|
|
str = g_strndup ( xcb_get_property_value ( r ), xcb_get_property_value_length ( r ) );
|
|
|
|
|
}
|
|
|
|
|
else if ( r->type == netatoms[STRING] ) {
|
|
|
|
|
str = rofi_latin_to_utf8_strdup ( xcb_get_property_value ( r ), xcb_get_property_value_length ( r ) );
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
str = g_strdup ( "Invalid encoding." );
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-04 18:41:32 +00:00
|
|
|
|
free ( r );
|
|
|
|
|
return str;
|
|
|
|
|
}
|
2016-02-28 14:32:53 +00:00
|
|
|
|
free ( r );
|
2015-02-09 18:35:51 +00:00
|
|
|
|
}
|
2016-02-27 23:09:05 +00:00
|
|
|
|
return NULL;
|
2015-02-09 18:35:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-03-01 17:11:55 +00:00
|
|
|
|
void window_set_atom_prop ( xcb_window_t w, xcb_atom_t prop, xcb_atom_t *atoms, int count )
|
2015-02-09 18:35:51 +00:00
|
|
|
|
{
|
2016-03-01 17:11:55 +00:00
|
|
|
|
xcb_change_property ( xcb->connection, XCB_PROP_MODE_REPLACE, w, prop, XCB_ATOM_ATOM, 32, count, atoms );
|
2015-02-09 18:35:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-08-19 08:46:38 +00:00
|
|
|
|
/****
|
|
|
|
|
* Code used to get monitor layout.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Free monitor structure.
|
|
|
|
|
*/
|
|
|
|
|
static void x11_monitor_free ( workarea *m )
|
2015-10-18 11:40:39 +00:00
|
|
|
|
{
|
2016-08-19 08:46:38 +00:00
|
|
|
|
g_free ( m->name );
|
|
|
|
|
g_free ( m );
|
|
|
|
|
}
|
2016-03-04 16:03:24 +00:00
|
|
|
|
|
2016-08-19 08:46:38 +00:00
|
|
|
|
static void x11_monitors_free ( void )
|
|
|
|
|
{
|
|
|
|
|
while ( xcb->monitors != NULL ) {
|
|
|
|
|
workarea *m = xcb->monitors;
|
|
|
|
|
xcb->monitors = m->next;
|
|
|
|
|
x11_monitor_free ( m );
|
2016-02-27 19:45:47 +00:00
|
|
|
|
}
|
2016-08-19 08:46:38 +00:00
|
|
|
|
}
|
2015-02-09 18:35:51 +00:00
|
|
|
|
|
2016-08-19 08:46:38 +00:00
|
|
|
|
/**
|
|
|
|
|
* Create monitor based on output id
|
|
|
|
|
*/
|
|
|
|
|
static workarea * x11_get_monitor_from_output ( xcb_randr_output_t out )
|
|
|
|
|
{
|
|
|
|
|
xcb_randr_get_output_info_reply_t *op_reply;
|
|
|
|
|
xcb_randr_get_crtc_info_reply_t *crtc_reply;
|
|
|
|
|
xcb_randr_get_output_info_cookie_t it = xcb_randr_get_output_info ( xcb->connection, out, XCB_CURRENT_TIME );
|
|
|
|
|
op_reply = xcb_randr_get_output_info_reply ( xcb->connection, it, NULL );
|
|
|
|
|
if ( op_reply->crtc == XCB_NONE ) {
|
|
|
|
|
free ( op_reply );
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
xcb_randr_get_crtc_info_cookie_t ct = xcb_randr_get_crtc_info ( xcb->connection, op_reply->crtc, XCB_CURRENT_TIME );
|
|
|
|
|
crtc_reply = xcb_randr_get_crtc_info_reply ( xcb->connection, ct, NULL );
|
|
|
|
|
if ( !crtc_reply ) {
|
|
|
|
|
free ( op_reply );
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
workarea *retv = g_malloc0 ( sizeof ( workarea ) );
|
|
|
|
|
retv->x = crtc_reply->x;
|
|
|
|
|
retv->y = crtc_reply->y;
|
|
|
|
|
retv->w = crtc_reply->width;
|
|
|
|
|
retv->h = crtc_reply->height;
|
|
|
|
|
|
2017-01-21 11:58:52 +00:00
|
|
|
|
retv->mw = op_reply->mm_width;
|
|
|
|
|
retv->mh = op_reply->mm_height;
|
|
|
|
|
|
2016-08-19 08:46:38 +00:00
|
|
|
|
char *tname = (char *) xcb_randr_get_output_info_name ( op_reply );
|
|
|
|
|
int tname_len = xcb_randr_get_output_info_name_length ( op_reply );
|
2016-03-04 16:03:24 +00:00
|
|
|
|
|
2016-08-19 08:46:38 +00:00
|
|
|
|
retv->name = g_malloc0 ( ( tname_len + 1 ) * sizeof ( char ) );
|
|
|
|
|
memcpy ( retv->name, tname, tname_len );
|
|
|
|
|
|
|
|
|
|
free ( crtc_reply );
|
|
|
|
|
free ( op_reply );
|
|
|
|
|
return retv;
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-08 13:55:13 +00:00
|
|
|
|
#if ( ( ( XCB_RANDR_MAJOR_VERSION >= RANDR_PREF_MAJOR_VERSION ) && ( XCB_RANDR_MINOR_VERSION >= RANDR_PREF_MINOR_VERSION ) ) \
|
|
|
|
|
|| XCB_RANDR_MAJOR_VERSION > RANDR_PREF_MAJOR_VERSION )
|
2018-02-07 15:06:16 +00:00
|
|
|
|
/**
|
|
|
|
|
* @param mon The randr monitor to parse.
|
|
|
|
|
*
|
|
|
|
|
* Create monitor based on xrandr monitor id.
|
|
|
|
|
*
|
|
|
|
|
* @returns A workarea representing the monitor mon
|
|
|
|
|
*/
|
|
|
|
|
static workarea *x11_get_monitor_from_randr_monitor ( xcb_randr_monitor_info_t *mon )
|
|
|
|
|
{
|
|
|
|
|
// Query to the name of the monitor.
|
2018-08-08 13:55:13 +00:00
|
|
|
|
xcb_generic_error_t *err;
|
|
|
|
|
xcb_get_atom_name_cookie_t anc = xcb_get_atom_name ( xcb->connection, mon->name );
|
|
|
|
|
xcb_get_atom_name_reply_t *atom_reply = xcb_get_atom_name_reply ( xcb->connection, anc, &err );
|
|
|
|
|
if ( err != NULL ) {
|
|
|
|
|
g_warning ( "Could not get RandR monitor name: X11 error code %d\n", err->error_code );
|
|
|
|
|
free ( err );
|
2018-02-07 15:06:16 +00:00
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
workarea *retv = g_malloc0 ( sizeof ( workarea ) );
|
|
|
|
|
|
|
|
|
|
// Is primary monitor.
|
|
|
|
|
retv->primary = mon->primary;
|
|
|
|
|
|
|
|
|
|
// Position and size.
|
2018-08-08 13:55:13 +00:00
|
|
|
|
retv->x = mon->x;
|
|
|
|
|
retv->y = mon->y;
|
|
|
|
|
retv->w = mon->width;
|
|
|
|
|
retv->h = mon->height;
|
2018-02-07 15:06:16 +00:00
|
|
|
|
|
|
|
|
|
// Physical
|
|
|
|
|
retv->mw = mon->width_in_millimeters;
|
|
|
|
|
retv->mh = mon->height_in_millimeters;
|
|
|
|
|
|
|
|
|
|
// Name
|
2018-08-08 13:55:13 +00:00
|
|
|
|
retv->name = g_strdup_printf ( "%.*s", xcb_get_atom_name_name_length ( atom_reply ), xcb_get_atom_name_name ( atom_reply ) );
|
2018-02-07 15:06:16 +00:00
|
|
|
|
|
|
|
|
|
// Free name atom.
|
|
|
|
|
free ( atom_reply );
|
|
|
|
|
|
|
|
|
|
return retv;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
2016-08-31 08:06:42 +00:00
|
|
|
|
static int x11_is_extension_present ( const char *extension )
|
|
|
|
|
{
|
|
|
|
|
xcb_query_extension_cookie_t randr_cookie = xcb_query_extension ( xcb->connection, strlen ( extension ), extension );
|
2016-08-31 07:10:30 +00:00
|
|
|
|
|
2016-08-31 08:06:42 +00:00
|
|
|
|
xcb_query_extension_reply_t *randr_reply = xcb_query_extension_reply ( xcb->connection, randr_cookie, NULL );
|
2016-08-31 07:10:30 +00:00
|
|
|
|
|
2016-08-31 08:06:42 +00:00
|
|
|
|
int present = randr_reply->present;
|
2016-08-31 07:26:47 +00:00
|
|
|
|
|
|
|
|
|
free ( randr_reply );
|
|
|
|
|
|
2016-08-31 07:44:43 +00:00
|
|
|
|
return present;
|
2016-08-31 07:10:30 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-08-31 08:06:42 +00:00
|
|
|
|
static void x11_build_monitor_layout_xinerama ()
|
|
|
|
|
{
|
2016-08-31 07:10:30 +00:00
|
|
|
|
xcb_xinerama_query_screens_cookie_t screens_cookie = xcb_xinerama_query_screens_unchecked (
|
2018-08-08 13:55:13 +00:00
|
|
|
|
xcb->connection
|
|
|
|
|
);
|
2016-08-31 07:10:30 +00:00
|
|
|
|
|
|
|
|
|
xcb_xinerama_query_screens_reply_t *screens_reply = xcb_xinerama_query_screens_reply (
|
2018-08-08 13:55:13 +00:00
|
|
|
|
xcb->connection,
|
|
|
|
|
screens_cookie,
|
|
|
|
|
NULL
|
|
|
|
|
);
|
2016-08-31 07:10:30 +00:00
|
|
|
|
|
|
|
|
|
xcb_xinerama_screen_info_iterator_t screens_iterator = xcb_xinerama_query_screens_screen_info_iterator (
|
2018-08-08 13:55:13 +00:00
|
|
|
|
screens_reply
|
|
|
|
|
);
|
2016-08-31 07:10:30 +00:00
|
|
|
|
|
2016-08-31 08:06:42 +00:00
|
|
|
|
for (; screens_iterator.rem > 0; xcb_xinerama_screen_info_next ( &screens_iterator ) ) {
|
2016-08-31 07:10:30 +00:00
|
|
|
|
workarea *w = g_malloc0 ( sizeof ( workarea ) );
|
|
|
|
|
|
|
|
|
|
w->x = screens_iterator.data->x_org;
|
|
|
|
|
w->y = screens_iterator.data->y_org;
|
|
|
|
|
w->w = screens_iterator.data->width;
|
|
|
|
|
w->h = screens_iterator.data->height;
|
|
|
|
|
|
2016-09-02 07:32:32 +00:00
|
|
|
|
w->next = xcb->monitors;
|
|
|
|
|
xcb->monitors = w;
|
2016-08-31 07:10:30 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int index = 0;
|
|
|
|
|
for ( workarea *iter = xcb->monitors; iter; iter = iter->next ) {
|
|
|
|
|
iter->monitor_id = index++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
free ( screens_reply );
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-01 12:55:17 +00:00
|
|
|
|
static void x11_build_monitor_layout ()
|
2016-08-19 08:46:38 +00:00
|
|
|
|
{
|
|
|
|
|
if ( xcb->monitors ) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2016-08-31 07:44:43 +00:00
|
|
|
|
// If RANDR is not available, try Xinerama
|
|
|
|
|
if ( !x11_is_extension_present ( "RANDR" ) ) {
|
|
|
|
|
// Check if xinerama is available.
|
|
|
|
|
if ( x11_is_extension_present ( "XINERAMA" ) ) {
|
2017-04-15 09:37:50 +00:00
|
|
|
|
g_debug ( "Query XINERAMA for monitor layout." );
|
2016-08-31 07:44:43 +00:00
|
|
|
|
x11_build_monitor_layout_xinerama ();
|
2016-08-31 08:06:42 +00:00
|
|
|
|
return;
|
2016-08-31 07:44:43 +00:00
|
|
|
|
}
|
2017-04-15 09:37:50 +00:00
|
|
|
|
g_debug ( "No RANDR or Xinerama available for getting monitor layout." );
|
2016-08-31 07:44:43 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
2017-04-15 09:37:50 +00:00
|
|
|
|
g_debug ( "Query RANDR for monitor layout." );
|
2016-08-31 07:44:43 +00:00
|
|
|
|
|
2018-02-07 15:06:16 +00:00
|
|
|
|
g_debug ( "Randr XCB api version: %d.%d.", XCB_RANDR_MAJOR_VERSION, XCB_RANDR_MINOR_VERSION );
|
2018-08-08 13:55:13 +00:00
|
|
|
|
#if ( ( ( XCB_RANDR_MAJOR_VERSION == RANDR_PREF_MAJOR_VERSION ) && ( XCB_RANDR_MINOR_VERSION >= RANDR_PREF_MINOR_VERSION ) ) \
|
|
|
|
|
|| XCB_RANDR_MAJOR_VERSION > RANDR_PREF_MAJOR_VERSION )
|
|
|
|
|
xcb_randr_query_version_cookie_t cversion = xcb_randr_query_version ( xcb->connection,
|
|
|
|
|
RANDR_PREF_MAJOR_VERSION, RANDR_PREF_MINOR_VERSION );
|
|
|
|
|
xcb_randr_query_version_reply_t *rversion = xcb_randr_query_version_reply ( xcb->connection, cversion, NULL );
|
2018-02-07 15:06:16 +00:00
|
|
|
|
if ( rversion ) {
|
|
|
|
|
g_debug ( "Found randr version: %d.%d", rversion->major_version, rversion->minor_version );
|
|
|
|
|
// Check if we are 1.5 and up.
|
2019-02-21 15:03:34 +00:00
|
|
|
|
if ( ( ( rversion->major_version == RANDR_PREF_MAJOR_VERSION ) && ( rversion->minor_version >= RANDR_PREF_MINOR_VERSION ) ) ||
|
|
|
|
|
( rversion->major_version > RANDR_PREF_MAJOR_VERSION ) ) {
|
2018-08-08 13:55:13 +00:00
|
|
|
|
xcb_randr_get_monitors_cookie_t t = xcb_randr_get_monitors ( xcb->connection, xcb->screen->root, 1 );
|
|
|
|
|
xcb_randr_get_monitors_reply_t *mreply = xcb_randr_get_monitors_reply ( xcb->connection, t, NULL );
|
|
|
|
|
if ( mreply ) {
|
2018-02-07 15:06:16 +00:00
|
|
|
|
xcb_randr_monitor_info_iterator_t iter = xcb_randr_get_monitors_monitors_iterator ( mreply );
|
|
|
|
|
while ( iter.rem > 0 ) {
|
|
|
|
|
workarea *w = x11_get_monitor_from_randr_monitor ( iter.data );
|
|
|
|
|
if ( w ) {
|
|
|
|
|
w->next = xcb->monitors;
|
|
|
|
|
xcb->monitors = w;
|
|
|
|
|
}
|
2018-08-08 13:55:13 +00:00
|
|
|
|
xcb_randr_monitor_info_next ( &iter );
|
2018-02-07 15:06:16 +00:00
|
|
|
|
}
|
|
|
|
|
free ( mreply );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
free ( rversion );
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
// If no monitors found.
|
|
|
|
|
if ( xcb->monitors == NULL ) {
|
|
|
|
|
xcb_randr_get_screen_resources_current_reply_t *res_reply;
|
|
|
|
|
xcb_randr_get_screen_resources_current_cookie_t src;
|
|
|
|
|
src = xcb_randr_get_screen_resources_current ( xcb->connection, xcb->screen->root );
|
|
|
|
|
res_reply = xcb_randr_get_screen_resources_current_reply ( xcb->connection, src, NULL );
|
|
|
|
|
if ( !res_reply ) {
|
|
|
|
|
return; //just report error
|
|
|
|
|
}
|
|
|
|
|
int mon_num = xcb_randr_get_screen_resources_current_outputs_length ( res_reply );
|
|
|
|
|
xcb_randr_output_t *ops = xcb_randr_get_screen_resources_current_outputs ( res_reply );
|
|
|
|
|
|
|
|
|
|
// Get primary.
|
|
|
|
|
xcb_randr_get_output_primary_cookie_t pc = xcb_randr_get_output_primary ( xcb->connection, xcb->screen->root );
|
|
|
|
|
xcb_randr_get_output_primary_reply_t *pc_rep = xcb_randr_get_output_primary_reply ( xcb->connection, pc, NULL );
|
|
|
|
|
|
|
|
|
|
for ( int i = mon_num - 1; i >= 0; i-- ) {
|
|
|
|
|
workarea *w = x11_get_monitor_from_output ( ops[i] );
|
|
|
|
|
if ( w ) {
|
|
|
|
|
w->next = xcb->monitors;
|
|
|
|
|
xcb->monitors = w;
|
|
|
|
|
if ( pc_rep && pc_rep->output == ops[i] ) {
|
|
|
|
|
w->primary = TRUE;
|
|
|
|
|
}
|
2016-08-19 08:46:38 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-02-07 15:06:16 +00:00
|
|
|
|
// If exists, free primary output reply.
|
|
|
|
|
if ( pc_rep ) {
|
|
|
|
|
free ( pc_rep );
|
|
|
|
|
}
|
|
|
|
|
free ( res_reply );
|
2016-08-19 08:46:38 +00:00
|
|
|
|
}
|
2018-02-07 15:06:16 +00:00
|
|
|
|
|
2016-08-19 08:46:38 +00:00
|
|
|
|
// Number monitor
|
|
|
|
|
int index = 0;
|
|
|
|
|
for ( workarea *iter = xcb->monitors; iter; iter = iter->next ) {
|
|
|
|
|
iter->monitor_id = index++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-01 12:55:17 +00:00
|
|
|
|
void display_dump_monitor_layout ( void )
|
2016-08-19 08:46:38 +00:00
|
|
|
|
{
|
|
|
|
|
int is_term = isatty ( fileno ( stdout ) );
|
|
|
|
|
printf ( "Monitor layout:\n" );
|
|
|
|
|
for ( workarea *iter = xcb->monitors; iter; iter = iter->next ) {
|
|
|
|
|
printf ( "%s ID%s: %d", ( is_term ) ? color_bold : "", is_term ? color_reset : "", iter->monitor_id );
|
|
|
|
|
if ( iter->primary ) {
|
|
|
|
|
printf ( " (primary)" );
|
|
|
|
|
}
|
|
|
|
|
printf ( "\n" );
|
|
|
|
|
printf ( "%s name%s: %s\n", ( is_term ) ? color_bold : "", is_term ? color_reset : "", iter->name );
|
|
|
|
|
printf ( "%s position%s: %d,%d\n", ( is_term ) ? color_bold : "", is_term ? color_reset : "", iter->x, iter->y );
|
|
|
|
|
printf ( "%s size%s: %d,%d\n", ( is_term ) ? color_bold : "", is_term ? color_reset : "", iter->w, iter->h );
|
2017-01-21 11:58:52 +00:00
|
|
|
|
if ( iter->mw > 0 && iter->mh > 0 ) {
|
|
|
|
|
printf ( "%s size%s: %dmm,%dmm dpi: %.0f,%.0f\n",
|
2018-08-08 13:55:13 +00:00
|
|
|
|
( is_term ) ? color_bold : "",
|
|
|
|
|
is_term ? color_reset : "",
|
|
|
|
|
iter->mw,
|
|
|
|
|
iter->mh,
|
|
|
|
|
iter->w * 25.4 / (double) iter->mw,
|
|
|
|
|
iter->h * 25.4 / (double) iter->mh
|
|
|
|
|
);
|
2017-01-21 11:58:52 +00:00
|
|
|
|
}
|
2016-08-19 08:46:38 +00:00
|
|
|
|
printf ( "\n" );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-02 12:54:21 +00:00
|
|
|
|
void display_startup_notification ( RofiHelperExecuteContext *context, GSpawnChildSetupFunc *child_setup, gpointer *user_data )
|
|
|
|
|
{
|
2017-06-02 12:57:51 +00:00
|
|
|
|
if ( context == NULL ) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SnLauncherContext *sncontext;
|
|
|
|
|
|
|
|
|
|
sncontext = sn_launcher_context_new ( xcb->sndisplay, xcb->screen_nbr );
|
|
|
|
|
|
|
|
|
|
sn_launcher_context_set_name ( sncontext, context->name );
|
|
|
|
|
sn_launcher_context_set_description ( sncontext, context->description );
|
|
|
|
|
if ( context->binary != NULL ) {
|
|
|
|
|
sn_launcher_context_set_binary_name ( sncontext, context->binary );
|
|
|
|
|
}
|
|
|
|
|
if ( context->icon != NULL ) {
|
|
|
|
|
sn_launcher_context_set_icon_name ( sncontext, context->icon );
|
|
|
|
|
}
|
|
|
|
|
if ( context->app_id != NULL ) {
|
|
|
|
|
sn_launcher_context_set_application_id ( sncontext, context->app_id );
|
|
|
|
|
}
|
|
|
|
|
if ( context->wmclass != NULL ) {
|
|
|
|
|
sn_launcher_context_set_wmclass ( sncontext, context->wmclass );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
xcb_get_property_cookie_t c;
|
|
|
|
|
unsigned int current_desktop = 0;
|
|
|
|
|
|
|
|
|
|
c = xcb_ewmh_get_current_desktop ( &xcb->ewmh, xcb->screen_nbr );
|
|
|
|
|
if ( xcb_ewmh_get_current_desktop_reply ( &xcb->ewmh, c, ¤t_desktop, NULL ) ) {
|
|
|
|
|
sn_launcher_context_set_workspace ( sncontext, current_desktop );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sn_launcher_context_initiate ( sncontext, "rofi", context->command, xcb->last_timestamp );
|
|
|
|
|
|
|
|
|
|
*child_setup = (GSpawnChildSetupFunc) sn_launcher_context_setup_child_process;
|
|
|
|
|
*user_data = sncontext;
|
2017-06-02 12:54:21 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-10-14 14:46:54 +00:00
|
|
|
|
static int monitor_get_dimension ( int monitor_id, workarea *mon )
|
2015-08-02 13:45:52 +00:00
|
|
|
|
{
|
|
|
|
|
memset ( mon, 0, sizeof ( workarea ) );
|
2016-03-01 17:11:55 +00:00
|
|
|
|
mon->w = xcb->screen->width_in_pixels;
|
|
|
|
|
mon->h = xcb->screen->height_in_pixels;
|
2015-08-02 13:45:52 +00:00
|
|
|
|
|
2016-08-19 08:46:38 +00:00
|
|
|
|
workarea *iter = NULL;
|
|
|
|
|
for ( iter = xcb->monitors; iter; iter = iter->next ) {
|
|
|
|
|
if ( iter->monitor_id == monitor_id ) {
|
|
|
|
|
*mon = *iter;
|
|
|
|
|
return TRUE;
|
|
|
|
|
}
|
2015-08-02 13:45:52 +00:00
|
|
|
|
}
|
|
|
|
|
return FALSE;
|
|
|
|
|
}
|
2015-02-09 18:35:51 +00:00
|
|
|
|
// find the dimensions of the monitor displaying point x,y
|
2016-10-14 14:46:54 +00:00
|
|
|
|
static void monitor_dimensions ( int x, int y, workarea *mon )
|
2015-02-09 18:35:51 +00:00
|
|
|
|
{
|
|
|
|
|
memset ( mon, 0, sizeof ( workarea ) );
|
2016-03-01 17:11:55 +00:00
|
|
|
|
mon->w = xcb->screen->width_in_pixels;
|
|
|
|
|
mon->h = xcb->screen->height_in_pixels;
|
2015-02-09 18:35:51 +00:00
|
|
|
|
|
2016-08-19 08:46:38 +00:00
|
|
|
|
for ( workarea *iter = xcb->monitors; iter; iter = iter->next ) {
|
2016-10-25 20:45:11 +00:00
|
|
|
|
if ( INTERSECT ( x, y, iter->x, iter->y, iter->w, iter->h ) ) {
|
2016-08-19 08:46:38 +00:00
|
|
|
|
*mon = *iter;
|
2016-02-27 19:45:47 +00:00
|
|
|
|
break;
|
2015-02-09 18:35:51 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2016-07-29 06:32:34 +00:00
|
|
|
|
* @param root The X11 window used to find the pointer position. Usually the root window.
|
|
|
|
|
* @param x The x position of the mouse [out]
|
|
|
|
|
* @param y The y position of the mouse [out]
|
2015-02-09 18:35:51 +00:00
|
|
|
|
*
|
|
|
|
|
* find mouse pointer location
|
|
|
|
|
*
|
2016-07-29 06:32:34 +00:00
|
|
|
|
* @returns TRUE when found, FALSE otherwise
|
2015-02-09 18:35:51 +00:00
|
|
|
|
*/
|
2016-03-01 17:11:55 +00:00
|
|
|
|
static int pointer_get ( xcb_window_t root, int *x, int *y )
|
2015-02-09 18:35:51 +00:00
|
|
|
|
{
|
|
|
|
|
*x = 0;
|
|
|
|
|
*y = 0;
|
2016-03-01 17:11:55 +00:00
|
|
|
|
xcb_query_pointer_cookie_t c = xcb_query_pointer ( xcb->connection, root );
|
|
|
|
|
xcb_query_pointer_reply_t *r = xcb_query_pointer_reply ( xcb->connection, c, NULL );
|
2016-02-27 18:51:06 +00:00
|
|
|
|
if ( r ) {
|
|
|
|
|
*x = r->root_x;
|
|
|
|
|
*y = r->root_y;
|
2016-02-28 14:32:53 +00:00
|
|
|
|
free ( r );
|
2016-07-29 06:32:34 +00:00
|
|
|
|
return TRUE;
|
2015-02-09 18:35:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-07-29 06:32:34 +00:00
|
|
|
|
return FALSE;
|
2015-02-09 18:35:51 +00:00
|
|
|
|
}
|
2017-07-12 10:27:45 +00:00
|
|
|
|
static int monitor_active_from_winid ( xcb_drawable_t id, workarea *mon )
|
|
|
|
|
{
|
|
|
|
|
xcb_window_t root = xcb->screen->root;
|
|
|
|
|
xcb_get_geometry_cookie_t c = xcb_get_geometry ( xcb->connection, id );
|
|
|
|
|
xcb_get_geometry_reply_t *r = xcb_get_geometry_reply ( xcb->connection, c, NULL );
|
|
|
|
|
if ( r ) {
|
|
|
|
|
xcb_translate_coordinates_cookie_t ct = xcb_translate_coordinates ( xcb->connection, id, root, r->x, r->y );
|
|
|
|
|
xcb_translate_coordinates_reply_t *t = xcb_translate_coordinates_reply ( xcb->connection, ct, NULL );
|
|
|
|
|
if ( t ) {
|
|
|
|
|
// place the menu above the window
|
|
|
|
|
// if some window is focused, place menu above window, else fall
|
|
|
|
|
// back to selected monitor.
|
|
|
|
|
mon->x = t->dst_x - r->x;
|
|
|
|
|
mon->y = t->dst_y - r->y;
|
|
|
|
|
mon->w = r->width;
|
|
|
|
|
mon->h = r->height;
|
|
|
|
|
free ( r );
|
|
|
|
|
free ( t );
|
|
|
|
|
return TRUE;
|
|
|
|
|
}
|
|
|
|
|
free ( r );
|
|
|
|
|
}
|
|
|
|
|
return FALSE;
|
|
|
|
|
}
|
2017-11-02 20:06:28 +00:00
|
|
|
|
static int monitor_active_from_id_focused ( int mon_id, workarea *mon )
|
|
|
|
|
{
|
2017-11-07 19:04:07 +00:00
|
|
|
|
int retv = FALSE;
|
2017-11-02 20:06:28 +00:00
|
|
|
|
xcb_window_t active_window;
|
|
|
|
|
xcb_get_property_cookie_t awc;
|
|
|
|
|
awc = xcb_ewmh_get_active_window ( &xcb->ewmh, xcb->screen_nbr );
|
2017-11-07 19:04:07 +00:00
|
|
|
|
if ( !xcb_ewmh_get_active_window_reply ( &xcb->ewmh, awc, &active_window, NULL ) ) {
|
2017-11-02 20:06:28 +00:00
|
|
|
|
g_debug ( "Failed to get active window, falling back to mouse location (-5)." );
|
|
|
|
|
return retv;
|
|
|
|
|
}
|
|
|
|
|
xcb_query_tree_cookie_t tree_cookie = xcb_query_tree ( xcb->connection, active_window );
|
2017-11-07 19:04:07 +00:00
|
|
|
|
xcb_query_tree_reply_t *tree_reply = xcb_query_tree_reply ( xcb->connection, tree_cookie, NULL );
|
2017-11-02 20:06:28 +00:00
|
|
|
|
if ( !tree_reply ) {
|
|
|
|
|
g_debug ( "Failed to get parent window, falling back to mouse location (-5)." );
|
|
|
|
|
return retv;
|
|
|
|
|
}
|
|
|
|
|
// get geometry.
|
|
|
|
|
xcb_get_geometry_cookie_t c = xcb_get_geometry ( xcb->connection, active_window );
|
|
|
|
|
xcb_get_geometry_reply_t *r = xcb_get_geometry_reply ( xcb->connection, c, NULL );
|
|
|
|
|
if ( !r ) {
|
|
|
|
|
g_debug ( "Failed to get geometry of active window, falling back to mouse location (-5)." );
|
|
|
|
|
free ( tree_reply );
|
|
|
|
|
return retv;
|
|
|
|
|
}
|
|
|
|
|
xcb_translate_coordinates_cookie_t ct = xcb_translate_coordinates ( xcb->connection, tree_reply->parent, r->root, r->x, r->y );
|
|
|
|
|
xcb_translate_coordinates_reply_t *t = xcb_translate_coordinates_reply ( xcb->connection, ct, NULL );
|
|
|
|
|
if ( t ) {
|
|
|
|
|
if ( mon_id == -2 ) {
|
|
|
|
|
// place the menu above the window
|
|
|
|
|
// if some window is focused, place menu above window, else fall
|
|
|
|
|
// back to selected monitor.
|
|
|
|
|
mon->x = t->dst_x - r->x;
|
|
|
|
|
mon->y = t->dst_y - r->y;
|
|
|
|
|
mon->w = r->width;
|
|
|
|
|
mon->h = r->height;
|
2017-11-07 19:04:07 +00:00
|
|
|
|
retv = TRUE;
|
2017-11-02 20:06:28 +00:00
|
|
|
|
}
|
|
|
|
|
else if ( mon_id == -4 ) {
|
|
|
|
|
monitor_dimensions ( t->dst_x, t->dst_y, mon );
|
|
|
|
|
retv = TRUE;
|
|
|
|
|
}
|
|
|
|
|
free ( t );
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
g_debug ( "Failed to get translate position of active window, falling back to mouse location (-5)." );
|
|
|
|
|
}
|
|
|
|
|
free ( r );
|
|
|
|
|
free ( tree_reply );
|
|
|
|
|
return retv;
|
|
|
|
|
}
|
2016-10-13 16:09:03 +00:00
|
|
|
|
static int monitor_active_from_id ( int mon_id, workarea *mon )
|
2015-02-09 18:35:51 +00:00
|
|
|
|
{
|
2016-03-01 17:11:55 +00:00
|
|
|
|
xcb_window_t root = xcb->screen->root;
|
2016-02-28 14:32:53 +00:00
|
|
|
|
int x, y;
|
2016-10-13 16:09:03 +00:00
|
|
|
|
// At mouse position.
|
|
|
|
|
if ( mon_id == -3 ) {
|
|
|
|
|
if ( pointer_get ( root, &x, &y ) ) {
|
|
|
|
|
monitor_dimensions ( x, y, mon );
|
|
|
|
|
mon->x = x;
|
|
|
|
|
mon->y = y;
|
|
|
|
|
return TRUE;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// Focused monitor
|
|
|
|
|
else if ( mon_id == -1 ) {
|
|
|
|
|
// Get the current desktop.
|
|
|
|
|
unsigned int current_desktop = 0;
|
|
|
|
|
xcb_get_property_cookie_t gcdc;
|
|
|
|
|
gcdc = xcb_ewmh_get_current_desktop ( &xcb->ewmh, xcb->screen_nbr );
|
|
|
|
|
if ( xcb_ewmh_get_current_desktop_reply ( &xcb->ewmh, gcdc, ¤t_desktop, NULL ) ) {
|
|
|
|
|
xcb_get_property_cookie_t c = xcb_ewmh_get_desktop_viewport ( &xcb->ewmh, xcb->screen_nbr );
|
|
|
|
|
xcb_ewmh_get_desktop_viewport_reply_t vp;
|
|
|
|
|
if ( xcb_ewmh_get_desktop_viewport_reply ( &xcb->ewmh, c, &vp, NULL ) ) {
|
|
|
|
|
if ( current_desktop < vp.desktop_viewport_len ) {
|
|
|
|
|
monitor_dimensions ( vp.desktop_viewport[current_desktop].x,
|
2018-08-08 13:55:13 +00:00
|
|
|
|
vp.desktop_viewport[current_desktop].y, mon );
|
2016-10-13 16:09:03 +00:00
|
|
|
|
xcb_ewmh_get_desktop_viewport_reply_wipe ( &vp );
|
|
|
|
|
return TRUE;
|
|
|
|
|
}
|
2017-06-14 06:26:53 +00:00
|
|
|
|
else {
|
|
|
|
|
g_debug ( "Viewport does not exist for current desktop: %d, falling back to mouse location (-5)", current_desktop );
|
|
|
|
|
}
|
2016-10-13 16:09:03 +00:00
|
|
|
|
xcb_ewmh_get_desktop_viewport_reply_wipe ( &vp );
|
|
|
|
|
}
|
2017-06-14 06:26:53 +00:00
|
|
|
|
else {
|
|
|
|
|
g_debug ( "Failed to get viewport for current desktop: %d, falling back to mouse location (-5).", current_desktop );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
g_debug ( "Failed to get current desktop, falling back to mouse location (-5)." );
|
2016-10-13 16:09:03 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if ( mon_id == -2 || mon_id == -4 ) {
|
2017-11-07 19:04:07 +00:00
|
|
|
|
if ( monitor_active_from_id_focused ( mon_id, mon ) ) {
|
2017-11-02 20:06:28 +00:00
|
|
|
|
return TRUE;
|
2016-10-13 16:09:03 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// Monitor that has mouse pointer.
|
|
|
|
|
else if ( mon_id == -5 ) {
|
|
|
|
|
if ( pointer_get ( root, &x, &y ) ) {
|
|
|
|
|
monitor_dimensions ( x, y, mon );
|
|
|
|
|
return TRUE;
|
|
|
|
|
}
|
|
|
|
|
// This is our give up point.
|
|
|
|
|
return FALSE;
|
|
|
|
|
}
|
2017-04-15 09:37:50 +00:00
|
|
|
|
g_debug ( "Failed to find monitor, fall back to monitor showing mouse." );
|
2016-10-13 16:09:03 +00:00
|
|
|
|
return monitor_active_from_id ( -5, mon );
|
|
|
|
|
}
|
2015-02-09 18:35:51 +00:00
|
|
|
|
|
2016-10-13 16:09:03 +00:00
|
|
|
|
// determine which monitor holds the active window, or failing that the mouse pointer
|
|
|
|
|
int monitor_active ( workarea *mon )
|
|
|
|
|
{
|
2016-08-19 08:46:38 +00:00
|
|
|
|
if ( config.monitor != NULL ) {
|
|
|
|
|
for ( workarea *iter = xcb->monitors; iter; iter = iter->next ) {
|
|
|
|
|
if ( g_strcmp0 ( config.monitor, iter->name ) == 0 ) {
|
|
|
|
|
*mon = *iter;
|
|
|
|
|
return TRUE;
|
2016-02-09 20:25:29 +00:00
|
|
|
|
}
|
2016-02-28 14:32:53 +00:00
|
|
|
|
}
|
2016-02-09 20:25:29 +00:00
|
|
|
|
}
|
2016-08-19 08:46:38 +00:00
|
|
|
|
// Grab primary.
|
|
|
|
|
if ( g_strcmp0 ( config.monitor, "primary" ) == 0 ) {
|
|
|
|
|
for ( workarea *iter = xcb->monitors; iter; iter = iter->next ) {
|
|
|
|
|
if ( iter->primary ) {
|
|
|
|
|
*mon = *iter;
|
|
|
|
|
return TRUE;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-07-12 10:27:45 +00:00
|
|
|
|
if ( g_str_has_prefix ( config.monitor, "wid:" ) ) {
|
|
|
|
|
char *end = NULL;
|
|
|
|
|
xcb_drawable_t win = g_ascii_strtoll ( config.monitor + 4, &end, 0 );
|
|
|
|
|
if ( end != config.monitor ) {
|
|
|
|
|
if ( monitor_active_from_winid ( win, mon ) ) {
|
2016-08-19 08:46:38 +00:00
|
|
|
|
return TRUE;
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-07-12 10:27:45 +00:00
|
|
|
|
}
|
|
|
|
|
{
|
|
|
|
|
// IF fail, fall back to classic mode.
|
|
|
|
|
char *end = NULL;
|
|
|
|
|
gint64 mon_id = g_ascii_strtoll ( config.monitor, &end, 0 );
|
|
|
|
|
if ( end != config.monitor ) {
|
|
|
|
|
if ( mon_id >= 0 ) {
|
|
|
|
|
if ( monitor_get_dimension ( mon_id, mon ) ) {
|
|
|
|
|
return TRUE;
|
|
|
|
|
}
|
|
|
|
|
g_warning ( "Failed to find selected monitor." );
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
return monitor_active_from_id ( mon_id, mon );
|
|
|
|
|
}
|
2015-02-09 18:35:51 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2016-08-19 08:46:38 +00:00
|
|
|
|
// Fallback.
|
2016-03-01 17:11:55 +00:00
|
|
|
|
monitor_dimensions ( 0, 0, mon );
|
2016-08-19 08:46:38 +00:00
|
|
|
|
return FALSE;
|
2015-02-09 18:35:51 +00:00
|
|
|
|
}
|
2017-06-01 09:50:26 +00:00
|
|
|
|
|
2017-06-01 11:30:29 +00:00
|
|
|
|
/**
|
|
|
|
|
* @param state Internal state of the menu.
|
|
|
|
|
* @param xse X selection event.
|
|
|
|
|
*
|
|
|
|
|
* Handle paste event.
|
|
|
|
|
*/
|
|
|
|
|
static void rofi_view_paste ( RofiViewState *state, xcb_selection_notify_event_t *xse )
|
|
|
|
|
{
|
|
|
|
|
if ( xse->property == XCB_ATOM_NONE ) {
|
|
|
|
|
g_warning ( "Failed to convert selection" );
|
|
|
|
|
}
|
|
|
|
|
else if ( xse->property == xcb->ewmh.UTF8_STRING ) {
|
|
|
|
|
gchar *text = window_get_text_prop ( xse->requestor, xcb->ewmh.UTF8_STRING );
|
|
|
|
|
if ( text != NULL && text[0] != '\0' ) {
|
|
|
|
|
unsigned int dl = strlen ( text );
|
|
|
|
|
// Strip new line
|
|
|
|
|
for ( unsigned int i = 0; i < dl; i++ ) {
|
|
|
|
|
if ( text[i] == '\n' ) {
|
|
|
|
|
text[i] = '\0';
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
rofi_view_handle_text ( state, text );
|
|
|
|
|
}
|
|
|
|
|
g_free ( text );
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
g_warning ( "Failed" );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-26 09:10:40 +00:00
|
|
|
|
static gboolean x11_button_to_nk_bindings_button ( guint32 x11_button, NkBindingsMouseButton *button )
|
|
|
|
|
{
|
|
|
|
|
switch ( x11_button )
|
|
|
|
|
{
|
2018-08-08 13:55:13 +00:00
|
|
|
|
case 1:
|
|
|
|
|
*button = NK_BINDINGS_MOUSE_BUTTON_PRIMARY;
|
|
|
|
|
break;
|
|
|
|
|
case 3:
|
|
|
|
|
*button = NK_BINDINGS_MOUSE_BUTTON_SECONDARY;
|
|
|
|
|
break;
|
|
|
|
|
case 2:
|
|
|
|
|
*button = NK_BINDINGS_MOUSE_BUTTON_MIDDLE;
|
|
|
|
|
break;
|
|
|
|
|
case 8:
|
|
|
|
|
*button = NK_BINDINGS_MOUSE_BUTTON_BACK;
|
|
|
|
|
break;
|
|
|
|
|
case 9:
|
|
|
|
|
*button = NK_BINDINGS_MOUSE_BUTTON_FORWARD;
|
|
|
|
|
break;
|
|
|
|
|
case 4:
|
|
|
|
|
case 5:
|
|
|
|
|
case 6:
|
|
|
|
|
case 7:
|
|
|
|
|
return FALSE;
|
|
|
|
|
default:
|
|
|
|
|
*button = NK_BINDINGS_MOUSE_BUTTON_EXTRA + x11_button;
|
2017-12-26 09:10:40 +00:00
|
|
|
|
}
|
|
|
|
|
return TRUE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static gboolean x11_button_to_nk_bindings_scroll ( guint32 x11_button, NkBindingsScrollAxis *axis, gint32 *steps )
|
|
|
|
|
{
|
|
|
|
|
*steps = 1;
|
|
|
|
|
switch ( x11_button )
|
|
|
|
|
{
|
2018-08-08 13:55:13 +00:00
|
|
|
|
case 4:
|
|
|
|
|
*steps = -1;
|
|
|
|
|
/* fallthrough */
|
|
|
|
|
case 5:
|
|
|
|
|
*axis = NK_BINDINGS_SCROLL_AXIS_VERTICAL;
|
|
|
|
|
break;
|
|
|
|
|
case 6:
|
|
|
|
|
*steps = -1;
|
|
|
|
|
/* fallthrough */
|
|
|
|
|
case 7:
|
|
|
|
|
*axis = NK_BINDINGS_SCROLL_AXIS_HORIZONTAL;
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
return FALSE;
|
2017-12-26 09:10:40 +00:00
|
|
|
|
}
|
|
|
|
|
return TRUE;
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-01 09:50:26 +00:00
|
|
|
|
/**
|
|
|
|
|
* Process X11 events in the main-loop (gui-thread) of the application.
|
|
|
|
|
*/
|
2017-06-01 11:30:29 +00:00
|
|
|
|
static void main_loop_x11_event_handler_view ( xcb_generic_event_t *event )
|
2017-06-01 09:50:26 +00:00
|
|
|
|
{
|
|
|
|
|
RofiViewState *state = rofi_view_get_active ();
|
2017-06-01 11:30:29 +00:00
|
|
|
|
if ( state == NULL ) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
switch ( event->response_type & ~0x80 )
|
|
|
|
|
{
|
2020-03-26 12:45:20 +00:00
|
|
|
|
case XCB_CLIENT_MESSAGE:
|
|
|
|
|
{
|
|
|
|
|
xcb_client_message_event_t *cme = (xcb_client_message_event_t *) event;
|
|
|
|
|
xcb_atom_t atom = cme->data.data32[0];
|
|
|
|
|
xcb_timestamp_t time = cme->data.data32[1];
|
|
|
|
|
if ( atom == netatoms[WM_TAKE_FOCUS] )
|
|
|
|
|
{
|
|
|
|
|
xcb_set_input_focus ( xcb->connection, XCB_INPUT_FOCUS_NONE, cme->window, time );
|
|
|
|
|
xcb_flush ( xcb->connection );
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
2018-08-08 13:55:13 +00:00
|
|
|
|
case XCB_EXPOSE:
|
|
|
|
|
rofi_view_frame_callback ();
|
|
|
|
|
break;
|
|
|
|
|
case XCB_CONFIGURE_NOTIFY:
|
|
|
|
|
{
|
|
|
|
|
xcb_configure_notify_event_t *xce = (xcb_configure_notify_event_t *) event;
|
|
|
|
|
rofi_view_temp_configure_notify ( state, xce );
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case XCB_MOTION_NOTIFY:
|
|
|
|
|
{
|
|
|
|
|
if ( config.click_to_exit == TRUE ) {
|
|
|
|
|
xcb->mouse_seen = TRUE;
|
|
|
|
|
}
|
|
|
|
|
xcb_motion_notify_event_t *xme = (xcb_motion_notify_event_t *) event;
|
|
|
|
|
rofi_view_handle_mouse_motion ( state, xme->event_x, xme->event_y );
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case XCB_BUTTON_PRESS:
|
|
|
|
|
{
|
|
|
|
|
xcb_button_press_event_t *bpe = (xcb_button_press_event_t *) event;
|
|
|
|
|
NkBindingsMouseButton button;
|
|
|
|
|
NkBindingsScrollAxis axis;
|
|
|
|
|
gint32 steps;
|
|
|
|
|
|
|
|
|
|
xcb->last_timestamp = bpe->time;
|
|
|
|
|
rofi_view_handle_mouse_motion ( state, bpe->event_x, bpe->event_y );
|
|
|
|
|
if ( x11_button_to_nk_bindings_button ( bpe->detail, &button ) ) {
|
|
|
|
|
nk_bindings_seat_handle_button ( xcb->bindings_seat, NULL, button, NK_BINDINGS_BUTTON_STATE_PRESS, bpe->time );
|
|
|
|
|
}
|
|
|
|
|
else if ( x11_button_to_nk_bindings_scroll ( bpe->detail, &axis, &steps ) ) {
|
|
|
|
|
nk_bindings_seat_handle_scroll ( xcb->bindings_seat, NULL, axis, steps );
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case XCB_BUTTON_RELEASE:
|
|
|
|
|
{
|
|
|
|
|
xcb_button_release_event_t *bre = (xcb_button_release_event_t *) event;
|
|
|
|
|
NkBindingsMouseButton button;
|
|
|
|
|
|
|
|
|
|
xcb->last_timestamp = bre->time;
|
|
|
|
|
if ( x11_button_to_nk_bindings_button ( bre->detail, &button ) ) {
|
|
|
|
|
nk_bindings_seat_handle_button ( xcb->bindings_seat, NULL, button, NK_BINDINGS_BUTTON_STATE_RELEASE, bre->time );
|
|
|
|
|
}
|
|
|
|
|
if ( config.click_to_exit == TRUE ) {
|
|
|
|
|
if ( !xcb->mouse_seen ) {
|
|
|
|
|
rofi_view_temp_click_to_exit ( state, bre->event );
|
2018-02-07 15:06:16 +00:00
|
|
|
|
}
|
2018-08-08 13:55:13 +00:00
|
|
|
|
xcb->mouse_seen = FALSE;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
// Paste event.
|
|
|
|
|
case XCB_SELECTION_NOTIFY:
|
|
|
|
|
rofi_view_paste ( state, (xcb_selection_notify_event_t *) event );
|
|
|
|
|
break;
|
|
|
|
|
case XCB_KEYMAP_NOTIFY:
|
|
|
|
|
{
|
|
|
|
|
xcb_keymap_notify_event_t *kne = (xcb_keymap_notify_event_t *) event;
|
|
|
|
|
for ( gint32 by = 0; by < 31; ++by ) {
|
|
|
|
|
for ( gint8 bi = 0; bi < 7; ++bi ) {
|
|
|
|
|
if ( kne->keys[by] & ( 1 << bi ) ) {
|
|
|
|
|
// X11 keycodes starts at 8
|
|
|
|
|
nk_bindings_seat_handle_key ( xcb->bindings_seat, NULL, ( 8 * by + bi ) + 8, NK_BINDINGS_KEY_STATE_PRESSED );
|
2018-02-07 15:06:16 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-08-08 13:55:13 +00:00
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case XCB_KEY_PRESS:
|
|
|
|
|
{
|
|
|
|
|
xcb_key_press_event_t *xkpe = (xcb_key_press_event_t *) event;
|
|
|
|
|
gchar *text;
|
|
|
|
|
|
|
|
|
|
xcb->last_timestamp = xkpe->time;
|
|
|
|
|
text = nk_bindings_seat_handle_key_with_modmask ( xcb->bindings_seat, NULL, xkpe->state, xkpe->detail, NK_BINDINGS_KEY_STATE_PRESS );
|
|
|
|
|
if ( text != NULL ) {
|
|
|
|
|
rofi_view_handle_text ( state, text );
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case XCB_KEY_RELEASE:
|
|
|
|
|
{
|
|
|
|
|
xcb_key_release_event_t *xkre = (xcb_key_release_event_t *) event;
|
|
|
|
|
xcb->last_timestamp = xkre->time;
|
|
|
|
|
nk_bindings_seat_handle_key ( xcb->bindings_seat, NULL, xkre->detail, NK_BINDINGS_KEY_STATE_RELEASE );
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
default:
|
|
|
|
|
break;
|
2017-06-01 09:50:26 +00:00
|
|
|
|
}
|
2017-06-01 11:30:29 +00:00
|
|
|
|
rofi_view_maybe_update ( state );
|
2017-06-01 09:50:26 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static gboolean main_loop_x11_event_handler ( xcb_generic_event_t *ev, G_GNUC_UNUSED gpointer user_data )
|
|
|
|
|
{
|
|
|
|
|
if ( ev == NULL ) {
|
|
|
|
|
int status = xcb_connection_has_error ( xcb->connection );
|
|
|
|
|
if ( status > 0 ) {
|
|
|
|
|
g_warning ( "The XCB connection to X server had a fatal error: %d", status );
|
|
|
|
|
g_main_loop_quit ( xcb->main_loop );
|
|
|
|
|
return G_SOURCE_REMOVE;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
g_warning ( "main_loop_x11_event_handler: ev == NULL, status == %d", status );
|
|
|
|
|
return G_SOURCE_CONTINUE;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
uint8_t type = ev->response_type & ~0x80;
|
|
|
|
|
if ( type == xcb->xkb.first_event ) {
|
|
|
|
|
switch ( ev->pad0 )
|
|
|
|
|
{
|
2018-08-08 13:55:13 +00:00
|
|
|
|
case XCB_XKB_MAP_NOTIFY:
|
|
|
|
|
{
|
|
|
|
|
struct xkb_keymap *keymap = xkb_x11_keymap_new_from_device ( nk_bindings_seat_get_context ( xcb->bindings_seat ), xcb->connection, xcb->xkb.device_id, 0 );
|
|
|
|
|
struct xkb_state *state = xkb_x11_state_new_from_device ( keymap, xcb->connection, xcb->xkb.device_id );
|
|
|
|
|
nk_bindings_seat_update_keymap ( xcb->bindings_seat, keymap, state );
|
|
|
|
|
xkb_keymap_unref ( keymap );
|
|
|
|
|
xkb_state_unref ( state );
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case XCB_XKB_STATE_NOTIFY:
|
|
|
|
|
{
|
|
|
|
|
xcb_xkb_state_notify_event_t *ksne = (xcb_xkb_state_notify_event_t *) ev;
|
|
|
|
|
nk_bindings_seat_update_mask ( xcb->bindings_seat, NULL,
|
|
|
|
|
ksne->baseMods,
|
|
|
|
|
ksne->latchedMods,
|
|
|
|
|
ksne->lockedMods,
|
|
|
|
|
ksne->baseGroup,
|
|
|
|
|
ksne->latchedGroup,
|
|
|
|
|
ksne->lockedGroup );
|
|
|
|
|
rofi_view_maybe_update ( rofi_view_get_active () );
|
|
|
|
|
break;
|
|
|
|
|
}
|
2017-06-01 09:50:26 +00:00
|
|
|
|
}
|
|
|
|
|
return G_SOURCE_CONTINUE;
|
|
|
|
|
}
|
|
|
|
|
if ( xcb->sndisplay != NULL ) {
|
|
|
|
|
sn_xcb_display_process_event ( xcb->sndisplay, ev );
|
|
|
|
|
}
|
|
|
|
|
main_loop_x11_event_handler_view ( ev );
|
|
|
|
|
return G_SOURCE_CONTINUE;
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-01 10:47:52 +00:00
|
|
|
|
static int take_pointer ( xcb_window_t w, int iters )
|
2016-05-09 06:47:28 +00:00
|
|
|
|
{
|
2017-01-06 10:00:27 +00:00
|
|
|
|
int i = 0;
|
|
|
|
|
while ( TRUE ) {
|
2016-05-09 06:47:28 +00:00
|
|
|
|
if ( xcb_connection_has_error ( xcb->connection ) ) {
|
2017-04-15 09:58:49 +00:00
|
|
|
|
g_warning ( "Connection has error" );
|
2016-05-09 06:47:28 +00:00
|
|
|
|
exit ( EXIT_FAILURE );
|
|
|
|
|
}
|
|
|
|
|
xcb_grab_pointer_cookie_t cc = xcb_grab_pointer ( xcb->connection, 1, w, XCB_EVENT_MASK_BUTTON_RELEASE,
|
2018-08-08 13:55:13 +00:00
|
|
|
|
XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC, w, XCB_NONE, XCB_CURRENT_TIME );
|
2016-05-09 06:47:28 +00:00
|
|
|
|
xcb_grab_pointer_reply_t *r = xcb_grab_pointer_reply ( xcb->connection, cc, NULL );
|
|
|
|
|
if ( r ) {
|
|
|
|
|
if ( r->status == XCB_GRAB_STATUS_SUCCESS ) {
|
|
|
|
|
free ( r );
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
free ( r );
|
|
|
|
|
}
|
2017-01-08 20:36:06 +00:00
|
|
|
|
if ( ( ++i ) > iters ) {
|
2017-01-06 10:00:27 +00:00
|
|
|
|
break;
|
|
|
|
|
}
|
2019-02-02 10:53:33 +00:00
|
|
|
|
struct timespec del = {
|
2020-02-02 12:56:37 +00:00
|
|
|
|
.tv_sec = 0,
|
|
|
|
|
.tv_nsec = 1000000
|
2019-02-02 10:53:33 +00:00
|
|
|
|
};
|
|
|
|
|
nanosleep ( &del, NULL );
|
2016-05-09 06:47:28 +00:00
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2017-06-01 10:47:52 +00:00
|
|
|
|
|
|
|
|
|
static int take_keyboard ( xcb_window_t w, int iters )
|
2015-02-09 18:35:51 +00:00
|
|
|
|
{
|
2017-01-06 10:00:27 +00:00
|
|
|
|
int i = 0;
|
|
|
|
|
while ( TRUE ) {
|
2016-03-04 16:03:24 +00:00
|
|
|
|
if ( xcb_connection_has_error ( xcb->connection ) ) {
|
2017-04-15 09:58:49 +00:00
|
|
|
|
g_warning ( "Connection has error" );
|
2016-03-04 16:03:24 +00:00
|
|
|
|
exit ( EXIT_FAILURE );
|
|
|
|
|
}
|
2016-03-01 17:11:55 +00:00
|
|
|
|
xcb_grab_keyboard_cookie_t cc = xcb_grab_keyboard ( xcb->connection,
|
2018-08-08 13:55:13 +00:00
|
|
|
|
1, w, XCB_CURRENT_TIME, XCB_GRAB_MODE_ASYNC,
|
|
|
|
|
XCB_GRAB_MODE_ASYNC );
|
2016-03-01 17:11:55 +00:00
|
|
|
|
xcb_grab_keyboard_reply_t *r = xcb_grab_keyboard_reply ( xcb->connection, cc, NULL );
|
2016-02-27 23:09:05 +00:00
|
|
|
|
if ( r ) {
|
2016-02-28 14:32:53 +00:00
|
|
|
|
if ( r->status == XCB_GRAB_STATUS_SUCCESS ) {
|
|
|
|
|
free ( r );
|
2016-02-28 09:36:13 +00:00
|
|
|
|
return 1;
|
|
|
|
|
}
|
2016-02-27 23:09:05 +00:00
|
|
|
|
free ( r );
|
2015-05-02 10:42:36 +00:00
|
|
|
|
}
|
2017-01-08 20:36:06 +00:00
|
|
|
|
if ( ( ++i ) > iters ) {
|
2017-01-06 10:00:27 +00:00
|
|
|
|
break;
|
|
|
|
|
}
|
2019-02-02 11:07:36 +00:00
|
|
|
|
struct timespec del = {
|
2020-02-02 12:56:37 +00:00
|
|
|
|
.tv_sec = 0,
|
|
|
|
|
.tv_nsec = 1000000
|
2019-02-02 11:07:36 +00:00
|
|
|
|
};
|
|
|
|
|
nanosleep ( &del, NULL );
|
2015-02-09 18:35:51 +00:00
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-01 10:47:52 +00:00
|
|
|
|
static void release_keyboard ( void )
|
2015-02-09 18:35:51 +00:00
|
|
|
|
{
|
2016-03-01 17:11:55 +00:00
|
|
|
|
xcb_ungrab_keyboard ( xcb->connection, XCB_CURRENT_TIME );
|
2015-02-09 18:35:51 +00:00
|
|
|
|
}
|
2017-06-01 10:47:52 +00:00
|
|
|
|
static void release_pointer ( void )
|
2016-05-09 06:47:28 +00:00
|
|
|
|
{
|
|
|
|
|
xcb_ungrab_pointer ( xcb->connection, XCB_CURRENT_TIME );
|
|
|
|
|
}
|
2015-02-09 18:35:51 +00:00
|
|
|
|
|
2017-05-31 22:39:11 +00:00
|
|
|
|
/** X server error depth. to handle nested errors. */
|
|
|
|
|
static int error_trap_depth = 0;
|
|
|
|
|
static void error_trap_push ( G_GNUC_UNUSED SnDisplay *display, G_GNUC_UNUSED xcb_connection_t *xdisplay )
|
|
|
|
|
{
|
|
|
|
|
++error_trap_depth;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void error_trap_pop ( G_GNUC_UNUSED SnDisplay *display, xcb_connection_t *xdisplay )
|
|
|
|
|
{
|
|
|
|
|
if ( error_trap_depth == 0 ) {
|
|
|
|
|
g_warning ( "Error trap underflow!" );
|
|
|
|
|
exit ( EXIT_FAILURE );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
xcb_flush ( xdisplay );
|
|
|
|
|
--error_trap_depth;
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-10 07:12:03 +00:00
|
|
|
|
/**
|
2016-07-29 06:32:34 +00:00
|
|
|
|
* Fill in the list of frequently used X11 Atoms.
|
2015-02-10 07:12:03 +00:00
|
|
|
|
*/
|
2016-03-01 17:11:55 +00:00
|
|
|
|
static void x11_create_frequently_used_atoms ( void )
|
2015-02-09 18:35:51 +00:00
|
|
|
|
{
|
2016-02-28 14:32:53 +00:00
|
|
|
|
// X atom values
|
2015-02-09 18:35:51 +00:00
|
|
|
|
for ( int i = 0; i < NUM_NETATOMS; i++ ) {
|
2016-03-01 17:11:55 +00:00
|
|
|
|
xcb_intern_atom_cookie_t cc = xcb_intern_atom ( xcb->connection, 0, strlen ( netatom_names[i] ), netatom_names[i] );
|
|
|
|
|
xcb_intern_atom_reply_t *r = xcb_intern_atom_reply ( xcb->connection, cc, NULL );
|
2016-02-27 23:09:05 +00:00
|
|
|
|
if ( r ) {
|
2016-02-28 14:32:53 +00:00
|
|
|
|
netatoms[i] = r->atom;
|
|
|
|
|
free ( r );
|
2016-02-27 23:09:05 +00:00
|
|
|
|
}
|
2015-02-09 18:35:51 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-01 13:08:20 +00:00
|
|
|
|
static void x11_helper_discover_window_manager ( void )
|
|
|
|
|
{
|
|
|
|
|
xcb_window_t wm_win = 0;
|
|
|
|
|
xcb_get_property_cookie_t cc = xcb_ewmh_get_supporting_wm_check_unchecked ( &xcb->ewmh,
|
2018-08-08 13:55:13 +00:00
|
|
|
|
xcb_stuff_get_root_window () );
|
2017-06-01 13:08:20 +00:00
|
|
|
|
|
|
|
|
|
if ( xcb_ewmh_get_supporting_wm_check_reply ( &xcb->ewmh, cc, &wm_win, NULL ) ) {
|
|
|
|
|
xcb_ewmh_get_utf8_strings_reply_t wtitle;
|
|
|
|
|
xcb_get_property_cookie_t cookie = xcb_ewmh_get_wm_name_unchecked ( &( xcb->ewmh ), wm_win );
|
|
|
|
|
if ( xcb_ewmh_get_wm_name_reply ( &( xcb->ewmh ), cookie, &wtitle, (void *) 0 ) ) {
|
|
|
|
|
if ( wtitle.strings_len > 0 ) {
|
|
|
|
|
g_debug ( "Found window manager: %s", wtitle.strings );
|
|
|
|
|
if ( g_strcmp0 ( wtitle.strings, "i3" ) == 0 ) {
|
2017-10-05 15:45:50 +00:00
|
|
|
|
current_window_manager = WM_DO_NOT_CHANGE_CURRENT_DESKTOP | WM_PANGO_WORKSPACE_NAMES;
|
2017-07-05 19:30:29 +00:00
|
|
|
|
}
|
2017-06-01 13:08:20 +00:00
|
|
|
|
}
|
|
|
|
|
xcb_ewmh_get_utf8_strings_reply_wipe ( &wtitle );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-01 12:55:17 +00:00
|
|
|
|
gboolean display_setup ( GMainLoop *main_loop, NkBindings *bindings )
|
2015-02-09 18:35:51 +00:00
|
|
|
|
{
|
2017-05-31 22:39:11 +00:00
|
|
|
|
// Get DISPLAY, first env, then argument.
|
|
|
|
|
// We never modify display_str content.
|
|
|
|
|
char *display_str = ( char *) g_getenv ( "DISPLAY" );
|
|
|
|
|
find_arg_str ( "-display", &display_str );
|
|
|
|
|
|
2017-06-01 09:44:21 +00:00
|
|
|
|
xcb->main_loop = main_loop;
|
2017-06-03 18:35:50 +00:00
|
|
|
|
xcb->source = g_water_xcb_source_new ( g_main_loop_get_context ( xcb->main_loop ), display_str, &xcb->screen_nbr, main_loop_x11_event_handler, NULL, NULL );
|
2017-06-01 09:44:21 +00:00
|
|
|
|
if ( xcb->source == NULL ) {
|
2017-05-31 22:39:11 +00:00
|
|
|
|
g_warning ( "Failed to open display: %s", display_str );
|
|
|
|
|
return FALSE;
|
|
|
|
|
}
|
2017-06-01 09:44:21 +00:00
|
|
|
|
xcb->connection = g_water_xcb_source_get_connection ( xcb->source );
|
2017-05-31 22:39:11 +00:00
|
|
|
|
|
|
|
|
|
TICK_N ( "Open Display" );
|
|
|
|
|
|
|
|
|
|
xcb->screen = xcb_aux_get_screen ( xcb->connection, xcb->screen_nbr );
|
|
|
|
|
|
|
|
|
|
x11_build_monitor_layout ();
|
|
|
|
|
|
|
|
|
|
xcb_intern_atom_cookie_t *ac = xcb_ewmh_init_atoms ( xcb->connection, &xcb->ewmh );
|
|
|
|
|
xcb_generic_error_t *errors = NULL;
|
|
|
|
|
xcb_ewmh_init_atoms_replies ( &xcb->ewmh, ac, &errors );
|
|
|
|
|
if ( errors ) {
|
|
|
|
|
g_warning ( "Failed to create EWMH atoms" );
|
|
|
|
|
free ( errors );
|
|
|
|
|
}
|
|
|
|
|
// Discover the current active window manager.
|
|
|
|
|
x11_helper_discover_window_manager ();
|
|
|
|
|
TICK_N ( "Setup XCB" );
|
|
|
|
|
|
|
|
|
|
if ( xkb_x11_setup_xkb_extension ( xcb->connection, XKB_X11_MIN_MAJOR_XKB_VERSION, XKB_X11_MIN_MINOR_XKB_VERSION,
|
2018-08-08 13:55:13 +00:00
|
|
|
|
XKB_X11_SETUP_XKB_EXTENSION_NO_FLAGS, NULL, NULL, &xcb->xkb.first_event, NULL ) < 0 ) {
|
2017-05-31 22:39:11 +00:00
|
|
|
|
g_warning ( "cannot setup XKB extension!" );
|
|
|
|
|
return FALSE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
xcb->xkb.device_id = xkb_x11_get_core_keyboard_device_id ( xcb->connection );
|
|
|
|
|
|
|
|
|
|
enum
|
|
|
|
|
{
|
|
|
|
|
required_events =
|
|
|
|
|
( XCB_XKB_EVENT_TYPE_NEW_KEYBOARD_NOTIFY |
|
|
|
|
|
XCB_XKB_EVENT_TYPE_MAP_NOTIFY |
|
|
|
|
|
XCB_XKB_EVENT_TYPE_STATE_NOTIFY ),
|
|
|
|
|
|
|
|
|
|
required_nkn_details =
|
|
|
|
|
( XCB_XKB_NKN_DETAIL_KEYCODES ),
|
|
|
|
|
|
|
|
|
|
required_map_parts =
|
|
|
|
|
( XCB_XKB_MAP_PART_KEY_TYPES |
|
|
|
|
|
XCB_XKB_MAP_PART_KEY_SYMS |
|
|
|
|
|
XCB_XKB_MAP_PART_MODIFIER_MAP |
|
|
|
|
|
XCB_XKB_MAP_PART_EXPLICIT_COMPONENTS |
|
|
|
|
|
XCB_XKB_MAP_PART_KEY_ACTIONS |
|
|
|
|
|
XCB_XKB_MAP_PART_VIRTUAL_MODS |
|
|
|
|
|
XCB_XKB_MAP_PART_VIRTUAL_MOD_MAP ),
|
|
|
|
|
|
|
|
|
|
required_state_details =
|
|
|
|
|
( XCB_XKB_STATE_PART_MODIFIER_BASE |
|
|
|
|
|
XCB_XKB_STATE_PART_MODIFIER_LATCH |
|
|
|
|
|
XCB_XKB_STATE_PART_MODIFIER_LOCK |
|
|
|
|
|
XCB_XKB_STATE_PART_GROUP_BASE |
|
|
|
|
|
XCB_XKB_STATE_PART_GROUP_LATCH |
|
|
|
|
|
XCB_XKB_STATE_PART_GROUP_LOCK ),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static const xcb_xkb_select_events_details_t details = {
|
|
|
|
|
.affectNewKeyboard = required_nkn_details,
|
|
|
|
|
.newKeyboardDetails = required_nkn_details,
|
|
|
|
|
.affectState = required_state_details,
|
|
|
|
|
.stateDetails = required_state_details,
|
|
|
|
|
};
|
|
|
|
|
xcb_xkb_select_events ( xcb->connection, xcb->xkb.device_id, required_events, /* affectWhich */
|
2018-08-08 13:55:13 +00:00
|
|
|
|
0, /* clear */
|
|
|
|
|
required_events, /* selectAll */
|
|
|
|
|
required_map_parts, /* affectMap */
|
|
|
|
|
required_map_parts, /* map */
|
|
|
|
|
&details );
|
2017-05-31 22:39:11 +00:00
|
|
|
|
|
2017-06-01 12:55:17 +00:00
|
|
|
|
xcb->bindings_seat = nk_bindings_seat_new ( bindings, XKB_CONTEXT_NO_FLAGS );
|
2017-05-31 22:39:11 +00:00
|
|
|
|
struct xkb_keymap *keymap = xkb_x11_keymap_new_from_device ( nk_bindings_seat_get_context ( xcb->bindings_seat ), xcb->connection, xcb->xkb.device_id, XKB_KEYMAP_COMPILE_NO_FLAGS );
|
|
|
|
|
if ( keymap == NULL ) {
|
|
|
|
|
g_warning ( "Failed to get Keymap for current keyboard device." );
|
|
|
|
|
return FALSE;
|
|
|
|
|
}
|
|
|
|
|
struct xkb_state *state = xkb_x11_state_new_from_device ( keymap, xcb->connection, xcb->xkb.device_id );
|
|
|
|
|
if ( state == NULL ) {
|
|
|
|
|
g_warning ( "Failed to get state object for current keyboard device." );
|
|
|
|
|
return FALSE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
nk_bindings_seat_update_keymap ( xcb->bindings_seat, keymap, state );
|
2017-06-03 18:35:50 +00:00
|
|
|
|
xkb_state_unref ( state );
|
|
|
|
|
xkb_keymap_unref ( keymap );
|
2017-05-31 22:39:11 +00:00
|
|
|
|
|
2015-02-09 18:35:51 +00:00
|
|
|
|
// determine numlock mask so we can bind on keys with and without it
|
2016-03-01 17:11:55 +00:00
|
|
|
|
x11_create_frequently_used_atoms ( );
|
2017-05-31 22:39:11 +00:00
|
|
|
|
|
|
|
|
|
if ( xcb_connection_has_error ( xcb->connection ) ) {
|
|
|
|
|
g_warning ( "Connection has error" );
|
|
|
|
|
return FALSE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// startup not.
|
|
|
|
|
xcb->sndisplay = sn_xcb_display_new ( xcb->connection, error_trap_push, error_trap_pop );
|
|
|
|
|
if ( xcb_connection_has_error ( xcb->connection ) ) {
|
|
|
|
|
g_warning ( "Connection has error" );
|
|
|
|
|
return FALSE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( xcb->sndisplay != NULL ) {
|
|
|
|
|
xcb->sncontext = sn_launchee_context_new_from_environment ( xcb->sndisplay, xcb->screen_nbr );
|
|
|
|
|
}
|
|
|
|
|
if ( xcb_connection_has_error ( xcb->connection ) ) {
|
|
|
|
|
g_warning ( "Connection has error" );
|
|
|
|
|
return FALSE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return TRUE;
|
2015-02-09 18:35:51 +00:00
|
|
|
|
}
|
2015-02-14 18:42:04 +00:00
|
|
|
|
|
2017-06-01 09:50:44 +00:00
|
|
|
|
static void x11_create_visual_and_colormap ( void )
|
2015-02-14 18:42:04 +00:00
|
|
|
|
{
|
2016-10-14 14:46:54 +00:00
|
|
|
|
xcb_depth_t *root_depth = NULL;
|
2016-02-21 18:42:32 +00:00
|
|
|
|
xcb_depth_iterator_t depth_iter;
|
2016-03-01 17:11:55 +00:00
|
|
|
|
for ( depth_iter = xcb_screen_allowed_depths_iterator ( xcb->screen ); depth_iter.rem; xcb_depth_next ( &depth_iter ) ) {
|
2016-02-21 18:42:32 +00:00
|
|
|
|
xcb_depth_t *d = depth_iter.data;
|
|
|
|
|
|
|
|
|
|
xcb_visualtype_iterator_t visual_iter;
|
|
|
|
|
for ( visual_iter = xcb_depth_visuals_iterator ( d ); visual_iter.rem; xcb_visualtype_next ( &visual_iter ) ) {
|
|
|
|
|
xcb_visualtype_t *v = visual_iter.data;
|
2016-08-15 06:58:48 +00:00
|
|
|
|
if ( ( v->bits_per_rgb_value == 8 ) && ( d->depth == 32 ) && ( v->_class == XCB_VISUAL_CLASS_TRUE_COLOR ) ) {
|
2016-02-21 18:42:32 +00:00
|
|
|
|
depth = d;
|
|
|
|
|
visual = v;
|
|
|
|
|
}
|
2016-03-01 17:11:55 +00:00
|
|
|
|
if ( xcb->screen->root_visual == v->visual_id ) {
|
2016-02-21 18:42:32 +00:00
|
|
|
|
root_depth = d;
|
|
|
|
|
root_visual = v;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if ( visual != NULL ) {
|
|
|
|
|
xcb_void_cookie_t c;
|
|
|
|
|
xcb_generic_error_t *e;
|
2016-03-01 17:11:55 +00:00
|
|
|
|
map = xcb_generate_id ( xcb->connection );
|
|
|
|
|
c = xcb_create_colormap_checked ( xcb->connection, XCB_COLORMAP_ALLOC_NONE, map, xcb->screen->root, visual->visual_id );
|
|
|
|
|
e = xcb_request_check ( xcb->connection, c );
|
2016-02-21 18:42:32 +00:00
|
|
|
|
if ( e ) {
|
|
|
|
|
depth = NULL;
|
|
|
|
|
visual = NULL;
|
|
|
|
|
free ( e );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( visual == NULL ) {
|
|
|
|
|
depth = root_depth;
|
|
|
|
|
visual = root_visual;
|
2016-03-01 17:11:55 +00:00
|
|
|
|
map = xcb->screen->default_colormap;
|
2015-02-14 18:42:04 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2015-09-27 09:46:19 +00:00
|
|
|
|
|
2017-06-01 09:57:57 +00:00
|
|
|
|
/** Retry count of grabbing keyboard. */
|
|
|
|
|
unsigned int lazy_grab_retry_count_kb = 0;
|
|
|
|
|
/** Retry count of grabbing pointer. */
|
|
|
|
|
unsigned int lazy_grab_retry_count_pt = 0;
|
|
|
|
|
static gboolean lazy_grab_pointer ( G_GNUC_UNUSED gpointer data )
|
|
|
|
|
{
|
|
|
|
|
// After 5 sec.
|
|
|
|
|
if ( lazy_grab_retry_count_pt > ( 5 * 1000 ) ) {
|
|
|
|
|
g_warning ( "Failed to grab pointer after %u times. Giving up.", lazy_grab_retry_count_pt );
|
|
|
|
|
return G_SOURCE_REMOVE;
|
|
|
|
|
}
|
|
|
|
|
if ( take_pointer ( xcb_stuff_get_root_window (), 0 ) ) {
|
|
|
|
|
return G_SOURCE_REMOVE;
|
|
|
|
|
}
|
|
|
|
|
lazy_grab_retry_count_pt++;
|
|
|
|
|
return G_SOURCE_CONTINUE;
|
|
|
|
|
}
|
|
|
|
|
static gboolean lazy_grab_keyboard ( G_GNUC_UNUSED gpointer data )
|
|
|
|
|
{
|
|
|
|
|
// After 5 sec.
|
|
|
|
|
if ( lazy_grab_retry_count_kb > ( 5 * 1000 ) ) {
|
|
|
|
|
g_warning ( "Failed to grab keyboard after %u times. Giving up.", lazy_grab_retry_count_kb );
|
|
|
|
|
g_main_loop_quit ( xcb->main_loop );
|
|
|
|
|
return G_SOURCE_REMOVE;
|
|
|
|
|
}
|
|
|
|
|
if ( take_keyboard ( xcb_stuff_get_root_window (), 0 ) ) {
|
|
|
|
|
return G_SOURCE_REMOVE;
|
|
|
|
|
}
|
|
|
|
|
lazy_grab_retry_count_kb++;
|
|
|
|
|
return G_SOURCE_CONTINUE;
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-01 12:55:17 +00:00
|
|
|
|
gboolean display_late_setup ( void )
|
2017-06-01 09:50:44 +00:00
|
|
|
|
{
|
|
|
|
|
x11_create_visual_and_colormap ();
|
2017-06-01 09:57:57 +00:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Create window (without showing)
|
|
|
|
|
*/
|
|
|
|
|
// Try to grab the keyboard as early as possible.
|
|
|
|
|
// We grab this using the rootwindow (as dmenu does it).
|
|
|
|
|
// this seems to result in the smallest delay for most people.
|
|
|
|
|
if ( find_arg ( "-normal-window" ) >= 0 ) {
|
2017-06-01 10:47:11 +00:00
|
|
|
|
return TRUE;
|
2017-06-01 09:57:57 +00:00
|
|
|
|
}
|
|
|
|
|
if ( find_arg ( "-no-lazy-grab" ) >= 0 ) {
|
|
|
|
|
if ( !take_keyboard ( xcb_stuff_get_root_window (), 500 ) ) {
|
|
|
|
|
g_warning ( "Failed to grab keyboard, even after %d uS.", 500 * 1000 );
|
|
|
|
|
return FALSE;
|
|
|
|
|
}
|
|
|
|
|
if ( !take_pointer ( xcb_stuff_get_root_window (), 100 ) ) {
|
|
|
|
|
g_warning ( "Failed to grab mouse pointer, even after %d uS.", 100 * 1000 );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
if ( !take_keyboard ( xcb_stuff_get_root_window (), 0 ) ) {
|
|
|
|
|
g_timeout_add ( 1, lazy_grab_keyboard, NULL );
|
|
|
|
|
}
|
|
|
|
|
if ( !take_pointer ( xcb_stuff_get_root_window (), 0 ) ) {
|
|
|
|
|
g_timeout_add ( 1, lazy_grab_pointer, NULL );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return TRUE;
|
2017-06-01 09:50:44 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-06-01 09:44:21 +00:00
|
|
|
|
xcb_window_t xcb_stuff_get_root_window ( void )
|
2016-03-01 17:11:55 +00:00
|
|
|
|
{
|
|
|
|
|
return xcb->screen->root;
|
|
|
|
|
}
|
2016-03-01 17:33:26 +00:00
|
|
|
|
|
2017-06-01 12:55:17 +00:00
|
|
|
|
void display_early_cleanup ( void )
|
2017-06-01 10:47:52 +00:00
|
|
|
|
{
|
|
|
|
|
release_keyboard ( );
|
|
|
|
|
release_pointer ( );
|
|
|
|
|
xcb_flush ( xcb->connection );
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-01 12:55:17 +00:00
|
|
|
|
void display_cleanup ( void )
|
2016-03-01 17:33:26 +00:00
|
|
|
|
{
|
2017-06-01 09:44:21 +00:00
|
|
|
|
if ( xcb->connection == NULL ) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
g_debug ( "Cleaning up XCB and XKB" );
|
|
|
|
|
|
2017-05-31 22:39:11 +00:00
|
|
|
|
nk_bindings_seat_free ( xcb->bindings_seat );
|
2017-06-01 09:44:21 +00:00
|
|
|
|
if ( xcb->sncontext != NULL ) {
|
|
|
|
|
sn_launchee_context_unref ( xcb->sncontext );
|
|
|
|
|
xcb->sncontext = NULL;
|
|
|
|
|
}
|
|
|
|
|
if ( xcb->sndisplay != NULL ) {
|
|
|
|
|
sn_display_unref ( xcb->sndisplay );
|
|
|
|
|
xcb->sndisplay = NULL;
|
2016-03-01 17:33:26 +00:00
|
|
|
|
}
|
2017-06-01 09:44:21 +00:00
|
|
|
|
x11_monitors_free ();
|
|
|
|
|
xcb_ewmh_connection_wipe ( &( xcb->ewmh ) );
|
|
|
|
|
xcb_flush ( xcb->connection );
|
|
|
|
|
xcb_aux_sync ( xcb->connection );
|
|
|
|
|
g_water_xcb_source_free ( xcb->source );
|
2017-06-03 18:35:50 +00:00
|
|
|
|
xcb->source = NULL;
|
2017-06-01 09:44:21 +00:00
|
|
|
|
xcb->connection = NULL;
|
|
|
|
|
xcb->screen = NULL;
|
|
|
|
|
xcb->screen_nbr = 0;
|
2016-03-01 17:33:26 +00:00
|
|
|
|
}
|
2016-09-08 06:53:14 +00:00
|
|
|
|
|
|
|
|
|
void x11_disable_decoration ( xcb_window_t window )
|
|
|
|
|
{
|
2016-11-15 07:24:06 +00:00
|
|
|
|
// Flag used to indicate we are setting the decoration type.
|
|
|
|
|
const uint32_t MWM_HINTS_DECORATIONS = ( 1 << 1 );
|
2016-11-15 07:24:27 +00:00
|
|
|
|
// Motif property data structure
|
2016-09-08 06:53:14 +00:00
|
|
|
|
struct MotifWMHints
|
|
|
|
|
{
|
|
|
|
|
uint32_t flags;
|
|
|
|
|
uint32_t functions;
|
|
|
|
|
uint32_t decorations;
|
|
|
|
|
int32_t inputMode;
|
|
|
|
|
uint32_t state;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct MotifWMHints hints;
|
2016-11-15 07:24:06 +00:00
|
|
|
|
hints.flags = MWM_HINTS_DECORATIONS;
|
2016-09-08 06:53:14 +00:00
|
|
|
|
hints.decorations = 0;
|
|
|
|
|
hints.functions = 0;
|
|
|
|
|
hints.inputMode = 0;
|
|
|
|
|
hints.state = 0;
|
|
|
|
|
|
|
|
|
|
xcb_atom_t ha = netatoms[_MOTIF_WM_HINTS];
|
|
|
|
|
xcb_change_property ( xcb->connection, XCB_PROP_MODE_REPLACE, window, ha, ha, 32, 5, &hints );
|
|
|
|
|
}
|