aboutsummaryrefslogtreecommitdiffstats
path: root/util/matchbox-remote.c
blob: 78f1c6f6c4e5e1bba49b3703f8cc788258d38f3a (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
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/*
 *  Matchbox Window Manager - A lightweight window manager not for the
 *                            desktop.
 *
 *  Authored By Matthew Allum <mallum@o-hand.com>
 *
 *  Copyright (c) 2008 OpenedHand Ltd - http://o-hand.com
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 *
 */
#define _GNU_SOURCE

#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xatom.h>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#define MB_CMD_SET_THEME       1
#define MB_CMD_EXIT            2
#define MB_CMD_DESKTOP         3
#define MB_CMD_NEXT            4
#define MB_CMD_PREV            5
#define MB_CMD_SHOW_EXT_MENU   6
#define MB_CMD_MISC            7
#define MB_CMD_COMPOSITE       8
#define MB_CMB_KEYS_RELOAD     9

#define MB_CMD_PANEL_TOGGLE_VISIBILITY 1
#define MB_CMD_PANEL_SIZE              2
#define MB_CMD_PANEL_ORIENTATION       3

#define MB_PANEL_ORIENTATION_NORTH     1
#define MB_PANEL_ORIENTATION_EAST      2
#define MB_PANEL_ORIENTATION_SOUTH     3
#define MB_PANEL_ORIENTATION_WEST      4

Display* dpy;

static void
getRootProperty(char * name, Bool delete)
{
   Atom prop;
   Atom realType;
   unsigned long n;
   unsigned long extra;
   int format;
   int status;
   char * value;

   prop = XInternAtom(dpy, name, True);
   if (prop == None) {
      fprintf(stderr, "mbcontrol: Unable to find theme name\n");
      return;
   }

   status = XGetWindowProperty(dpy, DefaultRootWindow(dpy),
			       prop, 0L, 512L, delete,
			       AnyPropertyType, &realType, &format,
			       &n, &extra, (unsigned char **) &value);

   if (status != Success) { // || value == 0 || *value == 0 || n == 0) {
      fprintf(stderr, "Unable to find theme name\n");
      return;
   }

   if (value) printf("%s\n", value);

}

static void
mbpanelcommand(int cmd_id, int panel_id)
{
  XEvent	ev;
  Window	root, panel;
  Atom          cmd_prop, system_tray_atom;
  char          atomName[64];

  root = DefaultRootWindow(dpy);

  snprintf(atomName, 64, "_NET_SYSTEM_TRAY_S%d", panel_id);

  system_tray_atom = XInternAtom(dpy, atomName, False);
  cmd_prop         = XInternAtom(dpy, "_MB_COMMAND", False);

  if (cmd_id == MB_CMD_PANEL_TOGGLE_VISIBILITY)
    {
      panel = XGetSelectionOwner(dpy, system_tray_atom);

      if (panel != None)
	{
	  memset(&ev, '\0', sizeof ev);
	  ev.xclient.type   = ClientMessage;
	  ev.xclient.window = root;
	  ev.xclient.message_type = cmd_prop;
	  ev.xclient.format = 8;
	  ev.xclient.data.l[0] = cmd_id;

	  XSendEvent(dpy, panel,False,
		     SubstructureRedirectMask|SubstructureNotifyMask, &ev);

	}
      else
	{
	  fprintf(stderr, "Cant find panel %i\n", panel_id);
	  exit(1);
	}
    }
}

static void
mbcommand(int cmd_id, char *data)
{

   XEvent	ev;
   Window	root;
   Atom theme_prop, cmd_prop, desktop_manager_atom;

   desktop_manager_atom = XInternAtom(dpy, "_NET_DESKTOP_MANAGER",False);

   root = DefaultRootWindow(dpy);

   if (cmd_id == MB_CMD_SET_THEME)
   {
      theme_prop = XInternAtom(dpy, "_MB_THEME", False);
      XChangeProperty(dpy, root, theme_prop, XA_STRING, 8,
		      PropModeReplace, (unsigned char*)data, strlen(data));
   }

   if (cmd_id == MB_CMD_DESKTOP)
     {
       /* Check if desktop is running */
       if (!XGetSelectionOwner(dpy, desktop_manager_atom))
	 {
	   fprintf(stderr, "Desktop not running, exiting...\n");
	   switch (fork())
	     {
	     case 0:
	       execvp ("mbdesktop", NULL);
	       break;
	     case -1:
	       fprintf(stderr, "failed to exec mbdesktop");
	       break;
	     }
	   exit(0);
	 }
     }

   cmd_prop = XInternAtom(dpy, "_MB_COMMAND", False);

   memset(&ev, '\0', sizeof ev);
   ev.xclient.type = ClientMessage;
   ev.xclient.window = root; 	/* we send it _from_ root as we have no win  */
   ev.xclient.message_type = cmd_prop;
   ev.xclient.format = 8;

   ev.xclient.data.l[0] = cmd_id;

   XSendEvent(dpy, root, False,
	      SubstructureRedirectMask|SubstructureNotifyMask, &ev);

}

static void
select_desktop (int desktop)
{
   XEvent	ev;
   Window	root;
   Atom atom;

   atom = XInternAtom(dpy, "_NET_CURRENT_DESKTOP",False);

   root = DefaultRootWindow(dpy);

   memset(&ev, '\0', sizeof ev);
   ev.xclient.type = ClientMessage;
   ev.xclient.window = root; 	/* we send it _from_ root as we have no win  */
   ev.xclient.message_type = atom;
   ev.xclient.format = 32;

   ev.xclient.data.l[0] = desktop;

   XSendEvent(dpy, root, False,
	      SubstructureRedirectMask|SubstructureNotifyMask, &ev);

}

void
send_input_manager_request(int show)
{
   XEvent	ev;
   Window	root;
   Atom         atom_input;

   atom_input = XInternAtom(dpy, "_MB_INPUT_REQUEST",False);

   root = DefaultRootWindow(dpy);

   memset(&ev, '\0', sizeof ev);

   ev.xclient.type = ClientMessage;
   ev.xclient.window = root; 	/* we send it _from_ root as we have no win  */
   ev.xclient.message_type = atom_input;
   ev.xclient.format = 8;

   ev.xclient.data.l[0] = show;

   printf("valie : %i\n", show);

   XSendEvent(dpy, root, False,
	      SubstructureRedirectMask|SubstructureNotifyMask, &ev);
}


static void
usage(char *progname)
{
   printf("Usage: %s [options...]\n", progname);
   printf("Options:\n");
   printf("  -t                       <matchbox theme name>  switch matchbox theme\n");
   printf("  -r                       Print current matchbox theme to stdout \n");
   printf("  -exit                    Request matchbox to exit \n");
   printf("  -next                    Page to next window \n");
   printf("  -prev                    Page to previous window \n");
   printf("  -desktop                 Toggle desktop visibility\n");
   printf("  -menu                    Activate mb-applet-menu-launcher\n");
   printf("  -panel-toggle [panel id] Toogle panel visibility\n");
   printf("  -input-toggle [1|0]      Toggle Input method ( requires input-manager )\n");
   printf("  -composite-toggle        Toggle Compositing Engine ( if enabled )\n");
   printf("  -keys-reload             Reload key shortcut config ( if enabled )\n");
   printf("  -select-desktop [id]     Select current desktop\n");

   /*
   printf("  -panel-size <int>\n");
   printf("  -panel-orientate <north|east|south|west>\n");
   */
   printf("  -h  this help\n\n");
   exit(1);
}

int main(int argc, char* argv[])
{
  char *display_name = (char *)getenv("DISPLAY");
  int i;

  if (argc < 2) usage(argv[0]);

  dpy = XOpenDisplay(display_name);
  if (dpy == NULL) {
     printf("Cant connect to display: %s\n", display_name);
     exit(1);
  }

  /* pass command line */
  for (i=1; argv[i]; i++) {
     char *arg = argv[i];
     if (*arg=='-') {
	switch (arg[1])
	{
	case 't' :
	  if (argv[i+1] != NULL)
	    mbcommand(MB_CMD_SET_THEME, argv[i+1]);
	  i++;
	  break;
	case 'r' :
	  getRootProperty("_MB_THEME", False);
	  i++;
	  break;
	case 'e':
	  mbcommand(MB_CMD_EXIT, NULL);
	  break;
	case 'd':
	  mbcommand(MB_CMD_DESKTOP, NULL);
	  break;
	case 'n':
	  mbcommand(MB_CMD_NEXT, NULL);
	  break;
	case 'c':
	  mbcommand(MB_CMD_COMPOSITE, NULL);
	  break;
	case 'k':
	  mbcommand(MB_CMB_KEYS_RELOAD, NULL);
	  break;
	case 'p':
	  if (!strcmp(arg+1,"panel-toggle"))
	    {
	      int panel_id = 0;

	      if (argc > i+1) panel_id = atoi(argv[i+1]);
	      mbpanelcommand(MB_CMD_PANEL_TOGGLE_VISIBILITY, panel_id);
	    }
	  else if (strcmp(arg+1,"prev") == 0 || strlen(arg+1) == 1)
	    {
	      mbcommand(MB_CMD_PREV, NULL);
	    }
	  else usage(argv[0]);
	  break;
	case 'm':
	  mbcommand(MB_CMD_SHOW_EXT_MENU, NULL);
	  break;
	case 'x':
	  mbcommand(MB_CMD_MISC, NULL);
	  break;
	case 'i':
	  if (argv[i+1] != NULL)
	    send_input_manager_request(atoi(argv[i+1]));
	  else
	    usage(argv[0]);
	  break;
	case 's':
	  if (argv[i+1] != NULL)
	    select_desktop (atoi(argv[i+1]));
	  else
	    usage(argv[0]);
	  break;
	default:
	  usage(argv[0]);
	  break;
	}
     }
  }
  XSync(dpy, False);
  XCloseDisplay(dpy);

  return 0;
}