summaryrefslogtreecommitdiff
path: root/libgame/game-portable.c
blob: fa2dfaeca68ddb32f93cc762aeac72f2ad91f0e1 (plain)
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
/*
 *  Copyright (C) 2003 Benjamin Otte <otte@gnome.org>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2, or (at your option)
 *  any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 *  $Id$
 */

#include "game-private.h"
#include "game-portable.h"

#ifdef G_OS_WIN32
#  include <windows.h>
#  include <winsock2.h>
#else
#  include <stdio.h>
#  include <sys/stat.h>
#  include <unistd.h>
#  include <pwd.h>
#  include <stdlib.h>
#  ifdef HAVE_NETINET_TCP_H
#    include <netinet/tcp.h>
#  endif
#  ifdef HAVE_NETINET_IN_H
#    include <netinet/in.h>
#  endif
#endif

gboolean
game_gnet_set_nonblock (GIOChannel *channel)
{
#ifdef G_OS_WIN32
  int fd = g_io_channel_win32_get_fd (channel);
  gulong mode = 1;
  return ioctlsocket (fd, FIONBIO, &mode) == 0;
#else
  return (g_io_channel_set_flags (channel, G_IO_FLAG_NONBLOCK, NULL)
      == G_IO_STATUS_NORMAL);
#endif
}
void
game_gnet_set_nodelay (GIOChannel *channel)
{
#ifdef TCP_NODELAY
  int fd, on = 1;
#ifdef G_OS_WIN32
  fd = g_io_channel_win32_get_fd (channel);
#else
  fd = g_io_channel_unix_get_fd (channel);
#endif
  setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (void *) &on, sizeof(on));
#endif
}

const char *
game_get_plugin_dir (void)
{
  static gchar *dir = NULL;
  
  if (dir == NULL) {
#ifdef G_OS_WIN32
    dir = g_win32_get_package_installation_subdirectory ("libgame",
	"libgame", "plugins");
#else
    dir = g_build_filename (LIBDIR, LIBGAME_NAME, NULL);
#endif
  }

  return dir;
}

const char *
game_get_high_score_dir (void)
{
  static gchar *dir = NULL;
  
  if (dir == NULL) {
#ifdef G_OS_WIN32
    dir = g_win32_get_package_installation_subdirectory ("libgame",
	"libgame", "scores");
#else
    dir = g_build_filename (HIGHSCOREDIR, NULL);
#endif
  }

  g_assert (dir);
  if (!g_file_test (dir, G_FILE_TEST_IS_DIR) && 
      g_mkdir_with_parents (dir, GAME_PERMISSION_REPLAY_DIR) != 0) {
    g_free (dir);
    return NULL;
  }
  return dir;
}

const char *
game_get_data_dir (void)
{
  static gchar *dir = NULL;
  
  if (dir == NULL) {
#ifdef G_OS_WIN32
    dir = g_win32_get_package_installation_subdirectory ("libgame",
	"libgame", "data");
#else
    dir = g_build_filename (LIBGAME_DATADIR, NULL);
#endif
  }

  g_assert (dir);
  if (!g_file_test (dir, G_FILE_TEST_IS_DIR) && 
      g_mkdir_with_parents (dir, GAME_PERMISSION_REPLAY_DIR) != 0) {
    g_free (dir);
    return NULL;
  }
  return dir;
}

gboolean
game_file_copy (const gchar *from, const gchar *to)
{
#ifdef G_OS_WIN32
  return CopyFile (from, to, FALSE) ? TRUE : FALSE;
#else
  gboolean ret;
  gchar *command;

  command = g_strdup_printf ("cp \"%s\" \"%s\"", from, to);
  ret = system (command) == 0;
  g_free (command);

  return ret ? TRUE : FALSE;
#endif
}

glong
game_time_val_difference (GTimeVal *to, GTimeVal *from)
{
  glong diff;

  g_return_val_if_fail (to != NULL, 0);
  g_return_val_if_fail (from != NULL, 0);

  /* needs bounds checking */
  diff = (to->tv_sec - from->tv_sec) * G_USEC_PER_SEC;
  diff += to->tv_usec - from->tv_usec;

  return diff;
}