2 #include <X11/Xft/Xft.h>
10 #include <semaphore.h>
17 static void die(const char *format
, ...)
21 vfprintf(stderr
, format
, ap
);
22 fprintf(stderr
, "\n");
27 int get_max_len(char *body
, XftFont
*font
, int max_text_width
)
29 int eol
= strlen(body
);
31 XftTextExtentsUtf8(display
, font
, (FcChar8
*)body
, eol
, &info
);
33 if (info
.width
> max_text_width
)
36 eol
= max_text_width
/ font
->max_advance_width
;
39 while (info
.width
< max_text_width
)
42 XftTextExtentsUtf8(display
, font
, (FcChar8
*)body
, eol
, &info
);
48 for (int i
= 0; i
< eol
; i
++)
49 if (body
[i
] == '\n') {
54 if (info
.width
< max_text_width
)
59 while (body
[eol
] != ' ' && eol
)
71 event
.type
= ButtonPress
;
72 XSendEvent(display
, window
, 0, 0, &event
);
76 int main(int argc
, char *argv
[])
79 die("Usage: %s body", argv
[0]);
81 signal(SIGALRM
, expire
);
82 signal(SIGTERM
, expire
);
83 signal(SIGINT
, expire
);
85 display
= XOpenDisplay(0);
88 die("Cannot open display");
90 int screen
= DefaultScreen(display
);
91 Visual
*visual
= DefaultVisual(display
, screen
);
92 Colormap colormap
= DefaultColormap(display
, screen
);
94 int screen_width
= DisplayWidth(display
, screen
);
95 int screen_height
= DisplayHeight(display
, screen
);
99 XSetWindowAttributes attributes
;
100 attributes
.override_redirect
= True
;
101 XftColorAllocName(display
, visual
, colormap
, background_color
, &color
);
102 attributes
.background_pixel
= color
.pixel
;
103 XftColorAllocName(display
, visual
, colormap
, border_color
, &color
);
104 attributes
.border_pixel
= color
.pixel
;
106 XftFont
*font
= XftFontOpenName(display
, screen
, font_pattern
);
108 int num_of_lines
= 0;
109 int max_text_width
= width
- 2 * padding
;
111 char **words
= malloc(words_size
* sizeof(char *));
113 die("malloc failed");
115 for (int i
= 1; i
< argc
; i
++)
117 char *body
= argv
[i
];
119 for (unsigned int eol
= get_max_len(body
, font
, max_text_width
); eol
<= strlen(body
) && eol
; body
+= eol
, num_of_lines
++, eol
= get_max_len(body
, font
, max_text_width
))
121 if (words_size
<= num_of_lines
)
123 words
= realloc(words
, (words_size
+= 5) * sizeof(char *));
125 die("malloc failed");
127 words
[num_of_lines
] = malloc((eol
+ 1) * sizeof(char));
128 if (!words
[num_of_lines
])
129 die("malloc failed");
130 strncpy(words
[num_of_lines
], body
, eol
);
131 words
[num_of_lines
][eol
] = '\0';
135 unsigned int x
= pos_x
;
136 unsigned int y
= pos_y
;
137 unsigned int text_height
= font
->ascent
- font
->descent
;
138 unsigned int height
= (num_of_lines
- 1) * line_spacing
+ num_of_lines
* text_height
+ 2 * padding
;
140 if (corner
== TOP_RIGHT
|| corner
== BOTTOM_RIGHT
)
141 x
= screen_width
- width
- border_size
* 2 - pos_x
;
143 if (corner
== BOTTOM_LEFT
|| corner
== BOTTOM_RIGHT
)
144 y
= screen_height
- height
- border_size
* 2 - pos_y
;
146 window
= XCreateWindow(display
, RootWindow(display
, screen
), x
, y
, width
, height
, border_size
, DefaultDepth(display
, screen
), CopyFromParent
, visual
,
147 CWOverrideRedirect
| CWBackPixel
| CWBorderPixel
, &attributes
);
149 XftDraw
*draw
= XftDrawCreate(display
, window
, visual
, colormap
);
150 XftColorAllocName(display
, visual
, colormap
, font_color
, &color
);
152 XSelectInput(display
, window
, ExposureMask
| ButtonPress
);
154 XMapWindow(display
, window
);
156 sem_t
*mutex
= sem_open("/herbe", O_CREAT
, 0644, 1);
165 XNextEvent(display
, &event
);
167 if (event
.type
== Expose
)
169 XClearWindow(display
, window
);
170 for (int i
= 0; i
< num_of_lines
; i
++)
171 XftDrawStringUtf8(draw
, &color
, font
, padding
, line_spacing
* i
+ text_height
* (i
+ 1) + padding
, (FcChar8
*)words
[i
], strlen(words
[i
]));
173 if (event
.type
== ButtonPress
)
180 for (int i
= 0; i
< num_of_lines
; i
++)
184 XftDrawDestroy(draw
);
185 XftColorFree(display
, visual
, colormap
, &color
);
186 XftFontClose(display
, font
);
187 XCloseDisplay(display
);