~ubuntu-branches/ubuntu/trusty/fcitx/trusty-proposed

« back to all changes in this revision

Viewing changes to tools/fcitx-remote.c

  • Committer: Package Import Robot
  • Author(s): Aron Xu
  • Date: 2013-02-10 17:03:56 UTC
  • mfrom: (1.3.18) (33.1.3 experimental)
  • Revision ID: package-import@ubuntu.com-20130210170356-2yuv6xy3ed378kn0
Tags: 1:4.2.7-1
* New upstream release.
* New binary packages:
  - fcitx-libs-gclient: D-Bus client library for Glib
  - fcitx-libs-qt: D-Bus client library for Qt
  - fcitx-module-quickphrase-editor: Quick Phrase editor module

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/***************************************************************************
2
 
 *   Copyright (C) 2008~2010 by wind (xihe)                                *
3
 
 *   xihels@gmail.com                                                      *
4
 
 *                                                                         *
5
 
 *   This program is free software; you can redistribute it and/or modify  *
6
 
 *   it under the terms of the GNU General Public License as published by  *
7
 
 *   the Free Software Foundation; either version 2 of the License, or     *
8
 
 *   (at your option) any later version.                                   *
9
 
 *                                                                         *
10
 
 *   This program is distributed in the hope that it will be useful,       *
11
 
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
12
 
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
13
 
 *   GNU General Public License for more details.                          *
14
 
 *                                                                         *
15
 
 *   You should have received a copy of the GNU General Public License     *
16
 
 *   along with this program; if not, write to the                         *
17
 
 *   Free Software Foundation, Inc.,                                       *
18
 
 *   51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.              *
19
 
 ***************************************************************************/
20
 
 
21
 
#ifdef FCITX_HAVE_CONFIG_H
22
 
#  include <config.h>
23
 
#endif
24
 
 
25
 
#include <errno.h>
26
 
#include <stdio.h>
27
 
#include <stdlib.h>
28
 
#include <string.h>
29
 
#include <sys/socket.h>
30
 
#include <sys/types.h>
31
 
#include <sys/stat.h>
32
 
#include <sys/un.h>
33
 
#include <unistd.h>
34
 
#include <fcntl.h>
35
 
#include <poll.h>
36
 
#include <limits.h>
37
 
#include "fcitx/frontend.h"
38
 
#include "fcitx-utils/utils.h"
39
 
 
40
 
int create_socket(const char *name)
41
 
{
42
 
    int fd;
43
 
    int r;
44
 
 
45
 
    struct sockaddr_un uds_addr;
46
 
 
47
 
    fd = socket(AF_UNIX, SOCK_STREAM, 0);
48
 
 
49
 
    if (fd < 0) {
50
 
        return fd;
51
 
    }
52
 
 
53
 
    /* setup address struct */
54
 
    memset(&uds_addr, 0, sizeof(uds_addr));
55
 
 
56
 
    uds_addr.sun_family = AF_UNIX;
57
 
 
58
 
    strcpy(uds_addr.sun_path, name);
59
 
 
60
 
    r = connect(fd, (struct sockaddr *) & uds_addr, sizeof(uds_addr));
61
 
 
62
 
    if (r < 0) {
63
 
        return r;
64
 
    }
65
 
 
66
 
    return fd;
67
 
}
68
 
 
69
 
void usage()
70
 
{
71
 
    printf("Usage: fcitx-remote [OPTION]\n"
72
 
           "\t-c\t\tinactivate input method\n"
73
 
           "\t-o\t\tactivate input method\n"
74
 
           "\t-r\t\treload fcitx config\n"
75
 
           "\t-t,-T\t\tswitch Active/Inactive\n"
76
 
           "\t[no option]\tdisplay fcitx state, %d for close, %d for inactive, %d for acitve\n"
77
 
           "\t-h\t\tdisplay this help and exit\n",
78
 
           IS_CLOSED, IS_INACTIVE, IS_ACTIVE);
79
 
}
80
 
 
81
 
int main(int argc, char *argv[])
82
 
{
83
 
    char *socketfile = NULL;
84
 
    int socket_fd;
85
 
 
86
 
    int o = 0;
87
 
    char c;
88
 
 
89
 
    while ((c = getopt(argc, argv, "chortT")) != -1) {
90
 
        switch (c) {
91
 
        case 'o':
92
 
            o = 1;
93
 
            o |= (1 << 16);
94
 
            break;
95
 
 
96
 
        case 'c':
97
 
            o = 1;
98
 
            break;
99
 
 
100
 
        case 'r':
101
 
            o = 2;
102
 
            break;
103
 
 
104
 
        case 't':
105
 
        case 'T':
106
 
            o = 3;
107
 
            break;
108
 
 
109
 
        case 'h':
110
 
 
111
 
        default:
112
 
            usage();
113
 
            return 0;
114
 
            break;
115
 
        }
116
 
    }
117
 
 
118
 
    asprintf(&socketfile, "/tmp/fcitx-socket-:%d", fcitx_utils_get_display_number());
119
 
 
120
 
    socket_fd = create_socket(socketfile);
121
 
 
122
 
    if (socket_fd < 0) {
123
 
        fprintf(stderr, "Can't open socket %s: %s\n", socketfile, strerror(errno));
124
 
        free(socketfile);
125
 
        return 1;
126
 
    }
127
 
    free(socketfile);
128
 
 
129
 
    if (o == 0) {
130
 
        write(socket_fd, &o, sizeof(o));
131
 
        int buf;
132
 
        read(socket_fd, &buf, sizeof(buf));
133
 
        printf("%d\n", buf);
134
 
        close(socket_fd);
135
 
    } else {
136
 
        write(socket_fd, &o, sizeof(o));
137
 
        close(socket_fd);
138
 
    }
139
 
 
140
 
    return 0;
141
 
}               /* ----------  end of function main  ---------- */
142
 
 
143
 
 
144
 
// kate: indent-mode cstyle; space-indent on; indent-width 4;