aboutsummaryrefslogtreecommitdiffstats
path: root/src/panel_util.c
blob: 593413f265c9fed465eb83b4ef6287fdbbe83226 (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
#include "panel_util.h"

extern MBPanel* G_panel;

void
util_cleanup_children(int s)
{
  DBG("DIE DIE\n");
  kill(-getpid(), 15);  /* kill every one in our process group  */
  exit(0);
}

void 
util_install_signal_handlers(void)
{

  signal (SIGCHLD, SIG_IGN);  /* kernel can deal with zombies  */
  signal (SIGINT, util_cleanup_children);
  signal (SIGQUIT, util_cleanup_children);
  signal (SIGTERM, util_cleanup_children);
  signal (SIGHUP, util_handle_hup);
  signal (SIGALRM, util_handle_alarm);
  //  signal (SIGSEGV, cleanup_children);

}

int 
util_handle_xerror(Display *dpy, XErrorEvent *e)
{
  char msg[255];
  XGetErrorText(dpy, e->error_code, msg, sizeof msg);
  fprintf(stderr, "Panel X error (%#lx):\n %s (opcode: %i)\n",
	  e->resourceid, msg, e->request_code);
  return 0;
}

pid_t 
util_fork_exec(char *cmd)
{
  pid_t pid, mypid;
  mypid = getpid();
  pid = fork();

  switch (pid) {
  case 0:
    setpgid (0, mypid); /* set pgid to parents pid  */
    mb_exec(cmd);
    fprintf(stderr, "exec failed, cleaning up child\n");
    exit(1);
  case -1:
    fprintf(stderr, "can't fork\n"); break;
  }
  return pid;
}


void 
util_handle_alarm(int s)
{
  MBPanel *p = G_panel;
  DBG("%s() called", __func__);
  session_save(p);
}

void 
util_handle_hup(int s)
{
  MBPanel *p = G_panel;
  DBG("%s() called", __func__);

  if (p != NULL)
    {
      p->reload_pending = True;
    }
}

void
util_get_mouse_position(MBPanel *panel, int *x, int *y)
{
  Window mouse_root, mouse_win;
  int win_x, win_y;
  unsigned int mask;

  XQueryPointer(panel->dpy, panel->win_root, &mouse_root, &mouse_win,
		x, y, &win_x, &win_y, &mask);
}

pid_t
util_get_window_pid_from_prop(MBPanel *panel, Window win)
{
  Atom type;
  int format;
  long bytes_after;
  unsigned int *data = NULL;
  long n_items;
  int result;
  pid_t pid_result = 0; 

  result =  XGetWindowProperty (panel->dpy, win, 
				panel->atoms[ATOM_NET_WM_PID],
				0, 16L,
				False, XA_CARDINAL,
				&type, &format, &n_items,
				&bytes_after, (unsigned char **)&data);

  if (result == Success && n_items) 
    pid_result = *data;

  if (data) XFree(data);

  return pid_result;
}

Bool
util_get_command_str_from_win(MBPanel *panel, Window win, char **result)
{
   int i, bytes_needed = 0; 
   char *p = NULL, *cmd = NULL, **argv_win;
   int argc_win;

   if (!XGetCommand(panel->dpy, win, &argv_win, &argc_win))
     return False;

   bytes_needed = strlen(argv_win[0])+2;

   for(i=1;i<argc_win;i++)
   {
     bytes_needed += strlen(argv_win[i])+2;
     for (p = argv_win[i]; *p != '\0'; p++)
       if (*p == ' ' || *p == '\t')
	 bytes_needed++;
   }

   *result = malloc(sizeof(char)*bytes_needed);
   cmd = *result;

   strcpy(cmd, argv_win[0]);
   while(*cmd != '\0') cmd++;

   for(i=1;i<argc_win;i++)
   {
     p = argv_win[i];
     *cmd++ = ' ';

     if (strpbrk(p, " \t") == NULL)
       {
	 while (*p) *cmd++ = *p++;
       } else {
	 *cmd++ = '\'';
	 while (*p)
	   {
	     if (*p == '\'')
	       *cmd++ = '\\';
	     *cmd++ = *p++;
	   }
	 *cmd++ = '\'';
       }
   }

   *cmd = '\0';

   XFreeStringList(argv_win);

   return True;
}

Bool
util_xcol_from_spec(MBPanel *panel, MBColor *col, char *spec)
{
  mb_col_set (col, spec);
  return True;
}

Pixmap
util_get_root_pixmap(MBPanel *panel)
{
  Pixmap root_pxm = None;

  Atom type;
  int format;
  long bytes_after;
  Pixmap *data = NULL;
  long n_items;
  int result;

  result =  XGetWindowProperty (panel->dpy, panel->win_root, 
				panel->atoms[ATOM_XROOTPMAP_ID],
				0, 16L,
				False, XA_PIXMAP,
				&type, &format, &n_items,
				&bytes_after, (unsigned char **)&data);

  if (result == Success && n_items) 
    root_pxm = *data;

  if (data) XFree(data);

  panel->root_pixmap_id = root_pxm;

  return root_pxm;
}

unsigned char *
util_get_utf8_prop(MBPanel *panel, Window win, Atom req_atom)
{
  Atom type;
  int format;
  long bytes_after;
  unsigned char *str = NULL;
  long n_items;
  int result;

  result =  XGetWindowProperty (panel->dpy, win, 
				req_atom,
				0, 1024L,
				False, panel->atoms[ATOM_UTF8_STRING],
				&type, &format, &n_items,
				&bytes_after, (unsigned char **)&str);

  if (result != Success || str == NULL)
    {
      if (str) XFree (str);
      return NULL;
    }

  if (type != panel->atoms[ATOM_UTF8_STRING] || format != 8 || n_items == 0)
    {
      XFree (str);
      return NULL;
    }

  return str;
}