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
|
/* handles the handing out of and uniqueness of window numbers.
* Copyright (C) 2000, 2001, 2002, 2003, 2004 Shawn Betts <sabetts@vcn.bc.ca>
*
* This file is part of ratpoison.
*
* ratpoison 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.
*
* ratpoison 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 software; see the file COPYING. If not, write to
* the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
* Boston, MA 02111-1307 USA
*/
#include <stdlib.h>
#include <stdio.h>
#include "ratpoison.h"
/* Initialize a numset structure. */
static void
numset_init (struct numset *ns)
{
ns->max_taken = 10;
ns->num_taken = 0;
ns->numbers_taken = xmalloc (ns->max_taken * sizeof (int));
}
static int
numset_num_is_taken (struct numset *ns, int n)
{
int i;
for (i=0; i<ns->num_taken; i++)
{
if (ns->numbers_taken[i] == n) return 1;
}
return 0;
}
/* returns index into numbers_taken that can be used. */
static int
numset_find_empty_cell (struct numset *ns)
{
int i;
for (i=0; i<ns->num_taken; i++)
{
if (ns->numbers_taken[i] == -1) return i;
}
/* no vacant ones, so grow the array. */
if (ns->num_taken >= ns->max_taken)
{
ns->max_taken *= 2;
ns->numbers_taken = xrealloc (ns->numbers_taken, sizeof (int) * ns->max_taken);
}
ns->num_taken++;
return ns->num_taken-1;
}
int
numset_add_num (struct numset *ns, int n)
{
PRINT_DEBUG(("ns=%p add_num %d\n", ns, n));
if (numset_num_is_taken (ns, n))
return 0; /* failed. */
ns->numbers_taken[numset_find_empty_cell(ns)] = n;
return 1; /* success! */
}
/* returns a unique number that can be used as the window number in
the program bar. */
int
numset_request (struct numset *ns)
{
int i;
/* look for a unique number, and add it to the list of taken
numbers. */
i = 0;
while (!numset_add_num (ns, i)) i++;
PRINT_DEBUG(("ns=%p request got %d\n", ns, i));
return i;
}
/* When a window is destroyed, it gives back its window number with
this function. */
void
numset_release (struct numset *ns, int n)
{
int i;
PRINT_DEBUG(("ns=%p release %d\n", ns, n));
if (n < 0)
PRINT_ERROR(("ns=%p Attempt to release %d!\n", ns, n));
for (i=0; i<ns->num_taken; i++)
{
if (ns->numbers_taken[i] == n)
{
ns->numbers_taken[i] = -1;
return;
}
}
}
/* Create a new numset and return a pointer to it. */
struct numset *
numset_new ()
{
struct numset *ns;
ns = (struct numset *)xmalloc (sizeof (struct numset));
numset_init (ns);
return ns;
}
/* Free a numset structure and it's internal data. */
void
numset_free (struct numset *ns)
{
free (ns->numbers_taken);
free (ns);
}
void
numset_clear (struct numset *ns)
{
ns->num_taken = 0;
}
|