2015-02-02 08:49:19 -05:00
|
|
|
/**
|
|
|
|
* rofi
|
|
|
|
*
|
|
|
|
* MIT/X11 License
|
2015-12-31 18:27:00 -05:00
|
|
|
* Copyright (c) 2013-2016 Qball Cow <qball@gmpclient.org>
|
2015-02-02 08:49:19 -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>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <time.h>
|
2016-02-27 18:09:05 -05:00
|
|
|
#include <xcb/xcb.h>
|
2015-02-02 08:49:19 -05:00
|
|
|
#include <sys/socket.h>
|
2015-07-04 09:36:11 -04:00
|
|
|
#include <sys/un.h>
|
2016-03-01 12:11:55 -05:00
|
|
|
#include "xcb.h"
|
2015-02-02 08:49:19 -05:00
|
|
|
#include "rofi.h"
|
2015-02-09 13:35:51 -05:00
|
|
|
#include "x11-helper.h"
|
2015-02-12 16:26:28 -05:00
|
|
|
#include "i3-support.h"
|
2016-02-08 03:03:11 -05:00
|
|
|
#include "view.h"
|
2015-02-02 08:49:19 -05:00
|
|
|
|
|
|
|
#ifdef HAVE_I3_IPC_H
|
|
|
|
#include <i3/ipc.h>
|
|
|
|
// Path to HAVE_I3_IPC_H socket.
|
2016-03-01 13:48:18 -05:00
|
|
|
char *i3_socket_path = NULL;
|
2016-02-28 06:18:15 -05:00
|
|
|
void i3_support_focus_window ( xcb_window_t id )
|
2015-02-02 08:49:19 -05:00
|
|
|
{
|
|
|
|
i3_ipc_header_t head;
|
2016-02-02 03:36:39 -05:00
|
|
|
int s;
|
2015-02-02 08:49:19 -05:00
|
|
|
ssize_t t;
|
|
|
|
struct sockaddr_un remote;
|
2015-07-05 03:47:55 -04:00
|
|
|
size_t upm = sizeof ( remote.sun_path );
|
2015-07-04 10:53:36 -04:00
|
|
|
char command[upm];
|
2015-02-02 08:49:19 -05:00
|
|
|
|
2015-07-05 03:47:55 -04:00
|
|
|
if ( strlen ( i3_socket_path ) > upm ) {
|
2015-07-13 14:52:35 -04:00
|
|
|
fprintf ( stderr, "Socket path is too long. %zu > %zu\n", strlen ( i3_socket_path ), upm );
|
2015-02-02 08:49:19 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ( s = socket ( AF_UNIX, SOCK_STREAM, 0 ) ) == -1 ) {
|
|
|
|
fprintf ( stderr, "Failed to open connection to I3: %s\n", strerror ( errno ) );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
remote.sun_family = AF_UNIX;
|
2015-07-05 03:47:55 -04:00
|
|
|
g_strlcpy ( remote.sun_path, i3_socket_path, upm );
|
2015-02-02 08:49:19 -05:00
|
|
|
|
2016-02-02 03:36:39 -05:00
|
|
|
if ( connect ( s, ( struct sockaddr * ) &remote, sizeof ( struct sockaddr_un ) ) == -1 ) {
|
2015-02-02 08:49:19 -05:00
|
|
|
fprintf ( stderr, "Failed to connect to I3 (%s): %s\n", i3_socket_path, strerror ( errno ) );
|
|
|
|
close ( s );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Formulate command
|
2016-02-28 09:33:57 -05:00
|
|
|
snprintf ( command, upm, "[id=\"%u\"] focus", id );
|
2015-02-02 08:49:19 -05:00
|
|
|
// Prepare header.
|
|
|
|
memcpy ( head.magic, I3_IPC_MAGIC, 6 );
|
|
|
|
head.size = strlen ( command );
|
|
|
|
head.type = I3_IPC_MESSAGE_TYPE_COMMAND;
|
|
|
|
// Send header.
|
|
|
|
t = send ( s, &head, sizeof ( i3_ipc_header_t ), 0 );
|
|
|
|
if ( t == -1 ) {
|
|
|
|
char *msg = g_strdup_printf ( "Failed to send message header to i3: %s\n", strerror ( errno ) );
|
2016-02-08 03:03:11 -05:00
|
|
|
rofi_view_error_dialog ( msg, FALSE );
|
2015-02-02 08:49:19 -05:00
|
|
|
g_free ( msg );
|
|
|
|
close ( s );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Send message
|
|
|
|
t = send ( s, command, strlen ( command ), 0 );
|
|
|
|
if ( t == -1 ) {
|
|
|
|
char *msg = g_strdup_printf ( "Failed to send message body to i3: %s\n", strerror ( errno ) );
|
2016-02-08 03:03:11 -05:00
|
|
|
rofi_view_error_dialog ( msg, FALSE );
|
2015-02-02 08:49:19 -05:00
|
|
|
g_free ( msg );
|
|
|
|
close ( s );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Receive header.
|
|
|
|
t = recv ( s, &head, sizeof ( head ), 0 );
|
|
|
|
|
|
|
|
if ( t == sizeof ( head ) ) {
|
|
|
|
t = recv ( s, command, head.size, 0 );
|
|
|
|
if ( t == head.size ) {
|
|
|
|
// Response.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
close ( s );
|
|
|
|
}
|
|
|
|
|
2016-03-01 12:11:55 -05:00
|
|
|
int i3_support_initialize ( xcb_stuff *xcb )
|
2015-02-02 08:49:19 -05:00
|
|
|
{
|
2015-07-31 04:21:32 -04:00
|
|
|
// If we where initialized, clean this first.
|
|
|
|
i3_support_free_internals ();
|
2015-02-02 08:49:19 -05:00
|
|
|
|
2016-02-21 13:42:32 -05:00
|
|
|
// Get atom for I3_SOCKET_PATH
|
2016-03-01 13:48:18 -05:00
|
|
|
i3_socket_path = window_get_text_prop ( xcb_stuff_get_root_window ( xcb ), netatoms[I3_SOCKET_PATH] );
|
2015-02-02 08:49:19 -05:00
|
|
|
// If we find it, go into i3 mode.
|
|
|
|
return ( i3_socket_path != NULL ) ? TRUE : FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
void i3_support_free_internals ( void )
|
|
|
|
{
|
2015-02-12 16:42:29 -05:00
|
|
|
g_free ( i3_socket_path );
|
|
|
|
i3_socket_path = NULL;
|
2015-02-02 08:49:19 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
2016-02-28 06:18:15 -05:00
|
|
|
void i3_support_focus_window ( G_GNUC_UNUSED xcb_window_t id )
|
2015-02-02 08:49:19 -05:00
|
|
|
{
|
|
|
|
fprintf ( stderr, "Trying to control i3, when i3 support is not enabled.\n" );
|
|
|
|
abort ();
|
|
|
|
}
|
|
|
|
void i3_support_free_internals ( void )
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2016-03-02 12:07:59 -05:00
|
|
|
int i3_support_initialize ( G_GNUC_UNUSED xcb_stuff *xcb )
|
2015-02-02 08:49:19 -05:00
|
|
|
{
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
#endif // HAVE_I3_IPC_H
|