File: 3ddesk.cpp

package info (click to toggle)
3ddesktop 0.2.8-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 1,096 kB
  • ctags: 686
  • sloc: cpp: 5,996; sh: 3,222; makefile: 78
file content (352 lines) | stat: -rw-r--r-- 11,535 bytes parent folder | download
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
//  ----------------------------------------------------------
//
//  Copyright (C) 2002 Brad Wasson <bard@systemtoolbox.com>
//
//  This file is part of 3ddesktop.
//
//  3ddesktop 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.
//
//  3ddesktop 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 3ddesktop; see the file COPYING.   If not, write to
//  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
//
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <string.h>
#include <getopt.h>
#include <sys/ipc.h>
#include <sys/sem.h>


#include "3ddesk.h"
#include "message.hpp"

#ifdef printf
#undef printf
#endif

static int start_daemon(void);
static int server_running(void);
static int wait_for_server_to_start(void);

static void 
usage_and_bye (void)
{
    printf ("usage: 3ddesk [ OPTIONS ]\n\n"
            "Activates the 3d Desktop.  3ddeskd daemon must be running.\n"
            "Where OPTIONS are:\n"
            "     --view=xxx        Uses the options from the view in 3ddesktop.conf\n"
            "     --mode=xxx        Sets the arrangement mode \n"
            "                        (one of carousel, cylinder, linear, viewmaster,\n"
            "                         priceisright, flip, or random)\n"
            "     --acquire[=#]     Grab images for all the desktops by cycling thru\n"
            "                        (sleep for x millisecs at each screen for refresh)\n"
            "     --acquirecurrent  Grab image for current desktop\n"
            "     --nozoom          Disable the zoom out\n"
            "     --gotoright       Goto the desktop to the right\n"
            "     --gotoleft        Goto the desktop to the left\n"
            "     --gotoup          Goto the desktop to the up\n"
            "     --gotodown        Goto the desktop to the down\n"
            "     --goto=#          Goto specified column (deprecated)\n"
            "     --gotocolumn=#    Goto specified column\n"
            "     --gotorow=#       Goto specified row\n"
            "     --dontexit        Don't exit after a goto\n"
            "     --stop            Stop 3ddesktop (kill 3ddeskd daemon)\n"
            "     --reload          Force a reload of 3ddesktop.conf\n"
	    "     --noautofun       Don't Automatically turn on Fun Mode\n"
            "     --revmousewheel   Reverse the mousewheel\n"
            "     --swapmousebuttons Swap the mousebuttons\n"
            "     --altmousebuttons Use alternate mousebuttons scheme\n"
            "     --justswitch      Just switch desktops and acquire without graphics\n\n"
        );
    exit (1);
}

int verbose = 0;

int main(int argc, char **argv)
{

    MessageManager msgmgr;
    
    static struct option long_options[] = {
        {"nozoom", 0, 0, 0},
        {"goto", 1, 0, 0},
        {"gotoright", 0, 0, 0},
        {"gotoleft", 0, 0, 0},
        {"gotoup", 0, 0, 0},
        {"gotodown", 0, 0, 0},
        {"mode", 1, 0, 0},
        {"acquire", 2, 0, 0},
        {"acquirecurrent", 0, 0, 0},
        {"stop", 0, 0, 0},
        {"gotocolumn", 1, 0, 0},
        {"gotorow", 1, 0, 0},
        {"view", 1, 0, 0},
        {"zoom", 0, 0, 0},
        {"reload", 0, 0, 0},
        {"changespeed", 1, 0, 0},
        {"zoomspeed", 1, 0, 0},
        {"dontexit", 0, 0, 0},
	{"noautofun", 0, 0, 0},
	{"revmousewheel", 0, 0, 0},
	{"swapmousebuttons", 0, 0, 0},
	{"altmousebuttons", 0, 0, 0},
	{"justswitch", 0, 0, 0},
        {0, 0, 0, 0}
    };

    float tmp;
    int c;
    int option_index;
    while (1) {

        c = getopt_long (argc, argv, "v",
                         long_options, &option_index);
        if (c == -1)
            break;

        switch (c) {
        case 0: // long option

            switch (option_index) {
            case 0: // 'nozoom'
                msgmgr.msg.zoom = 0;
                break;
            case 1: // 'goto'  ---- use is deprecated (in favor of gotocolumn)
                msgmgr.msg.goto_column = atoi (optarg);
                if (msgmgr.msg.goto_column < 1) 
                    usage_and_bye();
                break;
            case 2: // 'gotoright'
                msgmgr.msg.goto_column = GOTO_FACE_RIGHT;
                break;
            case 3: // 'gotoleft'
                msgmgr.msg.goto_column = GOTO_FACE_LEFT;
                break;
            case 4: // 'gotonext'
                msgmgr.msg.goto_row = GOTO_FACE_UP;
                break;
            case 5: // 'gotoprev'
                msgmgr.msg.goto_row = GOTO_FACE_DOWN;
                break;
            case 6: // 'mode'
                if (strcmp(optarg, "cylinder") == 0) {
                    msgmgr.msg.face_type = FACE_TYPE_CYLINDER;
                } else if (strcmp(optarg, "linear") == 0) {
                    msgmgr.msg.face_type = FACE_TYPE_LINEAR;
                } else if (strcmp(optarg, "viewmaster") == 0) {
                    msgmgr.msg.face_type = FACE_TYPE_VIEWMASTER;
                } else if (strcmp(optarg, "carousel") == 0) {
                    msgmgr.msg.face_type = FACE_TYPE_CAROUSEL;
                } else if (strcmp(optarg, "priceisright") == 0) {
                    msgmgr.msg.face_type = FACE_TYPE_PRICEISRIGHT;
                } else if (strcmp(optarg, "flip") == 0) {
                    msgmgr.msg.face_type = FACE_TYPE_FLIP;
                } else if (strcmp(optarg, "random") == 0) {
                    msgmgr.msg.face_type = FACE_TYPE_RANDOM;
                } else {
                    usage_and_bye();
                }
                break;
            case 7: // 'acquire'
                msgmgr.msg.mtype = MTYPE_ACQUIRE;
                if (optarg) {
                    int tmpint = atoi (optarg);
                    // must be between 10ms and 3sec for now
                    if (10 < tmpint && tmpint < 3000)
                        msgmgr.msg.acquire_sleep = tmpint;
                } else {
                    msgmgr.msg.acquire_sleep = 700;
                }
                break;
            case 8: // 'acquirecurrent'
                msgmgr.msg.mtype = MTYPE_ACQUIRE_CURRENT;
                break;
            case 9: // 'stop'
                msgmgr.msg.mtype = MTYPE_STOP;
                printf ("Stopping 3ddesktop daemon...\n");
                break;
            case 10: // 'gotocolumn'
                msgmgr.msg.goto_column = atoi (optarg);
                if (msgmgr.msg.goto_column < 1) 
                    usage_and_bye();
                break;
            case 11: // 'gotorow'
                msgmgr.msg.goto_row = atoi (optarg);
                if (msgmgr.msg.goto_row < 1) 
                    usage_and_bye();
                break;
            case 12: // 'view'
                strcpy(msgmgr.msg.view, optarg);
                break;
            case 13: // 'zoom'
                msgmgr.msg.zoom = 1;
                break;
            case 14: // 'reload'
                msgmgr.msg.reload = 1;
                break;
            case 15: // 'changespeed'
                tmp = atof(optarg);
                if (tmp >= 1.0 && tmp <= 300.0) {
                  msgmgr.msg.face_change_steps = tmp;
                }
                break;
            case 16: // 'zoomspeed'
                tmp = atof(optarg);
                if (tmp >= 1.0 && tmp <= 300.0) {
                  msgmgr.msg.zoom_steps = tmp;
                }
                break;
            case 17: // 'dontexit'
                msgmgr.msg.disable_exit_after_goto = 1;
                break;
	    case 18: // 'noautofun'
		msgmgr.msg.disable_random_fun = 1;
                break;
	    case 19: // 'revmousewheel'
		msgmgr.msg.reverse_mousewheel = 1;
                break;
	    case 20: // 'swapmousebuttons'
		msgmgr.msg.swap_mousebuttons = 1;
                break;
	    case 21: // 'altmousebuttons'
		msgmgr.msg.alt_mousebuttons = 1;
                break;
            case 22: // 'justswitch'
                msgmgr.msg.mtype = MTYPE_JUSTSWITCH;
                break;
            }
            break;
        case 'v':
            verbose = 1;
            break;
        default:
            usage_and_bye();
            break;
        }
    }


    if (msgmgr.open() < 0 || !server_running()) {
        if (msgmgr.msg.mtype == MTYPE_STOP) {
            // already stopped...
            fprintf(stderr, "Server not running.\n");
            exit (1);
        }

        fprintf(stderr, "Attempting to start 3ddesktop server.\n");
  
        if (start_daemon() < 0) {
            fprintf(stderr, "Could not start server.\n"
                    "Try starting manually (3ddeskd)\n\n");
            exit(1);
        }

        if (wait_for_server_to_start() == 0) {
            if (msgmgr.open() < 0) {
                fprintf(stderr, "Can't get message queue: %s\n", strerror(errno));
                fprintf(stderr, "Maybe 3ddeskd server not started?\n");
                exit (1);
            }
        } else {
            fprintf(stderr, "Could not find server.\n"
                    "Try starting manually (3ddeskd)\n\n");
            exit (1);
        }
    }

    if (msgmgr.send() < 0) {
        fprintf(stderr, "Failed to send server a message\n");
        exit (1);
    }

    return 0;
}

static int
start_daemon()
{
    int pid;

    // since 3ddeskd puts itself in the background we could just use
    // system here I think but this to me is more direct

    pid = fork();

    if (pid < 0) {  // fork error
        fprintf(stderr, "Fork failed: %s\n", strerror(errno));
        return -1;
    }

    if (!pid) {  // child
        // 3ddeskd should be in path or current dir
        execlp("3ddeskd", "3ddeskd", NULL);
        execlp("./3ddeskd", "3ddeskd", NULL);
        execlp("/usr/bin/3ddeskd", "3ddeskd", NULL);
        execlp("/usr/local/bin/3ddeskd", "3ddeskd", NULL);
        fprintf(stderr, "Failed to execute 3ddeskd: %s\n", strerror(errno));
        return -1;
    }
    return 0;
}

static int 
server_running(void)
{
    int semid;
    key_t key = ftok("/bin/sh", 99);
    if ((key >= 0)
        && ((semid = semget(key, 1, 0666)) >= 0)
        && (semctl(semid, 0, GETVAL, NULL) > 0))
        return true;

    return false;
}


// returns 0 for server was found to be started
// returns -1 for error or server was not found to have started
static int
wait_for_server_to_start(void)
{
    key_t key = ftok("/bin/sh", 99);
    if (key < 0) {
        fprintf(stderr, "wait_for_server: ftok failed: %d: %s\n", errno, strerror(errno));
        return -1;
    }

    struct timeval tm;

    // poll the demon semaphore for a few seconds at most
#define SECONDS_TO_WAIT 5

    int i;
    for (i = 0; i < SECONDS_TO_WAIT * 100; i++) {
        if (server_running())
            break;
        
        tm.tv_sec = 0;
        tm.tv_usec = 10000;
        select (0, 0, 0, 0, &tm); // sleep 
    }

    if (i == SECONDS_TO_WAIT * 100) {
        printf("Server not found after waiting %d seconds.\n", SECONDS_TO_WAIT);
        return -1; // timeout for server
    } 

    return 0;
}