File: fcitx-remote.c

package info (click to toggle)
fcitx 1%3A4.2.4.1-7
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 12,636 kB
  • sloc: ansic: 120,224; cpp: 1,115; sh: 332; xml: 163; makefile: 29
file content (144 lines) | stat: -rw-r--r-- 4,028 bytes parent folder | download
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/***************************************************************************
 *   Copyright (C) 2008~2010 by wind (xihe)                                *
 *   xihels@gmail.com                                                      *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.              *
 ***************************************************************************/

#ifdef FCITX_HAVE_CONFIG_H
#  include <config.h>
#endif

#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/un.h>
#include <unistd.h>
#include <fcntl.h>
#include <poll.h>
#include <limits.h>
#include "fcitx/frontend.h"
#include "fcitx-utils/utils.h"

int create_socket(const char *name)
{
    int fd;
    int r;

    struct sockaddr_un uds_addr;

    fd = socket(AF_UNIX, SOCK_STREAM, 0);

    if (fd < 0) {
        return fd;
    }

    /* setup address struct */
    memset(&uds_addr, 0, sizeof(uds_addr));

    uds_addr.sun_family = AF_UNIX;

    strcpy(uds_addr.sun_path, name);

    r = connect(fd, (struct sockaddr *) & uds_addr, sizeof(uds_addr));

    if (r < 0) {
        return r;
    }

    return fd;
}

void usage()
{
    printf("Usage: fcitx-remote [OPTION]\n"
           "\t-c\t\tinactivate input method\n"
           "\t-o\t\tactivate input method\n"
           "\t-r\t\treload fcitx config\n"
           "\t-t,-T\t\tswitch Active/Inactive\n"
           "\t[no option]\tdisplay fcitx state, %d for close, %d for inactive, %d for acitve\n"
           "\t-h\t\tdisplay this help and exit\n",
           IS_CLOSED, IS_INACTIVE, IS_ACTIVE);
}

int main(int argc, char *argv[])
{
    char *socketfile = NULL;
    int socket_fd;

    int o = 0;
    char c;

    while ((c = getopt(argc, argv, "chortT")) != -1) {
        switch (c) {
        case 'o':
            o = 1;
            o |= (1 << 16);
            break;

        case 'c':
            o = 1;
            break;

        case 'r':
            o = 2;
            break;

        case 't':
        case 'T':
            o = 3;
            break;

        case 'h':

        default:
            usage();
            return 0;
            break;
        }
    }

    asprintf(&socketfile, "/tmp/fcitx-socket-:%d", fcitx_utils_get_display_number());

    socket_fd = create_socket(socketfile);

    if (socket_fd < 0) {
        fprintf(stderr, "Can't open socket %s: %s\n", socketfile, strerror(errno));
        free(socketfile);
        return 1;
    }
    free(socketfile);

    if (o == 0) {
        write(socket_fd, &o, sizeof(o));
        int buf;
        read(socket_fd, &buf, sizeof(buf));
        printf("%d\n", buf);
        close(socket_fd);
    } else {
        write(socket_fd, &o, sizeof(o));
        close(socket_fd);
    }

    return 0;
}               /* ----------  end of function main  ---------- */


// kate: indent-mode cstyle; space-indent on; indent-width 4;