aboutsummaryrefslogtreecommitdiff
path: root/.local/bin/i3_tmux_vim_focus.c
blob: 8f0d0750f3076dcb75ffd9cdfd72941f5f364554 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/* File: i3_vim_focus.c
 *
 * Compile with:
 * gcc i3_tmux_vim_focus.c -lX11 -lxdo -o i3_tmux_vim_focus $(pkg-config --libs --cflags i3ipc-glib-1.0)
 *
 */

#include <stdio.h>

#include <strings.h>
#include <string.h>

#include <xdo.h>

#include <glib/gprintf.h>
#include <i3ipc-glib/i3ipc-glib.h>

int main(int argc, char *argv[]) {

    char cmd[20];

    unsigned char *name;
    int name_len;
    int name_type;
    Window window_ret;

    i3ipcConnection *conn;
    gchar *reply;

    if(argc < 2){
        printf("Missing argument\n");
        return 1;
    }

    xdo_t *xdo = xdo_new(NULL);
    xdo_get_active_window(xdo, &window_ret);
    xdo_get_window_name(xdo, window_ret, &name, &name_len, &name_type);

    if(strstr(name, "VIM") || strstr(name, "vim"))
    {
        strcpy(cmd, "Ctrl+c+Ctrl+");


        strcat(cmd, (argv[1][0] == 'l')? "h" :
                    (argv[1][0] == 'd')? "j" :
                    (argv[1][0] == 'u')? "k" :
                                         "l" );
        xdo_send_keysequence_window(xdo, window_ret, cmd, 0);
    }
    else if(strstr(name, "tmux"))
    {
        strcpy(cmd, "ctrl+a");


        xdo_send_keysequence_window(xdo, window_ret, cmd, 0);
        strcpy(cmd, (argv[1][0] == 'l')? "Left" :
                    (argv[1][0] == 'd')? "Down" :
                    (argv[1][0] == 'u')? "Up" :
                                         "Right" );

        xdo_send_keysequence_window(xdo, window_ret, cmd, 0);
    }
    else
    {
        conn = i3ipc_connection_new(NULL, NULL);
        strcpy(cmd, "focus ");
        strcat(cmd, argv[1]);
        reply = i3ipc_connection_message(conn, I3IPC_MESSAGE_TYPE_COMMAND, cmd, NULL);
        g_free(reply);
        g_object_unref(conn);
    }

    XFree(name);
    return  0;
}