-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathUtils.cpp
298 lines (251 loc) · 7.36 KB
/
Utils.cpp
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
/*
* Copyright 2013-2023, Stefano Ceccherini <stefano.ceccherini@gmail.com>
* All rights reserved. Distributed under the terms of the MIT license.
*/
#include "Utils.h"
#include "Constants.h"
#include "Settings.h"
// Private Haiku header
#include "WindowInfo.h"
#include <Directory.h>
#include <Entry.h>
#include <Path.h>
#include <String.h>
#include <Window.h>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <iostream>
// TODO: Refactor
void
PrintMediaFormat(const media_format& format)
{
std::cout << "media format" << std::endl;
std::cout << "width: " << format.Width() << std::endl;
std::cout << "height: " << format.Height() << std::endl;
}
bool
IsFileFormatUsable(const media_file_format& format)
{
const uint32 neededMask = media_file_format::B_KNOWS_ENCODED_VIDEO
| media_file_format::B_WRITABLE;
if ((format.capabilities & neededMask) != neededMask)
return false;
// Known broken file formats in Haiku
static BString brokenFileFormats[] = {
"DV Movie",
"Ogg Audio",
"Ogg Video",
// TODO: blacklist AVI and Matroska since, as today, Haiku can't encode
// these formats correctly. Remove when it's fixed
"AVI",
"Matroska"
};
BString formatString = format.pretty_name;
for (size_t i = 0; i < sizeof(brokenFileFormats) / sizeof(brokenFileFormats[i]); i++) {
if (formatString.StartsWith(brokenFileFormats[i]))
return false;
};
return true;
}
bool
IsFFMPEGAvailable()
{
int result = system("type ffmpeg > /dev/null 2>&1");
return WEXITSTATUS(result) == 0;
}
// If prettyName is NULL, returns the first found media_file_format,
// otherwise returns the media_file_format with the same prettyName
// TODO: Use the short_name instead
bool
GetMediaFileFormat(const BString& prettyName, media_file_format* outFormat)
{
media_file_format mediaFileFormat;
int32 cookie = 0;
while (get_next_file_format(&cookie, &mediaFileFormat) == B_OK) {
if (!IsFileFormatUsable(mediaFileFormat))
continue;
if (prettyName == "" || prettyName == mediaFileFormat.pretty_name) {
*outFormat = mediaFileFormat;
return true;
}
}
if (prettyName == NULL_FORMAT_PRETTY_NAME) {
MakeNULLMediaFileFormat(*outFormat);
return true;
} else if (prettyName == GIF_FORMAT_PRETTY_NAME) {
MakeGIFMediaFileFormat(*outFormat);
return true;
}
return false;
}
void
MakeGIFMediaFileFormat(media_file_format& outFormat)
{
::strlcpy(outFormat.pretty_name, "GIF", sizeof(outFormat.pretty_name));
::strlcpy(outFormat.short_name, GIF_FORMAT_SHORT_NAME, sizeof(outFormat.short_name));
outFormat.capabilities = media_file_format::B_KNOWS_OTHER;
::strlcpy(outFormat.file_extension, "gif", sizeof(outFormat.file_extension));
outFormat.family = B_ANY_FORMAT_FAMILY;
}
void
MakeNULLMediaFileFormat(media_file_format& outFormat)
{
::strlcpy(outFormat.pretty_name, "Export frames as Bitmaps", sizeof(outFormat.pretty_name));
::strlcpy(outFormat.short_name, NULL_FORMAT_SHORT_NAME, sizeof(outFormat.short_name));
outFormat.capabilities = media_file_format::B_KNOWS_OTHER;
::strlcpy(outFormat.file_extension, "bmp", sizeof(outFormat.file_extension));
outFormat.family = B_ANY_FORMAT_FAMILY;
}
BPath
GetUniqueFileName(const BPath& filePath)
{
BString extension = filePath.Leaf();
int32 extensionIndex = extension.FindLast(".");
if (extensionIndex != -1)
extension.Remove(0, extensionIndex);
char timeSuffix[128];
time_t currentTime = ::time(NULL);
struct tm timeStruct;
struct tm* localTime = ::localtime_r(¤tTime, &timeStruct);
strftime(timeSuffix, sizeof(timeSuffix), "%Y-%m-%d", localTime);
BPath parent;
filePath.GetParent(&parent);
BEntry parentEntry(parent.Path());
BDirectory directory(&parentEntry);
BEntry newEntry(filePath.Path());
BString newName = filePath.Leaf();
int32 suffix = 0;
while (BEntry(&directory, newName).Exists()) {
newName = filePath.Leaf();
// remove extension (if any)
if (extension != "")
newName.RemoveLast(extension);
// remove suffix
newName.RemoveLast(timeSuffix);
newName.RemoveLast("-");
// add back time suffix
newName << '-' << timeSuffix;
// add suffix
if (suffix != 0)
newName << "-" << suffix;
// add back extension
if (extension != "")
newName << extension;
newEntry.SetTo(&directory, newName);
suffix++;
}
return BPath(&newEntry);
}
void
FixRect(BRect &rect, const BRect& maxRect,
const bool fixWidth, const bool fixHeight)
{
const static int kAlignAmount = 16;
if (rect.left < 0)
rect.left = 0;
if (rect.top < 0)
rect.top = 0;
if (rect.right > maxRect.right)
rect.right = maxRect.right;
if (rect.bottom > maxRect.bottom)
rect.bottom = maxRect.bottom;
// Adjust width and/or height to be a multiple of 16
// as some codecs create bad movies otherwise
int32 diffHorizontal = kAlignAmount - (rect.IntegerWidth() + 1) % kAlignAmount;
if (fixWidth && diffHorizontal != kAlignAmount) {
if (rect.left < diffHorizontal) {
diffHorizontal -= (int32)rect.left;
rect.left = 0;
} else {
rect.left -= diffHorizontal;
diffHorizontal = 0;
}
rect.right += diffHorizontal;
}
int32 diffVertical = kAlignAmount - (rect.IntegerHeight() + 1) % kAlignAmount;
if (fixHeight && diffVertical != kAlignAmount) {
if (rect.top < diffVertical) {
diffVertical -= (int32)rect.top;
rect.top = 0;
} else {
rect.top -= diffVertical;
diffVertical = 0;
}
rect.bottom += diffVertical;
}
}
void
GetWindowsFrameList(BObjectList<BRect> &framesList, int32 border)
{
int32 tokenCount = 0;
// Haiku does not have a public API to retrieve windows from other teams,
// so we have to use this.
int32 *tokenList = NULL;
status_t status = BPrivate::get_window_order(current_workspace(), &tokenList, &tokenCount);
if (status == B_OK) {
for (int32 i = 0; i < tokenCount; i++) {
client_window_info* info = get_window_info(tokenList[i]);
if (info != NULL) {
if (info->layer >= 3 && !info->is_mini && info->show_hide_level == 0) {
BRect* rect = new BRect(info->window_left, info->window_top, info->window_right, info->window_bottom);
rect->InsetBy(-border, -border);
framesList.AddItem(rect);
}
::free(info);
}
}
::free(tokenList);
}
}
int32
GetWindowTokenForFrame(BRect frame, int32 border)
{
int32 tokenCount = 0;
int32 *tokenList = NULL;
int32 token = -1;
frame.InsetBy(border, border);
status_t status = BPrivate::get_window_order(current_workspace(), &tokenList, &tokenCount);
if (status == B_OK) {
for (int32 i = 0; i < tokenCount && token == -1; i++) {
client_window_info* info = get_window_info(tokenList[i]);
if (info != NULL) {
if (info->layer >= 3 && !info->is_mini && info->show_hide_level == 0) {
BRect rect(info->window_left, info->window_top, info->window_right, info->window_bottom);
if (rect == frame)
token = tokenList[i];
}
::free(info);
}
}
::free(tokenList);
}
return token;
}
BRect
GetWindowFrameForToken(int32 token, int32 border)
{
BRect rect;
client_window_info* info = get_window_info(token);
if (info != NULL) {
rect.Set(info->window_left, info->window_top, info->window_right, info->window_bottom);
rect.InsetBy(-border, -border);
::free(info);
}
return rect;
}
float
CalculateFPS(const uint32& numFrames, const bigtime_t& elapsedTime)
{
// Don't ever return negative fps
return std::max(float(1000000 * uint64(numFrames)) / elapsedTime, float(0));
}
uint64
GetFreeMemory()
{
system_info info;
if (get_system_info(&info) != B_OK)
return 0;
return info.free_memory;
}