2 #include <X11/Xft/Xft.h>
10 #include <semaphore.h>
18 int exit_code
= EXIT_SUCCESS
;
20 static void die(const char *format
, ...)
24 vfprintf(stderr
, format
, ap
);
25 fprintf(stderr
, "\n");
30 int get_max_len(char *body
, XftFont
*font
, int max_text_width
)
32 int eol
= strlen(body
);
34 XftTextExtentsUtf8(display
, font
, (FcChar8
*)body
, eol
, &info
);
36 if (info
.width
> max_text_width
)
38 eol
= max_text_width
/ font
->max_advance_width
;
41 while (info
.width
< max_text_width
)
44 XftTextExtentsUtf8(display
, font
, (FcChar8
*)body
, eol
, &info
);
50 for (int i
= 0; i
< eol
; i
++)
57 if (info
.width
< max_text_width
)
62 while (body
[eol
] != ' ' && eol
)
74 event
.type
= ButtonPress
;
75 event
.xbutton
.button
= (sig
== SIGUSR2
) ? (ACTION_BUTTON
) : (DISMISS_BUTTON
);
76 XSendEvent(display
, window
, 0, 0, &event
);
80 int main(int argc
, char *argv
[])
85 die("Usage: %s body", argv
[0]);
88 struct sigaction act_expire
, act_ignore
;
89 act_expire
.sa_handler
= expire
;
90 act_expire
.sa_flags
= SA_RESTART
;
91 sigemptyset(&act_expire
.sa_mask
);
93 act_ignore
.sa_handler
= SIG_IGN
;
94 act_ignore
.sa_flags
= 0;
95 sigemptyset(&act_ignore
.sa_mask
);
97 sigaction(SIGALRM
, &act_expire
, 0);
98 sigaction(SIGTERM
, &act_expire
, 0);
99 sigaction(SIGINT
, &act_expire
, 0);
101 sigaction(SIGUSR1
, &act_ignore
, 0);
102 sigaction(SIGUSR2
, &act_ignore
, 0);
104 display
= XOpenDisplay(0);
107 die("Cannot open display");
109 int screen
= DefaultScreen(display
);
110 Visual
*visual
= DefaultVisual(display
, screen
);
111 Colormap colormap
= DefaultColormap(display
, screen
);
113 int screen_width
= DisplayWidth(display
, screen
);
114 int screen_height
= DisplayHeight(display
, screen
);
118 XSetWindowAttributes attributes
;
119 attributes
.override_redirect
= True
;
120 XftColorAllocName(display
, visual
, colormap
, background_color
, &color
);
121 attributes
.background_pixel
= color
.pixel
;
122 XftColorAllocName(display
, visual
, colormap
, border_color
, &color
);
123 attributes
.border_pixel
= color
.pixel
;
125 XftFont
*font
= XftFontOpenName(display
, screen
, font_pattern
);
127 int num_of_lines
= 0;
128 int max_text_width
= width
- 2 * padding
;
130 char **words
= malloc(words_size
* sizeof(char *));
132 die("malloc failed");
134 for (int i
= 1; i
< argc
; i
++)
136 char *body
= argv
[i
];
138 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
))
140 if (words_size
<= num_of_lines
)
142 words
= realloc(words
, (words_size
+= 5) * sizeof(char *));
144 die("malloc failed");
146 words
[num_of_lines
] = malloc((eol
+ 1) * sizeof(char));
147 if (!words
[num_of_lines
])
148 die("malloc failed");
149 strncpy(words
[num_of_lines
], body
, eol
);
150 words
[num_of_lines
][eol
] = '\0';
154 unsigned int x
= pos_x
;
155 unsigned int y
= pos_y
;
156 unsigned int text_height
= font
->ascent
- font
->descent
;
157 unsigned int height
= (num_of_lines
- 1) * line_spacing
+ num_of_lines
* text_height
+ 2 * padding
;
159 if (corner
== TOP_RIGHT
|| corner
== BOTTOM_RIGHT
)
160 x
= screen_width
- width
- border_size
* 2 - pos_x
;
162 if (corner
== BOTTOM_LEFT
|| corner
== BOTTOM_RIGHT
)
163 y
= screen_height
- height
- border_size
* 2 - pos_y
;
165 window
= XCreateWindow(display
, RootWindow(display
, screen
), x
, y
, width
, height
, border_size
, DefaultDepth(display
, screen
), CopyFromParent
, visual
,
166 CWOverrideRedirect
| CWBackPixel
| CWBorderPixel
, &attributes
);
168 XftDraw
*draw
= XftDrawCreate(display
, window
, visual
, colormap
);
169 XftColorAllocName(display
, visual
, colormap
, font_color
, &color
);
171 XSelectInput(display
, window
, ExposureMask
| ButtonPress
);
173 XMapWindow(display
, window
);
175 sem_t
*mutex
= sem_open("/herbe", O_CREAT
, 0644, 1);
178 sigaction(SIGUSR1
, &act_expire
, 0);
179 sigaction(SIGUSR2
, &act_expire
, 0);
187 XNextEvent(display
, &event
);
189 if (event
.type
== Expose
)
191 XClearWindow(display
, window
);
192 for (int i
= 0; i
< num_of_lines
; i
++)
193 XftDrawStringUtf8(draw
, &color
, font
, padding
, line_spacing
* i
+ text_height
* (i
+ 1) + padding
, (FcChar8
*)words
[i
], strlen(words
[i
]));
195 if (event
.type
== ButtonPress
)
197 if (event
.xbutton
.button
== DISMISS_BUTTON
)
199 else if (event
.xbutton
.button
== ACTION_BUTTON
)
201 exit_code
= EXIT_ACTION
;
210 for (int i
= 0; i
< num_of_lines
; i
++)
214 XftDrawDestroy(draw
);
215 XftColorFree(display
, visual
, colormap
, &color
);
216 XftFontClose(display
, font
);
217 XCloseDisplay(display
);