i3
fake_outputs.c
Go to the documentation of this file.
1 /*
2  * vim:ts=4:sw=4:expandtab
3  *
4  * i3 - an improved dynamic tiling window manager
5  * © 2009 Michael Stapelberg and contributors (see also: LICENSE)
6  *
7  * Faking outputs is useful in pathological situations (like network X servers
8  * which don’t support multi-monitor in a useful way) and for our testsuite.
9  *
10  */
11 #include "all.h"
12 
13 static int num_screens;
14 
15 /*
16  * Looks in outputs for the Output whose start coordinates are x, y
17  *
18  */
19 static Output *get_screen_at(unsigned int x, unsigned int y) {
20  Output *output;
21  TAILQ_FOREACH (output, &outputs, outputs) {
22  if (output->rect.x == x && output->rect.y == y) {
23  return output;
24  }
25  }
26 
27  return NULL;
28 }
29 
30 /*
31  * Creates outputs according to the given specification.
32  * The specification must be in the format wxh+x+y, for example 1024x768+0+0,
33  * optionally followed by 'P' to indicate a primary output,
34  * with multiple outputs separated by commas:
35  * 1900x1200+0+0P,1280x1024+1900+0
36  *
37  */
38 void fake_outputs_init(const char *output_spec) {
39  const char *walk = output_spec;
40  unsigned int x, y, width, height;
41  int chars_consumed;
42  while (sscanf(walk, "%ux%u+%u+%u%n", &width, &height, &x, &y, &chars_consumed) == 4) {
43  walk += chars_consumed;
44  bool primary = false;
45  if (*walk == 'P') {
46  primary = true;
47  walk++;
48  }
49  if (*walk == ',')
50  walk++; /* Skip delimiter */
51  DLOG("Parsed output as width = %u, height = %u at (%u, %u)%s\n",
52  width, height, x, y, primary ? " (primary)" : "");
53 
54  Output *new_output = get_screen_at(x, y);
55  if (new_output != NULL) {
56  DLOG("Re-used old output %p\n", new_output);
57  /* This screen already exists. We use the littlest screen so that the user
58  can always see the complete workspace */
59  new_output->rect.width = min(new_output->rect.width, width);
60  new_output->rect.height = min(new_output->rect.height, height);
61  } else {
62  struct output_name *output_name = scalloc(1, sizeof(struct output_name));
63  new_output = scalloc(1, sizeof(Output));
64  sasprintf(&(output_name->name), "fake-%d", num_screens);
65  SLIST_INIT(&(new_output->names_head));
66  SLIST_INSERT_HEAD(&(new_output->names_head), output_name, names);
67  DLOG("Created new fake output %s (%p)\n", output_primary_name(new_output), new_output);
68  new_output->active = true;
69  new_output->rect.x = x;
70  new_output->rect.y = y;
71  new_output->rect.width = width;
72  new_output->rect.height = height;
73  /* We always treat the screen at 0x0 as the primary screen */
74  if (new_output->rect.x == 0 && new_output->rect.y == 0)
75  TAILQ_INSERT_HEAD(&outputs, new_output, outputs);
76  else
77  TAILQ_INSERT_TAIL(&outputs, new_output, outputs);
78  output_init_con(new_output);
79  init_ws_for_output(new_output);
80  num_screens++;
81  }
82  new_output->primary = primary;
83  }
84 
85  if (num_screens == 0) {
86  ELOG("No screens found. Please fix your setup. i3 will exit now.\n");
87  exit(EXIT_FAILURE);
88  }
89 }
#define y(x,...)
Definition: commands.c:18
static Output * get_screen_at(unsigned int x, unsigned int y)
Definition: fake_outputs.c:19
void fake_outputs_init(const char *output_spec)
Creates outputs according to the given specification.
Definition: fake_outputs.c:38
static int num_screens
Definition: fake_outputs.c:13
char * output_primary_name(Output *output)
Retrieves the primary name of an output.
Definition: output.c:53
void init_ws_for_output(Output *output)
Initializes at least one workspace for this output, trying the following steps until there is at leas...
Definition: randr.c:437
void output_init_con(Output *output)
Initializes a CT_OUTPUT Con (searches existing ones from inplace restart before) to use for the given...
Definition: randr.c:329
struct outputs_head outputs
Definition: randr.c:22
xcb_randr_get_output_primary_reply_t * primary
Definition: randr.c:19
int min(int a, int b)
Definition: util.c:24
#define DLOG(fmt,...)
Definition: libi3.h:105
#define ELOG(fmt,...)
Definition: libi3.h:100
int sasprintf(char **strp, const char *fmt,...)
Safe-wrapper around asprintf which exits if it returns -1 (meaning that there is no more memory avail...
void * scalloc(size_t num, size_t size)
Safe-wrapper around calloc which exits if malloc returns NULL (meaning that there is no more memory a...
#define TAILQ_FOREACH(var, head, field)
Definition: queue.h:347
#define SLIST_INIT(head)
Definition: queue.h:127
#define SLIST_INSERT_HEAD(head, elm, field)
Definition: queue.h:138
#define TAILQ_INSERT_TAIL(head, elm, field)
Definition: queue.h:376
#define TAILQ_INSERT_HEAD(head, elm, field)
Definition: queue.h:366
uint32_t height
Definition: data.h:160
uint32_t x
Definition: data.h:157
uint32_t y
Definition: data.h:158
uint32_t width
Definition: data.h:159
char * name
Definition: data.h:350
An Output is a physical output on your graphics driver.
Definition: data.h:361
bool active
Whether the output is currently active (has a CRTC attached with a valid mode)
Definition: data.h:367
bool primary
Definition: data.h:373
Rect rect
x, y, width, height
Definition: data.h:384