Atrinik Client  4.0
misc.c
Go to the documentation of this file.
1 /*************************************************************************
2  * Atrinik, a Multiplayer Online Role Playing Game *
3  * *
4  * Copyright (C) 2009-2014 Alex Tokar and Atrinik Development Team *
5  * *
6  * Fork from Crossfire (Multiplayer game for X-windows). *
7  * *
8  * This program is free software; you can redistribute it and/or modify *
9  * it under the terms of the GNU General Public License as published by *
10  * the Free Software Foundation; either version 2 of the License, or *
11  * (at your option) any later version. *
12  * *
13  * This program is distributed in the hope that it will be useful, *
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16  * GNU General Public License for more details. *
17  * *
18  * You should have received a copy of the GNU General Public License *
19  * along with this program; if not, write to the Free Software *
20  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *
21  * *
22  * The author can be reached at admin@atrinik.org *
23  ************************************************************************/
24 
30 #include <global.h>
31 
37 void browser_open(const char *url)
38 {
39 #if defined(WIN32)
40  ShellExecute(NULL, "open", url, NULL, NULL, SW_SHOWDEFAULT);
41 #elif defined(__GNUC__)
42  char buf[HUGE_BUF];
43 
44  snprintf(buf, sizeof(buf), "xdg-open \"%s\"", url);
45 
46  if (system(buf) != 0) {
47  snprintf(buf, sizeof(buf), "x-www-browser \"%s\"", url);
48 
49  if (system(buf) != 0) {
50  LOG(BUG, "Could not open '%s'.", url);
51  }
52  }
53 #else
54  LOG(DEBUG, "Unknown platform, cannot open '%s'.", url);
55 #endif
56 }
57 
69 char *package_get_version_full(char *dst, size_t dstlen)
70 {
71 #if PACKAGE_VERSION_PATCH == 0
72  package_get_version_partial(dst, dstlen);
73 #else
74  snprintf(dst, dstlen, "%d.%d.%d", PACKAGE_VERSION_MAJOR, PACKAGE_VERSION_MINOR, PACKAGE_VERSION_PATCH);
75 #endif
76  return dst;
77 }
78 
89 char *package_get_version_partial(char *dst, size_t dstlen)
90 {
91  /* Upgrader version will overrule the package version if the upgrader
92  * is currently doing its job. */
93  if (upgrader_get_version_partial(dst, dstlen)) {
94  return dst;
95  }
96 
97  snprintf(dst, dstlen, "%d.%d", PACKAGE_VERSION_MAJOR, PACKAGE_VERSION_MINOR);
98  return dst;
99 }
100 
108 int bmp2png(const char *path)
109 {
110 #if defined(__GNUC__) && !defined(WIN32)
111  char buf[HUGE_BUF];
112 
113  snprintf(buf, sizeof(buf), "convert \"%s\" \"`echo \"%s\" | sed -e 's/.bmp/.png/'`\" && rm \"%s\"", path, path, path);
114 
115  if (system(buf) != 0) {
116  LOG(INFO, "Could not convert %s from BMP to PNG.", path);
117  return 0;
118  }
119 
120  return 1;
121 #else
122  (void) path;
123  return 0;
124 #endif
125 }
126 
132 void screenshot_create(SDL_Surface *surface)
133 {
134  char path[HUGE_BUF], timebuf[MAX_BUF];
135  struct timeval tv;
136  struct tm *tm;
137 
138  if (!surface) {
139  return;
140  }
141 
142  gettimeofday(&tv, NULL);
143  tm = localtime(&tv.tv_sec);
144 
145  if (tm) {
146  char timebuf2[MAX_BUF];
147 
148  strftime(timebuf2, sizeof(timebuf2), "%Y-%m-%d-%H-%M-%S", tm);
149  snprintf(timebuf, sizeof(timebuf), "%s-%06"PRIu64, timebuf2, (uint64_t) tv.tv_usec);
150  } else {
151  draw_info(COLOR_RED, "Could not get time information.");
152  return;
153  }
154 
155  snprintf(path, sizeof(path), "%s/.atrinik/screenshots/Atrinik-%s.bmp", get_config_dir(), timebuf);
156  mkdir_ensure(path);
157 
158  if (SDL_SaveBMP(surface, path) == 0) {
159  draw_info_format(COLOR_GREEN, "Saved screenshot as %s successfully.", path);
160 
161  if (bmp2png(path)) {
162  draw_info(COLOR_GREEN, "Converted to PNG successfully.");
163  }
164  } else {
165  draw_info_format(COLOR_RED, "Failed to write screenshot data (path: %s).", path);
166  }
167 }
void mkdir_ensure(const char *path)
Definition: wrapper.c:130
void draw_info(const char *color, const char *str)
Definition: textwin.c:448
void browser_open(const char *url)
Definition: misc.c:37
char * package_get_version_partial(char *dst, size_t dstlen)
Definition: misc.c:89
#define COLOR_RED
Definition: text.h:295
#define COLOR_GREEN
Definition: text.h:297
char * package_get_version_full(char *dst, size_t dstlen)
Definition: misc.c:69
int bmp2png(const char *path)
Definition: misc.c:108
char * upgrader_get_version_partial(char *dst, size_t dstlen)
Definition: upgrader.c:332
void draw_info_format(const char *color, const char *format,...)
Definition: textwin.c:429
const char * get_config_dir(void)
Definition: wrapper.c:303
void screenshot_create(SDL_Surface *surface)
Definition: misc.c:132