Adding upstream version 0.9.0.
[debian/lxpanel.git] / src / lxpanelctl.c
CommitLineData
0688b017
AG
1/*
2 * Copyright (C) 2006 Hong Jen Yee (PCMan) <pcman.tw@gmail.com>
3 * 2006 Jim Huang <jserv.tw@gmail.com>
4 * 2009-2010 Marty Jack <martyj19@comcast.net>
7a1c5048 5 * 2016 Andriy Grytsenko <andrej@rep.kiev.ua>
0688b017
AG
6 *
7 * This file is a part of LXPanel project.
6cc5e1a6
DB
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software Foundation,
21 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 */
23
24/* Remote controller of lxpanel */
25
26#include "lxpanelctl.h"
27#include <X11/Xlib.h>
28#include <X11/Xutil.h>
29#include <X11/Xatom.h>
30#include <stdio.h>
31#include <stdlib.h>
32#include <string.h>
7a1c5048 33#include <stdint.h>
6cc5e1a6
DB
34
35static Display* dpy;
36
37static const char usage[] =
38 "\nlxpanelctl - LXPanel Controller\n"
39 "Usage: lxpanelctl <command>\n\n"
40 "Available commands:\n"
7a1c5048
AG
41 "menu\t\t\tshow system menu\n"
42 "run\t\t\tshow run dialog\n"
43 "config\t\t\tshow configuration dialog\n"
44 "restart\t\t\trestart lxpanel\n"
45 "exit\t\t\texit lxpanel\n"
46 "command <plugin> <cmd>\tsend a command to a plugin\n\n";
6cc5e1a6
DB
47
48static int get_cmd( const char* cmd )
49{
50 if( ! strcmp( cmd, "menu") )
51 return LXPANEL_CMD_SYS_MENU;
52 else if( ! strcmp( cmd, "run") )
53 return LXPANEL_CMD_RUN;
4652f59b
DB
54 else if( ! strcmp( cmd, "config") )
55 return LXPANEL_CMD_CONFIG;
6cc5e1a6
DB
56 else if( ! strcmp( cmd, "restart") )
57 return LXPANEL_CMD_RESTART;
58 else if( ! strcmp( cmd, "exit") )
59 return LXPANEL_CMD_EXIT;
7a1c5048
AG
60 else if( ! strcmp( cmd, "command") )
61 return LXPANEL_CMD_COMMAND;
6cc5e1a6
DB
62 return -1;
63}
64
7a1c5048
AG
65/* format: either "<edge>" or "<num>:<edge>" */
66static int parse_id(const char *arg, int *mon)
67{
68 char *end;
69 long lmon = strtoul(arg, &end, 10);
70
71 if (*end == ':')
72 arg = (const char *)end + 1;
73 else
74 lmon = 0;
75 *mon = lmon;
76 if (strcmp(arg, "top") == 0)
77 return EDGE_TOP;
78 if (strcmp(arg, "bottom") == 0)
79 return EDGE_BOTTOM;
80 if (strcmp(arg, "left") == 0)
81 return EDGE_LEFT;
82 if (strcmp(arg, "right") == 0)
83 return EDGE_RIGHT;
84 return EDGE_NONE;
85}
86
6cc5e1a6
DB
87int main( int argc, char** argv )
88{
89 char *display_name = (char *)getenv("DISPLAY");
90 XEvent ev;
91 Window root;
92 Atom cmd_atom;
93 int cmd;
94 /* int restart; */
7a1c5048
AG
95 /* target of message, it's XClientMessageEvent::b[1]
96 * valid only if XClientMessageEvent::b[0] == LXPANEL_CMD_COMMAND */
97 uint8_t target;
6cc5e1a6
DB
98
99 if( argc < 2 )
100 {
101 printf( usage );
102 return 1;
103 }
104
105 /*
106 if( restart = !strcmp( argv[1], "restart" ) )
107 argv[1] = "exit";
108 */
109
110 if( ( cmd = get_cmd( argv[1] ) ) == -1 )
111 return 1;
112
7a1c5048
AG
113 if (cmd == LXPANEL_CMD_COMMAND && argc < 4)
114 {
115 printf( usage );
116 return 1;
117 }
118
6cc5e1a6
DB
119 dpy = XOpenDisplay(display_name);
120 if (dpy == NULL) {
121 printf("Cant connect to display: %s\n", display_name);
122 exit(1);
123 }
124 root = DefaultRootWindow(dpy);
125 cmd_atom = XInternAtom(dpy, "_LXPANEL_CMD", False);
126 memset(&ev, '\0', sizeof ev);
127 ev.xclient.type = ClientMessage;
128 ev.xclient.window = root;
129 ev.xclient.message_type = cmd_atom;
130 ev.xclient.format = 8;
131
4652f59b 132 ev.xclient.data.b[0] = cmd;
6cc5e1a6 133
7a1c5048
AG
134 if (cmd == LXPANEL_CMD_COMMAND)
135 {
136 int i = 2;
137 if (argc > 4 && strncmp(argv[2], "--panel=", 8) == 0)
138 {
139 int monitor;
140 int edge = parse_id(argv[2] + 8, &monitor);
141 /* edge: EDGE_NONE ..., monitor: 0 - none, 1...8 - selected */
142 target = ((edge & 0x7) << 4) + (monitor & 0xf);
143 i++;
144 }
145 else
146 target = (EDGE_NONE << 4) + 0; /* edge: none, monitor: none */
147 ev.xclient.data.b[1] = target;
148 snprintf(&ev.xclient.data.b[2], 18, "%s\t%s", argv[i], argv[i+1]);
149 }
150
6cc5e1a6
DB
151 XSendEvent(dpy, root, False,
152 SubstructureRedirectMask|SubstructureNotifyMask, &ev);
153 XSync(dpy, False);
154 XCloseDisplay(dpy);
155
156/*
157 if( restart ) {
158 system( PACKAGE_BIN_DIR "/lxpanel &" );
159 }
160*/
161 return 0;
162}
163