Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2014 The Chromium OS Authors. All rights reserved. |
| 3 | * Use of this source code is governed by a BSD-style license that can be |
| 4 | * found in the LICENSE file. |
| 5 | */ |
| 6 | |
| 7 | #ifndef UTIL_H |
| 8 | #define UTIL_H |
| 9 | |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 10 | #include <stdio.h> |
| 11 | #include <stdlib.h> |
| 12 | #include <stdarg.h> |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 13 | #include <stdint.h> |
David Sodman | e46ea8a | 2015-03-13 15:47:12 -0700 | [diff] [blame] | 14 | #include <stdbool.h> |
Chris Morin | 255d9f0 | 2019-02-04 23:29:27 -0800 | [diff] [blame] | 15 | #include <syslog.h> |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 16 | #include <time.h> |
Chris Morin | 255d9f0 | 2019-02-04 23:29:27 -0800 | [diff] [blame] | 17 | #include <unistd.h> |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 18 | |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 19 | #define MAX(A, B) ((A) > (B) ? (A) : (B)) |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 20 | #define MIN(A, B) ((A) < (B) ? (A) : (B)) |
| 21 | |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 22 | #define ARRAY_SIZE(A) (sizeof(A)/sizeof(*(A))) |
| 23 | |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 24 | #define MS_PER_SEC (1000LL) |
| 25 | #define NS_PER_SEC (1000LL * 1000LL * 1000LL) |
| 26 | #define NS_PER_MS (NS_PER_SEC / MS_PER_SEC); |
| 27 | |
| 28 | /* Returns the current CLOCK_MONOTONIC time in milliseconds. */ |
Manoj Gupta | 755f3ca | 2019-05-09 18:23:24 -0700 | [diff] [blame] | 29 | static inline int64_t get_monotonic_time_ms() { |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 30 | struct timespec spec; |
| 31 | clock_gettime(CLOCK_MONOTONIC, &spec); |
| 32 | return MS_PER_SEC * spec.tv_sec + spec.tv_nsec / NS_PER_MS; |
| 33 | } |
| 34 | |
Chris Morin | 255d9f0 | 2019-02-04 23:29:27 -0800 | [diff] [blame] | 35 | #define LOG(severity, fmt, ...) (fprintf(stderr, "<%i>frecon[%d]: " fmt "\n", \ |
| 36 | severity, getpid(), ##__VA_ARGS__)) |
| 37 | |
Dominik Behr | 09f2b72 | 2016-10-05 12:02:04 -0700 | [diff] [blame] | 38 | void daemonize(bool wait_child); |
| 39 | void daemon_exit_code(char code); |
Stéphane Marchesin | 00ff187 | 2015-12-14 13:40:09 -0800 | [diff] [blame] | 40 | void parse_location(char* loc_str, int* x, int* y); |
| 41 | void parse_filespec(char* filespec, char* filename, |
| 42 | int32_t* offset_x, int32_t* offset_y, uint32_t* duration, |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 43 | uint32_t default_duration, int32_t default_x, int32_t default_y); |
| 44 | void parse_image_option(char* optionstr, char** name, char** val); |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 45 | |
Dominik Behr | 2b9f123 | 2016-08-02 01:11:12 -0700 | [diff] [blame] | 46 | /* make sure stdio file descriptors are somewhat sane */ |
| 47 | void fix_stdio(void); |
Dominik Behr | da6df41 | 2016-08-02 12:56:42 -0700 | [diff] [blame] | 48 | bool write_string_to_file(const char *path, const char *s); |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 49 | |
Chris Morin | 255d9f0 | 2019-02-04 23:29:27 -0800 | [diff] [blame] | 50 | #define ERROR (LOG_ERR) |
| 51 | #define WARNING (LOG_WARNING) |
| 52 | #define INFO (LOG_INFO) |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 53 | |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 54 | #endif |