summaryrefslogtreecommitdiff
path: root/src/format.c
blob: ed1f571c74c39c6b970a93043caa7453299d84a8 (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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
/*
 * Copyright (C) 2006 Antti Nykänen <aon@iki.fi>
 *
 * This file is part of ratpoison.
 *
 * ratpoison 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, or (at your option)
 * any later version.
 *
 * ratpoison 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 software; see the file COPYING.  If not, write to
 * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
 * Boston, MA 02111-1307 USA
 */

#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "ratpoison.h"

/* Function prototypes for format char expanders. */
#define RP_FMT(fn) static void fmt_ ## fn (rp_window_elem *elem, struct sbuf *buf)
RP_FMT(framenum);
RP_FMT(lastaccess);
RP_FMT(name);
RP_FMT(number);
RP_FMT(resname);
RP_FMT(resclass);
RP_FMT(status);
RP_FMT(windowid);
RP_FMT(height);
RP_FMT(width);
RP_FMT(incheight);
RP_FMT(incwidth);
RP_FMT(gravity);
RP_FMT(screen);
RP_FMT(xrandrscreen);
RP_FMT(transient);
RP_FMT(maxsize);
RP_FMT(pid);

struct fmt_item {
  /* The format character */
  char fmt_char;

  /* Callback to return the expanded string. */
  void (*fmt_fn)(rp_window_elem *, struct sbuf *);
};

struct fmt_item fmt_items[] = {
  { 'a', fmt_resname },
  { 'g', fmt_gravity },
  { 'h', fmt_height },
  { 'H', fmt_incheight },
  { 'c', fmt_resclass },
  { 'f', fmt_framenum },
  { 'i', fmt_windowid },
  { 'l', fmt_lastaccess },
  { 'n', fmt_number },
  { 'p', fmt_pid },
  { 's', fmt_status },
  { 'S', fmt_screen },
  { 't', fmt_name },
  { 'T', fmt_transient },
  { 'M', fmt_maxsize },  
  { 'w', fmt_width },
  { 'W', fmt_incwidth },
  { 'x', fmt_xrandrscreen},
  { 0, NULL }
};

void
format_string (char *fmt, rp_window_elem *win_elem, struct sbuf *buffer)
{
#define STATE_READ   0
#define STATE_NUMBER 1
#define STATE_ESCAPE 2
  int state = STATE_READ;
  char dbuf[10];
  int width = -1;
  struct sbuf *retbuf;
  int fip, found;

  retbuf = sbuf_new (0);

  for(; *fmt; fmt++)
    {
      if (*fmt == '%' && state == STATE_READ)
        {
          state = STATE_ESCAPE;
          continue;
        }

      if ((state == STATE_ESCAPE || state == STATE_NUMBER) && isdigit(*fmt))
        {
          /* Accumulate the width one digit at a time. */
          if (state == STATE_ESCAPE)
            width = 0;
          width *= 10;
          width += *fmt - '0';
          state = STATE_NUMBER;
          continue;
        }

      found = 0;
      if (state == STATE_ESCAPE || state == STATE_NUMBER)
        {
          if (*fmt == '%')
              sbuf_concat (buffer, "%");
          else
            {
              for (fip = 0; fmt_items[fip].fmt_char; fip++)
                {
                  if (fmt_items[fip].fmt_char == *fmt)
                    {
                      sbuf_clear (retbuf);
                      fmt_items[fip].fmt_fn(win_elem, retbuf);
                      sbuf_utf8_nconcat (buffer, sbuf_get (retbuf), width);
                      found = 1;
                      break;
                    }
                }
              if (!found)
                {
                  sbuf_printf_concat (buffer, "%%%c", *fmt);
                  break;
                }
            }
          state = STATE_READ;
          width = -1;
        }
      else
        {
          /* Insert the character. */
          dbuf[0] = *fmt;
          dbuf[1] = 0;
          sbuf_concat (buffer, dbuf);
        }
    }
  sbuf_free (retbuf);
#undef STATE_READ
#undef STATE_ESCAPE
#undef STATE_NUMBER
}

static void
fmt_framenum (rp_window_elem *win_elem, struct sbuf *buf)
{
  if (win_elem->win->frame_number != EMPTY)
    {
      sbuf_printf_concat (buf, "%d", win_elem->win->frame_number);
    }
  else
    sbuf_copy (buf, " ");
}

static void
fmt_lastaccess (rp_window_elem *win_elem, struct sbuf *buf)
{
  sbuf_printf_concat (buf, "%d", win_elem->win->last_access);
}

static void
fmt_name (rp_window_elem *win_elem, struct sbuf *buf)
{
  sbuf_copy(buf, window_name (win_elem->win));
}

static void
fmt_number (rp_window_elem *win_elem, struct sbuf *buf)
{
  sbuf_printf_concat (buf, "%d", win_elem->number);
}

static void
fmt_resname (rp_window_elem *win_elem, struct sbuf *buf)
{
  if (win_elem->win->res_name)
    sbuf_copy (buf, win_elem->win->res_name);
  else
    sbuf_copy (buf, "None");
}

static void
fmt_resclass (rp_window_elem *win_elem, struct sbuf *buf)
{
  if (win_elem->win->res_class)
    sbuf_copy (buf, win_elem->win->res_class);
  else
    sbuf_copy (buf, "None");
}

static void
fmt_status (rp_window_elem *win_elem, struct sbuf *buf)
{
  rp_window *other_window;

  other_window = find_window_other (rp_current_screen);
  if (win_elem->win == other_window)
    sbuf_copy (buf, "+");
  else if (win_elem->win == current_window())
    sbuf_copy (buf, "*");
  else
    sbuf_copy (buf, "-");
}

static void
fmt_windowid (rp_window_elem *elem, struct sbuf *buf)
{
  sbuf_printf_concat (buf, "%ld", (unsigned long)elem->win->w);
}

static void
fmt_height (rp_window_elem *elem, struct sbuf *buf)
{
  sbuf_printf_concat (buf, "%d", elem->win->height);
}

static void
fmt_width (rp_window_elem *elem, struct sbuf *buf)
{
  sbuf_printf_concat (buf, "%d", elem->win->width);
}

static void
fmt_incheight (rp_window_elem *elem, struct sbuf *buf)
{
  int height;
  height = elem->win->height;

  if (elem->win->hints->flags & PResizeInc)
    height /= elem->win->hints->height_inc;

  sbuf_printf_concat (buf, "%d", height);
}

static void
fmt_incwidth (rp_window_elem *elem, struct sbuf *buf)
{
  int width;
  width = elem->win->width;

  if (elem->win->hints->flags & PResizeInc)
    width /= elem->win->hints->width_inc;

  sbuf_printf_concat (buf, "%d", width);
}

static void
fmt_gravity (rp_window_elem *elem, struct sbuf *buf)
{
  sbuf_copy (buf, wingravity_to_string (elem->win->gravity));
}

static void
fmt_screen (rp_window_elem *elem, struct sbuf *buf)
{
  sbuf_printf_concat (buf, "%d", elem->win->scr->screen_num);
}

static void
fmt_xrandrscreen (rp_window_elem *elem, struct sbuf *buf)
{
  sbuf_printf_concat (buf, "%d", elem->win->scr->xrandr.output);
}

static void
fmt_transient (rp_window_elem *elem, struct sbuf *buf)
{
  if (elem->win->transient)
    sbuf_concat (buf, "Transient");
}

static void
fmt_maxsize (rp_window_elem *elem, struct sbuf *buf)
{
  if (elem->win->hints->flags & PMaxSize)
    sbuf_concat (buf, "Maxsize");
}

static void
fmt_pid (rp_window_elem *elem, struct sbuf *buf)
{
  struct rp_child_info *info;

  info = get_child_info (elem->win->w);
  if (info)
    sbuf_printf_concat (buf, "%d", info->pid);
  else
    sbuf_concat (buf, "?");
}

#ifdef RUN_FORMAT_TEST

#include <assert.h>
#include <locale.h>

void test_sbuf_utf8_nconcat(void);

int main(void)
{
  /* We need to set up locale information for multibyte functions to work
     correctly. */
  setlocale(LC_ALL, "");

  test_sbuf_utf8_nconcat();
  return 0;
}

void test_sbuf_utf8_nconcat(void)
{
  struct sbuf *buf = NULL;

  /* Zero length string, no limit. */

  buf = sbuf_new(0);
  sbuf_utf8_nconcat(buf, "", -1);
  assert(strcmp(sbuf_get(buf), "") == 0);
  sbuf_free(buf);

  /* Zero length string, non-zero limit. */

  buf = sbuf_new(0);
  sbuf_utf8_nconcat(buf, "", 5);
  assert(strcmp(sbuf_get(buf), "") == 0);
  sbuf_free(buf);

  /* ASCII string, no limit. */

  buf = sbuf_new(0);
  sbuf_utf8_nconcat(buf, "hi there", -1);
  assert(strcmp(sbuf_get(buf), "hi there") == 0);
  sbuf_utf8_nconcat(buf, " you", -1);
  assert(strcmp(sbuf_get(buf), "hi there you") == 0);
  sbuf_free(buf);

  /* ASCII string, non-zero limit, truncated. */

  buf = sbuf_new(0);
  sbuf_utf8_nconcat(buf, "hi there", 4);
  assert(strcmp(sbuf_get(buf), "hi t") == 0);
  sbuf_free(buf);

  /* ASCII string, non-zero limit, not truncated. */

  buf = sbuf_new(0);
  sbuf_utf8_nconcat(buf, "hi", 4);
  assert(strcmp(sbuf_get(buf), "hi") == 0);
  sbuf_free(buf);

  /* UTF-8 string, no limit. */

  buf = sbuf_new(0);
  /* 0xe2 0x84 0xa2 is U+2122, the trademark symbol. */
  sbuf_utf8_nconcat(buf, "hi \xe2\x84\xa2 there", -1);
  assert(strcmp(sbuf_get(buf), "hi \xe2\x84\xa2 there") == 0);
  sbuf_free(buf);

  /* UTF-8 string, non-zero limit, truncated at an okay spot counting either
     by bytes or by characters. */

  buf = sbuf_new(0);
  sbuf_utf8_nconcat(buf, "hi \xe2\x84\xa2 there you", 11);
  assert(strcmp(sbuf_get(buf), "hi \xe2\x84\xa2 there ") == 0);
  sbuf_free(buf);

  /* UTF-8 string, non-zero limit, truncated such that if the limit were in
     bytes that we would cut in the middle of a character. */

  buf = sbuf_new(0);
  sbuf_utf8_nconcat(buf, "hi \xe2\x84\xa2 there you", 5);
  assert(strcmp(sbuf_get(buf), "hi \xe2\x84\xa2 ") == 0);
  sbuf_free(buf);

  /* UTF-8 string, non-zero limit, not truncated. */

  buf = sbuf_new(0);
  sbuf_utf8_nconcat(buf, "hi \xe2\x84\xa2 there you", 20);
  assert(strcmp(sbuf_get(buf), "hi \xe2\x84\xa2 there you") == 0);
  sbuf_free(buf);

  /* Invalid character. */

  buf = sbuf_new(0);
  /* This is an invalid UTF-8 sequence. It's missing 0xa2. */
  sbuf_utf8_nconcat(buf, "hi \xe2\x84 there you", 20);
  assert(strcmp(sbuf_get(buf), "hi ") == 0);
  sbuf_free(buf);
}

#endif