00001
00002 #include <iostream>
00003 using namespace std;
00004
00005
00006 #include <cerrno>
00007 #include <unistd.h>
00008 #include <stdlib.h>
00009 #include <fcntl.h>
00010
00011
00012 #include "compat.h"
00013 #ifdef USING_MINGW
00014 # include <sys/types.h>
00015 # include <sys/stat.h>
00016 # include <sys/param.h>
00017 #else
00018 # include <sys/types.h>
00019 # include <sys/wait.h>
00020 # include <sys/stat.h>
00021 # ifdef linux
00022 # include <sys/vfs.h>
00023 # include <sys/statvfs.h>
00024 # include <sys/sysinfo.h>
00025 # else
00026 # include <sys/param.h>
00027 # ifdef __FreeBSD__
00028 # include <sys/mount.h>
00029 # endif
00030 # ifdef CONFIG_CYGWIN
00031 # include <sys/statfs.h>
00032 # else // if !CONFIG_CYGWIN
00033 # include <sys/sysctl.h>
00034 # endif // !CONFIG_CYGWIN
00035 # endif
00036 #endif //MINGW
00037
00038
00039 #include <qapplication.h>
00040 #include <qimage.h>
00041 #include <qpainter.h>
00042 #include <qpixmap.h>
00043 #include <qfont.h>
00044 #include <qfile.h>
00045 #include <qdeepcopy.h>
00046
00047
00048 #include "mythconfig.h"
00049 #include "exitcodes.h"
00050 #include "util.h"
00051 #include "util-x11.h"
00052 #include "mythcontext.h"
00053 #include "mythmediamonitor.h"
00054
00055 #ifdef CONFIG_DARWIN
00056 #include <mach/mach.h>
00057 #include <sys/mount.h>
00058 #endif
00059
00060 #ifdef USE_LIRC
00061 #include "lircevent.h"
00062 #endif
00063
00064 #ifdef USE_JOYSTICK_MENU
00065 #include "jsmenuevent.h"
00066 #endif
00067
00068 #include "mythconfig.h"
00069
00073 QDateTime mythCurrentDateTime()
00074 {
00075 QDateTime rettime = QDateTime::currentDateTime();
00076 QTime orig = rettime.time();
00077 rettime.setTime(orig.addMSecs(-orig.msec()));
00078 return rettime;
00079 }
00080
00081 int calc_utc_offset(void)
00082 {
00083 QDateTime loc = QDateTime::currentDateTime(Qt::LocalTime);
00084 QDateTime utc = QDateTime::currentDateTime(Qt::UTC);
00085
00086 int utc_offset = MythSecsTo(utc, loc);
00087
00088
00089 int off = utc_offset % 60;
00090 if (abs(off) < 10)
00091 utc_offset -= off;
00092 if (off < -50 && off > -60)
00093 utc_offset -= 60 + off;
00094 if (off > +50 && off < +60)
00095 utc_offset += 60 - off;
00096
00097 return utc_offset;
00098 }
00099
00111 void encodeLongLong(QStringList &list, long long num)
00112 {
00113 list << QString::number((int)(num >> 32));
00114 list << QString::number((int)(num & 0xffffffffLL));
00115 }
00116
00134 long long decodeLongLong(QStringList &list, uint offset)
00135 {
00136 long long retval = 0;
00137 if (offset >= list.size())
00138 {
00139 VERBOSE(VB_IMPORTANT,
00140 "decodeLongLong() called with offset >= list size.");
00141 return retval;
00142 }
00143
00144 int l1 = list[offset].toInt();
00145 int l2 = list[offset + 1].toInt();
00146
00147 retval = ((long long)(l2) & 0xffffffffLL) | ((long long)(l1) << 32);
00148
00149 return retval;
00150 }
00151
00165 long long decodeLongLong(QStringList &list, QStringList::const_iterator &it)
00166 {
00167 (void)list;
00168
00169 long long retval = 0;
00170
00171 bool ok = true;
00172 int l1=0, l2=0;
00173
00174 if (it == list.end())
00175 ok = false;
00176 else
00177 l1 = (*(it++)).toInt();
00178
00179 if (it == list.end())
00180 ok = false;
00181 else
00182 l2 = (*(it++)).toInt();
00183
00184 if (!ok)
00185 {
00186 VERBOSE(VB_IMPORTANT,
00187 "decodeLongLong() called with the iterator too close "
00188 "to the end of the list.");
00189 return 0;
00190 }
00191
00192 retval = ((long long)(l2) & 0xffffffffLL) | ((long long)(l1) << 32);
00193
00194 return retval;
00195 }
00196
00200 QRgb blendColors(QRgb source, QRgb add, int alpha)
00201 {
00202 int sred = qRed(source);
00203 int sgreen = qGreen(source);
00204 int sblue = qBlue(source);
00205
00206 int tmp1 = (qRed(add) - sred) * alpha;
00207 int tmp2 = sred + ((tmp1 + (tmp1 >> 8) + 0x80) >> 8);
00208 sred = tmp2 & 0xff;
00209
00210 tmp1 = (qGreen(add) - sgreen) * alpha;
00211 tmp2 = sgreen + ((tmp1 + (tmp1 >> 8) + 0x80) >> 8);
00212 sgreen = tmp2 & 0xff;
00213
00214 tmp1 = (qBlue(add) - sblue) * alpha;
00215 tmp2 = sblue + ((tmp1 + (tmp1 >> 8) + 0x80) >> 8);
00216 sblue = tmp2 & 0xff;
00217
00218 return qRgb(sred, sgreen, sblue);
00219 }
00220
00227 uint myth_system(const QString &command, int flags)
00228 {
00229 (void)flags;
00230
00231 bool ready_to_lock = gContext && gContext->GetMainWindow();
00232
00233 (void)ready_to_lock;
00234 #ifdef USE_LIRC
00235 bool lirc_lock_flag = !(flags & MYTH_SYSTEM_DONT_BLOCK_LIRC);
00236 LircEventLock lirc_lock(lirc_lock_flag && ready_to_lock);
00237 #endif
00238
00239 #ifdef USE_JOYSTICK_MENU
00240 bool joy_lock_flag = !(flags & MYTH_SYSTEM_DONT_BLOCK_JOYSTICK_MENU);
00241 JoystickMenuEventLock joystick_lock(joy_lock_flag && ready_to_lock);
00242 #endif
00243
00244 #ifndef USING_MINGW
00245 pid_t child = fork();
00246
00247 if (child < 0)
00248 {
00249
00250 VERBOSE(VB_IMPORTANT,
00251 QString("myth_system(): Error, fork() failed because %1")
00252 .arg(strerror(errno)));
00253 return GENERIC_EXIT_NOT_OK;
00254 }
00255 else if (child == 0)
00256 {
00257
00258
00259 for (int i = sysconf(_SC_OPEN_MAX) - 1; i > 2; i--)
00260 close(i);
00261
00262
00263 close(0);
00264 int fd = open("/dev/null", O_RDONLY);
00265 dup2(fd, 0);
00266 if (fd != 0)
00267 close(fd);
00268
00269
00270 execl("/bin/sh", "sh", "-c", QString(command.utf8()).ascii(), NULL);
00271 if (errno)
00272 {
00273 VERBOSE(VB_IMPORTANT,
00274 QString("myth_system(): Error, execl() failed because %1")
00275 .arg(strerror(errno)));
00276 }
00277
00278
00279 _exit(MYTHSYSTEM__EXIT__EXECL_ERROR);
00280 }
00281 else
00282 {
00283
00284 int status;
00285
00286 if (flags & MYTH_SYSTEM_DONT_BLOCK_PARENT)
00287 {
00288 int res = 0;
00289
00290 while (res == 0)
00291 {
00292 res = waitpid(child, &status, WNOHANG);
00293 if (res == -1)
00294 {
00295 VERBOSE(VB_IMPORTANT,
00296 QString("myth_system(): Error, waitpid() failed because %1")
00297 .arg(strerror(errno)));
00298 return GENERIC_EXIT_NOT_OK;
00299 }
00300
00301 qApp->processEvents();
00302
00303 if (res > 0)
00304 return WEXITSTATUS(status);
00305
00306 usleep(100000);
00307 }
00308 }
00309 else
00310 {
00311 if (waitpid(child, &status, 0) < 0)
00312 {
00313 VERBOSE(VB_IMPORTANT,
00314 QString("myth_system(): Error, waitpid() failed because %1")
00315 .arg(strerror(errno)));
00316 return GENERIC_EXIT_NOT_OK;
00317 }
00318 return WEXITSTATUS(status);
00319 }
00320 }
00321
00322 return GENERIC_EXIT_NOT_OK;
00323 }
00324 #else
00325 STARTUPINFO si;
00326 PROCESS_INFORMATION pi;
00327 memset(&si, 0, sizeof(si));
00328 memset(&pi, 0, sizeof(pi));
00329 si.cb = sizeof(si);
00330 QString cmd = QString("cmd.exe /c %1").arg(command.utf8()).ascii();
00331 char* ch = new char[cmd.length() + 1];
00332 strcpy(ch, cmd);
00333 if (!::CreateProcessA(NULL, ch, NULL, NULL, FALSE, CREATE_NO_WINDOW, NULL, NULL, &si, &pi)) {
00334 delete[] ch;
00335 VERBOSE(VB_IMPORTANT,
00336 QString("myth_system(): Error, CreateProcess() failed because %1")
00337 .arg(::GetLastError()));
00338 return MYTHSYSTEM__EXIT__EXECL_ERROR;
00339 } else {
00340 delete[] ch;
00341 if (::WaitForSingleObject(pi.hProcess, INFINITE) == WAIT_FAILED)
00342 VERBOSE(VB_IMPORTANT,
00343 QString("myth_system(): Error, WaitForSingleObject() failed because %1")
00344 .arg(::GetLastError()));
00345 CloseHandle(pi.hProcess);
00346 CloseHandle(pi.hThread);
00347 return GENERIC_EXIT_OK;
00348 }
00349 return GENERIC_EXIT_NOT_OK;
00350 }
00351 #endif
00352
00356 QString cutDownString(const QString &text, QFont *testFont, uint maxwidth)
00357 {
00358 QFontMetrics fm(*testFont);
00359
00360 uint curFontWidth = fm.width(text);
00361 if (curFontWidth > maxwidth)
00362 {
00363 QString testInfo = "";
00364 curFontWidth = fm.width(testInfo);
00365 int tmaxwidth = maxwidth - fm.width("LLL");
00366 int count = 0;
00367
00368 while ((int)curFontWidth < tmaxwidth)
00369 {
00370 testInfo = text.left(count);
00371 curFontWidth = fm.width(testInfo);
00372 count = count + 1;
00373 }
00374
00375 testInfo = testInfo + "...";
00376 return testInfo;
00377 }
00378
00379 return text;
00380 }
00381
00385 int MythSecsTo(const QDateTime &from, const QDateTime &to)
00386 {
00387 return (from.time().secsTo(to.time()) +
00388 from.date().daysTo(to.date()) * 60 * 60 * 24);
00389 }
00390
00394 QDateTime MythUTCToLocal(const QDateTime &utc)
00395 {
00396 QDateTime local = QDateTime(QDate(1970, 1, 1));
00397
00398 int timesecs = MythSecsTo(local, utc);
00399 QDateTime localdt;
00400 localdt.setTime_t(timesecs);
00401
00402 return localdt;
00403 }
00404
00410 long long stringToLongLong(const QString &str)
00411 {
00412 long long retval = 0;
00413 if (str != QString::null)
00414 {
00415 retval = strtoll(str.ascii(), NULL, 0);
00416 }
00417 return retval;
00418 }
00419
00425 QString longLongToString(long long ll)
00426 {
00427 char str[21];
00428 snprintf(str, 20, "%lld", ll);
00429 str[20] = '\0';
00430 return str;
00431 }
00432
00438 bool getUptime(time_t &uptime)
00439 {
00440 #ifdef __linux__
00441 struct sysinfo sinfo;
00442 if (sysinfo(&sinfo) == -1)
00443 {
00444 VERBOSE(VB_IMPORTANT, "sysinfo() error");
00445 return false;
00446 }
00447 else
00448 uptime = sinfo.uptime;
00449
00450 #elif defined(__FreeBSD__) || defined(CONFIG_DARWIN)
00451
00452 int mib[2];
00453 struct timeval bootTime;
00454 size_t len;
00455
00456
00457
00458 len = sizeof(bootTime);
00459 mib[0] = CTL_KERN;
00460 mib[1] = KERN_BOOTTIME;
00461 if (sysctl(mib, 2, &bootTime, &len, NULL, 0) == -1)
00462 {
00463 VERBOSE(VB_IMPORTANT, "sysctl() error");
00464 return false;
00465 }
00466 else
00467 uptime = time(NULL) - bootTime.tv_sec;
00468 #elif defined(USING_MINGW)
00469 uptime = ::GetTickCount() / 1000;
00470 #else
00471
00472 VERBOSE(VB_IMPORTANT, "Unknown platform. How do I get the uptime?");
00473 return false;
00474 #endif
00475
00476 return true;
00477 }
00478
00484 long long getDiskSpace(const QString &file_on_disk,
00485 long long &total, long long &used)
00486 {
00487 struct statfs statbuf;
00488 bzero(&statbuf, sizeof(statbuf));
00489 long long freespace = -1;
00490 QCString cstr = file_on_disk.local8Bit();
00491
00492 total = used = -1;
00493
00494
00495
00496
00497
00498 if ((statfs(cstr, &statbuf) == 0) &&
00499 (statbuf.f_blocks > 0) &&
00500 (statbuf.f_bsize > 0))
00501 {
00502 total = statbuf.f_blocks;
00503 total *= statbuf.f_bsize;
00504 total = total >> 10;
00505
00506 freespace = statbuf.f_bavail;
00507 freespace *= statbuf.f_bsize;
00508 freespace = freespace >> 10;
00509
00510 used = total - freespace;
00511 }
00512
00513 return freespace;
00514 }
00515
00522 bool getMemStats(int &totalMB, int &freeMB, int &totalVM, int &freeVM)
00523 {
00524 #ifdef __linux__
00525 size_t MB = (1024*1024);
00526 struct sysinfo sinfo;
00527 if (sysinfo(&sinfo) == -1)
00528 {
00529 VERBOSE(VB_IMPORTANT, "getMemStats(): Error, sysinfo() call failed.");
00530 return false;
00531 }
00532 else
00533 totalMB = (int)((sinfo.totalram * sinfo.mem_unit)/MB),
00534 freeMB = (int)((sinfo.freeram * sinfo.mem_unit)/MB),
00535 totalVM = (int)((sinfo.totalswap * sinfo.mem_unit)/MB),
00536 freeVM = (int)((sinfo.freeswap * sinfo.mem_unit)/MB);
00537
00538 #elif defined(CONFIG_DARWIN)
00539 mach_port_t mp;
00540 mach_msg_type_number_t count, pageSize;
00541 vm_statistics_data_t s;
00542
00543 mp = mach_host_self();
00544
00545
00546 if (host_page_size(mp, &pageSize) != KERN_SUCCESS)
00547 pageSize = 4096;
00548
00549 count = HOST_VM_INFO_COUNT;
00550 if (host_statistics(mp, HOST_VM_INFO,
00551 (host_info_t)&s, &count) != KERN_SUCCESS)
00552 {
00553 VERBOSE(VB_IMPORTANT, "getMemStats(): Error, "
00554 "failed to get virtual memory statistics.");
00555 return false;
00556 }
00557
00558 pageSize >>= 10;
00559 totalMB = (s.active_count + s.inactive_count
00560 + s.wire_count + s.free_count) * pageSize / 1024;
00561 freeMB = s.free_count * pageSize / 1024;
00562
00563
00564
00565
00566
00567 long long total, used, free;
00568 free = getDiskSpace("/private/var/vm", total, used);
00569 totalVM = (int)(total/1024LL), freeVM = (int)(free/1024LL);
00570
00571 #else
00572 VERBOSE(VB_IMPORTANT, "getMemStats(): Unknown platform. "
00573 "How do I get the memory stats?");
00574 return false;
00575 #endif
00576
00577 return true;
00578 }
00579
00586 void myth_eject()
00587 {
00588 MediaMonitor *mon = MediaMonitor::GetMediaMonitor();
00589 if (mon)
00590 mon->ChooseAndEjectMedia();
00591 else
00592 {
00593 VERBOSE(VB_MEDIA, "CD/DVD Monitor isn't enabled.");
00594 #ifdef __linux__
00595 VERBOSE(VB_MEDIA, "Trying Linux 'eject -T' command");
00596 myth_system("eject -T");
00597 #elif defined(CONFIG_DARWIN)
00598 VERBOSE(VB_MEDIA, "Trying 'disktool -e disk1");
00599 myth_system("disktool -e disk1");
00600 #endif
00601 }
00602 }
00603
00611 bool hasUtf8(const char *str)
00612 {
00613 const uchar *c = (uchar *) str;
00614
00615 while (*c++)
00616 {
00617
00618
00619
00620
00621 if (*c > 0xC1 && *c < 0xF5)
00622 {
00623 int bytesToCheck = 2;
00624
00625 if (*c > 0xDF)
00626 ++bytesToCheck;
00627 if (*c > 0xEF)
00628 ++bytesToCheck;
00629
00630 while (bytesToCheck--)
00631 {
00632 ++c;
00633
00634 if (! *c)
00635 return false;
00636
00637 if (*c < 0x80 || *c > 0xBF)
00638 break;
00639 }
00640
00641 if (!bytesToCheck)
00642 return true;
00643 }
00644 }
00645
00646 return false;
00647 }
00648
00649 #ifdef USING_MINGW
00650 u_short in_cksum(u_short *addr, int len)
00651 {
00652 register int nleft = len;
00653 register u_short *w = addr;
00654 register u_short answer;
00655 register int sum = 0;
00656
00657
00658
00659
00660
00661
00662
00663 while( nleft > 1 ) {
00664 sum += *w++;
00665 nleft -= 2;
00666 }
00667
00668
00669 if( nleft == 1 ) {
00670 u_short u = 0;
00671
00672 *(u_char *)(&u) = *(u_char *)w ;
00673 sum += u;
00674 }
00675
00676
00677
00678
00679 sum = (sum >> 16) + (sum & 0xffff);
00680 sum += (sum >> 16);
00681 answer = ~sum;
00682 return (answer);
00683 }
00684 #endif
00685
00689 bool ping(const QString &host, int timeout)
00690 {
00691 #ifdef USING_MINGW
00692 VERBOSE(VB_SOCKET, QString("Ping: pinging %1 (%2 seconds max)").arg(host).arg(timeout));
00693 SOCKET rawSocket;
00694 LPHOSTENT lpHost;
00695 struct sockaddr_in saDest;
00696
00697 #define ICMP_ECHOREPLY 0
00698 #define ICMP_ECHOREQ 8
00699 struct IPHDR {
00700 u_char VIHL;
00701 u_char TOS;
00702 short TotLen;
00703 short ID;
00704 short FlagOff;
00705 u_char TTL;
00706 u_char Protocol;
00707 u_short Checksum;
00708 struct in_addr iaSrc;
00709 struct in_addr iaDst;
00710 };
00711 struct ICMPHDR {
00712 u_char Type;
00713 u_char Code;
00714 u_short Checksum;
00715 u_short ID;
00716 u_short Seq;
00717 char Data;
00718 };
00719
00720 struct Request {
00721 ICMPHDR icmpHdr;
00722 DWORD dwTime;
00723 char cData[32];
00724 };
00725 struct Reply {
00726 IPHDR ipHdr;
00727 Request echoRequest;
00728 char cFiller[256];
00729 };
00730
00731 if (INVALID_SOCKET == (rawSocket = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP))) {
00732 VERBOSE(VB_SOCKET, "Ping: can't create socket");
00733 return false;
00734 }
00735
00736 lpHost = gethostbyname(host);
00737 if (!lpHost) {
00738 VERBOSE(VB_SOCKET, "Ping: gethostbyname failed");
00739 closesocket(rawSocket);
00740 return false;
00741 }
00742
00743 saDest.sin_addr.s_addr = *((u_long FAR *) (lpHost->h_addr));
00744 saDest.sin_family = AF_INET;
00745 saDest.sin_port = 0;
00746
00747 Request echoReq;
00748 echoReq.icmpHdr.Type = ICMP_ECHOREQ;
00749 echoReq.icmpHdr.Code = 0;
00750 echoReq.icmpHdr.ID = 123;
00751 echoReq.icmpHdr.Seq = 456;
00752 for (unsigned i = 0; i < sizeof(echoReq.cData); i++)
00753 echoReq.cData[i] = ' ' + i;
00754 echoReq.dwTime = GetTickCount();
00755 echoReq.icmpHdr.Checksum = in_cksum((u_short *)&echoReq, sizeof(Request));
00756
00757 if (SOCKET_ERROR == sendto(rawSocket, (LPSTR)&echoReq, sizeof(Request), 0, (LPSOCKADDR)&saDest, sizeof(SOCKADDR_IN))) {
00758 VERBOSE(VB_SOCKET, "Ping: send failed");
00759 closesocket(rawSocket);
00760 return false;
00761 }
00762
00763 struct timeval Timeout;
00764 fd_set readfds;
00765 readfds.fd_count = 1;
00766 readfds.fd_array[0] = rawSocket;
00767 Timeout.tv_sec = timeout;
00768 Timeout.tv_usec = 0;
00769
00770 if (SOCKET_ERROR == select(1, &readfds, NULL, NULL, &Timeout)) {
00771 VERBOSE(VB_SOCKET, "Ping: timeout expired or select failed");
00772 closesocket(rawSocket);
00773 return false;
00774 }
00775
00776 closesocket(rawSocket);
00777 VERBOSE(VB_SOCKET, "Ping: done");
00778 return true;
00779 #else
00780 QString cmd = QString("ping -t %1 -c 1 %2 >/dev/null 2>&1")
00781 .arg(timeout).arg(host);
00782
00783 if (myth_system(cmd))
00784 {
00785
00786
00787 cmd = QString("ping -c 1 %2 >/dev/null 2>&1").arg(host);
00788
00789 if (myth_system(cmd))
00790 return false;
00791 }
00792
00793 return true;
00794 #endif
00795 }
00796
00800 bool telnet(const QString &host, int port)
00801 {
00802 MythSocket *s = new MythSocket();
00803
00804 if (s->connect(host, port))
00805 {
00806 s->close();
00807 return true;
00808 }
00809
00810 return false;
00811 }
00812
00834 long long copy(QFile &dst, QFile &src, uint block_size)
00835 {
00836 uint buflen = (block_size < 1024) ? (16 * 1024) : block_size;
00837 char *buf = new char[buflen];
00838 bool odst = false, osrc = false;
00839
00840 if (!buf)
00841 return -1LL;
00842
00843 if (!dst.isWritable() && !dst.isOpen())
00844 odst = dst.open(IO_Raw|IO_WriteOnly|IO_Truncate);
00845
00846 if (!src.isReadable() && !src.isOpen())
00847 osrc = src.open(IO_Raw|IO_ReadOnly);
00848
00849 bool ok = dst.isWritable() && src.isReadable();
00850 long long total_bytes = 0LL;
00851 while (ok)
00852 {
00853 long long rlen, wlen, off = 0;
00854 rlen = src.readBlock(buf, buflen);
00855 if (rlen<0)
00856 {
00857 VERBOSE(VB_IMPORTANT, "util.cpp:copy: read error");
00858 ok = false;
00859 break;
00860 }
00861 if (rlen==0)
00862 break;
00863
00864 total_bytes += (long long) rlen;
00865
00866 while ((rlen-off>0) && ok)
00867 {
00868 wlen = dst.writeBlock(buf + off, rlen - off);
00869 if (wlen>=0)
00870 off+= wlen;
00871 if (wlen<0)
00872 {
00873 VERBOSE(VB_IMPORTANT, "util.cpp:copy: write error");
00874 ok = false;
00875 }
00876 }
00877 }
00878 delete[] buf;
00879
00880 if (odst)
00881 dst.close();
00882
00883 if (osrc)
00884 src.close();
00885
00886 return (ok) ? total_bytes : -1LL;
00887 }
00888
00889 QString createTempFile(QString name_template, bool dir)
00890 {
00891 int ret = -1;
00892
00893 #ifdef USING_MINGW
00894 char temppath[MAX_PATH] = ".";
00895 char tempfilename[MAX_PATH] = "";
00896
00897 GetTempPathA(MAX_PATH, temppath);
00898 if (GetTempFileNameA(temppath, "mth", 0, tempfilename))
00899 {
00900 if (dir)
00901 ret = mkdir(tempfilename);
00902 else
00903 ret = open(tempfilename, O_CREAT | O_RDWR, S_IREAD | S_IWRITE);
00904 }
00905 QString tmpFileName(tempfilename);
00906 #else
00907 const char *tmp = name_template.ascii();
00908 char *ctemplate = strdup(tmp);
00909
00910 if (dir)
00911 {
00912 ret = (mkdtemp(ctemplate)) ? 0 : -1;
00913 }
00914 else
00915 {
00916 ret = mkstemp(ctemplate);
00917 }
00918
00919 QString tmpFileName(ctemplate);
00920 free(ctemplate);
00921 #endif
00922
00923 if (ret == -1)
00924 {
00925 VERBOSE(VB_IMPORTANT, QString("createTempFile(%1), Error ")
00926 .arg(name_template) + ENO);
00927 return name_template;
00928 }
00929
00930 if (!dir && (ret >= 0))
00931 close(ret);
00932
00933 return tmpFileName;
00934 }
00935
00936 double MythGetPixelAspectRatio(void)
00937 {
00938 float pixelAspect = 1.0;
00939 #ifdef USING_X11
00940 pixelAspect = MythXGetPixelAspectRatio();
00941 #endif // USING_X11
00942 return pixelAspect;
00943 }
00944
00945 unsigned long long myth_get_approximate_large_file_size(const QString &fname)
00946 {
00947
00948 QString filename = QDeepCopy<QString>(fname);
00949 #ifdef USING_MINGW
00950 struct _stati64 status;
00951 _stati64(filename.local8Bit(), &status);
00952 return status.st_size;
00953 #else
00954 struct stat status;
00955 stat(filename.local8Bit(), &status);
00956
00957
00958 unsigned long long bsize = status.st_blksize;
00959 unsigned long long nblk = status.st_blocks;
00960 unsigned long long approx_size = nblk * bsize;
00961 return approx_size;
00962 #endif
00963 }
00964
00965 bool IsPulseAudioRunning(void)
00966 {
00967 #if defined(CONFIG_DARWIN) || (__FreeBSD__) || defined(__OpenBSD__)
00968 const char *command = "ps -ax | grep -i pulseaudio | grep -v grep > /dev/null";
00969 #else
00970 const char *command = "ps -ae | grep pulseaudio > /dev/null";
00971 #endif
00972 bool res = myth_system(command,
00973 MYTH_SYSTEM_DONT_BLOCK_LIRC |
00974 MYTH_SYSTEM_DONT_BLOCK_JOYSTICK_MENU);
00975 return !res;
00976 }
00977
00978