File: gitg-platform-support-osx.c

package info (click to toggle)
gitg 41-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 18,876 kB
  • sloc: ansic: 1,636; ruby: 1,445; sh: 314; python: 261; xml: 57; makefile: 15
file content (275 lines) | stat: -rw-r--r-- 6,034 bytes parent folder | download | duplicates (2)
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
/*
 * This file is part of gitg
 *
 * Copyright (C) 2015 - Jesse van den Kieboom
 *
 * gitg 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 of the License, or
 * (at your option) any later version.
 *
 * gitg 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 gitg. If not, see <http://www.gnu.org/licenses/>.
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include "gitg-platform-support.h"

#import <AppKit/AppKit.h>
#include <gdk/gdkquartz.h>
#include <gio/gunixinputstream.h>

#include <sys/types.h>
#include <pwd.h>

gboolean
gitg_platform_support_use_native_window_controls (GdkDisplay *display)
{
	if (display == NULL)
	{
		display = gdk_display_get_default ();
	}

	return GDK_IS_QUARTZ_DISPLAY (display);
}

void
gitg_platform_support_http_get (GFile               *file,
                                GCancellable        *cancellable,
                                GAsyncReadyCallback  callback,
                                gpointer             user_data)
{
	@autoreleasepool
	{
		NSString *dataUrl = [NSString stringWithUTF8String:g_file_get_uri (file)];
		NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:dataUrl]];
		NSOperationQueue *queue = [[NSOperationQueue alloc] init];

		GTask *task = g_task_new (file, cancellable, callback, user_data);

		[NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
			if (g_task_return_error_if_cancelled (task))
			{
			}
			else if (error)
			{
				const gchar *message;

				message = [[error localizedDescription] UTF8String];

				g_task_return_new_error (task, G_IO_ERROR, G_IO_ERROR_FAILED, "%s", message);
			}
			else
			{
				GInputStream *stream;
				GBytes *bytes;

				bytes = g_bytes_new ([data bytes], [data length]);

				stream = g_memory_input_stream_new_from_bytes (bytes);
				g_bytes_unref (bytes);

				g_task_return_pointer (task, stream, NULL);
			}

			g_object_unref (task);
		}];
	}
}

GInputStream *
gitg_platform_support_http_get_finish (GAsyncResult  *result,
                                       GError       **error)
{
	return g_task_propagate_pointer (G_TASK (result), error);
}

cairo_surface_t *
gitg_platform_support_create_cursor_surface (GdkDisplay    *display,
                                             GdkCursorType  cursor_type,
                                             gdouble       *hot_x,
                                             gdouble       *hot_y,
                                             gdouble       *width,
                                             gdouble       *height)
{
	NSCursor *cursor;
	NSImage *image;
	NSBitmapImageRep *image_rep;
	const unsigned char *pixel_data;
	NSSize size;
	NSPoint hotspot;
	gint w, h;
	cairo_surface_t *surface, *target;
	cairo_t *ctx;

	@autoreleasepool
	{
		switch (cursor_type)
		{
		case GDK_HAND1:
			cursor = [NSCursor pointingHandCursor];
			break;
		default:
			cursor = [NSCursor arrowCursor];
			break;
		}

		image = [cursor image];

		image_rep = [[NSBitmapImageRep alloc] initWithData:[image TIFFRepresentation]];
		pixel_data = [image_rep bitmapData];

		w = [image_rep pixelsWide];
		h = [image_rep pixelsHigh];

		hotspot = [cursor hotSpot];
		size = [image size];

		if (hot_x)
		{
			*hot_x = hotspot.x;
		}

		if (hot_y)
		{
			*hot_y = hotspot.y;
		}

		if (width)
		{
			*width = size.width;
		}

		if (height)
		{
			*height = size.height;
		}

		surface = cairo_image_surface_create_for_data (pixel_data, CAIRO_FORMAT_ARGB32, w, h, [image_rep bytesPerRow]);
		target = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, size.width, size.height);

		ctx = cairo_create (target);

		cairo_scale (ctx, size.width / w, size.height / h);
		cairo_set_source_surface (ctx, surface, 0, 0);
		cairo_paint (ctx);
		cairo_destroy (ctx);

		cairo_surface_destroy (surface);

		return target;
	}
}

GInputStream *
gitg_platform_support_new_input_stream_from_fd (gint     fd,
                                                gboolean close_fd)
{
	return g_unix_input_stream_new (fd, close_fd);
}

static gchar *
get_resource_path (const gchar *fallback, ...)
{
	NSBundle *bundle;
	va_list vl;
	gchar *ret;
	GPtrArray *args;
	const gchar *arg;
	NSAutoreleasePool *pool;
	NSString *resource_path;

	@autoreleasepool
	{
		bundle = [NSBundle mainBundle];

		if (bundle == NULL || [bundle bundleIdentifier] == NULL)
		{
			return g_strdup (fallback);
		}

		resource_path = [bundle resourcePath];

		if (resource_path == NULL)
		{
			return g_strdup (fallback);
		}

		va_start (vl, fallback);
		args = g_ptr_array_new ();

		g_ptr_array_add (args, [resource_path UTF8String]);

		while ((arg = va_arg (vl, const gchar *)) != NULL)
		{
			g_ptr_array_add (args, arg);
		}

		va_end (vl);

		g_ptr_array_add (args, NULL);

		ret = g_build_filenamev (args->pdata);
		g_ptr_array_free (args, TRUE);

		return ret;
	}
}

gchar *
gitg_platform_support_get_lib_dir (void)
{
	return get_resource_path (GITG_LIBDIR, "lib", "gitg", NULL);
}

gchar *
gitg_platform_support_get_locale_dir (void)
{
	return get_resource_path (GITG_LOCALEDIR, "share", "locale", NULL);
}

gchar *
gitg_platform_support_get_data_dir (void)
{
	return get_resource_path (GITG_DATADIR, "share", "gitg", NULL);
}

gchar *
gitg_platform_support_get_user_home_dir (const gchar *name)
{
	struct passwd *pwd;

	if (name == NULL)
	{
		name = g_get_user_name ();
	}

	if (name == NULL)
	{
		return NULL;
	}

	pwd = getpwnam (name);

	if (pwd == NULL)
	{
		return NULL;
	}

	return g_strdup (pwd->pw_dir);
}

void
gitg_platform_support_application_support_prepare_startup (void)
{
}

// ex:ts=4 noet